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