Home | History | Annotate | Line # | Download | only in rdist
main.c revision 1.19
      1  1.19     lukem /*	$NetBSD: main.c,v 1.19 2008/07/21 14:19:25 lukem Exp $	*/
      2   1.5   thorpej 
      3   1.1       cgd /*
      4   1.4       cgd  * Copyright (c) 1983, 1993
      5   1.4       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.15       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.6       mrg #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.19     lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
     35  1.19     lukem  The Regents of the University of California.  All rights reserved.");
     36   1.1       cgd #endif /* not lint */
     37   1.1       cgd 
     38   1.1       cgd #ifndef lint
     39   1.5   thorpej #if 0
     40   1.5   thorpej static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/9/93";
     41   1.5   thorpej #else
     42  1.19     lukem __RCSID("$NetBSD: main.c,v 1.19 2008/07/21 14:19:25 lukem Exp $");
     43   1.5   thorpej #endif
     44   1.1       cgd #endif /* not lint */
     45   1.6       mrg 
     46   1.6       mrg #include <sys/types.h>
     47   1.6       mrg 
     48  1.10       mrg #include <err.h>
     49   1.6       mrg #include <errno.h>
     50   1.6       mrg #include <pwd.h>
     51   1.1       cgd 
     52   1.1       cgd #include "defs.h"
     53   1.1       cgd 
     54   1.1       cgd #define NHOSTS 100
     55   1.1       cgd 
     56   1.1       cgd /*
     57   1.1       cgd  * Remote distribution program.
     58   1.1       cgd  */
     59   1.1       cgd 
     60   1.1       cgd char	*distfile = NULL;
     61   1.1       cgd #define _RDIST_TMP	"/rdistXXXXXX"
     62   1.1       cgd char	tempfile[sizeof _PATH_TMP + sizeof _RDIST_TMP + 1];
     63   1.1       cgd char	*tempname;
     64   1.1       cgd 
     65   1.1       cgd int	debug;		/* debugging flag */
     66   1.1       cgd int	nflag;		/* NOP flag, just print commands without executing */
     67   1.1       cgd int	qflag;		/* Quiet. Don't print messages */
     68   1.1       cgd int	options;	/* global options */
     69   1.1       cgd int	iamremote;	/* act as remote server for transfering files */
     70   1.1       cgd 
     71   1.1       cgd FILE	*fin = NULL;	/* input file pointer */
     72   1.1       cgd int	rem = -1;	/* file descriptor to remote source/sink process */
     73   1.9       mrg char	host[MAXHOSTNAMELEN + 1];	/* host name */
     74   1.1       cgd int	nerrs;		/* number of errors while sending/receiving */
     75   1.9       mrg char	user[34];	/* user's name */
     76   1.9       mrg char	homedir[PATH_MAX];	/* user's home directory */
     77   1.8   mycroft uid_t	userid;		/* user's user ID */
     78   1.8   mycroft gid_t	groupid;	/* user's group ID */
     79   1.1       cgd 
     80   1.1       cgd struct	passwd *pw;	/* pointer to static area used by getpwent */
     81   1.1       cgd struct	group *gr;	/* pointer to static area used by getgrent */
     82   1.1       cgd 
     83  1.13       wiz int	main(int, char **);
     84  1.13       wiz static void usage(void);
     85  1.13       wiz static void docmdargs(int, char *[]);
     86   1.4       cgd 
     87   1.4       cgd int
     88  1.13       wiz main(int argc, char **argv)
     89   1.1       cgd {
     90   1.7     lukem 	char *arg;
     91   1.1       cgd 	int cmdargs = 0;
     92   1.1       cgd 	char *dhosts[NHOSTS], **hp = dhosts;
     93  1.10       mrg 	int fd;
     94   1.1       cgd 
     95   1.1       cgd 	pw = getpwuid(userid = getuid());
     96   1.1       cgd 	if (pw == NULL) {
     97   1.1       cgd 		fprintf(stderr, "%s: Who are you?\n", argv[0]);
     98   1.1       cgd 		exit(1);
     99   1.1       cgd 	}
    100  1.14    itojun 	strlcpy(user, pw->pw_name, sizeof(user));
    101  1.14    itojun 	strlcpy(homedir, pw->pw_dir, sizeof(homedir));
    102   1.1       cgd 	groupid = pw->pw_gid;
    103   1.1       cgd 	gethostname(host, sizeof(host));
    104   1.9       mrg 	host[sizeof(host) - 1] = '\0';
    105  1.14    itojun 	strlcpy(tempfile, _PATH_TMP, sizeof(tempfile));
    106  1.14    itojun 	strlcat(tempfile, _RDIST_TMP, sizeof(tempfile));
    107   1.7     lukem 	if ((tempname = strrchr(tempfile, '/')) != 0)
    108   1.1       cgd 		tempname++;
    109   1.1       cgd 	else
    110   1.1       cgd 		tempname = tempfile;
    111   1.1       cgd 
    112   1.1       cgd 	while (--argc > 0) {
    113   1.1       cgd 		if ((arg = *++argv)[0] != '-')
    114   1.1       cgd 			break;
    115   1.1       cgd 		if (!strcmp(arg, "-Server"))
    116   1.1       cgd 			iamremote++;
    117   1.1       cgd 		else while (*++arg)
    118   1.1       cgd 			switch (*arg) {
    119   1.1       cgd 			case 'f':
    120   1.1       cgd 				if (--argc <= 0)
    121   1.1       cgd 					usage();
    122   1.1       cgd 				distfile = *++argv;
    123   1.1       cgd 				if (distfile[0] == '-' && distfile[1] == '\0')
    124   1.1       cgd 					fin = stdin;
    125   1.1       cgd 				break;
    126   1.1       cgd 
    127   1.1       cgd 			case 'm':
    128   1.1       cgd 				if (--argc <= 0)
    129   1.1       cgd 					usage();
    130   1.1       cgd 				if (hp >= &dhosts[NHOSTS-2]) {
    131   1.1       cgd 					fprintf(stderr, "rdist: too many destination hosts\n");
    132   1.1       cgd 					exit(1);
    133   1.1       cgd 				}
    134   1.1       cgd 				*hp++ = *++argv;
    135   1.1       cgd 				break;
    136   1.1       cgd 
    137   1.1       cgd 			case 'd':
    138   1.1       cgd 				if (--argc <= 0)
    139   1.1       cgd 					usage();
    140   1.1       cgd 				define(*++argv);
    141   1.1       cgd 				break;
    142   1.1       cgd 
    143   1.1       cgd 			case 'D':
    144   1.1       cgd 				debug++;
    145   1.1       cgd 				break;
    146   1.1       cgd 
    147   1.1       cgd 			case 'c':
    148   1.1       cgd 				cmdargs++;
    149   1.1       cgd 				break;
    150   1.1       cgd 
    151   1.1       cgd 			case 'n':
    152   1.1       cgd 				if (options & VERIFY) {
    153   1.1       cgd 					printf("rdist: -n overrides -v\n");
    154   1.1       cgd 					options &= ~VERIFY;
    155   1.1       cgd 				}
    156   1.1       cgd 				nflag++;
    157   1.1       cgd 				break;
    158   1.1       cgd 
    159   1.1       cgd 			case 'q':
    160   1.1       cgd 				qflag++;
    161   1.1       cgd 				break;
    162   1.1       cgd 
    163   1.1       cgd 			case 'b':
    164   1.1       cgd 				options |= COMPARE;
    165   1.1       cgd 				break;
    166   1.1       cgd 
    167   1.1       cgd 			case 'R':
    168   1.1       cgd 				options |= REMOVE;
    169   1.1       cgd 				break;
    170   1.1       cgd 
    171   1.1       cgd 			case 'v':
    172   1.1       cgd 				if (nflag) {
    173   1.1       cgd 					printf("rdist: -n overrides -v\n");
    174   1.1       cgd 					break;
    175   1.1       cgd 				}
    176   1.1       cgd 				options |= VERIFY;
    177   1.1       cgd 				break;
    178   1.1       cgd 
    179   1.1       cgd 			case 'w':
    180   1.1       cgd 				options |= WHOLE;
    181   1.1       cgd 				break;
    182   1.1       cgd 
    183   1.1       cgd 			case 'y':
    184   1.1       cgd 				options |= YOUNGER;
    185   1.1       cgd 				break;
    186   1.1       cgd 
    187   1.1       cgd 			case 'h':
    188   1.1       cgd 				options |= FOLLOW;
    189   1.1       cgd 				break;
    190   1.1       cgd 
    191   1.1       cgd 			case 'i':
    192   1.1       cgd 				options |= IGNLNKS;
    193   1.1       cgd 				break;
    194   1.1       cgd 
    195   1.1       cgd 			default:
    196   1.1       cgd 				usage();
    197   1.1       cgd 			}
    198   1.1       cgd 	}
    199   1.1       cgd 	*hp = NULL;
    200   1.1       cgd 
    201   1.3       cgd 	seteuid(userid);
    202  1.10       mrg 	fd = mkstemp(tempfile);
    203  1.10       mrg 	if (fd == -1)
    204  1.10       mrg 		err(1, "could not make a temporary file");
    205  1.10       mrg 	close (fd);
    206   1.1       cgd 
    207   1.1       cgd 	if (iamremote) {
    208   1.1       cgd 		server();
    209  1.11       mrg 		unlink(tempfile);
    210   1.1       cgd 		exit(nerrs != 0);
    211   1.1       cgd 	}
    212   1.1       cgd 
    213   1.1       cgd 	if (cmdargs)
    214   1.1       cgd 		docmdargs(argc, argv);
    215   1.1       cgd 	else {
    216   1.1       cgd 		if (fin == NULL) {
    217  1.11       mrg 			if (distfile == NULL) {
    218  1.11       mrg 				if ((fin = fopen("distfile","r")) == NULL)
    219   1.1       cgd 					fin = fopen("Distfile", "r");
    220   1.1       cgd 			} else
    221   1.1       cgd 				fin = fopen(distfile, "r");
    222  1.11       mrg 			if (fin == NULL) {
    223   1.1       cgd 				perror(distfile ? distfile : "distfile");
    224  1.11       mrg 				unlink(tempfile);
    225   1.1       cgd 				exit(1);
    226   1.1       cgd 			}
    227   1.1       cgd 		}
    228   1.1       cgd 		yyparse();
    229   1.1       cgd 		if (nerrs == 0)
    230   1.1       cgd 			docmds(dhosts, argc, argv);
    231   1.1       cgd 	}
    232   1.1       cgd 
    233  1.11       mrg 	unlink(tempfile);
    234   1.1       cgd 	exit(nerrs != 0);
    235   1.1       cgd }
    236   1.1       cgd 
    237   1.4       cgd static void
    238  1.13       wiz usage(void)
    239   1.1       cgd {
    240  1.17       wiz 
    241  1.17       wiz 	(void)fprintf(stderr,
    242  1.17       wiz 	    "usage: %s [-bDhinqRvwy] [-d var=value] [-f distfile] [-m host] "
    243  1.17       wiz 	    "[name ...]\n"
    244  1.17       wiz 	    "or   : %s [-bDhinqRvwy] -c name ... [login@]host[:dest]\n",
    245  1.17       wiz 	    getprogname(), getprogname());
    246   1.1       cgd 	exit(1);
    247   1.1       cgd }
    248   1.1       cgd 
    249   1.1       cgd /*
    250   1.1       cgd  * rcp like interface for distributing files.
    251   1.1       cgd  */
    252   1.4       cgd static void
    253  1.13       wiz docmdargs(int nargs, char **args)
    254   1.1       cgd {
    255   1.7     lukem 	struct namelist *nl, *prev;
    256   1.7     lukem 	char *cp;
    257   1.1       cgd 	struct namelist *files, *hosts;
    258   1.1       cgd 	struct subcmd *cmds;
    259   1.1       cgd 	char *dest;
    260   1.1       cgd 	static struct namelist tnl = { NULL, NULL };
    261   1.1       cgd 	int i;
    262   1.1       cgd 
    263   1.1       cgd 	if (nargs < 2)
    264   1.1       cgd 		usage();
    265   1.1       cgd 
    266   1.7     lukem 	files = NULL;
    267   1.1       cgd 	prev = NULL;
    268   1.1       cgd 	for (i = 0; i < nargs - 1; i++) {
    269   1.1       cgd 		nl = makenl(args[i]);
    270   1.1       cgd 		if (prev == NULL)
    271   1.1       cgd 			files = prev = nl;
    272   1.1       cgd 		else {
    273   1.1       cgd 			prev->n_next = nl;
    274   1.1       cgd 			prev = nl;
    275   1.1       cgd 		}
    276   1.1       cgd 	}
    277   1.1       cgd 
    278   1.1       cgd 	cp = args[i];
    279   1.7     lukem 	if ((dest = strchr(cp, ':')) != NULL)
    280   1.1       cgd 		*dest++ = '\0';
    281   1.1       cgd 	tnl.n_name = cp;
    282   1.1       cgd 	hosts = expand(&tnl, E_ALL);
    283   1.1       cgd 	if (nerrs)
    284  1.11       mrg 		return;
    285   1.1       cgd 
    286   1.1       cgd 	if (dest == NULL || *dest == '\0')
    287   1.1       cgd 		cmds = NULL;
    288   1.1       cgd 	else {
    289   1.1       cgd 		cmds = makesubcmd(INSTALL);
    290   1.1       cgd 		cmds->sc_options = options;
    291   1.1       cgd 		cmds->sc_name = dest;
    292   1.1       cgd 	}
    293   1.1       cgd 
    294   1.1       cgd 	if (debug) {
    295   1.1       cgd 		printf("docmdargs()\nfiles = ");
    296   1.1       cgd 		prnames(files);
    297   1.1       cgd 		printf("hosts = ");
    298   1.1       cgd 		prnames(hosts);
    299   1.1       cgd 	}
    300   1.1       cgd 	insert(NULL, files, hosts, cmds);
    301   1.1       cgd 	docmds(NULL, 0, NULL);
    302  1.18  christos 	freenl(files);
    303  1.18  christos 	freenl(hosts);
    304  1.18  christos 	freesubcmd(cmds);
    305   1.1       cgd }
    306   1.1       cgd 
    307   1.1       cgd /*
    308   1.1       cgd  * Print a list of NAME blocks (mostly for debugging).
    309   1.1       cgd  */
    310   1.4       cgd void
    311  1.13       wiz prnames(struct namelist *nl)
    312   1.1       cgd {
    313   1.1       cgd 	printf("( ");
    314   1.1       cgd 	while (nl != NULL) {
    315   1.1       cgd 		printf("%s ", nl->n_name);
    316   1.1       cgd 		nl = nl->n_next;
    317   1.1       cgd 	}
    318   1.1       cgd 	printf(")\n");
    319   1.1       cgd }
    320