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