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"
45 #include "ctdl_module.h"
49 * network_talking_to() -- concurrency checker
51 static HashList *nttlist = NULL;
52 int network_talking_to(const char *nodename, long len, int operation) {
58 begin_critical_section(S_NTTLIST);
64 nttlist = NewHash(1, NULL);
65 Put(nttlist, nodename, len, NewStrBufPlain(nodename, len), HFreeStrBuf);
66 syslog(LOG_DEBUG, "nttlist: added <%s>\n", nodename);
69 if ((nttlist == NULL) ||
70 (GetCount(nttlist) == 0))
72 Pos = GetNewHashPos(nttlist, 1);
73 GetHashPosFromKey (nttlist, nodename, len, Pos);
75 DeleteEntryFromHash(nttlist, Pos);
77 syslog(LOG_DEBUG, "nttlist: removed <%s>\n", nodename);
82 if ((nttlist == NULL) ||
83 (GetCount(nttlist) == 0))
85 if (!GetHash(nttlist, nodename, len, &vdata))
87 syslog(LOG_DEBUG, "nttlist: have [%d] <%s>\n", retval, nodename);
91 end_critical_section(S_NTTLIST);
95 void cleanup_nttlist(void)
97 begin_critical_section(S_NTTLIST);
99 end_critical_section(S_NTTLIST);
105 * Server command to delete a file from a room's directory
107 void cmd_delf(char *filename)
112 if (CtdlAccessCheck(ac_room_aide))
115 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
116 cprintf("%d No directory in this room.\n",
121 if (IsEmptyStr(filename)) {
122 cprintf("%d You must specify a file name.\n",
123 ERROR + FILE_NOT_FOUND);
126 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
127 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
131 snprintf(pathname, sizeof pathname,
134 CC->room.QRdirname, filename);
135 a = unlink(pathname);
137 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
140 cprintf("%d File '%s' not found.\n",
141 ERROR + FILE_NOT_FOUND, pathname);
149 * move a file from one room directory to another
151 void cmd_movf(char *cmdbuf)
153 char filename[PATH_MAX];
154 char pathname[PATH_MAX];
155 char newpath[PATH_MAX];
156 char newroom[ROOMNAMELEN];
159 struct ctdlroom qrbuf;
162 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
163 extract_token(newroom, cmdbuf, 1, '|', sizeof newroom);
165 if (CtdlAccessCheck(ac_room_aide)) return;
167 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
168 cprintf("%d No directory in this room.\n",
173 if (IsEmptyStr(filename)) {
174 cprintf("%d You must specify a file name.\n",
175 ERROR + FILE_NOT_FOUND);
179 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
180 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
184 snprintf(pathname, sizeof pathname, "./files/%s/%s",
185 CC->room.QRdirname, filename);
186 if (access(pathname, 0) != 0) {
187 cprintf("%d File '%s' not found.\n",
188 ERROR + FILE_NOT_FOUND, pathname);
192 if (CtdlGetRoom(&qrbuf, newroom) != 0) {
193 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
196 if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
197 cprintf("%d '%s' is not a directory room.\n",
198 ERROR + NOT_HERE, qrbuf.QRname);
201 snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
203 if (link(pathname, newpath) != 0) {
204 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR,
210 /* this is a crude method of copying the file description */
211 snprintf(buf, sizeof buf,
212 "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir",
213 CC->room.QRdirname, filename, qrbuf.QRdirname);
215 cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
220 * This code is common to all commands which open a file for downloading,
221 * regardless of whether it's a file from the directory, an image, a network
222 * spool file, a MIME attachment, etc.
223 * It examines the file and displays the OK result code and some information
224 * about the file. NOTE: this stuff is Unix dependent.
226 void OpenCmdResult(char *filename, const char *mime_type)
232 fstat(fileno(CC->download_fp), &statbuf);
233 CC->download_fp_total = statbuf.st_size;
234 filesize = (long) statbuf.st_size;
235 modtime = (time_t) statbuf.st_mtime;
237 cprintf("%d %ld|%ld|%s|%s\n",
238 CIT_OK, filesize, (long)modtime, filename, mime_type);
243 * open a file for downloading
245 void cmd_open(char *cmdbuf)
248 char pathname[PATH_MAX];
251 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
253 if (CtdlAccessCheck(ac_logged_in)) return;
255 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
256 cprintf("%d No directory in this room.\n",
261 if (IsEmptyStr(filename)) {
262 cprintf("%d You must specify a file name.\n",
263 ERROR + FILE_NOT_FOUND);
267 if (CC->download_fp != NULL) {
268 cprintf("%d You already have a download file open.\n",
269 ERROR + RESOURCE_BUSY);
273 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
274 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
279 snprintf(pathname, sizeof pathname,
282 CC->room.QRdirname, filename);
283 CC->download_fp = fopen(pathname, "r");
285 if (CC->download_fp == NULL) {
286 cprintf("%d cannot open %s: %s\n",
287 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
291 OpenCmdResult(filename, "application/octet-stream");
297 void cmd_oimg(char *cmdbuf)
300 char pathname[PATH_MAX];
301 char MimeTestBuf[32];
302 struct ctdluser usbuf;
303 char which_user[USERNAME_SIZE];
308 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
310 if (IsEmptyStr(filename)) {
311 cprintf("%d You must specify a file name.\n",
312 ERROR + FILE_NOT_FOUND);
316 if (CC->download_fp != NULL) {
317 cprintf("%d You already have a download file open.\n",
318 ERROR + RESOURCE_BUSY);
322 if (!strcasecmp(filename, "_userpic_")) {
323 extract_token(which_user, cmdbuf, 1, '|', sizeof which_user);
324 if (CtdlGetUser(&usbuf, which_user) != 0) {
325 cprintf("%d No such user.\n",
326 ERROR + NO_SUCH_USER);
329 snprintf(pathname, sizeof pathname,
333 } else if (!strcasecmp(filename, "_floorpic_")) {
334 which_floor = extract_int(cmdbuf, 1);
335 snprintf(pathname, sizeof pathname,
337 ctdl_image_dir, which_floor);
338 } else if (!strcasecmp(filename, "_roompic_")) {
339 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
341 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
342 filename[a] = tolower(filename[a]);
343 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
347 snprintf(pathname, sizeof pathname,
353 CC->download_fp = fopen(pathname, "rb");
354 if (CC->download_fp == NULL) {
355 strcat(pathname, ".gif");
356 CC->download_fp = fopen(pathname, "rb");
358 if (CC->download_fp == NULL) {
359 cprintf("%d Cannot open %s: %s\n",
360 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
363 rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
365 cprintf("%d Cannot access %s: %s\n",
366 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
370 rewind (CC->download_fp);
371 OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
375 * open a file for uploading
377 void cmd_uopn(char *cmdbuf)
381 extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
382 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
383 extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
385 if (CtdlAccessCheck(ac_logged_in)) return;
387 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
388 cprintf("%d No directory in this room.\n",
393 if (IsEmptyStr(CC->upl_file)) {
394 cprintf("%d You must specify a file name.\n",
395 ERROR + FILE_NOT_FOUND);
399 if (CC->upload_fp != NULL) {
400 cprintf("%d You already have a upload file open.\n",
401 ERROR + RESOURCE_BUSY);
405 for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
406 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
407 CC->upl_file[a] = '_';
410 snprintf(CC->upl_path, sizeof CC->upl_path,
413 CC->room.QRdirname, CC->upl_file);
414 snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
419 CC->upload_fp = fopen(CC->upl_path, "r");
420 if (CC->upload_fp != NULL) {
421 fclose(CC->upload_fp);
422 CC->upload_fp = NULL;
423 cprintf("%d '%s' already exists\n",
424 ERROR + ALREADY_EXISTS, CC->upl_path);
428 CC->upload_fp = fopen(CC->upl_path, "wb");
429 if (CC->upload_fp == NULL) {
430 cprintf("%d Cannot open %s: %s\n",
431 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
434 cprintf("%d Ok\n", CIT_OK);
440 * open an image file for uploading
442 void cmd_uimg(char *cmdbuf)
444 int is_this_for_real;
449 if (num_parms(cmdbuf) < 2) {
450 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
454 is_this_for_real = extract_int(cmdbuf, 0);
455 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
456 extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
457 if (CC->upload_fp != NULL) {
458 cprintf("%d You already have an upload file open.\n",
459 ERROR + RESOURCE_BUSY);
463 strcpy(CC->upl_path, "");
465 for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
466 basenm[a] = tolower(basenm[a]);
467 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
472 if (CC->user.axlevel >= AxAideU) {
473 snprintf(CC->upl_path, sizeof CC->upl_path,
479 if (!strcasecmp(basenm, "_userpic_")) {
480 snprintf(CC->upl_path, sizeof CC->upl_path,
486 if ((!strcasecmp(basenm, "_floorpic_"))
487 && (CC->user.axlevel >= AxAideU)) {
488 which_floor = extract_int(cmdbuf, 2);
489 snprintf(CC->upl_path, sizeof CC->upl_path,
495 if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
496 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
499 if (IsEmptyStr(CC->upl_path)) {
500 cprintf("%d Higher access required.\n",
501 ERROR + HIGHER_ACCESS_REQUIRED);
505 if (is_this_for_real == 0) {
506 cprintf("%d Ok to send image\n", CIT_OK);
510 CC->upload_fp = fopen(CC->upl_path, "wb");
511 if (CC->upload_fp == NULL) {
512 cprintf("%d Cannot open %s: %s\n",
513 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
516 cprintf("%d Ok\n", CIT_OK);
517 CC->upload_type = UPL_IMAGE;
522 * close the download file
524 void cmd_clos(char *cmdbuf)
528 if (CC->download_fp == NULL) {
529 cprintf("%d You don't have a download file open.\n",
530 ERROR + RESOURCE_NOT_OPEN);
534 fclose(CC->download_fp);
535 CC->download_fp = NULL;
537 if (CC->dl_is_net == 1) {
539 snprintf(buf, sizeof buf,
546 cprintf("%d Ok\n", CIT_OK);
553 void abort_upl(CitContext *who)
555 if (who->upload_fp != NULL) {
556 fclose(who->upload_fp);
557 who->upload_fp = NULL;
558 unlink(CC->upl_path);
565 * close the upload file
567 void cmd_ucls(char *cmd)
570 char upload_notice[512];
573 if (CC->upload_fp == NULL) {
574 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
578 fclose(CC->upload_fp);
579 CC->upload_fp = NULL;
581 if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
582 cprintf("%d Upload completed.\n", CIT_OK);
584 if (CC->upload_type == UPL_NET) {
585 char final_filename[PATH_MAX];
586 snprintf(final_filename, sizeof final_filename,
594 if (link(CC->upl_path, final_filename) == 0) {
595 unlink(CC->upl_path);
598 syslog(LOG_ALERT, "Cannot link %s to %s: %s\n",
599 CC->upl_path, final_filename, strerror(errno)
603 /* FIXME ... here we need to trigger a network run */
606 CC->upload_type = UPL_FILE;
610 if (!strcasecmp(cmd, "1")) {
611 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
612 fp = fopen(CC->upl_filedir, "a");
614 fp = fopen(CC->upl_filedir, "w");
617 fprintf(fp, "%s %s %s\n", CC->upl_file,
623 /* put together an upload notice */
624 snprintf(upload_notice, sizeof upload_notice,
625 "NEW UPLOAD: '%s'\n %s\n%s\n",
629 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
630 upload_notice, 0, NULL);
633 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
639 * read from the download file
641 void cmd_read(char *cmdbuf)
647 /* The client will transmit its requested offset and byte count */
648 start_pos = extract_long(cmdbuf, 0);
649 bytes = extract_int(cmdbuf, 1);
651 if (CC->download_fp == NULL) {
652 cprintf("%d You don't have a download file open.\n",
653 ERROR + RESOURCE_NOT_OPEN);
657 /* If necessary, reduce the byte count to the size of our buffer */
658 if (bytes > sizeof(buf)) {
662 fseek(CC->download_fp, start_pos, 0);
663 bytes = fread(buf, 1, bytes, CC->download_fp);
665 /* Tell the client the actual byte count and transmit it */
666 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
667 client_write(buf, bytes);
670 cprintf("%d %s\n", ERROR, strerror(errno));
676 * write to the upload file
678 void cmd_writ(char *cmdbuf)
686 bytes = extract_int(cmdbuf, 0);
688 if (CC->upload_fp == NULL) {
689 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
693 if (bytes > 100000) {
694 cprintf("%d You may not write more than 100000 bytes.\n",
699 cprintf("%d %d\n", SEND_BINARY, bytes);
700 buf = malloc(bytes + 1);
701 client_read(buf, bytes);
702 rv = fwrite(buf, bytes, 1, CC->upload_fp);
704 syslog(LOG_EMERG, "Couldn't write: %s\n",
714 * cmd_ndop() - open a network spool file for downloading
716 void cmd_ndop(char *cmdbuf)
721 if (IsEmptyStr(CC->net_node)) {
722 cprintf("%d Not authenticated as a network node.\n",
723 ERROR + NOT_LOGGED_IN);
727 if (CC->download_fp != NULL) {
728 cprintf("%d You already have a download file open.\n",
729 ERROR + RESOURCE_BUSY);
733 snprintf(pathname, sizeof pathname,
738 /* first open the file in append mode in order to create a
739 * zero-length file if it doesn't already exist
741 CC->download_fp = fopen(pathname, "a");
742 if (CC->download_fp != NULL)
743 fclose(CC->download_fp);
746 CC->download_fp = fopen(pathname, "r");
747 if (CC->download_fp == NULL) {
748 cprintf("%d cannot open %s: %s\n",
749 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
754 /* set this flag so other routines know that the download file
755 * currently open is a network spool file
759 stat(pathname, &statbuf);
760 CC->download_fp_total = statbuf.st_size;
761 cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
765 * cmd_nuop() - open a network spool file for uploading
767 void cmd_nuop(char *cmdbuf)
771 if (IsEmptyStr(CC->net_node)) {
772 cprintf("%d Not authenticated as a network node.\n",
773 ERROR + NOT_LOGGED_IN);
777 if (CC->upload_fp != NULL) {
778 cprintf("%d You already have an upload file open.\n",
779 ERROR + RESOURCE_BUSY);
783 snprintf(CC->upl_path, sizeof CC->upl_path,
790 CC->upload_fp = fopen(CC->upl_path, "r");
791 if (CC->upload_fp != NULL) {
792 fclose(CC->upload_fp);
793 CC->upload_fp = NULL;
794 cprintf("%d '%s' already exists\n",
795 ERROR + ALREADY_EXISTS, CC->upl_path);
799 CC->upload_fp = fopen(CC->upl_path, "w");
800 if (CC->upload_fp == NULL) {
801 cprintf("%d Cannot open %s: %s\n",
802 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
806 CC->upload_type = UPL_NET;
807 cprintf("%d Ok\n", CIT_OK);
811 /*****************************************************************************/
812 /* MODULE INITIALIZATION STUFF */
813 /*****************************************************************************/
815 CTDL_MODULE_INIT(file_ops)
818 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
819 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
820 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
821 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
822 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
823 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
824 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
825 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
826 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
827 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
828 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
829 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
830 CtdlRegisterCleanupHook(cleanup_nttlist);
832 /* return our Subversion id for the Log */