Annotation of src/usr.bin/oldrdist/main.c, Revision 1.8
1.8 ! mickey 1: /* $OpenBSD: main.c,v 1.7 1997/04/04 18:41:31 deraadt Exp $ */
1.2 deraadt 2:
1.1 dm 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: #ifndef lint
37: static char copyright[] =
38: "@(#) 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: /* from: static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/9/93"; */
1.8 ! mickey 44: static char *rcsid = "$OpenBSD: main.c,v 1.7 1997/04/04 18:41:31 deraadt Exp $";
1.1 dm 45: #endif /* not lint */
46:
47: #include "defs.h"
48:
49: #define NHOSTS 100
50:
51: /*
52: * Remote distribution program.
53: */
54:
55: char *distfile = NULL;
1.7 deraadt 56: #define _RDIST_TMP "rdistXXXXXXXXXX"
1.1 dm 57: char tempfile[sizeof _PATH_TMP + sizeof _RDIST_TMP + 1];
58: char *tempname;
59:
60: int debug; /* debugging flag */
61: int nflag; /* NOP flag, just print commands without executing */
62: int qflag; /* Quiet. Don't print messages */
63: int options; /* global options */
64: int iamremote; /* act as remote server for transfering files */
65:
66: FILE *fin = NULL; /* input file pointer */
67: int rem = -1; /* file descriptor to remote source/sink process */
68: char host[32]; /* host name */
69: int nerrs; /* number of errors while sending/receiving */
70: char user[10]; /* user's name */
71: char homedir[128]; /* user's home directory */
72: int userid; /* user's user ID */
73: int groupid; /* user's group ID */
74:
75: struct passwd *pw; /* pointer to static area used by getpwent */
76: struct group *gr; /* pointer to static area used by getgrent */
77:
78: static void usage __P((void));
79: static void docmdargs __P((int, char *[]));
80:
81: int
82: main(argc, argv)
83: int argc;
84: char *argv[];
85: {
86: register char *arg;
1.5 millert 87: int cmdargs = 0, fd;
1.1 dm 88: char *dhosts[NHOSTS], **hp = dhosts;
89:
90: pw = getpwuid(userid = getuid());
91: if (pw == NULL) {
92: fprintf(stderr, "%s: Who are you?\n", argv[0]);
93: exit(1);
94: }
95: strcpy(user, pw->pw_name);
96: strcpy(homedir, pw->pw_dir);
97: groupid = pw->pw_gid;
98: gethostname(host, sizeof(host));
99: strcpy(tempfile, _PATH_TMP);
100: strcat(tempfile, _RDIST_TMP);
1.3 millert 101: tempname = xbasename(tempfile);
1.1 dm 102:
103: while (--argc > 0) {
104: if ((arg = *++argv)[0] != '-')
105: break;
106: if (!strcmp(arg, "-Server"))
107: iamremote++;
108: else while (*++arg)
109: switch (*arg) {
110: case 'f':
111: if (--argc <= 0)
112: usage();
113: distfile = *++argv;
114: if (distfile[0] == '-' && distfile[1] == '\0')
115: fin = stdin;
116: break;
117:
118: case 'm':
119: if (--argc <= 0)
120: usage();
121: if (hp >= &dhosts[NHOSTS-2]) {
122: fprintf(stderr, "rdist: too many destination hosts\n");
123: exit(1);
124: }
125: *hp++ = *++argv;
126: break;
127:
128: case 'd':
129: if (--argc <= 0)
130: usage();
131: define(*++argv);
132: break;
133:
134: case 'D':
135: debug++;
136: break;
137:
138: case 'c':
139: cmdargs++;
140: break;
141:
142: case 'n':
143: if (options & VERIFY) {
144: printf("rdist: -n overrides -v\n");
145: options &= ~VERIFY;
146: }
147: nflag++;
148: break;
149:
150: case 'q':
151: qflag++;
152: break;
153:
154: case 'b':
155: options |= COMPARE;
156: break;
157:
158: case 'R':
159: options |= REMOVE;
160: break;
161:
162: case 'v':
163: if (nflag) {
164: printf("rdist: -n overrides -v\n");
165: break;
166: }
167: options |= VERIFY;
168: break;
169:
170: case 'w':
171: options |= WHOLE;
172: break;
173:
174: case 'y':
175: options |= YOUNGER;
176: break;
177:
178: case 'h':
179: options |= FOLLOW;
180: break;
181:
182: case 'i':
183: options |= IGNLNKS;
184: break;
185:
186: default:
187: usage();
188: }
189: }
190: *hp = NULL;
191:
1.3 millert 192: #if defined(DIRECT_RCMD)
1.1 dm 193: seteuid(userid);
1.3 millert 194: #else /* DIRECT_RCMD */
195: if (!iamremote && getuid() != geteuid()) {
196: error("This version of rdist should not be installed setuid.\n");
197: exit(1);
198: }
199: #endif /* DIRECT_RCMD */
1.5 millert 200:
201: if (mktemp(tempfile) == NULL)
202: fatal("cannot get temp file\n");
1.1 dm 203:
204: if (iamremote) {
205: server();
206: exit(nerrs != 0);
207: }
208:
209: if (cmdargs)
210: docmdargs(argc, argv);
211: else {
212: if (fin == NULL) {
213: if(distfile == NULL) {
214: if((fin = fopen("distfile","r")) == NULL)
215: fin = fopen("Distfile", "r");
216: } else
217: fin = fopen(distfile, "r");
1.3 millert 218: if (fin == NULL) {
1.1 dm 219: perror(distfile ? distfile : "distfile");
220: exit(1);
221: }
222: }
223: yyparse();
224: if (nerrs == 0)
225: docmds(dhosts, argc, argv);
226: }
227:
228: exit(nerrs != 0);
229: }
230:
231: static void
232: usage()
233: {
234: printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n");
235: printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
236: exit(1);
237: }
238:
239: /*
240: * rcp like interface for distributing files.
241: */
242: static void
243: docmdargs(nargs, args)
244: int nargs;
245: char *args[];
246: {
247: register struct namelist *nl, *prev;
248: register char *cp;
249: struct namelist *files, *hosts;
250: struct subcmd *cmds;
251: char *dest;
252: static struct namelist tnl = { NULL, NULL };
253: int i;
254:
255: if (nargs < 2)
256: usage();
257:
258: prev = NULL;
259: for (i = 0; i < nargs - 1; i++) {
260: nl = makenl(args[i]);
261: if (prev == NULL)
262: files = prev = nl;
263: else {
264: prev->n_next = nl;
265: prev = nl;
266: }
267: }
268:
269: cp = args[i];
1.3 millert 270: if ((dest = strchr(cp, ':')) != NULL)
1.1 dm 271: *dest++ = '\0';
272: tnl.n_name = cp;
273: hosts = expand(&tnl, E_ALL);
274: if (nerrs)
275: exit(1);
276:
277: if (dest == NULL || *dest == '\0')
278: cmds = NULL;
279: else {
280: cmds = makesubcmd(INSTALL);
281: cmds->sc_options = options;
282: cmds->sc_name = dest;
283: }
284:
285: if (debug) {
286: printf("docmdargs()\nfiles = ");
287: prnames(files);
288: printf("hosts = ");
289: prnames(hosts);
290: }
291: insert(NULL, files, hosts, cmds);
292: docmds(NULL, 0, NULL);
293: }
294:
295: /*
296: * Print a list of NAME blocks (mostly for debugging).
297: */
298: void
299: prnames(nl)
300: register struct namelist *nl;
301: {
302: printf("( ");
303: while (nl != NULL) {
304: printf("%s ", nl->n_name);
305: nl = nl->n_next;
306: }
307: printf(")\n");
308: }
309:
1.8 ! mickey 310: #ifdef __STDC__
1.1 dm 311: #include <stdarg.h>
312: #else
313: #include <varargs.h>
314: #endif
315:
316: void
1.8 ! mickey 317: #ifdef __STDC__
1.1 dm 318: warn(const char *fmt, ...)
319: #else
320: warn(fmt, va_alist)
321: char *fmt;
322: va_dcl
323: #endif
324: {
325: extern int yylineno;
326: va_list ap;
1.8 ! mickey 327: #ifdef __STDC__
1.1 dm 328: va_start(ap, fmt);
329: #else
330: va_start(ap);
331: #endif
332: (void)fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
333: (void)vfprintf(stderr, fmt, ap);
334: (void)fprintf(stderr, "\n");
335: va_end(ap);
1.3 millert 336: }
337:
338: /*
339: * Private version of basename()
340: */
341: char *xbasename(path)
342: char *path;
343: {
344: register char *cp;
345:
346: if (cp = strrchr(path, '/'))
347: return(cp+1);
348: else
349: return(path);
1.1 dm 350: }