114921Sralph #ifndef lint 2*15611Sralph static char *sccsid = "@(#)main.c 4.9 (Berkeley) 83/11/29"; 314921Sralph #endif 414921Sralph 514921Sralph #include "defs.h" 614921Sralph 714921Sralph /* 814921Sralph * Remote distribution program. 914921Sralph */ 1014921Sralph 1114921Sralph char *distfile = "distfile"; 12*15611Sralph char tmpfile[] = "/tmp/rdistXXXXXX"; 1315113Sralph char *tmpname = &tmpfile[5]; 1414921Sralph 1514921Sralph int debug; /* debugging flag */ 1614921Sralph int nflag; /* NOP flag, just print commands without executing */ 1714921Sralph int qflag; /* Quiet. Don't print messages */ 1815218Sralph int options; /* global options */ 1914921Sralph int iamremote; /* act as remote server for transfering files */ 2014921Sralph 2114921Sralph int filec; /* number of files to update */ 2214921Sralph char **filev; /* list of files/directories to update */ 2314921Sralph FILE *fin = NULL; /* input file pointer */ 2414921Sralph int rem = 0; /* file descriptor to remote source/sink process */ 2514921Sralph char host[32]; /* host name */ 2614921Sralph int errs; /* number of errors while sending/receiving */ 2714921Sralph char user[10]; /* user's name */ 2814921Sralph char homedir[128]; /* user's home directory */ 2914921Sralph int userid; /* user's user ID */ 3015113Sralph int groupid; /* user's group ID */ 3114921Sralph 3215349Sralph struct passwd *pw; /* pointer to static area used by getpwent */ 3315349Sralph struct group *gr; /* pointer to static area used by getgrent */ 3415349Sralph 3514921Sralph int cleanup(); 3614921Sralph int lostconn(); 3714921Sralph 3814921Sralph main(argc, argv) 3914921Sralph int argc; 4014921Sralph char *argv[]; 4114921Sralph { 4214921Sralph register char *arg; 4315218Sralph int cmdargs = 0; 4414921Sralph 4514921Sralph pw = getpwuid(userid = getuid()); 4614921Sralph if (pw == NULL) { 4715113Sralph fprintf(stderr, "%s: Who are you?\n", argv[0]); 4814921Sralph exit(1); 4914921Sralph } 5014921Sralph strcpy(user, pw->pw_name); 5114921Sralph strcpy(homedir, pw->pw_dir); 5215113Sralph groupid = pw->pw_gid; 5314921Sralph gethostname(host, sizeof(host)); 5414921Sralph 5514921Sralph while (--argc > 0) { 5614921Sralph if ((arg = *++argv)[0] != '-') 5714921Sralph break; 5814921Sralph if (!strcmp(arg, "-Server")) 5914921Sralph iamremote++; 6014921Sralph else while (*++arg) 6114921Sralph switch (*arg) { 6214921Sralph case 'f': 6314921Sralph if (--argc <= 0) 6414921Sralph usage(); 6514921Sralph distfile = *++argv; 6614921Sralph if (distfile[0] == '-' && distfile[1] == '\0') 6714921Sralph fin = stdin; 6814921Sralph break; 6914921Sralph 7014921Sralph case 'd': 7115113Sralph if (--argc <= 0) 7215113Sralph usage(); 7315113Sralph define(*++argv); 7415113Sralph break; 7515113Sralph 7615113Sralph case 'D': 7714921Sralph debug++; 7814921Sralph break; 7914921Sralph 8015218Sralph case 'c': 8115218Sralph cmdargs++; 8215218Sralph break; 8315218Sralph 8414921Sralph case 'n': 85*15611Sralph if (options & VERIFY) { 86*15611Sralph printf("rdist: -n overrides -v\n"); 87*15611Sralph options &= ~VERIFY; 88*15611Sralph } 8914921Sralph nflag++; 9014921Sralph break; 9114921Sralph 9214921Sralph case 'q': 9314921Sralph qflag++; 9414921Sralph break; 9514921Sralph 9615293Sralph case 'b': 9715293Sralph options |= COMPARE; 9815293Sralph break; 9915293Sralph 100*15611Sralph case 'R': 10115271Sralph options |= REMOVE; 10215271Sralph break; 10315271Sralph 10414921Sralph case 'v': 105*15611Sralph if (nflag) { 106*15611Sralph printf("rdist: -n overrides -v\n"); 107*15611Sralph break; 108*15611Sralph } 10915218Sralph options |= VERIFY; 11014921Sralph break; 11114921Sralph 11215218Sralph case 'w': 11315218Sralph options |= WHOLE; 11415218Sralph break; 11515218Sralph 11614921Sralph case 'y': 11715218Sralph options |= YOUNGER; 11814921Sralph break; 11914921Sralph 12014921Sralph default: 12114921Sralph usage(); 12214921Sralph } 12314921Sralph } 12415113Sralph 12515113Sralph mktemp(tmpfile); 12614921Sralph signal(SIGPIPE, lostconn); 12714921Sralph if (iamremote) { 12814921Sralph server(); 12914921Sralph exit(errs); 13014921Sralph } 13114921Sralph 13214921Sralph signal(SIGHUP, cleanup); 13314921Sralph signal(SIGINT, cleanup); 13414921Sralph signal(SIGQUIT, cleanup); 13514921Sralph signal(SIGTERM, cleanup); 13614921Sralph 13715218Sralph if (cmdargs) 13815218Sralph docmdargs(argc, argv); 13915218Sralph else { 14015218Sralph filec = argc; 14115218Sralph filev = argv; 14215218Sralph if (fin == NULL && (fin = fopen(distfile, "r")) == NULL) { 14315218Sralph perror(distfile); 14415218Sralph exit(1); 14515218Sralph } 14615218Sralph yyparse(); 14715113Sralph } 14815113Sralph 14914921Sralph exit(errs); 15014921Sralph } 15114921Sralph 15214921Sralph usage() 15314921Sralph { 15415293Sralph printf("Usage: rdist [-nqbrvwyD] [-f distfile] [-d var=value] [file ...]\n"); 15515293Sralph printf("or: rdist [-nqbrvwyD] -c source [...] machine[:dest]\n"); 15614921Sralph exit(1); 15714921Sralph } 15814921Sralph 15914921Sralph /* 16015218Sralph * rcp like interface for distributing files. 16115218Sralph */ 16215218Sralph docmdargs(nargs, args) 16315218Sralph int nargs; 16415218Sralph char *args[]; 16515218Sralph { 16615218Sralph struct block *bp, *files, *hosts, *cmds, *prev; 16715218Sralph int i; 16815218Sralph char *pos, dest[BUFSIZ]; 16915218Sralph 17015218Sralph if (nargs < 2) 17115218Sralph usage(); 17215218Sralph 17315218Sralph prev = NULL; 17415218Sralph bp = files = ALLOC(block); 17515218Sralph for (i = 0; i < nargs - 1; bp = ALLOC(block), i++) { 17615218Sralph bp->b_type = NAME; 17715218Sralph bp->b_name = args[i]; 17815218Sralph if (prev != NULL) 17915218Sralph prev->b_next = bp; 18015218Sralph bp->b_next = bp->b_args = NULL; 18115218Sralph prev = bp; 18215218Sralph } 18315218Sralph 18415218Sralph hosts = ALLOC(block); 18515218Sralph hosts->b_type = NAME; 18615218Sralph hosts->b_name = args[i]; 18715218Sralph hosts->b_name = args[i]; 18815218Sralph hosts->b_next = hosts->b_args = NULL; 18915218Sralph if ((pos = index(hosts->b_name, ':')) != NULL) { 19015218Sralph *pos++ = '\0'; 19115218Sralph strcpy(dest, pos); 19215218Sralph } else 19315218Sralph dest[0] = '\0'; 19415218Sralph 195*15611Sralph hosts = expand(hosts, 1); 19615218Sralph 19715218Sralph if (dest[0] == '\0') 19815218Sralph cmds = NULL; 19915218Sralph else { 20015218Sralph cmds = ALLOC(block); 20115218Sralph cmds->b_type = INSTALL; 20215218Sralph cmds->b_options = options; 20315218Sralph cmds->b_name = dest; 20415218Sralph cmds->b_next = cmds->b_args = NULL; 20515218Sralph } 20615218Sralph 20715218Sralph if (debug) { 20815218Sralph printf("docmdargs()\nfiles = "); 20915218Sralph prnames(files); 21015218Sralph printf("hosts = "); 21115218Sralph prnames(hosts); 21215218Sralph } 21315218Sralph dohcmds(files, hosts, cmds); 21415218Sralph } 21515218Sralph 21615218Sralph /* 21714921Sralph * Remove temporary files and do any cleanup operations before exiting. 21814921Sralph */ 21914921Sralph cleanup() 22014921Sralph { 221*15611Sralph (void) unlink(tmpfile); 22214921Sralph exit(1); 22314921Sralph } 22414921Sralph 22514921Sralph /* 22614921Sralph * Print a list of NAME blocks (mostly for debugging). 22714921Sralph */ 22814921Sralph prnames(bp) 22914921Sralph register struct block *bp; 23014921Sralph { 23114921Sralph printf("( "); 23214921Sralph while (bp != NULL) { 23314921Sralph printf("%s ", bp->b_name); 23414921Sralph bp = bp->b_next; 23514921Sralph } 23614921Sralph printf(")\n"); 23714921Sralph } 23814921Sralph 23914921Sralph /*VARARGS*/ 24014921Sralph warn(fmt, a1, a2,a3) 24114921Sralph char *fmt; 24214921Sralph { 24314921Sralph extern int yylineno; 24414921Sralph 24514921Sralph fprintf(stderr, "rdist: line %d: Warning: ", yylineno); 24614921Sralph fprintf(stderr, fmt, a1, a2, a3); 24714921Sralph fputc('\n', stderr); 24814921Sralph } 249