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) syslog(LOG_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);
377 cprintf("%d Cannot access %s: %s\n",
378 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
382 rewind (CC->download_fp);
383 OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
387 * open a file for uploading
389 void cmd_uopn(char *cmdbuf)
393 extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
394 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
395 extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
397 if (CtdlAccessCheck(ac_logged_in)) return;
399 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
400 cprintf("%d No directory in this room.\n",
405 if (IsEmptyStr(CC->upl_file)) {
406 cprintf("%d You must specify a file name.\n",
407 ERROR + FILE_NOT_FOUND);
411 if (CC->upload_fp != NULL) {
412 cprintf("%d You already have a upload file open.\n",
413 ERROR + RESOURCE_BUSY);
417 for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
418 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
419 CC->upl_file[a] = '_';
422 snprintf(CC->upl_path, sizeof CC->upl_path,
425 CC->room.QRdirname, CC->upl_file);
426 snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
431 CC->upload_fp = fopen(CC->upl_path, "r");
432 if (CC->upload_fp != NULL) {
433 fclose(CC->upload_fp);
434 CC->upload_fp = NULL;
435 cprintf("%d '%s' already exists\n",
436 ERROR + ALREADY_EXISTS, CC->upl_path);
440 CC->upload_fp = fopen(CC->upl_path, "wb");
441 if (CC->upload_fp == NULL) {
442 cprintf("%d Cannot open %s: %s\n",
443 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
446 cprintf("%d Ok\n", CIT_OK);
452 * open an image file for uploading
454 void cmd_uimg(char *cmdbuf)
456 int is_this_for_real;
461 if (num_parms(cmdbuf) < 2) {
462 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
466 is_this_for_real = extract_int(cmdbuf, 0);
467 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
468 extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
469 if (CC->upload_fp != NULL) {
470 cprintf("%d You already have an upload file open.\n",
471 ERROR + RESOURCE_BUSY);
475 strcpy(CC->upl_path, "");
477 for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
478 basenm[a] = tolower(basenm[a]);
479 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
484 if (CC->user.axlevel >= AxAideU) {
485 snprintf(CC->upl_path, sizeof CC->upl_path,
491 if (!strcasecmp(basenm, "_userpic_")) {
492 snprintf(CC->upl_path, sizeof CC->upl_path,
498 if ((!strcasecmp(basenm, "_floorpic_"))
499 && (CC->user.axlevel >= AxAideU)) {
500 which_floor = extract_int(cmdbuf, 2);
501 snprintf(CC->upl_path, sizeof CC->upl_path,
507 if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
508 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
511 if (IsEmptyStr(CC->upl_path)) {
512 cprintf("%d Higher access required.\n",
513 ERROR + HIGHER_ACCESS_REQUIRED);
517 if (is_this_for_real == 0) {
518 cprintf("%d Ok to send image\n", CIT_OK);
522 CC->upload_fp = fopen(CC->upl_path, "wb");
523 if (CC->upload_fp == NULL) {
524 cprintf("%d Cannot open %s: %s\n",
525 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
528 cprintf("%d Ok\n", CIT_OK);
529 CC->upload_type = UPL_IMAGE;
534 * close the download file
536 void cmd_clos(char *cmdbuf)
540 if (CC->download_fp == NULL) {
541 cprintf("%d You don't have a download file open.\n",
542 ERROR + RESOURCE_NOT_OPEN);
546 fclose(CC->download_fp);
547 CC->download_fp = NULL;
549 if (CC->dl_is_net == 1) {
551 snprintf(buf, sizeof buf,
558 cprintf("%d Ok\n", CIT_OK);
565 void abort_upl(CitContext *who)
567 if (who->upload_fp != NULL) {
568 fclose(who->upload_fp);
569 who->upload_fp = NULL;
570 unlink(CC->upl_path);
577 * close the upload file
579 void cmd_ucls(char *cmd)
582 char upload_notice[512];
585 if (CC->upload_fp == NULL) {
586 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
590 fclose(CC->upload_fp);
591 CC->upload_fp = NULL;
593 if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
594 cprintf("%d Upload completed.\n", CIT_OK);
596 if (CC->upload_type == UPL_NET) {
597 char final_filename[PATH_MAX];
598 snprintf(final_filename, sizeof final_filename,
606 if (link(CC->upl_path, final_filename) == 0) {
607 unlink(CC->upl_path);
610 syslog(LOG_ALERT, "Cannot link %s to %s: %s\n",
611 CC->upl_path, final_filename, strerror(errno)
615 /* FIXME ... here we need to trigger a network run */
618 CC->upload_type = UPL_FILE;
622 if (!strcasecmp(cmd, "1")) {
623 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
624 fp = fopen(CC->upl_filedir, "a");
626 fp = fopen(CC->upl_filedir, "w");
629 fprintf(fp, "%s %s %s\n", CC->upl_file,
635 /* put together an upload notice */
636 snprintf(upload_notice, sizeof upload_notice,
637 "NEW UPLOAD: '%s'\n %s\n%s\n",
641 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
642 upload_notice, 0, NULL);
645 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
651 * read from the download file
653 void cmd_read(char *cmdbuf)
659 /* The client will transmit its requested offset and byte count */
660 start_pos = extract_long(cmdbuf, 0);
661 bytes = extract_int(cmdbuf, 1);
663 if (CC->download_fp == NULL) {
664 cprintf("%d You don't have a download file open.\n",
665 ERROR + RESOURCE_NOT_OPEN);
669 /* If necessary, reduce the byte count to the size of our buffer */
670 if (bytes > sizeof(buf)) {
674 fseek(CC->download_fp, start_pos, 0);
675 bytes = fread(buf, 1, bytes, CC->download_fp);
677 /* Tell the client the actual byte count and transmit it */
678 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
679 client_write(buf, bytes);
682 cprintf("%d %s\n", ERROR, strerror(errno));
688 * write to the upload file
690 void cmd_writ(char *cmdbuf)
698 bytes = extract_int(cmdbuf, 0);
700 if (CC->upload_fp == NULL) {
701 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
705 if (bytes > 100000) {
706 cprintf("%d You may not write more than 100000 bytes.\n",
711 cprintf("%d %d\n", SEND_BINARY, bytes);
712 buf = malloc(bytes + 1);
713 client_read(buf, bytes);
714 rv = fwrite(buf, bytes, 1, CC->upload_fp);
716 syslog(LOG_EMERG, "Couldn't write: %s\n",
726 * cmd_ndop() - open a network spool file for downloading
728 void cmd_ndop(char *cmdbuf)
733 if (IsEmptyStr(CC->net_node)) {
734 cprintf("%d Not authenticated as a network node.\n",
735 ERROR + NOT_LOGGED_IN);
739 if (CC->download_fp != NULL) {
740 cprintf("%d You already have a download file open.\n",
741 ERROR + RESOURCE_BUSY);
745 snprintf(pathname, sizeof pathname,
750 /* first open the file in append mode in order to create a
751 * zero-length file if it doesn't already exist
753 CC->download_fp = fopen(pathname, "a");
754 if (CC->download_fp != NULL)
755 fclose(CC->download_fp);
758 CC->download_fp = fopen(pathname, "r");
759 if (CC->download_fp == NULL) {
760 cprintf("%d cannot open %s: %s\n",
761 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
766 /* set this flag so other routines know that the download file
767 * currently open is a network spool file
771 stat(pathname, &statbuf);
772 CC->download_fp_total = statbuf.st_size;
773 cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
777 * cmd_nuop() - open a network spool file for uploading
779 void cmd_nuop(char *cmdbuf)
783 if (IsEmptyStr(CC->net_node)) {
784 cprintf("%d Not authenticated as a network node.\n",
785 ERROR + NOT_LOGGED_IN);
789 if (CC->upload_fp != NULL) {
790 cprintf("%d You already have an upload file open.\n",
791 ERROR + RESOURCE_BUSY);
795 snprintf(CC->upl_path, sizeof CC->upl_path,
802 CC->upload_fp = fopen(CC->upl_path, "r");
803 if (CC->upload_fp != NULL) {
804 fclose(CC->upload_fp);
805 CC->upload_fp = NULL;
806 cprintf("%d '%s' already exists\n",
807 ERROR + ALREADY_EXISTS, CC->upl_path);
811 CC->upload_fp = fopen(CC->upl_path, "w");
812 if (CC->upload_fp == NULL) {
813 cprintf("%d Cannot open %s: %s\n",
814 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
818 CC->upload_type = UPL_NET;
819 cprintf("%d Ok\n", CIT_OK);
823 /*****************************************************************************/
824 /* MODULE INITIALIZATION STUFF */
825 /*****************************************************************************/
827 CTDL_MODULE_INIT(file_ops)
830 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
831 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
832 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
833 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
834 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
835 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
836 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
837 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
838 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
839 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
840 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
841 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
842 CtdlRegisterCleanupHook(cleanup_nttlist);
844 /* return our Subversion id for the Log */