docmd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. #ifndef lint
  2. static char *sccsid = "@(#)docmd.c 4.21 (Berkeley) 84/06/28";
  3. #endif
  4. #include "defs.h"
  5. #include <setjmp.h>
  6. FILE *lfp; /* log file for recording files updated */
  7. struct subcmd *subcmds; /* list of sub-commands for current cmd */
  8. jmp_buf env;
  9. int cleanup();
  10. int lostconn();
  11. /*
  12. * Do the commands in cmds (initialized by yyparse).
  13. */
  14. docmds(argc, argv)
  15. int argc;
  16. char **argv;
  17. {
  18. register struct cmd *c;
  19. register struct namelist *f;
  20. register char **cpp;
  21. extern struct cmd *cmds;
  22. signal(SIGHUP, cleanup);
  23. signal(SIGINT, cleanup);
  24. signal(SIGQUIT, cleanup);
  25. signal(SIGTERM, cleanup);
  26. for (c = cmds; c != NULL; c = c->c_next) {
  27. if (argc) {
  28. for (cpp = argv; *cpp; cpp++) {
  29. if (c->c_label != NULL &&
  30. strcmp(c->c_label, *cpp) == 0) {
  31. cpp = NULL;
  32. goto found;
  33. }
  34. for (f = c->c_files; f != NULL; f = f->n_next)
  35. if (strcmp(f->n_name, *cpp) == 0)
  36. goto found;
  37. }
  38. continue;
  39. } else
  40. cpp = NULL;
  41. found:
  42. switch (c->c_type) {
  43. case ARROW:
  44. doarrow(cpp, c->c_files, c->c_name, c->c_cmds);
  45. break;
  46. case DCOLON:
  47. dodcolon(cpp, c->c_files, c->c_name, c->c_cmds);
  48. break;
  49. default:
  50. fatal("illegal command type %d\n", c->c_type);
  51. }
  52. }
  53. closeconn();
  54. }
  55. /*
  56. * Process commands for sending files to other machines.
  57. */
  58. doarrow(filev, files, rhost, cmds)
  59. char **filev;
  60. struct namelist *files;
  61. char *rhost;
  62. struct subcmd *cmds;
  63. {
  64. register struct namelist *f;
  65. register struct subcmd *sc;
  66. register char **cpp;
  67. int n, ddir, opts = options;
  68. if (debug)
  69. printf("doarrow(%x, %s, %x)\n", files, rhost, cmds);
  70. if (files == NULL) {
  71. error("no files to be updated\n");
  72. return;
  73. }
  74. subcmds = cmds;
  75. ddir = files->n_next != NULL; /* destination is a directory */
  76. if (nflag)
  77. printf("updating host %s\n", rhost);
  78. else {
  79. if (setjmp(env))
  80. goto done;
  81. signal(SIGPIPE, lostconn);
  82. if (!makeconn(rhost))
  83. return;
  84. if ((lfp = fopen(tmpfile, "w")) == NULL) {
  85. fatal("cannot open %s\n", tmpfile);
  86. exit(1);
  87. }
  88. }
  89. for (f = files; f != NULL; f = f->n_next) {
  90. if (filev) {
  91. for (cpp = filev; *cpp; cpp++)
  92. if (strcmp(f->n_name, *cpp) == 0)
  93. goto found;
  94. if (!nflag)
  95. (void) fclose(lfp);
  96. continue;
  97. }
  98. found:
  99. n = 0;
  100. for (sc = cmds; sc != NULL; sc = sc->sc_next) {
  101. if (sc->sc_type != INSTALL)
  102. continue;
  103. n++;
  104. install(f->n_name, sc->sc_name,
  105. sc->sc_name == NULL ? 0 : ddir, sc->sc_options);
  106. opts = sc->sc_options;
  107. }
  108. if (n == 0)
  109. install(f->n_name, NULL, 0, options);
  110. }
  111. done:
  112. if (!nflag) {
  113. (void) signal(SIGPIPE, cleanup);
  114. (void) fclose(lfp);
  115. lfp = NULL;
  116. }
  117. /*++23-Nov-84++*/
  118. /*
  119. * scan for make commands which have to be executed.
  120. */
  121. for (sc = cmds; sc != NULL; sc = sc->sc_next)
  122. if ( sc->sc_type == DEPEND )
  123. dependcmd(sc->sc_name, sc->sc_options, sc->sc_args);
  124. /*--23-Nov-84--*/
  125. for (sc = cmds; sc != NULL; sc = sc->sc_next)
  126. if (sc->sc_type == NOTIFY)
  127. notify(tmpfile, rhost, sc->sc_args, 0);
  128. if (!nflag) {
  129. (void) unlink(tmpfile);
  130. for (; ihead != NULL; ihead = ihead->nextp) {
  131. free(ihead);
  132. if ((opts & IGNLNKS) || ihead->count == 0)
  133. continue;
  134. log(lfp, "%s: Warning: missing links\n",
  135. ihead->pathname);
  136. }
  137. }
  138. }
  139. /*
  140. * Create a connection to the rdist server on the machine rhost.
  141. */
  142. makeconn(rhost)
  143. char *rhost;
  144. {
  145. register char *ruser, *cp;
  146. static char *cur_host = NULL;
  147. int n;
  148. extern char user[];
  149. if (debug)
  150. printf("makeconn(%s)\n", rhost);
  151. if (cur_host != NULL && rem >= 0) {
  152. if (strcmp(cur_host, rhost) == 0)
  153. return(1);
  154. closeconn();
  155. }
  156. ruser = rindex(rhost, '.');
  157. if (ruser != NULL) {
  158. *ruser++ = '\0';
  159. if (!okname(ruser))
  160. return(0);
  161. } else
  162. ruser = user;
  163. if (!qflag)
  164. printf("updating host %s\n", rhost);
  165. (void) sprintf(buf, "/usr/local/rdist -Server%s", qflag ? " -q" : "");
  166. if (debug) {
  167. printf("luser = %s, ruser = %s\n", user, ruser);
  168. printf("buf = %s\n", buf);
  169. }
  170. fflush(stdout);
  171. cur_host = rhost;
  172. rem = rcmd(&rhost, IPPORT_CMDSERVER, user, ruser, buf, 0);
  173. if (rem < 0)
  174. return(0);
  175. cp = buf;
  176. if (read(rem, cp, 1) != 1)
  177. lostconn();
  178. if (*cp == 'V') {
  179. do {
  180. if (read(rem, cp, 1) != 1)
  181. lostconn();
  182. } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
  183. *--cp = '\0';
  184. cp = buf;
  185. n = 0;
  186. while (*cp >= '0' && *cp <= '9')
  187. n = (n * 10) + (*cp++ - '0');
  188. if (*cp == '\0' && n == VERSION)
  189. return(1);
  190. error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION, n);
  191. } else
  192. error("connection failed: version numbers don't match\n");
  193. closeconn();
  194. return(0);
  195. }
  196. /*
  197. * Signal end of previous connection.
  198. */
  199. closeconn()
  200. {
  201. if (debug)
  202. printf("closeconn()\n");
  203. if (rem >= 0) {
  204. (void) write(rem, "\2\n", 2);
  205. (void) close(rem);
  206. rem = -1;
  207. }
  208. }
  209. lostconn()
  210. {
  211. if (iamremote)
  212. cleanup();
  213. log(lfp, "rdist: lost connection\n");
  214. longjmp(env, 1);
  215. }
  216. okname(name)
  217. register char *name;
  218. {
  219. register char *cp = name;
  220. register int c;
  221. do {
  222. c = *cp;
  223. if (c & 0200)
  224. goto bad;
  225. if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
  226. goto bad;
  227. cp++;
  228. } while (*cp);
  229. return(1);
  230. bad:
  231. error("invalid user name %s\n", name);
  232. return(0);
  233. }
  234. time_t lastmod;
  235. FILE *tfp;
  236. extern char target[], *tp;
  237. /*
  238. * Process commands for comparing files to time stamp files.
  239. */
  240. dodcolon(filev, files, stamp, cmds)
  241. char **filev;
  242. struct namelist *files;
  243. char *stamp;
  244. struct subcmd *cmds;
  245. {
  246. register struct subcmd *sc;
  247. register struct namelist *f;
  248. register char **cpp;
  249. struct timeval tv[2];
  250. struct timezone tz;
  251. struct stat stb;
  252. if (debug)
  253. printf("dodcolon()\n");
  254. if (files == NULL) {
  255. error("no files to be updated\n");
  256. return;
  257. }
  258. if (stat(stamp, &stb) < 0) {
  259. error("%s: %s\n", stamp, sys_errlist[errno]);
  260. return;
  261. }
  262. if (debug)
  263. printf("%s: %d\n", stamp, stb.st_mtime);
  264. subcmds = cmds;
  265. lastmod = stb.st_mtime;
  266. if (nflag || (options & VERIFY))
  267. tfp = NULL;
  268. else {
  269. if ((tfp = fopen(tmpfile, "w")) == NULL) {
  270. error("%s: %s\n", stamp, sys_errlist[errno]);
  271. return;
  272. }
  273. (void) gettimeofday(&tv[0], &tz);
  274. tv[1] = tv[0];
  275. (void) utimes(stamp, tv);
  276. }
  277. for (f = files; f != NULL; f = f->n_next) {
  278. if (filev) {
  279. for (cpp = filev; *cpp; cpp++)
  280. if (strcmp(f->n_name, *cpp) == 0)
  281. goto found;
  282. continue;
  283. }
  284. found:
  285. tp = NULL;
  286. cmptime(f->n_name);
  287. }
  288. if (tfp != NULL)
  289. (void) fclose(tfp);
  290. for (sc = cmds; sc != NULL; sc = sc->sc_next)
  291. if (sc->sc_type == NOTIFY)
  292. notify(tmpfile, NULL, sc->sc_args, lastmod);
  293. if (!nflag && !(options & VERIFY))
  294. (void) unlink(tmpfile);
  295. }
  296. /*
  297. * Compare the mtime of file to the list of time stamps.
  298. */
  299. cmptime(name)
  300. char *name;
  301. {
  302. struct stat stb;
  303. if (debug)
  304. printf("cmptime(%s)\n", name);
  305. if (except(name))
  306. return;
  307. if (nflag) {
  308. printf("comparing dates: %s\n", name);
  309. return;
  310. }
  311. /*
  312. * first time cmptime() is called?
  313. */
  314. if (tp == NULL) {
  315. if (exptilde(target, name) == NULL)
  316. return;
  317. tp = name = target;
  318. while (*tp)
  319. tp++;
  320. }
  321. if (access(name, 4) < 0 || stat(name, &stb) < 0) {
  322. error("%s: %s\n", name, sys_errlist[errno]);
  323. return;
  324. }
  325. switch (stb.st_mode & S_IFMT) {
  326. case S_IFREG:
  327. break;
  328. case S_IFDIR:
  329. rcmptime(&stb);
  330. return;
  331. default:
  332. error("%s: not a plain file\n", name);
  333. return;
  334. }
  335. if (stb.st_mtime > lastmod)
  336. log(tfp, "new: %s\n", name);
  337. }
  338. rcmptime(st)
  339. struct stat *st;
  340. {
  341. register DIR *d;
  342. register struct direct *dp;
  343. register char *cp;
  344. char *otp;
  345. int len;
  346. if (debug)
  347. printf("rcmptime(%x)\n", st);
  348. if ((d = opendir(target)) == NULL) {
  349. error("%s: %s\n", target, sys_errlist[errno]);
  350. return;
  351. }
  352. otp = tp;
  353. len = tp - target;
  354. while (dp = readdir(d)) {
  355. if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
  356. continue;
  357. if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
  358. error("%s/%s: Name too long\n", target, dp->d_name);
  359. continue;
  360. }
  361. tp = otp;
  362. *tp++ = '/';
  363. cp = dp->d_name;
  364. while (*tp++ = *cp++)
  365. ;
  366. tp--;
  367. cmptime(target);
  368. }
  369. closedir(d);
  370. tp = otp;
  371. *tp = '\0';
  372. }
  373. /*
  374. * Notify the list of people the changes that were made.
  375. * rhost == NULL if we are mailing a list of changes compared to at time
  376. * stamp file.
  377. */
  378. notify(file, rhost, to, lmod)
  379. char *file, *rhost;
  380. register struct namelist *to;
  381. time_t lmod;
  382. {
  383. register int fd, len;
  384. FILE *pf, *popen();
  385. struct stat stb;
  386. if ((options & VERIFY) || to == NULL)
  387. return;
  388. if (!qflag) {
  389. printf("notify ");
  390. if (rhost)
  391. printf("@%s ", rhost);
  392. prnames(to);
  393. }
  394. if (nflag)
  395. return;
  396. if ((fd = open(file, 0)) < 0) {
  397. error("%s: %s\n", file, sys_errlist[errno]);
  398. return;
  399. }
  400. if (fstat(fd, &stb) < 0) {
  401. error("%s: %s\n", file, sys_errlist[errno]);
  402. (void) close(fd);
  403. return;
  404. }
  405. if (stb.st_size == 0) {
  406. (void) close(fd);
  407. return;
  408. }
  409. /*
  410. * Create a pipe to mailling program.
  411. */
  412. pf = popen(MAILCMD, "w");
  413. if (pf == NULL) {
  414. error("notify: \"%s\" failed\n", MAILCMD);
  415. (void) close(fd);
  416. return;
  417. }
  418. /*
  419. * Output the proper header information.
  420. */
  421. fprintf(pf, "From: rdist (Remote distribution program)\n");
  422. fprintf(pf, "To:");
  423. if (!any('@', to->n_name) && rhost != NULL)
  424. fprintf(pf, " %s@%s", to->n_name, rhost);
  425. else
  426. fprintf(pf, " %s", to->n_name);
  427. to = to->n_next;
  428. while (to != NULL) {
  429. if (!any('@', to->n_name) && rhost != NULL)
  430. fprintf(pf, ", %s@%s", to->n_name, rhost);
  431. else
  432. fprintf(pf, ", %s", to->n_name);
  433. to = to->n_next;
  434. }
  435. putc('\n', pf);
  436. if (rhost != NULL)
  437. fprintf(pf, "Subject: files updated by rdist from %s to %s\n",
  438. host, rhost);
  439. else
  440. fprintf(pf, "Subject: files updated after %s\n", ctime(&lmod));
  441. putc('\n', pf);
  442. while ((len = read(fd, buf, BUFSIZ)) > 0)
  443. (void) fwrite(buf, 1, len, pf);
  444. (void) close(fd);
  445. (void) pclose(pf);
  446. }
  447. /*
  448. * Return true if name is in the list.
  449. */
  450. inlist(list, file)
  451. struct namelist *list;
  452. char *file;
  453. {
  454. register struct namelist *nl;
  455. for (nl = list; nl != NULL; nl = nl->n_next)
  456. if (!strcmp(file, nl->n_name))
  457. return(1);
  458. return(0);
  459. }
  460. /*
  461. * Return TRUE if file is in the exception list.
  462. */
  463. except(file)
  464. char *file;
  465. {
  466. register struct subcmd *sc;
  467. register struct namelist *nl;
  468. if (debug)
  469. printf("except(%s)\n", file);
  470. for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
  471. if (sc->sc_type != EXCEPT && sc->sc_type != PATTERN)
  472. continue;
  473. for (nl = sc->sc_args; nl != NULL; nl = nl->n_next) {
  474. if (sc->sc_type == EXCEPT) {
  475. if (!strcmp(file, nl->n_name))
  476. return(1);
  477. continue;
  478. }
  479. re_comp(nl->n_name);
  480. if (re_exec(file) > 0)
  481. return(1);
  482. }
  483. }
  484. return(0);
  485. }
  486. char *
  487. colon(cp)
  488. register char *cp;
  489. {
  490. while (*cp) {
  491. if (*cp == ':')
  492. return(cp);
  493. if (*cp == '/')
  494. return(0);
  495. cp++;
  496. }
  497. return(0);
  498. }