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>
19 #include <sys/types.h>
24 #include <WINNT/afsevent.h>
25 #include <sys/utime.h>
26 #include <afs/dirent.h>
30 #include <afs/procmgmt.h>
34 #include <netinet/in.h>
53 #include <afs/com_err.h>
54 #include <afs/cellconfig.h>
55 #include <afs/afsutil.h>
56 #include <afs/fileutil.h>
64 static int GetFileFromUpServer(struct rx_connection *conn, char *filename,
65 short uid, short gid, afs_uint32 mode,
66 afs_int32 atime, afs_int32 mtime);
67 static int RenameNewFiles(struct filestr *modFiles);
68 static int PathsAreEquivalent(char *path1, char *path2);
70 afs_int32 GetServer(aname)
72 register struct hostent *th;
75 th = gethostbyname(aname);
77 printf("host %s not found \n", aname);
80 memcpy(&addr, th->h_addr, sizeof(addr));
87 /* this sucks but it works for now.
93 #include "AFS_component_version_number.c"
100 struct rx_connection *conn;
101 struct rx_call *call;
102 struct afsconf_dir *cdir;
104 struct rx_securityClass *sc;
107 afs_uint32 u_uid, u_gid; /*Unsigned long versions of the above*/
112 char hostname[MAXSIZE];
114 afs_int32 time, length, atime;
118 unsigned int interval;
123 char dirbuf[MAXSIZE], filename[MAXSIZE];
124 struct filestr *dirname, *ModFiles, *okhostfiles;
127 * The following signal action for AIX is necessary so that in case of a
128 * crash (i.e. core is generated) we can include the user's data section
129 * in the core dump. Unfortunately, by default, only a partial core is
130 * generated which, in many cases, isn't too useful.
132 struct sigaction nsa;
134 sigemptyset(&nsa.sa_mask);
135 nsa.sa_handler = SIG_DFL;
136 nsa.sa_flags = SA_FULLDUMP;
137 sigaction(SIGABRT, &nsa, NULL);
138 sigaction(SIGSEGV, &nsa, NULL);
142 /* dummy signal call to force afsprocmgmt.dll to load on NT */
143 signal(SIGUSR1, SIG_DFL);
145 /* initialize winsock */
146 if (afs_winsockInit()<0) {
147 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0,
149 fprintf(stderr, "%s: Couldn't initialize winsock.\n", whoami);
154 /* Initialize dirpaths */
155 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
157 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0],0);
159 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n", argv[0]);
169 level = rxkad_crypt; /* safest default */
170 strcpy (hostname, "");
172 /* Note that IsArg only checks as many bytes as specified in the command line arg,
173 * so that, for instance, -t still matches -time.
175 for (a=1; a<argc; a++) {
176 if (argv[a][0] == '-') { /* parse options */
177 int arglen = strlen(argv[a]);
179 lcstring (arg, argv[a], sizeof(arg));
180 #define IsArg(a) (strncmp (arg,a, arglen) == 0)
181 if (IsArg("-time")) interval = atol(argv[++a]);
182 else if (IsArg("-crypt")) level = rxkad_crypt;
183 else if (IsArg("-clear")) level = rxkad_clear;
184 else if (IsArg("-verbose")) verbose++;
187 printf("Usage: upclient <hostname> [-crypt] [-clear] [-t <retry time>] [-verbose]* <dir>+ [-help]\n");
191 else if (strlen (hostname) == 0) strcpy (hostname, argv[a]);
193 strcpy(filename, argv[a]);
194 FilepathNormalize(filename);
195 AddToList(&dirname, filename);
198 if (level == -1) goto usage;
199 if (strlen(hostname) == 0) goto usage;
200 host = GetServer(hostname);
201 if(interval < retrytime) retrytime = interval;
202 if (dirname == 0) goto usage;
204 errcode = rx_Init(0);
206 printf("Rx initialize failed \n");
207 com_err (whoami, errcode, "calling Rx init");
211 cdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
213 fprintf (stderr, "Can't get server configuration info (%s)\n",
214 AFSDIR_SERVER_ETC_DIRPATH);
218 if (level == rxkad_crypt)
219 errcode = afsconf_ClientAuthSecure (cdir, &sc, &scIndex);
220 else if (level == rxkad_clear)
221 errcode = afsconf_ClientAuth (cdir, &sc, &scIndex);
223 printf("Unsupported security level %d\n", level);
227 com_err (whoami, errcode, "Couldn't get security obect for localAuth");
232 conn = rx_NewConnection(host, htons(AFSCONF_UPDATEPORT), UPDATE_SERVICEID,
235 while(1){/*keep doing it */
237 for(df = dirname; df; df=df->next) { /*for each directory do */
240 if (verbose) printf ("Checking dir %s\n", df->name);
241 /* initialize lists */
243 ZapList(&okhostfiles);
245 /* construct local path from canonical (wire-format) path */
246 if ((errcode = ConstructLocalPath(df->name, "/", &curDir))) {
247 com_err(whoami, errcode, "Unable to construct local path");
251 if (stat(curDir, &tstat) < 0) {
252 /* try to make the dir */
254 if (mkdir(curDir) < 0) {
256 if (mkdir(curDir, 0700) < 0) {
258 com_err(whoami, errno, "can't create dir");
259 printf("upclient: Can't update dir %s\n", curDir);
263 if ((tstat.st_mode & S_IFMT) != S_IFDIR){
264 printf(" file %s is not a directory; aborting\n", curDir);
268 call = rx_NewCall(conn);
270 /* scratch pad file */
271 sprintf(dirbuf, "%s/upclient.%d", gettmpdir(), getpid());
273 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*/
274 error = rx_EndCall(call, 0);
275 if(error && !errcode){
276 printf("could not end rx call \n");
277 com_err (whoami, error, "calling EndCall");
281 printf("warning: could not fetch the contents of directory %s \n", df->name);
282 com_err (whoami, errcode, "calling FetchFile");
287 stream = fopen(dirbuf,"r");
289 printf("fopen failed on %s \n",dirbuf);
290 com_err (whoami, errno, "fopen");
295 /* while there is more info about files in file dirbuf */
296 while(fscanf(stream,"%c%[^\"]%c %u %u %u %u %u %u\n", &c, filename, &c1,
297 &time, &length, &mode, &u_uid, &u_gid,
301 AddToList(&okhostfiles, filename);
302 /*record all the file names which exist on the remote
303 sync site, to enable purging of redundant files*/
304 if (verbose >= 3) 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*/
308 if (verbose >= 2) printf (" getting %s\n", filename);
309 AddToList(&ModFiles, filename);
311 /* now get the file from the server. The received
312 * file is created under the name filename.NEW */
313 errcode = GetFileFromUpServer(conn, filename, uid, gid,
315 if (errcode == 1) /* this file failed, but keep trying */
317 if (errcode == -1) /* time to quit */ {
328 {/*delete all the redundant files on the client */
331 char filename[MAXSIZE];
333 dirp = opendir(curDir);
335 com_err (whoami, errno,
336 "Can't open local dir %s", curDir);
340 while((dp = readdir(dirp))) {
341 /* for all the files in the directory df->name do*/
342 strcpy(filename, curDir);
343 strcat(filename,"/");
344 strcat(filename,dp->d_name);
345 /* if the file filename is redundant, delete it */
346 errcode = NotOnHost(filename, okhostfiles);
350 if (verbose >= 2) printf (" flushing %s\n", filename);
351 errcode = unlink(filename);
353 printf("could not delete file %s \n",filename);
354 com_err (whoami, errno,"could not delete file %s",filename);
360 /* Now, rename the .NEW files created by FetchFile */
361 if (RenameNewFiles(ModFiles))
365 } /* end for each dir loop */
366 /*delete the file with info on files in directory df->name */
367 IOMGR_Sleep(interval);
374 IOMGR_Sleep(retrytime);
376 rx_DestroyConnection(conn);
379 /* start the cycle again */
384 /* returns 1 if the file is upto date else returns 0*/
385 int IsCompatible(filename, time, length)/*check the dir case more carefully*/
387 afs_int32 time, length;
393 /* construct a local path from canonical (wire-format) path */
394 if ((error = ConstructLocalPath(filename, "/", &localname))) {
395 com_err(whoami, error, "Unable to construct local path");
399 error = stat(localname, &status);
403 if(error == -1) return 0;/*a non-existent file, has to be fetched fresh*/
404 if( (status.st_mode & S_IFMT) == S_IFDIR || ((status.st_mtime == time) && (status.st_size == length)))
411 FetchFile(call, remoteFile, localFile, dirFlag)
412 struct rx_call *call;
413 char *localFile, *remoteFile;
416 int fd = -1, error = 0;
420 if (StartUPDATE_FetchInfo(call,remoteFile)) return UPDATE_ERROR;
423 if (StartUPDATE_FetchFile(call,remoteFile)) return UPDATE_ERROR;
425 fd = open(localFile, O_CREAT|O_TRUNC|O_WRONLY, 0666);
427 printf("Could not create %s\n", localFile);
428 com_err (whoami, errno, "Could not create %s", localFile);
429 error = UPDATE_ERROR;
432 if (fstat(fd, &status) < 0) {
433 com_err (whoami, errno, "Could not stat %s", localFile);
435 printf("could not stast %s\n", localFile);
438 if (update_ReceiveFile(fd, call, &status)) error = UPDATE_ERROR;
447 int update_ReceiveFile(fd, call, status)
449 register struct rx_call *call;
450 register struct stat *status;
452 register char *buffer = (char*) 0;
457 register int blockSize;
458 afs_int32 error = 0, len;
460 #include <sys/statfs.h>
461 struct statfs tstatfs;
465 xdrrx_create(&xdr, call, XDR_DECODE);
466 if (!xdr_afs_int32(&xdr, &length)) return UPDATE_ERROR;
468 len = rx_Read(call, &length, sizeof(afs_int32));
469 length = ntohl(length);
470 if (len != sizeof(afs_int32)) return UPDATE_ERROR;
473 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
474 fstatfs(fd, &tstatfs);
475 blockSize = tstatfs.f_bsize;
479 blockSize = status->st_blksize;
481 buffer = (char *)malloc(blockSize);
483 printf("malloc failed\n");
486 while (!error && length) {
487 register int nbytes = (length>blockSize?blockSize:length);
488 nbytes = rx_Read(call, buffer, nbytes);
489 if (!nbytes) error = UPDATE_ERROR;
490 if (write(fd, buffer, nbytes) != nbytes) {
491 com_err (whoami, errno, "File system write failed!");
492 printf("File system write failed!\n");
493 error = UPDATE_ERROR;
497 if (buffer) free(buffer);
498 if (!error) fstat(fd, status);
504 * PathsAreEquivalent() -- determine if paths are equivalent
507 PathsAreEquivalent(char *path1, char *path2)
510 char pathNorm1[AFSDIR_PATH_MAX], pathNorm2[AFSDIR_PATH_MAX];
513 /* case-insensitive comparison of normalized, same-flavor (short) paths */
516 status = GetShortPathName(path1, pathNorm1, AFSDIR_PATH_MAX);
517 if (status == 0 || status > AFSDIR_PATH_MAX) {
518 /* can't convert path to short version; just use long version */
519 strcpy(pathNorm1, path1);
521 FilepathNormalize(pathNorm1);
523 status = GetShortPathName(path2, pathNorm2, AFSDIR_PATH_MAX);
524 if (status == 0 || status > AFSDIR_PATH_MAX) {
525 /* can't convert path to short version; just use long version */
526 strcpy(pathNorm2, path2);
528 FilepathNormalize(pathNorm2);
530 if (_stricmp(pathNorm1, pathNorm2) == 0) {
534 /* case-sensitive comparison of normalized paths */
535 strcpy(pathNorm1, path1);
536 FilepathNormalize(pathNorm1);
538 strcpy(pathNorm2, path2);
539 FilepathNormalize(pathNorm2);
541 if (strcmp(pathNorm1, pathNorm2) == 0) {
544 #endif /* AFS_NT40_ENV */
550 /* returns 1 if filename does not exist on the host site (=> it should be
551 * deleted on client site) else it returns 0 */
553 int NotOnHost(filename, okhostfiles)
555 struct filestr *okhostfiles;
562 stat(filename,&status);
564 if((status.st_mode & S_IFMT) == S_IFDIR)
566 i = strlen(filename);
567 if(!strcmp(&filename[i-4],".NEW"))
570 for(tf=okhostfiles; tf; tf=tf->next) {
571 /* construct local path from canonical (wire-format) path */
572 if ((rc = ConstructLocalPath(tf->name, "/", &hostfile))) {
573 com_err(whoami, rc, "Unable to construct local path");
576 if (PathsAreEquivalent(hostfile, filename)) {
586 /* RenameNewFiles() - renames all the newly copied files from the
587 * server. Looks for files with .NEW extension and renames them
589 static int RenameNewFiles(struct filestr *modFiles)
591 char newname[MAXSIZE];
596 for(tf = modFiles; tf; tf=tf->next) {
597 /* construct local path from canonical (wire-format) path */
598 if ((errcode = ConstructLocalPath(tf->name, "/", &fname))) {
599 com_err(whoami, errcode, "Unable to construct local path");
602 strcpy(newname, fname); strcat(newname, ".NEW");
603 if (verbose >= 2) printf (" renaming %s\n", newname);
604 errcode = renamefile(newname, fname);
606 printf("could not rename %s to %s\n", newname, fname);
607 com_err (whoami, errno, "could not rename %s to %s", newname, fname);
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 int 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) /* access/modification times */
635 struct rx_call *call;
639 struct _utimbuf utbuf;
641 struct timeval tvp[2];
643 char newfile[MAXSIZE];
645 /* construct local path from canonical (wire-format) path */
646 errcode = ConstructLocalPath(filename, "/", &lfile);
648 com_err(whoami, errcode, "Unable to construct local path");
651 strcpy(newfile, lfile);
654 strcat(newfile, ".NEW");
656 /* fetch filename into newfile from the host, since the current file
657 * is outdated. the new versions of changed files is stored as
659 call = rx_NewCall(conn);
660 errcode = FetchFile(call, filename, newfile, 0);
661 errcode = rx_EndCall(call, errcode);
664 printf("failed to fetch file %s \n",filename);
665 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,
673 (unsigned int) mode);
674 com_err (whoami, errno,
675 "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",newfile, gid, uid);
687 com_err (whoami, errno,
688 "warning: could not change uid and gid on %s to %u and %u",newfile, gid, uid);
690 tvp[0].tv_sec = atime;
692 tvp[1].tv_sec = mtime;
694 errcode = utimes(newfile, tvp);
697 printf("could not change access and modify times on %s to %u %u\n",newfile, (unsigned int) atime, (unsigned int) mtime);
698 com_err (whoami, errno,
699 "could not change access and modify times on %s to %u %u",newfile, atime, mtime);