2 * Server functions which handle file transfers and room directories.
4 * Copyright (c) 1987-2015 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <libcitadel.h>
19 #include "ctdl_module.h"
20 #include "citserver.h"
27 * Server command to delete a file from a room's directory
29 void cmd_delf(char *filename)
34 if (CtdlAccessCheck(ac_room_aide))
37 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
38 cprintf("%d No directory in this room.\n",
43 if (IsEmptyStr(filename)) {
44 cprintf("%d You must specify a file name.\n",
45 ERROR + FILE_NOT_FOUND);
48 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
49 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
53 snprintf(pathname, sizeof pathname,
56 CC->room.QRdirname, filename);
59 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
62 cprintf("%d File '%s' not found.\n",
63 ERROR + FILE_NOT_FOUND, pathname);
71 * move a file from one room directory to another
73 void cmd_movf(char *cmdbuf)
75 char filename[PATH_MAX];
76 char pathname[PATH_MAX];
77 char newpath[PATH_MAX];
78 char newroom[ROOMNAMELEN];
81 struct ctdlroom qrbuf;
83 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
84 extract_token(newroom, cmdbuf, 1, '|', sizeof newroom);
86 if (CtdlAccessCheck(ac_room_aide)) return;
88 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
89 cprintf("%d No directory in this room.\n",
94 if (IsEmptyStr(filename)) {
95 cprintf("%d You must specify a file name.\n",
96 ERROR + FILE_NOT_FOUND);
100 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
101 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
105 snprintf(pathname, sizeof pathname, "./files/%s/%s",
106 CC->room.QRdirname, filename);
107 if (access(pathname, 0) != 0) {
108 cprintf("%d File '%s' not found.\n",
109 ERROR + FILE_NOT_FOUND, pathname);
113 if (CtdlGetRoom(&qrbuf, newroom) != 0) {
114 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
117 if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
118 cprintf("%d '%s' is not a directory room.\n",
119 ERROR + NOT_HERE, qrbuf.QRname);
122 snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
124 if (link(pathname, newpath) != 0) {
125 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR,
131 /* this is a crude method of copying the file description */
132 snprintf(buf, sizeof buf,
133 "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir",
134 CC->room.QRdirname, filename, qrbuf.QRdirname);
136 cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
141 * This code is common to all commands which open a file for downloading,
142 * regardless of whether it's a file from the directory, an image, a network
143 * spool file, a MIME attachment, etc.
144 * It examines the file and displays the OK result code and some information
145 * about the file. NOTE: this stuff is Unix dependent.
147 void OpenCmdResult(char *filename, const char *mime_type)
153 fstat(fileno(CC->download_fp), &statbuf);
154 CC->download_fp_total = statbuf.st_size;
155 filesize = (long) statbuf.st_size;
156 modtime = (time_t) statbuf.st_mtime;
158 cprintf("%d %ld|%ld|%s|%s\n",
159 CIT_OK, filesize, (long)modtime, filename, mime_type);
164 * open a file for downloading
166 void cmd_open(char *cmdbuf)
169 char pathname[PATH_MAX];
172 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
174 if (CtdlAccessCheck(ac_logged_in)) return;
176 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
177 cprintf("%d No directory in this room.\n",
182 if (IsEmptyStr(filename)) {
183 cprintf("%d You must specify a file name.\n",
184 ERROR + FILE_NOT_FOUND);
187 if (strstr(filename, "../") != NULL)
189 cprintf("%d syntax error.\n",
190 ERROR + ILLEGAL_VALUE);
194 if (CC->download_fp != NULL) {
195 cprintf("%d You already have a download file open.\n",
196 ERROR + RESOURCE_BUSY);
200 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
201 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
206 snprintf(pathname, sizeof pathname,
209 CC->room.QRdirname, filename);
210 CC->download_fp = fopen(pathname, "r");
212 if (CC->download_fp == NULL) {
213 cprintf("%d cannot open %s: %s\n",
214 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
218 OpenCmdResult(filename, "application/octet-stream");
224 void cmd_oimg(char *cmdbuf)
226 char filename[PATH_MAX];
227 char pathname[PATH_MAX];
228 char MimeTestBuf[32];
233 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
235 if (IsEmptyStr(filename)) {
236 cprintf("%d You must specify a file name.\n",
237 ERROR + FILE_NOT_FOUND);
241 if (CC->download_fp != NULL) {
242 cprintf("%d You already have a download file open.\n",
243 ERROR + RESOURCE_BUSY);
247 if (!strcasecmp(filename, "_floorpic_")) {
248 which_floor = extract_int(cmdbuf, 1);
249 snprintf(pathname, sizeof pathname, "%s/floor.%d", ctdl_image_dir, which_floor);
251 else if (!strcasecmp(filename, "_roompic_")) {
252 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
255 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
256 filename[a] = tolower(filename[a]);
257 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
261 if (strstr(filename, "../") != NULL)
263 cprintf("%d syntax error.\n",
264 ERROR + ILLEGAL_VALUE);
268 snprintf(pathname, sizeof pathname,
274 CC->download_fp = fopen(pathname, "rb");
275 if (CC->download_fp == NULL) {
276 strcat(pathname, ".gif");
277 CC->download_fp = fopen(pathname, "rb");
279 if (CC->download_fp == NULL) {
280 cprintf("%d Cannot open %s: %s\n",
281 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
284 rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
286 cprintf("%d Cannot access %s: %s\n",
287 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
291 rewind (CC->download_fp);
292 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, "_floorpic_"))
402 && (CC->user.axlevel >= AxAideU)) {
403 which_floor = extract_int(cmdbuf, 2);
404 snprintf(CC->upl_path, sizeof CC->upl_path,
410 if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
411 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
414 if (IsEmptyStr(CC->upl_path)) {
415 cprintf("%d Higher access required.\n",
416 ERROR + HIGHER_ACCESS_REQUIRED);
420 if (is_this_for_real == 0) {
421 cprintf("%d Ok to send image\n", CIT_OK);
425 CC->upload_fp = fopen(CC->upl_path, "wb");
426 if (CC->upload_fp == NULL) {
427 cprintf("%d Cannot open %s: %s\n",
428 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
431 cprintf("%d Ok\n", CIT_OK);
432 CC->upload_type = UPL_IMAGE;
437 * close the download file
439 void cmd_clos(char *cmdbuf)
443 if (CC->download_fp == NULL) {
444 cprintf("%d You don't have a download file open.\n",
445 ERROR + RESOURCE_NOT_OPEN);
449 fclose(CC->download_fp);
450 CC->download_fp = NULL;
452 if (CC->dl_is_net == 1) {
454 snprintf(buf, sizeof buf,
461 cprintf("%d Ok\n", CIT_OK);
468 void abort_upl(CitContext *who)
470 if (who->upload_fp != NULL) {
471 fclose(who->upload_fp);
472 who->upload_fp = NULL;
473 unlink(CC->upl_path);
480 * close the upload file
482 void cmd_ucls(char *cmd)
484 struct CitContext *CCC = CC;
486 char upload_notice[512];
489 if (CCC->upload_fp == NULL) {
490 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
494 fclose(CC->upload_fp);
495 CCC->upload_fp = NULL;
497 if ((!strcasecmp(cmd, "1")) && (CCC->upload_type != UPL_FILE)) {
498 cprintf("%d Upload completed.\n", CIT_OK);
500 if (CCC->upload_type == UPL_NET) {
501 char final_filename[PATH_MAX];
502 snprintf(final_filename, sizeof final_filename,
510 if (link(CCC->upl_path, final_filename) == 0) {
511 CTDL_syslog(LOG_INFO, "UCLS: updoaded %s",
513 unlink(CCC->upl_path);
516 CTDL_syslog(LOG_INFO, "Cannot link %s to %s: %s",
517 CCC->upl_path, final_filename, strerror(errno)
522 /* FIXME ... here we need to trigger a network run */
525 CCC->upload_type = UPL_FILE;
529 if (!strcasecmp(cmd, "1")) {
530 cprintf("%d File '%s' saved.\n", CIT_OK, CCC->upl_path);
531 fp = fopen(CCC->upl_filedir, "a");
533 fp = fopen(CCC->upl_filedir, "w");
536 fprintf(fp, "%s %s %s\n", CCC->upl_file,
542 if ((CCC->room.QRflags2 & QR2_NOUPLMSG) == 0) {
543 /* put together an upload notice */
544 snprintf(upload_notice, sizeof upload_notice,
545 "NEW UPLOAD: '%s'\n %s\n%s\n",
549 quickie_message(CCC->curr_user, NULL, NULL, CCC->room.QRname,
550 upload_notice, 0, NULL);
554 cprintf("%d File '%s' aborted.\n", CIT_OK, CCC->upl_path);
560 * read from the download file
562 void cmd_read(char *cmdbuf)
569 /* The client will transmit its requested offset and byte count */
570 start_pos = extract_long(cmdbuf, 0);
571 bytes = extract_int(cmdbuf, 1);
572 if ((start_pos < 0) || (bytes <= 0)) {
573 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
577 if (CC->download_fp == NULL) {
578 cprintf("%d You don't have a download file open.\n",
579 ERROR + RESOURCE_NOT_OPEN);
583 /* If necessary, reduce the byte count to the size of our buffer */
584 if (bytes > sizeof(buf)) {
588 rc = fseek(CC->download_fp, start_pos, 0);
590 struct CitContext *CCC = CC;
591 cprintf("%d your file is smaller then %ld.\n", ERROR + ILLEGAL_VALUE, start_pos);
592 CTDL_syslog(LOG_ERR, "your file %s is smaller then %ld. [%s]\n",
599 bytes = fread(buf, 1, bytes, CC->download_fp);
601 /* Tell the client the actual byte count and transmit it */
602 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
603 client_write(buf, bytes);
606 cprintf("%d %s\n", ERROR, strerror(errno));
612 * write to the upload file
614 void cmd_writ(char *cmdbuf)
616 struct CitContext *CCC = CC;
623 bytes = extract_int(cmdbuf, 0);
625 if (CCC->upload_fp == NULL) {
626 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
630 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
634 if (bytes > 100000) {
638 cprintf("%d %d\n", SEND_BINARY, bytes);
639 buf = malloc(bytes + 1);
640 client_read(buf, bytes);
641 rv = fwrite(buf, bytes, 1, CCC->upload_fp);
643 CTDL_syslog(LOG_EMERG, "Couldn't write: %s\n",
653 * cmd_ndop() - open a network spool file for downloading
655 void cmd_ndop(char *cmdbuf)
657 struct CitContext *CCC = CC;
661 if (IsEmptyStr(CCC->net_node)) {
662 cprintf("%d Not authenticated as a network node.\n",
663 ERROR + NOT_LOGGED_IN);
667 if (CCC->download_fp != NULL) {
668 cprintf("%d You already have a download file open.\n",
669 ERROR + RESOURCE_BUSY);
673 snprintf(pathname, sizeof pathname,
678 /* first open the file in append mode in order to create a
679 * zero-length file if it doesn't already exist
681 CCC->download_fp = fopen(pathname, "a");
682 if (CCC->download_fp != NULL)
683 fclose(CCC->download_fp);
686 CCC->download_fp = fopen(pathname, "r");
687 if (CCC->download_fp == NULL) {
688 cprintf("%d cannot open %s: %s\n",
689 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
694 /* set this flag so other routines know that the download file
695 * currently open is a network spool file
699 stat(pathname, &statbuf);
700 CCC->download_fp_total = statbuf.st_size;
701 cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
705 * cmd_nuop() - open a network spool file for uploading
707 void cmd_nuop(char *cmdbuf)
711 if (IsEmptyStr(CC->net_node)) {
712 cprintf("%d Not authenticated as a network node.\n",
713 ERROR + NOT_LOGGED_IN);
717 if (CC->upload_fp != NULL) {
718 cprintf("%d You already have an upload file open.\n",
719 ERROR + RESOURCE_BUSY);
723 snprintf(CC->upl_path, sizeof CC->upl_path,
730 CC->upload_fp = fopen(CC->upl_path, "r");
731 if (CC->upload_fp != NULL) {
732 fclose(CC->upload_fp);
733 CC->upload_fp = NULL;
734 cprintf("%d '%s' already exists\n",
735 ERROR + ALREADY_EXISTS, CC->upl_path);
739 CC->upload_fp = fopen(CC->upl_path, "w");
740 if (CC->upload_fp == NULL) {
741 cprintf("%d Cannot open %s: %s\n",
742 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
746 CC->upload_type = UPL_NET;
747 cprintf("%d Ok\n", CIT_OK);
749 void files_logout_hook(void)
751 CitContext *CCC = MyContext();
754 * If there is a download in progress, abort it.
756 if (CCC->download_fp != NULL) {
757 fclose(CCC->download_fp);
758 CCC->download_fp = NULL;
762 * If there is an upload in progress, abort it.
764 if (CCC->upload_fp != NULL) {
771 * help_subst() - support routine for help file viewer
773 void help_subst(char *strbuf, char *source, char *dest)
778 while (p = pattern2(strbuf, source), (p >= 0)) {
779 strcpy(workbuf, &strbuf[p + strlen(source)]);
780 strcpy(&strbuf[p], dest);
781 strcat(strbuf, workbuf);
785 void do_help_subst(char *buffer)
789 help_subst(buffer, "^nodename", CtdlGetConfigStr("c_nodename"));
790 help_subst(buffer, "^humannode", CtdlGetConfigStr("c_humannode"));
791 help_subst(buffer, "^fqdn", CtdlGetConfigStr("c_fqdn"));
792 help_subst(buffer, "^username", CC->user.fullname);
793 snprintf(buf2, sizeof buf2, "%ld", CC->user.usernum);
794 help_subst(buffer, "^usernum", buf2);
795 help_subst(buffer, "^sysadm", CtdlGetConfigStr("c_sysadm"));
796 help_subst(buffer, "^variantname", CITADEL);
797 help_subst(buffer, "^maxsessions", CtdlGetConfigStr("c_maxsessions")); // yes it's numeric but str is ok here
798 help_subst(buffer, "^bbsdir", ctdl_message_dir);
802 typedef const char *ccharp;
804 * display system messages or help
806 void cmd_mesg(char *mname)
816 extract_token(buf, mname, 0, '|', sizeof buf);
818 dirs[0] = strdup(ctdl_message_dir);
819 dirs[1] = strdup(ctdl_hlp_dir);
821 snprintf(buf2, sizeof buf2, "%s.%d.%d",
822 buf, CC->cs_clientdev, CC->cs_clienttyp);
824 /* If the client requested "?" then produce a listing */
825 if (!strcmp(buf, "?")) {
826 cprintf("%d %s\n", LISTING_FOLLOWS, buf);
827 dp = opendir(dirs[1]);
829 while (d = readdir(dp), d != NULL) {
830 if (d->d_name[0] != '.') {
831 cprintf(" %s\n", d->d_name);
842 /* Otherwise, look for the requested file by name. */
844 mesg_locate(targ, sizeof targ, buf2, 2, (const ccharp*)dirs);
845 if (IsEmptyStr(targ)) {
846 snprintf(buf2, sizeof buf2, "%s.%d",
847 buf, CC->cs_clientdev);
848 mesg_locate(targ, sizeof targ, buf2, 2,
849 (const ccharp*)dirs);
850 if (IsEmptyStr(targ)) {
851 mesg_locate(targ, sizeof targ, buf, 2,
852 (const ccharp*)dirs);
860 if (IsEmptyStr(targ)) {
861 cprintf("%d '%s' not found. (Searching in %s and %s)\n",
862 ERROR + FILE_NOT_FOUND,
870 mfp = fopen(targ, "r");
872 cprintf("%d Cannot open '%s': %s\n",
873 ERROR + INTERNAL_ERROR, targ, strerror(errno));
876 cprintf("%d %s\n", LISTING_FOLLOWS,buf);
878 while (fgets(buf, (sizeof buf - 1), mfp) != NULL) {
879 buf[strlen(buf)-1] = 0;
890 * enter system messages or help
892 void cmd_emsg(char *mname)
902 if (CtdlAccessCheck(ac_aide)) return;
904 extract_token(buf, mname, 0, '|', sizeof buf);
905 for (a=0; !IsEmptyStr(&buf[a]); ++a) { /* security measure */
906 if (buf[a] == '/') buf[a] = '.';
909 dirs[0] = strdup(ctdl_message_dir);
910 dirs[1] = strdup(ctdl_hlp_dir);
912 mesg_locate(targ, sizeof targ, buf, 2, (const ccharp*)dirs);
916 if (IsEmptyStr(targ)) {
917 snprintf(targ, sizeof targ,
922 mfp = fopen(targ,"w");
924 cprintf("%d Cannot open '%s': %s\n",
925 ERROR + INTERNAL_ERROR, targ, strerror(errno));
928 cprintf("%d %s\n", SEND_LISTING, targ);
930 while (client_getln(buf, sizeof buf) >=0 && strcmp(buf, "000")) {
931 fprintf(mfp, "%s\n", buf);
937 /*****************************************************************************/
938 /* MODULE INITIALIZATION STUFF */
939 /*****************************************************************************/
941 CTDL_MODULE_INIT(file_ops)
944 CtdlRegisterSessionHook(files_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 8);
946 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
947 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
948 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
949 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
950 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
951 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
952 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
953 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
954 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
955 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
956 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
957 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
959 CtdlRegisterProtoHook(cmd_mesg, "MESG", "fetch system banners");
960 CtdlRegisterProtoHook(cmd_emsg, "EMSG", "submit system banners");
962 /* return our Subversion id for the Log */