add some sanity checks
[unix-history] / usr / src / usr.bin / rdist / docmd.c
CommitLineData
b6b5501c 1#ifndef lint
82572cb6 2static char *sccsid = "@(#)docmd.c 4.2 (Berkeley) 83/09/27";
b6b5501c
RC
3#endif
4
5#include "defs.h"
6
7FILE *lfp; /* log file for recording files updated */
8
9/*
82572cb6 10 * Process commands for sending files to other machines.
b6b5501c 11 */
82572cb6 12dohcmds(files, hosts, cmds)
b6b5501c
RC
13 struct block *files, *hosts, *cmds;
14{
15 register struct block *h, *f, *c;
16 register char *cp, **cpp;
82572cb6 17 int n, ddir;
b6b5501c
RC
18
19 if (debug)
82572cb6 20 printf("dohcmds(%x, %x, %x)\n", files, hosts, cmds);
b6b5501c 21
82572cb6
RC
22 files = expand(files, 0);
23 hosts = expand(hosts, 1);
b6b5501c
RC
24 if (files == NULL)
25 fatal("no files to be updated\n");
26 if (hosts == NULL)
27 fatal("empty list of hosts to be updated\n");
28 except = cmds;
82572cb6 29 ddir = files->b_next != NULL;
b6b5501c
RC
30
31 for (h = hosts; h != NULL; h = h->b_next) {
82572cb6
RC
32 if (!qflag)
33 printf("updating host %s\n", h->b_name);
b6b5501c 34 if (!nflag) {
82572cb6
RC
35 if (!makeconn(h->b_name))
36 continue;
b6b5501c
RC
37 if ((lfp = fopen(tmpfile, "w")) == NULL) {
38 fatal("cannot open %s\n", tmpfile);
39 exit(1);
40 }
b6b5501c
RC
41 }
42 for (f = files; f != NULL; f = f->b_next) {
43 if (filec) {
44 for (cpp = filev; *cpp; cpp++)
45 if (!strcmp(f->b_name, *cpp))
46 goto found;
82572cb6
RC
47 if (!nflag) {
48 (void) fclose(lfp);
49 }
b6b5501c
RC
50 continue;
51 }
52 found:
53 n = 0;
54 for (c = cmds; c != NULL; c = c->b_next)
55 if (c->b_type == INSTALL) {
82572cb6 56 install(f->b_name, c->b_name, ddir, 0);
b6b5501c
RC
57 n++;
58 } else if (c->b_type == VERIFY) {
82572cb6 59 install(f->b_name, c->b_name, ddir, 1);
b6b5501c
RC
60 n++;
61 }
62 if (n == 0)
82572cb6 63 install(f->b_name, f->b_name, 0, 0);
b6b5501c
RC
64 }
65 if (!nflag) {
82572cb6
RC
66 /* signal end of connection */
67 (void) write(rem, "\2\n", 2);
b6b5501c 68 (void) close(rem);
82572cb6 69 (void) fclose(lfp);
b6b5501c
RC
70 }
71 for (c = cmds; c != NULL; c = c->b_next)
72 if (c->b_type == NOTIFY)
82572cb6 73 notify(tmpfile, h->b_name, c->b_args);
b6b5501c
RC
74 }
75 if (!nflag)
76 (void) unlink(tmpfile);
77}
78
79/*
80 * Create a connection to the rdist server on the machine rhost.
81 */
82makeconn(rhost)
83 char *rhost;
84{
85 register char *ruser;
86 extern char user[];
87
88 (void) sprintf(buf, "/usr/local/rdist -Server%s%s%s%s%s",
89 vflag ? " -v" : "", qflag ? " -q" : "", nflag ? " -n" : "",
82572cb6 90 yflag ? " -y" : "", debug ? " -D" : "");
b6b5501c
RC
91
92 ruser = rindex(rhost, '.');
93 if (ruser != NULL) {
94 *ruser++ = '\0';
95 if (!okname(ruser))
96 return(0);
97 } else
98 ruser = user;
99
100 if (debug) {
101 printf("makeconn(%s)\n", rhost);
102 printf("luser = %s, ruser = %s\n", user, ruser);
103 printf("buf = %s\n", buf);
104 }
105
82572cb6 106 fflush(stdout);
b6b5501c
RC
107 rem = rcmd(&rhost, IPPORT_CMDSERVER, user, ruser, buf, 0);
108 if (rem < 0)
109 return(0);
110 if (response() < 0)
111 return(0);
112 return(1);
113}
114
82572cb6
RC
115extern char target[], *tp;
116
b6b5501c
RC
117/*
118 * Update the file(s) if they are different.
82572cb6
RC
119 * destdir = 1 if destination should be a directory
120 * (i.e., more than one source is being copied to the same destination).
b6b5501c 121 */
82572cb6 122install(src, dest, destdir, verify)
b6b5501c 123 char *src, *dest;
82572cb6 124 int destdir, verify;
b6b5501c 125{
82572cb6
RC
126 if (exclude(src))
127 return;
b6b5501c 128
82572cb6 129 if (nflag) {
b6b5501c 130 printf("%s %s %s\n", verify ? "verify" : "install", src, dest);
b6b5501c 131 return;
82572cb6 132 }
b6b5501c
RC
133 /*
134 * Pass the destination file/directory name to remote.
135 */
82572cb6 136 (void) sprintf(buf, "%c%s\n", destdir ? 'T' : 't', dest);
b6b5501c
RC
137 if (debug)
138 printf("buf = %s", buf);
139 (void) write(rem, buf, strlen(buf));
140 tp = NULL;
82572cb6
RC
141 sendf(src, verify);
142}
143
144struct tstamp {
145 time_t lastmod;
146 FILE *tfp;
147} ts[NSTAMPS];
148
149int nstamps;
150
151/*
152 * Process commands for comparing files to time stamp files.
153 */
154dofcmds(files, stamps, cmds)
155 struct block *files, *stamps, *cmds;
156{
157 register struct block *b;
158 register struct tstamp *t;
159 register char **cpp;
160 struct stat stb;
161 extern char *tmpinc;
162 int n;
163
164 if (debug)
165 printf("dofcmds()\n");
166
167 files = expand(files, 0);
168 stamps = expand(stamps, 1);
169 if (files == NULL)
170 fatal("no files to be updated\n");
171 if (stamps == NULL)
172 fatal("empty time stamp file list\n");
173 except = cmds;
174
175 t = ts;
176 nstamps = 0;
177 for (b = stamps; b != NULL; b = b->b_next) {
178 if (stat(b->b_name, &stb) < 0) {
179 error("%s: %s\n", b->b_name, sys_errlist[errno]);
180 continue;
181 }
182 if (++nstamps > NSTAMPS)
183 fatal("too many time stamp files in one command\n");
184 if (debug)
185 printf("%s: %d\n", b->b_name, stb.st_mtime);
186 t->lastmod = stb.st_mtime;
187 if (!nflag && !vflag) {
188 if ((t->tfp = fopen(tmpfile, "w")) == NULL)
189 error("%s: %s\n", b->b_name, sys_errlist[errno]);
190 (*tmpinc)++;
191 } else
192 t->tfp = NULL;
193 t++;
194 }
195 for (b = files; b != NULL; b = b->b_next) {
196 if (filec) {
197 for (cpp = filev; *cpp; cpp++)
198 if (!strcmp(b->b_name, *cpp))
199 goto found;
200 continue;
201 }
202 found:
203 tp = NULL;
204 cmptime(b->b_name);
205 }
206 if (!nflag && !vflag)
207 for (t = ts; t < &ts[n]; t++)
208 if (t->tfp != NULL)
209 (void) fclose(t->tfp);
210 *tmpinc = 'A';
211 while (n--) {
212 for (b = cmds; b != NULL; b = b->b_next)
213 if (b->b_type == NOTIFY)
214 notify(tmpfile, NULL, b->b_args);
215 if (!nflag && !vflag)
216 (void) unlink(tmpfile);
217 (*tmpinc)++;
218 }
219}
220
221/*
222 * Compare the mtime of file to the list of time stamps.
223 */
224cmptime(name)
225 char *name;
226{
227 register struct tstamp *t;
228 struct stat stb;
229
230 if (debug)
231 printf("cmptime(%s)\n", name);
232
233 if (exclude(name))
234 return;
235
236 /*
237 * first time cmptime() is called?
238 */
239 if (tp == NULL) {
240 exptilde(target, name);
241 tp = name = target;
242 while (*tp)
243 tp++;
244 }
245 if (access(name, 4) < 0 || stat(name, &stb) < 0) {
246 error("%s: %s\n", name, sys_errlist[errno]);
247 return;
248 }
249
250 switch (stb.st_mode & S_IFMT) {
251 case S_IFREG:
252 break;
253
254 case S_IFDIR:
255 rcmptime(&stb);
256 return;
257
258 default:
259 error("%s: not a plain file\n", name);
260 return;
261 }
262
263 for (t = ts; t < &ts[nstamps]; t++) {
264 if (stb.st_mtime <= t->lastmod)
265 return;
266 log(t->tfp, "updating: %s\n", name);
267 }
268}
269
270rcmptime(st)
271 struct stat *st;
272{
273 register DIR *d;
274 register struct direct *dp;
275 register char *cp;
276 char *otp;
277 int len;
278
279 if (debug)
280 printf("rcmptime(%x)\n", st);
281
282 if ((d = opendir(target)) == NULL) {
283 error("%s: %s\n", target, sys_errlist[errno]);
284 return;
285 }
286 otp = tp;
287 len = tp - target;
288 while (dp = readdir(d)) {
289 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
290 continue;
291 if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
292 error("%s/%s: Name too long\n", target, dp->d_name);
293 continue;
294 }
295 tp = otp;
296 *tp++ = '/';
297 cp = dp->d_name;
298 while (*tp++ = *cp++)
299 ;
300 tp--;
301 cmptime(target);
302 }
303 closedir(d);
304 tp = otp;
305 *tp = '\0';
b6b5501c
RC
306}
307
308/*
309 * Notify the list of people the changes that were made.
310 */
82572cb6
RC
311notify(file, host, to)
312 char *file, *host;
b6b5501c
RC
313 register struct block *to;
314{
315 register int fd, len;
316 FILE *pf, *popen();
317 struct stat stb;
318
319 if (vflag)
320 return;
321 if (!qflag) {
82572cb6
RC
322 printf("notify ");
323 if (host)
324 printf("@%s ", host);
b6b5501c
RC
325 prnames(to);
326 }
327 if (nflag)
328 return;
329
82572cb6
RC
330 if ((fd = open(file, 0)) < 0) {
331 error("%s: %s\n", file, sys_errlist[errno]);
332 return;
333 }
334 if (fstat(fd, &stb) < 0) {
335 error("%s: %s\n", file, sys_errlist[errno]);
336 (void) close(fd);
337 return;
338 }
339 if (stb.st_size == 0) {
340 (void) close(fd);
b6b5501c
RC
341 return;
342 }
343 /*
344 * Create a pipe to mailling program.
345 */
346 pf = popen(MAILCMD, "w");
347 if (pf == NULL)
348 fatal("notify: \"%s\" failed\n", MAILCMD);
349 /*
350 * Output the proper header information.
351 */
352 fprintf(pf, "From: rdist (Remote distribution program)\n");
353 fprintf(pf, "To:");
354 while (to != NULL) {
82572cb6
RC
355 if (!any('@', to->b_name))
356 fprintf(pf, " %s@%s", to->b_name, host);
357 else
358 fprintf(pf, " %s", to->b_name);
b6b5501c
RC
359 to = to->b_next;
360 }
361 putc('\n', pf);
362 fprintf(pf, "Subject: files updated by rdist\n");
363 putc('\n', pf);
364
365 while ((len = read(fd, buf, BUFSIZ)) > 0)
366 (void) fwrite(buf, 1, len, pf);
367 (void) close(fd);
368 (void) pclose(pf);
369}
370
371struct block *except; /* list of files to exclude */
372
373/*
374 * Return true if name is in list.
375 */
376exclude(file)
377 char *file;
378{
379 register struct block *b, *c;
380
381 for (c = except; c != NULL; c = c->b_next) {
382 if (c->b_type != EXCEPT)
383 continue;
384 for (b = c->b_args; b != NULL; b = b->b_next)
385 if (!strcmp(file, b->b_name))
386 return(1);
387 }
388 return(0);
389}
390
391okname(name)
392 register char *name;
393{
394 register char *cp = name;
395 register int c;
396
397 do {
398 c = *cp;
399 if (c & 0200)
400 goto bad;
401 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
402 goto bad;
403 cp++;
404 } while (*cp);
405 return(1);
406bad:
407 error("invalid user name %s\n", name);
408 return(0);
409}
410
411char *
412colon(cp)
413 register char *cp;
414{
415
416 while (*cp) {
417 if (*cp == ':')
418 return(cp);
419 if (*cp == '/')
420 return(0);
421 cp++;
422 }
423 return(0);
424}