da858d59f9d8f671265a0d8716974694b643f0de
[openafs.git] / src / tests / afscp.c
1 #include <afsconfig.h>
2 #include <afs/param.h>
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <sys/select.h>
14 #include <sys/stat.h>
15 #include <netdb.h>
16
17 #include <afs/afsint.h>
18 #define FSINT_COMMON_XG 1
19 #include <afs/afscbint.h>
20 #include <sys/ioctl.h>
21 #include <afs/venus.h>
22 #include <afs/cellconfig.h>
23 #include <afs/sys_prototypes.h>
24
25 #include <afs/afs_consts.h>
26
27 /*#include <rx/rxkad.h>*/
28 #include <rx/rx_null.h>
29 #include <rx/rx_prototypes.h>
30
31 /*#include <krb.h>*/
32 #include <afs/com_err.h>
33
34 struct VenusFid {
35     afs_int32 Cell;
36     struct AFSFid Fid;
37 };
38
39 int
40 statfile(char *path, char *cellname, afs_uint32 * server, struct AFSFid *f)
41 {
42
43     struct ViceIoctl v;
44     struct VenusFid vf;
45     afs_int32 srvbuf[AFS_MAXHOSTS];
46     int code;
47
48     if (!strncmp(path, "@afs:", 5)) {
49         char *pdup, *p;
50         struct hostent *he;
51
52         pdup = strdup(path);
53         strtok(pdup, ":");
54
55         if (!(p = strtok(NULL, ":"))) {
56             free(pdup);
57             return -1;
58         }
59         strncpy(cellname, p, MAXCELLCHARS);
60
61         if (!(p = strtok(NULL, ":"))) {
62             free(pdup);
63             return -1;
64         }
65         he = gethostbyname(p);
66         if (!he) {
67             printf("Unknown host %s\n", p);
68             free(pdup);
69             return -1;
70         }
71         memcpy(server, he->h_addr, he->h_length);
72
73         if (!(p = strtok(NULL, ":"))) {
74             free(pdup);
75             return -1;
76         }
77         f->Volume = atoi(p);
78
79         if (!(p = strtok(NULL, ":"))) {
80             free(pdup);
81             return -1;
82         }
83         f->Vnode = atoi(p);
84
85         if (!(p = strtok(NULL, ":"))) {
86             free(pdup);
87             return -1;
88         }
89         f->Unique = atoi(p);
90
91         if (strtok(NULL, ":")) {
92             printf("Too much extra stuff after @afs:...\n");
93             free(pdup);
94             return -1;
95         }
96
97         return 0;
98     }
99
100     v.in = 0;
101     v.in_size = 0;
102     v.out = cellname;
103     v.out_size = MAXCELLCHARS;
104     if ((code = pioctl(path, VIOC_FILE_CELL_NAME, &v, 1)))
105         return code;
106
107     v.out = (char *)&vf;
108     v.out_size = sizeof(struct VenusFid);
109     if ((code = pioctl(path, VIOCGETFID, &v, 1)))
110         return code;
111     memcpy(f, &vf.Fid, sizeof(struct AFSFid));
112
113     v.out = (char *)srvbuf;
114     v.out_size = sizeof(srvbuf);
115     if ((code = pioctl(path, VIOCWHEREIS, &v, 1)))
116         return code;
117     if (v.out_size <= sizeof(afs_int32))
118         return EINVAL;
119
120     memcpy(server, srvbuf, sizeof(afs_int32));
121     return 0;
122 }
123
124
125 struct rx_securityClass *sc;
126
127 extern int
128 start_cb_server(void)
129 {
130     struct rx_service *s;
131
132     sc = rxnull_NewServerSecurityObject();
133     s = rx_NewService(0, 1, "afs", &sc, 1, RXAFSCB_ExecuteRequest);
134     if (!s)
135         return 1;
136     rx_StartServer(0);
137     return 0;
138 }
139
140
141 /*extern int rx_socket;*/
142 extern unsigned short rx_port;
143 int
144 do_rx_Init(void)
145 {
146     struct sockaddr_in s;
147     int len;
148
149     if (rx_Init(0)) {
150         fprintf(stderr, "Cannot initialize rx\n");
151         return 1;
152     }
153
154     len = sizeof(struct sockaddr_in);
155     if (getsockname(rx_socket, (struct sockaddr *)&s, (socklen_t *)&len)) {
156         perror("getsockname");
157         return 1;
158     }
159     rx_port = ntohs(s.sin_port);
160
161     return 0;
162 }
163
164 struct rx_securityClass *
165 get_sc(char *cellname)
166 {
167 #if 0
168     char realm[REALM_SZ];
169     CREDENTIALS c;
170 #endif
171
172     return rxnull_NewClientSecurityObject();
173 #if 0
174
175     ucstring(realm, cellname, REALM_SZ);
176
177     if (krb_get_cred("afs", "", realm, &c)) {
178         if (get_ad_tkt("afs", "", realm, DEFAULT_TKT_LIFE)) {
179             return NULL;
180         } else {
181             if (krb_get_cred("afs", "", realm, &c)) {
182                 return NULL;
183             }
184         }
185     }
186
187     return rxkad_NewClientSecurityObject(rxkad_clear, c.session, c.kvno,
188                                          c.ticket_st.length, c.ticket_st.dat);
189 #endif
190 }
191
192 #define scindex_NULL 0
193 #define scindex_RXKAD 2
194
195 #define scindex scindex_RXKAD
196 int
197 main(int argc, char **argv)
198 {
199     char scell[MAXCELLCHARS], dcell[MAXCELLCHARS];
200     afs_uint32 ssrv, dsrv;
201     char *databuffer, *srcf = NULL, *destd = NULL, *destf = NULL, *destpath = NULL;
202     struct stat statbuf;
203
204     struct AFSStoreStatus sst;
205     struct AFSFetchStatus fst, dfst;
206     struct AFSVolSync vs;
207     struct AFSCallBack scb, dcb;
208     struct AFSFid sf, dd, df;
209
210     int filesz = 0;
211     int ch, blksize, bytesremaining, bytes;
212     struct timeval start, finish;
213     struct timezone tz;
214     struct rx_securityClass *ssc = 0, *dsc = 0;
215     int sscindex, dscindex;
216     struct rx_connection *sconn = NULL, *dconn = NULL;
217     struct rx_call *scall = NULL, *dcall = NULL;
218     int code = 0, fetchcode, storecode, printcallerrs = 0;
219     int slcl = 0, dlcl = 0, unlock = 0;
220     int sfd = 0, dfd = 0, unauth = 0;
221
222     struct AFSCBFids theFids;
223     struct AFSCBs theCBs;
224
225
226     blksize = 8 * 1024;
227
228     while ((ch = getopt(argc, argv, "iouUb:")) != -1) {
229         switch (ch) {
230         case 'b':
231             blksize = atoi(optarg);
232             break;
233         case 'i':
234             slcl = 1;
235             break;
236         case 'o':
237             dlcl = 1;
238             break;
239         case 'u':
240             unauth = 1;
241             break;
242         case 'U':
243             unlock = 1;
244             break;
245         default:
246             printf("Unknown option '%c'\n", ch);
247             exit(1);
248         }
249     }
250
251
252     if (argc - optind + unlock < 2) {
253         fprintf(stderr,
254                 "Usage: afscp [-i|-o]] [-b xfersz] [-u] [-U] source [dest]\n");
255         fprintf(stderr, "  -b   Set block size\n");
256         fprintf(stderr, "  -i   Source is local (copy into AFS)\n");
257         fprintf(stderr, "  -o   Dest is local (copy out of AFS)\n");
258         fprintf(stderr, "  -u   Run unauthenticated\n");
259         fprintf(stderr, "  -U   Send an unlock request for source. (dest path not required)\n");
260         fprintf(stderr, "source and dest can be paths or specified as:\n");
261         fprintf(stderr, "     @afs:cellname:servername:volume:vnode:uniq\n");
262         exit(1);
263     }
264     srcf = argv[optind++];
265     if (!unlock) {
266         destpath = argv[optind++];
267         destd = strdup(destpath);
268         if (!destd) {
269             perror("strdup");
270             exit(1);
271         }
272         if ((destf = strrchr(destd, '/'))) {
273             *destf++ = 0;
274         } else {
275             destf = destd;
276             destd = ".";
277         }
278     } else if (slcl) {
279         fprintf(stderr, "-i and -U cannot be used together\n");
280     }
281
282     if (!slcl && statfile(srcf, scell, &ssrv, &sf)) {
283         fprintf(stderr, "Cannot get attributes of %s\n", srcf);
284         exit(1);
285     }
286     if (!unlock && !dlcl && statfile(destd, dcell, &dsrv, &dd)) {
287         fprintf(stderr, "Cannot get attributes of %s\n", destd);
288         exit(1);
289     }
290
291     if ((databuffer = malloc(blksize)) == NULL) {
292         perror("malloc");
293         exit(1);
294     }
295
296     if (do_rx_Init())
297         exit(1);
298
299     if (start_cb_server()) {
300         printf("Cannot start callback service\n");
301         goto Fail_rx;
302     }
303
304     if (!slcl) {
305         sscindex = scindex_RXKAD;
306         if (unauth || (ssc = get_sc(scell)) == NULL) {
307             ssc = rxnull_NewClientSecurityObject();
308             sscindex = scindex_NULL;
309             /*printf("Cannot get authentication for cell %s; running unauthenticated\n", scell); */
310         }
311         sscindex = scindex_NULL;
312
313         if ((sconn =
314              rx_NewConnection(ssrv, htons(AFSCONF_FILEPORT), 1, ssc,
315                               sscindex))
316             == NULL) {
317             struct in_addr s;
318             s.s_addr = ssrv;
319             printf("Cannot initialize rx connection to source server (%s)\n",
320                    inet_ntoa(s));
321             goto Fail_sc;
322         }
323     }
324
325     if (!dlcl && !unlock) {
326         if (!slcl && ssrv == dsrv) {
327             dconn = sconn;
328             dsc = NULL;
329         } else {
330             if (slcl || strcmp(scell, dcell)) {
331                 dscindex = scindex_RXKAD;
332                 if (unauth || (dsc = get_sc(dcell)) == NULL) {
333                     dsc = rxnull_NewClientSecurityObject();
334                     dscindex = scindex_NULL;
335                     /*printf("Cannot get authentication for cell %s; running unauthenticated\n", dcell); */
336                 }
337                 dscindex = scindex_NULL;
338             } else {
339                 dsc = ssc;
340                 dscindex = sscindex;
341             }
342
343             if ((dconn =
344                  rx_NewConnection(dsrv, htons(AFSCONF_FILEPORT), 1, dsc,
345                                   dscindex))
346                 == NULL) {
347                 struct in_addr s;
348                 s.s_addr = dsrv;
349                 printf
350                     ("Cannot initialize rx connection to dest server (%s)\n",
351                      inet_ntoa(s));
352                 goto Fail_sconn;
353             }
354         }
355     }
356
357
358     memset(&sst, 0, sizeof(struct AFSStoreStatus));
359
360     if (dlcl && !unlock) {
361         dfd = open(destpath, O_RDWR | O_CREAT | O_EXCL, 0666);
362         if (dfd < 0 && errno == EEXIST) {
363             printf("%s already exists, overwriting\n", destpath);
364             dfd = open(destpath, O_RDWR | O_TRUNC, 0666);
365             if (dfd < 0) {
366                 fprintf(stderr, "Cannot open %s (%s)\n", destpath,
367                         afs_error_message(errno));
368                 goto Fail_dconn;
369             }
370         } else if (dfd < 0) {
371             fprintf(stderr, "Cannot open %s (%s)\n", destpath,
372                     afs_error_message(errno));
373             goto Fail_dconn;
374         }
375     } else if (!unlock) {
376         if ((code =
377              RXAFS_CreateFile(dconn, &dd, destf, &sst, &df, &fst, &dfst, &dcb,
378                               &vs))) {
379             if (code == EEXIST) {
380                 printf("%s already exits, overwriting\n", destpath);
381                 if (statfile(destpath, dcell, &dsrv, &df))
382                     fprintf(stderr, "Cannot get attributes of %s\n",
383                             destpath);
384                 else
385                     code = 0;
386             } else {
387                 printf("Cannot create %s (%s)\n", destpath,
388                        afs_error_message(code));
389                 if (code)
390                     goto Fail_dconn;
391             }
392         }
393     }
394
395     if (slcl) {
396         sfd = open(srcf, O_RDONLY, 0);
397         if (sfd < 0) {
398             fprintf(stderr, "Cannot open %s (%s)\n", srcf,
399                     afs_error_message(errno));
400             goto Fail_dconn;
401         }
402         if (fstat(sfd, &statbuf) < 0) {
403             fprintf(stderr, "Cannot stat %s (%s)\n", srcf,
404                     afs_error_message(errno));
405             close(sfd);
406             goto Fail_dconn;
407         }
408     } else {
409         if ((code = RXAFS_FetchStatus(sconn, &sf, &fst, &scb, &vs))) {
410             printf("Cannot fetchstatus of %d.%d (%s)\n", sf.Volume, sf.Vnode,
411                    afs_error_message(code));
412             goto Fail_dconn;
413         }
414     }
415
416
417
418     if (slcl) {
419         filesz = statbuf.st_size;
420     } else {
421         filesz = fst.Length;
422     }
423
424     printcallerrs = 0;
425     fetchcode = 0;
426     storecode = 0;
427     if (!slcl && !unlock)
428         scall = rx_NewCall(sconn);
429     if (!dlcl && !unlock)
430         dcall = rx_NewCall(dconn);
431     gettimeofday(&start, &tz);
432     if (unlock) {
433         if (fst.lockCount) {
434             printf("Sending 1 unlock for %s (%d locks)\n", srcf, fst.lockCount);
435             if ((code = RXAFS_ReleaseLock(sconn, &sf, &vs))) {
436                 printf("Unable to unlock %s (%s)\n", srcf,
437                        afs_error_message(code));
438             }
439         } else {
440             printf("No locks for %s\n", srcf);
441         }
442         fetchcode = code;
443         goto Finish;
444     }
445
446     if (!slcl) {
447         if ((code = StartRXAFS_FetchData(scall, &sf, 0, filesz))) {
448             printf("Unable to fetch data from %s (%s)\n", srcf,
449                    afs_error_message(code));
450             goto Fail_call;
451         }
452     }
453
454     if (!dlcl) {
455         if (slcl) {
456             sst.Mask = AFS_SETMODTIME | AFS_SETMODE;
457             sst.ClientModTime = statbuf.st_mtime;
458             sst.UnixModeBits =
459                 statbuf.st_mode & ~(S_IFMT | S_ISUID | S_ISGID);
460         } else {
461             sst.Mask = AFS_SETMODTIME | AFS_SETMODE;
462             sst.ClientModTime = fst.ClientModTime;
463             sst.UnixModeBits =
464                 fst.UnixModeBits & ~(S_IFMT | S_ISUID | S_ISGID);
465         }
466
467         if ((code =
468              StartRXAFS_StoreData(dcall, &df, &sst, 0, filesz, filesz))) {
469             printf("Unable to store data to %s (%s)\n", destpath,
470                    afs_error_message(code));
471             goto Fail_call;
472         }
473     }
474
475     if (slcl) {
476         bytesremaining = statbuf.st_size;
477     } else {
478         rx_Read(scall, (char *)&bytesremaining, sizeof(afs_int32));
479         bytesremaining = ntohl(bytesremaining);
480     }
481
482     while (bytesremaining > 0) {
483         /*printf("%d bytes remaining\n",bytesremaining); */
484         if (slcl) {
485             if ((bytes =
486                  read(sfd, databuffer, min(blksize, bytesremaining))) <= 0) {
487                 fetchcode = errno;
488                 break;
489             }
490         } else {
491             if ((bytes =
492                  rx_Read(scall, databuffer,
493                          min(blksize, bytesremaining))) <= 0)
494                 break;
495         }
496         if (dlcl) {
497             if (write(dfd, databuffer, bytes) != bytes) {
498                 storecode = errno;
499                 break;
500             }
501         } else {
502             if (rx_Write(dcall, databuffer, bytes) != bytes)
503                 break;
504         }
505         bytesremaining -= bytes;
506         /*printf("%d bytes copied\n",bytes); */
507     }
508
509
510     if (bytesremaining > 0) {
511         printf("Some network error occured while copying data\n");
512         goto Fail_call;
513     }
514
515     if (!slcl)
516         fetchcode = EndRXAFS_FetchData(scall, &fst, &scb, &vs);
517     if (!dlcl)
518         storecode = EndRXAFS_StoreData(dcall, &fst, &vs);
519     printcallerrs = 1;
520   Fail_call:
521
522     if (slcl) {
523         if (close(sfd) && !fetchcode)
524             fetchcode = errno;
525     } else {
526         fetchcode = rx_EndCall(scall, fetchcode);
527     }
528     if (fetchcode && printcallerrs)
529         printf("Error returned from fetch: %s\n", afs_error_message(fetchcode));
530
531     if (dlcl) {
532         if (close(dfd) && !storecode)
533             storecode = errno;
534     } else if (!unlock) {
535         storecode = rx_EndCall(dcall, storecode);
536     }
537     if (storecode && printcallerrs)
538         printf("Error returned from store: %s\n", afs_error_message(storecode));
539 Finish:
540     gettimeofday(&finish, &tz);
541
542     if (!slcl) {
543         theFids.AFSCBFids_len = 1;
544         theFids.AFSCBFids_val = &sf;
545         theCBs.AFSCBs_len = 1;
546         theCBs.AFSCBs_val = &scb;
547         scb.CallBackType = CB_DROPPED;
548         if ((code = RXAFS_GiveUpCallBacks(sconn, &theFids, &theCBs)))
549             printf("Could not give up source callback: %s\n",
550                    afs_error_message(code));
551     }
552
553     if (!dlcl) {
554         theFids.AFSCBFids_len = 1;
555         theFids.AFSCBFids_val = &df;
556         theCBs.AFSCBs_len = 1;
557         theCBs.AFSCBs_val = &dcb;
558         dcb.CallBackType = CB_DROPPED;
559         if ((code = RXAFS_GiveUpCallBacks(dconn, &theFids, &theCBs)))
560             printf("Could not give up target callback: %s\n",
561                    afs_error_message(code));
562     }
563
564     if (code == 0)
565         code = storecode;
566     if (code == 0)
567         code = fetchcode;
568
569   Fail_dconn:
570     if (!dlcl && !unlock && (slcl || dconn != sconn))
571         rx_DestroyConnection(dconn);
572   Fail_sconn:
573     if (!slcl)
574         rx_DestroyConnection(sconn);
575   Fail_sc:
576     if (dsc && dsc != ssc)
577         RXS_Close(dsc);
578     if (ssc)
579         RXS_Close(ssc);
580   Fail_rx:
581     rx_Finalize();
582
583     free(databuffer);
584     if (printcallerrs && !unlock) {
585         double rate, size, time;
586         if (finish.tv_sec == start.tv_sec) {
587             printf("Copied %d bytes in %d microseconds\n", filesz,
588                    finish.tv_usec - start.tv_usec);
589         } else {
590             printf("Copied %d bytes in %d seconds\n", filesz,
591                    (int)(finish.tv_sec - start.tv_sec));
592         }
593
594         size = filesz / 1024.0;
595         time =
596             finish.tv_sec - start.tv_sec + (finish.tv_usec -
597                                             start.tv_usec) / 1e+06;
598         rate = size / time;
599         printf("Transfer rate %g Kbytes/sec\n", rate);
600
601     }
602
603     exit(code != 0);
604 }