2 * Server functions which handle file transfers and room directories.
4 * Copyright (c) 1987-2022 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.
19 #include <libcitadel.h>
21 #include <sys/types.h>
23 #include "../../ctdl_module.h"
24 #include "../../citserver.h"
25 #include "../../support.h"
26 #include "../../config.h"
27 #include "../../user_ops.h"
31 * Server command to delete a file from a room's directory
33 void cmd_delf(char *filename)
38 if (CtdlAccessCheck(ac_room_aide))
41 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
42 cprintf("%d No directory in this room.\n", ERROR + NOT_HERE);
46 if (IsEmptyStr(filename)) {
47 cprintf("%d You must specify a file name.\n", ERROR + FILE_NOT_FOUND);
50 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
51 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
55 snprintf(pathname, sizeof pathname,
58 CC->room.QRdirname, filename
62 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
65 cprintf("%d File '%s' not found.\n", 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", ERROR + NOT_HERE);
93 if (IsEmptyStr(filename)) {
94 cprintf("%d You must specify a file name.\n", ERROR + FILE_NOT_FOUND);
98 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
99 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
103 snprintf(pathname, sizeof pathname, "./files/%s/%s", CC->room.QRdirname, filename);
104 if (access(pathname, 0) != 0) {
105 cprintf("%d File '%s' not found.\n", ERROR + FILE_NOT_FOUND, pathname);
109 if (CtdlGetRoom(&qrbuf, newroom) != 0) {
110 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
113 if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
114 cprintf("%d '%s' is not a directory room.\n", ERROR + NOT_HERE, qrbuf.QRname);
117 snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname, filename);
118 if (link(pathname, newpath) != 0) {
119 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR, strerror(errno));
124 /* this is a crude method of copying the file description */
125 snprintf(buf, sizeof buf, "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir", CC->room.QRdirname, filename, qrbuf.QRdirname);
127 cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
132 * This code is common to all commands which open a file for downloading,
133 * regardless of whether it's a file from the directory, an image, a network
134 * spool file, a MIME attachment, etc.
135 * It examines the file and displays the OK result code and some information
136 * about the file. NOTE: this stuff is Unix dependent.
138 void OpenCmdResult(char *filename, const char *mime_type)
144 fstat(fileno(CC->download_fp), &statbuf);
145 CC->download_fp_total = statbuf.st_size;
146 filesize = (long) statbuf.st_size;
147 modtime = (time_t) statbuf.st_mtime;
149 cprintf("%d %ld|%ld|%s|%s\n", CIT_OK, filesize, (long)modtime, filename, mime_type);
154 * open a file for downloading
156 void cmd_open(char *cmdbuf)
159 char pathname[PATH_MAX];
162 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
164 if (CtdlAccessCheck(ac_logged_in)) return;
166 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
167 cprintf("%d No directory in this room.\n", ERROR + NOT_HERE);
171 if (IsEmptyStr(filename)) {
172 cprintf("%d You must specify a file name.\n", ERROR + FILE_NOT_FOUND);
175 if (strstr(filename, "../") != NULL)
177 cprintf("%d syntax error.\n", ERROR + ILLEGAL_VALUE);
181 if (CC->download_fp != NULL) {
182 cprintf("%d You already have a download file open.\n", ERROR + RESOURCE_BUSY);
186 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
187 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
192 snprintf(pathname, sizeof pathname, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filename);
193 CC->download_fp = fopen(pathname, "r");
195 if (CC->download_fp == NULL) {
196 cprintf("%d cannot open %s: %s\n", ERROR + INTERNAL_ERROR, pathname, strerror(errno));
200 OpenCmdResult(filename, "application/octet-stream");
207 void cmd_oimg(char *cmdbuf)
209 char filename[PATH_MAX];
210 char pathname[PATH_MAX];
211 char MimeTestBuf[32];
214 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
216 if (IsEmptyStr(filename)) {
217 cprintf("%d You must specify a file name.\n", ERROR + FILE_NOT_FOUND);
221 if (CC->download_fp != NULL) {
222 cprintf("%d You already have a download file open.\n", ERROR + RESOURCE_BUSY);
226 CC->download_fp = fopen(pathname, "rb");
227 if (CC->download_fp == NULL) {
228 strcat(pathname, ".gif");
229 CC->download_fp = fopen(pathname, "rb");
231 if (CC->download_fp == NULL) {
232 cprintf("%d Cannot open %s: %s\n", ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
235 rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
237 cprintf("%d Cannot access %s: %s\n", ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
241 rewind (CC->download_fp);
242 OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
247 * open a file for uploading
249 void cmd_uopn(char *cmdbuf)
253 extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
254 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
255 extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
257 if (CtdlAccessCheck(ac_logged_in)) return;
259 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
260 cprintf("%d No directory in this room.\n", ERROR + NOT_HERE);
264 if (IsEmptyStr(CC->upl_file)) {
265 cprintf("%d You must specify a file name.\n", ERROR + FILE_NOT_FOUND);
269 if (CC->upload_fp != NULL) {
270 cprintf("%d You already have a upload file open.\n", ERROR + RESOURCE_BUSY);
274 for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
275 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
276 CC->upl_file[a] = '_';
279 snprintf(CC->upl_path, sizeof CC->upl_path, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, CC->upl_file);
280 snprintf(CC->upl_filedir, sizeof CC->upl_filedir, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
282 CC->upload_fp = fopen(CC->upl_path, "r");
283 if (CC->upload_fp != NULL) {
284 fclose(CC->upload_fp);
285 CC->upload_fp = NULL;
286 cprintf("%d '%s' already exists\n", ERROR + ALREADY_EXISTS, CC->upl_path);
290 CC->upload_fp = fopen(CC->upl_path, "wb");
291 if (CC->upload_fp == NULL) {
292 cprintf("%d Cannot open %s: %s\n", ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
295 cprintf("%d Ok\n", CIT_OK);
300 * open an image file for uploading
302 void cmd_uimg(char *cmdbuf)
304 int is_this_for_real;
308 if (num_parms(cmdbuf) < 2) {
309 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
313 is_this_for_real = extract_int(cmdbuf, 0);
314 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
315 extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
316 if (CC->upload_fp != NULL) {
317 cprintf("%d You already have an upload file open.\n", ERROR + RESOURCE_BUSY);
321 strcpy(CC->upl_path, "");
323 for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
324 basenm[a] = tolower(basenm[a]);
325 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
330 if (CC->user.axlevel >= AxAideU) {
331 snprintf(CC->upl_path, sizeof CC->upl_path, "%s/%s", ctdl_image_dir, basenm);
334 if (IsEmptyStr(CC->upl_path)) {
335 cprintf("%d Higher access required.\n", ERROR + HIGHER_ACCESS_REQUIRED);
339 if (is_this_for_real == 0) {
340 cprintf("%d Ok to send image\n", CIT_OK);
344 CC->upload_fp = fopen(CC->upl_path, "wb");
345 if (CC->upload_fp == NULL) {
346 cprintf("%d Cannot open %s: %s\n", ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
349 cprintf("%d Ok\n", CIT_OK);
354 * close the download file
356 void cmd_clos(char *cmdbuf)
358 if (CC->download_fp == NULL) {
359 cprintf("%d You don't have a download file open.\n", ERROR + RESOURCE_NOT_OPEN);
363 fclose(CC->download_fp);
364 CC->download_fp = NULL;
365 cprintf("%d Ok\n", CIT_OK);
372 void abort_upl(CitContext *who)
374 if (who->upload_fp != NULL) {
375 fclose(who->upload_fp);
376 who->upload_fp = NULL;
377 unlink(CC->upl_path);
383 * close the upload file
385 void cmd_ucls(char *cmd)
388 char upload_notice[SIZ];
390 if (CC->upload_fp == NULL) {
391 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
395 fclose(CC->upload_fp);
396 CC->upload_fp = NULL;
398 if (!strcasecmp(cmd, "1")) {
399 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
400 fp = fopen(CC->upl_filedir, "a");
402 fp = fopen(CC->upl_filedir, "w");
405 fprintf(fp, "%s %s %s\n", CC->upl_file, CC->upl_mimetype, CC->upl_comment);
409 if ((CC->room.QRflags2 & QR2_NOUPLMSG) == 0) {
410 /* put together an upload notice */
411 snprintf(upload_notice, sizeof upload_notice,
412 "NEW UPLOAD: '%s'\n %s\n%s\n",
417 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname, upload_notice, 0, NULL);
421 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
427 * read from the download file
429 void cmd_read(char *cmdbuf)
436 /* The client will transmit its requested offset and byte count */
437 start_pos = extract_long(cmdbuf, 0);
438 bytes = extract_int(cmdbuf, 1);
439 if ((start_pos < 0) || (bytes <= 0)) {
440 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
444 if (CC->download_fp == NULL) {
445 cprintf("%d You don't have a download file open.\n", ERROR + RESOURCE_NOT_OPEN);
449 /* If necessary, reduce the byte count to the size of our buffer */
450 if (bytes > sizeof(buf)) {
454 rc = fseek(CC->download_fp, start_pos, 0);
456 cprintf("%d your file is smaller than %ld.\n", ERROR + ILLEGAL_VALUE, start_pos);
457 syslog(LOG_ERR, "serv_file: your file %s is smaller than %ld [%s]",
465 bytes = fread(buf, 1, bytes, CC->download_fp);
467 /* Tell the client the actual byte count and transmit it */
468 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
469 client_write(buf, bytes);
472 cprintf("%d %s\n", ERROR, strerror(errno));
478 * write to the upload file
480 void cmd_writ(char *cmdbuf)
482 struct CitContext *CCC = CC;
489 bytes = extract_int(cmdbuf, 0);
491 if (CCC->upload_fp == NULL) {
492 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
496 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
500 if (bytes > 100000) {
504 cprintf("%d %d\n", SEND_BINARY, bytes);
505 buf = malloc(bytes + 1);
506 client_read(buf, bytes);
507 rv = fwrite(buf, bytes, 1, CCC->upload_fp);
509 syslog(LOG_ERR, "serv_file: %s", strerror(errno));
515 void files_logout_hook(void)
517 CitContext *CCC = MyContext();
520 * If there is a download in progress, abort it.
522 if (CCC->download_fp != NULL) {
523 fclose(CCC->download_fp);
524 CCC->download_fp = NULL;
528 * If there is an upload in progress, abort it.
530 if (CCC->upload_fp != NULL) {
538 * help_subst() - support routine for help file viewer
540 void help_subst(char *strbuf, char *source, char *dest)
545 while (p = pattern2(strbuf, source), (p >= 0)) {
546 strcpy(workbuf, &strbuf[p + strlen(source)]);
547 strcpy(&strbuf[p], dest);
548 strcat(strbuf, workbuf);
553 void do_help_subst(char *buffer)
557 help_subst(buffer, "^nodename", CtdlGetConfigStr("c_nodename"));
558 help_subst(buffer, "^humannode", CtdlGetConfigStr("c_humannode"));
559 help_subst(buffer, "^fqdn", CtdlGetConfigStr("c_fqdn"));
560 help_subst(buffer, "^username", CC->user.fullname);
561 snprintf(buf2, sizeof buf2, "%ld", CC->user.usernum);
562 help_subst(buffer, "^usernum", buf2);
563 help_subst(buffer, "^sysadm", CtdlGetConfigStr("c_sysadm"));
564 help_subst(buffer, "^variantname", CITADEL);
565 help_subst(buffer, "^maxsessions", CtdlGetConfigStr("c_maxsessions")); // yes it's numeric but str is ok here
566 help_subst(buffer, "^bbsdir", ctdl_message_dir);
570 typedef const char *ccharp;
572 * display system messages or help
574 void cmd_mesg(char *mname)
583 extract_token(buf, mname, 0, '|', sizeof buf);
585 snprintf(buf2, sizeof buf2, "%s.%d.%d", buf, CC->cs_clientdev, CC->cs_clienttyp);
587 /* If the client requested "?" then produce a listing */
588 if (!strcmp(buf, "?")) {
589 cprintf("%d %s\n", LISTING_FOLLOWS, buf);
590 dp = opendir(ctdl_message_dir);
592 while (d = readdir(dp), d != NULL) {
593 if (d->d_name[0] != '.') {
594 cprintf(" %s\n", d->d_name);
603 /* Otherwise, look for the requested file by name. */
604 snprintf(targ, sizeof targ, "%s/%s", ctdl_message_dir, buf);
605 mfp = fopen(targ, "r");
607 cprintf("%d Cannot open '%s': %s\n",
608 ERROR + FILE_NOT_FOUND, targ, strerror(errno));
611 cprintf("%d %s\n", LISTING_FOLLOWS, buf);
613 while (fgets(buf, (sizeof buf - 1), mfp) != NULL) {
614 buf[strlen(buf)-1] = 0;
625 * enter system messages or help
627 void cmd_emsg(char *mname)
636 if (CtdlAccessCheck(ac_aide)) return;
638 extract_token(buf, mname, 0, '|', sizeof buf);
639 for (a=0; !IsEmptyStr(&buf[a]); ++a) { /* security measure */
640 if (buf[a] == '/') buf[a] = '.';
643 if (IsEmptyStr(targ)) {
644 snprintf(targ, sizeof targ, "%s/%s", ctdl_message_dir, buf);
647 mfp = fopen(targ, "w");
649 cprintf("%d Cannot open '%s': %s\n",
650 ERROR + INTERNAL_ERROR, targ, strerror(errno));
653 cprintf("%d %s\n", SEND_LISTING, targ);
655 while (client_getln(buf, sizeof buf) >=0 && strcmp(buf, "000")) {
656 fprintf(mfp, "%s\n", buf);
663 // Initialization function, called from modules_init.c
664 char *ctdl_module_init_file_ops(void) {
666 CtdlRegisterSessionHook(files_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 8);
667 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
668 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
669 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
670 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
671 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
672 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
673 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
674 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
675 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
676 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
677 CtdlRegisterProtoHook(cmd_mesg, "MESG", "fetch system banners");
678 CtdlRegisterProtoHook(cmd_emsg, "EMSG", "submit system banners");
680 // return a module name for the log