2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include <afs/param.h>
14 #include <afs/procmgmt.h>
21 #include <sys/types.h>
26 #include <WINNT/afsevent.h>
27 #include <sys/utime.h>
31 #include <afs/procmgmt.h>
35 #include <netinet/in.h>
48 #include <afs/com_err.h>
49 #include <afs/cellconfig.h>
50 #include <afs/afsutil.h>
51 #include <afs/fileutil.h>
53 #include <sys/statfs.h>
57 #include "update_internal.h"
63 static int GetFileFromUpServer(struct rx_connection *conn, char *filename,
64 short uid, short gid, afs_uint32 mode,
65 afs_int32 atime, afs_int32 mtime);
66 static int RenameNewFiles(struct filestr *modFiles);
67 static int PathsAreEquivalent(char *path1, char *path2);
68 int FetchFile(struct rx_call *, char *, char *, int);
69 int IsCompatible(char *, afs_int32, afs_int32);
70 int NotOnHost(char *, struct filestr *);
71 int update_ReceiveFile(int, struct rx_call *, struct stat *);
74 GetServer(char *aname)
79 th = gethostbyname(aname);
81 printf("host %s not found \n", aname);
84 memcpy(&addr, th->h_addr, sizeof(addr));
92 /* this sucks but it works for now.
98 #include "AFS_component_version_number.c"
102 main(int argc, char **argv)
104 struct rx_connection *conn;
105 struct rx_call *call;
106 struct afsconf_dir *cdir;
108 struct rx_securityClass *sc;
111 afs_uint32 u_uid, u_gid; /*Unsigned long versions of the above */
116 char hostname[MAXFNSIZE];
118 afs_int32 time, length, atime;
122 unsigned int interval;
127 char dirbuf[MAXFNSIZE], filename[MAXFNSIZE];
128 struct filestr *dirname, *ModFiles, *okhostfiles;
131 * The following signal action for AIX is necessary so that in case of a
132 * crash (i.e. core is generated) we can include the user's data section
133 * in the core dump. Unfortunately, by default, only a partial core is
134 * generated which, in many cases, isn't too useful.
136 struct sigaction nsa;
138 sigemptyset(&nsa.sa_mask);
139 nsa.sa_handler = SIG_DFL;
140 nsa.sa_flags = SA_FULLDUMP;
141 sigaction(SIGABRT, &nsa, NULL);
142 sigaction(SIGSEGV, &nsa, NULL);
146 /* dummy signal call to force afsprocmgmt.dll to load on NT */
147 signal(SIGUSR1, SIG_DFL);
149 /* initialize winsock */
150 if (afs_winsockInit() < 0) {
151 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
152 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
157 /* Initialize dirpaths */
158 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
160 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
162 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
173 level = rxkad_crypt; /* safest default */
174 strcpy(hostname, "");
176 /* Note that IsArg only checks as many bytes as specified in the command line arg,
177 * so that, for instance, -t still matches -time.
179 for (a = 1; a < argc; a++) {
180 if (argv[a][0] == '-') { /* parse options */
181 int arglen = strlen(argv[a]);
183 lcstring(arg, argv[a], sizeof(arg));
184 #define IsArg(a) (strncmp (arg,a, arglen) == 0)
186 interval = atol(argv[++a]);
187 else if (IsArg("-crypt"))
189 else if (IsArg("-clear"))
191 else if (IsArg("-verbose"))
196 ("Usage: upclient <hostname> [-crypt] [-clear] [-t <retry time>] [-verbose]* <dir>+ [-help]\n");
199 } else if (strlen(hostname) == 0)
200 strcpy(hostname, argv[a]);
202 strcpy(filename, argv[a]);
203 FilepathNormalize(filename);
204 AddToList(&dirname, filename);
209 if (strlen(hostname) == 0)
211 host = GetServer(hostname);
212 if (interval < retrytime)
213 retrytime = interval;
217 errcode = rx_Init(0);
219 printf("Rx initialize failed \n");
220 afs_com_err(whoami, errcode, "calling Rx init");
224 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
226 fprintf(stderr, "Can't get server configuration info (%s)\n",
227 AFSDIR_SERVER_ETC_DIRPATH);
231 if (level == rxkad_crypt)
232 errcode = afsconf_ClientAuthSecure(cdir, &sc, &scIndex);
233 else if (level == rxkad_clear)
234 errcode = afsconf_ClientAuth(cdir, &sc, &scIndex);
236 printf("Unsupported security level %d\n", level);
240 afs_com_err(whoami, errcode, "Couldn't get security obect for localAuth");
246 rx_NewConnection(host, htons(AFSCONF_UPDATEPORT), UPDATE_SERVICEID,
249 while (1) { /*keep doing it */
251 for (df = dirname; df; df = df->next) { /*for each directory do */
255 printf("Checking dir %s\n", df->name);
256 /* initialize lists */
258 ZapList(&okhostfiles);
260 /* construct local path from canonical (wire-format) path */
261 if ((errcode = ConstructLocalPath(df->name, "/", &curDir))) {
262 afs_com_err(whoami, errcode, "Unable to construct local path");
266 if (stat(curDir, &tstat) < 0) {
267 /* try to make the dir */
269 if (mkdir(curDir) < 0) {
271 if (mkdir(curDir, 0700) < 0) {
273 afs_com_err(whoami, errno, "can't create dir");
274 printf("upclient: Can't update dir %s\n", curDir);
278 if ((tstat.st_mode & S_IFMT) != S_IFDIR) {
279 printf(" file %s is not a directory; aborting\n", curDir);
283 call = rx_NewCall(conn);
285 /* scratch pad file */
286 sprintf(dirbuf, "%s/upclient.%d", gettmpdir(), getpid());
288 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 */
289 error = rx_EndCall(call, 0);
290 if (error && !errcode) {
291 printf("could not end rx call \n");
292 afs_com_err(whoami, error, "calling EndCall");
297 ("warning: could not fetch the contents of directory %s \n",
299 afs_com_err(whoami, errcode, "calling FetchFile");
304 stream = fopen(dirbuf, "r");
305 if (stream == NULL) {
306 printf("fopen failed on %s \n", dirbuf);
307 afs_com_err(whoami, errno, "fopen");
312 /* while there is more info about files in file dirbuf */
314 (stream, "%c%[^\"]%c %u %u %u %u %u %u\n", &c, filename,
315 &c1, &time, &length, &mode, &u_uid, &u_gid,
319 AddToList(&okhostfiles, filename);
320 /*record all the file names which exist on the remote
321 * sync site, to enable purging of redundant files */
323 printf(" checking %s\n", filename);
324 if (!IsCompatible(filename, time, length)) {
325 /* if the file info has changed , record all the
326 *changed files in the ModFiles array*/
328 printf(" getting %s\n", filename);
329 AddToList(&ModFiles, filename);
331 /* now get the file from the server. The received
332 * file is created under the name filename.NEW */
334 GetFileFromUpServer(conn, filename, uid, gid, mode,
336 if (errcode == 1) /* this file failed, but keep trying */
338 if (errcode == -1) { /* time to quit */
349 { /*delete all the redundant files on the client */
352 char filename[MAXFNSIZE];
354 dirp = opendir(curDir);
356 afs_com_err(whoami, errno, "Can't open local dir %s", curDir);
360 while ((dp = readdir(dirp))) {
361 /* for all the files in the directory df->name do */
362 strcpy(filename, curDir);
363 strcat(filename, "/");
364 strcat(filename, dp->d_name);
365 /* if the file filename is redundant, delete it */
366 errcode = NotOnHost(filename, okhostfiles);
371 printf(" flushing %s\n", filename);
372 errcode = unlink(filename);
374 printf("could not delete file %s \n", filename);
375 afs_com_err(whoami, errno, "could not delete file %s",
382 /* Now, rename the .NEW files created by FetchFile */
383 if (RenameNewFiles(ModFiles))
387 } /* end for each dir loop */
388 /*delete the file with info on files in directory df->name */
389 IOMGR_Sleep(interval);
396 IOMGR_Sleep(retrytime);
398 rx_DestroyConnection(conn);
401 /* start the cycle again */
406 /* returns 1 if the file is upto date else returns 0*/
407 /*check the dir case more carefully */
409 IsCompatible(char *filename, afs_int32 time, afs_int32 length)
415 /* construct a local path from canonical (wire-format) path */
416 if ((error = ConstructLocalPath(filename, "/", &localname))) {
417 afs_com_err(whoami, error, "Unable to construct local path");
421 error = stat(localname, &status);
426 return 0; /*a non-existent file, has to be fetched fresh */
427 if ((status.st_mode & S_IFMT) == S_IFDIR
428 || ((status.st_mtime == time) && (status.st_size == length)))
435 FetchFile(struct rx_call *call, char *remoteFile, char *localFile, int dirFlag)
437 int fd = -1, error = 0;
441 if (StartUPDATE_FetchInfo(call, remoteFile))
444 if (StartUPDATE_FetchFile(call, remoteFile))
447 fd = open(localFile, O_CREAT | O_TRUNC | O_WRONLY, 0666);
449 printf("Could not create %s\n", localFile);
450 afs_com_err(whoami, errno, "Could not create %s", localFile);
451 error = UPDATE_ERROR;
454 if (fstat(fd, &status) < 0) {
455 afs_com_err(whoami, errno, "Could not stat %s", localFile);
457 printf("could not stat %s\n", localFile);
460 if (update_ReceiveFile(fd, call, &status))
461 error = UPDATE_ERROR;
471 update_ReceiveFile(int fd, struct rx_call *call, struct stat *status)
473 char *buffer = (char *)0;
476 afs_int32 error = 0, len;
478 struct statfs tstatfs;
481 len = rx_Read(call, (char *)&length, sizeof(afs_int32));
482 length = ntohl(length);
483 if (len != sizeof(afs_int32))
486 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
487 fstatfs(fd, &tstatfs);
488 blockSize = tstatfs.f_bsize;
492 blockSize = status->st_blksize;
494 buffer = (char *)malloc(blockSize);
496 printf("malloc failed\n");
499 while (!error && length) {
500 int nbytes = (length > blockSize ? blockSize : length);
501 nbytes = rx_Read(call, buffer, nbytes);
503 error = UPDATE_ERROR;
504 if (write(fd, buffer, nbytes) != nbytes) {
505 afs_com_err(whoami, errno, "File system write failed!");
506 printf("File system write failed!\n");
507 error = UPDATE_ERROR;
520 * PathsAreEquivalent() -- determine if paths are equivalent
523 PathsAreEquivalent(char *path1, char *path2)
526 char pathNorm1[AFSDIR_PATH_MAX], pathNorm2[AFSDIR_PATH_MAX];
529 /* case-insensitive comparison of normalized, same-flavor (short) paths */
532 status = GetShortPathName(path1, pathNorm1, AFSDIR_PATH_MAX);
533 if (status == 0 || status > AFSDIR_PATH_MAX) {
534 /* can't convert path to short version; just use long version */
535 strcpy(pathNorm1, path1);
537 FilepathNormalize(pathNorm1);
539 status = GetShortPathName(path2, pathNorm2, AFSDIR_PATH_MAX);
540 if (status == 0 || status > AFSDIR_PATH_MAX) {
541 /* can't convert path to short version; just use long version */
542 strcpy(pathNorm2, path2);
544 FilepathNormalize(pathNorm2);
546 if (_stricmp(pathNorm1, pathNorm2) == 0) {
550 /* case-sensitive comparison of normalized paths */
551 strcpy(pathNorm1, path1);
552 FilepathNormalize(pathNorm1);
554 strcpy(pathNorm2, path2);
555 FilepathNormalize(pathNorm2);
557 if (strcmp(pathNorm1, pathNorm2) == 0) {
560 #endif /* AFS_NT40_ENV */
566 /* returns 1 if filename does not exist on the host site (=> it should be
567 * deleted on client site) else it returns 0 */
570 NotOnHost(char *filename, struct filestr *okhostfiles)
577 stat(filename, &status);
579 if ((status.st_mode & S_IFMT) == S_IFDIR)
581 i = strlen(filename);
582 if (!strcmp(&filename[i - 4], ".NEW"))
585 for (tf = okhostfiles; tf; tf = tf->next) {
586 /* construct local path from canonical (wire-format) path */
587 if ((rc = ConstructLocalPath(tf->name, "/", &hostfile))) {
588 afs_com_err(whoami, rc, "Unable to construct local path");
591 if (PathsAreEquivalent(hostfile, filename)) {
601 /* RenameNewFiles() - renames all the newly copied files from the
602 * server. Looks for files with .NEW extension and renames them
605 RenameNewFiles(struct filestr *modFiles)
607 char newname[MAXFNSIZE];
612 for (tf = modFiles; tf; tf = tf->next) {
613 /* construct local path from canonical (wire-format) path */
614 if ((errcode = ConstructLocalPath(tf->name, "/", &fname))) {
615 afs_com_err(whoami, errcode, "Unable to construct local path");
618 strcpy(newname, fname);
619 strcat(newname, ".NEW");
621 printf(" renaming %s\n", newname);
622 errcode = renamefile(newname, fname);
624 printf("could not rename %s to %s\n", newname, fname);
625 afs_com_err(whoami, errno, "could not rename %s to %s", newname,
635 /* GetFileFromUpServer() - Makes the FetchFile() call and gets the
636 * file from the upserver.
639 * -1 - Serious error. Quit right away.
640 * 1 - Error, but keep trying for the other files
642 * The file obtained is written to the localized version of the filename.NEW
643 * and the uid, gid, file mode, access and modification times will be set to
644 * the passed in values.
647 GetFileFromUpServer(struct rx_connection *conn, /* handle for upserver */
648 char *filename, /* name of file to be fetched */
649 short uid, short gid, /* uid/gid for fetched file */
650 afs_uint32 mode, /* file mode */
651 afs_int32 atime, afs_int32 mtime)
652 { /* access/modification times */
653 struct rx_call *call;
657 struct _utimbuf utbuf;
659 struct timeval tvp[2];
661 char newfile[MAXFNSIZE];
663 /* construct local path from canonical (wire-format) path */
664 errcode = ConstructLocalPath(filename, "/", &lfile);
666 afs_com_err(whoami, errcode, "Unable to construct local path");
669 strcpy(newfile, lfile);
672 strcat(newfile, ".NEW");
674 /* fetch filename into newfile from the host, since the current file
675 * is outdated. the new versions of changed files is stored as
677 call = rx_NewCall(conn);
678 errcode = FetchFile(call, filename, newfile, 0);
679 errcode = rx_EndCall(call, errcode);
682 printf("failed to fetch file %s \n", filename);
683 afs_com_err(whoami, errcode, "fetching file");
688 /* now set the rest of the file status */
689 errcode = chmod(newfile, mode);
691 printf("could not change protection on %s to %u\n", newfile,
693 afs_com_err(whoami, errno, "could not change protection on %s to %u",
699 utbuf.actime = atime;
700 utbuf.modtime = mtime;
701 errcode = _utime(newfile, &utbuf);
703 errcode = chown(newfile, uid, gid);
705 printf("warning: could not change uid and gid on %s to %u and %u \n",
707 afs_com_err(whoami, errno,
708 "warning: could not change uid and gid on %s to %u and %u",
711 tvp[0].tv_sec = atime;
713 tvp[1].tv_sec = mtime;
715 errcode = utimes(newfile, tvp);
718 printf("could not change access and modify times on %s to %u %u\n",
719 newfile, (unsigned int)atime, (unsigned int)mtime);
720 afs_com_err(whoami, errno,
721 "could not change access and modify times on %s to %u %u",
722 newfile, atime, mtime);