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);
207 if (CC->download_fp != NULL) {
208 cprintf("%d You already have a download file open.\n",
209 ERROR + RESOURCE_BUSY);
213 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
214 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
219 snprintf(pathname, sizeof pathname,
222 CC->room.QRdirname, filename);
223 CC->download_fp = fopen(pathname, "r");
225 if (CC->download_fp == NULL) {
226 cprintf("%d cannot open %s: %s\n",
227 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
231 OpenCmdResult(filename, "application/octet-stream");
237 void cmd_oimg(char *cmdbuf)
240 char pathname[PATH_MAX];
241 char MimeTestBuf[32];
242 struct ctdluser usbuf;
243 char which_user[USERNAME_SIZE];
248 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
250 if (IsEmptyStr(filename)) {
251 cprintf("%d You must specify a file name.\n",
252 ERROR + FILE_NOT_FOUND);
256 if (CC->download_fp != NULL) {
257 cprintf("%d You already have a download file open.\n",
258 ERROR + RESOURCE_BUSY);
262 if (!strcasecmp(filename, "_userpic_")) {
263 extract_token(which_user, cmdbuf, 1, '|', sizeof which_user);
264 if (CtdlGetUser(&usbuf, which_user) != 0) {
265 cprintf("%d No such user.\n",
266 ERROR + NO_SUCH_USER);
269 snprintf(pathname, sizeof pathname,
273 } else if (!strcasecmp(filename, "_floorpic_")) {
274 which_floor = extract_int(cmdbuf, 1);
275 snprintf(pathname, sizeof pathname,
277 ctdl_image_dir, which_floor);
278 } else if (!strcasecmp(filename, "_roompic_")) {
279 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
281 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
282 filename[a] = tolower(filename[a]);
283 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
287 snprintf(pathname, sizeof pathname,
293 CC->download_fp = fopen(pathname, "rb");
294 if (CC->download_fp == NULL) {
295 strcat(pathname, ".gif");
296 CC->download_fp = fopen(pathname, "rb");
298 if (CC->download_fp == NULL) {
299 cprintf("%d Cannot open %s: %s\n",
300 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
303 rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
305 cprintf("%d Cannot access %s: %s\n",
306 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
310 rewind (CC->download_fp);
311 OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
315 * open a file for uploading
317 void cmd_uopn(char *cmdbuf)
321 extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
322 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
323 extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
325 if (CtdlAccessCheck(ac_logged_in)) return;
327 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
328 cprintf("%d No directory in this room.\n",
333 if (IsEmptyStr(CC->upl_file)) {
334 cprintf("%d You must specify a file name.\n",
335 ERROR + FILE_NOT_FOUND);
339 if (CC->upload_fp != NULL) {
340 cprintf("%d You already have a upload file open.\n",
341 ERROR + RESOURCE_BUSY);
345 for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
346 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
347 CC->upl_file[a] = '_';
350 snprintf(CC->upl_path, sizeof CC->upl_path,
353 CC->room.QRdirname, CC->upl_file);
354 snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
359 CC->upload_fp = fopen(CC->upl_path, "r");
360 if (CC->upload_fp != NULL) {
361 fclose(CC->upload_fp);
362 CC->upload_fp = NULL;
363 cprintf("%d '%s' already exists\n",
364 ERROR + ALREADY_EXISTS, CC->upl_path);
368 CC->upload_fp = fopen(CC->upl_path, "wb");
369 if (CC->upload_fp == NULL) {
370 cprintf("%d Cannot open %s: %s\n",
371 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
374 cprintf("%d Ok\n", CIT_OK);
380 * open an image file for uploading
382 void cmd_uimg(char *cmdbuf)
384 int is_this_for_real;
389 if (num_parms(cmdbuf) < 2) {
390 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
394 is_this_for_real = extract_int(cmdbuf, 0);
395 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
396 extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
397 if (CC->upload_fp != NULL) {
398 cprintf("%d You already have an upload file open.\n",
399 ERROR + RESOURCE_BUSY);
403 strcpy(CC->upl_path, "");
405 for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
406 basenm[a] = tolower(basenm[a]);
407 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
412 if (CC->user.axlevel >= AxAideU) {
413 snprintf(CC->upl_path, sizeof CC->upl_path,
419 if (!strcasecmp(basenm, "_userpic_")) {
420 snprintf(CC->upl_path, sizeof CC->upl_path,
426 if ((!strcasecmp(basenm, "_floorpic_"))
427 && (CC->user.axlevel >= AxAideU)) {
428 which_floor = extract_int(cmdbuf, 2);
429 snprintf(CC->upl_path, sizeof CC->upl_path,
435 if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
436 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
439 if (IsEmptyStr(CC->upl_path)) {
440 cprintf("%d Higher access required.\n",
441 ERROR + HIGHER_ACCESS_REQUIRED);
445 if (is_this_for_real == 0) {
446 cprintf("%d Ok to send image\n", CIT_OK);
450 CC->upload_fp = fopen(CC->upl_path, "wb");
451 if (CC->upload_fp == NULL) {
452 cprintf("%d Cannot open %s: %s\n",
453 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
456 cprintf("%d Ok\n", CIT_OK);
457 CC->upload_type = UPL_IMAGE;
462 * close the download file
464 void cmd_clos(char *cmdbuf)
468 if (CC->download_fp == NULL) {
469 cprintf("%d You don't have a download file open.\n",
470 ERROR + RESOURCE_NOT_OPEN);
474 fclose(CC->download_fp);
475 CC->download_fp = NULL;
477 if (CC->dl_is_net == 1) {
479 snprintf(buf, sizeof buf,
486 cprintf("%d Ok\n", CIT_OK);
493 void abort_upl(CitContext *who)
495 if (who->upload_fp != NULL) {
496 fclose(who->upload_fp);
497 who->upload_fp = NULL;
498 unlink(CC->upl_path);
505 * close the upload file
507 void cmd_ucls(char *cmd)
510 char upload_notice[512];
513 if (CC->upload_fp == NULL) {
514 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
518 fclose(CC->upload_fp);
519 CC->upload_fp = NULL;
521 if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
522 cprintf("%d Upload completed.\n", CIT_OK);
524 if (CC->upload_type == UPL_NET) {
525 char final_filename[PATH_MAX];
526 snprintf(final_filename, sizeof final_filename,
534 if (link(CC->upl_path, final_filename) == 0) {
535 unlink(CC->upl_path);
538 syslog(LOG_ALERT, "Cannot link %s to %s: %s\n",
539 CC->upl_path, final_filename, strerror(errno)
543 /* FIXME ... here we need to trigger a network run */
546 CC->upload_type = UPL_FILE;
550 if (!strcasecmp(cmd, "1")) {
551 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
552 fp = fopen(CC->upl_filedir, "a");
554 fp = fopen(CC->upl_filedir, "w");
557 fprintf(fp, "%s %s %s\n", CC->upl_file,
563 /* put together an upload notice */
564 snprintf(upload_notice, sizeof upload_notice,
565 "NEW UPLOAD: '%s'\n %s\n%s\n",
569 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
570 upload_notice, 0, NULL);
573 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
579 * read from the download file
581 void cmd_read(char *cmdbuf)
588 /* The client will transmit its requested offset and byte count */
589 start_pos = extract_long(cmdbuf, 0);
590 bytes = extract_int(cmdbuf, 1);
591 if ((start_pos < 0) || (bytes <= 0)) {
592 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
596 if (CC->download_fp == NULL) {
597 cprintf("%d You don't have a download file open.\n",
598 ERROR + RESOURCE_NOT_OPEN);
602 /* If necessary, reduce the byte count to the size of our buffer */
603 if (bytes > sizeof(buf)) {
607 rc = fseek(CC->download_fp, start_pos, 0);
609 cprintf("%d your file is smaller then %ld.\n", ERROR + ILLEGAL_VALUE, start_pos);
610 syslog(LOG_ALERT, "your file %s is smaller then %ld. [%s]\n",
617 bytes = fread(buf, 1, bytes, CC->download_fp);
619 /* Tell the client the actual byte count and transmit it */
620 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
621 client_write(buf, bytes);
624 cprintf("%d %s\n", ERROR, strerror(errno));
630 * write to the upload file
632 void cmd_writ(char *cmdbuf)
640 bytes = extract_int(cmdbuf, 0);
642 if (CC->upload_fp == NULL) {
643 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
647 cprintf("%d you have to specify a value > 0.\n", ERROR + ILLEGAL_VALUE);
651 if (bytes > 100000) {
655 cprintf("%d %d\n", SEND_BINARY, bytes);
656 buf = malloc(bytes + 1);
657 client_read(buf, bytes);
658 rv = fwrite(buf, bytes, 1, CC->upload_fp);
660 syslog(LOG_EMERG, "Couldn't write: %s\n",
670 * cmd_ndop() - open a network spool file for downloading
672 void cmd_ndop(char *cmdbuf)
677 if (IsEmptyStr(CC->net_node)) {
678 cprintf("%d Not authenticated as a network node.\n",
679 ERROR + NOT_LOGGED_IN);
683 if (CC->download_fp != NULL) {
684 cprintf("%d You already have a download file open.\n",
685 ERROR + RESOURCE_BUSY);
689 snprintf(pathname, sizeof pathname,
694 /* first open the file in append mode in order to create a
695 * zero-length file if it doesn't already exist
697 CC->download_fp = fopen(pathname, "a");
698 if (CC->download_fp != NULL)
699 fclose(CC->download_fp);
702 CC->download_fp = fopen(pathname, "r");
703 if (CC->download_fp == NULL) {
704 cprintf("%d cannot open %s: %s\n",
705 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
710 /* set this flag so other routines know that the download file
711 * currently open is a network spool file
715 stat(pathname, &statbuf);
716 CC->download_fp_total = statbuf.st_size;
717 cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
721 * cmd_nuop() - open a network spool file for uploading
723 void cmd_nuop(char *cmdbuf)
727 if (IsEmptyStr(CC->net_node)) {
728 cprintf("%d Not authenticated as a network node.\n",
729 ERROR + NOT_LOGGED_IN);
733 if (CC->upload_fp != NULL) {
734 cprintf("%d You already have an upload file open.\n",
735 ERROR + RESOURCE_BUSY);
739 snprintf(CC->upl_path, sizeof CC->upl_path,
746 CC->upload_fp = fopen(CC->upl_path, "r");
747 if (CC->upload_fp != NULL) {
748 fclose(CC->upload_fp);
749 CC->upload_fp = NULL;
750 cprintf("%d '%s' already exists\n",
751 ERROR + ALREADY_EXISTS, CC->upl_path);
755 CC->upload_fp = fopen(CC->upl_path, "w");
756 if (CC->upload_fp == NULL) {
757 cprintf("%d Cannot open %s: %s\n",
758 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
762 CC->upload_type = UPL_NET;
763 cprintf("%d Ok\n", CIT_OK);
767 /*****************************************************************************/
768 /* MODULE INITIALIZATION STUFF */
769 /*****************************************************************************/
771 CTDL_MODULE_INIT(file_ops)
775 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
776 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
777 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
778 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
779 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
780 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
781 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
782 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
783 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
784 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
785 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
786 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
788 /* return our Subversion id for the Log */