-
Notifications
You must be signed in to change notification settings - Fork 1
/
srm.c
2016 lines (1756 loc) · 51.7 KB
/
srm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <netinet/in.h>
#include <netinet/udp.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <glib.h>
#include <stdarg.h>
#include <endian.h>
#include "lansrm.h"
#include "srm.h"
#include "epoll.h"
#include "debug.h"
#include "config.h"
#define MAYBE_NULL(p, member) ((p) ? (p)->member : "")
static struct sockaddr_in bcaddr;
static GList *srmctx_list;
static GTree *open_files;
struct srm_epoll_ctx {
struct fd_ctx *fdctx;
GTree *clients;
void *inbuf;
void *outbuf;
struct sockaddr_in hostaddr;
struct sockaddr_in clientaddr;
ssize_t inlen;
ssize_t outlen;
int fd;
};
static char *srm_strerror(srm_errno_t error)
{
switch(error) {
case SRM_ERRNO_NO_ERROR:
return "No error";
case SRM_ERRNO_SOFTWARE_BUG:
return "Software Bug";
case SRM_ERRNO_BAD_SELECT_CODE:
return "Bad select code";
case SRM_ERRNO_UNALLOCATED_EXTENT:
return "Unallocated extend";
case SRM_ERRNO_DS_ROM_MISSING:
return "DS ROM missing";
case SRM_ERRNO_UNSUPPORTED_DAM:
return "Unsupported DAM";
case SRM_ERRNO_DEVICE_DRIVERS_DONT_MATCH:
return "Device drivers dont match";
case SRM_ERRNO_INVALID_IOS_REQUEST:
return "Invalid IOS request";
case SRM_ERRNO_ATTACH_TABLE_FULL:
return "Attach Table full";
case SRM_ERRNO_IMPROPER_MASS_STORAGE_DEVICE:
return "Improper mass storage device";
case SRM_ERRNO_DIRECTORY_FORMATS_DONT_MATCH:
return "Directory formats don't match";
case SRM_ERRNO_INVALID_FILE_SIZE:
return "Invalid file size";
case SRM_ERRNO_INVALID_FILE_ID:
return "Invalid file ID";
case SRM_ERRNO_VOLUME_RECOVERABLE_ERROR:
return "Volume recoverable error";
case SRM_ERRNO_VOLUME_IO_ERROR:
return "Volume I/O error";
case SRM_ERRNO_FILE_PATHNAME_MISSING:
return "File/Pathname missing";
case SRM_ERRNO_ILLEGAL_BYTE_NUMBER:
return "Illegal byte number";
case SRM_ERRNO_CORRUPT_DIRECTORY:
return "Corrupt directory";
case SRM_ERRNO_SUCCESSFUL_COMPLETION:
return "Successful Completion";
case SRM_ERRNO_SYSTEM_DOWN:
return "System Down";
case SRM_ERRNO_FILE_UNOPENED:
return "File unopened";
case SRM_ERRNO_VOLUME_OFFLINE:
return "Volume offline";
case SRM_ERRNO_VOLUME_LABELS_DONT_MATCH:
return "Volume labels don't match";
case SRM_ERRNO_PASSWORD_NOT_ALLOWED:
return "Password not allowed";
case SRM_ERRNO_ACCESS_TO_FILE_NOT_ALLOWED:
return "Access to file not allowed";
case SRM_ERRNO_UNSUPPORTED_DIRECTORY_OPERATION:
return "Unsupported directory operation";
case SRM_ERRNO_CONFLICTING_SHARE_MODES:
return "Conflicting share modes";
case SRM_ERRNO_BAD_FILE_NAME:
return "Bad file name";
case SRM_ERRNO_FILE_IN_USE:
return "File in use";
case SRM_ERRNO_INSUFFICIENT_DISK_SPACE:
return "Insufficient disk space";
case SRM_ERRNO_DUPLICATE_FILENAMES:
return "Duplicate Filenames";
case SRM_ERRNO_PHYS_EOF_ENCOUNTERED:
return "Physical EOF encountered";
case SRM_ERRNO_NO_CAPABILITY_FOR_FILE:
return "No capability for file";
case SRM_ERRNO_FILE_NOT_FOUND:
return "File not found";
case SRM_ERRNO_VOLUME_IN_USE:
return "Volume in use";
case SRM_ERRNO_FILE_NOT_DIRECTORY:
return "File not directory";
case SRM_ERRNO_DIRECTORY_NOT_EMPTY:
return "Directory not empty";
case SRM_ERRNO_VOLUME_NOT_FOUND:
return "Volume not found";
case SRM_ERRNO_INVALID_PROTECT_CODE:
return "Invalid Protect Code";
case SRM_ERRNO_VOLUME_UNRECOVERABLE_ERROR:
return "Volume unrecoverable error";
case SRM_ERRNO_PASSWORD_NOT_FOUND:
return "Password not found";
case SRM_ERRNO_DUPLICATE_PASSWORDS:
return "Duplicate Passwords";
case SRM_ERRNO_DEADLOCK_DETECTED:
return "Deadlock detected";
case SRM_ERRNO_LINK_TO_DIRECTORY_NOT_ALLOWED:
return "Link to directory not allowed";
case SRM_ERRNO_RENAME_ACROSS_VOLUMES:
return "Rename across volumes";
case SRM_ERRNO_VOLUME_DOWN:
return "Volume down";
case SRM_ERRNO_EOF_ENCOUNTERED:
return "EOF encountered";
case SRM_ERRNO_INVALID_FILE_CODE:
return "Invalid file code";
case SRM_ERRNO_FILE_LOCKED_PLEASE_RETRY:
return "File locked, please retry";
case SRM_ERRNO_NO_REPLY:
return "No reply";
case SRM_ERRNO_PURGE_ON_OPEN:
return "Purge on open";
case SRM_ERRNO_ERROR_TOP:
return "";
}
return "Unknown error";
}
static void srm_log_request(struct srm_client *client, int error, char *fmt, ...)
{
gchar *tmp = g_strdup_printf("%s error=%d (%s)\n", fmt, error, srm_strerror(error));
int level = DBGMSG_REQUEST;
va_list ap;
if (error)
level = DBGMSG_ERROR;
va_start(ap, fmt);
vdbgmsg(level, MAYBE_NULL(client, ipstr), tmp, ap);
va_end(ap);
g_free(tmp);
}
static int srm_open_files_compare(const void *a, const void *b)
{
const struct open_file_entry *filea = a;
const struct open_file_entry *fileb = b;
return strcmp(filea->filename->str, fileb->filename->str);
}
static int path_levels(char *pathname)
{
int level = 0;
gchar **parts = g_strsplit(pathname, "/", 0);
for (int i = 0; parts[i]; i++) {
if (!strcmp(parts[i], "."))
continue;
if (!strcmp(parts[i], ".."))
level--;
else
level++;
}
g_strfreev(parts);
return level;
}
static char *srm_to_c_string(char *s)
{
char *p, *ret = strndup(s, SRM_VOLNAME_LENGTH);
if ((p = strchr(ret, ' ')))
*p = '\0';
return ret;
}
static void c_string_to_srm(char *d, char *s)
{
memset(d, ' ', SRM_VOLNAME_LENGTH);
memcpy(d, s, MIN(SRM_VOLNAME_LENGTH, strlen(s)));
}
static struct srm_volume *get_volume_by_name(struct srm_client *client, char *name)
{
for (GList *p = client->config->volumes; p; p = g_list_next(p)) {
struct srm_volume *volume = p->data;
if (!strcmp(volume->name, name))
return volume;
}
return NULL;
}
static struct srm_volume *get_volume_by_index(struct srm_client *client, int index)
{
if (!index)
index = 8;
for (GList *p = client->config->volumes; p; p = g_list_next(p)) {
struct srm_volume *volume = p->data;
if (volume->index == index)
return volume;
}
return NULL;
}
static int srm_drop_privs(struct srm_client *client, struct srm_volume *volume)
{
if (volume->gid && setresgid(volume->gid, volume->gid, 0) == -1) {
dbgmsg(DBGMSG_ERROR, client->ipstr, "%s: setregid(%d): %m\n",
__func__, volume->gid);
return -1;
}
if (volume->uid && setresuid(volume->uid, volume->uid, 0) == -1) {
dbgmsg(DBGMSG_ERROR, client->ipstr, "%s: setreuid(%d): %m\n",
__func__, volume->uid);
return -1;
}
volume->old_umask = umask(volume->umask);
return 0;
}
static int srm_restore_privs(struct srm_client *client, struct srm_volume *volume)
{
if (volume->uid && setresuid(0, 0, 0) == -1) {
dbgmsg(DBGMSG_ERROR, client->ipstr, "%s: setreuid: %m\n", __func__);
return -1;
}
if (volume->gid && setresgid(0, 0, 0) == -1) {
dbgmsg(DBGMSG_ERROR, client->ipstr, "%s: setregid: %m\n", __func__);
return -1;
}
umask(volume->old_umask);
return 0;
}
static int srm_volume_lstat(struct srm_client *client,
struct srm_volume *volume,
const char *filename,
struct stat *out)
{
int ret;
if (srm_drop_privs(client, volume) == -1)
return -1;
ret = lstat(filename, out);
if (srm_restore_privs(client, volume) == -1)
return -1;
return ret;
}
static int srm_volume_fstat(struct srm_client *client,
struct srm_volume *volume,
int fd, struct stat *out)
{
int ret;
if (srm_drop_privs(client, volume) == -1)
return -1;
ret = fstat(fd, out);
if (srm_restore_privs(client, volume) == -1)
return -1;
return ret;
}
static int srm_volume_mkdir(struct srm_client *client,
struct srm_volume *volume,
const char *filename)
{
int ret;
if (srm_drop_privs(client, volume) == -1)
return -1;
ret = mkdir(filename, 0777);
if (srm_restore_privs(client, volume) == -1)
return -1;
return ret;
}
static int srm_volume_unlink_file(struct srm_client *client,
struct srm_volume *volume,
const char *filename)
{
int ret;
if (srm_drop_privs(client, volume) == -1)
return -1;
ret = unlink(filename);
if (srm_restore_privs(client, volume) == -1)
return -1;
return ret;
}
static int srm_volume_link(struct srm_client *client,
struct srm_volume *volume,
const char *from,
const char *to)
{
int ret;
if (srm_drop_privs(client, volume) == -1)
return -1;
ret = link(from, to);
if (srm_restore_privs(client, volume) == -1)
return -1;
return ret;
}
static int srm_volume_rename(struct srm_client *client,
struct srm_volume *volume,
const char *from,
const char *to)
{
int ret;
if (srm_drop_privs(client, volume) == -1)
return -1;
ret = rename(from, to);
if (srm_restore_privs(client, volume) == -1)
return -1;
return ret;
}
static int srm_volume_open_file(struct srm_client *client,
struct srm_volume *volume,
const char *filename, int flags)
{
int fd;
if (srm_drop_privs(client, volume) == -1)
return -1;
fd = open(filename, flags, 0666);
if (srm_restore_privs(client, volume) == -1)
return -1;
return fd;
}
static int handle_srm_reset(struct srm_client *client)
{
client->cleanup = 1;
return 1;
}
static int handle_srm_areyoualive(void)
{
return 0x01000000;
}
static int errno_to_srm_error(struct srm_client *client)
{
switch(errno) {
case 0:
return 0;
case ENOSPC:
return SRM_ERRNO_INSUFFICIENT_DISK_SPACE;
case EEXIST:
return SRM_ERRNO_DUPLICATE_FILENAMES;
case EXDEV:
return SRM_ERRNO_RENAME_ACROSS_VOLUMES;
case ENOENT:
return SRM_ERRNO_FILE_NOT_FOUND;
case EPERM:
case EACCES:
return SRM_ERRNO_ACCESS_TO_FILE_NOT_ALLOWED;
case EISDIR:
case ENOTDIR:
return SRM_ERRNO_FILE_NOT_FOUND;
case EIO:
return SRM_ERRNO_VOLUME_IO_ERROR;
case EINVAL:
return SRM_ERRNO_VOLUME_IO_ERROR;
default:
dbgmsg(DBGMSG_ERROR, client->ipstr, "%s: unhandled errno %d (%m)\n", __func__, errno);
return SRM_ERRNO_SOFTWARE_BUG;
}
}
static struct open_file_entry *find_file_entry(struct srm_client *client, int fd)
{
return g_tree_lookup(client->files, &fd);
}
static int handle_srm_write(struct srm_client *client,
struct srm_write *request,
struct srm_return_write *response,
int *responselen)
{
uint32_t offset,requested, id, acc;
struct open_file_entry *entry;
ssize_t len = 0;
off_t curpos;
int error = 0;
*responselen = sizeof(struct srm_return_write);
requested = ntohl(request->requested);
id = ntohl(request->file_id);
acc = ntohl(request->access_code);
offset = ntohl(request->offset);
entry = find_file_entry(client, id);
if (!entry) {
error = SRM_ERRNO_FILE_UNOPENED;
goto error;
}
if (acc == 0) {
curpos = lseek(entry->fd, offset + entry->hdr_offset, SEEK_SET);
if (curpos == -1) {
error = errno_to_srm_error(client);
goto error;
}
}
len = write(entry->fd, request->data, requested);
if (len == -1) {
error = errno_to_srm_error(client);
goto error;
}
response->actual = htonl(len);
srm_log_request(client, error, "WRITE id=%08x offset=%x requested=%d written=%zd acc=%d",
id, offset, requested, len, acc);
error:
return error;
}
static int handle_srm_position(struct srm_client *client,
struct srm_position *request)
{
struct open_file_entry *entry;
uint32_t id, offset;
uint8_t whence;
int error = 0;
off_t curpos;
offset = ntohl(request->offset);
whence = request->position_type ? SEEK_CUR : SEEK_SET;
id = ntohl(request->file_id);
entry = find_file_entry(client, id);
if (!entry) {
error = SRM_ERRNO_FILE_UNOPENED;
goto error;
}
if (whence == SEEK_SET)
offset += entry->hdr_offset;
curpos = lseek(entry->fd, offset, whence);
if (curpos == -1) {
error = errno_to_srm_error(client);
goto error;
}
error:
srm_log_request(client, error, "POSITION id=%x offset=%x, whence=%d",
entry ? entry->client_fd : 0, offset, whence);
return error;
}
static int handle_srm_read(struct srm_client *client,
struct srm_read *request,
struct srm_return_read *response,
int *responselen)
{
uint32_t requested, offset, id, acc;
struct open_file_entry *entry;
ssize_t len = 0;
int error = 0;
off_t curpos;
requested = ntohl(request->requested);
offset = ntohl(request->offset);
id = ntohl(request->file_id);
acc = ntohl(request->access_code);
entry = find_file_entry(client, id);
if (!entry) {
error = SRM_ERRNO_FILE_UNOPENED;
goto error;
}
if (acc == 0) {
curpos = lseek(entry->fd, offset + entry->hdr_offset, SEEK_SET);
if (curpos == -1) {
error = errno_to_srm_error(client);
goto error;
}
}
if (requested > 512)
requested = 512;
len = read(entry->fd, response->data, requested);
if (len == -1) {
error = errno_to_srm_error(client);
goto error;
}
if (len > 0) {
response->actual = htonl(len);
if (len != requested)
error = SRM_ERRNO_EOF_ENCOUNTERED;
}
*responselen = offsetof(struct srm_return_read, data) + len;
error:
dbgmsg(DBGMSG_REQUEST, client->ipstr, "READ id=%x file='%s:%s' size=%d "
"actual=%zd offset=%x accesscode=%d, hdr_offset=%zx\n",
id, MAYBE_NULL(entry, volume->name), MAYBE_NULL(entry, filename->str),
requested, len, offset, acc, entry ? entry->hdr_offset : 0);
return error;
}
static int srm_update_file_header(struct srm_client *client,
struct open_file_entry *entry)
{
struct wshfs hdr;
off_t lif_offset;
struct stat statbuf;
uint32_t newsize;
int error = 0;
ssize_t ret;
ret = srm_volume_fstat(client, entry->volume, entry->fd, &statbuf);
if (ret == -1) {
error = errno_to_srm_error(client);
return -1;
}
ret = lseek(entry->fd, 0, SEEK_SET);
if (ret == -1) {
error = errno_to_srm_error(client);
return -1;
}
ret = read(entry->fd, &hdr.hfs, sizeof(hdr.hfs));
if (ret == -1) {
error = errno_to_srm_error(client);
return -1;
}
if (ret != sizeof(hdr.hfs)) {
error = SRM_ERRNO_VOLUME_IO_ERROR;
return -1;
}
lif_offset = be32toh(hdr.hfs.lif_offset) << 8;
ret = lseek(entry->fd, lif_offset, SEEK_SET);
if (ret == -1 || ret != lif_offset) {
error = errno_to_srm_error(client);
return -1;
}
ret = read(entry->fd, &hdr.lif, sizeof(hdr.lif));
if (ret == -1) {
error = errno_to_srm_error(client);
return -1;
}
if (ret != sizeof(hdr.lif)) {
error = SRM_ERRNO_VOLUME_IO_ERROR;
return -1;
}
ret = lseek(entry->fd, lif_offset, SEEK_SET);
if (ret == -1 || ret != lif_offset) {
error = errno_to_srm_error(client);
return -1;
}
newsize = (statbuf.st_size - entry->hdr_offset) / LIF_BLOCK_SIZE;
if (be32toh(hdr.lif.lif.size) == newsize)
return 0;
hdr.lif.lif.size = htobe32(newsize);
ret = write(entry->fd, &hdr.lif, sizeof(hdr.lif));
if (ret == -1) {
error = errno_to_srm_error(client);
return -1;
}
return error;
}
static int handle_srm_set_eof(struct srm_client *client,
struct srm_set_eof *request)
{
struct open_file_entry *entry;
uint32_t id, offset;
uint8_t whence;
int error = 0;
off_t pos = 0;
offset = ntohl(request->offset);
whence = request->position_type ? SEEK_CUR : SEEK_SET;
id = ntohl(request->file_id);
entry = find_file_entry(client, id);
if (!entry) {
error = SRM_ERRNO_FILE_UNOPENED;
goto error;
}
if (whence == SEEK_SET)
offset += entry->hdr_offset;
pos = lseek(entry->fd, offset, whence);
if (pos == -1) {
error = errno_to_srm_error(client);
goto error;
}
if (ftruncate(entry->fd, pos) == -1) {
error = errno_to_srm_error(client);
goto error;
}
srm_update_file_header(client, entry);
error:
srm_log_request(client, error, "SET EOF: relative=%d pos=%08lx",
whence, pos + 1);
return error;
}
static int get_lif_info(int fd, int32_t *out, uint16_t *gp, off_t *hdr_offset, int32_t *bdat_size)
{
struct wshfs hdr;
*hdr_offset = 0;
*bdat_size = INT_MAX;
if (read(fd, &hdr.hfs, sizeof(hdr.hfs)) != sizeof(hdr.hfs))
return -1;
if (be16toh(hdr.hfs.magic_8000) != 0x8000) {
errno = EINVAL;
return -1;
}
if (lseek(fd, be32toh(hdr.hfs.lif_offset) * LIF_BLOCK_SIZE, SEEK_SET) == -1)
return -1;
if (read(fd, &hdr.lif, sizeof(hdr.lif)) != sizeof(hdr.lif))
return -1;
*hdr_offset = be32toh(hdr.lif.lif.loc) * LIF_BLOCK_SIZE;
*out = (int32_t)(int16_t)be16toh(hdr.lif.lif.type);
if (gp) {
gp[0] = hdr.lif.lif.gp[0];
gp[1] = hdr.lif.lif.gp[1];
}
if (bdat_size && *out == HP300_FILETYPE_BDAT) {
if (read(fd, &hdr.bdat, sizeof(hdr.bdat)) != sizeof(hdr.bdat))
return -1;
*bdat_size = be32toh(hdr.bdat.blocks) * 256 + be32toh(hdr.bdat.remainder);
}
return 0;
}
static void unix_to_srm_time(struct srm_date_type *srmtime, time_t *unixtime, int id)
{
struct tm *tm = localtime(unixtime);
srmtime->date = htons(((tm->tm_mon+1) << 12) | (tm->tm_mday << 7) | tm->tm_year);
srmtime->seconds_since_midnight = htonl(tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec);
srmtime->id = htons(id);
}
static off_t srm_file_size(off_t size, off_t hdr_offset)
{
if (size < hdr_offset)
return 0;
return (off_t)(size - hdr_offset);
}
static int srm_map_filetype(mode_t mode)
{
if (S_ISREG(mode))
return SRM_FILETYPE_REG_FILE;
if (S_ISDIR(mode))
return SRM_FILETYPE_DIRECTORY;
if (S_ISFIFO(mode))
return SRM_FILETYPE_PIPEFIFO;
if (S_ISCHR(mode))
return SRM_FILETYPE_CHARDEV;
if (S_ISBLK(mode))
return SRM_FILETYPE_BLOCKDEV;
return SRM_FILETYPE_UNKNOWN;
}
static int get_file_info(struct srm_client *client, struct srm_volume *volume,
char *filename, struct srm_file_info *fi)
{
srm_filetype_t filetype;
int32_t lif_type, bdat_size;
struct stat stbuf;
off_t hdr_offset;
uint16_t gp[2];
char *p;
if (srm_volume_lstat(client, volume, filename, &stbuf) == -1)
return -1;
fi->perm = htons(stbuf.st_mode & 0777);
fi->open_flag = htonl(0);
fi->max_file_size = htonl(INT_MAX);
fi->share_code = -1;
fi->capabilities = -1;
filetype = srm_map_filetype(stbuf.st_mode);
switch (filetype) {
case SRM_FILETYPE_DIRECTORY:
fi->file_code = htonl(HP300_FILETYPE_DIRECTORY);
fi->record_mode = htonl(1);
fi->share_code = htonl(1);
fi->max_record_size = htonl(256);
fi->logical_eof = htonl(1024);
fi->physical_size = htonl(1);
break;
case SRM_FILETYPE_CHARDEV:
fi->file_code = htonl(HP300_FILETYPE_CDEV);
break;
case SRM_FILETYPE_BLOCKDEV:
fi->file_code = htonl(HP300_FILETYPE_BDEV);
break;
case SRM_FILETYPE_PIPEFIFO:
fi->file_code = htonl(HP300_FILETYPE_PIPE);
break;
case SRM_FILETYPE_REMOTE_PROCESS:
case SRM_FILETYPE_UNKNOWN:
fi->file_code = htonl(HP300_FILETYPE_MISC);
break;
case SRM_FILETYPE_REG_FILE:
fi->file_code = htonl(HP300_FILETYPE_UX);
int fd = srm_volume_open_file(client, volume, filename, O_RDONLY);
if (fd == -1)
return -1;
if (get_lif_info(fd, &lif_type, gp, &hdr_offset, &bdat_size) == -1)
break;
close(fd);
fi->file_code = htonl(lif_type);
fi->max_record_size = htonl(LIF_BLOCK_SIZE);
fi->logical_eof = htonl(srm_file_size(stbuf.st_size, hdr_offset));
fi->physical_size = htonl(srm_file_size(stbuf.st_size, hdr_offset));
if (lif_type == HP300_FILETYPE_BDAT) {
fi->max_record_size = gp[1] << 1;
if (!fi->max_record_size)
fi->max_record_size = 1;
if (bdat_size < (int32_t)ntohl(fi->logical_eof))
fi->logical_eof = htonl(bdat_size);
}
break;
}
fi->max_file_size = htonl(INT_MAX);
fi->last_access.id = htons(stbuf.st_gid);
fi->creation_date.id = htons(stbuf.st_uid);
unix_to_srm_time(&fi->last_access, &stbuf.st_mtime, stbuf.st_gid);
unix_to_srm_time(&fi->creation_date, &stbuf.st_ctime, stbuf.st_uid);
if ((p = strrchr(filename, '/')))
p++;
else
p = filename;
c_string_to_srm(fi->filename, p);
return 0;
}
static int handle_srm_fileinfo(struct srm_client *client,
struct srm_fileinfo *request,
struct srm_return_fileinfo *response,
int *responselen)
{
int id = ntohl(request->file_id);
struct open_file_entry *entry;
int error = 0;
entry = find_file_entry(client, id);
if (!entry) {
error = SRM_ERRNO_INVALID_FILE_ID;
goto error;
}
if (get_file_info(client, entry->volume, entry->filename->str, &response->fi) == -1) {
error = errno_to_srm_error(client);
goto error;
}
error:
srm_log_request(client, error, "FILEINFO id=%08x file='%s:%s'",
id, MAYBE_NULL(entry, volume->name), MAYBE_NULL(entry, filename->str));
*responselen = sizeof(struct srm_return_fileinfo);
return error;
}
static int handle_srm_close(struct srm_client *client,
struct srm_close *request)
{
int id = ntohl(request->file_id);
struct open_file_entry *entry;
int nodeallocate, error = 0;
nodeallocate = ntohl(request->nodeallocate);
entry = find_file_entry(client, id);
if (!entry) {
error = SRM_ERRNO_INVALID_FILE_ID;
goto error;
}
if (entry->filetype == SRM_FILETYPE_REG_FILE) {
error = srm_update_file_header(client, entry);
goto error;
}
if (entry->unlink_on_close) {
if (srm_volume_unlink_file(client, entry->volume, entry->filename->str) == -1) {
error = errno_to_srm_error(client);
goto error;
}
}
error:
srm_log_request(client, error, "CLOSE %08x file='%s:%s' nodeallocate %d",
id, MAYBE_NULL(entry, volume->name), MAYBE_NULL(entry, filename->str),
nodeallocate, error);
if (!error) {
g_tree_remove(open_files, entry->filename);
g_tree_remove(client->files, &id);
}
return error;
}
static GString *srm_filename_from_fh(struct srm_file_header *fh,
struct srm_file_name_set *names,
int start)
{
GString *ret = g_string_sized_new(128);
for(unsigned int i = start; i < start + ntohl(fh->file_name_sets); i++) {
g_string_append_c(ret, '/');
char *s = names[i].file_name;
int j = 0;
while(*s != ' ' && *s != '<' && *s != '>' && j++ < 16)
g_string_append_c(ret, *s++);
}
return ret;
}
static struct srm_volume *srm_volume_from_vh(struct srm_client *client,
struct srm_volume_header *vh,
int *error)
{
struct srm_volume *volume;
char *driver, *name, *cat;
int addr, present;
present = ntohl(vh->device_address_present);
addr = ntohl(vh->device_address.address1);
driver = srm_to_c_string(vh->driver_name);
cat = srm_to_c_string(vh->catalogue_organization);
name = srm_to_c_string(vh->volume_name);
if (!present) {
volume = get_volume_by_name(client, name);
if (!volume) {
dbgmsg(DBGMSG_FILE, client->ipstr, "%s: failed to get volume %s\n", __func__, name);
*error = SRM_ERRNO_VOLUME_NOT_FOUND;
goto error;
}
} else {
volume = get_volume_by_index(client, addr);
if (!volume) {
dbgmsg(DBGMSG_FILE, client->ipstr, "%s: failed to get volume %d\n", __func__, addr);
*error = SRM_ERRNO_VOLUME_NOT_FOUND;
goto error;
}
}
error:
free(driver);
free(cat);
free(name);
return volume;
}
#define TEMPFILE_STR "/WORKSTATIONS/TEMP_FILES"
static GString *srm_get_filename(struct srm_client *client,
struct srm_volume *volume,
struct srm_file_header *fh,
struct srm_file_name_set *names,
int start, int *error)
{
struct open_file_entry *entry;
GString *ret, *filename;
int wd = ntohl(fh->working_directory);
char *p;
ret = g_string_sized_new(128);
if (wd > 0) {
entry = g_tree_lookup(client->files, &wd);
if (!entry) {
*error = SRM_ERRNO_INVALID_FILE_ID;
goto error;
}
g_string_append_printf(ret, "/%s", entry->filename->str);
} else {
g_string_append_printf(ret, "/%s", volume->path);
}
filename = srm_filename_from_fh(fh, names, start);
if (filename->len)
g_string_append_printf(ret, "/%s", filename->str);
g_string_free(filename, TRUE);
strip_dup_slashes(ret);
if (path_levels(ret->str) < path_levels(volume->path)) {
dbgmsg(DBGMSG_ERROR, client->ipstr, "request outside of volume: %s\n", ret->str);
*error = SRM_ERRNO_FILE_PATHNAME_MISSING;
goto error;
}
if (client->config->tempdir) {
p = strstr(ret->str, TEMPFILE_STR);
if (p) {
p += sizeof(TEMPFILE_STR)-1;
g_string_printf(ret, "%s/%s", client->config->tempdir, p);
}
}
strip_dup_slashes(ret);
return ret;
error:
g_string_free(ret, TRUE);
return NULL;
}
static int client_insert_file_entry(struct srm_client *client,
struct srm_volume *volume,
GString *filename,
int fd, off_t hdr_offset,
srm_filetype_t filetype)
{
struct open_file_entry *entry = g_new0(struct open_file_entry, 1);
int file_id;
entry->filename = filename;
entry->fd = fd;
entry->hdr_offset = hdr_offset;
entry->volume = volume;
entry->filetype = filetype;
do {
file_id = g_random_int_range(1, INT_MAX);
} while(g_tree_lookup(client->files, &file_id));
entry->client_fd = file_id;
g_tree_insert(client->files, &entry->client_fd, entry);
return file_id;
}
static int handle_srm_open(struct srm_client *client,
struct srm_open *request,
struct srm_return_open *response,
int *responselen)
{
srm_filetype_t filetype, opentype;
int32_t lif_type, bdat_size;
struct srm_volume *volume;
GString *filename = NULL;
struct stat stbuf = { 0 };
int fd = -1, error = 0;
off_t hdr_offset = 0;