-
Notifications
You must be signed in to change notification settings - Fork 37
/
nfsshell.c
2319 lines (2144 loc) · 58 KB
/
nfsshell.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
/*
* Copyright (c) 1990-1998 by Leendert van Doorn <leendert@paramecium.org>
* Copyright (c) 2013 by Michael Brown, Net Direct <michael@netdirect.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Vrije Universiteit, Net Direct nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL LEENDERT VAN DOORN, VRIJE UNIVERSITEIT
* (AMSTERDAM), MICHAEL BROWN, OR NET DIRECT (CANADA) BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Tested on the following systems:
* System V release 4 (386)
* SunOS 4.[123] (SPARC/SUN3)
* DEC Ultrix 4.[23] (DEC Station 5100)
* AIX 4.1
* Linux 2.0.33
* Linux Redhat 5
*/
/*
* nfs - A shell that provides access to NFS file systems
*
* Contributions:
* - Source routing inspired by Casper Dik's code.
* - Linux modifications (and other cleanup) inspired by Marc Heuse
* - Porting to NFSv3 done by Michael Brown
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <setjmp.h>
#include <netdb.h>
#include <errno.h>
#ifdef AIX
#define blkcnt_t long /* hack alert */
#endif
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <rpc/rpc.h>
#include <rpc/key_prot.h>
#include <rpc/pmap_clnt.h>
#ifdef SYSV
#include <rpc/clnt_soc.h>
#endif
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/sysmacros.h>
#include "mount.h"
#include "nfs_prot.h"
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#ifdef READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
/*
* Missing from clnt.h on Linux
*/
#ifndef CLSET_FD_CLOSE
#define CLSET_FD_CLOSE 8 /* close fd while clnt_destroy */
#endif
/*
* Fundamental constants
*/
#define NARGVEC 100 /* maximum number of arguments */
/*
* File modes
*/
#ifndef IFCHR
#define IFCHR 0020000 /* character special */
#define IFBLK 0060000 /* block special */
#define IFSOCK 0140000 /* socket */
#endif /* IFCHR */
/*
* NFS mount options
*/
#define NFS_OVER_UDP 01 /* use NFS over UDP/IP */
#define NFS_OVER_TCP 02 /* use NFS over TCP/IP */
#define TRANSPORT_MASK 03 /* mask out transport */
#define THRU_PORTMAP 010 /* use portmapper proxy call */
#define MOUNT_UMOUNT 020 /* mount followed by umount */
/*
* List of command identifiers
*/
#define CMD_UNKNOWN 0 /* unknown command */
#define CMD_HOST 1 /* host <host> */
#define CMD_UID 2 /* uid [<uid> [<secret-key>]] */
#define CMD_GID 3 /* gid [<gid>] */
#define CMD_CD 4 /* cd [<path>] */
#define CMD_LCD 5 /* lcd [<path>] */
#define CMD_CAT 6 /* cat <filespec> */
#define CMD_LS 7 /* ls [-l] <filespec> */
#define CMD_GET 8 /* get <filespec> */
#define CMD_DF 9 /* df */
#define CMD_MOUNT 10 /* mount [-upTU] <path> */
#define CMD_UMOUNT 11 /* umount */
#define CMD_UMOUNTALL 12 /* umountall */
#define CMD_EXPORT 13 /* export */
#define CMD_DUMP 14 /* dump */
#define CMD_STATUS 15 /* status */
#define CMD_HELP 16 /* help */
#define CMD_QUIT 17 /* quit */
#define CMD_RM 18 /* rm <file> */
#define CMD_LN 19 /* ln <file1> <file2> */
#define CMD_MV 20 /* mv <file1> <file2> */
#define CMD_MKDIR 21 /* mkdir <dir> */
#define CMD_RMDIR 22 /* rmdir <dir> */
#define CMD_CHMOD 23 /* chmod <mode> <file> */
#define CMD_CHOWN 24 /* chown <uid>[.<gid>] <file> */
#define CMD_PUT 25 /* put <local-file> [<remote-file>] */
#define CMD_HANDLE 26 /* handle [<file-handle>] */
#define CMD_MKNOD 27 /* mknod <name> [b/c major minor] [p] */
/*
* Key word table
*/
struct keyword {
char *kw_command;
int kw_value;
char *kw_help;
} keyword[] = {
{ "host", CMD_HOST, "<host> - set remote host name" },
{ "uid", CMD_UID, "[<uid> [<secret-key>]] - set remote user id" },
{ "gid", CMD_GID, "[<gid>] - set remote group id" },
{ "cd", CMD_CD, "[<path>] - change remote working directory" },
{ "lcd", CMD_LCD, "[<path>] - change local working directory" },
{ "cat", CMD_CAT, "<filespec> - display remote file" },
{ "ls", CMD_LS, "[-l] <filespec> - list remote directory" },
{ "get", CMD_GET, "<filespec> - get remote files" },
{ "df", CMD_DF, "- file system information" },
{ "rm", CMD_RM, "<file> - delete remote file" },
{ "ln", CMD_LN, "<file1> <file2> - link file" },
{ "mv", CMD_MV, "<file1> <file2> - move file" },
{ "mkdir", CMD_MKDIR, "<dir> - make remote directory" },
{ "rmdir", CMD_RMDIR, "<dir> - remove remote directory" },
{ "chmod", CMD_CHMOD, "<mode> <file> - change mode" },
{ "chown", CMD_CHOWN, "<uid>[.<gid>] <file> - change owner" },
{ "put", CMD_PUT, "<local-file> [<remote-file>] - put file" },
{ "mount", CMD_MOUNT, "[-upTU] [-P port] <path> - mount file system" },
{ "umount", CMD_UMOUNT, "- umount remote file system" },
{ "umountall",CMD_UMOUNTALL,"- umount all remote file systems" },
{ "export", CMD_EXPORT, "- show all exported file systems" },
{ "dump", CMD_DUMP, "- show all remote mounted file systems" },
{ "status", CMD_STATUS, "- general status report" },
{ "help", CMD_HELP, "- this help message" },
{ "quit", CMD_QUIT, "- its all in the name" },
{ "bye", CMD_QUIT, "- good bye" },
{ "handle", CMD_HANDLE, "[<handle>] - get/set directory file handle" },
{ "mknod", CMD_MKNOD, "<name> [b/c major minor] [p] - make device" }
};
/* run-time settable flags */
int verbose = 1; /* verbosity flag */
int interact = 1; /* interactive mode */
/* user provided credentials */
int authtype = AUTH_UNIX; /* type of authentication */
int uid = -2; /* remote user id (initialy nobody) */
int gid = -2; /* remote group id (initialy nobody) */
keybuf secretkey; /* remote user's secret key */
/* server information (also used as state information) */
char *mountpath = NULL; /* remote mount path */
char *remotehost = NULL; /* remote host name */
struct sockaddr_in server_addr; /* remote server address information */
struct sockaddr_in mntserver_addr; /* remote mount server address */
struct sockaddr_in nfsserver_addr; /* remote nfs server address */
CLIENT *mntclient = NULL; /* mount RPC client */
CLIENT *nfsclient = NULL; /* nfs RPC client */
mountres3 *mountpoint = NULL; /* remote mount point */
nfs_fh3 directory_handle; /* current directory handle */
struct timeval timeout = { 60, 0 }; /* default time out */
int transfersize; /* NFS default transfer size */
/* interrupt environments */
jmp_buf intenv; /* where to go in interrupts */
void interrupt(int);
int command(char *);
int ngetline(char *, int, int *, char **, int);
void do_host(int, char **);
void do_setuid(int, char **);
void do_setgid(int, char **);
void do_cd(int, char **);
void do_lcd(int, char **);
void do_cat(int, char **);
void do_ls(int, char **);
void do_get(int, char **);
void do_df(int, char **);
void do_rm(int, char **);
void do_ln(int, char **);
void do_mv(int, char **);
void do_mkdir(int, char **);
void do_rmdir(int, char **);
void do_chmod(int, char **);
void do_mknod(int, char **);
void do_chown(int, char **);
void do_put(int, char **);
void do_handle(int, char **);
void do_mount(int, char **);
void do_umount(int, char **);
void do_umountall(int, char **);
void do_export(int, char **);
void do_dump(int, char **);
void do_status(int, char **);
void do_help(int, char **);
AUTH *create_authenticator(void);
char *nfs_error(enum nfsstat3);
int open_mount(char *);
void close_mount(void);
int sourceroute(char *, struct sockaddr_in *, int, int);
int open_nfs(char *, int, int);
mountres3 *pmap_mnt(dirpath *, struct sockaddr_in *);
int determine_transfersize(void);
int setup(int , struct sockaddr_in *, int, int);
int privileged(int, struct sockaddr_in *);
void close_nfs(void);
int getdirentries(nfs_fh3 *, char ***, char ***, int);
void printfilestatus(char *file);
int writefiledate(time_t);
int match(char *, int, char **);
int matchpattern(char *, char *);
int amatchpattern(char *, char *);
int umatchpattern(char *, char *);
void*
memcpy(void *dest, const void *src, size_t n);
void*
fhandle3copy(fhandle3 *dest, const fhandle3 *src)
{
dest->fhandle3_len = src->fhandle3_len;
return memcpy(dest->fhandle3_val, src->fhandle3_val, FHSIZE3);
}
void*
fhandle3_to_nfs_fh3(nfs_fh3 *dest, const fhandle3 *src)
{
dest->data.data_len = src->fhandle3_len;
return memcpy(dest->data.data_val, src->fhandle3_val, MIN(NFS3_FHSIZE,FHSIZE3));
}
void*
nfs_fh3copy(nfs_fh3 *dest, const nfs_fh3 *src)
{
dest->data.data_len = src->data.data_len;
return memcpy(dest->data.data_val, src->data.data_val, NFS3_FHSIZE);
}
int
main(int argc, char **argv)
{
int opt, cmd, argcount;
char *argvec[NARGVEC];
char buffer[BUFSIZ];
/* command line option processing */
while ((opt = getopt(argc, argv, "vi")) != EOF) {
switch (opt) {
case 'v':
verbose = 0;
break;
case 'i':
interact = 0;
break;
default:
fprintf(stderr, "Usage: %s [-vi]\n"
"\t-v\tverbose off\n"
"\t-i\tinteractive mode off\n", argv[0]);
exit(1);
}
}
signal(SIGINT, interrupt);
/* interpreter's main command loop */
if (setjmp(intenv)) putchar('\n');
while (ngetline(buffer, BUFSIZ, &argcount, argvec, NARGVEC)) {
if (argcount == 0) continue;
if ((cmd = command(argvec[0])) == CMD_QUIT)
break;
else switch (cmd) {
case CMD_HOST:
do_host(argcount, argvec);
break;
case CMD_UID:
do_setuid(argcount, argvec);
break;
case CMD_GID:
do_setgid(argcount, argvec);
break;
case CMD_CD:
do_cd(argcount, argvec);
break;
case CMD_LCD:
do_lcd(argcount, argvec);
break;
case CMD_CAT:
do_cat(argcount, argvec);
break;
case CMD_LS:
do_ls(argcount, argvec);
break;
case CMD_GET:
do_get(argcount, argvec);
break;
case CMD_DF:
do_df(argcount, argvec);
break;
case CMD_RM:
do_rm(argcount, argvec);
break;
case CMD_LN:
do_ln(argcount, argvec);
break;
case CMD_MV:
do_mv(argcount, argvec);
break;
case CMD_MKDIR:
do_mkdir(argcount, argvec);
break;
case CMD_RMDIR:
do_rmdir(argcount, argvec);
break;
case CMD_CHMOD:
do_chmod(argcount, argvec);
break;
case CMD_CHOWN:
do_chown(argcount, argvec);
break;
case CMD_PUT:
do_put(argcount, argvec);
break;
case CMD_HANDLE:
do_handle(argcount, argvec);
break;
case CMD_MKNOD:
do_mknod(argcount, argvec);
break;
case CMD_MOUNT:
do_mount(argcount, argvec);
break;
case CMD_UMOUNT:
do_umount(argcount, argvec);
break;
case CMD_UMOUNTALL:
do_umountall(argcount, argvec);
break;
case CMD_EXPORT:
do_export(argcount, argvec);
break;
case CMD_DUMP:
do_dump(argcount, argvec);
break;
case CMD_STATUS:
do_status(argcount, argvec);
break;
case CMD_HELP:
do_help(argcount, argvec);
break;
case CMD_UNKNOWN:
if (buffer[0] == '!') {
system(buffer + 1);
printf("!\n");
} else
fprintf(stderr, "%s: unrecognized command\n", argvec[0]);
break;
default:
fprintf(stderr, "internal error: '%s' not is case\n", argvec[0]);
break;
}
}
if (remotehost) close_mount();
exit(0);
}
void
interrupt(int signo)
{
signal(SIGINT, (void (*)(int))interrupt);
longjmp(intenv, 1);
}
/*
* Read a line from standard input and break
* it up into an argument vector.
*/
int
ngetline(char *buf, int bufsize, int *argc, char **argv, int argvsize)
{
register char *p;
#ifdef READLINE
if (interact) {
char *line;
if ((line = readline("nfs> ")) == NULL)
return 0;
strncpy(buf, line, bufsize);
add_history(line);
free(line);
} else {
if (fgets(buf, bufsize, stdin) == NULL)
return 0;
}
#else
if (interact) printf("nfs> ");
if (fgets(buf, bufsize, stdin) == NULL)
return 0;
#endif
*argc = 0;
for (p = buf; *p == ' ' || *p == '\t'; p++)
/* skip white spaces */;
while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') {
if (*argc > argvsize) break;
argv[(*argc)++] = p;
for (; *p != ' ' && *p != '\t' && *p != '\n' && *p != '\0'; p++)
/* skip word */;
if (*p != '\0') *p++ ='\0';
for (; *p == ' ' || *p == '\t'; p++)
/* skip white spaces */;
}
return 1;
}
/*
* Search for command in keyword table
*/
int
command(char *cmd)
{
register int i;
for (i = 0; i < sizeof(keyword)/sizeof(struct keyword); i++)
if (strcmp(keyword[i].kw_command, cmd) == 0)
return keyword[i].kw_value;
return CMD_UNKNOWN;
}
/*
* Set remote host and initialize RPC channel
* to mount daemon.
*/
void
do_host(int argc, char **argv)
{
if (argc != 2)
fprintf(stderr, "Usage: host <host>\n");
else
open_mount(argv[1]);
}
/*
* Set user id (updating RPC authentication info)
*/
void
do_setuid(int argc, char **argv)
{
if (argc > 3) {
fprintf(stderr, "Usage: uid [<uid> [<secret-key>]]\n");
return;
}
if (argc <= 2) {
authtype = AUTH_UNIX;
uid = argc == 1 ? -2 : atoi(argv[1]);
} else if (argc == 3) {
authtype = AUTH_DES;
memcpy(secretkey, argv[2], HEXKEYBYTES);
}
if (nfsclient) {
if (nfsclient->cl_auth)
auth_destroy(nfsclient->cl_auth);
nfsclient->cl_auth = create_authenticator();
}
}
/*
* Set group id (updating RPC authentication info)
*/
void
do_setgid(int argc, char **argv)
{
gid = argc == 2 ? atoi(argv[1]) : -2;
if (nfsclient) {
if (nfsclient->cl_auth)
auth_destroy(nfsclient->cl_auth);
nfsclient->cl_auth = create_authenticator();
}
}
/*
* Change remote working directory
*/
void
do_cd(int argc, char **argv)
{
register char *p;
char *component;
LOOKUP3args args;
LOOKUP3res *res;
nfs_fh3 handle;
if (mountpath == NULL) {
fprintf(stderr, "cd: no remote file system mounted\n");
return;
}
/* easy case: cd to root */
if (argc == 1) {
fhandle3_to_nfs_fh3(&directory_handle, &mountpoint->mountres3_u.mountinfo.fhandle);
return;
}
/* if a directory start with '/', we search from the root */
if (*(p = argv[1]) == '/') {
fhandle3_to_nfs_fh3(&handle, &mountpoint->mountres3_u.mountinfo.fhandle);
p++;
} else
nfs_fh3copy(&handle, &directory_handle);
/*
* Break path up into directory components and check every
* component for its validity.
*/
for (;;) {
if (*p == '\0') break;
for (component = p; *p != '/' && *p != '\0'; p++)
/* do nothing */;
*p++ = '\0';
args.what.name = component;
nfs_fh3copy(&args.what.dir, &handle);
if ((res = nfs3_lookup_3(&args, nfsclient)) == NULL) {
clnt_perror(nfsclient, "nfs3_lookup");
return;
}
if (res->status != NFS3_OK) {
fprintf(stderr, "%s: %s\n", component, nfs_error(res->status));
return;
}
if (res->LOOKUP3res_u.resok.dir_attributes.post_op_attr_u.attributes.type != NF3DIR) {
fprintf(stderr, "%s: is not a directory\n", component);
return;
}
nfs_fh3copy(&handle, &res->LOOKUP3res_u.resok.object);
}
nfs_fh3copy(&directory_handle, &handle);
}
/*
* Change local working directory
*/
void
do_lcd(int argc, char **argv)
{
if (argc == 1) {
char *home = getenv("HOME");
if (home != NULL)
if (chdir(home) != 0) perror("lcd");
} else
if (chdir(argv[1]) != 0) perror("lcd");
}
/*
* Display a remote file
*/
void
do_cat(int argc, char **argv)
{
LOOKUP3args dargs;
LOOKUP3res *dres;
READ3args rargs;
READ3res *rres;
long offset;
if (mountpath == NULL) {
fprintf(stderr, "cat: no remote file system mounted\n");
return;
}
if (argc != 2) {
fprintf(stderr, "Usage: cat <filespec>\n");
return;
}
/* lookup name in current directory */
dargs.what.name = argv[1];
nfs_fh3copy(&dargs.what.dir, &directory_handle);
if ((dres = nfs3_lookup_3(&dargs, nfsclient)) == NULL) {
clnt_perror(nfsclient, "nfs3_lookup");
return;
}
if (dres->status != NFS3_OK) {
fprintf(stderr, "%s: %s\n", argv[1], nfs_error(dres->status));
return;
}
if (dres->LOOKUP3res_u.resok.dir_attributes.post_op_attr_u.attributes.type != NF3REG) {
fprintf(stderr, "%s: is not a regular file\n", argv[1]);
return;
}
nfs_fh3copy(&rargs.file, &dres->LOOKUP3res_u.resok.object);
for (offset = 0; offset < dres->LOOKUP3res_u.resok.dir_attributes.post_op_attr_u.attributes.size; ) {
rargs.offset = offset;
rargs.count = transfersize;
if ((rres = nfs3_read_3(&rargs, nfsclient)) == NULL) {
clnt_perror(nfsclient, "nfs3_read_3");
break;
}
if (rres->status != NFS3_OK) {
fprintf(stderr, "%s: %s\n", argv[1], nfs_error(rres->status));
break;
}
fwrite(rres->READ3res_u.resok.data.data_val,
rres->READ3res_u.resok.data.data_len, 1, stdout);
offset += transfersize;
}
}
/*
* List remote directory
*/
void
do_ls(int argc, char **argv)
{
char **table, **ptr, **p;
int lflag = 0;
argv++; argc--;
if (mountpath == NULL) {
fprintf(stderr, "ls: no remote file system mounted\n");
return;
}
if (argc >= 1 && strcmp(argv[0], "-l") == 0) {
argv++; argc--;
lflag = 1;
}
if (!getdirentries(&directory_handle, &table, &ptr, 20))
return;
for (p = table; p < ptr; p++) {
if (!match(*p, argc, argv)) continue;
if (lflag == 1)
printfilestatus(*p);
else
printf("%s\n", *p);
free(*p);
}
free(table);
}
/*
* Print long listing of a files, much in the way ``ls -l'' does
*/
void
printfilestatus(char *file)
{
LOOKUP3args args;
LOOKUP3res *res;
int mode;
args.what.name = file;
nfs_fh3copy(&args.what.dir, &directory_handle);
if ((res = nfs3_lookup_3(&args, nfsclient)) == NULL) {
clnt_perror(nfsclient, "nfs3_lookup");
return;
}
if (res->status != NFS3_OK) {
fprintf(stderr, "Lookup failed: %s\n", nfs_error(res->status));
return;
}
switch (res->LOOKUP3res_u.resok.dir_attributes.post_op_attr_u.attributes.type) {
case NF3SOCK:
putchar('s');
break;
case NF3FIFO:
putchar('p');
break;
case NF3REG:
putchar('-');
break;
case NF3DIR:
putchar('d');
break;
case NF3BLK:
putchar('b');
break;
case NF3CHR:
putchar('c');
break;
case NF3LNK:
putchar('l');
break;
default:
putchar('?');
break;
}
mode = res->LOOKUP3res_u.resok.dir_attributes.post_op_attr_u.attributes.mode;
if (mode & 0400) putchar('r'); else putchar('-');
if (mode & 0200) putchar('w'); else putchar('-');
if (mode & 0100)
if (mode & 04000) putchar('s'); else putchar('x');
else
if (mode & 04000) putchar('S'); else putchar('-');
if (mode & 040) putchar('r'); else putchar('-');
if (mode & 020) putchar('w'); else putchar('-');
if (mode & 010)
if (mode & 02000) putchar('s'); else putchar('x');
else
if (mode & 02000) putchar('S'); else putchar('-');
if (mode & 04) putchar('r'); else putchar('-');
if (mode & 02) putchar('w'); else putchar('-');
if (mode & 01)
if (mode & 01000) putchar('t'); else putchar('x');
else
if (mode & 01000) putchar('T'); else putchar('-');
printf("%3d%9d%6d%10ld ",
res->LOOKUP3res_u.resok.dir_attributes.post_op_attr_u.attributes.nlink,
res->LOOKUP3res_u.resok.dir_attributes.post_op_attr_u.attributes.uid,
res->LOOKUP3res_u.resok.dir_attributes.post_op_attr_u.attributes.gid,
res->LOOKUP3res_u.resok.dir_attributes.post_op_attr_u.attributes.size);
writefiledate(res->LOOKUP3res_u.resok.dir_attributes.post_op_attr_u.attributes.ctime.seconds);
printf(" %s", file);
if (res->LOOKUP3res_u.resok.dir_attributes.post_op_attr_u.attributes.type == NF3LNK) {
READLINK3res *rlres;
READLINK3args rlargs;
nfs_fh3copy(&rlargs.symlink, &res->LOOKUP3res_u.resok.object);
if ((rlres = nfs3_readlink_3(&rlargs, nfsclient)) == NULL) {
clnt_perror(nfsclient, "nfs3_readlink");
return;
}
if (res->status != NFS3_OK) {
fprintf(stderr, "Lookup failed: %s\n", nfs_error(res->status));
return;
}
printf(" -> %s\n", rlres->READLINK3res_u.resok.data);
} else
putchar('\n');
}
int
writefiledate(time_t d)
{
time_t now, sixmonthsago, onehourfromnow;
char *cp;
(void) time(&now);
sixmonthsago = now - 6L*30L*24L*60L*60L;
onehourfromnow = now + 60L*60L;
cp = ctime(&d);
if ((d < sixmonthsago) || (d > onehourfromnow))
return printf(" %-7.7s %-4.4s ", cp+4, cp+20);
else
return printf(" %-12.12s ", cp+4);
}
/*
* Get remote files
*/
void
do_get(int argc, char **argv)
{
char **table, **ptr, **p;
char answer[512];
LOOKUP3args args;
LOOKUP3res *res;
READ3args rargs;
READ3res *rres;
int iflag = 0;
long offset;
FILE *fp;
argv++; argc--;
if (mountpath == NULL) {
fprintf(stderr, "get: no remote file system mounted\n");
return;
}
if (argc >= 1 && strcmp(argv[0], "-i") == 0) {
argv++; argc--;
iflag = 1;
}
if (!getdirentries(&directory_handle, &table, &ptr, 20))
return;
for (p = table; p < ptr; p++) {
/* match before going over the wire */
if (!match(*p, argc, argv)) continue;
/* only regular files can be transfered */
args.what.name = *p;
nfs_fh3copy(&args.what.dir, &directory_handle);
if ((res = nfs3_lookup_3(&args, nfsclient)) == NULL) {
clnt_perror(nfsclient, "nfs3_lookup");
return;
}
if (res->status != NFS3_OK) {
fprintf(stderr, "Lookup failed: %s\n", nfs_error(res->status));
return;
}
if (res->LOOKUP3res_u.resok.obj_attributes.post_op_attr_u.attributes.type != NF3REG)
continue;
/* ask for confirmation */
printf("%s? ", *p);
if (!iflag) {
if (fgets(answer, sizeof(answer), stdin) == NULL)
continue;
if (answer[0] != 'y' && answer[0] != 'Y')
continue;
} else
printf("Yes\n");
/* get actual file */
if ((fp = fopen(*p, "w")) == NULL) {
fprintf(stderr, "get: cannot create %s\n", *p);
continue;
}
nfs_fh3copy(&rargs.file, &res->LOOKUP3res_u.resok.object);
for (offset = 0; offset < res->LOOKUP3res_u.resok.dir_attributes.post_op_attr_u.attributes.size; ) {
rargs.offset = offset;
rargs.count = transfersize;
if ((rres = nfs3_read_3(&rargs, nfsclient)) == NULL) {
clnt_perror(nfsclient, "nfs3_read");
break;
}
if (rres->status != NFS3_OK) {
fprintf(stderr, "%s: %s\n", argv[1], nfs_error(rres->status));
break;
}
fwrite(rres->READ3res_u.resok.data.data_val,
rres->READ3res_u.resok.data.data_len, 1, fp);
offset += transfersize;
}
fclose(fp);
free(*p);
}
free(table);
}
/*
* Show file system information
*/
/* ARGUSED */
void
do_df(int argc, char **argv)
{
FSSTAT3args args;
FSSTAT3res *res;
if (mountpath == NULL) {
fprintf(stderr, "df: no remote file system mounted\n");
return;
}
if (argc != 1) {
fprintf(stderr, "Usage: df\n");
return;
}
fhandle3_to_nfs_fh3(&args.fsroot, &mountpoint->mountres3_u.mountinfo.fhandle);
if ((res = nfs3_fsstat_3(&args, nfsclient)) == NULL) {
clnt_perror(nfsclient, "nfs3_fsstat");
return;
}
if (res->status != NFS3_OK) {
fprintf(stderr, "Df failed: %s\n", nfs_error(res->status));
return;
}
#define x res->FSSTAT3res_u.resok
printf("%s:%s %ldK, %ldK used, %ldK free (%ldK useable).\n",
remotehost, mountpath,
x.tbytes/1024, (x.tbytes-x.fbytes)/1024,
x.fbytes/1024, x.abytes/1024);
#undef x
}
/*
* Delete a remote file
*/
void
do_rm(int argc, char **argv)
{
REMOVE3args args;
REMOVE3res *res;
if (mountpath == NULL) {
fprintf(stderr, "rm: no remote file system mounted\n");
return;
}
if (argc != 2) {
fprintf(stderr, "Usage: rm <file>\n");
return;
}
args.object.name = argv[1];
nfs_fh3copy(&args.object.dir, &directory_handle);
if ((res = nfs3_remove_3(&args, nfsclient)) == NULL) {
clnt_perror(nfsclient, "nfs3_remove");
return;
}
if (res->status != NFS3_OK) {
fprintf(stderr, "Remove failed: %s\n", nfs_error(res->status));
return;
}
}
/*
* Link a file
*/
void
do_ln(int argc, char **argv)
{
LOOKUP3args dargs;
LOOKUP3res *dres;
LINK3args largs;
LINK3res *lres;
if (mountpath == NULL) {
fprintf(stderr, "ln: no remote file system mounted\n");
return;
}
if (argc != 3) {
fprintf(stderr, "Usage: ln <file1> <file2>\n");
return;
}
dargs.what.name = argv[1];
nfs_fh3copy(&dargs.what.dir, &directory_handle);
if ((dres = nfs3_lookup_3(&dargs, nfsclient)) == NULL) {
clnt_perror(nfsclient, "nfs3_lookup");
return;
}
if (dres->status != NFS3_OK) {
fprintf(stderr, "%s: %s\n", argv[1], nfs_error(dres->status));
return;
}
nfs_fh3copy(&largs.file, &dres->LOOKUP3res_u.resok.object);
largs.link.name = argv[2];
nfs_fh3copy(&largs.link.dir, &directory_handle);
if ((lres = nfs3_link_3(&largs, nfsclient)) == NULL) {
clnt_perror(nfsclient, "nfs3_link");
return;
}
if (lres->status != NFS3_OK) {
fprintf(stderr, "Link failed: %s\n", nfs_error(lres->status));
return;
}
}
/*
* Move a file or directory
*/
void
do_mv(int argc, char **argv)
{
RENAME3args args;
RENAME3res *res;
if (mountpath == NULL) {
fprintf(stderr, "mv: no remote file system mounted\n");
return;
}
if (argc != 3) {
fprintf(stderr, "Usage: mv <file1> <file2>\n");