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>
19 #include <WINNT/afsevent.h>
20 #include <sys/utime.h>
25 #include <sys/statfs.h>
31 #include <afs/com_err.h>
32 #include <afs/cellconfig.h>
33 #include <afs/afsutil.h>
34 #include <afs/fileutil.h>
38 #include "update_internal.h"
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 *);
55 GetServer(char *aname)
60 th = gethostbyname(aname);
62 printf("host %s not found \n", aname);
65 memcpy(&addr, th->h_addr, sizeof(addr));
73 /* this sucks but it works for now.
79 #include "AFS_component_version_number.c"
83 main(int argc, char **argv)
85 struct rx_connection *conn;
87 struct afsconf_dir *cdir;
89 struct rx_securityClass *sc;
92 afs_uint32 u_uid, u_gid; /*Unsigned long versions of the above */
97 char hostname[MAXFNSIZE];
99 afs_int32 time, length, atime;
103 unsigned int interval;
108 char dirbuf[MAXFNSIZE], filename[MAXFNSIZE];
109 struct filestr *dirname, *ModFiles, *okhostfiles;
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.
117 struct sigaction nsa;
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);
127 /* dummy signal call to force afsprocmgmt.dll to load on NT */
128 signal(SIGUSR1, SIG_DFL);
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);
138 /* Initialize dirpaths */
139 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
141 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
143 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
154 level = rxkad_crypt; /* safest default */
155 strcpy(hostname, "");
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.
160 for (a = 1; a < argc; a++) {
161 if (argv[a][0] == '-') { /* parse options */
162 int arglen = strlen(argv[a]);
164 lcstring(arg, argv[a], sizeof(arg));
165 #define IsArg(a) (strncmp (arg,a, arglen) == 0)
167 interval = atol(argv[++a]);
168 else if (IsArg("-crypt"))
170 else if (IsArg("-clear"))
172 else if (IsArg("-verbose"))
177 ("Usage: upclient <hostname> [-crypt] [-clear] [-t <retry time>] [-verbose]* <dir>+ [-help]\n");
180 } else if (strlen(hostname) == 0)
181 strcpy(hostname, argv[a]);
183 strcpy(filename, argv[a]);
184 FilepathNormalize(filename);
185 AddToList(&dirname, filename);
190 if (strlen(hostname) == 0)
192 host = GetServer(hostname);
193 if (interval < retrytime)
194 retrytime = interval;
198 errcode = rx_Init(0);
200 printf("Rx initialize failed \n");
201 afs_com_err(whoami, errcode, "calling Rx init");
205 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
207 fprintf(stderr, "Can't get server configuration info (%s)\n",
208 AFSDIR_SERVER_ETC_DIRPATH);
212 if (level == rxkad_crypt)
213 errcode = afsconf_ClientAuthSecure(cdir, &sc, &scIndex);
214 else if (level == rxkad_clear)
215 errcode = afsconf_ClientAuth(cdir, &sc, &scIndex);
217 printf("Unsupported security level %d\n", level);
221 afs_com_err(whoami, errcode, "Couldn't get security obect for localAuth");
227 rx_NewConnection(host, htons(AFSCONF_UPDATEPORT), UPDATE_SERVICEID,
230 while (1) { /*keep doing it */
232 for (df = dirname; df; df = df->next) { /*for each directory do */
236 printf("Checking dir %s\n", df->name);
237 /* initialize lists */
239 ZapList(&okhostfiles);
241 /* construct local path from canonical (wire-format) path */
242 if ((errcode = ConstructLocalPath(df->name, "/", &curDir))) {
243 afs_com_err(whoami, errcode, "Unable to construct local path");
247 if (stat(curDir, &tstat) < 0) {
248 /* try to make the dir */
250 if (mkdir(curDir) < 0) {
252 if (mkdir(curDir, 0700) < 0) {
254 afs_com_err(whoami, errno, "can't create dir");
255 printf("upclient: Can't update dir %s\n", curDir);
259 if ((tstat.st_mode & S_IFMT) != S_IFDIR) {
260 printf(" file %s is not a directory; aborting\n", curDir);
264 call = rx_NewCall(conn);
266 /* scratch pad file */
267 sprintf(dirbuf, "%s/upclient.%d", gettmpdir(), getpid());
269 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 */
270 error = rx_EndCall(call, 0);
271 if (error && !errcode) {
272 printf("could not end rx call \n");
273 afs_com_err(whoami, error, "calling EndCall");
278 ("warning: could not fetch the contents of directory %s \n",
280 afs_com_err(whoami, errcode, "calling FetchFile");
285 stream = fopen(dirbuf, "r");
286 if (stream == NULL) {
287 printf("fopen failed on %s \n", dirbuf);
288 afs_com_err(whoami, errno, "fopen");
293 /* while there is more info about files in file dirbuf */
295 (stream, "%c%[^\"]%c %u %u %u %u %u %u\n", &c, filename,
296 &c1, &time, &length, &mode, &u_uid, &u_gid,
300 AddToList(&okhostfiles, filename);
301 /*record all the file names which exist on the remote
302 * sync site, to enable purging of redundant files */
304 printf(" checking %s\n", filename);
305 if (!IsCompatible(filename, time, length)) {
306 /* if the file info has changed , record all the
307 *changed files in the ModFiles array*/
309 printf(" getting %s\n", filename);
310 AddToList(&ModFiles, filename);
312 /* now get the file from the server. The received
313 * file is created under the name filename.NEW */
315 GetFileFromUpServer(conn, filename, uid, gid, mode,
317 if (errcode == 1) /* this file failed, but keep trying */
319 if (errcode == -1) { /* time to quit */
330 { /*delete all the redundant files on the client */
333 char filename[MAXFNSIZE];
335 dirp = opendir(curDir);
337 afs_com_err(whoami, errno, "Can't open local dir %s", curDir);
341 while ((dp = readdir(dirp))) {
342 /* for all the files in the directory df->name do */
343 strcpy(filename, curDir);
344 strcat(filename, "/");
345 strcat(filename, dp->d_name);
346 /* if the file filename is redundant, delete it */
347 errcode = NotOnHost(filename, okhostfiles);
352 printf(" flushing %s\n", filename);
353 errcode = unlink(filename);
355 printf("could not delete file %s \n", filename);
356 afs_com_err(whoami, errno, "could not delete file %s",
363 /* Now, rename the .NEW files created by FetchFile */
364 if (RenameNewFiles(ModFiles))
368 } /* end for each dir loop */
369 /*delete the file with info on files in directory df->name */
370 IOMGR_Sleep(interval);
377 IOMGR_Sleep(retrytime);
379 rx_DestroyConnection(conn);
382 /* start the cycle again */
387 /* returns 1 if the file is upto date else returns 0*/
388 /*check the dir case more carefully */
390 IsCompatible(char *filename, afs_int32 time, afs_int32 length)
396 /* construct a local path from canonical (wire-format) path */
397 if ((error = ConstructLocalPath(filename, "/", &localname))) {
398 afs_com_err(whoami, error, "Unable to construct local path");
402 error = stat(localname, &status);
407 return 0; /*a non-existent file, has to be fetched fresh */
408 if ((status.st_mode & S_IFMT) == S_IFDIR
409 || ((status.st_mtime == time) && (status.st_size == length)))
416 FetchFile(struct rx_call *call, char *remoteFile, char *localFile, int dirFlag)
418 int fd = -1, error = 0;
422 if (StartUPDATE_FetchInfo(call, remoteFile))
425 if (StartUPDATE_FetchFile(call, remoteFile))
428 fd = open(localFile, O_CREAT | O_TRUNC | O_WRONLY, 0666);
430 printf("Could not create %s\n", localFile);
431 afs_com_err(whoami, errno, "Could not create %s", localFile);
432 error = UPDATE_ERROR;
435 if (fstat(fd, &status) < 0) {
436 afs_com_err(whoami, errno, "Could not stat %s", localFile);
438 printf("could not stat %s\n", localFile);
441 if (update_ReceiveFile(fd, call, &status))
442 error = UPDATE_ERROR;
452 update_ReceiveFile(int fd, struct rx_call *call, struct stat *status)
454 char *buffer = (char *)0;
457 afs_int32 error = 0, len;
459 struct statfs tstatfs;
462 len = rx_Read(call, (char *)&length, sizeof(afs_int32));
463 length = ntohl(length);
464 if (len != sizeof(afs_int32))
467 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
468 fstatfs(fd, &tstatfs);
469 blockSize = tstatfs.f_bsize;
473 blockSize = status->st_blksize;
475 buffer = (char *)malloc(blockSize);
477 printf("malloc failed\n");
480 while (!error && length) {
481 int nbytes = (length > blockSize ? blockSize : length);
482 nbytes = rx_Read(call, buffer, nbytes);
484 error = UPDATE_ERROR;
485 if (write(fd, buffer, nbytes) != nbytes) {
486 afs_com_err(whoami, errno, "File system write failed!");
487 printf("File system write failed!\n");
488 error = UPDATE_ERROR;
501 * PathsAreEquivalent() -- determine if paths are equivalent
504 PathsAreEquivalent(char *path1, char *path2)
507 char pathNorm1[AFSDIR_PATH_MAX], pathNorm2[AFSDIR_PATH_MAX];
510 /* case-insensitive comparison of normalized, same-flavor (short) paths */
513 status = GetShortPathName(path1, pathNorm1, AFSDIR_PATH_MAX);
514 if (status == 0 || status > AFSDIR_PATH_MAX) {
515 /* can't convert path to short version; just use long version */
516 strcpy(pathNorm1, path1);
518 FilepathNormalize(pathNorm1);
520 status = GetShortPathName(path2, pathNorm2, AFSDIR_PATH_MAX);
521 if (status == 0 || status > AFSDIR_PATH_MAX) {
522 /* can't convert path to short version; just use long version */
523 strcpy(pathNorm2, path2);
525 FilepathNormalize(pathNorm2);
527 if (_stricmp(pathNorm1, pathNorm2) == 0) {
531 /* case-sensitive comparison of normalized paths */
532 strcpy(pathNorm1, path1);
533 FilepathNormalize(pathNorm1);
535 strcpy(pathNorm2, path2);
536 FilepathNormalize(pathNorm2);
538 if (strcmp(pathNorm1, pathNorm2) == 0) {
541 #endif /* AFS_NT40_ENV */
547 /* returns 1 if filename does not exist on the host site (=> it should be
548 * deleted on client site) else it returns 0 */
551 NotOnHost(char *filename, struct filestr *okhostfiles)
558 stat(filename, &status);
560 if ((status.st_mode & S_IFMT) == S_IFDIR)
562 i = strlen(filename);
563 if (!strcmp(&filename[i - 4], ".NEW"))
566 for (tf = okhostfiles; tf; tf = tf->next) {
567 /* construct local path from canonical (wire-format) path */
568 if ((rc = ConstructLocalPath(tf->name, "/", &hostfile))) {
569 afs_com_err(whoami, rc, "Unable to construct local path");
572 if (PathsAreEquivalent(hostfile, filename)) {
582 /* RenameNewFiles() - renames all the newly copied files from the
583 * server. Looks for files with .NEW extension and renames them
586 RenameNewFiles(struct filestr *modFiles)
588 char newname[MAXFNSIZE];
593 for (tf = modFiles; tf; tf = tf->next) {
594 /* construct local path from canonical (wire-format) path */
595 if ((errcode = ConstructLocalPath(tf->name, "/", &fname))) {
596 afs_com_err(whoami, errcode, "Unable to construct local path");
599 strcpy(newname, fname);
600 strcat(newname, ".NEW");
602 printf(" renaming %s\n", newname);
603 errcode = renamefile(newname, fname);
605 printf("could not rename %s to %s\n", newname, fname);
606 afs_com_err(whoami, errno, "could not rename %s to %s", newname,
616 /* GetFileFromUpServer() - Makes the FetchFile() call and gets the
617 * file from the upserver.
620 * -1 - Serious error. Quit right away.
621 * 1 - Error, but keep trying for the other files
623 * The file obtained is written to the localized version of the filename.NEW
624 * and the uid, gid, file mode, access and modification times will be set to
625 * the passed in values.
628 GetFileFromUpServer(struct rx_connection *conn, /* handle for upserver */
629 char *filename, /* name of file to be fetched */
630 short uid, short gid, /* uid/gid for fetched file */
631 afs_uint32 mode, /* file mode */
632 afs_int32 atime, afs_int32 mtime)
633 { /* access/modification times */
634 struct rx_call *call;
638 struct _utimbuf utbuf;
640 struct timeval tvp[2];
642 char newfile[MAXFNSIZE];
644 /* construct local path from canonical (wire-format) path */
645 errcode = ConstructLocalPath(filename, "/", &lfile);
647 afs_com_err(whoami, errcode, "Unable to construct local path");
650 strcpy(newfile, lfile);
653 strcat(newfile, ".NEW");
655 /* fetch filename into newfile from the host, since the current file
656 * is outdated. the new versions of changed files is stored as
658 call = rx_NewCall(conn);
659 errcode = FetchFile(call, filename, newfile, 0);
660 errcode = rx_EndCall(call, errcode);
663 printf("failed to fetch file %s \n", filename);
664 afs_com_err(whoami, errcode, "fetching file");
669 /* now set the rest of the file status */
670 errcode = chmod(newfile, mode);
672 printf("could not change protection on %s to %u\n", newfile,
674 afs_com_err(whoami, errno, "could not change protection on %s to %u",
680 utbuf.actime = atime;
681 utbuf.modtime = mtime;
682 errcode = _utime(newfile, &utbuf);
684 errcode = chown(newfile, uid, gid);
686 printf("warning: could not change uid and gid on %s to %u and %u \n",
688 afs_com_err(whoami, errno,
689 "warning: could not change uid and gid on %s to %u and %u",
692 tvp[0].tv_sec = atime;
694 tvp[1].tv_sec = mtime;
696 errcode = utimes(newfile, tvp);
699 printf("could not change access and modify times on %s to %u %u\n",
700 newfile, (unsigned int)atime, (unsigned int)mtime);
701 afs_com_err(whoami, errno,
702 "could not change access and modify times on %s to %u %u",
703 newfile, atime, mtime);