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>
23 #include <afs/procmgmt.h>
27 #include <sys/statfs.h>
33 #include <afs/com_err.h>
34 #include <afs/cellconfig.h>
35 #include <afs/afsutil.h>
36 #include <afs/fileutil.h>
40 #include "update_internal.h"
46 static int GetFileFromUpServer(struct rx_connection *conn, char *filename,
47 short uid, short gid, afs_uint32 mode,
48 afs_int32 atime, afs_int32 mtime);
49 static int RenameNewFiles(struct filestr *modFiles);
50 static int PathsAreEquivalent(char *path1, char *path2);
51 int FetchFile(struct rx_call *, char *, char *, int);
52 int IsCompatible(char *, afs_int32, afs_int32);
53 int NotOnHost(char *, struct filestr *);
54 int update_ReceiveFile(int, struct rx_call *, struct stat *);
57 GetServer(char *aname)
62 th = gethostbyname(aname);
64 printf("host %s not found \n", aname);
67 memcpy(&addr, th->h_addr, sizeof(addr));
75 /* this sucks but it works for now.
81 #include "AFS_component_version_number.c"
85 main(int argc, char **argv)
87 struct rx_connection *conn;
89 struct afsconf_dir *cdir;
91 struct rx_securityClass *sc;
94 afs_uint32 u_uid, u_gid; /*Unsigned long versions of the above */
99 char hostname[MAXFNSIZE];
101 afs_int32 time, length, atime;
105 unsigned int interval;
110 char dirbuf[MAXFNSIZE], filename[MAXFNSIZE];
111 struct filestr *dirname, *ModFiles, *okhostfiles;
114 * The following signal action for AIX is necessary so that in case of a
115 * crash (i.e. core is generated) we can include the user's data section
116 * in the core dump. Unfortunately, by default, only a partial core is
117 * generated which, in many cases, isn't too useful.
119 struct sigaction nsa;
121 sigemptyset(&nsa.sa_mask);
122 nsa.sa_handler = SIG_DFL;
123 nsa.sa_flags = SA_FULLDUMP;
124 sigaction(SIGABRT, &nsa, NULL);
125 sigaction(SIGSEGV, &nsa, NULL);
129 /* dummy signal call to force afsprocmgmt.dll to load on NT */
130 signal(SIGUSR1, SIG_DFL);
132 /* initialize winsock */
133 if (afs_winsockInit() < 0) {
134 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
135 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
140 /* Initialize dirpaths */
141 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
143 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
145 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
156 level = rxkad_crypt; /* safest default */
157 strcpy(hostname, "");
159 /* Note that IsArg only checks as many bytes as specified in the command line arg,
160 * so that, for instance, -t still matches -time.
162 for (a = 1; a < argc; a++) {
163 if (argv[a][0] == '-') { /* parse options */
164 int arglen = strlen(argv[a]);
166 lcstring(arg, argv[a], sizeof(arg));
167 #define IsArg(a) (strncmp (arg,a, arglen) == 0)
169 interval = atol(argv[++a]);
170 else if (IsArg("-crypt"))
172 else if (IsArg("-clear"))
174 else if (IsArg("-verbose"))
179 ("Usage: upclient <hostname> [-crypt] [-clear] [-t <retry time>] [-verbose]* <dir>+ [-help]\n");
182 } else if (strlen(hostname) == 0)
183 strcpy(hostname, argv[a]);
185 strcpy(filename, argv[a]);
186 FilepathNormalize(filename);
187 AddToList(&dirname, filename);
192 if (strlen(hostname) == 0)
194 host = GetServer(hostname);
195 if (interval < retrytime)
196 retrytime = interval;
200 errcode = rx_Init(0);
202 printf("Rx initialize failed \n");
203 afs_com_err(whoami, errcode, "calling Rx init");
207 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
209 fprintf(stderr, "Can't get server configuration info (%s)\n",
210 AFSDIR_SERVER_ETC_DIRPATH);
214 if (level == rxkad_crypt)
215 errcode = afsconf_ClientAuthSecure(cdir, &sc, &scIndex);
216 else if (level == rxkad_clear)
217 errcode = afsconf_ClientAuth(cdir, &sc, &scIndex);
219 printf("Unsupported security level %d\n", level);
223 afs_com_err(whoami, errcode, "Couldn't get security obect for localAuth");
229 rx_NewConnection(host, htons(AFSCONF_UPDATEPORT), UPDATE_SERVICEID,
232 while (1) { /*keep doing it */
234 for (df = dirname; df; df = df->next) { /*for each directory do */
238 printf("Checking dir %s\n", df->name);
239 /* initialize lists */
241 ZapList(&okhostfiles);
243 /* construct local path from canonical (wire-format) path */
244 if ((errcode = ConstructLocalPath(df->name, "/", &curDir))) {
245 afs_com_err(whoami, errcode, "Unable to construct local path");
249 if (stat(curDir, &tstat) < 0) {
250 /* try to make the dir */
252 if (mkdir(curDir) < 0) {
254 if (mkdir(curDir, 0700) < 0) {
256 afs_com_err(whoami, errno, "can't create dir");
257 printf("upclient: Can't update dir %s\n", curDir);
261 if ((tstat.st_mode & S_IFMT) != S_IFDIR) {
262 printf(" file %s is not a directory; aborting\n", curDir);
266 call = rx_NewCall(conn);
268 /* scratch pad file */
269 sprintf(dirbuf, "%s/upclient.%d", gettmpdir(), getpid());
271 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 */
272 error = rx_EndCall(call, 0);
273 if (error && !errcode) {
274 printf("could not end rx call \n");
275 afs_com_err(whoami, error, "calling EndCall");
280 ("warning: could not fetch the contents of directory %s \n",
282 afs_com_err(whoami, errcode, "calling FetchFile");
287 stream = fopen(dirbuf, "r");
288 if (stream == NULL) {
289 printf("fopen failed on %s \n", dirbuf);
290 afs_com_err(whoami, errno, "fopen");
295 /* while there is more info about files in file dirbuf */
297 (stream, "%c%[^\"]%c %u %u %u %u %u %u\n", &c, filename,
298 &c1, &time, &length, &mode, &u_uid, &u_gid,
302 AddToList(&okhostfiles, filename);
303 /*record all the file names which exist on the remote
304 * sync site, to enable purging of redundant files */
306 printf(" checking %s\n", filename);
307 if (!IsCompatible(filename, time, length)) {
308 /* if the file info has changed , record all the
309 *changed files in the ModFiles array*/
311 printf(" getting %s\n", filename);
312 AddToList(&ModFiles, filename);
314 /* now get the file from the server. The received
315 * file is created under the name filename.NEW */
317 GetFileFromUpServer(conn, filename, uid, gid, mode,
319 if (errcode == 1) /* this file failed, but keep trying */
321 if (errcode == -1) { /* time to quit */
332 { /*delete all the redundant files on the client */
335 char filename[MAXFNSIZE];
337 dirp = opendir(curDir);
339 afs_com_err(whoami, errno, "Can't open local dir %s", curDir);
343 while ((dp = readdir(dirp))) {
344 /* for all the files in the directory df->name do */
345 strcpy(filename, curDir);
346 strcat(filename, "/");
347 strcat(filename, dp->d_name);
348 /* if the file filename is redundant, delete it */
349 errcode = NotOnHost(filename, okhostfiles);
354 printf(" flushing %s\n", filename);
355 errcode = unlink(filename);
357 printf("could not delete file %s \n", filename);
358 afs_com_err(whoami, errno, "could not delete file %s",
365 /* Now, rename the .NEW files created by FetchFile */
366 if (RenameNewFiles(ModFiles))
370 } /* end for each dir loop */
371 /*delete the file with info on files in directory df->name */
372 IOMGR_Sleep(interval);
379 IOMGR_Sleep(retrytime);
381 rx_DestroyConnection(conn);
384 /* start the cycle again */
389 /* returns 1 if the file is upto date else returns 0*/
390 /*check the dir case more carefully */
392 IsCompatible(char *filename, afs_int32 time, afs_int32 length)
398 /* construct a local path from canonical (wire-format) path */
399 if ((error = ConstructLocalPath(filename, "/", &localname))) {
400 afs_com_err(whoami, error, "Unable to construct local path");
404 error = stat(localname, &status);
409 return 0; /*a non-existent file, has to be fetched fresh */
410 if ((status.st_mode & S_IFMT) == S_IFDIR
411 || ((status.st_mtime == time) && (status.st_size == length)))
418 FetchFile(struct rx_call *call, char *remoteFile, char *localFile, int dirFlag)
420 int fd = -1, error = 0;
424 if (StartUPDATE_FetchInfo(call, remoteFile))
427 if (StartUPDATE_FetchFile(call, remoteFile))
430 fd = open(localFile, O_CREAT | O_TRUNC | O_WRONLY, 0666);
432 printf("Could not create %s\n", localFile);
433 afs_com_err(whoami, errno, "Could not create %s", localFile);
434 error = UPDATE_ERROR;
437 if (fstat(fd, &status) < 0) {
438 afs_com_err(whoami, errno, "Could not stat %s", localFile);
440 printf("could not stat %s\n", localFile);
443 if (update_ReceiveFile(fd, call, &status))
444 error = UPDATE_ERROR;
454 update_ReceiveFile(int fd, struct rx_call *call, struct stat *status)
456 char *buffer = (char *)0;
459 afs_int32 error = 0, len;
461 struct statfs tstatfs;
464 len = rx_Read(call, (char *)&length, sizeof(afs_int32));
465 length = ntohl(length);
466 if (len != sizeof(afs_int32))
469 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
470 fstatfs(fd, &tstatfs);
471 blockSize = tstatfs.f_bsize;
475 blockSize = status->st_blksize;
477 buffer = (char *)malloc(blockSize);
479 printf("malloc failed\n");
482 while (!error && length) {
483 int nbytes = (length > blockSize ? blockSize : length);
484 nbytes = rx_Read(call, buffer, nbytes);
486 error = UPDATE_ERROR;
487 if (write(fd, buffer, nbytes) != nbytes) {
488 afs_com_err(whoami, errno, "File system write failed!");
489 printf("File system write failed!\n");
490 error = UPDATE_ERROR;
503 * PathsAreEquivalent() -- determine if paths are equivalent
506 PathsAreEquivalent(char *path1, char *path2)
509 char pathNorm1[AFSDIR_PATH_MAX], pathNorm2[AFSDIR_PATH_MAX];
512 /* case-insensitive comparison of normalized, same-flavor (short) paths */
515 status = GetShortPathName(path1, pathNorm1, AFSDIR_PATH_MAX);
516 if (status == 0 || status > AFSDIR_PATH_MAX) {
517 /* can't convert path to short version; just use long version */
518 strcpy(pathNorm1, path1);
520 FilepathNormalize(pathNorm1);
522 status = GetShortPathName(path2, pathNorm2, AFSDIR_PATH_MAX);
523 if (status == 0 || status > AFSDIR_PATH_MAX) {
524 /* can't convert path to short version; just use long version */
525 strcpy(pathNorm2, path2);
527 FilepathNormalize(pathNorm2);
529 if (_stricmp(pathNorm1, pathNorm2) == 0) {
533 /* case-sensitive comparison of normalized paths */
534 strcpy(pathNorm1, path1);
535 FilepathNormalize(pathNorm1);
537 strcpy(pathNorm2, path2);
538 FilepathNormalize(pathNorm2);
540 if (strcmp(pathNorm1, pathNorm2) == 0) {
543 #endif /* AFS_NT40_ENV */
549 /* returns 1 if filename does not exist on the host site (=> it should be
550 * deleted on client site) else it returns 0 */
553 NotOnHost(char *filename, struct filestr *okhostfiles)
560 stat(filename, &status);
562 if ((status.st_mode & S_IFMT) == S_IFDIR)
564 i = strlen(filename);
565 if (!strcmp(&filename[i - 4], ".NEW"))
568 for (tf = okhostfiles; tf; tf = tf->next) {
569 /* construct local path from canonical (wire-format) path */
570 if ((rc = ConstructLocalPath(tf->name, "/", &hostfile))) {
571 afs_com_err(whoami, rc, "Unable to construct local path");
574 if (PathsAreEquivalent(hostfile, filename)) {
584 /* RenameNewFiles() - renames all the newly copied files from the
585 * server. Looks for files with .NEW extension and renames them
588 RenameNewFiles(struct filestr *modFiles)
590 char newname[MAXFNSIZE];
595 for (tf = modFiles; tf; tf = tf->next) {
596 /* construct local path from canonical (wire-format) path */
597 if ((errcode = ConstructLocalPath(tf->name, "/", &fname))) {
598 afs_com_err(whoami, errcode, "Unable to construct local path");
601 strcpy(newname, fname);
602 strcat(newname, ".NEW");
604 printf(" renaming %s\n", newname);
605 errcode = renamefile(newname, fname);
607 printf("could not rename %s to %s\n", newname, fname);
608 afs_com_err(whoami, errno, "could not rename %s to %s", newname,
618 /* GetFileFromUpServer() - Makes the FetchFile() call and gets the
619 * file from the upserver.
622 * -1 - Serious error. Quit right away.
623 * 1 - Error, but keep trying for the other files
625 * The file obtained is written to the localized version of the filename.NEW
626 * and the uid, gid, file mode, access and modification times will be set to
627 * the passed in values.
630 GetFileFromUpServer(struct rx_connection *conn, /* handle for upserver */
631 char *filename, /* name of file to be fetched */
632 short uid, short gid, /* uid/gid for fetched file */
633 afs_uint32 mode, /* file mode */
634 afs_int32 atime, afs_int32 mtime)
635 { /* access/modification times */
636 struct rx_call *call;
640 struct _utimbuf utbuf;
642 struct timeval tvp[2];
644 char newfile[MAXFNSIZE];
646 /* construct local path from canonical (wire-format) path */
647 errcode = ConstructLocalPath(filename, "/", &lfile);
649 afs_com_err(whoami, errcode, "Unable to construct local path");
652 strcpy(newfile, lfile);
655 strcat(newfile, ".NEW");
657 /* fetch filename into newfile from the host, since the current file
658 * is outdated. the new versions of changed files is stored as
660 call = rx_NewCall(conn);
661 errcode = FetchFile(call, filename, newfile, 0);
662 errcode = rx_EndCall(call, errcode);
665 printf("failed to fetch file %s \n", filename);
666 afs_com_err(whoami, errcode, "fetching file");
671 /* now set the rest of the file status */
672 errcode = chmod(newfile, mode);
674 printf("could not change protection on %s to %u\n", newfile,
676 afs_com_err(whoami, errno, "could not change protection on %s to %u",
682 utbuf.actime = atime;
683 utbuf.modtime = mtime;
684 errcode = _utime(newfile, &utbuf);
686 errcode = chown(newfile, uid, gid);
688 printf("warning: could not change uid and gid on %s to %u and %u \n",
690 afs_com_err(whoami, errno,
691 "warning: could not change uid and gid on %s to %u and %u",
694 tvp[0].tv_sec = atime;
696 tvp[1].tv_sec = mtime;
698 errcode = utimes(newfile, tvp);
701 printf("could not change access and modify times on %s to %u %u\n",
702 newfile, (unsigned int)atime, (unsigned int)mtime);
703 afs_com_err(whoami, errno,
704 "could not change access and modify times on %s to %u %u",
705 newfile, atime, mtime);