| 1 | #ifndef lint |
| 2 | static char *sccsid = "@(#)main.c 4.5 (Berkeley) 83/10/12"; |
| 3 | #endif |
| 4 | |
| 5 | #include "defs.h" |
| 6 | |
| 7 | /* |
| 8 | * Remote distribution program. |
| 9 | */ |
| 10 | |
| 11 | char *distfile = "distfile"; |
| 12 | char tmpfile[] = "/tmp/rdistAXXXXXX"; |
| 13 | char *tmpname = &tmpfile[5]; |
| 14 | char *tmpinc = &tmpfile[10]; |
| 15 | |
| 16 | int debug; /* debugging flag */ |
| 17 | int nflag; /* NOP flag, just print commands without executing */ |
| 18 | int qflag; /* Quiet. Don't print messages */ |
| 19 | int options; /* global options */ |
| 20 | int iamremote; /* act as remote server for transfering files */ |
| 21 | |
| 22 | int filec; /* number of files to update */ |
| 23 | char **filev; /* list of files/directories to update */ |
| 24 | FILE *fin = NULL; /* input file pointer */ |
| 25 | int rem = 0; /* file descriptor to remote source/sink process */ |
| 26 | char host[32]; /* host name */ |
| 27 | int errs; /* number of errors while sending/receiving */ |
| 28 | char user[10]; /* user's name */ |
| 29 | char homedir[128]; /* user's home directory */ |
| 30 | int userid; /* user's user ID */ |
| 31 | int groupid; /* user's group ID */ |
| 32 | |
| 33 | int cleanup(); |
| 34 | int lostconn(); |
| 35 | |
| 36 | main(argc, argv) |
| 37 | int argc; |
| 38 | char *argv[]; |
| 39 | { |
| 40 | register char *arg; |
| 41 | register struct passwd *pw; |
| 42 | int cmdargs = 0; |
| 43 | |
| 44 | pw = getpwuid(userid = getuid()); |
| 45 | if (pw == NULL) { |
| 46 | fprintf(stderr, "%s: Who are you?\n", argv[0]); |
| 47 | exit(1); |
| 48 | } |
| 49 | strcpy(user, pw->pw_name); |
| 50 | strcpy(homedir, pw->pw_dir); |
| 51 | groupid = pw->pw_gid; |
| 52 | gethostname(host, sizeof(host)); |
| 53 | |
| 54 | while (--argc > 0) { |
| 55 | if ((arg = *++argv)[0] != '-') |
| 56 | break; |
| 57 | if (!strcmp(arg, "-Server")) |
| 58 | iamremote++; |
| 59 | else while (*++arg) |
| 60 | switch (*arg) { |
| 61 | case 'f': |
| 62 | if (--argc <= 0) |
| 63 | usage(); |
| 64 | distfile = *++argv; |
| 65 | if (distfile[0] == '-' && distfile[1] == '\0') |
| 66 | fin = stdin; |
| 67 | break; |
| 68 | |
| 69 | case 'd': |
| 70 | if (--argc <= 0) |
| 71 | usage(); |
| 72 | define(*++argv); |
| 73 | break; |
| 74 | |
| 75 | case 'D': |
| 76 | debug++; |
| 77 | break; |
| 78 | |
| 79 | case 'c': |
| 80 | cmdargs++; |
| 81 | break; |
| 82 | |
| 83 | case 'n': |
| 84 | nflag++; |
| 85 | break; |
| 86 | |
| 87 | case 'q': |
| 88 | qflag++; |
| 89 | break; |
| 90 | |
| 91 | case 'v': |
| 92 | options |= VERIFY; |
| 93 | break; |
| 94 | |
| 95 | case 'w': |
| 96 | options |= WHOLE; |
| 97 | break; |
| 98 | |
| 99 | case 'y': |
| 100 | options |= YOUNGER; |
| 101 | break; |
| 102 | |
| 103 | default: |
| 104 | usage(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | mktemp(tmpfile); |
| 109 | signal(SIGPIPE, lostconn); |
| 110 | if (iamremote) { |
| 111 | server(); |
| 112 | exit(errs); |
| 113 | } |
| 114 | |
| 115 | signal(SIGHUP, cleanup); |
| 116 | signal(SIGINT, cleanup); |
| 117 | signal(SIGQUIT, cleanup); |
| 118 | signal(SIGTERM, cleanup); |
| 119 | |
| 120 | if (cmdargs) |
| 121 | docmdargs(argc, argv); |
| 122 | else { |
| 123 | filec = argc; |
| 124 | filev = argv; |
| 125 | if (fin == NULL && (fin = fopen(distfile, "r")) == NULL) { |
| 126 | perror(distfile); |
| 127 | exit(1); |
| 128 | } |
| 129 | yyparse(); |
| 130 | } |
| 131 | |
| 132 | exit(errs); |
| 133 | } |
| 134 | |
| 135 | usage() |
| 136 | { |
| 137 | printf("Usage: rdist [-nqvwyD] [-f distfile] [-d var=value] [file ...]\n"); |
| 138 | printf("or: rdist [-nqvwyD] -c source [...] machine[:dest]\n"); |
| 139 | exit(1); |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * rcp like interface for distributing files. |
| 144 | */ |
| 145 | docmdargs(nargs, args) |
| 146 | int nargs; |
| 147 | char *args[]; |
| 148 | { |
| 149 | struct block *bp, *files, *hosts, *cmds, *prev; |
| 150 | int i; |
| 151 | char *pos, dest[BUFSIZ]; |
| 152 | |
| 153 | if (nargs < 2) |
| 154 | usage(); |
| 155 | |
| 156 | prev = NULL; |
| 157 | bp = files = ALLOC(block); |
| 158 | for (i = 0; i < nargs - 1; bp = ALLOC(block), i++) { |
| 159 | bp->b_type = NAME; |
| 160 | bp->b_name = args[i]; |
| 161 | if (prev != NULL) |
| 162 | prev->b_next = bp; |
| 163 | bp->b_next = bp->b_args = NULL; |
| 164 | prev = bp; |
| 165 | } |
| 166 | |
| 167 | hosts = ALLOC(block); |
| 168 | hosts->b_type = NAME; |
| 169 | hosts->b_name = args[i]; |
| 170 | hosts->b_name = args[i]; |
| 171 | hosts->b_next = hosts->b_args = NULL; |
| 172 | if ((pos = index(hosts->b_name, ':')) != NULL) { |
| 173 | *pos++ = '\0'; |
| 174 | strcpy(dest, pos); |
| 175 | } else |
| 176 | dest[0] = '\0'; |
| 177 | |
| 178 | hosts = expand(hosts, 0); |
| 179 | |
| 180 | if (dest[0] == '\0') |
| 181 | cmds = NULL; |
| 182 | else { |
| 183 | cmds = ALLOC(block); |
| 184 | cmds->b_type = INSTALL; |
| 185 | cmds->b_options = options; |
| 186 | cmds->b_name = dest; |
| 187 | cmds->b_next = cmds->b_args = NULL; |
| 188 | } |
| 189 | |
| 190 | if (debug) { |
| 191 | printf("docmdargs()\nfiles = "); |
| 192 | prnames(files); |
| 193 | printf("hosts = "); |
| 194 | prnames(hosts); |
| 195 | } |
| 196 | dohcmds(files, hosts, cmds); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Remove temporary files and do any cleanup operations before exiting. |
| 201 | */ |
| 202 | cleanup() |
| 203 | { |
| 204 | do { |
| 205 | (void) unlink(tmpfile); |
| 206 | (*tmpinc)--; |
| 207 | } while (*tmpinc >= 'A'); |
| 208 | exit(1); |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Print a list of NAME blocks (mostly for debugging). |
| 213 | */ |
| 214 | prnames(bp) |
| 215 | register struct block *bp; |
| 216 | { |
| 217 | printf("( "); |
| 218 | while (bp != NULL) { |
| 219 | printf("%s ", bp->b_name); |
| 220 | bp = bp->b_next; |
| 221 | } |
| 222 | printf(")\n"); |
| 223 | } |
| 224 | |
| 225 | /*VARARGS*/ |
| 226 | warn(fmt, a1, a2,a3) |
| 227 | char *fmt; |
| 228 | { |
| 229 | extern int yylineno; |
| 230 | |
| 231 | fprintf(stderr, "rdist: line %d: Warning: ", yylineno); |
| 232 | fprintf(stderr, fmt, a1, a2, a3); |
| 233 | fputc('\n', stderr); |
| 234 | } |