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 bcopy(th->h_addr, &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]);
163 dirname = (struct filestr *) 0;
164 ModFiles = (struct filestr *) 0;
165 okhostfiles = (struct filestr *) 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 */
325 {/*delete all the redundant files on the client */
328 char filename[MAXSIZE];
330 dirp = opendir(curDir);
332 com_err (whoami, errno,
333 "Can't open local dir %s", curDir);
337 while((dp = readdir(dirp))) {
338 /* for all the files in the directory df->name do*/
339 strcpy(filename, curDir);
340 strcat(filename,"/");
341 strcat(filename,dp->d_name);
342 /* if the file filename is redundant, delete it */
343 errcode = NotOnHost(filename, okhostfiles);
347 if (verbose >= 2) printf (" flushing %s\n", filename);
348 errcode = unlink(filename);
350 printf("could not delete file %s \n",filename);
351 com_err (whoami, errno,"could not delete file %s",filename);
357 /* Now, rename the .NEW files created by FetchFile */
358 if (RenameNewFiles(ModFiles))
362 } /* end for each dir loop */
363 /*delete the file with info on files in directory df->name */
364 IOMGR_Sleep(interval);
371 IOMGR_Sleep(retrytime);
373 rx_DestroyConnection(conn);
376 /* start the cycle again */
381 /* returns 1 if the file is upto date else returns 0*/
382 int IsCompatible(filename, time, length)/*check the dir case more carefully*/
384 afs_int32 time, length;
390 /* construct a local path from canonical (wire-format) path */
391 if ((error = ConstructLocalPath(filename, "/", &localname))) {
392 com_err(whoami, error, "Unable to construct local path");
396 error = stat(localname, &status);
400 if(error == -1) return 0;/*a non-existent file, has to be fetched fresh*/
401 if( (status.st_mode & S_IFMT) == S_IFDIR || ((status.st_mtime == time) && (status.st_size == length)))
408 FetchFile(call, remoteFile, localFile, dirFlag)
409 struct rx_call *call;
410 char *localFile, *remoteFile;
413 int fd = -1, error = 0;
417 if (StartUPDATE_FetchInfo(call,remoteFile)) return UPDATE_ERROR;
420 if (StartUPDATE_FetchFile(call,remoteFile)) return UPDATE_ERROR;
422 fd = open(localFile, O_CREAT|O_TRUNC|O_WRONLY, 0666);
424 printf("Could not create %s\n", localFile);
425 com_err (whoami, errno, "Could not create %s", localFile);
426 error = UPDATE_ERROR;
429 if (fstat(fd, &status) < 0) {
430 com_err (whoami, errno, "Could not stat %s", localFile);
432 printf("could not stast %s\n", localFile);
435 if (update_ReceiveFile(fd, call, &status)) error = UPDATE_ERROR;
444 int update_ReceiveFile(fd, call, status)
446 register struct rx_call *call;
447 register struct stat *status;
449 register char *buffer = (char*) 0;
454 register int blockSize;
455 afs_int32 error = 0, len;
457 #include <sys/statfs.h>
458 struct statfs tstatfs;
462 xdrrx_create(&xdr, call, XDR_DECODE);
463 if (!xdr_afs_int32(&xdr, &length)) return UPDATE_ERROR;
465 len = rx_Read(call, &length, sizeof(afs_int32));
466 length = ntohl(length);
467 if (len != sizeof(afs_int32)) return UPDATE_ERROR;
470 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the stat structure!! */
471 fstatfs(fd, &tstatfs);
472 blockSize = tstatfs.f_bsize;
476 blockSize = status->st_blksize;
478 buffer = (char *)malloc(blockSize);
480 printf("malloc failed\n");
483 while (!error && length) {
484 register int nbytes = (length>blockSize?blockSize:length);
485 nbytes = rx_Read(call, buffer, nbytes);
486 if (!nbytes) error = UPDATE_ERROR;
487 if (write(fd, buffer, nbytes) != nbytes) {
488 com_err (whoami, errno, "File system write failed!");
489 printf("File system write failed!\n");
490 error = UPDATE_ERROR;
494 if (buffer) free(buffer);
495 if (!error) fstat(fd, status);
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 */
550 int NotOnHost(filename, okhostfiles)
552 struct filestr *okhostfiles;
559 stat(filename,&status);
561 if((status.st_mode & S_IFMT) == S_IFDIR)
563 i = strlen(filename);
564 if(!strcmp(&filename[i-4],".NEW"))
567 for(tf=okhostfiles; tf; tf=tf->next) {
568 /* construct local path from canonical (wire-format) path */
569 if ((rc = ConstructLocalPath(tf->name, "/", &hostfile))) {
570 com_err(whoami, rc, "Unable to construct local path");
573 if (PathsAreEquivalent(hostfile, filename)) {
583 /* RenameNewFiles() - renames all the newly copied files from the
584 * server. Looks for files with .NEW extension and renames them
586 static int RenameNewFiles(struct filestr *modFiles)
588 char newname[MAXSIZE];
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 com_err(whoami, errcode, "Unable to construct local path");
599 strcpy(newname, fname); strcat(newname, ".NEW");
600 if (verbose >= 2) printf (" renaming %s\n", newname);
601 errcode = renamefile(newname, fname);
603 printf("could not rename %s to %s\n", newname, fname);
604 com_err (whoami, errno, "could not rename %s to %s", newname, fname);
613 /* GetFileFromUpServer() - Makes the FetchFile() call and gets the
614 * file from the upserver.
617 * -1 - Serious error. Quit right away.
618 * 1 - Error, but keep trying for the other files
620 * The file obtained is written to the localized version of the filename.NEW
621 * and the uid, gid, file mode, access and modification times will be set to
622 * the passed in values.
625 int GetFileFromUpServer(struct rx_connection *conn, /* handle for upserver */
626 char *filename, /* name of file to be fetched */
627 short uid, short gid, /* uid/gid for fetched file */
628 afs_uint32 mode, /* file mode */
629 afs_int32 atime, afs_int32 mtime) /* access/modification times */
632 struct rx_call *call;
636 struct _utimbuf utbuf;
638 struct timeval tvp[2];
640 char newfile[MAXSIZE];
642 /* construct local path from canonical (wire-format) path */
643 errcode = ConstructLocalPath(filename, "/", &lfile);
645 com_err(whoami, errcode, "Unable to construct local path");
648 strcpy(newfile, lfile);
651 strcat(newfile, ".NEW");
653 /* fetch filename into newfile from the host, since the current file
654 * is outdated. the new versions of changed files is stored as
656 call = rx_NewCall(conn);
657 errcode = FetchFile(call, filename, newfile, 0);
658 errcode = rx_EndCall(call, errcode);
661 printf("failed to fetch file %s \n",filename);
662 com_err (whoami, errcode, "fetching file");
666 /* now set the rest of the file status */
667 errcode = chmod(newfile, mode);
669 printf("could not change protection on %s to %u\n",newfile,
670 (unsigned int) mode);
671 com_err (whoami, errno,
672 "could not change protection on %s to %u",
677 utbuf.actime = atime;
678 utbuf.modtime = mtime;
679 errcode = _utime(newfile, &utbuf);
681 errcode = chown(newfile, uid, gid);
683 printf("warning: could not change uid and gid on %s to %u and %u \n",newfile, gid, uid);
684 com_err (whoami, errno,
685 "warning: could not change uid and gid on %s to %u and %u",newfile, gid, uid);
687 tvp[0].tv_sec = atime;
689 tvp[1].tv_sec = mtime;
691 errcode = utimes(newfile, tvp);
694 printf("could not change access and modify times on %s to %u %u\n",newfile, (unsigned int) atime, (unsigned int) mtime);
695 com_err (whoami, errno,
696 "could not change access and modify times on %s to %u %u",newfile, atime, mtime);