1 /* $NetBSD: docmd.c,v 1.27 2006/12/18 15:14:42 christos Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)docmd.c 8.1 (Berkeley) 6/9/93";
37 __RCSID("$NetBSD: docmd.c,v 1.27 2006/12/18 15:14:42 christos Exp $");
41 #include <sys/types.h>
42 #include <sys/ioctl.h>
52 FILE *lfp
; /* log file for recording files updated */
53 struct subcmd
*subcmds
; /* list of sub-commands for current cmd */
56 static int remerr
= -1; /* Remote stderr */
58 static int makeconn(char *);
59 static int okname(char *);
60 static void closeconn(void);
61 static void cmptime(char *);
62 static void doarrow(char **,
63 struct namelist
*, char *, struct subcmd
*);
64 static void dodcolon(char **,
65 struct namelist
*, char *, struct subcmd
*);
66 static void notify(char *, char *, struct namelist
*, time_t);
67 static void rcmptime(struct stat
*);
70 * Do the commands in cmds (initialized by yyparse).
73 docmds(char **dhosts
, int argc
, char **argv
)
78 extern struct cmd
*cmds
;
80 signal(SIGHUP
, cleanup
);
81 signal(SIGINT
, cleanup
);
82 signal(SIGQUIT
, cleanup
);
83 signal(SIGTERM
, cleanup
);
85 for (c
= cmds
; c
!= NULL
; c
= c
->c_next
) {
86 if (dhosts
!= NULL
&& *dhosts
!= NULL
) {
87 for (cpp
= dhosts
; *cpp
; cpp
++)
88 if (strcmp(c
->c_name
, *cpp
) == 0)
94 for (cpp
= argv
; *cpp
; cpp
++) {
95 if (c
->c_label
!= NULL
&&
96 strcmp(c
->c_label
, *cpp
) == 0) {
100 for (f
= c
->c_files
; f
!= NULL
; f
= f
->n_next
)
101 if (strcmp(f
->n_name
, *cpp
) == 0)
110 doarrow(cpp
, c
->c_files
, c
->c_name
, c
->c_cmds
);
113 dodcolon(cpp
, c
->c_files
, c
->c_name
, c
->c_cmds
);
116 fatal("illegal command type %d\n", c
->c_type
);
123 * Process commands for sending files to other machines.
126 doarrow(char **filev
, struct namelist
*files
, char *rhost
, struct subcmd
*cmds
)
137 printf("doarrow(%lx, %s, %lx)\n",
138 (long)files
, rhost
, (long)cmds
);
141 error("no files to be updated\n");
146 ddir
= files
->n_next
!= NULL
; /* destination is a directory */
148 printf("updating host %s\n", rhost
);
152 signal(SIGPIPE
, lostconn
);
153 if (!makeconn(rhost
))
155 if ((lfp
= fopen(tempfile
, "w")) == NULL
) {
156 fatal("cannot open %s\n", tempfile
);
160 for (f
= files
; f
!= NULL
; f
= f
->n_next
) {
162 for (cpp
= filev
; *cpp
; cpp
++)
163 if (strcmp(f
->n_name
, *cpp
) == 0)
171 for (sc
= cmds
; sc
!= NULL
; sc
= sc
->sc_next
) {
172 if (sc
->sc_type
!= INSTALL
)
175 install(f
->n_name
, sc
->sc_name
,
176 sc
->sc_name
== NULL
? 0 : ddir
, sc
->sc_options
);
177 opts
= sc
->sc_options
;
180 install(f
->n_name
, NULL
, 0, options
);
184 (void) signal(SIGPIPE
, cleanup
);
189 for (sc
= cmds
; sc
!= NULL
; sc
= sc
->sc_next
)
190 if (sc
->sc_type
== NOTIFY
)
191 notify(tempfile
, rhost
, sc
->sc_args
, 0);
193 for (; ihead
!= NULL
; ihead
= ihead
->nextp
) {
195 if ((opts
& IGNLNKS
) || ihead
->count
== 0)
198 dolog(lfp
, "%s: Warning: missing links\n",
205 * Create a connection to the rdist server on the machine rhost.
208 makeconn(char *rhost
)
211 static char *cur_host
= NULL
;
212 static int port
= -1;
218 printf("makeconn(%s)\n", rhost
);
220 if (cur_host
!= NULL
&& rem
>= 0) {
221 if (strcmp(cur_host
, rhost
) == 0)
226 cp
= strchr(rhost
, '@');
231 if (strlcpy(tuser
, rhost
, sizeof(tuser
)) >= sizeof(tuser
)) {
240 else if (!okname(ruser
))
245 printf("updating host %s\n", rhost
);
246 (void) snprintf(buf
, sizeof(buf
), "%s -Server%s", _PATH_RDIST
,
251 if ((sp
= getservbyname("shell", "tcp")) == NULL
)
252 fatal("shell/tcp: unknown service");
257 printf("port = %d, luser = %s, ruser = %s\n", ntohs(port
), user
, ruser
);
258 printf("buf = %s\n", buf
);
263 rem
= rcmd(&rhost
, port
, user
, ruser
, buf
, &remerr
);
268 if (read(rem
, cp
, 1) != 1)
272 if (read(rem
, cp
, 1) != 1)
274 } while (*cp
++ != '\n' && cp
< &buf
[BUFSIZ
]);
278 while (*cp
>= '0' && *cp
<= '9')
279 n
= (n
* 10) + (*cp
++ - '0');
280 if (*cp
== '\0' && n
== VERSION
)
282 error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION
, n
);
284 error("connection failed: version numbers don't match\n");
285 error("got unexpected input:");
288 } while (*cp
!= '\n' && read(rem
, cp
, 1) == 1);
295 * Signal end of previous connection.
301 printf("closeconn()\n");
304 if (write(rem
, "\2\n", 2) < 0 && debug
)
305 printf("write failed on fd %d: %s\n", rem
,
308 (void) close(remerr
);
316 lostconn(int signo __unused
)
322 if (ioctl(remerr
, FIONREAD
, &nr
) != -1) {
323 if (nr
>= (int)sizeof(lcbuf
))
324 nr
= sizeof(lcbuf
) - 1;
325 if ((nr
= read(remerr
, lcbuf
, nr
)) > 0) {
327 if (lcbuf
[nr
- 1] == '\n')
333 (void) strlcpy(lcbuf
, "lost connection", sizeof(lcbuf
));
338 dolog(lfp
, "rdist: %s\n", lcbuf
);
340 error("%s\n", lcbuf
);
354 if (!isalpha(c
) && !isdigit(c
) && c
!= '_' && c
!= '-')
360 error("invalid user name %s\n", name
);
366 extern char target
[], *tp
;
369 * Process commands for comparing files to time stamp files.
372 dodcolon(char **filev
, struct namelist
*files
, char *stamp
, struct subcmd
*cmds
)
377 struct timeval tv
[2];
381 printf("dodcolon()\n");
384 error("no files to be updated\n");
387 if (stat(stamp
, &stb
) < 0) {
388 error("%s: %s\n", stamp
, strerror(errno
));
392 printf("%s: %lu\n", stamp
, (u_long
)stb
.st_mtime
);
395 lastmod
= stb
.st_mtime
;
396 if (nflag
|| (options
& VERIFY
))
399 if ((tfp
= fopen(tempfile
, "w")) == NULL
) {
400 error("%s: %s\n", tempfile
, strerror(errno
));
403 (void) gettimeofday(&tv
[0], (struct timezone
*)0);
405 (void) utimes(stamp
, tv
);
408 for (f
= files
; f
!= NULL
; f
= f
->n_next
) {
410 for (cpp
= filev
; *cpp
; cpp
++)
411 if (strcmp(f
->n_name
, *cpp
) == 0)
422 for (sc
= cmds
; sc
!= NULL
; sc
= sc
->sc_next
)
423 if (sc
->sc_type
== NOTIFY
)
424 notify(tempfile
, NULL
, sc
->sc_args
, lastmod
);
428 * Compare the mtime of file to the list of time stamps.
436 printf("cmptime(%s)\n", name
);
442 printf("comparing dates: %s\n", name
);
447 * first time cmptime() is called?
450 if (exptilde(target
, name
) == NULL
)
456 if (access(name
, 4) < 0 || stat(name
, &stb
) < 0) {
457 error("%s: %s\n", name
, strerror(errno
));
461 switch (stb
.st_mode
& S_IFMT
) {
470 error("%s: not a plain file\n", name
);
474 if (stb
.st_mtime
> lastmod
)
475 dolog(tfp
, "new: %s\n", name
);
479 rcmptime(struct stat
*st
)
488 printf("rcmptime(%lx)\n", (long)st
);
490 if ((d
= opendir(target
)) == NULL
) {
491 error("%s: %s\n", target
, strerror(errno
));
496 while ((dp
= readdir(d
)) != NULL
) {
497 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
499 if (len
+ 1 + strlen(dp
->d_name
) >= BUFSIZ
- 1) {
500 error("%s/%s: Name too long\n", target
, dp
->d_name
);
506 while ((*tp
++ = *cp
++) != 0)
517 * Notify the list of people the changes that were made.
518 * rhost == NULL if we are mailing a list of changes compared to at time
522 notify(char *file
, char *rhost
, struct namelist
*to
, time_t lmod
)
527 char *cp
, *nrhost
= rhost
;
529 if ((options
& VERIFY
) || to
== NULL
)
532 /* strip any leading user@ prefix from rhost */
533 if (rhost
&& (cp
= strchr(rhost
, '@')) != NULL
)
539 printf("@%s ", nrhost
);
545 if ((fd
= open(file
, 0)) < 0) {
546 error("%s: %s\n", file
, strerror(errno
));
549 if (fstat(fd
, &stb
) < 0) {
550 error("%s: %s\n", file
, strerror(errno
));
554 if (stb
.st_size
== 0) {
559 * Create a pipe to mailling program.
561 (void)snprintf(buf
, sizeof(buf
), "%s -oi -t", _PATH_SENDMAIL
);
562 pf
= popen(buf
, "w");
564 error("notify: \"%s\" failed\n", _PATH_SENDMAIL
);
569 * Output the proper header information.
571 fprintf(pf
, "From: rdist (Remote distribution program)\n");
573 if (!any('@', to
->n_name
) && rhost
!= NULL
)
574 fprintf(pf
, " %s@%s", to
->n_name
, nrhost
);
576 fprintf(pf
, " %s", to
->n_name
);
579 if (!any('@', to
->n_name
) && rhost
!= NULL
)
580 fprintf(pf
, ", %s@%s", to
->n_name
, nrhost
);
582 fprintf(pf
, ", %s", to
->n_name
);
587 fprintf(pf
, "Subject: files updated by rdist from %s to %s\n",
590 fprintf(pf
, "Subject: files updated after %s\n", ctime(&lmod
));
593 while ((len
= read(fd
, buf
, BUFSIZ
)) > 0)
594 if (fwrite(buf
, 1, len
, pf
) < 1)
595 error("%s: %s\n", file
, strerror(errno
));
601 * Return true if name is in the list.
604 inlist(struct namelist
*list
, char *file
)
608 for (nl
= list
; nl
!= NULL
; nl
= nl
->n_next
)
609 if (!strcmp(file
, nl
->n_name
))
615 * Return TRUE if file is in the exception list.
626 printf("except(%s)\n", file
);
628 for (sc
= subcmds
; sc
!= NULL
; sc
= sc
->sc_next
) {
629 if (sc
->sc_type
!= EXCEPT
&& sc
->sc_type
!= PATTERN
)
631 for (nl
= sc
->sc_args
; nl
!= NULL
; nl
= nl
->n_next
) {
632 if (sc
->sc_type
== EXCEPT
) {
633 if (!strcmp(file
, nl
->n_name
))
637 if ((err
= regcomp(&s
, nl
->n_name
, 0)) != 0) {
639 (void) regerror(err
, &s
, ebuf
, sizeof(ebuf
));
640 error("%s: %s\n", nl
->n_name
, ebuf
);
642 if (regexec(&s
, file
, 0, NULL
, 0) == 0) {