2 * Server functions which handle file transfers and room directories.
6 #include <libcitadel.h>
9 #include "ctdl_module.h"
10 #include "citserver.h"
15 * Server command to delete a file from a room's directory
17 void cmd_delf(char *filename)
22 if (CtdlAccessCheck(ac_room_aide))
25 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
26 cprintf("%d No directory in this room.\n",
31 if (IsEmptyStr(filename)) {
32 cprintf("%d You must specify a file name.\n",
33 ERROR + FILE_NOT_FOUND);
36 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
37 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
41 snprintf(pathname, sizeof pathname,
44 CC->room.QRdirname, filename);
47 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
50 cprintf("%d File '%s' not found.\n",
51 ERROR + FILE_NOT_FOUND, pathname);
59 * move a file from one room directory to another
61 void cmd_movf(char *cmdbuf)
63 char filename[PATH_MAX];
64 char pathname[PATH_MAX];
65 char newpath[PATH_MAX];
66 char newroom[ROOMNAMELEN];
69 struct ctdlroom qrbuf;
71 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
72 extract_token(newroom, cmdbuf, 1, '|', sizeof newroom);
74 if (CtdlAccessCheck(ac_room_aide)) return;
76 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
77 cprintf("%d No directory in this room.\n",
82 if (IsEmptyStr(filename)) {
83 cprintf("%d You must specify a file name.\n",
84 ERROR + FILE_NOT_FOUND);
88 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
89 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
93 snprintf(pathname, sizeof pathname, "./files/%s/%s",
94 CC->room.QRdirname, filename);
95 if (access(pathname, 0) != 0) {
96 cprintf("%d File '%s' not found.\n",
97 ERROR + FILE_NOT_FOUND, pathname);
101 if (CtdlGetRoom(&qrbuf, newroom) != 0) {
102 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
105 if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
106 cprintf("%d '%s' is not a directory room.\n",
107 ERROR + NOT_HERE, qrbuf.QRname);
110 snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
112 if (link(pathname, newpath) != 0) {
113 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR,
119 /* this is a crude method of copying the file description */
120 snprintf(buf, sizeof buf,
121 "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir",
122 CC->room.QRdirname, filename, qrbuf.QRdirname);
124 cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
129 * This code is common to all commands which open a file for downloading,
130 * regardless of whether it's a file from the directory, an image, a network
131 * spool file, a MIME attachment, etc.
132 * It examines the file and displays the OK result code and some information
133 * about the file. NOTE: this stuff is Unix dependent.
135 void OpenCmdResult(char *filename, const char *mime_type)
141 fstat(fileno(CC->download_fp), &statbuf);
142 CC->download_fp_total = statbuf.st_size;
143 filesize = (long) statbuf.st_size;
144 modtime = (time_t) statbuf.st_mtime;
146 cprintf("%d %ld|%ld|%s|%s\n",
147 CIT_OK, filesize, (long)modtime, filename, mime_type);
152 * open a file for downloading
154 void cmd_open(char *cmdbuf)
157 char pathname[PATH_MAX];
160 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
162 if (CtdlAccessCheck(ac_logged_in)) return;
164 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
165 cprintf("%d No directory in this room.\n",
170 if (IsEmptyStr(filename)) {
171 cprintf("%d You must specify a file name.\n",
172 ERROR + FILE_NOT_FOUND);
175 if (strstr(filename, "../") != NULL)
177 cprintf("%d syntax error.\n",
178 ERROR + ILLEGAL_VALUE);
182 if (CC->download_fp != NULL) {
183 cprintf("%d You already have a download file open.\n",
184 ERROR + RESOURCE_BUSY);
188 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
189 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
194 snprintf(pathname, sizeof pathname,
197 CC->room.QRdirname, filename);
198 CC->download_fp = fopen(pathname, "r");
200 if (CC->download_fp == NULL) {
201 cprintf("%d cannot open %s: %s\n",
202 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
206 OpenCmdResult(filename, "application/octet-stream");
212 void cmd_oimg(char *cmdbuf)
215 char pathname[PATH_MAX];
216 char MimeTestBuf[32];
217 struct ctdluser usbuf;
218 char which_user[USERNAME_SIZE];
223 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
225 if (IsEmptyStr(filename)) {
226 cprintf("%d You must specify a file name.\n",
227 ERROR + FILE_NOT_FOUND);
231 if (CC->download_fp != NULL) {
232 cprintf("%d You already have a download file open.\n",
233 ERROR + RESOURCE_BUSY);
237 if (!strcasecmp(filename, "_userpic_")) {
238 extract_token(which_user, cmdbuf, 1, '|', sizeof which_user);
239 if (CtdlGetUser(&usbuf, which_user) != 0) {
240 cprintf("%d No such user.\n",
241 ERROR + NO_SUCH_USER);
244 snprintf(pathname, sizeof pathname,
248 } else if (!strcasecmp(filename, "_floorpic_")) {
249 which_floor = extract_int(cmdbuf, 1);
250 snprintf(pathname, sizeof pathname,
252 ctdl_image_dir, which_floor);
253 } else if (!strcasecmp(filename, "_roompic_")) {
254 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
256 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
257 filename[a] = tolower(filename[a]);
258 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
262 if (strstr(filename, "../") != NULL)
264 cprintf("%d syntax error.\n",
265 ERROR + ILLEGAL_VALUE);
269 snprintf(pathname, sizeof pathname,
275 CC->download_fp = fopen(pathname, "rb");
276 if (CC->download_fp == NULL) {
277 strcat(pathname, ".gif");
278 CC->download_fp = fopen(pathname, "rb");
280 if (CC->download_fp == NULL) {
281 cprintf("%d Cannot open %s: %s\n",
282 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
285 rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
287 cprintf("%d Cannot access %s: %s\n",
288 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
292 rewind (CC->download_fp);
293 OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
297 * open a file for uploading
299 void cmd_uopn(char *cmdbuf)
303 extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
304 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
305 extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
307 if (CtdlAccessCheck(ac_logged_in)) return;
309 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
310 cprintf("%d No directory in this room.\n",
315 if (IsEmptyStr(CC->upl_file)) {
316 cprintf("%d You must specify a file name.\n",
317 ERROR + FILE_NOT_FOUND);
321 if (CC->upload_fp != NULL) {
322 cprintf("%d You already have a upload file open.\n",
323 ERROR + RESOURCE_BUSY);
327 for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
328 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
329 CC->upl_file[a] = '_';
332 snprintf(CC->upl_path, sizeof CC->upl_path,
335 CC->room.QRdirname, CC->upl_file);
336 snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
341 CC->upload_fp = fopen(CC->upl_path, "r");
342 if (CC->upload_fp != NULL) {
343 fclose(CC->upload_fp);
344 CC->upload_fp = NULL;
345 cprintf("%d '%s' already exists\n",
346 ERROR + ALREADY_EXISTS, CC->upl_path);
350 CC->upload_fp = fopen(CC->upl_path, "wb");
351 if (CC->upload_fp == NULL) {
352 cprintf("%d Cannot open %s: %s\n",
353 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
356 cprintf("%d Ok\n", CIT_OK);
362 * open an image file for uploading
364 void cmd_uimg(char *cmdbuf)
366 int is_this_for_real;
371 if (num_parms(cmdbuf) < 2) {
372 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
376 is_this_for_real = extract_int(cmdbuf, 0);
377 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
378 extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
379 if (CC->upload_fp != NULL) {
380 cprintf("%d You already have an upload file open.\n",
381 ERROR + RESOURCE_BUSY);
385 strcpy(CC->upl_path, "");
387 for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
388 basenm[a] = tolower(basenm[a]);
389 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
394 if (CC->user.axlevel >= AxAideU) {
395 snprintf(CC->upl_path, sizeof CC->upl_path,
401 if (!strcasecmp(basenm, "_userpic_")) {
402 snprintf(CC->upl_path, sizeof CC->upl_path,
408 if ((!strcasecmp(basenm, "_floorpic_"))
409 && (CC->user.axlevel >= AxAideU)) {
410 which_floor = extract_int(cmdbuf, 2);
411 snprintf(CC->upl_path, sizeof CC->upl_path,
417 if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
418 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
421 if (IsEmptyStr(CC->upl_path)) {
422 cprintf("%d Higher access required.\n",
423 ERROR + HIGHER_ACCESS_REQUIRED);
427 if (is_this_for_real == 0) {
428 cprintf("%d Ok to send image\n", CIT_OK);
432 CC->upload_fp = fopen(CC->upl_path, "wb");
433 if (CC->upload_fp == NULL) {
434 cprintf("%d Cannot open %s: %s\n",
435 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
438 cprintf("%d Ok\n", CIT_OK);
439 CC->upload_type = UPL_IMAGE;
444 * close the download file
446 void cmd_clos(char *cmdbuf)
450 if (CC->download_fp == NULL) {
451 cprintf("%d You don't have a download file open.\n",
452 ERROR + RESOURCE_NOT_OPEN);
456 fclose(CC->download_fp);
457 CC->download_fp = NULL;
459 if (CC->dl_is_net == 1) {
461 snprintf(buf, sizeof buf,
468 cprintf("%d Ok\n", CIT_OK);
475 void abort_upl(CitContext *who)
477 if (who->upload_fp != NULL) {
478 fclose(who->upload_fp);
479 who->upload_fp = NULL;
480 unlink(CC->upl_path);
487 * close the upload file
489 void cmd_ucls(char *cmd)
492 char upload_notice[512];
495 if (CC->upload_fp == NULL) {
496 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
500 fclose(CC->upload_fp);
501 CC->upload_fp = NULL;
503 if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
504 cprintf("%d Upload completed.\n", CIT_OK);
506 if (CC->upload_type == UPL_NET) {
507 char final_filename[PATH_MAX];
508 snprintf(final_filename, sizeof final_filename,
516 if (link(CC->upl_path, final_filename) == 0) {
517 syslog(LOG_INFO, "UCLS: updoaded %s\n",
519 unlink(CC->upl_path);
522 syslog(LOG_ALERT, "Cannot link %s to %s: %s\n",
523 CC->upl_path, final_filename, strerror(errno)
528 /* FIXME ... here we need to trigger a network run */
531 CC->upload_type = UPL_FILE;
535 if (!strcasecmp(cmd, "1")) {
536 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
537 fp = fopen(CC->upl_filedir, "a");
539 fp = fopen(CC->upl_filedir, "w");
542 fprintf(fp, "%s %s %s\n", CC->upl_file,
548 /* put together an upload notice */
549 snprintf(upload_notice, sizeof upload_notice,
550 "NEW UPLOAD: '%s'\n %s\n%s\n",
554 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
555 upload_notice, 0, NULL);
558 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
564 * read from the download file
566 void cmd_read(char *cmdbuf)
573 /* The client will transmit its requested offset and byte count */
574 start_pos = extract_long(cmdbuf, 0);
575 bytes = extract_int(cmdbuf, 1);
576 if ((start_pos < 0) || (bytes <= 0)) {
577 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
581 if (CC->download_fp == NULL) {
582 cprintf("%d You don't have a download file open.\n",
583 ERROR + RESOURCE_NOT_OPEN);
587 /* If necessary, reduce the byte count to the size of our buffer */
588 if (bytes > sizeof(buf)) {
592 rc = fseek(CC->download_fp, start_pos, 0);
594 cprintf("%d your file is smaller then %ld.\n", ERROR + ILLEGAL_VALUE, start_pos);
595 syslog(LOG_ALERT, "your file %s is smaller then %ld. [%s]\n",
602 bytes = fread(buf, 1, bytes, CC->download_fp);
604 /* Tell the client the actual byte count and transmit it */
605 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
606 client_write(buf, bytes);
609 cprintf("%d %s\n", ERROR, strerror(errno));
615 * write to the upload file
617 void cmd_writ(char *cmdbuf)
625 bytes = extract_int(cmdbuf, 0);
627 if (CC->upload_fp == NULL) {
628 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
632 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
636 if (bytes > 100000) {
640 cprintf("%d %d\n", SEND_BINARY, bytes);
641 buf = malloc(bytes + 1);
642 client_read(buf, bytes);
643 rv = fwrite(buf, bytes, 1, CC->upload_fp);
645 syslog(LOG_EMERG, "Couldn't write: %s\n",
655 * cmd_ndop() - open a network spool file for downloading
657 void cmd_ndop(char *cmdbuf)
662 if (IsEmptyStr(CC->net_node)) {
663 cprintf("%d Not authenticated as a network node.\n",
664 ERROR + NOT_LOGGED_IN);
668 if (CC->download_fp != NULL) {
669 cprintf("%d You already have a download file open.\n",
670 ERROR + RESOURCE_BUSY);
674 snprintf(pathname, sizeof pathname,
679 /* first open the file in append mode in order to create a
680 * zero-length file if it doesn't already exist
682 CC->download_fp = fopen(pathname, "a");
683 if (CC->download_fp != NULL)
684 fclose(CC->download_fp);
687 CC->download_fp = fopen(pathname, "r");
688 if (CC->download_fp == NULL) {
689 cprintf("%d cannot open %s: %s\n",
690 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
695 /* set this flag so other routines know that the download file
696 * currently open is a network spool file
700 stat(pathname, &statbuf);
701 CC->download_fp_total = statbuf.st_size;
702 cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
706 * cmd_nuop() - open a network spool file for uploading
708 void cmd_nuop(char *cmdbuf)
712 if (IsEmptyStr(CC->net_node)) {
713 cprintf("%d Not authenticated as a network node.\n",
714 ERROR + NOT_LOGGED_IN);
718 if (CC->upload_fp != NULL) {
719 cprintf("%d You already have an upload file open.\n",
720 ERROR + RESOURCE_BUSY);
724 snprintf(CC->upl_path, sizeof CC->upl_path,
731 CC->upload_fp = fopen(CC->upl_path, "r");
732 if (CC->upload_fp != NULL) {
733 fclose(CC->upload_fp);
734 CC->upload_fp = NULL;
735 cprintf("%d '%s' already exists\n",
736 ERROR + ALREADY_EXISTS, CC->upl_path);
740 CC->upload_fp = fopen(CC->upl_path, "w");
741 if (CC->upload_fp == NULL) {
742 cprintf("%d Cannot open %s: %s\n",
743 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
747 CC->upload_type = UPL_NET;
748 cprintf("%d Ok\n", CIT_OK);
750 void files_logout_hook(void)
752 CitContext *CCC = MyContext();
755 * If there is a download in progress, abort it.
757 if (CCC->download_fp != NULL) {
758 fclose(CCC->download_fp);
759 CCC->download_fp = NULL;
763 * If there is an upload in progress, abort it.
765 if (CCC->upload_fp != NULL) {
772 * help_subst() - support routine for help file viewer
774 void help_subst(char *strbuf, char *source, char *dest)
779 while (p = pattern2(strbuf, source), (p >= 0)) {
780 strcpy(workbuf, &strbuf[p + strlen(source)]);
781 strcpy(&strbuf[p], dest);
782 strcat(strbuf, workbuf);
786 void do_help_subst(char *buffer)
790 help_subst(buffer, "^nodename", config.c_nodename);
791 help_subst(buffer, "^humannode", config.c_humannode);
792 help_subst(buffer, "^fqdn", config.c_fqdn);
793 help_subst(buffer, "^username", CC->user.fullname);
794 snprintf(buf2, sizeof buf2, "%ld", CC->user.usernum);
795 help_subst(buffer, "^usernum", buf2);
796 help_subst(buffer, "^sysadm", config.c_sysadm);
797 help_subst(buffer, "^variantname", CITADEL);
798 snprintf(buf2, sizeof buf2, "%d", config.c_maxsessions);
799 help_subst(buffer, "^maxsessions", buf2);
800 help_subst(buffer, "^bbsdir", ctdl_message_dir);
804 typedef const char *ccharp;
806 * display system messages or help
808 void cmd_mesg(char *mname)
818 extract_token(buf, mname, 0, '|', sizeof buf);
820 dirs[0] = strdup(ctdl_message_dir);
821 dirs[1] = strdup(ctdl_hlp_dir);
823 snprintf(buf2, sizeof buf2, "%s.%d.%d",
824 buf, CC->cs_clientdev, CC->cs_clienttyp);
826 /* If the client requested "?" then produce a listing */
827 if (!strcmp(buf, "?")) {
828 cprintf("%d %s\n", LISTING_FOLLOWS, buf);
829 dp = opendir(dirs[1]);
831 while (d = readdir(dp), d != NULL) {
832 if (d->d_name[0] != '.') {
833 cprintf(" %s\n", d->d_name);
844 /* Otherwise, look for the requested file by name. */
846 mesg_locate(targ, sizeof targ, buf2, 2, (const ccharp*)dirs);
847 if (IsEmptyStr(targ)) {
848 snprintf(buf2, sizeof buf2, "%s.%d",
849 buf, CC->cs_clientdev);
850 mesg_locate(targ, sizeof targ, buf2, 2,
851 (const ccharp*)dirs);
852 if (IsEmptyStr(targ)) {
853 mesg_locate(targ, sizeof targ, buf, 2,
854 (const ccharp*)dirs);
862 if (IsEmptyStr(targ)) {
863 cprintf("%d '%s' not found. (Searching in %s and %s)\n",
864 ERROR + FILE_NOT_FOUND,
872 mfp = fopen(targ, "r");
874 cprintf("%d Cannot open '%s': %s\n",
875 ERROR + INTERNAL_ERROR, targ, strerror(errno));
878 cprintf("%d %s\n", LISTING_FOLLOWS,buf);
880 while (fgets(buf, (sizeof buf - 1), mfp) != NULL) {
881 buf[strlen(buf)-1] = 0;
892 * enter system messages or help
894 void cmd_emsg(char *mname)
904 if (CtdlAccessCheck(ac_aide)) return;
906 extract_token(buf, mname, 0, '|', sizeof buf);
907 for (a=0; !IsEmptyStr(&buf[a]); ++a) { /* security measure */
908 if (buf[a] == '/') buf[a] = '.';
911 dirs[0] = strdup(ctdl_message_dir);
912 dirs[1] = strdup(ctdl_hlp_dir);
914 mesg_locate(targ, sizeof targ, buf, 2, (const ccharp*)dirs);
918 if (IsEmptyStr(targ)) {
919 snprintf(targ, sizeof targ,
924 mfp = fopen(targ,"w");
926 cprintf("%d Cannot open '%s': %s\n",
927 ERROR + INTERNAL_ERROR, targ, strerror(errno));
930 cprintf("%d %s\n", SEND_LISTING, targ);
932 while (client_getln(buf, sizeof buf) >=0 && strcmp(buf, "000")) {
933 fprintf(mfp, "%s\n", buf);
939 /*****************************************************************************/
940 /* MODULE INITIALIZATION STUFF */
941 /*****************************************************************************/
943 CTDL_MODULE_INIT(file_ops)
946 CtdlRegisterSessionHook(files_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 8);
948 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
949 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
950 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
951 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
952 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
953 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
954 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
955 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
956 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
957 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
958 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
959 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
961 CtdlRegisterProtoHook(cmd_mesg, "MESG", "fetch system banners");
962 CtdlRegisterProtoHook(cmd_emsg, "EMSG", "submit system banners");
964 /* return our Subversion id for the Log */