Home | History | Annotate | Line # | Download | only in rdist
main.c revision 1.6
      1 /*	$NetBSD: main.c,v 1.6 1997/10/18 14:34:57 mrg 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.6 1997/10/18 14:34:57 mrg 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 int	userid;		/* user's user ID */
     81 int	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 static void usage __P((void));
     87 static void docmdargs __P((int, char *[]));
     88 
     89 int
     90 main(argc, argv)
     91 	int argc;
     92 	char *argv[];
     93 {
     94 	register char *arg;
     95 	int cmdargs = 0;
     96 	char *dhosts[NHOSTS], **hp = dhosts;
     97 
     98 	pw = getpwuid(userid = getuid());
     99 	if (pw == NULL) {
    100 		fprintf(stderr, "%s: Who are you?\n", argv[0]);
    101 		exit(1);
    102 	}
    103 	strcpy(user, pw->pw_name);
    104 	strcpy(homedir, pw->pw_dir);
    105 	groupid = pw->pw_gid;
    106 	gethostname(host, sizeof(host));
    107 	strcpy(tempfile, _PATH_TMP);
    108 	strcat(tempfile, _RDIST_TMP);
    109 	if ((tempname = rindex(tempfile, '/')) != 0)
    110 		tempname++;
    111 	else
    112 		tempname = tempfile;
    113 
    114 	while (--argc > 0) {
    115 		if ((arg = *++argv)[0] != '-')
    116 			break;
    117 		if (!strcmp(arg, "-Server"))
    118 			iamremote++;
    119 		else while (*++arg)
    120 			switch (*arg) {
    121 			case 'f':
    122 				if (--argc <= 0)
    123 					usage();
    124 				distfile = *++argv;
    125 				if (distfile[0] == '-' && distfile[1] == '\0')
    126 					fin = stdin;
    127 				break;
    128 
    129 			case 'm':
    130 				if (--argc <= 0)
    131 					usage();
    132 				if (hp >= &dhosts[NHOSTS-2]) {
    133 					fprintf(stderr, "rdist: too many destination hosts\n");
    134 					exit(1);
    135 				}
    136 				*hp++ = *++argv;
    137 				break;
    138 
    139 			case 'd':
    140 				if (--argc <= 0)
    141 					usage();
    142 				define(*++argv);
    143 				break;
    144 
    145 			case 'D':
    146 				debug++;
    147 				break;
    148 
    149 			case 'c':
    150 				cmdargs++;
    151 				break;
    152 
    153 			case 'n':
    154 				if (options & VERIFY) {
    155 					printf("rdist: -n overrides -v\n");
    156 					options &= ~VERIFY;
    157 				}
    158 				nflag++;
    159 				break;
    160 
    161 			case 'q':
    162 				qflag++;
    163 				break;
    164 
    165 			case 'b':
    166 				options |= COMPARE;
    167 				break;
    168 
    169 			case 'R':
    170 				options |= REMOVE;
    171 				break;
    172 
    173 			case 'v':
    174 				if (nflag) {
    175 					printf("rdist: -n overrides -v\n");
    176 					break;
    177 				}
    178 				options |= VERIFY;
    179 				break;
    180 
    181 			case 'w':
    182 				options |= WHOLE;
    183 				break;
    184 
    185 			case 'y':
    186 				options |= YOUNGER;
    187 				break;
    188 
    189 			case 'h':
    190 				options |= FOLLOW;
    191 				break;
    192 
    193 			case 'i':
    194 				options |= IGNLNKS;
    195 				break;
    196 
    197 			default:
    198 				usage();
    199 			}
    200 	}
    201 	*hp = NULL;
    202 
    203 	seteuid(userid);
    204 	mktemp(tempfile);
    205 
    206 	if (iamremote) {
    207 		server();
    208 		exit(nerrs != 0);
    209 	}
    210 
    211 	if (cmdargs)
    212 		docmdargs(argc, argv);
    213 	else {
    214 		if (fin == NULL) {
    215 			if(distfile == NULL) {
    216 				if((fin = fopen("distfile","r")) == NULL)
    217 					fin = fopen("Distfile", "r");
    218 			} else
    219 				fin = fopen(distfile, "r");
    220 			if(fin == NULL) {
    221 				perror(distfile ? distfile : "distfile");
    222 				exit(1);
    223 			}
    224 		}
    225 		yyparse();
    226 		if (nerrs == 0)
    227 			docmds(dhosts, argc, argv);
    228 	}
    229 
    230 	exit(nerrs != 0);
    231 }
    232 
    233 static void
    234 usage()
    235 {
    236 	printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n");
    237 	printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
    238 	exit(1);
    239 }
    240 
    241 /*
    242  * rcp like interface for distributing files.
    243  */
    244 static void
    245 docmdargs(nargs, args)
    246 	int nargs;
    247 	char *args[];
    248 {
    249 	register struct namelist *nl, *prev;
    250 	register char *cp;
    251 	struct namelist *files, *hosts;
    252 	struct subcmd *cmds;
    253 	char *dest;
    254 	static struct namelist tnl = { NULL, NULL };
    255 	int i;
    256 
    257 	if (nargs < 2)
    258 		usage();
    259 
    260 	prev = NULL;
    261 	for (i = 0; i < nargs - 1; i++) {
    262 		nl = makenl(args[i]);
    263 		if (prev == NULL)
    264 			files = prev = nl;
    265 		else {
    266 			prev->n_next = nl;
    267 			prev = nl;
    268 		}
    269 	}
    270 
    271 	cp = args[i];
    272 	if ((dest = index(cp, ':')) != NULL)
    273 		*dest++ = '\0';
    274 	tnl.n_name = cp;
    275 	hosts = expand(&tnl, E_ALL);
    276 	if (nerrs)
    277 		exit(1);
    278 
    279 	if (dest == NULL || *dest == '\0')
    280 		cmds = NULL;
    281 	else {
    282 		cmds = makesubcmd(INSTALL);
    283 		cmds->sc_options = options;
    284 		cmds->sc_name = dest;
    285 	}
    286 
    287 	if (debug) {
    288 		printf("docmdargs()\nfiles = ");
    289 		prnames(files);
    290 		printf("hosts = ");
    291 		prnames(hosts);
    292 	}
    293 	insert(NULL, files, hosts, cmds);
    294 	docmds(NULL, 0, NULL);
    295 }
    296 
    297 /*
    298  * Print a list of NAME blocks (mostly for debugging).
    299  */
    300 void
    301 prnames(nl)
    302 	register struct namelist *nl;
    303 {
    304 	printf("( ");
    305 	while (nl != NULL) {
    306 		printf("%s ", nl->n_name);
    307 		nl = nl->n_next;
    308 	}
    309 	printf(")\n");
    310 }
    311 
    312 #if __STDC__
    313 #include <stdarg.h>
    314 #else
    315 #include <varargs.h>
    316 #endif
    317 
    318 void
    319 #if __STDC__
    320 warn(const char *fmt, ...)
    321 #else
    322 warn(fmt, va_alist)
    323 	char *fmt;
    324         va_dcl
    325 #endif
    326 {
    327 	extern int yylineno;
    328 	va_list ap;
    329 #if __STDC__
    330 	va_start(ap, fmt);
    331 #else
    332 	va_start(ap);
    333 #endif
    334 	(void)fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
    335 	(void)vfprintf(stderr, fmt, ap);
    336 	(void)fprintf(stderr, "\n");
    337 	va_end(ap);
    338 }
    339