* Copyright (c) 1983 Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)docmd.c 5.8 (Berkeley) 3/1/91";
FILE *lfp
; /* log file for recording files updated */
struct subcmd
*subcmds
; /* list of sub-commands for current cmd */
void cleanup(), lostconn();
* Do the commands in cmds (initialized by yyparse).
docmds(dhosts
, argc
, argv
)
register struct namelist
*f
;
signal(SIGQUIT
, cleanup
);
signal(SIGTERM
, cleanup
);
for (c
= cmds
; c
!= NULL
; c
= c
->c_next
) {
if (dhosts
!= NULL
&& *dhosts
!= NULL
) {
for (cpp
= dhosts
; *cpp
; cpp
++)
if (strcmp(c
->c_name
, *cpp
) == 0)
for (cpp
= argv
; *cpp
; cpp
++) {
if (c
->c_label
!= NULL
&&
strcmp(c
->c_label
, *cpp
) == 0) {
for (f
= c
->c_files
; f
!= NULL
; f
= f
->n_next
)
if (strcmp(f
->n_name
, *cpp
) == 0)
doarrow(cpp
, c
->c_files
, c
->c_name
, c
->c_cmds
);
dodcolon(cpp
, c
->c_files
, c
->c_name
, c
->c_cmds
);
fatal("illegal command type %d\n", c
->c_type
);
* Process commands for sending files to other machines.
doarrow(filev
, files
, rhost
, cmds
)
register struct namelist
*f
;
register struct subcmd
*sc
;
int n
, ddir
, opts
= options
;
printf("doarrow(%x, %s, %x)\n", files
, rhost
, cmds
);
error("no files to be updated\n");
ddir
= files
->n_next
!= NULL
; /* destination is a directory */
printf("updating host %s\n", rhost
);
signal(SIGPIPE
, lostconn
);
if ((lfp
= fopen(tempfile
, "w")) == NULL
) {
fatal("cannot open %s\n", tempfile
);
for (f
= files
; f
!= NULL
; f
= f
->n_next
) {
for (cpp
= filev
; *cpp
; cpp
++)
if (strcmp(f
->n_name
, *cpp
) == 0)
for (sc
= cmds
; sc
!= NULL
; sc
= sc
->sc_next
) {
if (sc
->sc_type
!= INSTALL
)
install(f
->n_name
, sc
->sc_name
,
sc
->sc_name
== NULL
? 0 : ddir
, sc
->sc_options
);
install(f
->n_name
, NULL
, 0, options
);
(void) signal(SIGPIPE
, cleanup
);
for (sc
= cmds
; sc
!= NULL
; sc
= sc
->sc_next
)
if (sc
->sc_type
== NOTIFY
)
notify(tempfile
, rhost
, sc
->sc_args
, 0);
for (; ihead
!= NULL
; ihead
= ihead
->nextp
) {
if ((opts
& IGNLNKS
) || ihead
->count
== 0)
log(lfp
, "%s: Warning: missing links\n",
* Create a connection to the rdist server on the machine rhost.
register char *ruser
, *cp
;
static char *cur_host
= NULL
;
printf("makeconn(%s)\n", rhost
);
if (cur_host
!= NULL
&& rem
>= 0) {
if (strcmp(cur_host
, rhost
) == 0)
strncpy(tuser
, rhost
, sizeof(tuser
)-1);
printf("updating host %s\n", rhost
);
(void) sprintf(buf
, "%s -Server%s", _PATH_RDIST
, qflag
? " -q" : "");
if ((sp
= getservbyname("shell", "tcp")) == NULL
)
fatal("shell/tcp: unknown service");
printf("port = %d, luser = %s, ruser = %s\n", ntohs(port
), user
, ruser
);
printf("buf = %s\n", buf
);
rem
= rcmd(&rhost
, port
, user
, ruser
, buf
, 0);
if (read(rem
, cp
, 1) != 1)
if (read(rem
, cp
, 1) != 1)
} while (*cp
++ != '\n' && cp
< &buf
[BUFSIZ
]);
while (*cp
>= '0' && *cp
<= '9')
n
= (n
* 10) + (*cp
++ - '0');
if (*cp
== '\0' && n
== VERSION
)
error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION
, n
);
error("connection failed: version numbers don't match\n");
* Signal end of previous connection.
(void) write(rem
, "\2\n", 2);
log(lfp
, "rdist: lost connection\n");
register char *cp
= name
;
if (!isalpha(c
) && !isdigit(c
) && c
!= '_' && c
!= '-')
error("invalid user name %s\n", name
);
extern char target
[], *tp
;
* Process commands for comparing files to time stamp files.
dodcolon(filev
, files
, stamp
, cmds
)
register struct subcmd
*sc
;
register struct namelist
*f
;
error("no files to be updated\n");
if (stat(stamp
, &stb
) < 0) {
error("%s: %s\n", stamp
, strerror(errno
));
printf("%s: %d\n", stamp
, stb
.st_mtime
);
if (nflag
|| (options
& VERIFY
))
if ((tfp
= fopen(tempfile
, "w")) == NULL
) {
error("%s: %s\n", stamp
, strerror(errno
));
(void) gettimeofday(&tv
[0], &tz
);
(void) utimes(stamp
, tv
);
for (f
= files
; f
!= NULL
; f
= f
->n_next
) {
for (cpp
= filev
; *cpp
; cpp
++)
if (strcmp(f
->n_name
, *cpp
) == 0)
for (sc
= cmds
; sc
!= NULL
; sc
= sc
->sc_next
)
if (sc
->sc_type
== NOTIFY
)
notify(tempfile
, NULL
, sc
->sc_args
, lastmod
);
if (!nflag
&& !(options
& VERIFY
))
* Compare the mtime of file to the list of time stamps.
printf("cmptime(%s)\n", name
);
printf("comparing dates: %s\n", name
);
* first time cmptime() is called?
if (exptilde(target
, name
) == NULL
)
if (access(name
, 4) < 0 || stat(name
, &stb
) < 0) {
error("%s: %s\n", name
, strerror(errno
));
switch (stb
.st_mode
& S_IFMT
) {
error("%s: not a plain file\n", name
);
if (stb
.st_mtime
> lastmod
)
log(tfp
, "new: %s\n", name
);
register struct direct
*dp
;
printf("rcmptime(%x)\n", st
);
if ((d
= opendir(target
)) == NULL
) {
error("%s: %s\n", target
, strerror(errno
));
while (dp
= readdir(d
)) {
if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
if (len
+ 1 + strlen(dp
->d_name
) >= BUFSIZ
- 1) {
error("%s/%s: Name too long\n", target
, dp
->d_name
);
* Notify the list of people the changes that were made.
* rhost == NULL if we are mailing a list of changes compared to at time
notify(file
, rhost
, to
, lmod
)
register struct namelist
*to
;
if ((options
& VERIFY
) || to
== NULL
)
if ((fd
= open(file
, 0)) < 0) {
error("%s: %s\n", file
, strerror(errno
));
if (fstat(fd
, &stb
) < 0) {
error("%s: %s\n", file
, strerror(errno
));
* Create a pipe to mailling program.
(void)sprintf(buf
, "%s -oi -t", _PATH_SENDMAIL
);
error("notify: \"%s\" failed\n", _PATH_SENDMAIL
);
* Output the proper header information.
fprintf(pf
, "From: rdist (Remote distribution program)\n");
if (!any('@', to
->n_name
) && rhost
!= NULL
)
fprintf(pf
, " %s@%s", to
->n_name
, rhost
);
fprintf(pf
, " %s", to
->n_name
);
if (!any('@', to
->n_name
) && rhost
!= NULL
)
fprintf(pf
, ", %s@%s", to
->n_name
, rhost
);
fprintf(pf
, ", %s", to
->n_name
);
fprintf(pf
, "Subject: files updated by rdist from %s to %s\n",
fprintf(pf
, "Subject: files updated after %s\n", ctime(&lmod
));
while ((len
= read(fd
, buf
, BUFSIZ
)) > 0)
(void) fwrite(buf
, 1, len
, pf
);
* Return true if name is in the list.
register struct namelist
*nl
;
for (nl
= list
; nl
!= NULL
; nl
= nl
->n_next
)
if (!strcmp(file
, nl
->n_name
))
* Return TRUE if file is in the exception list.
register struct subcmd
*sc
;
register struct namelist
*nl
;
printf("except(%s)\n", file
);
for (sc
= subcmds
; sc
!= NULL
; sc
= sc
->sc_next
) {
if (sc
->sc_type
!= EXCEPT
&& sc
->sc_type
!= PATTERN
)
for (nl
= sc
->sc_args
; nl
!= NULL
; nl
= nl
->n_next
) {
if (sc
->sc_type
== EXCEPT
) {
if (!strcmp(file
, nl
->n_name
))
if (regexec(file
, regcomp(nl
->n_name
)) > 0)