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 if (GetHashPosFromKey (nttlist, nodename, len, Pos))
74 DeleteEntryFromHash(nttlist, Pos);
76 syslog(LOG_DEBUG, "nttlist: removed <%s>\n", nodename);
81 if ((nttlist == NULL) ||
82 (GetCount(nttlist) == 0))
84 if (GetHash(nttlist, nodename, len, &vdata))
86 syslog(LOG_DEBUG, "nttlist: have [%d] <%s>\n", retval, nodename);
90 end_critical_section(S_NTTLIST);
94 void cleanup_nttlist(void)
96 begin_critical_section(S_NTTLIST);
98 end_critical_section(S_NTTLIST);
104 * Server command to delete a file from a room's directory
106 void cmd_delf(char *filename)
111 if (CtdlAccessCheck(ac_room_aide))
114 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
115 cprintf("%d No directory in this room.\n",
120 if (IsEmptyStr(filename)) {
121 cprintf("%d You must specify a file name.\n",
122 ERROR + FILE_NOT_FOUND);
125 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
126 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
130 snprintf(pathname, sizeof pathname,
133 CC->room.QRdirname, filename);
134 a = unlink(pathname);
136 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
139 cprintf("%d File '%s' not found.\n",
140 ERROR + FILE_NOT_FOUND, pathname);
148 * move a file from one room directory to another
150 void cmd_movf(char *cmdbuf)
152 char filename[PATH_MAX];
153 char pathname[PATH_MAX];
154 char newpath[PATH_MAX];
155 char newroom[ROOMNAMELEN];
158 struct ctdlroom qrbuf;
160 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
161 extract_token(newroom, cmdbuf, 1, '|', sizeof newroom);
163 if (CtdlAccessCheck(ac_room_aide)) return;
165 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
166 cprintf("%d No directory in this room.\n",
171 if (IsEmptyStr(filename)) {
172 cprintf("%d You must specify a file name.\n",
173 ERROR + FILE_NOT_FOUND);
177 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
178 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
182 snprintf(pathname, sizeof pathname, "./files/%s/%s",
183 CC->room.QRdirname, filename);
184 if (access(pathname, 0) != 0) {
185 cprintf("%d File '%s' not found.\n",
186 ERROR + FILE_NOT_FOUND, pathname);
190 if (CtdlGetRoom(&qrbuf, newroom) != 0) {
191 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
194 if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
195 cprintf("%d '%s' is not a directory room.\n",
196 ERROR + NOT_HERE, qrbuf.QRname);
199 snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
201 if (link(pathname, newpath) != 0) {
202 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR,
208 /* this is a crude method of copying the file description */
209 snprintf(buf, sizeof buf,
210 "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir",
211 CC->room.QRdirname, filename, qrbuf.QRdirname);
213 cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
218 * This code is common to all commands which open a file for downloading,
219 * regardless of whether it's a file from the directory, an image, a network
220 * spool file, a MIME attachment, etc.
221 * It examines the file and displays the OK result code and some information
222 * about the file. NOTE: this stuff is Unix dependent.
224 void OpenCmdResult(char *filename, const char *mime_type)
230 fstat(fileno(CC->download_fp), &statbuf);
231 CC->download_fp_total = statbuf.st_size;
232 filesize = (long) statbuf.st_size;
233 modtime = (time_t) statbuf.st_mtime;
235 cprintf("%d %ld|%ld|%s|%s\n",
236 CIT_OK, filesize, (long)modtime, filename, mime_type);
241 * open a file for downloading
243 void cmd_open(char *cmdbuf)
246 char pathname[PATH_MAX];
249 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
251 if (CtdlAccessCheck(ac_logged_in)) return;
253 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
254 cprintf("%d No directory in this room.\n",
259 if (IsEmptyStr(filename)) {
260 cprintf("%d You must specify a file name.\n",
261 ERROR + FILE_NOT_FOUND);
265 if (CC->download_fp != NULL) {
266 cprintf("%d You already have a download file open.\n",
267 ERROR + RESOURCE_BUSY);
271 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
272 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
277 snprintf(pathname, sizeof pathname,
280 CC->room.QRdirname, filename);
281 CC->download_fp = fopen(pathname, "r");
283 if (CC->download_fp == NULL) {
284 cprintf("%d cannot open %s: %s\n",
285 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
289 OpenCmdResult(filename, "application/octet-stream");
295 void cmd_oimg(char *cmdbuf)
298 char pathname[PATH_MAX];
299 char MimeTestBuf[32];
300 struct ctdluser usbuf;
301 char which_user[USERNAME_SIZE];
306 extract_token(filename, cmdbuf, 0, '|', sizeof filename);
308 if (IsEmptyStr(filename)) {
309 cprintf("%d You must specify a file name.\n",
310 ERROR + FILE_NOT_FOUND);
314 if (CC->download_fp != NULL) {
315 cprintf("%d You already have a download file open.\n",
316 ERROR + RESOURCE_BUSY);
320 if (!strcasecmp(filename, "_userpic_")) {
321 extract_token(which_user, cmdbuf, 1, '|', sizeof which_user);
322 if (CtdlGetUser(&usbuf, which_user) != 0) {
323 cprintf("%d No such user.\n",
324 ERROR + NO_SUCH_USER);
327 snprintf(pathname, sizeof pathname,
331 } else if (!strcasecmp(filename, "_floorpic_")) {
332 which_floor = extract_int(cmdbuf, 1);
333 snprintf(pathname, sizeof pathname,
335 ctdl_image_dir, which_floor);
336 } else if (!strcasecmp(filename, "_roompic_")) {
337 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
339 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
340 filename[a] = tolower(filename[a]);
341 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
345 snprintf(pathname, sizeof pathname,
351 CC->download_fp = fopen(pathname, "rb");
352 if (CC->download_fp == NULL) {
353 strcat(pathname, ".gif");
354 CC->download_fp = fopen(pathname, "rb");
356 if (CC->download_fp == NULL) {
357 cprintf("%d Cannot open %s: %s\n",
358 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
361 rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
363 cprintf("%d Cannot access %s: %s\n",
364 ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
368 rewind (CC->download_fp);
369 OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
373 * open a file for uploading
375 void cmd_uopn(char *cmdbuf)
379 extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
380 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
381 extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
383 if (CtdlAccessCheck(ac_logged_in)) return;
385 if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
386 cprintf("%d No directory in this room.\n",
391 if (IsEmptyStr(CC->upl_file)) {
392 cprintf("%d You must specify a file name.\n",
393 ERROR + FILE_NOT_FOUND);
397 if (CC->upload_fp != NULL) {
398 cprintf("%d You already have a upload file open.\n",
399 ERROR + RESOURCE_BUSY);
403 for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
404 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
405 CC->upl_file[a] = '_';
408 snprintf(CC->upl_path, sizeof CC->upl_path,
411 CC->room.QRdirname, CC->upl_file);
412 snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
417 CC->upload_fp = fopen(CC->upl_path, "r");
418 if (CC->upload_fp != NULL) {
419 fclose(CC->upload_fp);
420 CC->upload_fp = NULL;
421 cprintf("%d '%s' already exists\n",
422 ERROR + ALREADY_EXISTS, CC->upl_path);
426 CC->upload_fp = fopen(CC->upl_path, "wb");
427 if (CC->upload_fp == NULL) {
428 cprintf("%d Cannot open %s: %s\n",
429 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
432 cprintf("%d Ok\n", CIT_OK);
438 * open an image file for uploading
440 void cmd_uimg(char *cmdbuf)
442 int is_this_for_real;
447 if (num_parms(cmdbuf) < 2) {
448 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
452 is_this_for_real = extract_int(cmdbuf, 0);
453 extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
454 extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
455 if (CC->upload_fp != NULL) {
456 cprintf("%d You already have an upload file open.\n",
457 ERROR + RESOURCE_BUSY);
461 strcpy(CC->upl_path, "");
463 for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
464 basenm[a] = tolower(basenm[a]);
465 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
470 if (CC->user.axlevel >= AxAideU) {
471 snprintf(CC->upl_path, sizeof CC->upl_path,
477 if (!strcasecmp(basenm, "_userpic_")) {
478 snprintf(CC->upl_path, sizeof CC->upl_path,
484 if ((!strcasecmp(basenm, "_floorpic_"))
485 && (CC->user.axlevel >= AxAideU)) {
486 which_floor = extract_int(cmdbuf, 2);
487 snprintf(CC->upl_path, sizeof CC->upl_path,
493 if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
494 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
497 if (IsEmptyStr(CC->upl_path)) {
498 cprintf("%d Higher access required.\n",
499 ERROR + HIGHER_ACCESS_REQUIRED);
503 if (is_this_for_real == 0) {
504 cprintf("%d Ok to send image\n", CIT_OK);
508 CC->upload_fp = fopen(CC->upl_path, "wb");
509 if (CC->upload_fp == NULL) {
510 cprintf("%d Cannot open %s: %s\n",
511 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
514 cprintf("%d Ok\n", CIT_OK);
515 CC->upload_type = UPL_IMAGE;
520 * close the download file
522 void cmd_clos(char *cmdbuf)
526 if (CC->download_fp == NULL) {
527 cprintf("%d You don't have a download file open.\n",
528 ERROR + RESOURCE_NOT_OPEN);
532 fclose(CC->download_fp);
533 CC->download_fp = NULL;
535 if (CC->dl_is_net == 1) {
537 snprintf(buf, sizeof buf,
544 cprintf("%d Ok\n", CIT_OK);
551 void abort_upl(CitContext *who)
553 if (who->upload_fp != NULL) {
554 fclose(who->upload_fp);
555 who->upload_fp = NULL;
556 unlink(CC->upl_path);
563 * close the upload file
565 void cmd_ucls(char *cmd)
568 char upload_notice[512];
571 if (CC->upload_fp == NULL) {
572 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
576 fclose(CC->upload_fp);
577 CC->upload_fp = NULL;
579 if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
580 cprintf("%d Upload completed.\n", CIT_OK);
582 if (CC->upload_type == UPL_NET) {
583 char final_filename[PATH_MAX];
584 snprintf(final_filename, sizeof final_filename,
592 if (link(CC->upl_path, final_filename) == 0) {
593 unlink(CC->upl_path);
596 syslog(LOG_ALERT, "Cannot link %s to %s: %s\n",
597 CC->upl_path, final_filename, strerror(errno)
601 /* FIXME ... here we need to trigger a network run */
604 CC->upload_type = UPL_FILE;
608 if (!strcasecmp(cmd, "1")) {
609 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
610 fp = fopen(CC->upl_filedir, "a");
612 fp = fopen(CC->upl_filedir, "w");
615 fprintf(fp, "%s %s %s\n", CC->upl_file,
621 /* put together an upload notice */
622 snprintf(upload_notice, sizeof upload_notice,
623 "NEW UPLOAD: '%s'\n %s\n%s\n",
627 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
628 upload_notice, 0, NULL);
631 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
637 * read from the download file
639 void cmd_read(char *cmdbuf)
645 /* The client will transmit its requested offset and byte count */
646 start_pos = extract_long(cmdbuf, 0);
647 bytes = extract_int(cmdbuf, 1);
649 if (CC->download_fp == NULL) {
650 cprintf("%d You don't have a download file open.\n",
651 ERROR + RESOURCE_NOT_OPEN);
655 /* If necessary, reduce the byte count to the size of our buffer */
656 if (bytes > sizeof(buf)) {
660 fseek(CC->download_fp, start_pos, 0);
661 bytes = fread(buf, 1, bytes, CC->download_fp);
663 /* Tell the client the actual byte count and transmit it */
664 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
665 client_write(buf, bytes);
668 cprintf("%d %s\n", ERROR, strerror(errno));
674 * write to the upload file
676 void cmd_writ(char *cmdbuf)
684 bytes = extract_int(cmdbuf, 0);
686 if (CC->upload_fp == NULL) {
687 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
691 if (bytes > 100000) {
695 cprintf("%d %d\n", SEND_BINARY, bytes);
696 buf = malloc(bytes + 1);
697 client_read(buf, bytes);
698 rv = fwrite(buf, bytes, 1, CC->upload_fp);
700 syslog(LOG_EMERG, "Couldn't write: %s\n",
710 * cmd_ndop() - open a network spool file for downloading
712 void cmd_ndop(char *cmdbuf)
717 if (IsEmptyStr(CC->net_node)) {
718 cprintf("%d Not authenticated as a network node.\n",
719 ERROR + NOT_LOGGED_IN);
723 if (CC->download_fp != NULL) {
724 cprintf("%d You already have a download file open.\n",
725 ERROR + RESOURCE_BUSY);
729 snprintf(pathname, sizeof pathname,
734 /* first open the file in append mode in order to create a
735 * zero-length file if it doesn't already exist
737 CC->download_fp = fopen(pathname, "a");
738 if (CC->download_fp != NULL)
739 fclose(CC->download_fp);
742 CC->download_fp = fopen(pathname, "r");
743 if (CC->download_fp == NULL) {
744 cprintf("%d cannot open %s: %s\n",
745 ERROR + INTERNAL_ERROR, pathname, strerror(errno));
750 /* set this flag so other routines know that the download file
751 * currently open is a network spool file
755 stat(pathname, &statbuf);
756 CC->download_fp_total = statbuf.st_size;
757 cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
761 * cmd_nuop() - open a network spool file for uploading
763 void cmd_nuop(char *cmdbuf)
767 if (IsEmptyStr(CC->net_node)) {
768 cprintf("%d Not authenticated as a network node.\n",
769 ERROR + NOT_LOGGED_IN);
773 if (CC->upload_fp != NULL) {
774 cprintf("%d You already have an upload file open.\n",
775 ERROR + RESOURCE_BUSY);
779 snprintf(CC->upl_path, sizeof CC->upl_path,
786 CC->upload_fp = fopen(CC->upl_path, "r");
787 if (CC->upload_fp != NULL) {
788 fclose(CC->upload_fp);
789 CC->upload_fp = NULL;
790 cprintf("%d '%s' already exists\n",
791 ERROR + ALREADY_EXISTS, CC->upl_path);
795 CC->upload_fp = fopen(CC->upl_path, "w");
796 if (CC->upload_fp == NULL) {
797 cprintf("%d Cannot open %s: %s\n",
798 ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
802 CC->upload_type = UPL_NET;
803 cprintf("%d Ok\n", CIT_OK);
807 /*****************************************************************************/
808 /* MODULE INITIALIZATION STUFF */
809 /*****************************************************************************/
811 CTDL_MODULE_INIT(file_ops)
814 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
815 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
816 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
817 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
818 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
819 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
820 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
821 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
822 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
823 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
824 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
825 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
826 CtdlRegisterCleanupHook(cleanup_nttlist);
828 /* return our Subversion id for the Log */