114921Sralph #ifndef lint 2*15271Sralph static char *sccsid = "@(#)main.c 4.6 (Berkeley) 83/10/20"; 314921Sralph #endif 414921Sralph 514921Sralph #include "defs.h" 614921Sralph 714921Sralph /* 814921Sralph * Remote distribution program. 914921Sralph */ 1014921Sralph 1114921Sralph char *distfile = "distfile"; 1215113Sralph char tmpfile[] = "/tmp/rdistAXXXXXX"; 1315113Sralph char *tmpname = &tmpfile[5]; 1415113Sralph char *tmpinc = &tmpfile[10]; 1514921Sralph 1614921Sralph int debug; /* debugging flag */ 1714921Sralph int nflag; /* NOP flag, just print commands without executing */ 1814921Sralph int qflag; /* Quiet. Don't print messages */ 1915218Sralph int options; /* global options */ 2014921Sralph int iamremote; /* act as remote server for transfering files */ 2114921Sralph 2214921Sralph int filec; /* number of files to update */ 2314921Sralph char **filev; /* list of files/directories to update */ 2414921Sralph FILE *fin = NULL; /* input file pointer */ 2514921Sralph int rem = 0; /* file descriptor to remote source/sink process */ 2614921Sralph char host[32]; /* host name */ 2714921Sralph int errs; /* number of errors while sending/receiving */ 2814921Sralph char user[10]; /* user's name */ 2914921Sralph char homedir[128]; /* user's home directory */ 3014921Sralph int userid; /* user's user ID */ 3115113Sralph int groupid; /* user's group ID */ 3214921Sralph 3314921Sralph int cleanup(); 3414921Sralph int lostconn(); 3514921Sralph 3614921Sralph main(argc, argv) 3714921Sralph int argc; 3814921Sralph char *argv[]; 3914921Sralph { 4014921Sralph register char *arg; 4114921Sralph register struct passwd *pw; 4215218Sralph int cmdargs = 0; 4314921Sralph 4414921Sralph pw = getpwuid(userid = getuid()); 4514921Sralph if (pw == NULL) { 4615113Sralph fprintf(stderr, "%s: Who are you?\n", argv[0]); 4714921Sralph exit(1); 4814921Sralph } 4914921Sralph strcpy(user, pw->pw_name); 5014921Sralph strcpy(homedir, pw->pw_dir); 5115113Sralph groupid = pw->pw_gid; 5214921Sralph gethostname(host, sizeof(host)); 5314921Sralph 5414921Sralph while (--argc > 0) { 5514921Sralph if ((arg = *++argv)[0] != '-') 5614921Sralph break; 5714921Sralph if (!strcmp(arg, "-Server")) 5814921Sralph iamremote++; 5914921Sralph else while (*++arg) 6014921Sralph switch (*arg) { 6114921Sralph case 'f': 6214921Sralph if (--argc <= 0) 6314921Sralph usage(); 6414921Sralph distfile = *++argv; 6514921Sralph if (distfile[0] == '-' && distfile[1] == '\0') 6614921Sralph fin = stdin; 6714921Sralph break; 6814921Sralph 6914921Sralph case 'd': 7015113Sralph if (--argc <= 0) 7115113Sralph usage(); 7215113Sralph define(*++argv); 7315113Sralph break; 7415113Sralph 7515113Sralph case 'D': 7614921Sralph debug++; 7714921Sralph break; 7814921Sralph 7915218Sralph case 'c': 8015218Sralph cmdargs++; 8115218Sralph break; 8215218Sralph 8314921Sralph case 'n': 8414921Sralph nflag++; 8514921Sralph break; 8614921Sralph 8714921Sralph case 'q': 8814921Sralph qflag++; 8914921Sralph break; 9014921Sralph 91*15271Sralph case 'r': 92*15271Sralph options |= REMOVE; 93*15271Sralph break; 94*15271Sralph 9514921Sralph case 'v': 9615218Sralph options |= VERIFY; 9714921Sralph break; 9814921Sralph 9915218Sralph case 'w': 10015218Sralph options |= WHOLE; 10115218Sralph break; 10215218Sralph 10314921Sralph case 'y': 10415218Sralph options |= YOUNGER; 10514921Sralph break; 10614921Sralph 10714921Sralph default: 10814921Sralph usage(); 10914921Sralph } 11014921Sralph } 11115113Sralph 11215113Sralph mktemp(tmpfile); 11314921Sralph signal(SIGPIPE, lostconn); 11414921Sralph if (iamremote) { 11514921Sralph server(); 11614921Sralph exit(errs); 11714921Sralph } 11814921Sralph 11914921Sralph signal(SIGHUP, cleanup); 12014921Sralph signal(SIGINT, cleanup); 12114921Sralph signal(SIGQUIT, cleanup); 12214921Sralph signal(SIGTERM, cleanup); 12314921Sralph 12415218Sralph if (cmdargs) 12515218Sralph docmdargs(argc, argv); 12615218Sralph else { 12715218Sralph filec = argc; 12815218Sralph filev = argv; 12915218Sralph if (fin == NULL && (fin = fopen(distfile, "r")) == NULL) { 13015218Sralph perror(distfile); 13115218Sralph exit(1); 13215218Sralph } 13315218Sralph yyparse(); 13415113Sralph } 13515113Sralph 13614921Sralph exit(errs); 13714921Sralph } 13814921Sralph 13914921Sralph usage() 14014921Sralph { 14115218Sralph printf("Usage: rdist [-nqvwyD] [-f distfile] [-d var=value] [file ...]\n"); 14215218Sralph printf("or: rdist [-nqvwyD] -c source [...] machine[:dest]\n"); 14314921Sralph exit(1); 14414921Sralph } 14514921Sralph 14614921Sralph /* 14715218Sralph * rcp like interface for distributing files. 14815218Sralph */ 14915218Sralph docmdargs(nargs, args) 15015218Sralph int nargs; 15115218Sralph char *args[]; 15215218Sralph { 15315218Sralph struct block *bp, *files, *hosts, *cmds, *prev; 15415218Sralph int i; 15515218Sralph char *pos, dest[BUFSIZ]; 15615218Sralph 15715218Sralph if (nargs < 2) 15815218Sralph usage(); 15915218Sralph 16015218Sralph prev = NULL; 16115218Sralph bp = files = ALLOC(block); 16215218Sralph for (i = 0; i < nargs - 1; bp = ALLOC(block), i++) { 16315218Sralph bp->b_type = NAME; 16415218Sralph bp->b_name = args[i]; 16515218Sralph if (prev != NULL) 16615218Sralph prev->b_next = bp; 16715218Sralph bp->b_next = bp->b_args = NULL; 16815218Sralph prev = bp; 16915218Sralph } 17015218Sralph 17115218Sralph hosts = ALLOC(block); 17215218Sralph hosts->b_type = NAME; 17315218Sralph hosts->b_name = args[i]; 17415218Sralph hosts->b_name = args[i]; 17515218Sralph hosts->b_next = hosts->b_args = NULL; 17615218Sralph if ((pos = index(hosts->b_name, ':')) != NULL) { 17715218Sralph *pos++ = '\0'; 17815218Sralph strcpy(dest, pos); 17915218Sralph } else 18015218Sralph dest[0] = '\0'; 18115218Sralph 18215218Sralph hosts = expand(hosts, 0); 18315218Sralph 18415218Sralph if (dest[0] == '\0') 18515218Sralph cmds = NULL; 18615218Sralph else { 18715218Sralph cmds = ALLOC(block); 18815218Sralph cmds->b_type = INSTALL; 18915218Sralph cmds->b_options = options; 19015218Sralph cmds->b_name = dest; 19115218Sralph cmds->b_next = cmds->b_args = NULL; 19215218Sralph } 19315218Sralph 19415218Sralph if (debug) { 19515218Sralph printf("docmdargs()\nfiles = "); 19615218Sralph prnames(files); 19715218Sralph printf("hosts = "); 19815218Sralph prnames(hosts); 19915218Sralph } 20015218Sralph dohcmds(files, hosts, cmds); 20115218Sralph } 20215218Sralph 20315218Sralph /* 20414921Sralph * Remove temporary files and do any cleanup operations before exiting. 20514921Sralph */ 20614921Sralph cleanup() 20714921Sralph { 20815218Sralph do { 20915218Sralph (void) unlink(tmpfile); 21015218Sralph (*tmpinc)--; 21115218Sralph } while (*tmpinc >= 'A'); 21214921Sralph exit(1); 21314921Sralph } 21414921Sralph 21514921Sralph /* 21614921Sralph * Print a list of NAME blocks (mostly for debugging). 21714921Sralph */ 21814921Sralph prnames(bp) 21914921Sralph register struct block *bp; 22014921Sralph { 22114921Sralph printf("( "); 22214921Sralph while (bp != NULL) { 22314921Sralph printf("%s ", bp->b_name); 22414921Sralph bp = bp->b_next; 22514921Sralph } 22614921Sralph printf(")\n"); 22714921Sralph } 22814921Sralph 22914921Sralph /*VARARGS*/ 23014921Sralph warn(fmt, a1, a2,a3) 23114921Sralph char *fmt; 23214921Sralph { 23314921Sralph extern int yylineno; 23414921Sralph 23514921Sralph fprintf(stderr, "rdist: line %d: Warning: ", yylineno); 23614921Sralph fprintf(stderr, fmt, a1, a2, a3); 23714921Sralph fputc('\n', stderr); 23814921Sralph } 239