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"
40 #include "ctdl_module.h"
46 * Server command to delete a file from a room's directory
48 void cmd_delf(char *filename)
53 if (CtdlAccessCheck(ac_room_aide))
56 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
57 cprintf("%d No directory in this room.\n",
62 if (IsEmptyStr(filename)) {
63 cprintf("%d You must specify a file name.\n",
64 ERROR + FILE_NOT_FOUND);
67 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
68 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
72 snprintf(pathname, sizeof pathname,
75 CC->room.QRdirname, filename);
78 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
81 cprintf("%d File '%s' not found.\n",
82 ERROR + FILE_NOT_FOUND, pathname);
90 * move a file from one room directory to another
92 void cmd_movf(char *cmdbuf)
94 char filename[PATH_MAX];
95 char pathname[PATH_MAX];
96 char newpath[PATH_MAX];
97 char newroom[ROOMNAMELEN];
100 struct ctdlroom qrbuf;
102 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
103 extract_token(newroom, cmdbuf, 1, '|', sizeof newroom);
105 if (CtdlAccessCheck(ac_room_aide)) return;
107 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
108 cprintf("%d No directory in this room.\n",
113 if (IsEmptyStr(filename)) {
114 cprintf("%d You must specify a file name.\n",
115 ERROR + FILE_NOT_FOUND);
119 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
120 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
124 snprintf(pathname, sizeof pathname, "./files/%s/%s",
125 CC->room.QRdirname, filename);
126 if (access(pathname, 0) != 0) {
127 cprintf("%d File '%s' not found.\n",
128 ERROR + FILE_NOT_FOUND, pathname);
132 if (CtdlGetRoom(&qrbuf, newroom) != 0) {
133 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
136 if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
137 cprintf("%d '%s' is not a directory room.\n",
138 ERROR + NOT_HERE, qrbuf.QRname);
141 snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
143 if (link(pathname, newpath) != 0) {
144 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR,
150 /* this is a crude method of copying the file description */
151 snprintf(buf, sizeof buf,
152 "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir",
153 CC->room.QRdirname, filename, qrbuf.QRdirname);
155 cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
160 * This code is common to all commands which open a file for downloading,
161 * regardless of whether it's a file from the directory, an image, a network
162 * spool file, a MIME attachment, etc.
163 * It examines the file and displays the OK result code and some information
164 * about the file. NOTE: this stuff is Unix dependent.
166 void OpenCmdResult(char *filename, const char *mime_type)
172 fstat(fileno(CC->download_fp), &statbuf);
173 CC->download_fp_total = statbuf.st_size;
174 filesize = (long) statbuf.st_size;
175 modtime = (time_t) statbuf.st_mtime;
177 cprintf("%d %ld|%ld|%s|%s\n",
178 CIT_OK, filesize, (long)modtime, filename, mime_type);
183 * open a file for downloading
185 void cmd_open(char *cmdbuf)
188 char pathname[PATH_MAX];
191 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
193 if (CtdlAccessCheck(ac_logged_in)) return;
195 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
196 cprintf("%d No directory in this room.\n",
201 if (IsEmptyStr(filename)) {
202 cprintf("%d You must specify a file name.\n",
203 ERROR + FILE_NOT_FOUND);
206 if (strstr(filename, "../") != NULL)
208 cprintf("%d syntax error.\n",
209 ERROR + ILLEGAL_VALUE);
213 if (CC->download_fp != NULL) {
214 cprintf("%d You already have a download file open.\n",
215 ERROR + RESOURCE_BUSY);
219 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
220 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
225 snprintf(pathname, sizeof pathname,
228 CC->room.QRdirname, filename);
229 CC->download_fp = fopen(pathname, "r");
231 if (CC->download_fp == NULL) {
232 cprintf("%d cannot open %s: %s\n",
233 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
237 OpenCmdResult(filename, "application/octet-stream");
243 void cmd_oimg(char *cmdbuf)
246 char pathname[PATH_MAX];
247 char MimeTestBuf[32];
248 struct ctdluser usbuf;
249 char which_user[USERNAME_SIZE];
254 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
256 if (IsEmptyStr(filename)) {
257 cprintf("%d You must specify a file name.\n",
258 ERROR + FILE_NOT_FOUND);
262 if (CC->download_fp != NULL) {
263 cprintf("%d You already have a download file open.\n",
264 ERROR + RESOURCE_BUSY);
268 if (!strcasecmp(filename, "_userpic_")) {
269 extract_token(which_user, cmdbuf, 1, '|', sizeof which_user);
270 if (CtdlGetUser(&usbuf, which_user) != 0) {
271 cprintf("%d No such user.\n",
272 ERROR + NO_SUCH_USER);
275 snprintf(pathname, sizeof pathname,
279 } else if (!strcasecmp(filename, "_floorpic_")) {
280 which_floor = extract_int(cmdbuf, 1);
281 snprintf(pathname, sizeof pathname,
283 ctdl_image_dir, which_floor);
284 } else if (!strcasecmp(filename, "_roompic_")) {
285 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
287 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
288 filename[a] = tolower(filename[a]);
289 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
293 if (strstr(filename, "../") != NULL)
295 cprintf("%d syntax error.\n",
296 ERROR + ILLEGAL_VALUE);
300 snprintf(pathname, sizeof pathname,
306 CC->download_fp = fopen(pathname, "rb");
307 if (CC->download_fp == NULL) {
308 strcat(pathname, ".gif");
309 CC->download_fp = fopen(pathname, "rb");
311 if (CC->download_fp == NULL) {
312 cprintf("%d Cannot open %s: %s\n",
313 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
316 rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
318 cprintf("%d Cannot access %s: %s\n",
319 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
323 rewind (CC->download_fp);
324 OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
328 * open a file for uploading
330 void cmd_uopn(char *cmdbuf)
334 extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
335 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
336 extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
338 if (CtdlAccessCheck(ac_logged_in)) return;
340 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
341 cprintf("%d No directory in this room.\n",
346 if (IsEmptyStr(CC->upl_file)) {
347 cprintf("%d You must specify a file name.\n",
348 ERROR + FILE_NOT_FOUND);
352 if (CC->upload_fp != NULL) {
353 cprintf("%d You already have a upload file open.\n",
354 ERROR + RESOURCE_BUSY);
358 for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
359 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
360 CC->upl_file[a] = '_';
363 snprintf(CC->upl_path, sizeof CC->upl_path,
366 CC->room.QRdirname, CC->upl_file);
367 snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
372 CC->upload_fp = fopen(CC->upl_path, "r");
373 if (CC->upload_fp != NULL) {
374 fclose(CC->upload_fp);
375 CC->upload_fp = NULL;
376 cprintf("%d '%s' already exists\n",
377 ERROR + ALREADY_EXISTS, CC->upl_path);
381 CC->upload_fp = fopen(CC->upl_path, "wb");
382 if (CC->upload_fp == NULL) {
383 cprintf("%d Cannot open %s: %s\n",
384 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
387 cprintf("%d Ok\n", CIT_OK);
393 * open an image file for uploading
395 void cmd_uimg(char *cmdbuf)
397 int is_this_for_real;
402 if (num_parms(cmdbuf) < 2) {
403 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
407 is_this_for_real = extract_int(cmdbuf, 0);
408 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
409 extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
410 if (CC->upload_fp != NULL) {
411 cprintf("%d You already have an upload file open.\n",
412 ERROR + RESOURCE_BUSY);
416 strcpy(CC->upl_path, "");
418 for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
419 basenm[a] = tolower(basenm[a]);
420 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
425 if (CC->user.axlevel >= AxAideU) {
426 snprintf(CC->upl_path, sizeof CC->upl_path,
432 if (!strcasecmp(basenm, "_userpic_")) {
433 snprintf(CC->upl_path, sizeof CC->upl_path,
439 if ((!strcasecmp(basenm, "_floorpic_"))
440 && (CC->user.axlevel >= AxAideU)) {
441 which_floor = extract_int(cmdbuf, 2);
442 snprintf(CC->upl_path, sizeof CC->upl_path,
448 if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
449 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
452 if (IsEmptyStr(CC->upl_path)) {
453 cprintf("%d Higher access required.\n",
454 ERROR + HIGHER_ACCESS_REQUIRED);
458 if (is_this_for_real == 0) {
459 cprintf("%d Ok to send image\n", CIT_OK);
463 CC->upload_fp = fopen(CC->upl_path, "wb");
464 if (CC->upload_fp == NULL) {
465 cprintf("%d Cannot open %s: %s\n",
466 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
469 cprintf("%d Ok\n", CIT_OK);
470 CC->upload_type = UPL_IMAGE;
475 * close the download file
477 void cmd_clos(char *cmdbuf)
481 if (CC->download_fp == NULL) {
482 cprintf("%d You don't have a download file open.\n",
483 ERROR + RESOURCE_NOT_OPEN);
487 fclose(CC->download_fp);
488 CC->download_fp = NULL;
490 if (CC->dl_is_net == 1) {
492 snprintf(buf, sizeof buf,
499 cprintf("%d Ok\n", CIT_OK);
506 void abort_upl(CitContext *who)
508 if (who->upload_fp != NULL) {
509 fclose(who->upload_fp);
510 who->upload_fp = NULL;
511 unlink(CC->upl_path);
518 * close the upload file
520 void cmd_ucls(char *cmd)
523 char upload_notice[512];
526 if (CC->upload_fp == NULL) {
527 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
531 fclose(CC->upload_fp);
532 CC->upload_fp = NULL;
534 if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
535 cprintf("%d Upload completed.\n", CIT_OK);
537 if (CC->upload_type == UPL_NET) {
538 char final_filename[PATH_MAX];
539 snprintf(final_filename, sizeof final_filename,
547 if (link(CC->upl_path, final_filename) == 0) {
548 syslog(LOG_INFO, "UCLS: updoaded %s\n",
550 unlink(CC->upl_path);
553 syslog(LOG_ALERT, "Cannot link %s to %s: %s\n",
554 CC->upl_path, final_filename, strerror(errno)
559 /* FIXME ... here we need to trigger a network run */
562 CC->upload_type = UPL_FILE;
566 if (!strcasecmp(cmd, "1")) {
567 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
568 fp = fopen(CC->upl_filedir, "a");
570 fp = fopen(CC->upl_filedir, "w");
573 fprintf(fp, "%s %s %s\n", CC->upl_file,
579 /* put together an upload notice */
580 snprintf(upload_notice, sizeof upload_notice,
581 "NEW UPLOAD: '%s'\n %s\n%s\n",
585 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
586 upload_notice, 0, NULL);
589 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
595 * read from the download file
597 void cmd_read(char *cmdbuf)
604 /* The client will transmit its requested offset and byte count */
605 start_pos = extract_long(cmdbuf, 0);
606 bytes = extract_int(cmdbuf, 1);
607 if ((start_pos < 0) || (bytes <= 0)) {
608 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
612 if (CC->download_fp == NULL) {
613 cprintf("%d You don't have a download file open.\n",
614 ERROR + RESOURCE_NOT_OPEN);
618 /* If necessary, reduce the byte count to the size of our buffer */
619 if (bytes > sizeof(buf)) {
623 rc = fseek(CC->download_fp, start_pos, 0);
625 cprintf("%d your file is smaller then %ld.\n", ERROR + ILLEGAL_VALUE, start_pos);
626 syslog(LOG_ALERT, "your file %s is smaller then %ld. [%s]\n",
633 bytes = fread(buf, 1, bytes, CC->download_fp);
635 /* Tell the client the actual byte count and transmit it */
636 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
637 client_write(buf, bytes);
640 cprintf("%d %s\n", ERROR, strerror(errno));
646 * write to the upload file
648 void cmd_writ(char *cmdbuf)
656 bytes = extract_int(cmdbuf, 0);
658 if (CC->upload_fp == NULL) {
659 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
663 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
667 if (bytes > 100000) {
671 cprintf("%d %d\n", SEND_BINARY, bytes);
672 buf = malloc(bytes + 1);
673 client_read(buf, bytes);
674 rv = fwrite(buf, bytes, 1, CC->upload_fp);
676 syslog(LOG_EMERG, "Couldn't write: %s\n",
686 * cmd_ndop() - open a network spool file for downloading
688 void cmd_ndop(char *cmdbuf)
693 if (IsEmptyStr(CC->net_node)) {
694 cprintf("%d Not authenticated as a network node.\n",
695 ERROR + NOT_LOGGED_IN);
699 if (CC->download_fp != NULL) {
700 cprintf("%d You already have a download file open.\n",
701 ERROR + RESOURCE_BUSY);
705 snprintf(pathname, sizeof pathname,
710 /* first open the file in append mode in order to create a
711 * zero-length file if it doesn't already exist
713 CC->download_fp = fopen(pathname, "a");
714 if (CC->download_fp != NULL)
715 fclose(CC->download_fp);
718 CC->download_fp = fopen(pathname, "r");
719 if (CC->download_fp == NULL) {
720 cprintf("%d cannot open %s: %s\n",
721 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
726 /* set this flag so other routines know that the download file
727 * currently open is a network spool file
731 stat(pathname, &statbuf);
732 CC->download_fp_total = statbuf.st_size;
733 cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
737 * cmd_nuop() - open a network spool file for uploading
739 void cmd_nuop(char *cmdbuf)
743 if (IsEmptyStr(CC->net_node)) {
744 cprintf("%d Not authenticated as a network node.\n",
745 ERROR + NOT_LOGGED_IN);
749 if (CC->upload_fp != NULL) {
750 cprintf("%d You already have an upload file open.\n",
751 ERROR + RESOURCE_BUSY);
755 snprintf(CC->upl_path, sizeof CC->upl_path,
762 CC->upload_fp = fopen(CC->upl_path, "r");
763 if (CC->upload_fp != NULL) {
764 fclose(CC->upload_fp);
765 CC->upload_fp = NULL;
766 cprintf("%d '%s' already exists\n",
767 ERROR + ALREADY_EXISTS, CC->upl_path);
771 CC->upload_fp = fopen(CC->upl_path, "w");
772 if (CC->upload_fp == NULL) {
773 cprintf("%d Cannot open %s: %s\n",
774 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
778 CC->upload_type = UPL_NET;
779 cprintf("%d Ok\n", CIT_OK);
783 /*****************************************************************************/
784 /* MODULE INITIALIZATION STUFF */
785 /*****************************************************************************/
787 CTDL_MODULE_INIT(file_ops)
791 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
792 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
793 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
794 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
795 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
796 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
797 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
798 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
799 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
800 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
801 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
802 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
804 /* return our Subversion id for the Log */