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