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>
18 #include <sys/types.h>
23 #include <WINNT/afsevent.h>
24 #include <sys/utime.h>
25 #include <afs/dirent.h>
29 #include <afs/procmgmt.h>
33 #include <netinet/in.h>
46 #include <afs/com_err.h>
47 #include <afs/cellconfig.h>
48 #include <afs/afsutil.h>
49 #include <afs/fileutil.h>
51 #include <sys/statfs.h>
55 #include "update_internal.h"
61 static int GetFileFromUpServer(struct rx_connection *conn, char *filename,
62 short uid, short gid, afs_uint32 mode,
63 afs_int32 atime, afs_int32 mtime);
64 static int RenameNewFiles(struct filestr *modFiles);
65 static int PathsAreEquivalent(char *path1, char *path2);
66 int FetchFile(struct rx_call *, char *, char *, int);
67 int IsCompatible(char *, afs_int32, afs_int32);
68 int NotOnHost(char *, struct filestr *);
69 int update_ReceiveFile(int, struct rx_call *, struct stat *);
72 GetServer(char *aname)
77 th = gethostbyname(aname);
79 printf("host %s not found \n", aname);
82 memcpy(&addr, th->h_addr, sizeof(addr));
90 /* this sucks but it works for now.
96 #include "AFS_component_version_number.c"
100 main(int argc, char **argv)
102 struct rx_connection *conn;
103 struct rx_call *call;
104 struct afsconf_dir *cdir;
106 struct rx_securityClass *sc;
109 afs_uint32 u_uid, u_gid; /*Unsigned long versions of the above */
114 char hostname[MAXFNSIZE];
116 afs_int32 time, length, atime;
120 unsigned int interval;
125 char dirbuf[MAXFNSIZE], filename[MAXFNSIZE];
126 struct filestr *dirname, *ModFiles, *okhostfiles;
129 * The following signal action for AIX is necessary so that in case of a
130 * crash (i.e. core is generated) we can include the user's data section
131 * in the core dump. Unfortunately, by default, only a partial core is
132 * generated which, in many cases, isn't too useful.
134 struct sigaction nsa;
136 sigemptyset(&nsa.sa_mask);
137 nsa.sa_handler = SIG_DFL;
138 nsa.sa_flags = SA_FULLDUMP;
139 sigaction(SIGABRT, &nsa, NULL);
140 sigaction(SIGSEGV, &nsa, NULL);
144 /* dummy signal call to force afsprocmgmt.dll to load on NT */
145 signal(SIGUSR1, SIG_DFL);
147 /* initialize winsock */
148 if (afs_winsockInit() < 0) {
149 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
150 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
155 /* Initialize dirpaths */
156 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
158 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
160 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
171 level = rxkad_crypt; /* safest default */
172 strcpy(hostname, "");
174 /* Note that IsArg only checks as many bytes as specified in the command line arg,
175 * so that, for instance, -t still matches -time.
177 for (a = 1; a < argc; a++) {
178 if (argv[a][0] == '-') { /* parse options */
179 int arglen = strlen(argv[a]);
181 lcstring(arg, argv[a], sizeof(arg));
182 #define IsArg(a) (strncmp (arg,a, arglen) == 0)
184 interval = atol(argv[++a]);
185 else if (IsArg("-crypt"))
187 else if (IsArg("-clear"))
189 else if (IsArg("-verbose"))
194 ("Usage: upclient <hostname> [-crypt] [-clear] [-t <retry time>] [-verbose]* <dir>+ [-help]\n");
197 } else if (strlen(hostname) == 0)
198 strcpy(hostname, argv[a]);
200 strcpy(filename, argv[a]);
201 FilepathNormalize(filename);
202 AddToList(&dirname, filename);
207 if (strlen(hostname) == 0)
209 host = GetServer(hostname);
210 if (interval < retrytime)
211 retrytime = interval;
215 errcode = rx_Init(0);
217 printf("Rx initialize failed \n");
218 afs_com_err(whoami, errcode, "calling Rx init");
222 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
224 fprintf(stderr, "Can't get server configuration info (%s)\n",
225 AFSDIR_SERVER_ETC_DIRPATH);
229 if (level == rxkad_crypt)
230 errcode = afsconf_ClientAuthSecure(cdir, &sc, &scIndex);
231 else if (level == rxkad_clear)
232 errcode = afsconf_ClientAuth(cdir, &sc, &scIndex);
234 printf("Unsupported security level %d\n", level);
238 afs_com_err(whoami, errcode, "Couldn't get security obect for localAuth");
244 rx_NewConnection(host, htons(AFSCONF_UPDATEPORT), UPDATE_SERVICEID,
247 while (1) { /*keep doing it */
249 for (df = dirname; df; df = df->next) { /*for each directory do */
253 printf("Checking dir %s\n", df->name);
254 /* initialize lists */
256 ZapList(&okhostfiles);
258 /* construct local path from canonical (wire-format) path */
259 if ((errcode = ConstructLocalPath(df->name, "/", &curDir))) {
260 afs_com_err(whoami, errcode, "Unable to construct local path");
264 if (stat(curDir, &tstat) < 0) {
265 /* try to make the dir */
267 if (mkdir(curDir) < 0) {
269 if (mkdir(curDir, 0700) < 0) {
271 afs_com_err(whoami, errno, "can't create dir");
272 printf("upclient: Can't update dir %s\n", curDir);
276 if ((tstat.st_mode & S_IFMT) != S_IFDIR) {
277 printf(" file %s is not a directory; aborting\n", curDir);
281 call = rx_NewCall(conn);
283 /* scratch pad file */
284 sprintf(dirbuf, "%s/upclient.%d", gettmpdir(), getpid());
286 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 */
287 error = rx_EndCall(call, 0);
288 if (error && !errcode) {
289 printf("could not end rx call \n");
290 afs_com_err(whoami, error, "calling EndCall");
295 ("warning: could not fetch the contents of directory %s \n",
297 afs_com_err(whoami, errcode, "calling FetchFile");
302 stream = fopen(dirbuf, "r");
303 if (stream == NULL) {
304 printf("fopen failed on %s \n", dirbuf);
305 afs_com_err(whoami, errno, "fopen");
310 /* while there is more info about files in file dirbuf */
312 (stream, "%c%[^\"]%c %u %u %u %u %u %u\n", &c, filename,
313 &c1, &time, &length, &mode, &u_uid, &u_gid,
317 AddToList(&okhostfiles, filename);
318 /*record all the file names which exist on the remote
319 * sync site, to enable purging of redundant files */
321 printf(" checking %s\n", filename);
322 if (!IsCompatible(filename, time, length)) {
323 /* if the file info has changed , record all the
324 *changed files in the ModFiles array*/
326 printf(" getting %s\n", filename);
327 AddToList(&ModFiles, filename);
329 /* now get the file from the server. The received
330 * file is created under the name filename.NEW */
332 GetFileFromUpServer(conn, filename, uid, gid, mode,
334 if (errcode == 1) /* this file failed, but keep trying */
336 if (errcode == -1) { /* time to quit */
347 { /*delete all the redundant files on the client */
350 char filename[MAXFNSIZE];
352 dirp = opendir(curDir);
354 afs_com_err(whoami, errno, "Can't open local dir %s", curDir);
358 while ((dp = readdir(dirp))) {
359 /* for all the files in the directory df->name do */
360 strcpy(filename, curDir);
361 strcat(filename, "/");
362 strcat(filename, dp->d_name);
363 /* if the file filename is redundant, delete it */
364 errcode = NotOnHost(filename, okhostfiles);
369 printf(" flushing %s\n", filename);
370 errcode = unlink(filename);
372 printf("could not delete file %s \n", filename);
373 afs_com_err(whoami, errno, "could not delete file %s",
380 /* Now, rename the .NEW files created by FetchFile */
381 if (RenameNewFiles(ModFiles))
385 } /* end for each dir loop */
386 /*delete the file with info on files in directory df->name */
387 IOMGR_Sleep(interval);
394 IOMGR_Sleep(retrytime);
396 rx_DestroyConnection(conn);
399 /* start the cycle again */
404 /* returns 1 if the file is upto date else returns 0*/
405 /*check the dir case more carefully */
407 IsCompatible(char *filename, afs_int32 time, afs_int32 length)
413 /* construct a local path from canonical (wire-format) path */
414 if ((error = ConstructLocalPath(filename, "/", &localname))) {
415 afs_com_err(whoami, error, "Unable to construct local path");
419 error = stat(localname, &status);
424 return 0; /*a non-existent file, has to be fetched fresh */
425 if ((status.st_mode & S_IFMT) == S_IFDIR
426 || ((status.st_mtime == time) && (status.st_size == length)))
433 FetchFile(struct rx_call *call, char *remoteFile, char *localFile, int dirFlag)
435 int fd = -1, error = 0;
439 if (StartUPDATE_FetchInfo(call, remoteFile))
442 if (StartUPDATE_FetchFile(call, remoteFile))
445 fd = open(localFile, O_CREAT | O_TRUNC | O_WRONLY, 0666);
447 printf("Could not create %s\n", localFile);
448 afs_com_err(whoami, errno, "Could not create %s", localFile);
449 error = UPDATE_ERROR;
452 if (fstat(fd, &status) < 0) {
453 afs_com_err(whoami, errno, "Could not stat %s", localFile);
455 printf("could not stat %s\n", localFile);
458 if (update_ReceiveFile(fd, call, &status))
459 error = UPDATE_ERROR;
469 update_ReceiveFile(int fd, struct rx_call *call, struct stat *status)
471 char *buffer = (char *)0;
474 afs_int32 error = 0, len;
476 struct statfs tstatfs;
479 len = rx_Read(call, (char *)&length, sizeof(afs_int32));
480 length = ntohl(length);
481 if (len != sizeof(afs_int32))
484 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
485 fstatfs(fd, &tstatfs);
486 blockSize = tstatfs.f_bsize;
490 blockSize = status->st_blksize;
492 buffer = (char *)malloc(blockSize);
494 printf("malloc failed\n");
497 while (!error && length) {
498 int nbytes = (length > blockSize ? blockSize : length);
499 nbytes = rx_Read(call, buffer, nbytes);
501 error = UPDATE_ERROR;
502 if (write(fd, buffer, nbytes) != nbytes) {
503 afs_com_err(whoami, errno, "File system write failed!");
504 printf("File system write failed!\n");
505 error = UPDATE_ERROR;
518 * PathsAreEquivalent() -- determine if paths are equivalent
521 PathsAreEquivalent(char *path1, char *path2)
524 char pathNorm1[AFSDIR_PATH_MAX], pathNorm2[AFSDIR_PATH_MAX];
527 /* case-insensitive comparison of normalized, same-flavor (short) paths */
530 status = GetShortPathName(path1, pathNorm1, AFSDIR_PATH_MAX);
531 if (status == 0 || status > AFSDIR_PATH_MAX) {
532 /* can't convert path to short version; just use long version */
533 strcpy(pathNorm1, path1);
535 FilepathNormalize(pathNorm1);
537 status = GetShortPathName(path2, pathNorm2, AFSDIR_PATH_MAX);
538 if (status == 0 || status > AFSDIR_PATH_MAX) {
539 /* can't convert path to short version; just use long version */
540 strcpy(pathNorm2, path2);
542 FilepathNormalize(pathNorm2);
544 if (_stricmp(pathNorm1, pathNorm2) == 0) {
548 /* case-sensitive comparison of normalized paths */
549 strcpy(pathNorm1, path1);
550 FilepathNormalize(pathNorm1);
552 strcpy(pathNorm2, path2);
553 FilepathNormalize(pathNorm2);
555 if (strcmp(pathNorm1, pathNorm2) == 0) {
558 #endif /* AFS_NT40_ENV */
564 /* returns 1 if filename does not exist on the host site (=> it should be
565 * deleted on client site) else it returns 0 */
568 NotOnHost(char *filename, struct filestr *okhostfiles)
575 stat(filename, &status);
577 if ((status.st_mode & S_IFMT) == S_IFDIR)
579 i = strlen(filename);
580 if (!strcmp(&filename[i - 4], ".NEW"))
583 for (tf = okhostfiles; tf; tf = tf->next) {
584 /* construct local path from canonical (wire-format) path */
585 if ((rc = ConstructLocalPath(tf->name, "/", &hostfile))) {
586 afs_com_err(whoami, rc, "Unable to construct local path");
589 if (PathsAreEquivalent(hostfile, filename)) {
599 /* RenameNewFiles() - renames all the newly copied files from the
600 * server. Looks for files with .NEW extension and renames them
603 RenameNewFiles(struct filestr *modFiles)
605 char newname[MAXFNSIZE];
610 for (tf = modFiles; tf; tf = tf->next) {
611 /* construct local path from canonical (wire-format) path */
612 if ((errcode = ConstructLocalPath(tf->name, "/", &fname))) {
613 afs_com_err(whoami, errcode, "Unable to construct local path");
616 strcpy(newname, fname);
617 strcat(newname, ".NEW");
619 printf(" renaming %s\n", newname);
620 errcode = renamefile(newname, fname);
622 printf("could not rename %s to %s\n", newname, fname);
623 afs_com_err(whoami, errno, "could not rename %s to %s", newname,
633 /* GetFileFromUpServer() - Makes the FetchFile() call and gets the
634 * file from the upserver.
637 * -1 - Serious error. Quit right away.
638 * 1 - Error, but keep trying for the other files
640 * The file obtained is written to the localized version of the filename.NEW
641 * and the uid, gid, file mode, access and modification times will be set to
642 * the passed in values.
645 GetFileFromUpServer(struct rx_connection *conn, /* handle for upserver */
646 char *filename, /* name of file to be fetched */
647 short uid, short gid, /* uid/gid for fetched file */
648 afs_uint32 mode, /* file mode */
649 afs_int32 atime, afs_int32 mtime)
650 { /* access/modification times */
651 struct rx_call *call;
655 struct _utimbuf utbuf;
657 struct timeval tvp[2];
659 char newfile[MAXFNSIZE];
661 /* construct local path from canonical (wire-format) path */
662 errcode = ConstructLocalPath(filename, "/", &lfile);
664 afs_com_err(whoami, errcode, "Unable to construct local path");
667 strcpy(newfile, lfile);
670 strcat(newfile, ".NEW");
672 /* fetch filename into newfile from the host, since the current file
673 * is outdated. the new versions of changed files is stored as
675 call = rx_NewCall(conn);
676 errcode = FetchFile(call, filename, newfile, 0);
677 errcode = rx_EndCall(call, errcode);
680 printf("failed to fetch file %s \n", filename);
681 afs_com_err(whoami, errcode, "fetching file");
686 /* now set the rest of the file status */
687 errcode = chmod(newfile, mode);
689 printf("could not change protection on %s to %u\n", newfile,
691 afs_com_err(whoami, errno, "could not change protection on %s to %u",
697 utbuf.actime = atime;
698 utbuf.modtime = mtime;
699 errcode = _utime(newfile, &utbuf);
701 errcode = chown(newfile, uid, gid);
703 printf("warning: could not change uid and gid on %s to %u and %u \n",
705 afs_com_err(whoami, errno,
706 "warning: could not change uid and gid on %s to %u and %u",
709 tvp[0].tv_sec = atime;
711 tvp[1].tv_sec = mtime;
713 errcode = utimes(newfile, tvp);
716 printf("could not change access and modify times on %s to %u %u\n",
717 newfile, (unsigned int)atime, (unsigned int)mtime);
718 afs_com_err(whoami, errno,
719 "could not change access and modify times on %s to %u %u",
720 newfile, atime, mtime);