2 * Server functions which handle file transfers and room directories.
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
21 # include <sys/time.h>
28 #include <libcitadel.h>
33 #include "sysdep_decls.h"
37 #include "citserver.h"
44 #include "ctdl_module.h"
48 * network_talking_to() -- concurrency checker
50 int network_talking_to(char *nodename, int operation) {
52 static char *nttlist = NULL;
58 begin_critical_section(S_NTTLIST);
63 if (nttlist == NULL) nttlist = strdup("");
64 if (nttlist == NULL) break;
65 nttlist = (char *)realloc(nttlist,
66 (strlen(nttlist) + strlen(nodename) + 3) );
68 strcat(nttlist, nodename);
72 if (nttlist == NULL) break;
73 if (IsEmptyStr(nttlist)) break;
74 ptr = malloc(strlen(nttlist));
75 if (ptr == NULL) break;
77 for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
78 extract_token(buf, nttlist, i, '|', sizeof buf);
79 if ( (!IsEmptyStr(buf))
80 && (strcasecmp(buf, nodename)) ) {
90 if (nttlist == NULL) break;
91 if (IsEmptyStr(nttlist)) break;
92 for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
93 extract_token(buf, nttlist, i, '|', sizeof buf);
94 if (!strcasecmp(buf, nodename)) ++retval;
99 if (nttlist != NULL) CtdlLogPrintf(CTDL_DEBUG, "nttlist=<%s>\n", nttlist);
100 end_critical_section(S_NTTLIST);
108 * Server command to delete a file from a room's directory
110 void cmd_delf(char *filename)
115 if (CtdlAccessCheck(ac_room_aide))
118 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
119 cprintf("%d No directory in this room.\n",
124 if (IsEmptyStr(filename)) {
125 cprintf("%d You must specify a file name.\n",
126 ERROR + FILE_NOT_FOUND);
129 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
130 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
134 snprintf(pathname, sizeof pathname,
137 CC->room.QRdirname, filename);
138 a = unlink(pathname);
140 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
143 cprintf("%d File '%s' not found.\n",
144 ERROR + FILE_NOT_FOUND, pathname);
152 * move a file from one room directory to another
154 void cmd_movf(char *cmdbuf)
156 char filename[PATH_MAX];
157 char pathname[PATH_MAX];
158 char newpath[PATH_MAX];
159 char newroom[ROOMNAMELEN];
162 struct ctdlroom qrbuf;
165 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
166 extract_token(newroom, cmdbuf, 1, '|', sizeof newroom);
168 if (CtdlAccessCheck(ac_room_aide)) return;
170 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
171 cprintf("%d No directory in this room.\n",
176 if (IsEmptyStr(filename)) {
177 cprintf("%d You must specify a file name.\n",
178 ERROR + FILE_NOT_FOUND);
182 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
183 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
187 snprintf(pathname, sizeof pathname, "./files/%s/%s",
188 CC->room.QRdirname, filename);
189 if (access(pathname, 0) != 0) {
190 cprintf("%d File '%s' not found.\n",
191 ERROR + FILE_NOT_FOUND, pathname);
195 if (CtdlGetRoom(&qrbuf, newroom) != 0) {
196 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
199 if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
200 cprintf("%d '%s' is not a directory room.\n",
201 ERROR + NOT_HERE, qrbuf.QRname);
204 snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
206 if (link(pathname, newpath) != 0) {
207 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR,
213 /* this is a crude method of copying the file description */
214 snprintf(buf, sizeof buf,
215 "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir",
216 CC->room.QRdirname, filename, qrbuf.QRdirname);
218 cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
223 * This code is common to all commands which open a file for downloading,
224 * regardless of whether it's a file from the directory, an image, a network
225 * spool file, a MIME attachment, etc.
226 * It examines the file and displays the OK result code and some information
227 * about the file. NOTE: this stuff is Unix dependent.
229 void OpenCmdResult(char *filename, const char *mime_type)
235 fstat(fileno(CC->download_fp), &statbuf);
236 filesize = (long) statbuf.st_size;
237 modtime = (time_t) statbuf.st_mtime;
239 cprintf("%d %ld|%ld|%s|%s\n",
240 CIT_OK, filesize, (long)modtime, filename, mime_type);
245 * open a file for downloading
247 void cmd_open(char *cmdbuf)
250 char pathname[PATH_MAX];
253 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
255 if (CtdlAccessCheck(ac_logged_in)) return;
257 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
258 cprintf("%d No directory in this room.\n",
263 if (IsEmptyStr(filename)) {
264 cprintf("%d You must specify a file name.\n",
265 ERROR + FILE_NOT_FOUND);
269 if (CC->download_fp != NULL) {
270 cprintf("%d You already have a download file open.\n",
271 ERROR + RESOURCE_BUSY);
275 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
276 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
281 snprintf(pathname, sizeof pathname,
284 CC->room.QRdirname, filename);
285 CC->download_fp = fopen(pathname, "r");
287 if (CC->download_fp == NULL) {
288 cprintf("%d cannot open %s: %s\n",
289 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
293 OpenCmdResult(filename, "application/octet-stream");
299 void cmd_oimg(char *cmdbuf)
302 char pathname[PATH_MAX];
303 char MimeTestBuf[32];
304 struct ctdluser usbuf;
305 char which_user[USERNAME_SIZE];
310 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
312 if (IsEmptyStr(filename)) {
313 cprintf("%d You must specify a file name.\n",
314 ERROR + FILE_NOT_FOUND);
318 if (CC->download_fp != NULL) {
319 cprintf("%d You already have a download file open.\n",
320 ERROR + RESOURCE_BUSY);
324 if (!strcasecmp(filename, "_userpic_")) {
325 extract_token(which_user, cmdbuf, 1, '|', sizeof which_user);
326 if (CtdlGetUser(&usbuf, which_user) != 0) {
327 cprintf("%d No such user.\n",
328 ERROR + NO_SUCH_USER);
331 snprintf(pathname, sizeof pathname,
335 } else if (!strcasecmp(filename, "_floorpic_")) {
336 which_floor = extract_int(cmdbuf, 1);
337 snprintf(pathname, sizeof pathname,
339 ctdl_image_dir, which_floor);
340 } else if (!strcasecmp(filename, "_roompic_")) {
341 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
343 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
344 filename[a] = tolower(filename[a]);
345 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
349 snprintf(pathname, sizeof pathname,
355 CC->download_fp = fopen(pathname, "rb");
356 if (CC->download_fp == NULL) {
357 strcat(pathname, ".gif");
358 CC->download_fp = fopen(pathname, "rb");
360 if (CC->download_fp == NULL) {
361 cprintf("%d Cannot open %s: %s\n",
362 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
365 rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
366 rewind (CC->download_fp);
367 OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
371 * open a file for uploading
373 void cmd_uopn(char *cmdbuf)
377 extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
378 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
379 extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
381 if (CtdlAccessCheck(ac_logged_in)) return;
383 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
384 cprintf("%d No directory in this room.\n",
389 if (IsEmptyStr(CC->upl_file)) {
390 cprintf("%d You must specify a file name.\n",
391 ERROR + FILE_NOT_FOUND);
395 if (CC->upload_fp != NULL) {
396 cprintf("%d You already have a upload file open.\n",
397 ERROR + RESOURCE_BUSY);
401 for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
402 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
403 CC->upl_file[a] = '_';
406 snprintf(CC->upl_path, sizeof CC->upl_path,
409 CC->room.QRdirname, CC->upl_file);
410 snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
415 CC->upload_fp = fopen(CC->upl_path, "r");
416 if (CC->upload_fp != NULL) {
417 fclose(CC->upload_fp);
418 CC->upload_fp = NULL;
419 cprintf("%d '%s' already exists\n",
420 ERROR + ALREADY_EXISTS, CC->upl_path);
424 CC->upload_fp = fopen(CC->upl_path, "wb");
425 if (CC->upload_fp == NULL) {
426 cprintf("%d Cannot open %s: %s\n",
427 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
430 cprintf("%d Ok\n", CIT_OK);
436 * open an image file for uploading
438 void cmd_uimg(char *cmdbuf)
440 int is_this_for_real;
445 if (num_parms(cmdbuf) < 2) {
446 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
450 is_this_for_real = extract_int(cmdbuf, 0);
451 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
452 extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
453 if (CC->upload_fp != NULL) {
454 cprintf("%d You already have an upload file open.\n",
455 ERROR + RESOURCE_BUSY);
459 strcpy(CC->upl_path, "");
461 for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
462 basenm[a] = tolower(basenm[a]);
463 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
468 if (CC->user.axlevel >= AxAideU) {
469 snprintf(CC->upl_path, sizeof CC->upl_path,
475 if (!strcasecmp(basenm, "_userpic_")) {
476 snprintf(CC->upl_path, sizeof CC->upl_path,
482 if ((!strcasecmp(basenm, "_floorpic_"))
483 && (CC->user.axlevel >= AxAideU)) {
484 which_floor = extract_int(cmdbuf, 2);
485 snprintf(CC->upl_path, sizeof CC->upl_path,
491 if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
492 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
495 if (IsEmptyStr(CC->upl_path)) {
496 cprintf("%d Higher access required.\n",
497 ERROR + HIGHER_ACCESS_REQUIRED);
501 if (is_this_for_real == 0) {
502 cprintf("%d Ok to send image\n", CIT_OK);
506 CC->upload_fp = fopen(CC->upl_path, "wb");
507 if (CC->upload_fp == NULL) {
508 cprintf("%d Cannot open %s: %s\n",
509 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
512 cprintf("%d Ok\n", CIT_OK);
513 CC->upload_type = UPL_IMAGE;
518 * close the download file
520 void cmd_clos(char *cmdbuf)
524 if (CC->download_fp == NULL) {
525 cprintf("%d You don't have a download file open.\n",
526 ERROR + RESOURCE_NOT_OPEN);
530 fclose(CC->download_fp);
531 CC->download_fp = NULL;
533 if (CC->dl_is_net == 1) {
535 snprintf(buf, sizeof buf,
542 cprintf("%d Ok\n", CIT_OK);
549 void abort_upl(CitContext *who)
551 if (who->upload_fp != NULL) {
552 fclose(who->upload_fp);
553 who->upload_fp = NULL;
554 unlink(CC->upl_path);
561 * close the upload file
563 void cmd_ucls(char *cmd)
566 char upload_notice[512];
569 if (CC->upload_fp == NULL) {
570 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
574 fclose(CC->upload_fp);
575 CC->upload_fp = NULL;
577 if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
578 cprintf("%d Upload completed.\n", CIT_OK);
580 if (CC->upload_type == UPL_NET) {
581 char final_filename[PATH_MAX];
582 snprintf(final_filename, sizeof final_filename,
590 if (link(CC->upl_path, final_filename) == 0) {
591 unlink(CC->upl_path);
594 CtdlLogPrintf(CTDL_ALERT, "Cannot link %s to %s: %s\n",
595 CC->upl_path, final_filename, strerror(errno)
599 /* FIXME ... here we need to trigger a network run */
602 CC->upload_type = UPL_FILE;
606 if (!strcasecmp(cmd, "1")) {
607 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
608 fp = fopen(CC->upl_filedir, "a");
610 fp = fopen(CC->upl_filedir, "w");
613 fprintf(fp, "%s %s %s\n", CC->upl_file,
619 /* put together an upload notice */
620 snprintf(upload_notice, sizeof upload_notice,
621 "NEW UPLOAD: '%s'\n %s\n%s\n",
625 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
626 upload_notice, 0, NULL);
629 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
636 * read from the download file
638 void cmd_read(char *cmdbuf)
644 /* The client will transmit its requested offset and byte count */
645 start_pos = extract_long(cmdbuf, 0);
646 bytes = extract_int(cmdbuf, 1);
648 if (CC->download_fp == NULL) {
649 cprintf("%d You don't have a download file open.\n",
650 ERROR + RESOURCE_NOT_OPEN);
654 /* If necessary, reduce the byte count to the size of our buffer */
655 if (bytes > sizeof(buf)) {
659 fseek(CC->download_fp, start_pos, 0);
660 bytes = fread(buf, 1, bytes, CC->download_fp);
662 /* Tell the client the actual byte count and transmit it */
663 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
664 client_write(buf, bytes);
667 cprintf("%d %s\n", ERROR, strerror(errno));
673 * write to the upload file
675 void cmd_writ(char *cmdbuf)
683 bytes = extract_int(cmdbuf, 0);
685 if (CC->upload_fp == NULL) {
686 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
690 if (bytes > 100000) {
691 cprintf("%d You may not write more than 100000 bytes.\n",
696 cprintf("%d %d\n", SEND_BINARY, bytes);
697 buf = malloc(bytes + 1);
698 client_read(buf, bytes);
699 rv = fwrite(buf, bytes, 1, CC->upload_fp);
707 * cmd_ndop() - open a network spool file for downloading
709 void cmd_ndop(char *cmdbuf)
714 if (IsEmptyStr(CC->net_node)) {
715 cprintf("%d Not authenticated as a network node.\n",
716 ERROR + NOT_LOGGED_IN);
720 if (CC->download_fp != NULL) {
721 cprintf("%d You already have a download file open.\n",
722 ERROR + RESOURCE_BUSY);
726 snprintf(pathname, sizeof pathname,
731 /* first open the file in append mode in order to create a
732 * zero-length file if it doesn't already exist
734 CC->download_fp = fopen(pathname, "a");
735 if (CC->download_fp != NULL)
736 fclose(CC->download_fp);
739 CC->download_fp = fopen(pathname, "r");
740 if (CC->download_fp == NULL) {
741 cprintf("%d cannot open %s: %s\n",
742 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
747 /* set this flag so other routines know that the download file
748 * currently open is a network spool file
752 stat(pathname, &statbuf);
753 cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
757 * cmd_nuop() - open a network spool file for uploading
759 void cmd_nuop(char *cmdbuf)
763 if (IsEmptyStr(CC->net_node)) {
764 cprintf("%d Not authenticated as a network node.\n",
765 ERROR + NOT_LOGGED_IN);
769 if (CC->upload_fp != NULL) {
770 cprintf("%d You already have an upload file open.\n",
771 ERROR + RESOURCE_BUSY);
775 snprintf(CC->upl_path, sizeof CC->upl_path,
782 CC->upload_fp = fopen(CC->upl_path, "r");
783 if (CC->upload_fp != NULL) {
784 fclose(CC->upload_fp);
785 CC->upload_fp = NULL;
786 cprintf("%d '%s' already exists\n",
787 ERROR + ALREADY_EXISTS, CC->upl_path);
791 CC->upload_fp = fopen(CC->upl_path, "w");
792 if (CC->upload_fp == NULL) {
793 cprintf("%d Cannot open %s: %s\n",
794 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
798 CC->upload_type = UPL_NET;
799 cprintf("%d Ok\n", CIT_OK);
803 /*****************************************************************************/
804 /* MODULE INITIALIZATION STUFF */
805 /*****************************************************************************/
807 CTDL_MODULE_INIT(file_ops)
810 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
811 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
812 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
813 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
814 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
815 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
816 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
817 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
818 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
819 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
820 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
821 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
823 /* return our Subversion id for the Log */