2 * Server functions which handle file transfers and room directories.
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
22 # include <sys/time.h>
29 #include <libcitadel.h>
34 #include "sysdep_decls.h"
38 #include "citserver.h"
45 #include "ctdl_module.h"
49 * network_talking_to() -- concurrency checker
51 static char *nttlist = NULL;
52 int network_talking_to(char *nodename, int operation) {
59 begin_critical_section(S_NTTLIST);
64 if (nttlist == NULL) nttlist = strdup("");
65 if (nttlist == NULL) break;
66 nttlist = (char *)realloc(nttlist,
67 (strlen(nttlist) + strlen(nodename) + 3) );
69 strcat(nttlist, nodename);
73 if (nttlist == NULL) break;
74 if (IsEmptyStr(nttlist)) break;
75 ptr = malloc(strlen(nttlist));
76 if (ptr == NULL) break;
78 for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
79 extract_token(buf, nttlist, i, '|', sizeof buf);
80 if ( (!IsEmptyStr(buf))
81 && (strcasecmp(buf, nodename)) ) {
91 if (nttlist == NULL) break;
92 if (IsEmptyStr(nttlist)) break;
93 for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
94 extract_token(buf, nttlist, i, '|', sizeof buf);
95 if (!strcasecmp(buf, nodename)) ++retval;
100 if (nttlist != NULL) CtdlLogPrintf(CTDL_DEBUG, "nttlist=<%s>\n", nttlist);
101 end_critical_section(S_NTTLIST);
105 void cleanup_nttlist(void)
107 begin_critical_section(S_NTTLIST);
111 end_critical_section(S_NTTLIST);
117 * Server command to delete a file from a room's directory
119 void cmd_delf(char *filename)
124 if (CtdlAccessCheck(ac_room_aide))
127 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
128 cprintf("%d No directory in this room.\n",
133 if (IsEmptyStr(filename)) {
134 cprintf("%d You must specify a file name.\n",
135 ERROR + FILE_NOT_FOUND);
138 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
139 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
143 snprintf(pathname, sizeof pathname,
146 CC->room.QRdirname, filename);
147 a = unlink(pathname);
149 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
152 cprintf("%d File '%s' not found.\n",
153 ERROR + FILE_NOT_FOUND, pathname);
161 * move a file from one room directory to another
163 void cmd_movf(char *cmdbuf)
165 char filename[PATH_MAX];
166 char pathname[PATH_MAX];
167 char newpath[PATH_MAX];
168 char newroom[ROOMNAMELEN];
171 struct ctdlroom qrbuf;
174 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
175 extract_token(newroom, cmdbuf, 1, '|', sizeof newroom);
177 if (CtdlAccessCheck(ac_room_aide)) return;
179 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
180 cprintf("%d No directory in this room.\n",
185 if (IsEmptyStr(filename)) {
186 cprintf("%d You must specify a file name.\n",
187 ERROR + FILE_NOT_FOUND);
191 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
192 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
196 snprintf(pathname, sizeof pathname, "./files/%s/%s",
197 CC->room.QRdirname, filename);
198 if (access(pathname, 0) != 0) {
199 cprintf("%d File '%s' not found.\n",
200 ERROR + FILE_NOT_FOUND, pathname);
204 if (CtdlGetRoom(&qrbuf, newroom) != 0) {
205 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
208 if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
209 cprintf("%d '%s' is not a directory room.\n",
210 ERROR + NOT_HERE, qrbuf.QRname);
213 snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
215 if (link(pathname, newpath) != 0) {
216 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR,
222 /* this is a crude method of copying the file description */
223 snprintf(buf, sizeof buf,
224 "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir",
225 CC->room.QRdirname, filename, qrbuf.QRdirname);
227 cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
232 * This code is common to all commands which open a file for downloading,
233 * regardless of whether it's a file from the directory, an image, a network
234 * spool file, a MIME attachment, etc.
235 * It examines the file and displays the OK result code and some information
236 * about the file. NOTE: this stuff is Unix dependent.
238 void OpenCmdResult(char *filename, const char *mime_type)
244 fstat(fileno(CC->download_fp), &statbuf);
245 CC->download_fp_total = statbuf.st_size;
246 filesize = (long) statbuf.st_size;
247 modtime = (time_t) statbuf.st_mtime;
249 cprintf("%d %ld|%ld|%s|%s\n",
250 CIT_OK, filesize, (long)modtime, filename, mime_type);
255 * open a file for downloading
257 void cmd_open(char *cmdbuf)
260 char pathname[PATH_MAX];
263 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
265 if (CtdlAccessCheck(ac_logged_in)) return;
267 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
268 cprintf("%d No directory in this room.\n",
273 if (IsEmptyStr(filename)) {
274 cprintf("%d You must specify a file name.\n",
275 ERROR + FILE_NOT_FOUND);
279 if (CC->download_fp != NULL) {
280 cprintf("%d You already have a download file open.\n",
281 ERROR + RESOURCE_BUSY);
285 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
286 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
291 snprintf(pathname, sizeof pathname,
294 CC->room.QRdirname, filename);
295 CC->download_fp = fopen(pathname, "r");
297 if (CC->download_fp == NULL) {
298 cprintf("%d cannot open %s: %s\n",
299 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
303 OpenCmdResult(filename, "application/octet-stream");
309 void cmd_oimg(char *cmdbuf)
312 char pathname[PATH_MAX];
313 char MimeTestBuf[32];
314 struct ctdluser usbuf;
315 char which_user[USERNAME_SIZE];
320 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
322 if (IsEmptyStr(filename)) {
323 cprintf("%d You must specify a file name.\n",
324 ERROR + FILE_NOT_FOUND);
328 if (CC->download_fp != NULL) {
329 cprintf("%d You already have a download file open.\n",
330 ERROR + RESOURCE_BUSY);
334 if (!strcasecmp(filename, "_userpic_")) {
335 extract_token(which_user, cmdbuf, 1, '|', sizeof which_user);
336 if (CtdlGetUser(&usbuf, which_user) != 0) {
337 cprintf("%d No such user.\n",
338 ERROR + NO_SUCH_USER);
341 snprintf(pathname, sizeof pathname,
345 } else if (!strcasecmp(filename, "_floorpic_")) {
346 which_floor = extract_int(cmdbuf, 1);
347 snprintf(pathname, sizeof pathname,
349 ctdl_image_dir, which_floor);
350 } else if (!strcasecmp(filename, "_roompic_")) {
351 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
353 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
354 filename[a] = tolower(filename[a]);
355 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
359 snprintf(pathname, sizeof pathname,
365 CC->download_fp = fopen(pathname, "rb");
366 if (CC->download_fp == NULL) {
367 strcat(pathname, ".gif");
368 CC->download_fp = fopen(pathname, "rb");
370 if (CC->download_fp == NULL) {
371 cprintf("%d Cannot open %s: %s\n",
372 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
375 rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
376 rewind (CC->download_fp);
377 OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
381 * open a file for uploading
383 void cmd_uopn(char *cmdbuf)
387 extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
388 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
389 extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
391 if (CtdlAccessCheck(ac_logged_in)) return;
393 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
394 cprintf("%d No directory in this room.\n",
399 if (IsEmptyStr(CC->upl_file)) {
400 cprintf("%d You must specify a file name.\n",
401 ERROR + FILE_NOT_FOUND);
405 if (CC->upload_fp != NULL) {
406 cprintf("%d You already have a upload file open.\n",
407 ERROR + RESOURCE_BUSY);
411 for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
412 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
413 CC->upl_file[a] = '_';
416 snprintf(CC->upl_path, sizeof CC->upl_path,
419 CC->room.QRdirname, CC->upl_file);
420 snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
425 CC->upload_fp = fopen(CC->upl_path, "r");
426 if (CC->upload_fp != NULL) {
427 fclose(CC->upload_fp);
428 CC->upload_fp = NULL;
429 cprintf("%d '%s' already exists\n",
430 ERROR + ALREADY_EXISTS, CC->upl_path);
434 CC->upload_fp = fopen(CC->upl_path, "wb");
435 if (CC->upload_fp == NULL) {
436 cprintf("%d Cannot open %s: %s\n",
437 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
440 cprintf("%d Ok\n", CIT_OK);
446 * open an image file for uploading
448 void cmd_uimg(char *cmdbuf)
450 int is_this_for_real;
455 if (num_parms(cmdbuf) < 2) {
456 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
460 is_this_for_real = extract_int(cmdbuf, 0);
461 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
462 extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
463 if (CC->upload_fp != NULL) {
464 cprintf("%d You already have an upload file open.\n",
465 ERROR + RESOURCE_BUSY);
469 strcpy(CC->upl_path, "");
471 for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
472 basenm[a] = tolower(basenm[a]);
473 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
478 if (CC->user.axlevel >= AxAideU) {
479 snprintf(CC->upl_path, sizeof CC->upl_path,
485 if (!strcasecmp(basenm, "_userpic_")) {
486 snprintf(CC->upl_path, sizeof CC->upl_path,
492 if ((!strcasecmp(basenm, "_floorpic_"))
493 && (CC->user.axlevel >= AxAideU)) {
494 which_floor = extract_int(cmdbuf, 2);
495 snprintf(CC->upl_path, sizeof CC->upl_path,
501 if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
502 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
505 if (IsEmptyStr(CC->upl_path)) {
506 cprintf("%d Higher access required.\n",
507 ERROR + HIGHER_ACCESS_REQUIRED);
511 if (is_this_for_real == 0) {
512 cprintf("%d Ok to send image\n", CIT_OK);
516 CC->upload_fp = fopen(CC->upl_path, "wb");
517 if (CC->upload_fp == NULL) {
518 cprintf("%d Cannot open %s: %s\n",
519 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
522 cprintf("%d Ok\n", CIT_OK);
523 CC->upload_type = UPL_IMAGE;
528 * close the download file
530 void cmd_clos(char *cmdbuf)
534 if (CC->download_fp == NULL) {
535 cprintf("%d You don't have a download file open.\n",
536 ERROR + RESOURCE_NOT_OPEN);
540 fclose(CC->download_fp);
541 CC->download_fp = NULL;
543 if (CC->dl_is_net == 1) {
545 snprintf(buf, sizeof buf,
552 cprintf("%d Ok\n", CIT_OK);
559 void abort_upl(CitContext *who)
561 if (who->upload_fp != NULL) {
562 fclose(who->upload_fp);
563 who->upload_fp = NULL;
564 unlink(CC->upl_path);
571 * close the upload file
573 void cmd_ucls(char *cmd)
576 char upload_notice[512];
579 if (CC->upload_fp == NULL) {
580 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
584 fclose(CC->upload_fp);
585 CC->upload_fp = NULL;
587 if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
588 cprintf("%d Upload completed.\n", CIT_OK);
590 if (CC->upload_type == UPL_NET) {
591 char final_filename[PATH_MAX];
592 snprintf(final_filename, sizeof final_filename,
600 if (link(CC->upl_path, final_filename) == 0) {
601 unlink(CC->upl_path);
604 CtdlLogPrintf(CTDL_ALERT, "Cannot link %d to %d: %s\n",
605 CC->upl_path, final_filename, strerror(errno)
609 /* FIXME ... here we need to trigger a network run */
612 CC->upload_type = UPL_FILE;
616 if (!strcasecmp(cmd, "1")) {
617 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
618 fp = fopen(CC->upl_filedir, "a");
620 fp = fopen(CC->upl_filedir, "w");
623 fprintf(fp, "%s %s %s\n", CC->upl_file,
629 /* put together an upload notice */
630 snprintf(upload_notice, sizeof upload_notice,
631 "NEW UPLOAD: '%s'\n %s\n%s\n",
635 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
636 upload_notice, 0, NULL);
639 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
645 * read from the download file
647 void cmd_read(char *cmdbuf)
653 /* The client will transmit its requested offset and byte count */
654 start_pos = extract_long(cmdbuf, 0);
655 bytes = extract_int(cmdbuf, 1);
657 if (CC->download_fp == NULL) {
658 cprintf("%d You don't have a download file open.\n",
659 ERROR + RESOURCE_NOT_OPEN);
663 /* If necessary, reduce the byte count to the size of our buffer */
664 if (bytes > sizeof(buf)) {
668 fseek(CC->download_fp, start_pos, 0);
669 bytes = fread(buf, 1, bytes, CC->download_fp);
671 /* Tell the client the actual byte count and transmit it */
672 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
673 client_write(buf, bytes);
676 cprintf("%d %s\n", ERROR, strerror(errno));
682 * write to the upload file
684 void cmd_writ(char *cmdbuf)
692 bytes = extract_int(cmdbuf, 0);
694 if (CC->upload_fp == NULL) {
695 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
699 if (bytes > 100000) {
700 cprintf("%d You may not write more than 100000 bytes.\n",
705 cprintf("%d %d\n", SEND_BINARY, bytes);
706 buf = malloc(bytes + 1);
707 client_read(buf, bytes);
708 rv = fwrite(buf, bytes, 1, CC->upload_fp);
716 * cmd_ndop() - open a network spool file for downloading
718 void cmd_ndop(char *cmdbuf)
723 if (IsEmptyStr(CC->net_node)) {
724 cprintf("%d Not authenticated as a network node.\n",
725 ERROR + NOT_LOGGED_IN);
729 if (CC->download_fp != NULL) {
730 cprintf("%d You already have a download file open.\n",
731 ERROR + RESOURCE_BUSY);
735 snprintf(pathname, sizeof pathname,
740 /* first open the file in append mode in order to create a
741 * zero-length file if it doesn't already exist
743 CC->download_fp = fopen(pathname, "a");
744 if (CC->download_fp != NULL)
745 fclose(CC->download_fp);
748 CC->download_fp = fopen(pathname, "r");
749 if (CC->download_fp == NULL) {
750 cprintf("%d cannot open %s: %s\n",
751 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
756 /* set this flag so other routines know that the download file
757 * currently open is a network spool file
761 stat(pathname, &statbuf);
762 CC->download_fp_total = statbuf.st_size;
763 cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
767 * cmd_nuop() - open a network spool file for uploading
769 void cmd_nuop(char *cmdbuf)
773 if (IsEmptyStr(CC->net_node)) {
774 cprintf("%d Not authenticated as a network node.\n",
775 ERROR + NOT_LOGGED_IN);
779 if (CC->upload_fp != NULL) {
780 cprintf("%d You already have an upload file open.\n",
781 ERROR + RESOURCE_BUSY);
785 snprintf(CC->upl_path, sizeof CC->upl_path,
792 CC->upload_fp = fopen(CC->upl_path, "r");
793 if (CC->upload_fp != NULL) {
794 fclose(CC->upload_fp);
795 CC->upload_fp = NULL;
796 cprintf("%d '%s' already exists\n",
797 ERROR + ALREADY_EXISTS, CC->upl_path);
801 CC->upload_fp = fopen(CC->upl_path, "w");
802 if (CC->upload_fp == NULL) {
803 cprintf("%d Cannot open %s: %s\n",
804 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
808 CC->upload_type = UPL_NET;
809 cprintf("%d Ok\n", CIT_OK);
813 /*****************************************************************************/
814 /* MODULE INITIALIZATION STUFF */
815 /*****************************************************************************/
817 CTDL_MODULE_INIT(file_ops)
820 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
821 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
822 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
823 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
824 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
825 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
826 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
827 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
828 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
829 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
830 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
831 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
832 CtdlRegisterCleanupHook(cleanup_nttlist);
834 /* return our Subversion id for the Log */