10Sstevel@tonic-gate /*
2*473Sbw * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate * Copyright (c) 1983 Regents of the University of California.
80Sstevel@tonic-gate * All rights reserved.
90Sstevel@tonic-gate *
100Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted
110Sstevel@tonic-gate * provided that the above copyright notice and this paragraph are
120Sstevel@tonic-gate * duplicated in all such forms and that any documentation,
130Sstevel@tonic-gate * advertising materials, and other materials related to such
140Sstevel@tonic-gate * distribution and use acknowledge that the software was developed
150Sstevel@tonic-gate * by the University of California, Berkeley. The name of the
160Sstevel@tonic-gate * University may not be used to endorse or promote products derived
170Sstevel@tonic-gate * from this software without specific prior written permission.
180Sstevel@tonic-gate */
190Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
200Sstevel@tonic-gate
210Sstevel@tonic-gate #include "defs.h"
220Sstevel@tonic-gate #include <string.h>
230Sstevel@tonic-gate #include <setjmp.h>
240Sstevel@tonic-gate #include <netdb.h>
250Sstevel@tonic-gate #include <signal.h>
260Sstevel@tonic-gate #include <krb5defs.h>
270Sstevel@tonic-gate
280Sstevel@tonic-gate #ifndef RDIST
290Sstevel@tonic-gate #ifdef SYSV
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * Historically, the rdist program has had the following hard-coded
320Sstevel@tonic-gate * pathname. Some operating systems attempt to "improve" the
330Sstevel@tonic-gate * directory layout, in the process re-locating the rdist binary
340Sstevel@tonic-gate * to some other location. However, the first original implementation
350Sstevel@tonic-gate * sets a standard of sorts. In order to interoperate with other
360Sstevel@tonic-gate * systems, our implementation must do two things: It must provide
370Sstevel@tonic-gate * the an rdist binary at the pathname below, and it must use this
380Sstevel@tonic-gate * pathname when executing rdist on remote systems via the rcmd()
390Sstevel@tonic-gate * library. Thus the hard-coded path name below can never be changed.
400Sstevel@tonic-gate */
410Sstevel@tonic-gate #endif /* SYSV */
420Sstevel@tonic-gate #define RDIST "/usr/ucb/rdist"
430Sstevel@tonic-gate #endif
440Sstevel@tonic-gate
450Sstevel@tonic-gate FILE *lfp; /* log file for recording files updated */
460Sstevel@tonic-gate struct subcmd *subcmds; /* list of sub-commands for current cmd */
470Sstevel@tonic-gate jmp_buf env;
480Sstevel@tonic-gate
490Sstevel@tonic-gate void cleanup();
500Sstevel@tonic-gate void lostconn();
510Sstevel@tonic-gate static int init_service(int);
520Sstevel@tonic-gate static struct servent *sp;
530Sstevel@tonic-gate
54*473Sbw static void notify(char *file, char *rhost, struct namelist *to, time_t lmod);
55*473Sbw static void rcmptime(struct stat *st);
56*473Sbw static void cmptime(char *name);
57*473Sbw static void dodcolon(char **filev, struct namelist *files, char *stamp,
58*473Sbw struct subcmd *cmds);
59*473Sbw static void closeconn(void);
60*473Sbw static void doarrow(char **filev, struct namelist *files, char *rhost,
61*473Sbw struct subcmd *cmds);
62*473Sbw static int makeconn(char *rhost);
63*473Sbw static int okname(register char *name);
64*473Sbw
650Sstevel@tonic-gate #ifdef SYSV
660Sstevel@tonic-gate #include <libgen.h>
670Sstevel@tonic-gate
680Sstevel@tonic-gate static char *recomp;
690Sstevel@tonic-gate static char *errstring = "regcmp failed for some unknown reason";
700Sstevel@tonic-gate
710Sstevel@tonic-gate char *
re_comp(s)720Sstevel@tonic-gate re_comp(s)
730Sstevel@tonic-gate char *s;
740Sstevel@tonic-gate {
750Sstevel@tonic-gate if ((int)recomp != 0)
760Sstevel@tonic-gate free(recomp);
770Sstevel@tonic-gate recomp = regcmp(s, (char *)0);
780Sstevel@tonic-gate if (recomp == NULL)
790Sstevel@tonic-gate return (errstring);
800Sstevel@tonic-gate else
810Sstevel@tonic-gate return ((char *)0);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate
840Sstevel@tonic-gate
85*473Sbw static int
re_exec(s)860Sstevel@tonic-gate re_exec(s)
870Sstevel@tonic-gate char *s;
880Sstevel@tonic-gate {
890Sstevel@tonic-gate if ((int)recomp == 0)
900Sstevel@tonic-gate return (-1);
910Sstevel@tonic-gate if (regex(recomp, s) == NULL)
920Sstevel@tonic-gate return (0);
930Sstevel@tonic-gate else
940Sstevel@tonic-gate return (1);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate #endif /* SYSV */
970Sstevel@tonic-gate
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate * Do the commands in cmds (initialized by yyparse).
1000Sstevel@tonic-gate */
101*473Sbw void
docmds(dhosts,argc,argv)1020Sstevel@tonic-gate docmds(dhosts, argc, argv)
1030Sstevel@tonic-gate char **dhosts;
1040Sstevel@tonic-gate int argc;
1050Sstevel@tonic-gate char **argv;
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate register struct cmd *c;
1080Sstevel@tonic-gate register struct namelist *f;
1090Sstevel@tonic-gate register char **cpp;
1100Sstevel@tonic-gate extern struct cmd *cmds;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /* protect backgrounded rdist */
1130Sstevel@tonic-gate if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1140Sstevel@tonic-gate (void) signal(SIGINT, cleanup);
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate /* ... and running via nohup(1) */
1170Sstevel@tonic-gate if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1180Sstevel@tonic-gate (void) signal(SIGHUP, cleanup);
1190Sstevel@tonic-gate if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1200Sstevel@tonic-gate (void) signal(SIGQUIT, cleanup);
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate (void) signal(SIGTERM, cleanup);
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate if (debug)
1250Sstevel@tonic-gate if (!cmds)
1260Sstevel@tonic-gate printf("docmds: cmds == NULL\n");
1270Sstevel@tonic-gate else {
1280Sstevel@tonic-gate printf("docmds: cmds ");
1290Sstevel@tonic-gate prcmd(cmds);
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate for (c = cmds; c != NULL; c = c->c_next) {
1320Sstevel@tonic-gate if (dhosts != NULL && *dhosts != NULL) {
1330Sstevel@tonic-gate for (cpp = dhosts; *cpp; cpp++)
1340Sstevel@tonic-gate if (strcmp(c->c_name, *cpp) == 0)
1350Sstevel@tonic-gate goto fndhost;
1360Sstevel@tonic-gate continue;
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate fndhost:
1390Sstevel@tonic-gate if (argc) {
1400Sstevel@tonic-gate for (cpp = argv; *cpp; cpp++) {
1410Sstevel@tonic-gate if (c->c_label != NULL &&
1420Sstevel@tonic-gate strcmp(c->c_label, *cpp) == 0) {
1430Sstevel@tonic-gate cpp = NULL;
1440Sstevel@tonic-gate goto found;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate for (f = c->c_files; f != NULL; f = f->n_next)
1470Sstevel@tonic-gate if (strcmp(f->n_name, *cpp) == 0)
1480Sstevel@tonic-gate goto found;
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate continue;
1510Sstevel@tonic-gate } else
1520Sstevel@tonic-gate cpp = NULL;
1530Sstevel@tonic-gate found:
1540Sstevel@tonic-gate switch (c->c_type) {
1550Sstevel@tonic-gate case ARROW:
1560Sstevel@tonic-gate doarrow(cpp, c->c_files, c->c_name, c->c_cmds);
1570Sstevel@tonic-gate break;
1580Sstevel@tonic-gate case DCOLON:
1590Sstevel@tonic-gate dodcolon(cpp, c->c_files, c->c_name, c->c_cmds);
1600Sstevel@tonic-gate break;
1610Sstevel@tonic-gate default:
1620Sstevel@tonic-gate fatal("illegal command type %d\n", c->c_type);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate closeconn();
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate * Process commands for sending files to other machines.
1700Sstevel@tonic-gate */
171*473Sbw static void
doarrow(filev,files,rhost,cmds)1720Sstevel@tonic-gate doarrow(filev, files, rhost, cmds)
1730Sstevel@tonic-gate char **filev;
1740Sstevel@tonic-gate struct namelist *files;
1750Sstevel@tonic-gate char *rhost;
1760Sstevel@tonic-gate struct subcmd *cmds;
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate register struct namelist *f;
1790Sstevel@tonic-gate register struct subcmd *sc;
1800Sstevel@tonic-gate register char **cpp;
1810Sstevel@tonic-gate int n, ddir, opts = options;
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate if (debug)
1840Sstevel@tonic-gate printf("doarrow(%x, %s, %x)\n", files, rhost, cmds);
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate if (files == NULL) {
1870Sstevel@tonic-gate error("no files to be updated\n");
1880Sstevel@tonic-gate return;
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate subcmds = cmds;
1920Sstevel@tonic-gate ddir = files->n_next != NULL; /* destination is a directory */
1930Sstevel@tonic-gate if (nflag)
1940Sstevel@tonic-gate printf("updating host %s\n", rhost);
1950Sstevel@tonic-gate else {
1960Sstevel@tonic-gate if (setjmp(env))
1970Sstevel@tonic-gate goto done;
1980Sstevel@tonic-gate (void) signal(SIGPIPE, lostconn);
1990Sstevel@tonic-gate if (!makeconn(rhost))
2000Sstevel@tonic-gate return;
2010Sstevel@tonic-gate if (!nflag)
2020Sstevel@tonic-gate if ((lfp = fopen(Tmpfile, "w")) == NULL) {
2030Sstevel@tonic-gate fatal("cannot open %s\n", Tmpfile);
2040Sstevel@tonic-gate exit(1);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate for (f = files; f != NULL; f = f->n_next) {
2080Sstevel@tonic-gate if (filev) {
2090Sstevel@tonic-gate for (cpp = filev; *cpp; cpp++)
2100Sstevel@tonic-gate if (strcmp(f->n_name, *cpp) == 0)
2110Sstevel@tonic-gate goto found;
2120Sstevel@tonic-gate continue;
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate found:
2150Sstevel@tonic-gate n = 0;
2160Sstevel@tonic-gate for (sc = cmds; sc != NULL; sc = sc->sc_next) {
2170Sstevel@tonic-gate if (sc->sc_type != INSTALL)
2180Sstevel@tonic-gate continue;
2190Sstevel@tonic-gate n++;
2200Sstevel@tonic-gate install(f->n_name, sc->sc_name,
2210Sstevel@tonic-gate sc->sc_name == NULL ? 0 : ddir, sc->sc_options);
2220Sstevel@tonic-gate opts = sc->sc_options;
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate if (n == 0)
2250Sstevel@tonic-gate install(f->n_name, NULL, 0, options);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate done:
2280Sstevel@tonic-gate if (!nflag) {
2290Sstevel@tonic-gate (void) signal(SIGPIPE, cleanup);
2300Sstevel@tonic-gate (void) fclose(lfp);
2310Sstevel@tonic-gate lfp = NULL;
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate for (sc = cmds; sc != NULL; sc = sc->sc_next)
2340Sstevel@tonic-gate if (sc->sc_type == NOTIFY)
2350Sstevel@tonic-gate notify(Tmpfile, rhost, sc->sc_args, 0);
2360Sstevel@tonic-gate if (!nflag) {
2370Sstevel@tonic-gate (void) unlink(Tmpfile);
2380Sstevel@tonic-gate for (; ihead != NULL; ihead = ihead->nextp) {
2390Sstevel@tonic-gate free(ihead);
2400Sstevel@tonic-gate if ((opts & IGNLNKS) || ihead->count == 0)
2410Sstevel@tonic-gate continue;
2420Sstevel@tonic-gate log(lfp, "%s: Warning: missing links\n",
2430Sstevel@tonic-gate ihead->pathname);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate static int
init_service(int krb5flag)2490Sstevel@tonic-gate init_service(int krb5flag)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate boolean_t success = B_FALSE;
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate if (krb5flag > 0) {
2540Sstevel@tonic-gate if ((sp = getservbyname("kshell", "tcp")) == NULL) {
2550Sstevel@tonic-gate fatal("kshell/tcp: unknown service");
2560Sstevel@tonic-gate (void) fprintf(stderr,
2570Sstevel@tonic-gate gettext("trying shell/tcp service...\n"));
2580Sstevel@tonic-gate } else {
2590Sstevel@tonic-gate success = B_TRUE;
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate } else {
2620Sstevel@tonic-gate if ((sp = getservbyname("shell", "tcp")) == NULL) {
2630Sstevel@tonic-gate fatal("shell/tcp: unknown service");
2640Sstevel@tonic-gate exit(1);
2650Sstevel@tonic-gate } else {
2660Sstevel@tonic-gate success = B_TRUE;
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate return (success);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate /*
2720Sstevel@tonic-gate * Create a connection to the rdist server on the machine rhost.
2730Sstevel@tonic-gate */
274*473Sbw static int
makeconn(rhost)2750Sstevel@tonic-gate makeconn(rhost)
2760Sstevel@tonic-gate char *rhost;
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate register char *ruser, *cp;
2790Sstevel@tonic-gate static char *cur_host = NULL;
2800Sstevel@tonic-gate static int port = -1;
2810Sstevel@tonic-gate char tuser[20];
2820Sstevel@tonic-gate int n;
2830Sstevel@tonic-gate extern char user[];
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate if (debug)
2860Sstevel@tonic-gate printf("makeconn(%s)\n", rhost);
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate if (cur_host != NULL && rem >= 0) {
2890Sstevel@tonic-gate if (strcmp(cur_host, rhost) == 0)
2900Sstevel@tonic-gate return (1);
2910Sstevel@tonic-gate closeconn();
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate cur_host = rhost;
2940Sstevel@tonic-gate cp = index(rhost, '@');
2950Sstevel@tonic-gate if (cp != NULL) {
2960Sstevel@tonic-gate char c = *cp;
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate *cp = '\0';
2990Sstevel@tonic-gate strncpy(tuser, rhost, sizeof (tuser)-1);
3000Sstevel@tonic-gate *cp = c;
3010Sstevel@tonic-gate rhost = cp + 1;
3020Sstevel@tonic-gate ruser = tuser;
3030Sstevel@tonic-gate if (*ruser == '\0')
3040Sstevel@tonic-gate ruser = user;
3050Sstevel@tonic-gate else if (!okname(ruser))
3060Sstevel@tonic-gate return (0);
3070Sstevel@tonic-gate } else
3080Sstevel@tonic-gate ruser = user;
3090Sstevel@tonic-gate if (!qflag)
3100Sstevel@tonic-gate printf("updating host %s\n", rhost);
3110Sstevel@tonic-gate (void) snprintf(buf, RDIST_BUFSIZ, "%s%s -Server%s",
3120Sstevel@tonic-gate encrypt_flag ? "-x " : "", RDIST, qflag ? " -q" : "");
3130Sstevel@tonic-gate if (port < 0) {
3140Sstevel@tonic-gate if (debug_port == 0) {
3150Sstevel@tonic-gate if ((retval = (int)init_service(krb5auth_flag)) == 0) {
3160Sstevel@tonic-gate krb5auth_flag = encrypt_flag = 0;
3170Sstevel@tonic-gate (void) init_service(krb5auth_flag);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate port = sp->s_port;
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate } else {
3220Sstevel@tonic-gate port = debug_port;
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate if (debug) {
3270Sstevel@tonic-gate printf("port = %d, luser = %s, ruser = %s\n", ntohs(port),
3280Sstevel@tonic-gate user, ruser);
3290Sstevel@tonic-gate printf("buf = %s\n", buf);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate
3320Sstevel@tonic-gate fflush(stdout);
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate if (krb5auth_flag > 0) {
3350Sstevel@tonic-gate if ((encrypt_flag > 0) && (!krb5_privacy_allowed())) {
3360Sstevel@tonic-gate (void) fprintf(stderr, gettext("rdist: Encryption "
3370Sstevel@tonic-gate " not supported.\n"));
3380Sstevel@tonic-gate exit(1);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate authopts = AP_OPTS_MUTUAL_REQUIRED;
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate status = kcmd(&rem, &rhost, port,
3440Sstevel@tonic-gate user, ruser,
3450Sstevel@tonic-gate buf, 0, "host", krb_realm,
3460Sstevel@tonic-gate bsd_context,
3470Sstevel@tonic-gate &auth_context,
3480Sstevel@tonic-gate &cred,
3490Sstevel@tonic-gate 0, /* No need for sequence number */
3500Sstevel@tonic-gate 0, /* No need for server seq # */
3510Sstevel@tonic-gate authopts,
3520Sstevel@tonic-gate 1, /* Always set anyport */
3530Sstevel@tonic-gate &kcmd_proto);
3540Sstevel@tonic-gate if (status) {
3550Sstevel@tonic-gate /*
3560Sstevel@tonic-gate * If new protocol requested, we dont
3570Sstevel@tonic-gate * fallback to less secure ones.
3580Sstevel@tonic-gate */
3590Sstevel@tonic-gate if (kcmd_proto == KCMD_NEW_PROTOCOL) {
3600Sstevel@tonic-gate (void) fprintf(stderr, gettext("rdist: kcmdv2 "
3610Sstevel@tonic-gate "to host %s failed - %s\n"
3620Sstevel@tonic-gate "Fallback to normal rdist denied."),
3630Sstevel@tonic-gate host, error_message(status));
3640Sstevel@tonic-gate exit(1);
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate /* check NO_TKT_FILE or equivalent... */
3670Sstevel@tonic-gate if (status != -1) {
3680Sstevel@tonic-gate (void) fprintf(stderr, gettext("rdist: "
3690Sstevel@tonic-gate "kcmd to host %s failed - %s\n"
3700Sstevel@tonic-gate "trying normal rdist...\n\n"),
3710Sstevel@tonic-gate host, error_message(status));
3720Sstevel@tonic-gate } else {
3730Sstevel@tonic-gate (void) fprintf(stderr,
3740Sstevel@tonic-gate gettext("trying normal rdist...\n"));
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate /*
3770Sstevel@tonic-gate * kcmd() failed, so we now fallback to normal rdist
3780Sstevel@tonic-gate */
3790Sstevel@tonic-gate krb5auth_flag = encrypt_flag = 0;
3800Sstevel@tonic-gate (void) init_service(krb5auth_flag);
3810Sstevel@tonic-gate port = sp->s_port;
3820Sstevel@tonic-gate goto do_rcmd;
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate #ifdef DEBUG
3850Sstevel@tonic-gate else {
3860Sstevel@tonic-gate (void) fprintf(stderr, gettext("Kerberized rdist "
3870Sstevel@tonic-gate "session, port %d in use "), port);
3880Sstevel@tonic-gate if (kcmd_proto == KCMD_OLD_PROTOCOL)
3890Sstevel@tonic-gate (void) fprintf(stderr,
3900Sstevel@tonic-gate gettext("[kcmd ver.1].\n"));
3910Sstevel@tonic-gate else
3920Sstevel@tonic-gate (void) fprintf(stderr,
3930Sstevel@tonic-gate gettext("[kcmd ver.2].\n"));
3940Sstevel@tonic-gate }
3950Sstevel@tonic-gate #endif /* DEBUG */
3960Sstevel@tonic-gate session_key = &cred->keyblock;
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate if (kcmd_proto == KCMD_NEW_PROTOCOL) {
3990Sstevel@tonic-gate status = krb5_auth_con_getlocalsubkey(bsd_context,
4000Sstevel@tonic-gate auth_context,
4010Sstevel@tonic-gate &session_key);
4020Sstevel@tonic-gate if (status) {
4030Sstevel@tonic-gate com_err("rdist", status,
4040Sstevel@tonic-gate "determining subkey for session");
4050Sstevel@tonic-gate exit(1);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate if (!session_key) {
4080Sstevel@tonic-gate com_err("rdist", 0,
4090Sstevel@tonic-gate "no subkey negotiated for connection");
4100Sstevel@tonic-gate exit(1);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate
4140Sstevel@tonic-gate eblock.crypto_entry = session_key->enctype;
4150Sstevel@tonic-gate eblock.key = (krb5_keyblock *)session_key;
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate init_encrypt(encrypt_flag, bsd_context, kcmd_proto, &desinbuf,
4180Sstevel@tonic-gate &desoutbuf, CLIENT, &eblock);
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate
4210Sstevel@tonic-gate if (encrypt_flag > 0) {
4220Sstevel@tonic-gate char *s = gettext("This rdist session is using "
4230Sstevel@tonic-gate "encryption for all data transmissions.\r\n");
4240Sstevel@tonic-gate (void) write(2, s, strlen(s));
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate else
4290Sstevel@tonic-gate do_rcmd:
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate rem = rcmd_af(&rhost, port, user, ruser, buf, 0, AF_INET6);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate if (rem < 0)
4350Sstevel@tonic-gate return (0);
4360Sstevel@tonic-gate
4370Sstevel@tonic-gate cp = buf;
4380Sstevel@tonic-gate if (desread(rem, cp, 1, 0) != 1)
4390Sstevel@tonic-gate lostconn();
4400Sstevel@tonic-gate if (*cp == 'V') {
4410Sstevel@tonic-gate do {
4420Sstevel@tonic-gate if (desread(rem, cp, 1, 0) != 1)
4430Sstevel@tonic-gate lostconn();
4440Sstevel@tonic-gate } while (*cp++ != '\n' && cp < &buf[RDIST_BUFSIZ]);
4450Sstevel@tonic-gate *--cp = '\0';
4460Sstevel@tonic-gate cp = buf;
4470Sstevel@tonic-gate n = 0;
4480Sstevel@tonic-gate while (*cp >= '0' && *cp <= '9')
4490Sstevel@tonic-gate n = (n * 10) + (*cp++ - '0');
4500Sstevel@tonic-gate if (*cp == '\0' && n == VERSION)
4510Sstevel@tonic-gate return (1);
4520Sstevel@tonic-gate error("connection failed: version numbers don't match"
4530Sstevel@tonic-gate " (local %d, remote %d)\n", VERSION, n);
4540Sstevel@tonic-gate } else {
4550Sstevel@tonic-gate error("connection failed: version numbers don't match\n");
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate closeconn();
4580Sstevel@tonic-gate return (0);
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate
4610Sstevel@tonic-gate /*
4620Sstevel@tonic-gate * Signal end of previous connection.
4630Sstevel@tonic-gate */
464*473Sbw static void
closeconn(void)465*473Sbw closeconn(void)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate if (debug)
4680Sstevel@tonic-gate printf("closeconn()\n");
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate if (rem >= 0) {
4710Sstevel@tonic-gate (void) deswrite(rem, "\2\n", 2, 0);
4720Sstevel@tonic-gate (void) close(rem);
4730Sstevel@tonic-gate rem = -1;
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate void
lostconn()4780Sstevel@tonic-gate lostconn()
4790Sstevel@tonic-gate {
4800Sstevel@tonic-gate if (iamremote)
4810Sstevel@tonic-gate cleanup();
4820Sstevel@tonic-gate log(lfp, "rdist: lost connection\n");
4830Sstevel@tonic-gate longjmp(env, 1);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate
486*473Sbw static int
okname(name)4870Sstevel@tonic-gate okname(name)
4880Sstevel@tonic-gate register char *name;
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate register char *cp = name;
4910Sstevel@tonic-gate register int c;
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate do {
4940Sstevel@tonic-gate c = *cp;
4950Sstevel@tonic-gate if (c & 0200)
4960Sstevel@tonic-gate goto bad;
4970Sstevel@tonic-gate if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
4980Sstevel@tonic-gate goto bad;
4990Sstevel@tonic-gate cp++;
5000Sstevel@tonic-gate } while (*cp);
5010Sstevel@tonic-gate return (1);
5020Sstevel@tonic-gate bad:
5030Sstevel@tonic-gate error("invalid user name %s\n", name);
5040Sstevel@tonic-gate return (0);
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate time_t lastmod;
5080Sstevel@tonic-gate FILE *tfp;
5090Sstevel@tonic-gate extern char target[], *tp;
5100Sstevel@tonic-gate
5110Sstevel@tonic-gate /*
5120Sstevel@tonic-gate * Process commands for comparing files to time stamp files.
5130Sstevel@tonic-gate */
514*473Sbw static void
dodcolon(filev,files,stamp,cmds)5150Sstevel@tonic-gate dodcolon(filev, files, stamp, cmds)
5160Sstevel@tonic-gate char **filev;
5170Sstevel@tonic-gate struct namelist *files;
5180Sstevel@tonic-gate char *stamp;
5190Sstevel@tonic-gate struct subcmd *cmds;
5200Sstevel@tonic-gate {
5210Sstevel@tonic-gate register struct subcmd *sc;
5220Sstevel@tonic-gate register struct namelist *f;
5230Sstevel@tonic-gate register char **cpp;
5240Sstevel@tonic-gate struct timeval tv[2];
5250Sstevel@tonic-gate struct stat stb;
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate if (debug)
5280Sstevel@tonic-gate printf("dodcolon()\n");
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate if (files == NULL) {
5310Sstevel@tonic-gate error("no files to be updated\n");
5320Sstevel@tonic-gate return;
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate if (stat(stamp, &stb) < 0) {
5350Sstevel@tonic-gate error("%s: %s\n", stamp, strerror(errno));
5360Sstevel@tonic-gate return;
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate if (debug)
5390Sstevel@tonic-gate printf("%s: %d\n", stamp, stb.st_mtime);
5400Sstevel@tonic-gate
5410Sstevel@tonic-gate subcmds = cmds;
5420Sstevel@tonic-gate lastmod = stb.st_mtime;
5430Sstevel@tonic-gate if (nflag || (options & VERIFY))
5440Sstevel@tonic-gate tfp = NULL;
5450Sstevel@tonic-gate else {
5460Sstevel@tonic-gate if ((tfp = fopen(Tmpfile, "w")) == NULL) {
5470Sstevel@tonic-gate error("%s: %s\n", stamp, strerror(errno));
5480Sstevel@tonic-gate return;
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate (void) gettimeofday(&tv[0], (struct timezone *)NULL);
5510Sstevel@tonic-gate tv[1] = tv[0];
5520Sstevel@tonic-gate (void) utimes(stamp, tv);
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate for (f = files; f != NULL; f = f->n_next) {
5560Sstevel@tonic-gate if (filev) {
5570Sstevel@tonic-gate for (cpp = filev; *cpp; cpp++)
5580Sstevel@tonic-gate if (strcmp(f->n_name, *cpp) == 0)
5590Sstevel@tonic-gate goto found;
5600Sstevel@tonic-gate continue;
5610Sstevel@tonic-gate }
5620Sstevel@tonic-gate found:
5630Sstevel@tonic-gate tp = NULL;
5640Sstevel@tonic-gate cmptime(f->n_name);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate if (tfp != NULL)
5680Sstevel@tonic-gate (void) fclose(tfp);
5690Sstevel@tonic-gate for (sc = cmds; sc != NULL; sc = sc->sc_next)
5700Sstevel@tonic-gate if (sc->sc_type == NOTIFY)
5710Sstevel@tonic-gate notify(Tmpfile, NULL, sc->sc_args, lastmod);
5720Sstevel@tonic-gate if (!nflag && !(options & VERIFY))
5730Sstevel@tonic-gate (void) unlink(Tmpfile);
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate
5760Sstevel@tonic-gate /*
5770Sstevel@tonic-gate * Compare the mtime of file to the list of time stamps.
5780Sstevel@tonic-gate */
579*473Sbw static void
cmptime(name)5800Sstevel@tonic-gate cmptime(name)
5810Sstevel@tonic-gate char *name;
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate struct stat stb;
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate if (debug)
5860Sstevel@tonic-gate printf("cmptime(%s)\n", name);
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate if (except(name))
5890Sstevel@tonic-gate return;
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate if (nflag) {
5920Sstevel@tonic-gate printf("comparing dates: %s\n", name);
5930Sstevel@tonic-gate return;
5940Sstevel@tonic-gate }
5950Sstevel@tonic-gate
5960Sstevel@tonic-gate /*
5970Sstevel@tonic-gate * first time cmptime() is called?
5980Sstevel@tonic-gate */
5990Sstevel@tonic-gate if (tp == NULL) {
6000Sstevel@tonic-gate if (exptilde(target, RDIST_BUFSIZ, name) == NULL)
6010Sstevel@tonic-gate return;
6020Sstevel@tonic-gate tp = name = target;
6030Sstevel@tonic-gate while (*tp)
6040Sstevel@tonic-gate tp++;
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate if (access(name, 4) < 0 || stat(name, &stb) < 0) {
6070Sstevel@tonic-gate error("%s: %s\n", name, strerror(errno));
6080Sstevel@tonic-gate return;
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate
6110Sstevel@tonic-gate switch (stb.st_mode & S_IFMT) {
6120Sstevel@tonic-gate case S_IFREG:
6130Sstevel@tonic-gate break;
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate case S_IFDIR:
6160Sstevel@tonic-gate rcmptime(&stb);
6170Sstevel@tonic-gate return;
6180Sstevel@tonic-gate
6190Sstevel@tonic-gate default:
6200Sstevel@tonic-gate error("%s: not a plain file\n", name);
6210Sstevel@tonic-gate return;
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate
6240Sstevel@tonic-gate if (stb.st_mtime > lastmod)
6250Sstevel@tonic-gate log(tfp, "new: %s\n", name);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate
628*473Sbw static void
rcmptime(st)6290Sstevel@tonic-gate rcmptime(st)
6300Sstevel@tonic-gate struct stat *st;
6310Sstevel@tonic-gate {
6320Sstevel@tonic-gate register DIR *d;
6330Sstevel@tonic-gate register struct dirent *dp;
6340Sstevel@tonic-gate register char *cp;
6350Sstevel@tonic-gate char *otp;
6360Sstevel@tonic-gate int len;
6370Sstevel@tonic-gate
6380Sstevel@tonic-gate if (debug)
6390Sstevel@tonic-gate printf("rcmptime(%x)\n", st);
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate if ((d = opendir(target)) == NULL) {
6420Sstevel@tonic-gate error("%s: %s\n", target, strerror(errno));
6430Sstevel@tonic-gate return;
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate otp = tp;
6460Sstevel@tonic-gate len = tp - target;
6470Sstevel@tonic-gate while (dp = readdir(d)) {
6480Sstevel@tonic-gate if ((strcmp(dp->d_name, ".") == 0) ||
6490Sstevel@tonic-gate (strcmp(dp->d_name, "..") == 0))
6500Sstevel@tonic-gate continue;
6510Sstevel@tonic-gate if (len + 1 + strlen(dp->d_name) >= RDIST_BUFSIZ - 1) {
6520Sstevel@tonic-gate error("%s/%s: Name too long\n", target, dp->d_name);
6530Sstevel@tonic-gate continue;
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate tp = otp;
6560Sstevel@tonic-gate *tp++ = '/';
6570Sstevel@tonic-gate cp = dp->d_name;
6580Sstevel@tonic-gate while (*tp++ = *cp++)
6590Sstevel@tonic-gate ;
6600Sstevel@tonic-gate tp--;
6610Sstevel@tonic-gate cmptime(target);
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate closedir(d);
6640Sstevel@tonic-gate tp = otp;
6650Sstevel@tonic-gate *tp = '\0';
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate
6680Sstevel@tonic-gate /*
6690Sstevel@tonic-gate * Notify the list of people the changes that were made.
6700Sstevel@tonic-gate * rhost == NULL if we are mailing a list of changes compared to at time
6710Sstevel@tonic-gate * stamp file.
6720Sstevel@tonic-gate */
673*473Sbw static void
notify(file,rhost,to,lmod)6740Sstevel@tonic-gate notify(file, rhost, to, lmod)
6750Sstevel@tonic-gate char *file, *rhost;
6760Sstevel@tonic-gate register struct namelist *to;
6770Sstevel@tonic-gate time_t lmod;
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate register int fd, len;
6800Sstevel@tonic-gate FILE *pf, *popen();
6810Sstevel@tonic-gate struct stat stb;
6820Sstevel@tonic-gate
6830Sstevel@tonic-gate if ((options & VERIFY) || to == NULL)
6840Sstevel@tonic-gate return;
6850Sstevel@tonic-gate if (!qflag) {
6860Sstevel@tonic-gate printf("notify ");
6870Sstevel@tonic-gate if (rhost)
6880Sstevel@tonic-gate printf("@%s ", rhost);
6890Sstevel@tonic-gate prnames(to);
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate if (nflag)
6920Sstevel@tonic-gate return;
6930Sstevel@tonic-gate
6940Sstevel@tonic-gate if ((fd = open(file, 0)) < 0) {
6950Sstevel@tonic-gate error("%s: %s\n", file, strerror(errno));
6960Sstevel@tonic-gate return;
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate if (fstat(fd, &stb) < 0) {
6990Sstevel@tonic-gate error("%s: %s\n", file, strerror(errno));
7000Sstevel@tonic-gate (void) close(fd);
7010Sstevel@tonic-gate return;
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate if (stb.st_size == 0) {
7040Sstevel@tonic-gate (void) close(fd);
7050Sstevel@tonic-gate return;
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate /*
7080Sstevel@tonic-gate * Create a pipe to mailling program.
7090Sstevel@tonic-gate */
7100Sstevel@tonic-gate pf = popen(MAILCMD, "w");
7110Sstevel@tonic-gate if (pf == NULL) {
7120Sstevel@tonic-gate error("notify: \"%s\" failed\n", MAILCMD);
7130Sstevel@tonic-gate (void) close(fd);
7140Sstevel@tonic-gate return;
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate /*
7170Sstevel@tonic-gate * Output the proper header information.
7180Sstevel@tonic-gate */
7190Sstevel@tonic-gate fprintf(pf, "From: rdist (Remote distribution program)\n");
7200Sstevel@tonic-gate fprintf(pf, "To:");
7210Sstevel@tonic-gate if (!any('@', to->n_name) && rhost != NULL)
7220Sstevel@tonic-gate fprintf(pf, " %s@%s", to->n_name, rhost);
7230Sstevel@tonic-gate else
7240Sstevel@tonic-gate fprintf(pf, " %s", to->n_name);
7250Sstevel@tonic-gate to = to->n_next;
7260Sstevel@tonic-gate while (to != NULL) {
7270Sstevel@tonic-gate if (!any('@', to->n_name) && rhost != NULL)
7280Sstevel@tonic-gate fprintf(pf, ", %s@%s", to->n_name, rhost);
7290Sstevel@tonic-gate else
7300Sstevel@tonic-gate fprintf(pf, ", %s", to->n_name);
7310Sstevel@tonic-gate to = to->n_next;
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate putc('\n', pf);
7340Sstevel@tonic-gate if (rhost != NULL)
7350Sstevel@tonic-gate fprintf(pf, "Subject: files updated by rdist from %s to %s\n",
7360Sstevel@tonic-gate host, rhost);
7370Sstevel@tonic-gate else
7380Sstevel@tonic-gate fprintf(pf, "Subject: files updated after %s\n", ctime(&lmod));
7390Sstevel@tonic-gate putc('\n', pf);
7400Sstevel@tonic-gate
7410Sstevel@tonic-gate while ((len = read(fd, buf, RDIST_BUFSIZ)) > 0)
7420Sstevel@tonic-gate (void) fwrite(buf, 1, len, pf);
7430Sstevel@tonic-gate (void) close(fd);
7440Sstevel@tonic-gate (void) pclose(pf);
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate
7470Sstevel@tonic-gate /*
7480Sstevel@tonic-gate * Return true if name is in the list.
7490Sstevel@tonic-gate */
750*473Sbw int
inlist(list,file)7510Sstevel@tonic-gate inlist(list, file)
7520Sstevel@tonic-gate struct namelist *list;
7530Sstevel@tonic-gate char *file;
7540Sstevel@tonic-gate {
7550Sstevel@tonic-gate register struct namelist *nl;
7560Sstevel@tonic-gate
7570Sstevel@tonic-gate for (nl = list; nl != NULL; nl = nl->n_next)
7580Sstevel@tonic-gate if (strcmp(file, nl->n_name) == 0)
7590Sstevel@tonic-gate return (1);
7600Sstevel@tonic-gate return (0);
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate
7630Sstevel@tonic-gate /*
7640Sstevel@tonic-gate * Return TRUE if file is in the exception list.
7650Sstevel@tonic-gate */
766*473Sbw int
except(file)7670Sstevel@tonic-gate except(file)
7680Sstevel@tonic-gate char *file;
7690Sstevel@tonic-gate {
7700Sstevel@tonic-gate register struct subcmd *sc;
7710Sstevel@tonic-gate register struct namelist *nl;
7720Sstevel@tonic-gate
7730Sstevel@tonic-gate if (debug)
7740Sstevel@tonic-gate printf("except(%s)\n", file);
7750Sstevel@tonic-gate
7760Sstevel@tonic-gate for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
7770Sstevel@tonic-gate if (sc->sc_type != EXCEPT && sc->sc_type != PATTERN)
7780Sstevel@tonic-gate continue;
7790Sstevel@tonic-gate for (nl = sc->sc_args; nl != NULL; nl = nl->n_next) {
7800Sstevel@tonic-gate if (sc->sc_type == EXCEPT) {
7810Sstevel@tonic-gate if (strcmp(file, nl->n_name) == 0)
7820Sstevel@tonic-gate return (1);
7830Sstevel@tonic-gate continue;
7840Sstevel@tonic-gate }
7850Sstevel@tonic-gate re_comp(nl->n_name);
7860Sstevel@tonic-gate if (re_exec(file) > 0)
7870Sstevel@tonic-gate return (1);
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate return (0);
7910Sstevel@tonic-gate }
7920Sstevel@tonic-gate
7930Sstevel@tonic-gate char *
colon(cp)7940Sstevel@tonic-gate colon(cp)
7950Sstevel@tonic-gate register char *cp;
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate
7980Sstevel@tonic-gate while (*cp) {
7990Sstevel@tonic-gate if (*cp == ':')
8000Sstevel@tonic-gate return (cp);
8010Sstevel@tonic-gate if (*cp == '/')
8020Sstevel@tonic-gate return (0);
8030Sstevel@tonic-gate cp++;
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate return (0);
8060Sstevel@tonic-gate }
807