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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)docmd.c 8.1 (Berkeley) 6/9/93
30 * $FreeBSD: src/usr.bin/rdist/docmd.c,v 1.12 1999/08/28 01:05:06 peter Exp $
31 * $DragonFly: src/usr.bin/rdist/docmd.c,v 1.5 2008/10/16 01:52:32 swildner Exp $
34 #include <sys/types.h>
42 FILE *lfp
; /* log file for recording files updated */
43 struct subcmd
*subcmds
; /* list of sub-commands for current cmd */
46 static int makeconn(char *);
47 static int okname(char *);
48 static void closeconn(void);
49 static void cmptime(char *);
50 static void doarrow(char **,
51 struct namelist
*, char *, struct subcmd
*);
52 static void dodcolon(char **,
53 struct namelist
*, char *, struct subcmd
*);
54 static void notify(char *, char *, struct namelist
*, time_t);
55 static void rcmptime(struct stat
*);
56 static int remotecmd(char *, char *, char *, char *);
59 * Do the commands in cmds (initialized by yyparse).
62 docmds(char **dhosts
, int argc
, char **argv
)
67 extern struct cmd
*cmds
;
69 signal(SIGHUP
, cleanup
);
70 signal(SIGINT
, cleanup
);
71 signal(SIGQUIT
, cleanup
);
72 signal(SIGTERM
, cleanup
);
74 for (c
= cmds
; c
!= NULL
; c
= c
->c_next
) {
75 if (dhosts
!= NULL
&& *dhosts
!= NULL
) {
76 for (cpp
= dhosts
; *cpp
; cpp
++)
77 if (strcmp(c
->c_name
, *cpp
) == 0)
83 for (cpp
= argv
; *cpp
; cpp
++) {
84 if (c
->c_label
!= NULL
&&
85 strcmp(c
->c_label
, *cpp
) == 0) {
89 for (f
= c
->c_files
; f
!= NULL
; f
= f
->n_next
)
90 if (strcmp(f
->n_name
, *cpp
) == 0)
99 doarrow(cpp
, c
->c_files
, c
->c_name
, c
->c_cmds
);
102 dodcolon(cpp
, c
->c_files
, c
->c_name
, c
->c_cmds
);
105 fatal("illegal command type %d\n", c
->c_type
);
112 * Process commands for sending files to other machines.
115 doarrow(char **filev
, struct namelist
*files
, char *rhost
, struct subcmd
*cmds
)
120 int n
, ddir
, opts
= options
;
123 printf("doarrow(%p, %s, %p)\n", files
, rhost
, cmds
);
126 error("no files to be updated\n");
131 ddir
= files
->n_next
!= NULL
; /* destination is a directory */
133 printf("updating host %s\n", rhost
);
137 signal(SIGPIPE
, lostconn
);
138 if (!makeconn(rhost
))
140 if ((lfp
= fopen(tempfile
, "w")) == NULL
) {
141 fatal("cannot open %s\n", tempfile
);
145 for (f
= files
; f
!= NULL
; f
= f
->n_next
) {
147 for (cpp
= filev
; *cpp
; cpp
++)
148 if (strcmp(f
->n_name
, *cpp
) == 0)
156 for (sc
= cmds
; sc
!= NULL
; sc
= sc
->sc_next
) {
157 if (sc
->sc_type
!= INSTALL
)
160 install(f
->n_name
, sc
->sc_name
,
161 sc
->sc_name
== NULL
? 0 : ddir
, sc
->sc_options
);
162 opts
= sc
->sc_options
;
165 install(f
->n_name
, NULL
, 0, options
);
169 signal(SIGPIPE
, cleanup
);
174 for (sc
= cmds
; sc
!= NULL
; sc
= sc
->sc_next
)
175 if (sc
->sc_type
== NOTIFY
)
176 notify(tempfile
, rhost
, sc
->sc_args
, 0);
178 struct linkbuf
*nextihead
;
181 for (; ihead
!= NULL
; ihead
= nextihead
) {
182 nextihead
= ihead
->nextp
;
183 if ((opts
& IGNLNKS
) || ihead
->count
== 0)
185 dolog(lfp
, "%s: Warning: missing links\n",
192 static int remotecmd(char *rhost
, char *luser
, char *ruser
, char *cmd
)
195 static int port
= -1;
198 printf("local user = %s remote user = %s\n", luser
, ruser
);
199 printf("Remote command = '%s'\n", cmd
);
204 signal(SIGALRM
, cleanup
);
207 if (geteuid() == 0 && strcmp(path_rsh
, _PATH_RSH
) == 0) {
209 printf("I am root, therefore direct rcmd\n");
211 signal(SIGPIPE
, cleanup
);
216 if ((sp
= getservbyname("shell", "tcp")) == NULL
)
217 fatal("shell/tcp: unknown service");
221 desc
= rcmd(&rhost
, port
, luser
, ruser
, cmd
, 0);
224 printf("Remote shell command = '%s'\n", path_rsh
);
225 signal(SIGPIPE
, SIG_IGN
);
226 desc
= rshrcmd(&rhost
, -1, luser
, ruser
, cmd
, 0);
228 signal(SIGPIPE
, cleanup
);
238 * Create a connection to the rdist server on the machine rhost.
241 makeconn(char *rhost
)
244 static char *cur_host
= NULL
;
245 static int port
= -1;
249 #if defined(DIRECT_RCMD)
254 printf("makeconn(%s)\n", rhost
);
256 if (cur_host
!= NULL
&& rem
>= 0) {
257 if (strcmp(cur_host
, rhost
) == 0)
262 cp
= index(rhost
, '@');
267 strncpy(tuser
, rhost
, sizeof(tuser
)-1);
273 else if (!okname(ruser
))
278 printf("updating host %s\n", rhost
);
279 snprintf(buf
, sizeof(buf
), "%s -Server%s", _PATH_RDIST
, qflag
? " -q" : "");
283 if ((sp
= getservbyname("shell", "tcp")) == NULL
)
284 fatal("shell/tcp: unknown service");
289 printf("port = %d, luser = %s, ruser = %s\n", ntohs(port
), user
, ruser
);
290 printf("buf = %s\n", buf
);
294 #if defined(DIRECT_RCMD)
296 rem
= rcmd(&rhost
, port
, user
, ruser
, buf
, 0);
299 rem
= remotecmd(rhost
, user
, ruser
, buf
);
304 if (read(rem
, cp
, 1) != 1)
308 if (read(rem
, cp
, 1) != 1)
310 } while (*cp
++ != '\n' && cp
< &buf
[BUFSIZ
]);
314 while (*cp
>= '0' && *cp
<= '9')
315 n
= (n
* 10) + (*cp
++ - '0');
316 if (*cp
== '\0' && n
== VERSION
)
318 error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION
, n
);
320 error("connection failed: version numbers don't match\n");
321 error("got unexpected input:");
324 } while (*cp
!= '\n' && read(rem
, cp
, 1) == 1);
331 * Signal end of previous connection.
337 printf("closeconn()\n");
340 write(rem
, "\2\n", 2);
351 dolog(lfp
, "rdist: lost connection\n");
367 if (!isalpha(c
) && !isdigit(c
) && c
!= '_' && c
!= '-')
373 error("invalid user name %s\n", name
);
379 extern char target
[], *tp
;
382 * Process commands for comparing files to time stamp files.
385 dodcolon(char **filev
, struct namelist
*files
, char *stamp
, struct subcmd
*cmds
)
390 struct timeval tv
[2];
395 printf("dodcolon()\n");
398 error("no files to be updated\n");
401 if (stat(stamp
, &stb
) < 0) {
402 error("%s: %s\n", stamp
, strerror(errno
));
406 printf("%s: %ld\n", stamp
, stb
.st_mtime
);
409 lastmod
= stb
.st_mtime
;
410 if (nflag
|| (options
& VERIFY
))
413 if ((tfp
= fopen(tempfile
, "w")) == NULL
) {
414 error("%s: %s\n", stamp
, strerror(errno
));
417 gettimeofday(&tv
[0], &tz
);
422 for (f
= files
; f
!= NULL
; f
= f
->n_next
) {
424 for (cpp
= filev
; *cpp
; cpp
++)
425 if (strcmp(f
->n_name
, *cpp
) == 0)
436 for (sc
= cmds
; sc
!= NULL
; sc
= sc
->sc_next
)
437 if (sc
->sc_type
== NOTIFY
)
438 notify(tempfile
, NULL
, sc
->sc_args
, lastmod
);
439 if (!nflag
&& !(options
& VERIFY
))
444 * Compare the mtime of file to the list of time stamps.
452 printf("cmptime(%s)\n", name
);
458 printf("comparing dates: %s\n", name
);
463 * first time cmptime() is called?
466 if (exptilde(target
, name
, sizeof(target
)) == NULL
)
472 if (access(name
, 4) < 0 || stat(name
, &stb
) < 0) {
473 error("%s: %s\n", name
, strerror(errno
));
477 switch (stb
.st_mode
& S_IFMT
) {
486 error("%s: not a plain file\n", name
);
490 if (stb
.st_mtime
> lastmod
)
491 dolog(tfp
, "new: %s\n", name
);
495 rcmptime(struct stat
*st
)
504 printf("rcmptime(%p)\n", st
);
506 if ((d
= opendir(target
)) == NULL
) {
507 error("%s: %s\n", target
, strerror(errno
));
512 while ((dp
= readdir(d
))) {
513 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
515 if (len
+ 1 + strlen(dp
->d_name
) >= BUFSIZ
- 1) {
516 error("%s/%s: Name too long\n", target
, dp
->d_name
);
522 while ((*tp
++ = *cp
++))
533 * Notify the list of people the changes that were made.
534 * rhost == NULL if we are mailing a list of changes compared to at time
538 notify(char *file
, char *rhost
, struct namelist
*to
, time_t lmod
)
544 if ((options
& VERIFY
) || to
== NULL
)
549 printf("@%s ", rhost
);
555 if ((fd
= open(file
, 0)) < 0) {
556 error("%s: %s\n", file
, strerror(errno
));
559 if (fstat(fd
, &stb
) < 0) {
560 error("%s: %s\n", file
, strerror(errno
));
564 if (stb
.st_size
== 0) {
569 * Create a pipe to mailling program.
571 snprintf(buf
, sizeof(buf
), "%s -oi -t", _PATH_SENDMAIL
);
572 pf
= popen(buf
, "w");
574 error("notify: \"%s\" failed\n", _PATH_SENDMAIL
);
579 * Output the proper header information.
581 fprintf(pf
, "From: rdist (Remote distribution program)\n");
583 if (!any('@', to
->n_name
) && rhost
!= NULL
)
584 fprintf(pf
, " %s@%s", to
->n_name
, rhost
);
586 fprintf(pf
, " %s", to
->n_name
);
589 if (!any('@', to
->n_name
) && rhost
!= NULL
)
590 fprintf(pf
, ", %s@%s", to
->n_name
, rhost
);
592 fprintf(pf
, ", %s", to
->n_name
);
597 fprintf(pf
, "Subject: files updated by rdist from %s to %s\n",
600 fprintf(pf
, "Subject: files updated after %s\n", ctime(&lmod
));
603 while ((len
= read(fd
, buf
, BUFSIZ
)) > 0)
604 fwrite(buf
, 1, len
, pf
);
611 * Return true if name is in the list.
614 inlist(struct namelist
*list
, char *file
)
618 for (nl
= list
; nl
!= NULL
; nl
= nl
->n_next
)
619 if (!strcmp(file
, nl
->n_name
))
625 * Return TRUE if file is in the exception list.
636 printf("except(%s)\n", file
);
638 for (sc
= subcmds
; sc
!= NULL
; sc
= sc
->sc_next
) {
639 if (sc
->sc_type
!= EXCEPT
&& sc
->sc_type
!= PATTERN
)
641 for (nl
= sc
->sc_args
; nl
!= NULL
; nl
= nl
->n_next
) {
642 if (sc
->sc_type
== EXCEPT
) {
643 if (!strcmp(file
, nl
->n_name
))
647 val
= regcomp(&rx
, nl
->n_name
,
648 REG_EXTENDED
| REG_NOSUB
);
649 if (!regexec(&rx
, file
, 0, 0, 0)) {