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>
20 #include <sys/types.h>
25 #include <WINNT/afsevent.h>
26 #include <sys/utime.h>
27 #include <afs/dirent.h>
31 #include <afs/procmgmt.h>
35 #include <netinet/in.h>
54 #include <afs/com_err.h>
55 #include <afs/cellconfig.h>
56 #include <afs/afsutil.h>
57 #include <afs/fileutil.h>
65 static int GetFileFromUpServer(struct rx_connection *conn, char *filename,
66 short uid, short gid, afs_uint32 mode,
67 afs_int32 atime, afs_int32 mtime);
68 static int RenameNewFiles(struct filestr *modFiles);
69 static int PathsAreEquivalent(char *path1, char *path2);
75 register struct hostent *th;
78 th = gethostbyname(aname);
80 printf("host %s not found \n", aname);
83 memcpy(&addr, th->h_addr, sizeof(addr));
91 /* this sucks but it works for now.
97 #include "AFS_component_version_number.c"
105 struct rx_connection *conn;
106 struct rx_call *call;
107 struct afsconf_dir *cdir;
109 struct rx_securityClass *sc;
112 afs_uint32 u_uid, u_gid; /*Unsigned long versions of the above */
117 char hostname[MAXSIZE];
119 afs_int32 time, length, atime;
123 unsigned int interval;
128 char dirbuf[MAXSIZE], filename[MAXSIZE];
129 struct filestr *dirname, *ModFiles, *okhostfiles;
132 * The following signal action for AIX is necessary so that in case of a
133 * crash (i.e. core is generated) we can include the user's data section
134 * in the core dump. Unfortunately, by default, only a partial core is
135 * generated which, in many cases, isn't too useful.
137 struct sigaction nsa;
139 sigemptyset(&nsa.sa_mask);
140 nsa.sa_handler = SIG_DFL;
141 nsa.sa_flags = SA_FULLDUMP;
142 sigaction(SIGABRT, &nsa, NULL);
143 sigaction(SIGSEGV, &nsa, NULL);
147 /* dummy signal call to force afsprocmgmt.dll to load on NT */
148 signal(SIGUSR1, SIG_DFL);
150 /* initialize winsock */
151 if (afs_winsockInit() < 0) {
152 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
153 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
158 /* Initialize dirpaths */
159 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
161 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
163 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
174 level = rxkad_crypt; /* safest default */
175 strcpy(hostname, "");
177 /* Note that IsArg only checks as many bytes as specified in the command line arg,
178 * so that, for instance, -t still matches -time.
180 for (a = 1; a < argc; a++) {
181 if (argv[a][0] == '-') { /* parse options */
182 int arglen = strlen(argv[a]);
184 lcstring(arg, argv[a], sizeof(arg));
185 #define IsArg(a) (strncmp (arg,a, arglen) == 0)
187 interval = atol(argv[++a]);
188 else if (IsArg("-crypt"))
190 else if (IsArg("-clear"))
192 else if (IsArg("-verbose"))
197 ("Usage: upclient <hostname> [-crypt] [-clear] [-t <retry time>] [-verbose]* <dir>+ [-help]\n");
200 } else if (strlen(hostname) == 0)
201 strcpy(hostname, argv[a]);
203 strcpy(filename, argv[a]);
204 FilepathNormalize(filename);
205 AddToList(&dirname, filename);
210 if (strlen(hostname) == 0)
212 host = GetServer(hostname);
213 if (interval < retrytime)
214 retrytime = interval;
218 errcode = rx_Init(0);
220 printf("Rx initialize failed \n");
221 com_err(whoami, errcode, "calling Rx init");
225 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
227 fprintf(stderr, "Can't get server configuration info (%s)\n",
228 AFSDIR_SERVER_ETC_DIRPATH);
232 if (level == rxkad_crypt)
233 errcode = afsconf_ClientAuthSecure(cdir, &sc, &scIndex);
234 else if (level == rxkad_clear)
235 errcode = afsconf_ClientAuth(cdir, &sc, &scIndex);
237 printf("Unsupported security level %d\n", level);
241 com_err(whoami, errcode, "Couldn't get security obect for localAuth");
247 rx_NewConnection(host, htons(AFSCONF_UPDATEPORT), UPDATE_SERVICEID,
250 while (1) { /*keep doing it */
252 for (df = dirname; df; df = df->next) { /*for each directory do */
256 printf("Checking dir %s\n", df->name);
257 /* initialize lists */
259 ZapList(&okhostfiles);
261 /* construct local path from canonical (wire-format) path */
262 if ((errcode = ConstructLocalPath(df->name, "/", &curDir))) {
263 com_err(whoami, errcode, "Unable to construct local path");
267 if (stat(curDir, &tstat) < 0) {
268 /* try to make the dir */
270 if (mkdir(curDir) < 0) {
272 if (mkdir(curDir, 0700) < 0) {
274 com_err(whoami, errno, "can't create dir");
275 printf("upclient: Can't update dir %s\n", curDir);
279 if ((tstat.st_mode & S_IFMT) != S_IFDIR) {
280 printf(" file %s is not a directory; aborting\n", curDir);
284 call = rx_NewCall(conn);
286 /* scratch pad file */
287 sprintf(dirbuf, "%s/upclient.%d", gettmpdir(), getpid());
289 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 */
290 error = rx_EndCall(call, 0);
291 if (error && !errcode) {
292 printf("could not end rx call \n");
293 com_err(whoami, error, "calling EndCall");
298 ("warning: could not fetch the contents of directory %s \n",
300 com_err(whoami, errcode, "calling FetchFile");
305 stream = fopen(dirbuf, "r");
306 if (stream == NULL) {
307 printf("fopen failed on %s \n", dirbuf);
308 com_err(whoami, errno, "fopen");
313 /* while there is more info about files in file dirbuf */
315 (stream, "%c%[^\"]%c %u %u %u %u %u %u\n", &c, filename,
316 &c1, &time, &length, &mode, &u_uid, &u_gid,
320 AddToList(&okhostfiles, filename);
321 /*record all the file names which exist on the remote
322 * sync site, to enable purging of redundant files */
324 printf(" checking %s\n", filename);
325 if (!IsCompatible(filename, time, length)) {
326 /* if the file info has changed , record all the
327 *changed files in the ModFiles array*/
329 printf(" getting %s\n", filename);
330 AddToList(&ModFiles, filename);
332 /* now get the file from the server. The received
333 * file is created under the name filename.NEW */
335 GetFileFromUpServer(conn, filename, uid, gid, mode,
337 if (errcode == 1) /* this file failed, but keep trying */
339 if (errcode == -1) { /* time to quit */
350 { /*delete all the redundant files on the client */
353 char filename[MAXSIZE];
355 dirp = opendir(curDir);
357 com_err(whoami, errno, "Can't open local dir %s", curDir);
361 while ((dp = readdir(dirp))) {
362 /* for all the files in the directory df->name do */
363 strcpy(filename, curDir);
364 strcat(filename, "/");
365 strcat(filename, dp->d_name);
366 /* if the file filename is redundant, delete it */
367 errcode = NotOnHost(filename, okhostfiles);
372 printf(" flushing %s\n", filename);
373 errcode = unlink(filename);
375 printf("could not delete file %s \n", filename);
376 com_err(whoami, errno, "could not delete file %s",
383 /* Now, rename the .NEW files created by FetchFile */
384 if (RenameNewFiles(ModFiles))
388 } /* end for each dir loop */
389 /*delete the file with info on files in directory df->name */
390 IOMGR_Sleep(interval);
397 IOMGR_Sleep(retrytime);
399 rx_DestroyConnection(conn);
402 /* start the cycle again */
407 /* returns 1 if the file is upto date else returns 0*/
409 IsCompatible(filename, time, length) /*check the dir case more carefully */
411 afs_int32 time, length;
417 /* construct a local path from canonical (wire-format) path */
418 if ((error = ConstructLocalPath(filename, "/", &localname))) {
419 com_err(whoami, error, "Unable to construct local path");
423 error = stat(localname, &status);
428 return 0; /*a non-existent file, has to be fetched fresh */
429 if ((status.st_mode & S_IFMT) == S_IFDIR
430 || ((status.st_mtime == time) && (status.st_size == length)))
437 FetchFile(call, remoteFile, localFile, dirFlag)
438 struct rx_call *call;
439 char *localFile, *remoteFile;
442 int fd = -1, error = 0;
446 if (StartUPDATE_FetchInfo(call, remoteFile))
449 if (StartUPDATE_FetchFile(call, remoteFile))
452 fd = open(localFile, O_CREAT | O_TRUNC | O_WRONLY, 0666);
454 printf("Could not create %s\n", localFile);
455 com_err(whoami, errno, "Could not create %s", localFile);
456 error = UPDATE_ERROR;
459 if (fstat(fd, &status) < 0) {
460 com_err(whoami, errno, "Could not stat %s", localFile);
462 printf("could not stast %s\n", localFile);
465 if (update_ReceiveFile(fd, call, &status))
466 error = UPDATE_ERROR;
476 update_ReceiveFile(fd, call, status)
478 register struct rx_call *call;
479 register struct stat *status;
481 register char *buffer = (char *)0;
486 register int blockSize;
487 afs_int32 error = 0, len;
489 #include <sys/statfs.h>
490 struct statfs tstatfs;
494 xdrrx_create(&xdr, call, XDR_DECODE);
495 if (!xdr_afs_int32(&xdr, &length))
498 len = rx_Read(call, &length, sizeof(afs_int32));
499 length = ntohl(length);
500 if (len != sizeof(afs_int32))
504 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
505 fstatfs(fd, &tstatfs);
506 blockSize = tstatfs.f_bsize;
510 blockSize = status->st_blksize;
512 buffer = (char *)malloc(blockSize);
514 printf("malloc failed\n");
517 while (!error && length) {
518 register int nbytes = (length > blockSize ? blockSize : length);
519 nbytes = rx_Read(call, buffer, nbytes);
521 error = UPDATE_ERROR;
522 if (write(fd, buffer, nbytes) != nbytes) {
523 com_err(whoami, errno, "File system write failed!");
524 printf("File system write failed!\n");
525 error = UPDATE_ERROR;
538 * PathsAreEquivalent() -- determine if paths are equivalent
541 PathsAreEquivalent(char *path1, char *path2)
544 char pathNorm1[AFSDIR_PATH_MAX], pathNorm2[AFSDIR_PATH_MAX];
547 /* case-insensitive comparison of normalized, same-flavor (short) paths */
550 status = GetShortPathName(path1, pathNorm1, AFSDIR_PATH_MAX);
551 if (status == 0 || status > AFSDIR_PATH_MAX) {
552 /* can't convert path to short version; just use long version */
553 strcpy(pathNorm1, path1);
555 FilepathNormalize(pathNorm1);
557 status = GetShortPathName(path2, pathNorm2, AFSDIR_PATH_MAX);
558 if (status == 0 || status > AFSDIR_PATH_MAX) {
559 /* can't convert path to short version; just use long version */
560 strcpy(pathNorm2, path2);
562 FilepathNormalize(pathNorm2);
564 if (_stricmp(pathNorm1, pathNorm2) == 0) {
568 /* case-sensitive comparison of normalized paths */
569 strcpy(pathNorm1, path1);
570 FilepathNormalize(pathNorm1);
572 strcpy(pathNorm2, path2);
573 FilepathNormalize(pathNorm2);
575 if (strcmp(pathNorm1, pathNorm2) == 0) {
578 #endif /* AFS_NT40_ENV */
584 /* returns 1 if filename does not exist on the host site (=> it should be
585 * deleted on client site) else it returns 0 */
588 NotOnHost(filename, okhostfiles)
590 struct filestr *okhostfiles;
597 stat(filename, &status);
599 if ((status.st_mode & S_IFMT) == S_IFDIR)
601 i = strlen(filename);
602 if (!strcmp(&filename[i - 4], ".NEW"))
605 for (tf = okhostfiles; tf; tf = tf->next) {
606 /* construct local path from canonical (wire-format) path */
607 if ((rc = ConstructLocalPath(tf->name, "/", &hostfile))) {
608 com_err(whoami, rc, "Unable to construct local path");
611 if (PathsAreEquivalent(hostfile, filename)) {
621 /* RenameNewFiles() - renames all the newly copied files from the
622 * server. Looks for files with .NEW extension and renames them
625 RenameNewFiles(struct filestr *modFiles)
627 char newname[MAXSIZE];
632 for (tf = modFiles; tf; tf = tf->next) {
633 /* construct local path from canonical (wire-format) path */
634 if ((errcode = ConstructLocalPath(tf->name, "/", &fname))) {
635 com_err(whoami, errcode, "Unable to construct local path");
638 strcpy(newname, fname);
639 strcat(newname, ".NEW");
641 printf(" renaming %s\n", newname);
642 errcode = renamefile(newname, fname);
644 printf("could not rename %s to %s\n", newname, fname);
645 com_err(whoami, errno, "could not rename %s to %s", newname,
655 /* GetFileFromUpServer() - Makes the FetchFile() call and gets the
656 * file from the upserver.
659 * -1 - Serious error. Quit right away.
660 * 1 - Error, but keep trying for the other files
662 * The file obtained is written to the localized version of the filename.NEW
663 * and the uid, gid, file mode, access and modification times will be set to
664 * the passed in values.
668 GetFileFromUpServer(struct rx_connection *conn, /* handle for upserver */
669 char *filename, /* name of file to be fetched */
670 short uid, short gid, /* uid/gid for fetched file */
671 afs_uint32 mode, /* file mode */
672 afs_int32 atime, afs_int32 mtime)
673 { /* access/modification times */
674 struct rx_call *call;
678 struct _utimbuf utbuf;
680 struct timeval tvp[2];
682 char newfile[MAXSIZE];
684 /* construct local path from canonical (wire-format) path */
685 errcode = ConstructLocalPath(filename, "/", &lfile);
687 com_err(whoami, errcode, "Unable to construct local path");
690 strcpy(newfile, lfile);
693 strcat(newfile, ".NEW");
695 /* fetch filename into newfile from the host, since the current file
696 * is outdated. the new versions of changed files is stored as
698 call = rx_NewCall(conn);
699 errcode = FetchFile(call, filename, newfile, 0);
700 errcode = rx_EndCall(call, errcode);
703 printf("failed to fetch file %s \n", filename);
704 com_err(whoami, errcode, "fetching file");
708 /* now set the rest of the file status */
709 errcode = chmod(newfile, mode);
711 printf("could not change protection on %s to %u\n", newfile,
713 com_err(whoami, errno, "could not change protection on %s to %u",
718 utbuf.actime = atime;
719 utbuf.modtime = mtime;
720 errcode = _utime(newfile, &utbuf);
722 errcode = chown(newfile, uid, gid);
724 printf("warning: could not change uid and gid on %s to %u and %u \n",
726 com_err(whoami, errno,
727 "warning: could not change uid and gid on %s to %u and %u",
730 tvp[0].tv_sec = atime;
732 tvp[1].tv_sec = mtime;
734 errcode = utimes(newfile, tvp);
737 printf("could not change access and modify times on %s to %u %u\n",
738 newfile, (unsigned int)atime, (unsigned int)mtime);
739 com_err(whoami, errno,
740 "could not change access and modify times on %s to %u %u",
741 newfile, atime, mtime);