forward-port r4a30bae41553 from stable
[citadel.git] / citadel / file_ops.c
1 /* 
2  * Server functions which handle file transfers and room directories.
3  */
4
5 #include "sysdep.h"
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <sys/stat.h>
11 #include <errno.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <sys/stat.h>
15 #include <sys/mman.h>
16
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
19 # include <time.h>
20 #else
21 # if HAVE_SYS_TIME_H
22 #  include <sys/time.h>
23 # else
24 #  include <time.h>
25 # endif
26 #endif
27
28 #include <limits.h>
29 #include <libcitadel.h>
30 #include "citadel.h"
31 #include "server.h"
32 #include "config.h"
33 #include "file_ops.h"
34 #include "sysdep_decls.h"
35 #include "support.h"
36 #include "room_ops.h"
37 #include "msgbase.h"
38 #include "citserver.h"
39 #include "threads.h"
40
41 #ifndef HAVE_SNPRINTF
42 #include "snprintf.h"
43 #endif
44
45 #include "ctdl_module.h"
46 #include "user_ops.h"
47
48 /*
49  * network_talking_to()  --  concurrency checker
50  */
51 static char *nttlist = NULL;
52 int network_talking_to(char *nodename, int operation) {
53
54         char *ptr = NULL;
55         int i;
56         char buf[SIZ];
57         int retval = 0;
58
59         begin_critical_section(S_NTTLIST);
60
61         switch(operation) {
62
63                 case NTT_ADD:
64                         if (nttlist == NULL) nttlist = strdup("");
65                         if (nttlist == NULL) break;
66                         nttlist = (char *)realloc(nttlist,
67                                 (strlen(nttlist) + strlen(nodename) + 3) );
68                         strcat(nttlist, "|");
69                         strcat(nttlist, nodename);
70                         break;
71
72                 case NTT_REMOVE:
73                         if (nttlist == NULL) break;
74                         if (IsEmptyStr(nttlist)) break;
75                         ptr = malloc(strlen(nttlist));
76                         if (ptr == NULL) break;
77                         strcpy(ptr, "");
78                         for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
79                                 extract_token(buf, nttlist, i, '|', sizeof buf);
80                                 if ( (!IsEmptyStr(buf))
81                                      && (strcasecmp(buf, nodename)) ) {
82                                                 strcat(ptr, buf);
83                                                 strcat(ptr, "|");
84                                 }
85                         }
86                         free(nttlist);
87                         nttlist = ptr;
88                         break;
89
90                 case NTT_CHECK:
91                         if (nttlist == NULL) break;
92                         if (IsEmptyStr(nttlist)) break;
93                         for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
94                                 extract_token(buf, nttlist, i, '|', sizeof buf);
95                                 if (!strcasecmp(buf, nodename)) ++retval;
96                         }
97                         break;
98         }
99
100         if (nttlist != NULL) CtdlLogPrintf(CTDL_DEBUG, "nttlist=<%s>\n", nttlist);
101         end_critical_section(S_NTTLIST);
102         return(retval);
103 }
104
105 void cleanup_nttlist(void)
106 {
107         begin_critical_section(S_NTTLIST);
108         if (nttlist != NULL)
109                 free(nttlist);
110         nttlist = NULL;
111         end_critical_section(S_NTTLIST);
112 }
113
114
115
116 /*
117  * Server command to delete a file from a room's directory
118  */
119 void cmd_delf(char *filename)
120 {
121         char pathname[64];
122         int a;
123
124         if (CtdlAccessCheck(ac_room_aide))
125                 return;
126
127         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
128                 cprintf("%d No directory in this room.\n",
129                         ERROR + NOT_HERE);
130                 return;
131         }
132
133         if (IsEmptyStr(filename)) {
134                 cprintf("%d You must specify a file name.\n",
135                         ERROR + FILE_NOT_FOUND);
136                 return;
137         }
138         for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
139                 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
140                         filename[a] = '_';
141                 }
142         }
143         snprintf(pathname, sizeof pathname,
144                          "%s/%s/%s",
145                          ctdl_file_dir,
146                          CC->room.QRdirname, filename);
147         a = unlink(pathname);
148         if (a == 0) {
149                 cprintf("%d File '%s' deleted.\n", CIT_OK, pathname);
150         }
151         else {
152                 cprintf("%d File '%s' not found.\n",
153                         ERROR + FILE_NOT_FOUND, pathname);
154         }
155 }
156
157
158
159
160 /*
161  * move a file from one room directory to another
162  */
163 void cmd_movf(char *cmdbuf)
164 {
165         char filename[PATH_MAX];
166         char pathname[PATH_MAX];
167         char newpath[PATH_MAX];
168         char newroom[ROOMNAMELEN];
169         char buf[PATH_MAX];
170         int a;
171         struct ctdlroom qrbuf;
172         int rv = 0;
173
174         extract_token(filename, cmdbuf, 0, '|', sizeof filename);
175         extract_token(newroom, cmdbuf, 1, '|', sizeof newroom);
176
177         if (CtdlAccessCheck(ac_room_aide)) return;
178
179         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
180                 cprintf("%d No directory in this room.\n",
181                         ERROR + NOT_HERE);
182                 return;
183         }
184
185         if (IsEmptyStr(filename)) {
186                 cprintf("%d You must specify a file name.\n",
187                         ERROR + FILE_NOT_FOUND);
188                 return;
189         }
190
191         for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
192                 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
193                         filename[a] = '_';
194                 }
195         }
196         snprintf(pathname, sizeof pathname, "./files/%s/%s",
197                  CC->room.QRdirname, filename);
198         if (access(pathname, 0) != 0) {
199                 cprintf("%d File '%s' not found.\n",
200                         ERROR + FILE_NOT_FOUND, pathname);
201                 return;
202         }
203
204         if (CtdlGetRoom(&qrbuf, newroom) != 0) {
205                 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, newroom);
206                 return;
207         }
208         if ((qrbuf.QRflags & QR_DIRECTORY) == 0) {
209                 cprintf("%d '%s' is not a directory room.\n",
210                         ERROR + NOT_HERE, qrbuf.QRname);
211                 return;
212         }
213         snprintf(newpath, sizeof newpath, "./files/%s/%s", qrbuf.QRdirname,
214                  filename);
215         if (link(pathname, newpath) != 0) {
216                 cprintf("%d Couldn't move file: %s\n", ERROR + INTERNAL_ERROR,
217                         strerror(errno));
218                 return;
219         }
220         unlink(pathname);
221
222         /* this is a crude method of copying the file description */
223         snprintf(buf, sizeof buf,
224                  "cat ./files/%s/filedir |grep \"%s\" >>./files/%s/filedir",
225                  CC->room.QRdirname, filename, qrbuf.QRdirname);
226         rv = system(buf);
227         cprintf("%d File '%s' has been moved.\n", CIT_OK, filename);
228 }
229
230
231 /*
232  * This code is common to all commands which open a file for downloading,
233  * regardless of whether it's a file from the directory, an image, a network
234  * spool file, a MIME attachment, etc.
235  * It examines the file and displays the OK result code and some information
236  * about the file.  NOTE: this stuff is Unix dependent.
237  */
238 void OpenCmdResult(char *filename, const char *mime_type)
239 {
240         struct stat statbuf;
241         time_t modtime;
242         long filesize;
243
244         fstat(fileno(CC->download_fp), &statbuf);
245         CC->download_fp_total = statbuf.st_size;
246         filesize = (long) statbuf.st_size;
247         modtime = (time_t) statbuf.st_mtime;
248
249         cprintf("%d %ld|%ld|%s|%s\n",
250                 CIT_OK, filesize, (long)modtime, filename, mime_type);
251 }
252
253
254 /*
255  * open a file for downloading
256  */
257 void cmd_open(char *cmdbuf)
258 {
259         char filename[256];
260         char pathname[PATH_MAX];
261         int a;
262
263         extract_token(filename, cmdbuf, 0, '|', sizeof filename);
264
265         if (CtdlAccessCheck(ac_logged_in)) return;
266
267         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
268                 cprintf("%d No directory in this room.\n",
269                         ERROR + NOT_HERE);
270                 return;
271         }
272
273         if (IsEmptyStr(filename)) {
274                 cprintf("%d You must specify a file name.\n",
275                         ERROR + FILE_NOT_FOUND);
276                 return;
277         }
278
279         if (CC->download_fp != NULL) {
280                 cprintf("%d You already have a download file open.\n",
281                         ERROR + RESOURCE_BUSY);
282                 return;
283         }
284
285         for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
286                 if ( (filename[a] == '/') || (filename[a] == '\\') ) {
287                         filename[a] = '_';
288                 }
289         }
290
291         snprintf(pathname, sizeof pathname,
292                          "%s/%s/%s",
293                          ctdl_file_dir,
294                          CC->room.QRdirname, filename);
295         CC->download_fp = fopen(pathname, "r");
296
297         if (CC->download_fp == NULL) {
298                 cprintf("%d cannot open %s: %s\n",
299                         ERROR + INTERNAL_ERROR, pathname, strerror(errno));
300                 return;
301         }
302
303         OpenCmdResult(filename, "application/octet-stream");
304 }
305
306 /*
307  * open an image file
308  */
309 void cmd_oimg(char *cmdbuf)
310 {
311         char filename[256];
312         char pathname[PATH_MAX];
313         char MimeTestBuf[32];
314         struct ctdluser usbuf;
315         char which_user[USERNAME_SIZE];
316         int which_floor;
317         int a;
318         int rv;
319
320         extract_token(filename, cmdbuf, 0, '|', sizeof filename);
321
322         if (IsEmptyStr(filename)) {
323                 cprintf("%d You must specify a file name.\n",
324                         ERROR + FILE_NOT_FOUND);
325                 return;
326         }
327
328         if (CC->download_fp != NULL) {
329                 cprintf("%d You already have a download file open.\n",
330                         ERROR + RESOURCE_BUSY);
331                 return;
332         }
333
334         if (!strcasecmp(filename, "_userpic_")) {
335                 extract_token(which_user, cmdbuf, 1, '|', sizeof which_user);
336                 if (CtdlGetUser(&usbuf, which_user) != 0) {
337                         cprintf("%d No such user.\n",
338                                 ERROR + NO_SUCH_USER);
339                         return;
340                 }
341                 snprintf(pathname, sizeof pathname, 
342                                  "%s/%ld",
343                                  ctdl_usrpic_dir,
344                                  usbuf.usernum);
345         } else if (!strcasecmp(filename, "_floorpic_")) {
346                 which_floor = extract_int(cmdbuf, 1);
347                 snprintf(pathname, sizeof pathname,
348                                  "%s/floor.%d",
349                                  ctdl_image_dir, which_floor);
350         } else if (!strcasecmp(filename, "_roompic_")) {
351                 assoc_file_name(pathname, sizeof pathname, &CC->room, ctdl_image_dir);
352         } else {
353                 for (a = 0; !IsEmptyStr(&filename[a]); ++a) {
354                         filename[a] = tolower(filename[a]);
355                         if ( (filename[a] == '/') || (filename[a] == '\\') ) {
356                                 filename[a] = '_';
357                         }
358                 }
359                 snprintf(pathname, sizeof pathname,
360                                  "%s/%s",
361                                  ctdl_image_dir,
362                                  filename);
363         }
364
365         CC->download_fp = fopen(pathname, "rb");
366         if (CC->download_fp == NULL) {
367                 strcat(pathname, ".gif");
368                 CC->download_fp = fopen(pathname, "rb");
369         }
370         if (CC->download_fp == NULL) {
371                 cprintf("%d Cannot open %s: %s\n",
372                         ERROR + FILE_NOT_FOUND, pathname, strerror(errno));
373                 return;
374         }
375         rv = fread(&MimeTestBuf[0], 1, 32, CC->download_fp);
376         rewind (CC->download_fp);
377         OpenCmdResult(pathname, GuessMimeType(&MimeTestBuf[0], 32));
378 }
379
380 /*
381  * open a file for uploading
382  */
383 void cmd_uopn(char *cmdbuf)
384 {
385         int a;
386
387         extract_token(CC->upl_file, cmdbuf, 0, '|', sizeof CC->upl_file);
388         extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
389         extract_token(CC->upl_comment, cmdbuf, 2, '|', sizeof CC->upl_comment);
390
391         if (CtdlAccessCheck(ac_logged_in)) return;
392
393         if ((CC->room.QRflags & QR_DIRECTORY) == 0) {
394                 cprintf("%d No directory in this room.\n",
395                         ERROR + NOT_HERE);
396                 return;
397         }
398
399         if (IsEmptyStr(CC->upl_file)) {
400                 cprintf("%d You must specify a file name.\n",
401                         ERROR + FILE_NOT_FOUND);
402                 return;
403         }
404
405         if (CC->upload_fp != NULL) {
406                 cprintf("%d You already have a upload file open.\n",
407                         ERROR + RESOURCE_BUSY);
408                 return;
409         }
410
411         for (a = 0; !IsEmptyStr(&CC->upl_file[a]); ++a) {
412                 if ( (CC->upl_file[a] == '/') || (CC->upl_file[a] == '\\') ) {
413                         CC->upl_file[a] = '_';
414                 }
415         }
416         snprintf(CC->upl_path, sizeof CC->upl_path, 
417                          "%s/%s/%s",
418                          ctdl_file_dir,
419                          CC->room.QRdirname, CC->upl_file);
420         snprintf(CC->upl_filedir, sizeof CC->upl_filedir,
421                          "%s/%s/filedir", 
422                          ctdl_file_dir,
423                          CC->room.QRdirname);
424
425         CC->upload_fp = fopen(CC->upl_path, "r");
426         if (CC->upload_fp != NULL) {
427                 fclose(CC->upload_fp);
428                 CC->upload_fp = NULL;
429                 cprintf("%d '%s' already exists\n",
430                         ERROR + ALREADY_EXISTS, CC->upl_path);
431                 return;
432         }
433
434         CC->upload_fp = fopen(CC->upl_path, "wb");
435         if (CC->upload_fp == NULL) {
436                 cprintf("%d Cannot open %s: %s\n",
437                         ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
438                 return;
439         }
440         cprintf("%d Ok\n", CIT_OK);
441 }
442
443
444
445 /*
446  * open an image file for uploading
447  */
448 void cmd_uimg(char *cmdbuf)
449 {
450         int is_this_for_real;
451         char basenm[256];
452         int which_floor;
453         int a;
454
455         if (num_parms(cmdbuf) < 2) {
456                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
457                 return;
458         }
459
460         is_this_for_real = extract_int(cmdbuf, 0);
461         extract_token(CC->upl_mimetype, cmdbuf, 1, '|', sizeof CC->upl_mimetype);
462         extract_token(basenm, cmdbuf, 2, '|', sizeof basenm);
463         if (CC->upload_fp != NULL) {
464                 cprintf("%d You already have an upload file open.\n",
465                         ERROR + RESOURCE_BUSY);
466                 return;
467         }
468
469         strcpy(CC->upl_path, "");
470
471         for (a = 0; !IsEmptyStr(&basenm[a]); ++a) {
472                 basenm[a] = tolower(basenm[a]);
473                 if ( (basenm[a] == '/') || (basenm[a] == '\\') ) {
474                         basenm[a] = '_';
475                 }
476         }
477
478         if (CC->user.axlevel >= AxAideU) {
479                 snprintf(CC->upl_path, sizeof CC->upl_path, 
480                                  "%s/%s",
481                                  ctdl_image_dir,
482                                  basenm);
483         }
484
485         if (!strcasecmp(basenm, "_userpic_")) {
486                 snprintf(CC->upl_path, sizeof CC->upl_path,
487                                  "%s/%ld.gif",
488                                  ctdl_usrpic_dir,
489                                  CC->user.usernum);
490         }
491
492         if ((!strcasecmp(basenm, "_floorpic_"))
493             && (CC->user.axlevel >= AxAideU)) {
494                 which_floor = extract_int(cmdbuf, 2);
495                 snprintf(CC->upl_path, sizeof CC->upl_path,
496                                  "%s/floor.%d.gif",
497                                  ctdl_image_dir,
498                                  which_floor);
499         }
500
501         if ((!strcasecmp(basenm, "_roompic_")) && (is_room_aide())) {
502                 assoc_file_name(CC->upl_path, sizeof CC->upl_path, &CC->room, ctdl_image_dir);
503         }
504
505         if (IsEmptyStr(CC->upl_path)) {
506                 cprintf("%d Higher access required.\n",
507                         ERROR + HIGHER_ACCESS_REQUIRED);
508                 return;
509         }
510
511         if (is_this_for_real == 0) {
512                 cprintf("%d Ok to send image\n", CIT_OK);
513                 return;
514         }
515
516         CC->upload_fp = fopen(CC->upl_path, "wb");
517         if (CC->upload_fp == NULL) {
518                 cprintf("%d Cannot open %s: %s\n",
519                         ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
520                 return;
521         }
522         cprintf("%d Ok\n", CIT_OK);
523         CC->upload_type = UPL_IMAGE;
524 }
525
526
527 /*
528  * close the download file
529  */
530 void cmd_clos(char *cmdbuf)
531 {
532         char buf[256];
533
534         if (CC->download_fp == NULL) {
535                 cprintf("%d You don't have a download file open.\n",
536                         ERROR + RESOURCE_NOT_OPEN);
537                 return;
538         }
539
540         fclose(CC->download_fp);
541         CC->download_fp = NULL;
542
543         if (CC->dl_is_net == 1) {
544                 CC->dl_is_net = 0;
545                 snprintf(buf, sizeof buf, 
546                                  "%s/%s",
547                                  ctdl_netout_dir,
548                                  CC->net_node);
549                 unlink(buf);
550         }
551
552         cprintf("%d Ok\n", CIT_OK);
553 }
554
555
556 /*
557  * abort an upload
558  */
559 void abort_upl(CitContext *who)
560 {
561         if (who->upload_fp != NULL) {
562                 fclose(who->upload_fp);
563                 who->upload_fp = NULL;
564                 unlink(CC->upl_path);
565         }
566 }
567
568
569
570 /*
571  * close the upload file
572  */
573 void cmd_ucls(char *cmd)
574 {
575         FILE *fp;
576         char upload_notice[512];
577         static int seq = 0;
578
579         if (CC->upload_fp == NULL) {
580                 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
581                 return;
582         }
583
584         fclose(CC->upload_fp);
585         CC->upload_fp = NULL;
586
587         if ((!strcasecmp(cmd, "1")) && (CC->upload_type != UPL_FILE)) {
588                 cprintf("%d Upload completed.\n", CIT_OK);
589
590                 if (CC->upload_type == UPL_NET) {
591                         char final_filename[PATH_MAX];
592                         snprintf(final_filename, sizeof final_filename,
593                                 "%s/%s.%04lx.%04x",
594                                 ctdl_netin_dir,
595                                 CC->net_node,
596                                 (long)getpid(),
597                                 ++seq
598                         );
599
600                         if (link(CC->upl_path, final_filename) == 0) {
601                                 unlink(CC->upl_path);
602                         }
603                         else {
604                                 CtdlLogPrintf(CTDL_ALERT, "Cannot link %s to %s: %s\n",
605                                         CC->upl_path, final_filename, strerror(errno)
606                                 );
607                         }
608
609                         /* FIXME ... here we need to trigger a network run */
610                 }
611
612                 CC->upload_type = UPL_FILE;
613                 return;
614         }
615
616         if (!strcasecmp(cmd, "1")) {
617                 cprintf("%d File '%s' saved.\n", CIT_OK, CC->upl_path);
618                 fp = fopen(CC->upl_filedir, "a");
619                 if (fp == NULL) {
620                         fp = fopen(CC->upl_filedir, "w");
621                 }
622                 if (fp != NULL) {
623                         fprintf(fp, "%s %s %s\n", CC->upl_file,
624                                 CC->upl_mimetype,
625                                 CC->upl_comment);
626                         fclose(fp);
627                 }
628
629                 /* put together an upload notice */
630                 snprintf(upload_notice, sizeof upload_notice,
631                         "NEW UPLOAD: '%s'\n %s\n%s\n",
632                          CC->upl_file, 
633                          CC->upl_comment, 
634                          CC->upl_mimetype);
635                 quickie_message(CC->curr_user, NULL, NULL, CC->room.QRname,
636                                 upload_notice, 0, NULL);
637         } else {
638                 abort_upl(CC);
639                 cprintf("%d File '%s' aborted.\n", CIT_OK, CC->upl_path);
640         }
641 }
642
643
644 /*
645  * read from the download file
646  */
647 void cmd_read(char *cmdbuf)
648 {
649         long start_pos;
650         size_t bytes;
651         char buf[SIZ];
652
653         /* The client will transmit its requested offset and byte count */
654         start_pos = extract_long(cmdbuf, 0);
655         bytes = extract_int(cmdbuf, 1);
656
657         if (CC->download_fp == NULL) {
658                 cprintf("%d You don't have a download file open.\n",
659                         ERROR + RESOURCE_NOT_OPEN);
660                 return;
661         }
662
663         /* If necessary, reduce the byte count to the size of our buffer */
664         if (bytes > sizeof(buf)) {
665                 bytes = sizeof(buf);
666         }
667
668         fseek(CC->download_fp, start_pos, 0);
669         bytes = fread(buf, 1, bytes, CC->download_fp);
670         if (bytes > 0) {
671                 /* Tell the client the actual byte count and transmit it */
672                 cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
673                 client_write(buf, bytes);
674         }
675         else {
676                 cprintf("%d %s\n", ERROR, strerror(errno));
677         }
678 }
679
680
681 /*
682  * write to the upload file
683  */
684 void cmd_writ(char *cmdbuf)
685 {
686         int bytes;
687         char *buf;
688         int rv;
689
690         unbuffer_output();
691
692         bytes = extract_int(cmdbuf, 0);
693
694         if (CC->upload_fp == NULL) {
695                 cprintf("%d You don't have an upload file open.\n", ERROR + RESOURCE_NOT_OPEN);
696                 return;
697         }
698
699         if (bytes > 100000) {
700                 cprintf("%d You may not write more than 100000 bytes.\n",
701                         ERROR + TOO_BIG);
702                 return;
703         }
704
705         cprintf("%d %d\n", SEND_BINARY, bytes);
706         buf = malloc(bytes + 1);
707         client_read(buf, bytes);
708         rv = fwrite(buf, bytes, 1, CC->upload_fp);
709         free(buf);
710 }
711
712
713
714
715 /*
716  * cmd_ndop() - open a network spool file for downloading
717  */
718 void cmd_ndop(char *cmdbuf)
719 {
720         char pathname[256];
721         struct stat statbuf;
722
723         if (IsEmptyStr(CC->net_node)) {
724                 cprintf("%d Not authenticated as a network node.\n",
725                         ERROR + NOT_LOGGED_IN);
726                 return;
727         }
728
729         if (CC->download_fp != NULL) {
730                 cprintf("%d You already have a download file open.\n",
731                         ERROR + RESOURCE_BUSY);
732                 return;
733         }
734
735         snprintf(pathname, sizeof pathname, 
736                          "%s/%s",
737                          ctdl_netout_dir,
738                          CC->net_node);
739
740         /* first open the file in append mode in order to create a
741          * zero-length file if it doesn't already exist 
742          */
743         CC->download_fp = fopen(pathname, "a");
744         if (CC->download_fp != NULL)
745                 fclose(CC->download_fp);
746
747         /* now open it */
748         CC->download_fp = fopen(pathname, "r");
749         if (CC->download_fp == NULL) {
750                 cprintf("%d cannot open %s: %s\n",
751                         ERROR + INTERNAL_ERROR, pathname, strerror(errno));
752                 return;
753         }
754
755
756         /* set this flag so other routines know that the download file
757          * currently open is a network spool file 
758          */
759         CC->dl_is_net = 1;
760
761         stat(pathname, &statbuf);
762         CC->download_fp_total = statbuf.st_size;
763         cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
764 }
765
766 /*
767  * cmd_nuop() - open a network spool file for uploading
768  */
769 void cmd_nuop(char *cmdbuf)
770 {
771         static int seq = 1;
772
773         if (IsEmptyStr(CC->net_node)) {
774                 cprintf("%d Not authenticated as a network node.\n",
775                         ERROR + NOT_LOGGED_IN);
776                 return;
777         }
778
779         if (CC->upload_fp != NULL) {
780                 cprintf("%d You already have an upload file open.\n",
781                         ERROR + RESOURCE_BUSY);
782                 return;
783         }
784
785         snprintf(CC->upl_path, sizeof CC->upl_path,
786                          "%s/%s.%04lx.%04x",
787                          ctdl_nettmp_dir,
788                          CC->net_node, 
789                          (long)getpid(), 
790                          ++seq);
791
792         CC->upload_fp = fopen(CC->upl_path, "r");
793         if (CC->upload_fp != NULL) {
794                 fclose(CC->upload_fp);
795                 CC->upload_fp = NULL;
796                 cprintf("%d '%s' already exists\n",
797                         ERROR + ALREADY_EXISTS, CC->upl_path);
798                 return;
799         }
800
801         CC->upload_fp = fopen(CC->upl_path, "w");
802         if (CC->upload_fp == NULL) {
803                 cprintf("%d Cannot open %s: %s\n",
804                         ERROR + INTERNAL_ERROR, CC->upl_path, strerror(errno));
805                 return;
806         }
807
808         CC->upload_type = UPL_NET;
809         cprintf("%d Ok\n", CIT_OK);
810 }
811
812
813 /*****************************************************************************/
814 /*                      MODULE INITIALIZATION STUFF                          */
815 /*****************************************************************************/
816
817 CTDL_MODULE_INIT(file_ops)
818 {
819         if (!threading) {
820                 CtdlRegisterProtoHook(cmd_delf, "DELF", "Delete a file");
821                 CtdlRegisterProtoHook(cmd_movf, "MOVF", "Move a file");
822                 CtdlRegisterProtoHook(cmd_open, "OPEN", "Open a download file transfer");
823                 CtdlRegisterProtoHook(cmd_clos, "CLOS", "Close a download file transfer");
824                 CtdlRegisterProtoHook(cmd_uopn, "UOPN", "Open an upload file transfer");
825                 CtdlRegisterProtoHook(cmd_ucls, "UCLS", "Close an upload file transfer");
826                 CtdlRegisterProtoHook(cmd_read, "READ", "File transfer read operation");
827                 CtdlRegisterProtoHook(cmd_writ, "WRIT", "File transfer write operation");
828                 CtdlRegisterProtoHook(cmd_ndop, "NDOP", "Open a network spool file for download");
829                 CtdlRegisterProtoHook(cmd_nuop, "NUOP", "Open a network spool file for upload");
830                 CtdlRegisterProtoHook(cmd_oimg, "OIMG", "Open an image file for download");
831                 CtdlRegisterProtoHook(cmd_uimg, "UIMG", "Upload an image file");
832                 CtdlRegisterCleanupHook(cleanup_nttlist);
833         }
834         /* return our Subversion id for the Log */
835         return "file_ops";
836 }