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 HashList *nttlist = NULL;
52 int network_talking_to(const char *nodename, long len, int operation) {
58 begin_critical_section(S_NTTLIST);
64 nttlist = NewHash(1, NULL);
65 Put(nttlist, nodename, len, NewStrBufPlain(nodename, len), HFreeStrBuf);
66 syslog(LOG_DEBUG, "nttlist: added <%s>\n", nodename);
69 if ((nttlist == NULL) ||
70 (GetCount(nttlist) == 0))
72 Pos = GetNewHashPos(nttlist, 1);
73 if (GetHashPosFromKey (nttlist, nodename, len, Pos))
74 DeleteEntryFromHash(nttlist, Pos);
76 syslog(LOG_DEBUG, "nttlist: removed <%s>\n", nodename);
81 if ((nttlist == NULL) ||
82 (GetCount(nttlist) == 0))
84 if (!GetHash(nttlist, nodename, len, &vdata))
86 syslog(LOG_DEBUG, "nttlist: have [%d] <%s>\n", retval, nodename);
90 end_critical_section(S_NTTLIST);
94 void cleanup_nttlist(void)
96 begin_critical_section(S_NTTLIST);
98 end_critical_section(S_NTTLIST);
104 * Server command to delete a file from a room's directory
106 void cmd_delf(char *filename)
111 if (CtdlAccessCheck(ac_room_aide))
114 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
115 cprintf("%d No directory in this room.\n",
120 if (IsEmptyStr(filename)) {
121 cprintf("%d You must specify a file name.\n",
122 ERROR + FILE_NOT_FOUND);
125 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
126 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
130 snprintf(pathname, sizeof pathname,
133 CC->room.QRdirname, filename);
134 a = unlink(pathname);
136 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
139 cprintf("%d File '%s' not found.\n",
140 ERROR + FILE_NOT_FOUND, pathname);
148 * move a file from one room directory to another
150 void cmd_movf(char *cmdbuf)
152 char filename[PATH_MAX];
153 char pathname[PATH_MAX];
154 char newpath[PATH_MAX];
155 char newroom[ROOMNAMELEN];
158 struct ctdlroom qrbuf;
161 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
162 extract_token(newroom, cmdbuf, 1, '|', sizeof newroom);
164 if (CtdlAccessCheck(ac_room_aide)) return;
166 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
167 cprintf("%d No directory in this room.\n",
172 if (IsEmptyStr(filename)) {
173 cprintf("%d You must specify a file name.\n",
174 ERROR + FILE_NOT_FOUND);
178 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
179 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
183 snprintf(pathname, sizeof pathname, "./files/%s/%s",
184 CC->room.QRdirname, filename);
185 if (access(pathname, 0) != 0) {
186 cprintf("%d File '%s' not found.\n",
187 ERROR + FILE_NOT_FOUND, pathname);
191 if (CtdlGetRoom(&qrbuf, newroom) != 0) {
192 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
195 if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
196 cprintf("%d '%s' is not a directory room.\n",
197 ERROR + NOT_HERE, qrbuf.QRname);
200 snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
202 if (link(pathname, newpath) != 0) {
203 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR,
209 /* this is a crude method of copying the file description */
210 snprintf(buf, sizeof buf,
211 "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir",
212 CC->room.QRdirname, filename, qrbuf.QRdirname);
214 cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
219 * This code is common to all commands which open a file for downloading,
220 * regardless of whether it's a file from the directory, an image, a network
221 * spool file, a MIME attachment, etc.
222 * It examines the file and displays the OK result code and some information
223 * about the file. NOTE: this stuff is Unix dependent.
225 void OpenCmdResult(char *filename, const char *mime_type)
231 fstat(fileno(CC->download_fp), &statbuf);
232 CC->download_fp_total = statbuf.st_size;
233 filesize = (long) statbuf.st_size;
234 modtime = (time_t) statbuf.st_mtime;
236 cprintf("%d %ld|%ld|%s|%s\n",
237 CIT_OK, filesize, (long)modtime, filename, mime_type);
242 * open a file for downloading
244 void cmd_open(char *cmdbuf)
247 char pathname[PATH_MAX];
250 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
252 if (CtdlAccessCheck(ac_logged_in)) return;
254 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
255 cprintf("%d No directory in this room.\n",
260 if (IsEmptyStr(filename)) {
261 cprintf("%d You must specify a file name.\n",
262 ERROR + FILE_NOT_FOUND);
266 if (CC->download_fp != NULL) {
267 cprintf("%d You already have a download file open.\n",
268 ERROR + RESOURCE_BUSY);
272 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
273 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
278 snprintf(pathname, sizeof pathname,
281 CC->room.QRdirname, filename);
282 CC->download_fp = fopen(pathname, "r");
284 if (CC->download_fp == NULL) {
285 cprintf("%d cannot open %s: %s\n",
286 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
290 OpenCmdResult(filename, "application/octet-stream");
296 void cmd_oimg(char *cmdbuf)
299 char pathname[PATH_MAX];
300 char MimeTestBuf[32];
301 struct ctdluser usbuf;
302 char which_user[USERNAME_SIZE];
307 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
309 if (IsEmptyStr(filename)) {
310 cprintf("%d You must specify a file name.\n",
311 ERROR + FILE_NOT_FOUND);
315 if (CC->download_fp != NULL) {
316 cprintf("%d You already have a download file open.\n",
317 ERROR + RESOURCE_BUSY);
321 if (!strcasecmp(filename, "_userpic_")) {
322 extract_token(which_user, cmdbuf, 1, '|', sizeof which_user);
323 if (CtdlGetUser(&usbuf, which_user) != 0) {
324 cprintf("%d No such user.\n",
325 ERROR + NO_SUCH_USER);
328 snprintf(pathname, sizeof pathname,
332 } else if (!strcasecmp(filename, "_floorpic_")) {
333 which_floor = extract_int(cmdbuf, 1);
334 snprintf(pathname, sizeof pathname,
336 ctdl_image_dir, which_floor);
337 } else if (!strcasecmp(filename, "_roompic_")) {
338 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
340 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
341 filename[a] = tolower(filename[a]);
342 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
346 snprintf(pathname, sizeof pathname,
352 CC->download_fp = fopen(pathname, "rb");
353 if (CC->download_fp == NULL) {
354 strcat(pathname, ".gif");
355 CC->download_fp = fopen(pathname, "rb");
357 if (CC->download_fp == NULL) {
358 cprintf("%d Cannot open %s: %s\n",
359 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
362 rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
364 cprintf("%d Cannot access %s: %s\n",
365 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
369 rewind (CC->download_fp);
370 OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
374 * open a file for uploading
376 void cmd_uopn(char *cmdbuf)
380 extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
381 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
382 extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
384 if (CtdlAccessCheck(ac_logged_in)) return;
386 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
387 cprintf("%d No directory in this room.\n",
392 if (IsEmptyStr(CC->upl_file)) {
393 cprintf("%d You must specify a file name.\n",
394 ERROR + FILE_NOT_FOUND);
398 if (CC->upload_fp != NULL) {
399 cprintf("%d You already have a upload file open.\n",
400 ERROR + RESOURCE_BUSY);
404 for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
405 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
406 CC->upl_file[a] = '_';
409 snprintf(CC->upl_path, sizeof CC->upl_path,
412 CC->room.QRdirname, CC->upl_file);
413 snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
418 CC->upload_fp = fopen(CC->upl_path, "r");
419 if (CC->upload_fp != NULL) {
420 fclose(CC->upload_fp);
421 CC->upload_fp = NULL;
422 cprintf("%d '%s' already exists\n",
423 ERROR + ALREADY_EXISTS, CC->upl_path);
427 CC->upload_fp = fopen(CC->upl_path, "wb");
428 if (CC->upload_fp == NULL) {
429 cprintf("%d Cannot open %s: %s\n",
430 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
433 cprintf("%d Ok\n", CIT_OK);
439 * open an image file for uploading
441 void cmd_uimg(char *cmdbuf)
443 int is_this_for_real;
448 if (num_parms(cmdbuf) < 2) {
449 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
453 is_this_for_real = extract_int(cmdbuf, 0);
454 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
455 extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
456 if (CC->upload_fp != NULL) {
457 cprintf("%d You already have an upload file open.\n",
458 ERROR + RESOURCE_BUSY);
462 strcpy(CC->upl_path, "");
464 for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
465 basenm[a] = tolower(basenm[a]);
466 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
471 if (CC->user.axlevel >= AxAideU) {
472 snprintf(CC->upl_path, sizeof CC->upl_path,
478 if (!strcasecmp(basenm, "_userpic_")) {
479 snprintf(CC->upl_path, sizeof CC->upl_path,
485 if ((!strcasecmp(basenm, "_floorpic_"))
486 && (CC->user.axlevel >= AxAideU)) {
487 which_floor = extract_int(cmdbuf, 2);
488 snprintf(CC->upl_path, sizeof CC->upl_path,
494 if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
495 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
498 if (IsEmptyStr(CC->upl_path)) {
499 cprintf("%d Higher access required.\n",
500 ERROR + HIGHER_ACCESS_REQUIRED);
504 if (is_this_for_real == 0) {
505 cprintf("%d Ok to send image\n", CIT_OK);
509 CC->upload_fp = fopen(CC->upl_path, "wb");
510 if (CC->upload_fp == NULL) {
511 cprintf("%d Cannot open %s: %s\n",
512 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
515 cprintf("%d Ok\n", CIT_OK);
516 CC->upload_type = UPL_IMAGE;
521 * close the download file
523 void cmd_clos(char *cmdbuf)
527 if (CC->download_fp == NULL) {
528 cprintf("%d You don't have a download file open.\n",
529 ERROR + RESOURCE_NOT_OPEN);
533 fclose(CC->download_fp);
534 CC->download_fp = NULL;
536 if (CC->dl_is_net == 1) {
538 snprintf(buf, sizeof buf,
545 cprintf("%d Ok\n", CIT_OK);
552 void abort_upl(CitContext *who)
554 if (who->upload_fp != NULL) {
555 fclose(who->upload_fp);
556 who->upload_fp = NULL;
557 unlink(CC->upl_path);
564 * close the upload file
566 void cmd_ucls(char *cmd)
569 char upload_notice[512];
572 if (CC->upload_fp == NULL) {
573 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
577 fclose(CC->upload_fp);
578 CC->upload_fp = NULL;
580 if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
581 cprintf("%d Upload completed.\n", CIT_OK);
583 if (CC->upload_type == UPL_NET) {
584 char final_filename[PATH_MAX];
585 snprintf(final_filename, sizeof final_filename,
593 if (link(CC->upl_path, final_filename) == 0) {
594 unlink(CC->upl_path);
597 syslog(LOG_ALERT, "Cannot link %s to %s: %s\n",
598 CC->upl_path, final_filename, strerror(errno)
602 /* FIXME ... here we need to trigger a network run */
605 CC->upload_type = UPL_FILE;
609 if (!strcasecmp(cmd, "1")) {
610 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
611 fp = fopen(CC->upl_filedir, "a");
613 fp = fopen(CC->upl_filedir, "w");
616 fprintf(fp, "%s %s %s\n", CC->upl_file,
622 /* put together an upload notice */
623 snprintf(upload_notice, sizeof upload_notice,
624 "NEW UPLOAD: '%s'\n %s\n%s\n",
628 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
629 upload_notice, 0, NULL);
632 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
638 * read from the download file
640 void cmd_read(char *cmdbuf)
646 /* The client will transmit its requested offset and byte count */
647 start_pos = extract_long(cmdbuf, 0);
648 bytes = extract_int(cmdbuf, 1);
650 if (CC->download_fp == NULL) {
651 cprintf("%d You don't have a download file open.\n",
652 ERROR + RESOURCE_NOT_OPEN);
656 /* If necessary, reduce the byte count to the size of our buffer */
657 if (bytes > sizeof(buf)) {
661 fseek(CC->download_fp, start_pos, 0);
662 bytes = fread(buf, 1, bytes, CC->download_fp);
664 /* Tell the client the actual byte count and transmit it */
665 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
666 client_write(buf, bytes);
669 cprintf("%d %s\n", ERROR, strerror(errno));
675 * write to the upload file
677 void cmd_writ(char *cmdbuf)
685 bytes = extract_int(cmdbuf, 0);
687 if (CC->upload_fp == NULL) {
688 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
692 if (bytes > 100000) {
693 cprintf("%d You may not write more than 100000 bytes.\n",
698 cprintf("%d %d\n", SEND_BINARY, bytes);
699 buf = malloc(bytes + 1);
700 client_read(buf, bytes);
701 rv = fwrite(buf, bytes, 1, CC->upload_fp);
703 syslog(LOG_EMERG, "Couldn't write: %s\n",
713 * cmd_ndop() - open a network spool file for downloading
715 void cmd_ndop(char *cmdbuf)
720 if (IsEmptyStr(CC->net_node)) {
721 cprintf("%d Not authenticated as a network node.\n",
722 ERROR + NOT_LOGGED_IN);
726 if (CC->download_fp != NULL) {
727 cprintf("%d You already have a download file open.\n",
728 ERROR + RESOURCE_BUSY);
732 snprintf(pathname, sizeof pathname,
737 /* first open the file in append mode in order to create a
738 * zero-length file if it doesn't already exist
740 CC->download_fp = fopen(pathname, "a");
741 if (CC->download_fp != NULL)
742 fclose(CC->download_fp);
745 CC->download_fp = fopen(pathname, "r");
746 if (CC->download_fp == NULL) {
747 cprintf("%d cannot open %s: %s\n",
748 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
753 /* set this flag so other routines know that the download file
754 * currently open is a network spool file
758 stat(pathname, &statbuf);
759 CC->download_fp_total = statbuf.st_size;
760 cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
764 * cmd_nuop() - open a network spool file for uploading
766 void cmd_nuop(char *cmdbuf)
770 if (IsEmptyStr(CC->net_node)) {
771 cprintf("%d Not authenticated as a network node.\n",
772 ERROR + NOT_LOGGED_IN);
776 if (CC->upload_fp != NULL) {
777 cprintf("%d You already have an upload file open.\n",
778 ERROR + RESOURCE_BUSY);
782 snprintf(CC->upl_path, sizeof CC->upl_path,
789 CC->upload_fp = fopen(CC->upl_path, "r");
790 if (CC->upload_fp != NULL) {
791 fclose(CC->upload_fp);
792 CC->upload_fp = NULL;
793 cprintf("%d '%s' already exists\n",
794 ERROR + ALREADY_EXISTS, CC->upl_path);
798 CC->upload_fp = fopen(CC->upl_path, "w");
799 if (CC->upload_fp == NULL) {
800 cprintf("%d Cannot open %s: %s\n",
801 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
805 CC->upload_type = UPL_NET;
806 cprintf("%d Ok\n", CIT_OK);
810 /*****************************************************************************/
811 /* MODULE INITIALIZATION STUFF */
812 /*****************************************************************************/
814 CTDL_MODULE_INIT(file_ops)
817 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
818 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
819 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
820 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
821 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
822 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
823 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
824 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
825 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
826 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
827 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
828 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
829 CtdlRegisterCleanupHook(cleanup_nttlist);
831 /* return our Subversion id for the Log */