Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / update / client.c
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <afs/stds.h>
13
14 #include <afs/procmgmt.h>
15 #include <roken.h>
16 #include <afs/opr.h>
17
18 #ifdef AFS_NT40_ENV
19 #include <WINNT/afsevent.h>
20 #include <sys/utime.h>
21 #include <direct.h>
22 #endif
23
24 #ifdef AFS_AIX_ENV
25 #include <sys/statfs.h>
26 #endif
27
28 #include <rx/xdr.h>
29 #include <rx/rx.h>
30 #include <rx/rxkad.h>
31 #include <afs/com_err.h>
32 #include <afs/cellconfig.h>
33 #include <afs/afsutil.h>
34 #include <afs/fileutil.h>
35
36 #include "update.h"
37 #include "global.h"
38 #include "update_internal.h"
39
40 char *whoami;
41 static int verbose;
42
43 /* prototypes */
44 static int GetFileFromUpServer(struct rx_connection *conn, char *filename,
45 short uid, short gid, afs_uint32 mode,
46 afs_int32 atime, afs_int32 mtime);
47 static int RenameNewFiles(struct filestr *modFiles);
48 static int PathsAreEquivalent(char *path1, char *path2);
49 int FetchFile(struct rx_call *, char *, char *, int);
50 int IsCompatible(char *, afs_int32, afs_int32);
51 int NotOnHost(char *, struct filestr *);
52 int update_ReceiveFile(int, struct rx_call *, struct stat *);
53
54 afs_int32
55 GetServer(char *aname)
56 {
57 struct hostent *th;
58 afs_int32 addr;
59
60 th = gethostbyname(aname);
61 if (!th) {
62 printf("host %s not found \n", aname);
63 exit(1);
64 }
65 memcpy(&addr, th->h_addr, sizeof(addr));
66 return addr;
67 }
68
69
70 int
71 osi_audit(void)
72 {
73 /* this sucks but it works for now.
74 */
75 return 0;
76 }
77
78 #ifndef AFS_NT40_ENV
79 #include "AFS_component_version_number.c"
80 #endif
81
82 int
83 main(int argc, char **argv)
84 {
85 struct rx_connection *conn;
86 struct rx_call *call;
87 struct afsconf_dir *cdir;
88 afs_int32 scIndex;
89 struct rx_securityClass *sc;
90
91 short uid, gid;
92 afs_uint32 u_uid, u_gid; /*Unsigned long versions of the above */
93 struct stat tstat;
94
95 afs_uint32 mode;
96 int error;
97 char hostname[MAXFNSIZE];
98 FILE *stream;
99 afs_int32 time, length, atime;
100 struct filestr *df;
101 afs_int32 errcode;
102 int retrytime;
103 unsigned int interval;
104 afs_int32 host;
105 int a, cnt;
106 rxkad_level level;
107
108 char dirbuf[MAXFNSIZE], filename[MAXFNSIZE];
109 struct filestr *dirname, *ModFiles, *okhostfiles;
110 #ifdef AFS_AIX32_ENV
111 /*
112 * The following signal action for AIX is necessary so that in case of a
113 * crash (i.e. core is generated) we can include the user's data section
114 * in the core dump. Unfortunately, by default, only a partial core is
115 * generated which, in many cases, isn't too useful.
116 */
117 struct sigaction nsa;
118
119 sigemptyset(&nsa.sa_mask);
120 nsa.sa_handler = SIG_DFL;
121 nsa.sa_flags = SA_FULLDUMP;
122 sigaction(SIGABRT, &nsa, NULL);
123 sigaction(SIGSEGV, &nsa, NULL);
124 #endif
125 whoami = argv[0];
126 #ifdef AFS_NT40_ENV
127 /* dummy signal call to force afsprocmgmt.dll to load on NT */
128 signal(SIGUSR1, SIG_DFL);
129
130 /* initialize winsock */
131 if (afs_winsockInit() < 0) {
132 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
133 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
134 exit(1);
135 }
136 #endif
137
138 /* Initialize dirpaths */
139 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
140 #ifdef AFS_NT40_ENV
141 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
142 #endif
143 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
144 argv[0]);
145 exit(2);
146 }
147 retrytime = 60;
148 dirname = NULL;
149 ModFiles = NULL;
150 okhostfiles = NULL;
151
152 verbose = 0;
153 interval = TIMEOUT;
154 level = rxkad_crypt; /* safest default */
155 strcpy(hostname, "");
156
157 /* Note that IsArg only checks as many bytes as specified in the command line arg,
158 * so that, for instance, -t still matches -time.
159 */
160 for (a = 1; a < argc; a++) {
161 if (argv[a][0] == '-') { /* parse options */
162 int arglen = strlen(argv[a]);
163 char arg[256];
164 lcstring(arg, argv[a], sizeof(arg));
165 #define IsArg(a) (strncmp (arg,a, arglen) == 0)
166 if (IsArg("-time"))
167 interval = atol(argv[++a]);
168 else if (IsArg("-crypt"))
169 level = rxkad_crypt;
170 else if (IsArg("-clear"))
171 level = rxkad_clear;
172 else if (IsArg("-verbose"))
173 verbose++;
174 else {
175 usage:
176 printf
177 ("Usage: upclient <hostname> [-crypt] [-clear] [-t <retry time>] [-verbose]* <dir>+ [-help]\n");
178 exit(1);
179 }
180 } else if (strlen(hostname) == 0) {
181 if (strlcpy(hostname, argv[a], sizeof(hostname))
182 >= sizeof(hostname)) {
183 fprintf(stderr, "Supplied hostname is too long\n");
184 exit(1);
185 }
186 } else {
187 if (strlcpy(filename, argv[a], sizeof(filename))
188 >= sizeof(filename)) {
189 fprintf(stderr, "Supplied filename is too long\n");
190 exit(1);
191 }
192 FilepathNormalize(filename);
193 AddToList(&dirname, filename);
194 }
195 }
196 if (level == -1)
197 goto usage;
198 if (strlen(hostname) == 0)
199 goto usage;
200 host = GetServer(hostname);
201 if (interval < retrytime)
202 retrytime = interval;
203 if (dirname == 0)
204 goto usage;
205
206 errcode = rx_Init(0);
207 if (errcode) {
208 printf("Rx initialize failed \n");
209 afs_com_err(whoami, errcode, "calling Rx init");
210 exit(1);
211 }
212
213 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
214 if (cdir == 0) {
215 fprintf(stderr, "Can't get server configuration info (%s)\n",
216 AFSDIR_SERVER_ETC_DIRPATH);
217 exit(1);
218 }
219
220 if (level == rxkad_crypt)
221 errcode = afsconf_ClientAuthSecure(cdir, &sc, &scIndex);
222 else if (level == rxkad_clear)
223 errcode = afsconf_ClientAuth(cdir, &sc, &scIndex);
224 else {
225 printf("Unsupported security level %d\n", level);
226 exit(1);
227 }
228 if (errcode) {
229 afs_com_err(whoami, errcode, "Couldn't get security obect for localAuth");
230 exit(1);
231 }
232
233 again:
234 conn =
235 rx_NewConnection(host, htons(AFSCONF_UPDATEPORT), UPDATE_SERVICEID,
236 sc, scIndex);
237 cnt = 0;
238 while (1) { /*keep doing it */
239 char c, c1;
240 for (df = dirname; df; df = df->next) { /*for each directory do */
241 char *curDir;
242
243 if (verbose)
244 printf("Checking dir %s\n", df->name);
245 /* initialize lists */
246 ZapList(&ModFiles);
247 ZapList(&okhostfiles);
248
249 /* construct local path from canonical (wire-format) path */
250 if ((errcode = ConstructLocalPath(df->name, "/", &curDir))) {
251 afs_com_err(whoami, errcode, "Unable to construct local path");
252 return errcode;
253 }
254
255 if (stat(curDir, &tstat) < 0) {
256 /* try to make the dir */
257 #ifdef AFS_NT40_ENV
258 if (mkdir(curDir) < 0) {
259 #else
260 if (mkdir(curDir, 0700) < 0) {
261 #endif
262 afs_com_err(whoami, errno, "can't create dir");
263 printf("upclient: Can't update dir %s\n", curDir);
264 return -1;
265 }
266 } else {
267 if ((tstat.st_mode & S_IFMT) != S_IFDIR) {
268 printf(" file %s is not a directory; aborting\n", curDir);
269 return -1;
270 }
271 }
272 call = rx_NewCall(conn);
273
274 /* scratch pad file */
275 sprintf(dirbuf, "%s/upclient.%d", gettmpdir(), getpid());
276
277 errcode = FetchFile(call, df->name, dirbuf, 1); /* get the names and relevant info about all the files in the directory df->name into file dirbuf */
278 error = rx_EndCall(call, 0);
279 if (error && !errcode) {
280 printf("could not end rx call \n");
281 afs_com_err(whoami, error, "calling EndCall");
282 goto fail;
283 }
284 if (errcode) {
285 printf
286 ("warning: could not fetch the contents of directory %s \n",
287 df->name);
288 afs_com_err(whoami, errcode, "calling FetchFile");
289 cnt++;
290 goto fail;
291 }
292
293 stream = fopen(dirbuf, "r");
294 if (stream == NULL) {
295 printf("fopen failed on %s \n", dirbuf);
296 afs_com_err(whoami, errno, "fopen");
297 goto fail;
298 }
299 umask(00);
300
301 /* while there is more info about files in file dirbuf */
302 while (fscanf
303 (stream, "%c%[^\"]%c %u %u %u %u %u %u\n", &c, filename,
304 &c1, &time, &length, &mode, &u_uid, &u_gid,
305 &atime) != EOF) {
306 uid = (short)u_uid;
307 gid = (short)u_gid;
308 AddToList(&okhostfiles, filename);
309 /*record all the file names which exist on the remote
310 * sync site, to enable purging of redundant files */
311 if (verbose >= 3)
312 printf(" checking %s\n", filename);
313 if (!IsCompatible(filename, time, length)) {
314 /* if the file info has changed , record all the
315 *changed files in the ModFiles array*/
316 if (verbose >= 2)
317 printf(" getting %s\n", filename);
318 AddToList(&ModFiles, filename);
319
320 /* now get the file from the server. The received
321 * file is created under the name filename.NEW */
322 errcode =
323 GetFileFromUpServer(conn, filename, uid, gid, mode,
324 atime, time);
325 if (errcode == 1) /* this file failed, but keep trying */
326 goto fail_dirbuf;
327 if (errcode == -1) { /* time to quit */
328 fclose(stream);
329 unlink(dirbuf);
330 return -1;
331 }
332 }
333
334 }
335 fclose(stream);
336 unlink(dirbuf);
337
338 { /*delete all the redundant files on the client */
339 DIR *dirp;
340 struct dirent *dp;
341 char filename[MAXFNSIZE];
342
343 dirp = opendir(curDir);
344 if (dirp == 0) {
345 afs_com_err(whoami, errno, "Can't open local dir %s", curDir);
346 goto fail;
347 }
348
349 while ((dp = readdir(dirp))) {
350 /* for all the files in the directory df->name do */
351 strcpy(filename, curDir);
352 strcat(filename, "/");
353 strcat(filename, dp->d_name);
354 /* if the file filename is redundant, delete it */
355 errcode = NotOnHost(filename, okhostfiles);
356 if (errcode == -1)
357 return -1;
358 if (errcode == 1) {
359 if (verbose >= 2)
360 printf(" flushing %s\n", filename);
361 errcode = unlink(filename);
362 if (errcode) {
363 printf("could not delete file %s \n", filename);
364 afs_com_err(whoami, errno, "could not delete file %s",
365 filename);
366 }
367 }
368 }
369 closedir(dirp);
370 }
371 /* Now, rename the .NEW files created by FetchFile */
372 if (RenameNewFiles(ModFiles))
373 return -1;
374
375 free(curDir);
376 } /* end for each dir loop */
377 /*delete the file with info on files in directory df->name */
378 IOMGR_Sleep(interval);
379 continue;
380
381 fail_dirbuf:
382 fclose(stream);
383 unlink(dirbuf);
384 fail:
385 IOMGR_Sleep(retrytime);
386 if (cnt > 10) {
387 rx_DestroyConnection(conn);
388 goto again;
389 }
390 /* start the cycle again */
391
392 }
393 }
394
395 /* returns 1 if the file is upto date else returns 0*/
396 /*check the dir case more carefully */
397 int
398 IsCompatible(char *filename, afs_int32 time, afs_int32 length)
399 {
400 struct stat status;
401 afs_int32 error;
402 char *localname;
403
404 /* construct a local path from canonical (wire-format) path */
405 if ((error = ConstructLocalPath(filename, "/", &localname))) {
406 afs_com_err(whoami, error, "Unable to construct local path");
407 return error;
408 }
409
410 error = stat(localname, &status);
411
412 free(localname);
413
414 if (error == -1)
415 return 0; /*a non-existent file, has to be fetched fresh */
416 if ((status.st_mode & S_IFMT) == S_IFDIR
417 || ((status.st_mtime == time) && (status.st_size == length)))
418 return (1);
419 else
420 return 0;
421 }
422
423 int
424 FetchFile(struct rx_call *call, char *remoteFile, char *localFile, int dirFlag)
425 {
426 int fd = -1, error = 0;
427 struct stat status;
428
429 if (dirFlag) {
430 if (StartUPDATE_FetchInfo(call, remoteFile))
431 return UPDATE_ERROR;
432 } else {
433 if (StartUPDATE_FetchFile(call, remoteFile))
434 return UPDATE_ERROR;
435 }
436 fd = open(localFile, O_CREAT | O_TRUNC | O_WRONLY, 0666);
437 if (fd < 0) {
438 printf("Could not create %s\n", localFile);
439 afs_com_err(whoami, errno, "Could not create %s", localFile);
440 error = UPDATE_ERROR;
441 return error;
442 }
443 if (fstat(fd, &status) < 0) {
444 afs_com_err(whoami, errno, "Could not stat %s", localFile);
445 close(fd);
446 printf("could not stat %s\n", localFile);
447 return UPDATE_ERROR;
448 }
449 if (update_ReceiveFile(fd, call, &status))
450 error = UPDATE_ERROR;
451
452 close(fd);
453
454 return error;
455 }
456
457
458
459 int
460 update_ReceiveFile(int fd, struct rx_call *call, struct stat *status)
461 {
462 char *buffer = (char *)0;
463 afs_int32 length;
464 int blockSize;
465 afs_int32 error = 0, len;
466 #ifdef AFS_AIX_ENV
467 struct statfs tstatfs;
468 #endif
469
470 len = rx_Read(call, (char *)&length, sizeof(afs_int32));
471 length = ntohl(length);
472 if (len != sizeof(afs_int32))
473 return UPDATE_ERROR;
474 #ifdef AFS_AIX_ENV
475 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
476 fstatfs(fd, &tstatfs);
477 blockSize = tstatfs.f_bsize;
478 #elif AFS_NT40_ENV
479 blockSize = 4096;
480 #else
481 blockSize = status->st_blksize;
482 #endif
483 buffer = malloc(blockSize);
484 if (!buffer) {
485 printf("malloc failed\n");
486 return UPDATE_ERROR;
487 }
488 while (!error && length) {
489 int nbytes = (length > blockSize ? blockSize : length);
490 nbytes = rx_Read(call, buffer, nbytes);
491 if (!nbytes)
492 error = UPDATE_ERROR;
493 if (write(fd, buffer, nbytes) != nbytes) {
494 afs_com_err(whoami, errno, "File system write failed!");
495 printf("File system write failed!\n");
496 error = UPDATE_ERROR;
497 }
498 length -= nbytes;
499 }
500 if (buffer)
501 free(buffer);
502 if (!error)
503 fstat(fd, status);
504 return error;
505 }
506
507
508 /*
509 * PathsAreEquivalent() -- determine if paths are equivalent
510 * Returns 1 if yes, 0 if no, -1 on error.
511 */
512 static int
513 PathsAreEquivalent(char *path1, char *path2)
514 {
515 int areEq = 0, code;
516 char *pathNorm1, *pathNorm2;
517
518 #ifdef AFS_NT40_ENV
519 /* case-insensitive comparison of normalized, same-flavor (short) paths */
520 DWORD status;
521
522 pathNorm1 = malloc(AFSDIR_PATH_MAX);
523 if (pathNorm1 == NULL)
524 return -1;
525 status = GetShortPathName(path1, pathNorm1, AFSDIR_PATH_MAX);
526 if (status == 0 || status > AFSDIR_PATH_MAX) {
527 /* can't convert path to short version; just use long version */
528 free(pathNorm1);
529 pathNorm1 = strdup(path1);
530 if (pathNorm1 == NULL)
531 return -1;
532 }
533 FilepathNormalize(pathNorm1);
534
535 pathNorm2 = malloc(AFSDIR_PATH_MAX);
536 if (pathNorm2 == NULL) {
537 code = -1;
538 goto out;
539 }
540 status = GetShortPathName(path2, pathNorm2, AFSDIR_PATH_MAX);
541 if (status == 0 || status > AFSDIR_PATH_MAX) {
542 /* can't convert path to short version; just use long version */
543 free(pathNorm2);
544 pathNorm2 = strdup(path2);
545 if (pathNorm2 == NULL) {
546 code = -1;
547 goto out;
548 }
549 }
550 FilepathNormalize(pathNorm2);
551
552 if (_stricmp(pathNorm1, pathNorm2) == 0) {
553 areEq = 1;
554 }
555 #else
556 /* case-sensitive comparison of normalized paths */
557 pathNorm1 = strdup(path1);
558 if (pathNorm1 == NULL)
559 return -1;
560 FilepathNormalize(pathNorm1);
561
562 pathNorm2 = strdup(path2);
563 if (pathNorm2 == NULL) {
564 code = -1;
565 goto out;
566 }
567 FilepathNormalize(pathNorm2);
568
569 if (strcmp(pathNorm1, pathNorm2) == 0) {
570 areEq = 1;
571 }
572 #endif /* AFS_NT40_ENV */
573 code = 0;
574 out:
575 free(pathNorm1);
576 free(pathNorm2);
577 return (code != 0) ? code : areEq;
578 }
579
580
581
582 /* returns 1 if filename does not exist on the host site (=> it should be
583 * deleted on client site) else it returns 0 */
584
585 int
586 NotOnHost(char *filename, struct filestr *okhostfiles)
587 {
588 int i, rc;
589 struct stat status;
590 struct filestr *tf;
591 char *hostfile;
592
593 stat(filename, &status);
594
595 if ((status.st_mode & S_IFMT) == S_IFDIR)
596 return 0;
597 i = strlen(filename);
598 if (!strcmp(&filename[i - 4], ".NEW"))
599 return 0;
600
601 for (tf = okhostfiles; tf; tf = tf->next) {
602 /* construct local path from canonical (wire-format) path */
603 if ((rc = ConstructLocalPath(tf->name, "/", &hostfile))) {
604 afs_com_err(whoami, rc, "Unable to construct local path");
605 return -1;
606 }
607 rc = PathsAreEquivalent(hostfile, filename);
608 free(hostfile);
609 if (rc < 0)
610 return -1;
611 if (rc)
612 return 0;
613 }
614 return 1;
615 }
616
617
618 /* RenameNewFiles() - renames all the newly copied files from the
619 * server. Looks for files with .NEW extension and renames them
620 */
621 static int
622 RenameNewFiles(struct filestr *modFiles)
623 {
624 char newname[MAXFNSIZE];
625 char *fname;
626 int errcode = 0;
627 struct filestr *tf;
628
629 for (tf = modFiles; tf; tf = tf->next) {
630 /* construct local path from canonical (wire-format) path */
631 if ((errcode = ConstructLocalPath(tf->name, "/", &fname))) {
632 afs_com_err(whoami, errcode, "Unable to construct local path");
633 return errcode;
634 }
635 strcpy(newname, fname);
636 strcat(newname, ".NEW");
637 if (verbose >= 2)
638 printf(" renaming %s\n", newname);
639 errcode = rk_rename(newname, fname);
640 if (errcode) {
641 printf("could not rename %s to %s\n", newname, fname);
642 afs_com_err(whoami, errno, "could not rename %s to %s", newname,
643 fname);
644 }
645 free(fname);
646 }
647 return errcode;
648 }
649
650
651
652 /* GetFileFromUpServer() - Makes the FetchFile() call and gets the
653 * file from the upserver.
654 * Return Values:
655 * 0 - Alls well
656 * -1 - Serious error. Quit right away.
657 * 1 - Error, but keep trying for the other files
658 *
659 * The file obtained is written to the localized version of the filename.NEW
660 * and the uid, gid, file mode, access and modification times will be set to
661 * the passed in values.
662 */
663 static int
664 GetFileFromUpServer(struct rx_connection *conn, /* handle for upserver */
665 char *filename, /* name of file to be fetched */
666 short uid, short gid, /* uid/gid for fetched file */
667 afs_uint32 mode, /* file mode */
668 afs_int32 atime, afs_int32 mtime)
669 { /* access/modification times */
670 struct rx_call *call;
671 afs_int32 errcode;
672 char *lfile;
673 #ifdef AFS_NT40_ENV
674 struct _utimbuf utbuf;
675 #else
676 struct timeval tvp[2];
677 #endif
678 char newfile[MAXFNSIZE];
679
680 /* construct local path from canonical (wire-format) path */
681 errcode = ConstructLocalPath(filename, "/", &lfile);
682 if (errcode) {
683 afs_com_err(whoami, errcode, "Unable to construct local path");
684 return -1;
685 }
686 strcpy(newfile, lfile);
687 free(lfile);
688
689 strcat(newfile, ".NEW");
690
691 /* fetch filename into newfile from the host, since the current file
692 * is outdated. the new versions of changed files is stored as
693 * oldname.new */
694 call = rx_NewCall(conn);
695 errcode = FetchFile(call, filename, newfile, 0);
696 errcode = rx_EndCall(call, errcode);
697
698 if (errcode) {
699 printf("failed to fetch file %s \n", filename);
700 afs_com_err(whoami, errcode, "fetching file");
701 unlink(newfile);
702 return 1;
703 }
704
705 /* now set the rest of the file status */
706 errcode = chmod(newfile, mode);
707 if (errcode) {
708 printf("could not change protection on %s to %u\n", newfile,
709 (unsigned int)mode);
710 afs_com_err(whoami, errno, "could not change protection on %s to %u",
711 newfile, mode);
712 unlink(newfile);
713 return 1;
714 }
715 #ifdef AFS_NT40_ENV
716 utbuf.actime = atime;
717 utbuf.modtime = mtime;
718 errcode = _utime(newfile, &utbuf);
719 #else
720 errcode = chown(newfile, uid, gid);
721 if (errcode) {
722 printf("warning: could not change uid and gid on %s to %u and %u \n",
723 newfile, gid, uid);
724 afs_com_err(whoami, errno,
725 "warning: could not change uid and gid on %s to %u and %u",
726 newfile, gid, uid);
727 }
728 tvp[0].tv_sec = atime;
729 tvp[0].tv_usec = 0;
730 tvp[1].tv_sec = mtime;
731 tvp[1].tv_usec = 0;
732 errcode = utimes(newfile, tvp);
733 #endif /* NT40 */
734 if (errcode) {
735 printf("could not change access and modify times on %s to %u %u\n",
736 newfile, (unsigned int)atime, (unsigned int)mtime);
737 afs_com_err(whoami, errno,
738 "could not change access and modify times on %s to %u %u",
739 newfile, atime, mtime);
740 unlink(newfile);
741 return 1;
742 }
743
744 return 0;
745 }