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