AmendHub

Download

jcs

/

subtext

/

folder.c

 

(View History)

jcs   folder: Minor bug fixes Latest amendment: 644 on 2026-09-22

1 /*
2 * Copyright (c) 2022 joshua stein <jcs@jcs.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "subtext.h"
23 #include "ansi.h"
24 #include "folder.h"
25 #include "nomodem.h"
26 #include "sha1.h"
27 #include "user.h"
28 #include "zmodem.h"
29
30 #define FILES_PER_PAGE 20
31
32 #define FILE_VIEW_RETURN_DONE -1
33 #define FILE_VIEW_RETURN_LIST -2
34 #define FILE_VIEW_RETURN_FIND -3
35
36 const struct struct_field folder_fields[] = {
37 { "Folder ID", CONFIG_TYPE_LONG,
38 offsetof(struct folder, id),
39 1, LONG_MAX },
40 { "Name", CONFIG_TYPE_STRING,
41 offsetof(struct folder, name),
42 1, member_size(struct folder, name) },
43 { "Disk Path", CONFIG_TYPE_STRING,
44 offsetof(struct folder, path),
45 1, member_size(struct folder, path) },
46 { "Description", CONFIG_TYPE_STRING,
47 offsetof(struct folder, description),
48 0, member_size(struct folder, description) },
49 { "Restricted Posting", CONFIG_TYPE_BOOLEAN,
50 offsetof(struct folder, restricted_posting),
51 0, 0 },
52 { "Restricted Viewing", CONFIG_TYPE_BOOLEAN,
53 offsetof(struct folder, restricted_viewing),
54 0, 0 },
55 };
56 const size_t nfolder_fields = nitems(folder_fields);
57
58 const struct bile_object_field folder_object_fields[] = {
59 { offsetof(struct folder, id),
60 member_size(struct folder, id), -1 },
61 { offsetof(struct folder, name),
62 member_size(struct folder, name), -1 },
63 { offsetof(struct folder, description),
64 member_size(struct folder, description), -1 },
65 { offsetof(struct folder, restricted_posting),
66 member_size(struct folder, restricted_posting), -1 },
67 { offsetof(struct folder, restricted_viewing),
68 member_size(struct folder, restricted_viewing), -1 },
69 { offsetof(struct folder, last_upload_at),
70 member_size(struct folder, last_upload_at), -1 },
71 { offsetof(struct folder, file_count),
72 member_size(struct folder, file_count), -1 },
73 { offsetof(struct folder, path),
74 member_size(struct folder, path), -1 },
75 };
76 const size_t nfolder_object_fields = nitems(folder_object_fields);
77
78 const struct bile_object_field folder_file_object_fields[] = {
79 { offsetof(struct folder_file, id),
80 member_size(struct folder_file, id), -1 },
81 { offsetof(struct folder_file, time),
82 member_size(struct folder_file, time), -1 },
83 { offsetof(struct folder_file, uploader_user_id),
84 member_size(struct folder_file, uploader_user_id), -1 },
85 { offsetof(struct folder_file, filename),
86 member_size(struct folder_file, filename), -1 },
87 { offsetof(struct folder_file, description),
88 member_size(struct folder_file, description), -1 },
89 { offsetof(struct folder_file, size),
90 member_size(struct folder_file, size), -1 },
91 { offsetof(struct folder_file, sha1_checksum),
92 member_size(struct folder_file, sha1_checksum), -1 },
93 { offsetof(struct folder_file, notes_size),
94 member_size(struct folder_file, notes_size), -1 },
95 { offsetof(struct folder_file, notes),
96 -1, offsetof(struct folder_file, notes_size) },
97 };
98 const size_t nfolder_file_object_fields = nitems(folder_file_object_fields);
99
100 unsigned long folder_upload(struct session *s, struct folder *folder,
101 char *initial_filename, char *initial_description);
102 void folder_list_files(struct session *s, struct folder *folder,
103 size_t nfile_ids, unsigned long *file_ids, short files_per_page,
104 size_t page, size_t pages);
105 void folder_show(struct session *s, struct folder *folder);
106 short folder_file_view(struct session *s, struct folder *folder,
107 unsigned long id);
108 void folder_delete_file(struct session *s, struct folder *folder,
109 struct folder_file *file);
110 size_t folder_find_file_ids(struct folder *folder, size_t *nfile_ids,
111 unsigned long **file_ids);
112 bool folder_file_save(struct folder *folder, struct folder_file *file,
113 char *temp_path);
114 bool folder_file_valid_filename(struct session *session,
115 struct folder *folder, struct folder_file *file, char **error);
116 bool folder_edit_file(struct session *s, struct folder *folder,
117 struct folder_file *file, char *file_path);
118 bool folder_file_checksum(struct session *s, struct folder_file *file,
119 char *file_path);
120
121 void
122 folder_list(struct session *s)
123 {
124 static const struct session_menu_option opts[] = {
125 { '#', "#", "View file folder [#]" },
126 { 'l', "Ll", "List folders" },
127 { 'q', "QqXx", "Return to main menu" },
128 { '?', "?", "List menu options" },
129 };
130 static const char prompt_help[] =
131 "#:View Folder L:List Q:Return ?:Help";
132 short n, fn;
133 char c;
134 bool done, show_list, show_help;
135
136 show_list = true;
137 show_help = false;
138 done = false;
139
140 while (!done && !s->ending) {
141 if (show_list) {
142 session_printf(s, "{{B}}File Folders{{/B}}\r\n");
143 session_printf(s, "%s# Name Description%s\r\n",
144 ansi(s, ANSI_BOLD, ANSI_END), ansi(s, ANSI_RESET, ANSI_END));
145 session_flush(s);
146
147 for (n = 0; n < db->nfolders; n++) {
148 if (db->folders[n].restricted_viewing &&
149 !(s->user && s->user->is_sysop))
150 continue;
151
152 session_printf(s, "%2d %- 13.13s %s\r\n",
153 n + 1,
154 db->folders[n].name,
155 db->folders[n].description);
156 }
157 session_flush(s);
158
159 show_list = false;
160 }
161
162 c = session_menu(s, "File Folders", "Files", (char *)prompt_help,
163 opts, nitems(opts), show_help, "Folder #", &fn);
164 show_help = false;
165
166 handle_opt:
167 switch (c) {
168 case 'l':
169 show_list = true;
170 break;
171 case '#':
172 check_fn:
173 if (fn < 1 || fn > db->nfolders) {
174 session_printf(s, "Invalid folder\r\n");
175 session_flush(s);
176 break;
177 }
178 folder_show(s, &db->folders[fn - 1]);
179 break;
180 case '?':
181 show_help = true;
182 break;
183 default:
184 done = true;
185 break;
186 }
187 }
188 }
189
190 void
191 folder_show(struct session *s, struct folder *folder)
192 {
193 static const struct session_menu_option opts[] = {
194 { '#', "#", "View file [#]" },
195 { '<', "<", "Newer page of files" },
196 { 'l', "Ll", "List files" },
197 { '>', ">", "Older page of files" },
198 { 'u', "Uu", "Upload new file" },
199 { 'q', "QqXx", "Return to main menu" },
200 { '?', "?", "List menu options" },
201 };
202 static const char prompt_help[] =
203 "#:View <:Newer >:Older L:List U:Upload Q:Return ?:Help";
204 char prompt[6 + member_size(struct folder, name) + 8];
205 size_t page, pages, nfile_ids;
206 unsigned long *file_ids = NULL;
207 short fpp, fn;
208 char c;
209 bool done, show_list, show_help, find_files;
210
211 page = 0;
212 show_list = true;
213 find_files = true;
214 show_help = false;
215 done = false;
216
217 if (folder->restricted_viewing && !(s->user && s->user->is_sysop)) {
218 session_printf(s, "Invalid folder\r\n");
219 session_flush(s);
220 return;
221 }
222
223 snprintf(prompt, sizeof(prompt), "Files:%s", folder->name);
224
225 while (!done && !s->ending) {
226 if (find_files) {
227 if (file_ids != NULL)
228 xfree(&file_ids);
229 folder_find_file_ids(folder, &nfile_ids, &file_ids);
230 fpp = FILES_PER_PAGE;
231 if (s->terminal_lines < fpp + 3)
232 fpp = BOUND(fpp, 5, s->terminal_lines - 3);
233 /* ceil(nfile_ids / fpp) */
234 pages = (nfile_ids + fpp - 1) / fpp;
235
236 if (page >= pages)
237 page = pages - 1;
238
239 find_files = false;
240 }
241
242 if (show_list) {
243 folder_list_files(s, folder, nfile_ids, file_ids, fpp,
244 page + 1, pages);
245 show_list = false;
246 }
247
248 c = session_menu(s, folder->description, prompt, (char *)prompt_help,
249 opts, nitems(opts), show_help, "File #", &fn);
250 show_help = false;
251
252 handle_opt:
253 switch (c) {
254 case 'l':
255 show_list = true;
256 break;
257 case 'u':
258 if (folder_upload(s, folder, NULL, NULL))
259 find_files = true;
260 break;
261 case '>':
262 case '<':
263 if (c == '>' && page == pages - 1) {
264 session_printf(s, "You are at the last page of files\r\n");
265 session_flush(s);
266 break;
267 }
268 if (c == '<' && page == 0) {
269 session_printf(s, "You are already at the first page\r\n");
270 session_flush(s);
271 break;
272 }
273 if (c == '>')
274 page++;
275 else
276 page--;
277 show_list = true;
278 break;
279 case '#':
280 if (fn < 1 || fn > fpp ||
281 (fpp * page) + fn > nfile_ids) {
282 session_printf(s, "Invalid file\r\n");
283 session_flush(s);
284 break;
285 }
286 c = folder_file_view(s, folder,
287 file_ids[(fpp * page) + fn - 1]);
288 if (c == FILE_VIEW_RETURN_FIND)
289 find_files = true;
290 break;
291 case '?':
292 show_help = true;
293 break;
294 default:
295 done = true;
296 break;
297 }
298 }
299
300 if (file_ids != NULL)
301 xfree(&file_ids);
302 }
303
304 void
305 folder_list_files(struct session *s, struct folder *folder,
306 size_t nfile_ids, unsigned long *file_ids, short files_per_page,
307 unsigned long page, unsigned long pages)
308 {
309 char time[24];
310 size_t n, off, size;
311 struct folder_file file;
312 short ret;
313 char *data;
314
315 session_printf(s, "{{B}}%s: %s (Page %ld of %ld){{/B}}\r\n",
316 folder->name, folder->description, page, pages);
317 session_printf(s,
318 "%s # Date File Description%s\r\n",
319 ansi(s, ANSI_BOLD, ANSI_END), ansi(s, ANSI_RESET, ANSI_END));
320 session_flush(s);
321
322 if (nfile_ids == 0) {
323 session_printf(s, "No files here yet.\r\n");
324 session_flush(s);
325 return;
326 }
327
328 off = files_per_page * (page - 1);
329
330 for (n = 0; n < files_per_page; n++) {
331 if (off + n >= nfile_ids)
332 break;
333
334 size = bile_read_alloc(folder->bile, FOLDER_FILE_RTYPE,
335 file_ids[off + n], &data);
336 ret = bile_unmarshall_object(folder->bile, folder_file_object_fields,
337 nfolder_file_object_fields, data, size, &file, sizeof(file),
338 false);
339 xfree(&data);
340 if (ret == BILE_ERR_NO_MEMORY)
341 return;
342
343 strftime(time, sizeof(time), "%Y-%m-%d", localtime(&file.time));
344
345 session_printf(s, "%2ld %s {{#}}%- 17.17s %- 40s\r\n",
346 n + 1,
347 time,
348 file.filename,
349 file.description);
350 }
351 session_flush(s);
352 }
353
354 unsigned long
355 folder_upload(struct session *s, struct folder *folder,
356 char *initial_filename, char *initial_description)
357 {
358 struct folder_file file = { 0 };
359 struct stat sb;
360 struct zmodem_session *zs;
361 struct nomodem_session *ns;
362 char *upload_path = NULL, *file_name;
363 size_t file_size;
364 short error;
365
366 if (!s->user) {
367 session_printf(s, "Uploading is not available to guests.\r\n"
368 "Please create an account first.\r\n");
369 session_flush(s);
370 return 0;
371 }
372
373 if (folder->restricted_posting && !s->user->is_sysop) {
374 session_printf(s, "Uploading to this folder is not permitted.\r\n");
375 session_flush(s);
376 return 0;
377 }
378
379 if (initial_filename)
380 strlcpy(file.filename, initial_filename, sizeof(file.filename));
381
382 file.uploader_user_id = s->user->id;
383
384 upload_path = xmalloc(FILENAME_MAX);
385 if (upload_path == NULL)
386 return 0;
387
388 session_printf(s, "{{B}}Upload New File{{/B}}\r\n");
389 if (s->can_nomodem) {
390 session_printf(s,
391 "To begin your upload, press Enter and choose a file (only one file\r\n");
392 session_printf(s,
393 "can be uploaded at a time).\r\n");
394 } else {
395 session_printf(s,
396 "To begin your ZMODEM upload, press Enter and choose a file through\r\n");
397 session_printf(s,
398 "your terminal program (only one file can be uploaded at a time).\r\n");
399 }
400 session_printf(s,
401 "\r\n"
402 "You'll then be given the option to change the filename and enter a\r\n"
403 "description for the file.\r\n"
404 "\r\n"
405 "To cancel your upload, press ^C a few times.\r\n\r\n");
406 session_flush(s);
407 session_pause_return(s, 0, "when you are ready...");
408
409 snprintf(upload_path, FILENAME_MAX, "%s:upload-%08lx%08lx",
410 folder->path, xorshift32(), xorshift32());
411
412 if (s->can_nomodem) {
413 ns = nomodem_receive(s, upload_path);
414 if (ns == NULL) {
415 xfree(&upload_path);
416 return 0;
417 }
418 } else {
419 zs = ZCreateReceiver(s, upload_path);
420 if (zs == NULL) {
421 xfree(&upload_path);
422 return 0;
423 }
424 zs->DoIACEscape = s->is_telnet;
425
426 ZInit(zs);
427 s->direct_output = true;
428 }
429
430 session_flush(s);
431 session_logf(s, "[%s] Receiving uploaded file to %s", folder->name,
432 upload_path);
433 while (!s->ending) {
434 if (s->can_nomodem) {
435 if (nomodem_timed_out(ns))
436 goto timed_out;
437 if (!nomodem_continue(ns))
438 break;
439 } else {
440 if (ZHaveTimedOut(zs)) {
441 ZTimeOutProc(zs);
442 goto timed_out;
443 }
444
445 if (!ZParse(zs))
446 break;
447 }
448
449 if (s->obuflen)
450 session_flush(s);
451
452 continue;
453 timed_out:
454 session_logf(s, "[%s] Transfer timed out, canceling", folder->name);
455 break;
456 }
457
458 /* flush the pipes */
459 s->obuflen = 0;
460 uthread_msleep(1000);
461 session_clear_input(s);
462 s->direct_output = false;
463
464 if (s->can_nomodem) {
465 file_size = ns->file_size;
466 if (ns->file) {
467 fclose(ns->file);
468 ns->file = NULL;
469 }
470 } else {
471 if (zs->file) {
472 fclose(zs->file);
473 zs->file = NULL;
474 }
475 file_size = zs->file_size;
476 }
477
478 session_printf(s, "\r\n\r\n");
479
480 if (stat(upload_path, &sb) != 0 || sb.st_size == 0) {
481 session_logf(s, "[%s] Failed receiving upload, no temp file",
482 folder->name);
483 session_printf(s, "Failed receiving file, aborting.\r\n");
484 session_pause_return(s, CONTROL_C, "to continue...");
485 if (s->can_nomodem) {
486 nomodem_finish(&ns);
487 } else {
488 ZDestroy(zs);
489 zs = NULL;
490 }
491 xfree(&upload_path);
492 return 0;
493 }
494
495 if (sb.st_size != file_size) {
496 session_logf(s, "[%s] Received uploaded file of size %ld but "
497 "supposed to be %ld, canceling", folder->name, sb.st_size,
498 file_size);
499 session_printf(s,
500 "Uploaded file expected to be {{B}}%ld{{/B}} byte%s, but "
501 "received\r\n"
502 "{{B}}%ld{{/B}} byte%s. Deleting file and canceling upload.\r\n",
503 file_size, file_size == 1 ? "" : "s",
504 sb.st_size, sb.st_size == 1 ? "" : "s");
505 session_pause_return(s, CONTROL_C, "to continue...");
506 if (s->can_nomodem) {
507 nomodem_finish(&ns);
508 } else {
509 ZDestroy(zs);
510 zs = NULL;
511 }
512 goto file_upload_cancel;
513 }
514
515 if (s->can_nomodem)
516 file_name = ns->file_name;
517 else
518 file_name = zs->file_name;
519
520 strlcpy(file.filename, file_name, sizeof(file.filename));
521 file.size = sb.st_size;
522
523 session_printf(s, "Successfully received file {{B}}%s{{/B}} of size "
524 "{{B}}%ld{{/B}} byte%s.\r\n",
525 file.filename, file.size, file.size == 1 ? "" : "s");
526 session_pause_return(s, '\r', "to continue...");
527
528 session_logf(s, "[%s] Received uploaded file %s of size %ld",
529 folder->name, file.filename, file.size);
530
531 if (s->can_nomodem) {
532 nomodem_finish(&ns);
533 } else {
534 ZDestroy(zs);
535 zs = NULL;
536 }
537
538 session_printf(s, "Calculating SHA1 checksum of file... ");
539 session_flush(s);
540 if (!folder_file_checksum(s, &file, upload_path)) {
541 session_printf(s, "failed reading file!\r\n");
542 session_flush(s);
543 goto file_upload_cancel;
544 }
545 session_output(s, "\r\n\r\n", 4);
546 session_flush(s);
547
548 if (folder_edit_file(s, folder, &file, upload_path))
549 goto file_upload_done;
550 else {
551 session_printf(s, "\r\nFailed saving file!\r\n");
552 session_flush(s);
553 }
554
555 file_upload_cancel:
556 session_printf(s, "\r\n");
557 session_flush(s);
558
559 if (upload_path[0] != '\0') {
560 CtoPstr(upload_path);
561 error = FSDelete(upload_path, 0);
562 PtoCstr(upload_path);
563 if (error) {
564 session_logf(s, "[%s] Failed deleting temporary uploaded "
565 "file %s: %d", folder->name, upload_path, error);
566 } else {
567 session_logf(s, "[%s] Canceled upload, deleted temp file %s",
568 folder->name, upload_path);
569 }
570 }
571
572 file_upload_done:
573 if (file.notes)
574 xfree(&file.notes);
575 if (upload_path != NULL)
576 xfree(&upload_path);
577
578 return file.id;
579 }
580
581 short
582 folder_file_view(struct session *s, struct folder *folder,
583 unsigned long id)
584 {
585 static const struct session_menu_option opts[] = {
586 { 'd', "Dd", "Download this file" },
587 { 'r', "Rr", "Remove this file" },
588 { 'e', "Ee", "Edit this file" },
589 { 'q', "QqXx", "Return to folder" },
590 { '?', "?", "List these options" },
591 };
592 static const char prompt_help[] =
593 "D:Download R:Delete E:Edit Q:Return ?:Help";
594 char time[32];
595 char prompt[6 + member_size(struct folder, name) + 1 +
596 member_size(struct folder_file, filename)];
597 char *path = NULL, *data, c;
598 struct folder_file file;
599 struct username_cache *uploader;
600 struct zmodem_session *zs;
601 struct nomodem_session *ns;
602 struct session_menu_option *dopts = NULL;
603 FILE *fp;
604 size_t size;
605 short cc, n, bret, ret = FILE_VIEW_RETURN_FIND;
606 bool done = false, show_help = false;
607
608 size = bile_read_alloc(folder->bile, FOLDER_FILE_RTYPE, id, &data);
609 if (size == 0)
610 panic("failed fetching message %ld: %d", id,
611 bile_error(folder->bile));
612 bret = bile_unmarshall_object(folder->bile, folder_file_object_fields,
613 nfolder_file_object_fields, data, size, &file, sizeof(file), true);
614 xfree(&data);
615 if (bret == BILE_ERR_NO_MEMORY)
616 return ret;
617
618 dopts = xmalloc(sizeof(opts));
619 if (dopts == NULL)
620 return ret;
621 memcpy(dopts, opts, sizeof(opts));
622 if (!(s->user && (s->user->is_sysop ||
623 s->user->id == file.uploader_user_id))) {
624 /* disable deleting and editing */
625 for (n = 0; n < nitems(opts); n++) {
626 if (dopts[n].ret == 'r' || dopts[n].ret == 'e')
627 dopts[n].key[0] = '\0';
628 }
629 }
630
631 uploader = user_username(file.uploader_user_id);
632
633 strftime(time, sizeof(time), "%Y-%m-%d %H:%M:%S",
634 localtime(&file.time));
635
636 session_printf(s, "{{B}}Folder:{{/B}}{{#}} %s\r\n",
637 folder->name);
638 session_printf(s, "{{B}}File Name:{{/B}}{{#}} %s\r\n",
639 file.filename);
640 session_printf(s, "{{B}}Description:{{/B}}{{#}} %s\r\n",
641 file.description);
642 session_printf(s, "{{B}}File Size:{{/B}} %lu\r\n", file.size);
643 session_printf(s, "{{B}}File SHA1:{{/B}} %s\r\n", file.sha1_checksum);
644 session_printf(s, "{{B}}Uploaded:{{/B}} %s %s\r\n", time,
645 db->config.timezone);
646 session_printf(s, "{{B}}Uploaded By:{{/B}} %s\r\n",
647 uploader ? uploader->username : "(unknown)");
648 session_flush(s);
649
650 if (file.notes_size > 0) {
651 session_printf(s, "{{B}}Notes:{{/B}}\r\n");
652 session_output(s, file.notes, file.notes_size);
653 session_printf(s, "\r\n");
654 }
655
656 session_printf(s,
657 "\r\n[ Not viewing file contents, press 'd' to download. ]\r\n");
658 session_flush(s);
659
660 snprintf(prompt, sizeof(prompt), "Files:%s:%s", folder->name,
661 file.filename);
662
663 while (!done && !s->ending) {
664 c = session_menu(s, file.filename, prompt, (char *)prompt_help, dopts,
665 nitems(opts), show_help, NULL, NULL);
666 show_help = false;
667
668 switch (c) {
669 case 'd':
670 path = xmalloc(FILENAME_MAX);
671 if (path == NULL) {
672 done = true;
673 break;
674 }
675 snprintf(path, FILENAME_MAX, "%s:%s", folder->path,
676 file.filename);
677 fp = fopen(path, "rb");
678 if (!fp) {
679 session_logf(s, "[%s] Failed opening file %s",
680 folder->name, path);
681 session_printf(s,
682 "{{B}}Error:{{/B}} Failed opening file\r\n");
683 xfree(&path);
684 done = true;
685 break;
686 }
687
688 if (s->can_nomodem) {
689 ns = nomodem_send(s, fp, file.filename);
690 if (ns == NULL) {
691 fclose(fp);
692 xfree(&path);
693 done = true;
694 break;
695 }
696 } else {
697 zs = ZCreateSender(s, fp, file.filename);
698 if (zs == NULL) {
699 fclose(fp);
700 xfree(&path);
701 done = true;
702 break;
703 }
704 zs->DoIACEscape = s->is_telnet;
705 ZInit(zs);
706 s->direct_output = true;
707 }
708
709 session_logf(s, "[%s] Downloading file %s", folder->name,
710 file.filename);
711 for (;;) {
712 if (s->can_nomodem) {
713 if (nomodem_timed_out(ns))
714 goto timed_out;
715 if (!nomodem_continue(ns))
716 break;
717 } else {
718 if (ZHaveTimedOut(zs)) {
719 ZTimeOutProc(zs);
720 goto timed_out;
721 }
722
723 if (!ZParse(zs))
724 break;
725 }
726
727 if (s->obuflen)
728 session_flush(s);
729 continue;
730 timed_out:
731 session_logf(s, "[%s] Transfer timed out, canceling",
732 folder->name);
733 break;
734 }
735
736 /* flush the pipes */
737 s->obuflen = 0;
738 uthread_msleep(1000);
739 session_clear_input(s);
740
741 if (s->can_nomodem) {
742 nomodem_finish(&ns);
743 } else {
744 s->direct_output = false;
745 if (zs->file) {
746 fclose(zs->file);
747 zs->file = NULL;
748 }
749 ZDestroy(zs);
750 zs = NULL;
751 }
752 xfree(&path);
753
754 session_printf(s, "\r\n");
755 session_flush(s);
756 session_pause_return(s, CONTROL_C, "to continue...");
757 break;
758 case 'e':
759 if (!(s->user && (s->user->is_sysop ||
760 s->user->id == file.uploader_user_id))) {
761 session_printf(s, "Invalid option\r\n");
762 session_flush(s);
763 break;
764 }
765 path = xmalloc(FILENAME_MAX);
766 if (path == NULL) {
767 done = true;
768 break;
769 }
770 snprintf(path, FILENAME_MAX, "%s:%s", folder->path,
771 file.filename);
772 folder_edit_file(s, folder, &file, path);
773 xfree(&path);
774 break;
775 case 'r':
776 if (!(s->user && (s->user->is_sysop ||
777 s->user->id == file.uploader_user_id))) {
778 session_printf(s, "Invalid option\r\n");
779 session_flush(s);
780 break;
781 }
782
783 session_printf(s, "Are you sure you want to permanently "
784 "delete this file? [y/N] ");
785 session_flush(s);
786
787 cc = session_input_char(s);
788 if (cc == 'y' || cc == 'Y') {
789 session_printf(s, "%c\r\n", cc);
790 session_flush(s);
791
792 folder_delete_file(s, folder, &file);
793
794 session_printf(s, "\r\n{{B}}File deleted!{{/B}}\r\n");
795 session_flush(s);
796 ret = FILE_VIEW_RETURN_FIND;
797 done = true;
798 } else {
799 session_printf(s, "\r\nFile not deleted.\r\n");
800 session_flush(s);
801 }
802 break;
803 case 'q':
804 done = true;
805 break;
806 case '?':
807 show_help = true;
808 break;
809 }
810 }
811
812 if (file.notes != NULL)
813 xfree(&file.notes);
814
815 xfree(&dopts);
816
817 return ret;
818 }
819
820 size_t
821 folder_find_file_ids(struct folder *folder, size_t *nfile_ids,
822 unsigned long **file_ids)
823 {
824 struct bile_object *o;
825 struct folder_file file;
826 struct file_name_map {
827 unsigned long id;
828 char filename[10]; /* only sorted to 10 character places */
829 } *name_map = NULL, tmp_map;
830 size_t n;
831 short i, j, ret;
832 char *data;
833
834 *nfile_ids = bile_count_by_type(folder->bile, FOLDER_FILE_RTYPE);
835 if (*nfile_ids == 0)
836 return 0;
837
838 name_map = xcalloc(*nfile_ids, sizeof(struct file_name_map));
839 if (name_map == NULL)
840 return 0;
841
842 for (n = 0; (o = bile_get_nth_of_type(folder->bile, n,
843 FOLDER_FILE_RTYPE)); n++) {
844 if (n >= *nfile_ids) {
845 xfree(&o);
846 break;
847 }
848 if (bile_read_alloc(folder->bile, FOLDER_FILE_RTYPE, o->id,
849 &data) == 0) {
850 if (data != NULL)
851 xfree(&data);
852 xfree(&o);
853 break;
854 }
855 ret = bile_unmarshall_object(folder->bile, folder_file_object_fields,
856 nfolder_file_object_fields, data, o->size, &file, sizeof(file),
857 false);
858 xfree(&o);
859 xfree(&data);
860 if (ret == 0 && bile_error(folder->bile) == BILE_ERR_NO_MEMORY) {
861 xfree(&name_map);
862 return 0;
863 }
864
865 name_map[n].id = file.id;
866 strlcpy(name_map[n].filename, file.filename,
867 sizeof(name_map[n].filename));
868 }
869
870 /* sort by filename */
871 for (i = 1; i < *nfile_ids; i++) {
872 for (j = i; j > 0; j--) {
873 if (strcasecmp(name_map[j].filename,
874 name_map[j - 1].filename) > 0)
875 break;
876 tmp_map = name_map[j];
877 name_map[j] = name_map[j - 1];
878 name_map[j - 1] = tmp_map;
879 }
880 }
881
882 *file_ids = xcalloc(sizeof(long), *nfile_ids);
883 if (*file_ids == NULL) {
884 xfree(&name_map);
885 return 0;
886 }
887 for (i = 0; i < *nfile_ids; i++)
888 (*file_ids)[i] = name_map[i].id;
889
890 xfree(&name_map);
891
892 return *nfile_ids;
893 }
894
895 bool
896 folder_file_save(struct folder *folder, struct folder_file *file,
897 char *temp_path)
898 {
899 char *new_name = NULL;
900 short ret;
901 char *data;
902 size_t size;
903 bool need_move = false;
904
905 if (!file->id) {
906 need_move = true;
907 file->id = bile_next_id(folder->bile, FOLDER_FILE_RTYPE);
908 }
909 if (!file->time)
910 file->time = Time;
911
912 ret = bile_marshall_object(folder->bile, folder_file_object_fields,
913 nfolder_file_object_fields, file, &data, &size);
914 if (ret != 0 || size == 0) {
915 warn("failed to marshall new file object");
916 file->id = 0;
917 return false;
918 }
919 if (bile_write(folder->bile, FOLDER_FILE_RTYPE, file->id, data,
920 size) != size) {
921 warn("bile_write of new file failed! %d", bile_error(folder->bile));
922 file->id = 0;
923 xfree(&data);
924 return false;
925 }
926 xfree(&data);
927
928 bile_flush(folder->bile, true);
929
930 if (need_move) {
931 new_name = xmalloc(FILENAME_MAX);
932 if (new_name == NULL)
933 return false;
934 snprintf(new_name, FILENAME_MAX, "%s:%s", folder->path,
935 file->filename);
936 CtoPstr(temp_path);
937 CtoPstr(new_name);
938 ret = Rename(temp_path, 0, new_name);
939 PtoCstr(temp_path);
940 PtoCstr(new_name);
941 if (ret != 0) {
942 warn("FSRename(%s, 0, %s) failed: %d", temp_path, new_name,
943 ret);
944 bile_delete(folder->bile, FOLDER_FILE_RTYPE, file->id,
945 BILE_DELETE_FLAG_ZERO | BILE_DELETE_FLAG_PURGE);
946 file->id = 0;
947 xfree(&new_name);
948 return false;
949 }
950
951 xfree(&new_name);
952 }
953
954 return true;
955 }
956
957 void
958 folder_delete_file(struct session *s, struct folder *folder,
959 struct folder_file *file)
960 {
961 char *path = NULL;
962 short ret;
963
964 bile_delete(folder->bile, FOLDER_FILE_RTYPE, file->id,
965 BILE_DELETE_FLAG_ZERO | BILE_DELETE_FLAG_PURGE);
966 bile_flush(folder->bile, true);
967
968 path = xmalloc(FILENAME_MAX);
969 if (path == NULL)
970 return;
971 snprintf(path, FILENAME_MAX, "%s:%s", folder->path, file->filename);
972 CtoPstr(path);
973 ret = FSDelete(path, 0);
974 PtoCstr(path);
975
976 if (ret != 0)
977 session_logf(s, "[%s] Failed deleting %s: %d", folder->name,
978 path, ret);
979
980 xfree(&path);
981 if (file->notes != NULL)
982 xfree(&file->notes);
983
984 session_logf(s, "[%s] Deleted file %s (%ld)", folder->name,
985 file->filename, file->id);
986 }
987
988 bool
989 folder_file_valid_filename(struct session *session,
990 struct folder *folder, struct folder_file *file, char **error)
991 {
992 struct bile_object *o;
993 struct folder_file tfile;
994 size_t len, n;
995 short ret;
996 char *data;
997 char c;
998
999 if (file->filename[0] == '\0') {
1000 *error = xstrdup("filename cannot be empty");
1001 return false;
1002 }
1003
1004 len = strlen(file->filename);
1005 if (len > FOLDER_FILE_FILENAME_LENGTH) {
1006 *error = xmalloc(61);
1007 if (*error)
1008 snprintf(*error, 60, "filename cannot be more than %d "
1009 "characters", FOLDER_FILE_FILENAME_LENGTH);
1010 return false;
1011 }
1012
1013 for (n = 0; n < len; n++) {
1014 c = file->filename[n];
1015
1016 if (n == 0 && c == '.') {
1017 *error = xstrdup("filename cannot start with a dot");
1018 return false;
1019 }
1020
1021 if (n == len - 1 && c == ' ') {
1022 *error = xstrdup("filename cannot end with a space");
1023 return false;
1024 }
1025
1026 if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') ||
1027 (c >= 'a' && c <= 'z') || c == '_' || c == '-' || c == '+' ||
1028 c == '*' || c == ' ' || c == '.' || c == ',')) {
1029 *error = xmalloc(61);
1030 if (*error)
1031 snprintf(*error, 60, "filename cannot contain '%c' "
1032 "character", c);
1033 return false;
1034 }
1035 }
1036
1037 for (n = 0; (o = bile_get_nth_of_type(folder->bile, n,
1038 FOLDER_FILE_RTYPE)); n++) {
1039 if (o->id == file->id) {
1040 xfree(&o);
1041 continue;
1042 }
1043
1044 ret = bile_read_alloc(folder->bile, FOLDER_FILE_RTYPE, o->id, &data);
1045 if (ret == 0 || data == NULL) {
1046 xfree(&o);
1047 return false;
1048 }
1049 ret = bile_unmarshall_object(folder->bile, folder_file_object_fields,
1050 nfolder_file_object_fields, data, o->size, &tfile, sizeof(tfile),
1051 false);
1052 xfree(&data);
1053 xfree(&o);
1054 if (ret == BILE_ERR_NO_MEMORY)
1055 return false;
1056
1057 if (strcasecmp(file->filename, tfile.filename) == 0) {
1058 *error = xstrdup("filename is already taken");
1059 return false;
1060 }
1061 }
1062 if (bile_error(folder->bile) == BILE_ERR_NO_MEMORY)
1063 return false;
1064
1065 return true;
1066 }
1067
1068 bool
1069 folder_edit_file(struct session *s, struct folder *folder,
1070 struct folder_file *file, char *file_path)
1071 {
1072 struct username_cache *uploader;
1073 char *tmp, *errorstr;
1074 unsigned short c;
1075
1076 uploader = user_username(file->uploader_user_id);
1077
1078 session_printf(s, "{{B}}Uploaded By:{{/B}} %s\r\n",
1079 uploader ? uploader->username : "(unknown)");
1080 session_printf(s, "{{B}}Folder:{{/B}} %s\r\n",
1081 folder->name);
1082 session_printf(s, "{{B}}SHA1 Checksum:{{/B}} %s\r\n",
1083 file->sha1_checksum);
1084 session_printf(s, "{{B}}File Size:{{/B}} %ld\r\n",
1085 file->size);
1086 session_flush(s);
1087
1088 file_annotate:
1089 for (;;) {
1090 session_printf(s, "{{B}}File Name:{{/B}} ");
1091 session_flush(s);
1092
1093 if (file->id) {
1094 /* TODO: allow renaming and do FSRename */
1095 session_printf(s, "{{#}}%s\r\n", file->filename);
1096 } else {
1097 tmp = session_field_input(s, FOLDER_FILE_FILENAME_LENGTH,
1098 FOLDER_FILE_FILENAME_LENGTH - 1, file->filename, false, 0);
1099 if (tmp == NULL)
1100 return false;
1101 strlcpy(file->filename, tmp, sizeof(file->filename));
1102 xfree(&tmp);
1103 session_output(s, "\r\n", 2);
1104 session_flush(s);
1105
1106 rtrim(file->filename, "\r\n\t ");
1107
1108 if (!folder_file_valid_filename(s, folder, file, &errorstr)) {
1109 session_printf(s, "{{B}}Error:{{/B}} %s\r\n", errorstr);
1110 xfree(&errorstr);
1111 continue;
1112 }
1113 }
1114
1115 break;
1116 }
1117
1118 for (;;) {
1119 session_printf(s, "{{B}}Short Description:{{/B}} ");
1120 session_flush(s);
1121
1122 tmp = session_field_input(s, FOLDER_FILE_DESCR_LENGTH,
1123 FOLDER_FILE_DESCR_LENGTH - 1, file->description, false, 0);
1124 if (tmp == NULL)
1125 return false;
1126 strlcpy(file->description, tmp, sizeof(file->description));
1127 xfree(&tmp);
1128 session_output(s, "\r\n", 2);
1129 session_flush(s);
1130
1131 rtrim(file->description, "\r\n\t ");
1132
1133 if (file->description[0] == '\0') {
1134 session_printf(s, "{{B}}Error:{{/B}} File "
1135 "description cannot be blank (^C to cancel)\r\n");
1136 session_flush(s);
1137 continue;
1138 }
1139
1140 break;
1141 }
1142
1143 for (;;) {
1144 session_printf(s,
1145 "{{B}}Notes (Optional, ^D when finished):{{/B}}\r\n");
1146 session_flush(s);
1147
1148 tmp = session_field_input(s, 2048, s->terminal_columns - 1,
1149 file->notes, true, 0);
1150 if (file->notes != NULL)
1151 xfree(&file->notes);
1152 file->notes = tmp;
1153 session_output(s, "\r\n", 2);
1154 session_flush(s);
1155 if (file->notes == NULL) {
1156 file->notes_size = 0;
1157 break;
1158 }
1159
1160 rtrim(file->notes, "\r\n\t ");
1161
1162 if (file->notes[0] == '\0') {
1163 xfree(&file->notes);
1164 file->notes = NULL;
1165 file->notes_size = 0;
1166 break;
1167 }
1168 file->notes_size = strlen(file->notes) + 1;
1169 break;
1170 }
1171
1172 for (;;) {
1173 session_printf(s, "\r\n{{B}}(S){{/B}}ave, "
1174 "{{B}}(E){{/B}}dit again, ");
1175 if (file->id && s->user->is_sysop)
1176 session_printf(s, "Re-c{{B}}(h){{/B}}ecksum, ");
1177 session_printf(s, "or {{B}}(C){{/B}}ancel? ");
1178 session_flush(s);
1179
1180 c = session_input_char(s);
1181 if (c == 0 || s->ending)
1182 return false;
1183
1184 switch (c) {
1185 case 'h':
1186 case 'H':
1187 if (s->user->is_sysop) {
1188 session_printf(s, "%c\r\n", c);
1189 session_flush(s);
1190
1191 session_printf(s, "Calculating SHA1 checksum... ");
1192 session_flush(s);
1193
1194 if (!folder_file_checksum(s, file, file_path)) {
1195 session_printf(s, "failed reading %s!\r\n", file_path);
1196 session_flush(s);
1197 return false;
1198 }
1199 session_printf(s, "\r\nNew checksum: %s\r\n",
1200 file->sha1_checksum);
1201 session_flush(s);
1202 }
1203 break;
1204 case 's':
1205 case 'S':
1206 case 'y':
1207 session_printf(s, "%c\r\n", c);
1208 session_flush(s);
1209 /* FALLTHROUGH */
1210 case '\n':
1211 case '\r':
1212 /* save */
1213 session_printf(s, "Saving file... ");
1214 session_flush(s);
1215
1216 if (folder_file_save(folder, file, file_path)) {
1217 session_logf(s, "[%s] Saved file %s", folder->name,
1218 file->filename);
1219 session_printf(s, "done\r\n");
1220 session_flush(s);
1221 return true;
1222 }
1223
1224 session_printf(s, "failed!\r\n");
1225 session_flush(s);
1226 return false;
1227 case 'e':
1228 case 'E':
1229 session_printf(s, "%c\r\n", c);
1230 session_flush(s);
1231 goto file_annotate;
1232 case 'c':
1233 case 'C':
1234 session_printf(s, "%c\r\n", c);
1235 session_flush(s);
1236 return false;
1237 case CONTROL_C:
1238 return false;
1239 }
1240 }
1241
1242 return false;
1243 }
1244
1245 bool
1246 folder_file_checksum(struct session *s, struct folder_file *file,
1247 char *file_path)
1248 {
1249 SHA1_CTX sha1;
1250 FILE *fp;
1251 char *data;
1252 char fmt[12];
1253 size_t rsize, size, dg, olen, tsize;
1254
1255 data = xmalloc(512);
1256 if (data == NULL)
1257 return false;
1258
1259 SHA1Init(&sha1);
1260 fp = fopen(file_path, "rb");
1261 if (fp == NULL) {
1262 xfree(&data);
1263 return false;
1264 }
1265
1266 tsize = file->size;
1267 dg = 1;
1268 while (tsize >= 10) {
1269 dg++;
1270 tsize /= 10;
1271 }
1272 /* 123456 -> "% 6lu/%lu" */
1273 snprintf(fmt, sizeof(fmt), "%% %lulu/%%lu", dg);
1274
1275 rsize = 0;
1276 olen = session_printf(s, fmt, rsize, file->size);
1277 session_flush(s);
1278 while (!feof(fp)) {
1279 size = fread(data, 1, 512, fp);
1280 rsize += size;
1281 SHA1Update(&sha1, (const u_int8_t *)data, size);
1282 if (rsize % 2048 == 0)
1283 uthread_yield();
1284 if (rsize % 10240 == 0 || rsize == file->size) {
1285 session_printf(s, "%s", ansi(s, ANSI_BACK_N, (short)olen,
1286 ANSI_END));
1287 session_printf(s, fmt, rsize, file->size);
1288 session_flush(s);
1289 }
1290 }
1291 fclose(fp);
1292 xfree(&data);
1293
1294 SHA1End(&sha1, (char *)&file->sha1_checksum);
1295
1296 if (rsize != file->size)
1297 return false;
1298
1299 return true;
1300 }