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>
30 #include <libcitadel.h>
35 #include "sysdep_decls.h"
39 #include "citserver.h"
41 #include "ctdl_module.h"
47 * Server command to delete a file from a room's directory
49 void cmd_delf(char *filename)
54 if (CtdlAccessCheck(ac_room_aide))
57 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
58 cprintf("%d No directory in this room.\n",
63 if (IsEmptyStr(filename)) {
64 cprintf("%d You must specify a file name.\n",
65 ERROR + FILE_NOT_FOUND);
68 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
69 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
73 snprintf(pathname, sizeof pathname,
76 CC->room.QRdirname, filename);
79 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
82 cprintf("%d File '%s' not found.\n",
83 ERROR + FILE_NOT_FOUND, pathname);
91 * move a file from one room directory to another
93 void cmd_movf(char *cmdbuf)
95 char filename[PATH_MAX];
96 char pathname[PATH_MAX];
97 char newpath[PATH_MAX];
98 char newroom[ROOMNAMELEN];
101 struct ctdlroom qrbuf;
103 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
104 extract_token(newroom, cmdbuf, 1, '|', sizeof newroom);
106 if (CtdlAccessCheck(ac_room_aide)) return;
108 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
109 cprintf("%d No directory in this room.\n",
114 if (IsEmptyStr(filename)) {
115 cprintf("%d You must specify a file name.\n",
116 ERROR + FILE_NOT_FOUND);
120 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
121 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
125 snprintf(pathname, sizeof pathname, "./files/%s/%s",
126 CC->room.QRdirname, filename);
127 if (access(pathname, 0) != 0) {
128 cprintf("%d File '%s' not found.\n",
129 ERROR + FILE_NOT_FOUND, pathname);
133 if (CtdlGetRoom(&qrbuf, newroom) != 0) {
134 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
137 if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
138 cprintf("%d '%s' is not a directory room.\n",
139 ERROR + NOT_HERE, qrbuf.QRname);
142 snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
144 if (link(pathname, newpath) != 0) {
145 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR,
151 /* this is a crude method of copying the file description */
152 snprintf(buf, sizeof buf,
153 "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir",
154 CC->room.QRdirname, filename, qrbuf.QRdirname);
156 cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
161 * This code is common to all commands which open a file for downloading,
162 * regardless of whether it's a file from the directory, an image, a network
163 * spool file, a MIME attachment, etc.
164 * It examines the file and displays the OK result code and some information
165 * about the file. NOTE: this stuff is Unix dependent.
167 void OpenCmdResult(char *filename, const char *mime_type)
173 fstat(fileno(CC->download_fp), &statbuf);
174 CC->download_fp_total = statbuf.st_size;
175 filesize = (long) statbuf.st_size;
176 modtime = (time_t) statbuf.st_mtime;
178 cprintf("%d %ld|%ld|%s|%s\n",
179 CIT_OK, filesize, (long)modtime, filename, mime_type);
184 * open a file for downloading
186 void cmd_open(char *cmdbuf)
189 char pathname[PATH_MAX];
192 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
194 if (CtdlAccessCheck(ac_logged_in)) return;
196 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
197 cprintf("%d No directory in this room.\n",
202 if (IsEmptyStr(filename)) {
203 cprintf("%d You must specify a file name.\n",
204 ERROR + FILE_NOT_FOUND);
207 if (strstr(filename, "../") != NULL)
209 cprintf("%d syntax error.\n",
210 ERROR + ILLEGAL_VALUE);
214 if (CC->download_fp != NULL) {
215 cprintf("%d You already have a download file open.\n",
216 ERROR + RESOURCE_BUSY);
220 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
221 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
226 snprintf(pathname, sizeof pathname,
229 CC->room.QRdirname, filename);
230 CC->download_fp = fopen(pathname, "r");
232 if (CC->download_fp == NULL) {
233 cprintf("%d cannot open %s: %s\n",
234 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
238 OpenCmdResult(filename, "application/octet-stream");
244 void cmd_oimg(char *cmdbuf)
247 char pathname[PATH_MAX];
248 char MimeTestBuf[32];
249 struct ctdluser usbuf;
250 char which_user[USERNAME_SIZE];
255 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
257 if (IsEmptyStr(filename)) {
258 cprintf("%d You must specify a file name.\n",
259 ERROR + FILE_NOT_FOUND);
263 if (CC->download_fp != NULL) {
264 cprintf("%d You already have a download file open.\n",
265 ERROR + RESOURCE_BUSY);
269 if (!strcasecmp(filename, "_userpic_")) {
270 extract_token(which_user, cmdbuf, 1, '|', sizeof which_user);
271 if (CtdlGetUser(&usbuf, which_user) != 0) {
272 cprintf("%d No such user.\n",
273 ERROR + NO_SUCH_USER);
276 snprintf(pathname, sizeof pathname,
280 } else if (!strcasecmp(filename, "_floorpic_")) {
281 which_floor = extract_int(cmdbuf, 1);
282 snprintf(pathname, sizeof pathname,
284 ctdl_image_dir, which_floor);
285 } else if (!strcasecmp(filename, "_roompic_")) {
286 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
288 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
289 filename[a] = tolower(filename[a]);
290 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
294 if (strstr(filename, "../") != NULL)
296 cprintf("%d syntax error.\n",
297 ERROR + ILLEGAL_VALUE);
301 snprintf(pathname, sizeof pathname,
307 CC->download_fp = fopen(pathname, "rb");
308 if (CC->download_fp == NULL) {
309 strcat(pathname, ".gif");
310 CC->download_fp = fopen(pathname, "rb");
312 if (CC->download_fp == NULL) {
313 cprintf("%d Cannot open %s: %s\n",
314 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
317 rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
319 cprintf("%d Cannot access %s: %s\n",
320 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
324 rewind (CC->download_fp);
325 OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
329 * open a file for uploading
331 void cmd_uopn(char *cmdbuf)
335 extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
336 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
337 extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
339 if (CtdlAccessCheck(ac_logged_in)) return;
341 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
342 cprintf("%d No directory in this room.\n",
347 if (IsEmptyStr(CC->upl_file)) {
348 cprintf("%d You must specify a file name.\n",
349 ERROR + FILE_NOT_FOUND);
353 if (CC->upload_fp != NULL) {
354 cprintf("%d You already have a upload file open.\n",
355 ERROR + RESOURCE_BUSY);
359 for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
360 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
361 CC->upl_file[a] = '_';
364 snprintf(CC->upl_path, sizeof CC->upl_path,
367 CC->room.QRdirname, CC->upl_file);
368 snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
373 CC->upload_fp = fopen(CC->upl_path, "r");
374 if (CC->upload_fp != NULL) {
375 fclose(CC->upload_fp);
376 CC->upload_fp = NULL;
377 cprintf("%d '%s' already exists\n",
378 ERROR + ALREADY_EXISTS, CC->upl_path);
382 CC->upload_fp = fopen(CC->upl_path, "wb");
383 if (CC->upload_fp == NULL) {
384 cprintf("%d Cannot open %s: %s\n",
385 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
388 cprintf("%d Ok\n", CIT_OK);
394 * open an image file for uploading
396 void cmd_uimg(char *cmdbuf)
398 int is_this_for_real;
403 if (num_parms(cmdbuf) < 2) {
404 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
408 is_this_for_real = extract_int(cmdbuf, 0);
409 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
410 extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
411 if (CC->upload_fp != NULL) {
412 cprintf("%d You already have an upload file open.\n",
413 ERROR + RESOURCE_BUSY);
417 strcpy(CC->upl_path, "");
419 for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
420 basenm[a] = tolower(basenm[a]);
421 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
426 if (CC->user.axlevel >= AxAideU) {
427 snprintf(CC->upl_path, sizeof CC->upl_path,
433 if (!strcasecmp(basenm, "_userpic_")) {
434 snprintf(CC->upl_path, sizeof CC->upl_path,
440 if ((!strcasecmp(basenm, "_floorpic_"))
441 && (CC->user.axlevel >= AxAideU)) {
442 which_floor = extract_int(cmdbuf, 2);
443 snprintf(CC->upl_path, sizeof CC->upl_path,
449 if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
450 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
453 if (IsEmptyStr(CC->upl_path)) {
454 cprintf("%d Higher access required.\n",
455 ERROR + HIGHER_ACCESS_REQUIRED);
459 if (is_this_for_real == 0) {
460 cprintf("%d Ok to send image\n", CIT_OK);
464 CC->upload_fp = fopen(CC->upl_path, "wb");
465 if (CC->upload_fp == NULL) {
466 cprintf("%d Cannot open %s: %s\n",
467 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
470 cprintf("%d Ok\n", CIT_OK);
471 CC->upload_type = UPL_IMAGE;
476 * close the download file
478 void cmd_clos(char *cmdbuf)
482 if (CC->download_fp == NULL) {
483 cprintf("%d You don't have a download file open.\n",
484 ERROR + RESOURCE_NOT_OPEN);
488 fclose(CC->download_fp);
489 CC->download_fp = NULL;
491 if (CC->dl_is_net == 1) {
493 snprintf(buf, sizeof buf,
500 cprintf("%d Ok\n", CIT_OK);
507 void abort_upl(CitContext *who)
509 if (who->upload_fp != NULL) {
510 fclose(who->upload_fp);
511 who->upload_fp = NULL;
512 unlink(CC->upl_path);
519 * close the upload file
521 void cmd_ucls(char *cmd)
524 char upload_notice[512];
527 if (CC->upload_fp == NULL) {
528 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
532 fclose(CC->upload_fp);
533 CC->upload_fp = NULL;
535 if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
536 cprintf("%d Upload completed.\n", CIT_OK);
538 if (CC->upload_type == UPL_NET) {
539 char final_filename[PATH_MAX];
540 snprintf(final_filename, sizeof final_filename,
548 if (link(CC->upl_path, final_filename) == 0) {
549 syslog(LOG_INFO, "UCLS: updoaded %s\n",
551 unlink(CC->upl_path);
554 syslog(LOG_ALERT, "Cannot link %s to %s: %s\n",
555 CC->upl_path, final_filename, strerror(errno)
560 /* FIXME ... here we need to trigger a network run */
563 CC->upload_type = UPL_FILE;
567 if (!strcasecmp(cmd, "1")) {
568 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
569 fp = fopen(CC->upl_filedir, "a");
571 fp = fopen(CC->upl_filedir, "w");
574 fprintf(fp, "%s %s %s\n", CC->upl_file,
580 /* put together an upload notice */
581 snprintf(upload_notice, sizeof upload_notice,
582 "NEW UPLOAD: '%s'\n %s\n%s\n",
586 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
587 upload_notice, 0, NULL);
590 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
596 * read from the download file
598 void cmd_read(char *cmdbuf)
605 /* The client will transmit its requested offset and byte count */
606 start_pos = extract_long(cmdbuf, 0);
607 bytes = extract_int(cmdbuf, 1);
608 if ((start_pos < 0) || (bytes <= 0)) {
609 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
613 if (CC->download_fp == NULL) {
614 cprintf("%d You don't have a download file open.\n",
615 ERROR + RESOURCE_NOT_OPEN);
619 /* If necessary, reduce the byte count to the size of our buffer */
620 if (bytes > sizeof(buf)) {
624 rc = fseek(CC->download_fp, start_pos, 0);
626 cprintf("%d your file is smaller then %ld.\n", ERROR + ILLEGAL_VALUE, start_pos);
627 syslog(LOG_ALERT, "your file %s is smaller then %ld. [%s]\n",
634 bytes = fread(buf, 1, bytes, CC->download_fp);
636 /* Tell the client the actual byte count and transmit it */
637 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
638 client_write(buf, bytes);
641 cprintf("%d %s\n", ERROR, strerror(errno));
647 * write to the upload file
649 void cmd_writ(char *cmdbuf)
657 bytes = extract_int(cmdbuf, 0);
659 if (CC->upload_fp == NULL) {
660 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
664 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
668 if (bytes > 100000) {
672 cprintf("%d %d\n", SEND_BINARY, bytes);
673 buf = malloc(bytes + 1);
674 client_read(buf, bytes);
675 rv = fwrite(buf, bytes, 1, CC->upload_fp);
677 syslog(LOG_EMERG, "Couldn't write: %s\n",
687 * cmd_ndop() - open a network spool file for downloading
689 void cmd_ndop(char *cmdbuf)
694 if (IsEmptyStr(CC->net_node)) {
695 cprintf("%d Not authenticated as a network node.\n",
696 ERROR + NOT_LOGGED_IN);
700 if (CC->download_fp != NULL) {
701 cprintf("%d You already have a download file open.\n",
702 ERROR + RESOURCE_BUSY);
706 snprintf(pathname, sizeof pathname,
711 /* first open the file in append mode in order to create a
712 * zero-length file if it doesn't already exist
714 CC->download_fp = fopen(pathname, "a");
715 if (CC->download_fp != NULL)
716 fclose(CC->download_fp);
719 CC->download_fp = fopen(pathname, "r");
720 if (CC->download_fp == NULL) {
721 cprintf("%d cannot open %s: %s\n",
722 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
727 /* set this flag so other routines know that the download file
728 * currently open is a network spool file
732 stat(pathname, &statbuf);
733 CC->download_fp_total = statbuf.st_size;
734 cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
738 * cmd_nuop() - open a network spool file for uploading
740 void cmd_nuop(char *cmdbuf)
744 if (IsEmptyStr(CC->net_node)) {
745 cprintf("%d Not authenticated as a network node.\n",
746 ERROR + NOT_LOGGED_IN);
750 if (CC->upload_fp != NULL) {
751 cprintf("%d You already have an upload file open.\n",
752 ERROR + RESOURCE_BUSY);
756 snprintf(CC->upl_path, sizeof CC->upl_path,
763 CC->upload_fp = fopen(CC->upl_path, "r");
764 if (CC->upload_fp != NULL) {
765 fclose(CC->upload_fp);
766 CC->upload_fp = NULL;
767 cprintf("%d '%s' already exists\n",
768 ERROR + ALREADY_EXISTS, CC->upl_path);
772 CC->upload_fp = fopen(CC->upl_path, "w");
773 if (CC->upload_fp == NULL) {
774 cprintf("%d Cannot open %s: %s\n",
775 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
779 CC->upload_type = UPL_NET;
780 cprintf("%d Ok\n", CIT_OK);
782 void files_logout_hook(void)
784 CitContext *CCC = MyContext();
787 * If there is a download in progress, abort it.
789 if (CCC->download_fp != NULL) {
790 fclose(CCC->download_fp);
791 CCC->download_fp = NULL;
795 * If there is an upload in progress, abort it.
797 if (CCC->upload_fp != NULL) {
804 * help_subst() - support routine for help file viewer
806 void help_subst(char *strbuf, char *source, char *dest)
811 while (p = pattern2(strbuf, source), (p >= 0)) {
812 strcpy(workbuf, &strbuf[p + strlen(source)]);
813 strcpy(&strbuf[p], dest);
814 strcat(strbuf, workbuf);
818 void do_help_subst(char *buffer)
822 help_subst(buffer, "^nodename", config.c_nodename);
823 help_subst(buffer, "^humannode", config.c_humannode);
824 help_subst(buffer, "^fqdn", config.c_fqdn);
825 help_subst(buffer, "^username", CC->user.fullname);
826 snprintf(buf2, sizeof buf2, "%ld", CC->user.usernum);
827 help_subst(buffer, "^usernum", buf2);
828 help_subst(buffer, "^sysadm", config.c_sysadm);
829 help_subst(buffer, "^variantname", CITADEL);
830 snprintf(buf2, sizeof buf2, "%d", config.c_maxsessions);
831 help_subst(buffer, "^maxsessions", buf2);
832 help_subst(buffer, "^bbsdir", ctdl_message_dir);
836 typedef const char *ccharp;
838 * display system messages or help
840 void cmd_mesg(char *mname)
850 extract_token(buf, mname, 0, '|', sizeof buf);
852 dirs[0] = strdup(ctdl_message_dir);
853 dirs[1] = strdup(ctdl_hlp_dir);
855 snprintf(buf2, sizeof buf2, "%s.%d.%d",
856 buf, CC->cs_clientdev, CC->cs_clienttyp);
858 /* If the client requested "?" then produce a listing */
859 if (!strcmp(buf, "?")) {
860 cprintf("%d %s\n", LISTING_FOLLOWS, buf);
861 dp = opendir(dirs[1]);
863 while (d = readdir(dp), d != NULL) {
864 if (d->d_name[0] != '.') {
865 cprintf(" %s\n", d->d_name);
876 /* Otherwise, look for the requested file by name. */
878 mesg_locate(targ, sizeof targ, buf2, 2, (const ccharp*)dirs);
879 if (IsEmptyStr(targ)) {
880 snprintf(buf2, sizeof buf2, "%s.%d",
881 buf, CC->cs_clientdev);
882 mesg_locate(targ, sizeof targ, buf2, 2,
883 (const ccharp*)dirs);
884 if (IsEmptyStr(targ)) {
885 mesg_locate(targ, sizeof targ, buf, 2,
886 (const ccharp*)dirs);
894 if (IsEmptyStr(targ)) {
895 cprintf("%d '%s' not found. (Searching in %s and %s)\n",
896 ERROR + FILE_NOT_FOUND,
904 mfp = fopen(targ, "r");
906 cprintf("%d Cannot open '%s': %s\n",
907 ERROR + INTERNAL_ERROR, targ, strerror(errno));
910 cprintf("%d %s\n", LISTING_FOLLOWS,buf);
912 while (fgets(buf, (sizeof buf - 1), mfp) != NULL) {
913 buf[strlen(buf)-1] = 0;
924 * enter system messages or help
926 void cmd_emsg(char *mname)
936 if (CtdlAccessCheck(ac_aide)) return;
938 extract_token(buf, mname, 0, '|', sizeof buf);
939 for (a=0; !IsEmptyStr(&buf[a]); ++a) { /* security measure */
940 if (buf[a] == '/') buf[a] = '.';
943 dirs[0] = strdup(ctdl_message_dir);
944 dirs[1] = strdup(ctdl_hlp_dir);
946 mesg_locate(targ, sizeof targ, buf, 2, (const ccharp*)dirs);
950 if (IsEmptyStr(targ)) {
951 snprintf(targ, sizeof targ,
956 mfp = fopen(targ,"w");
958 cprintf("%d Cannot open '%s': %s\n",
959 ERROR + INTERNAL_ERROR, targ, strerror(errno));
962 cprintf("%d %s\n", SEND_LISTING, targ);
964 while (client_getln(buf, sizeof buf) >=0 && strcmp(buf, "000")) {
965 fprintf(mfp, "%s\n", buf);
971 /*****************************************************************************/
972 /* MODULE INITIALIZATION STUFF */
973 /*****************************************************************************/
975 CTDL_MODULE_INIT(file_ops)
978 CtdlRegisterSessionHook(files_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 8);
980 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
981 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
982 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
983 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
984 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
985 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
986 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
987 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
988 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
989 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
990 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
991 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
993 CtdlRegisterProtoHook(cmd_mesg, "MESG", "fetch system banners");
994 CtdlRegisterProtoHook(cmd_emsg, "EMSG", "submit system banners");
996 /* return our Subversion id for the Log */