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