2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)docmd.c 8.1 (Berkeley) 6/9/93
34 * $FreeBSD: src/usr.bin/rdist/docmd.c,v 1.12 1999/08/28 01:05:06 peter Exp $
35 * $DragonFly: src/usr.bin/rdist/docmd.c,v 1.4 2004/07/24 19:45:10 eirikn Exp $
38 #include <sys/types.h>
46 FILE *lfp
; /* log file for recording files updated */
47 struct subcmd
*subcmds
; /* list of sub-commands for current cmd */
50 static int makeconn(char *);
51 static int okname(char *);
52 static void closeconn(void);
53 static void cmptime(char *);
54 static void doarrow(char **,
55 struct namelist
*, char *, struct subcmd
*);
56 static void dodcolon(char **,
57 struct namelist
*, char *, struct subcmd
*);
58 static void notify(char *, char *, struct namelist
*, time_t);
59 static void rcmptime(struct stat
*);
60 static int remotecmd(char *, char *, char *, char *);
63 * Do the commands in cmds (initialized by yyparse).
66 docmds(char **dhosts
, int argc
, char **argv
)
68 register struct cmd
*c
;
69 register struct namelist
*f
;
71 extern struct cmd
*cmds
;
73 signal(SIGHUP
, cleanup
);
74 signal(SIGINT
, cleanup
);
75 signal(SIGQUIT
, cleanup
);
76 signal(SIGTERM
, cleanup
);
78 for (c
= cmds
; c
!= NULL
; c
= c
->c_next
) {
79 if (dhosts
!= NULL
&& *dhosts
!= NULL
) {
80 for (cpp
= dhosts
; *cpp
; cpp
++)
81 if (strcmp(c
->c_name
, *cpp
) == 0)
87 for (cpp
= argv
; *cpp
; cpp
++) {
88 if (c
->c_label
!= NULL
&&
89 strcmp(c
->c_label
, *cpp
) == 0) {
93 for (f
= c
->c_files
; f
!= NULL
; f
= f
->n_next
)
94 if (strcmp(f
->n_name
, *cpp
) == 0)
103 doarrow(cpp
, c
->c_files
, c
->c_name
, c
->c_cmds
);
106 dodcolon(cpp
, c
->c_files
, c
->c_name
, c
->c_cmds
);
109 fatal("illegal command type %d\n", c
->c_type
);
116 * Process commands for sending files to other machines.
119 doarrow(char **filev
, struct namelist
*files
, char *rhost
, struct subcmd
*cmds
)
121 register struct namelist
*f
;
122 register struct subcmd
*sc
;
124 int n
, ddir
, opts
= options
;
127 printf("doarrow(%p, %s, %p)\n", files
, rhost
, cmds
);
130 error("no files to be updated\n");
135 ddir
= files
->n_next
!= NULL
; /* destination is a directory */
137 printf("updating host %s\n", rhost
);
141 signal(SIGPIPE
, lostconn
);
142 if (!makeconn(rhost
))
144 if ((lfp
= fopen(tempfile
, "w")) == NULL
) {
145 fatal("cannot open %s\n", tempfile
);
149 for (f
= files
; f
!= NULL
; f
= f
->n_next
) {
151 for (cpp
= filev
; *cpp
; cpp
++)
152 if (strcmp(f
->n_name
, *cpp
) == 0)
160 for (sc
= cmds
; sc
!= NULL
; sc
= sc
->sc_next
) {
161 if (sc
->sc_type
!= INSTALL
)
164 install(f
->n_name
, sc
->sc_name
,
165 sc
->sc_name
== NULL
? 0 : ddir
, sc
->sc_options
);
166 opts
= sc
->sc_options
;
169 install(f
->n_name
, NULL
, 0, options
);
173 signal(SIGPIPE
, cleanup
);
178 for (sc
= cmds
; sc
!= NULL
; sc
= sc
->sc_next
)
179 if (sc
->sc_type
== NOTIFY
)
180 notify(tempfile
, rhost
, sc
->sc_args
, 0);
182 struct linkbuf
*nextihead
;
185 for (; ihead
!= NULL
; ihead
= nextihead
) {
186 nextihead
= ihead
->nextp
;
187 if ((opts
& IGNLNKS
) || ihead
->count
== 0)
189 log(lfp
, "%s: Warning: missing links\n",
196 static int remotecmd(char *rhost
, char *luser
, char *ruser
, char *cmd
)
199 static int port
= -1;
202 printf("local user = %s remote user = %s\n", luser
, ruser
);
203 printf("Remote command = '%s'\n", cmd
);
208 signal(SIGALRM
, cleanup
);
211 if (geteuid() == 0 && strcmp(path_rsh
, _PATH_RSH
) == 0) {
213 printf("I am root, therefore direct rcmd\n");
215 signal(SIGPIPE
, cleanup
);
220 if ((sp
= getservbyname("shell", "tcp")) == NULL
)
221 fatal("shell/tcp: unknown service");
225 desc
= rcmd(&rhost
, port
, luser
, ruser
, cmd
, 0);
228 printf("Remote shell command = '%s'\n", path_rsh
);
229 signal(SIGPIPE
, SIG_IGN
);
230 desc
= rshrcmd(&rhost
, -1, luser
, ruser
, cmd
, 0);
232 signal(SIGPIPE
, cleanup
);
242 * Create a connection to the rdist server on the machine rhost.
245 makeconn(char *rhost
)
247 register char *ruser
, *cp
;
248 static char *cur_host
= NULL
;
249 static int port
= -1;
253 #if defined(DIRECT_RCMD)
258 printf("makeconn(%s)\n", rhost
);
260 if (cur_host
!= NULL
&& rem
>= 0) {
261 if (strcmp(cur_host
, rhost
) == 0)
266 cp
= index(rhost
, '@');
271 strncpy(tuser
, rhost
, sizeof(tuser
)-1);
277 else if (!okname(ruser
))
282 printf("updating host %s\n", rhost
);
283 snprintf(buf
, sizeof(buf
), "%s -Server%s", _PATH_RDIST
, qflag
? " -q" : "");
287 if ((sp
= getservbyname("shell", "tcp")) == NULL
)
288 fatal("shell/tcp: unknown service");
293 printf("port = %d, luser = %s, ruser = %s\n", ntohs(port
), user
, ruser
);
294 printf("buf = %s\n", buf
);
298 #if defined(DIRECT_RCMD)
300 rem
= rcmd(&rhost
, port
, user
, ruser
, buf
, 0);
303 rem
= remotecmd(rhost
, user
, ruser
, buf
);
308 if (read(rem
, cp
, 1) != 1)
312 if (read(rem
, cp
, 1) != 1)
314 } while (*cp
++ != '\n' && cp
< &buf
[BUFSIZ
]);
318 while (*cp
>= '0' && *cp
<= '9')
319 n
= (n
* 10) + (*cp
++ - '0');
320 if (*cp
== '\0' && n
== VERSION
)
322 error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION
, n
);
324 error("connection failed: version numbers don't match\n");
325 error("got unexpected input:");
328 } while (*cp
!= '\n' && read(rem
, cp
, 1) == 1);
335 * Signal end of previous connection.
341 printf("closeconn()\n");
344 write(rem
, "\2\n", 2);
355 log(lfp
, "rdist: lost connection\n");
360 okname(register char *name
)
371 if (!isalpha(c
) && !isdigit(c
) && c
!= '_' && c
!= '-')
377 error("invalid user name %s\n", name
);
383 extern char target
[], *tp
;
386 * Process commands for comparing files to time stamp files.
389 dodcolon(char **filev
, struct namelist
*files
, char *stamp
, struct subcmd
*cmds
)
391 register struct subcmd
*sc
;
392 register struct namelist
*f
;
394 struct timeval tv
[2];
399 printf("dodcolon()\n");
402 error("no files to be updated\n");
405 if (stat(stamp
, &stb
) < 0) {
406 error("%s: %s\n", stamp
, strerror(errno
));
410 printf("%s: %ld\n", stamp
, stb
.st_mtime
);
413 lastmod
= stb
.st_mtime
;
414 if (nflag
|| (options
& VERIFY
))
417 if ((tfp
= fopen(tempfile
, "w")) == NULL
) {
418 error("%s: %s\n", stamp
, strerror(errno
));
421 gettimeofday(&tv
[0], &tz
);
426 for (f
= files
; f
!= NULL
; f
= f
->n_next
) {
428 for (cpp
= filev
; *cpp
; cpp
++)
429 if (strcmp(f
->n_name
, *cpp
) == 0)
440 for (sc
= cmds
; sc
!= NULL
; sc
= sc
->sc_next
)
441 if (sc
->sc_type
== NOTIFY
)
442 notify(tempfile
, NULL
, sc
->sc_args
, lastmod
);
443 if (!nflag
&& !(options
& VERIFY
))
448 * Compare the mtime of file to the list of time stamps.
456 printf("cmptime(%s)\n", name
);
462 printf("comparing dates: %s\n", name
);
467 * first time cmptime() is called?
470 if (exptilde(target
, name
, sizeof(target
)) == NULL
)
476 if (access(name
, 4) < 0 || stat(name
, &stb
) < 0) {
477 error("%s: %s\n", name
, strerror(errno
));
481 switch (stb
.st_mode
& S_IFMT
) {
490 error("%s: not a plain file\n", name
);
494 if (stb
.st_mtime
> lastmod
)
495 log(tfp
, "new: %s\n", name
);
499 rcmptime(struct stat
*st
)
502 register struct dirent
*dp
;
508 printf("rcmptime(%p)\n", st
);
510 if ((d
= opendir(target
)) == NULL
) {
511 error("%s: %s\n", target
, strerror(errno
));
516 while ((dp
= readdir(d
))) {
517 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
519 if (len
+ 1 + strlen(dp
->d_name
) >= BUFSIZ
- 1) {
520 error("%s/%s: Name too long\n", target
, dp
->d_name
);
526 while ((*tp
++ = *cp
++))
537 * Notify the list of people the changes that were made.
538 * rhost == NULL if we are mailing a list of changes compared to at time
542 notify(char *file
, char *rhost
, register struct namelist
*to
, time_t lmod
)
544 register int fd
, len
;
548 if ((options
& VERIFY
) || to
== NULL
)
553 printf("@%s ", rhost
);
559 if ((fd
= open(file
, 0)) < 0) {
560 error("%s: %s\n", file
, strerror(errno
));
563 if (fstat(fd
, &stb
) < 0) {
564 error("%s: %s\n", file
, strerror(errno
));
568 if (stb
.st_size
== 0) {
573 * Create a pipe to mailling program.
575 snprintf(buf
, sizeof(buf
), "%s -oi -t", _PATH_SENDMAIL
);
576 pf
= popen(buf
, "w");
578 error("notify: \"%s\" failed\n", _PATH_SENDMAIL
);
583 * Output the proper header information.
585 fprintf(pf
, "From: rdist (Remote distribution program)\n");
587 if (!any('@', to
->n_name
) && rhost
!= NULL
)
588 fprintf(pf
, " %s@%s", to
->n_name
, rhost
);
590 fprintf(pf
, " %s", to
->n_name
);
593 if (!any('@', to
->n_name
) && rhost
!= NULL
)
594 fprintf(pf
, ", %s@%s", to
->n_name
, rhost
);
596 fprintf(pf
, ", %s", to
->n_name
);
601 fprintf(pf
, "Subject: files updated by rdist from %s to %s\n",
604 fprintf(pf
, "Subject: files updated after %s\n", ctime(&lmod
));
607 while ((len
= read(fd
, buf
, BUFSIZ
)) > 0)
608 fwrite(buf
, 1, len
, pf
);
615 * Return true if name is in the list.
618 inlist(struct namelist
*list
, char *file
)
620 register struct namelist
*nl
;
622 for (nl
= list
; nl
!= NULL
; nl
= nl
->n_next
)
623 if (!strcmp(file
, nl
->n_name
))
629 * Return TRUE if file is in the exception list.
634 register struct subcmd
*sc
;
635 register struct namelist
*nl
;
640 printf("except(%s)\n", file
);
642 for (sc
= subcmds
; sc
!= NULL
; sc
= sc
->sc_next
) {
643 if (sc
->sc_type
!= EXCEPT
&& sc
->sc_type
!= PATTERN
)
645 for (nl
= sc
->sc_args
; nl
!= NULL
; nl
= nl
->n_next
) {
646 if (sc
->sc_type
== EXCEPT
) {
647 if (!strcmp(file
, nl
->n_name
))
651 val
= regcomp(&rx
, nl
->n_name
,
652 REG_EXTENDED
| REG_NOSUB
);
653 if (!regexec(&rx
, file
, 0, 0, 0)) {
664 colon(register char *cp
)