Annotation of src/usr.bin/oldrdist/main.c, Revision 1.15
1.15 ! deraadt 1: /* $OpenBSD: main.c,v 1.14 2002/06/12 06:07:16 mpech 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.15 ! deraadt 44: static char *rcsid = "$OpenBSD: main.c,v 1.14 2002/06/12 06:07:16 mpech Exp $";
1.1 dm 45: #endif /* not lint */
46:
1.12 millert 47: #include <stdarg.h>
1.15 ! deraadt 48: #include <libgen.h>
1.1 dm 49: #include "defs.h"
50:
51: #define NHOSTS 100
52:
53: /*
54: * Remote distribution program.
55: */
56:
57: char *distfile = NULL;
1.7 deraadt 58: #define _RDIST_TMP "rdistXXXXXXXXXX"
1.1 dm 59: char tempfile[sizeof _PATH_TMP + sizeof _RDIST_TMP + 1];
60: char *tempname;
61:
62: int debug; /* debugging flag */
63: int nflag; /* NOP flag, just print commands without executing */
64: int qflag; /* Quiet. Don't print messages */
65: int options; /* global options */
66: int iamremote; /* act as remote server for transfering files */
67:
68: FILE *fin = NULL; /* input file pointer */
69: int rem = -1; /* file descriptor to remote source/sink process */
70: char host[32]; /* host name */
71: int nerrs; /* number of errors while sending/receiving */
72: char user[10]; /* user's name */
73: char homedir[128]; /* user's home directory */
1.14 mpech 74: uid_t userid; /* user's user ID */
75: gid_t groupid; /* user's group ID */
1.1 dm 76:
77: struct passwd *pw; /* pointer to static area used by getpwent */
78: struct group *gr; /* pointer to static area used by getgrent */
79:
1.11 millert 80: static void usage(void);
81: static void docmdargs(int, char *[]);
1.1 dm 82:
83: int
84: main(argc, argv)
85: int argc;
86: char *argv[];
87: {
1.10 mpech 88: char *arg;
1.9 pvalchev 89: int cmdargs = 0;
1.1 dm 90: char *dhosts[NHOSTS], **hp = dhosts;
91:
92: pw = getpwuid(userid = getuid());
93: if (pw == NULL) {
94: fprintf(stderr, "%s: Who are you?\n", argv[0]);
95: exit(1);
96: }
97: strcpy(user, pw->pw_name);
98: strcpy(homedir, pw->pw_dir);
99: groupid = pw->pw_gid;
100: gethostname(host, sizeof(host));
101: strcpy(tempfile, _PATH_TMP);
102: strcat(tempfile, _RDIST_TMP);
1.13 hin 103: tempname = basename(tempfile);
1.1 dm 104:
105: while (--argc > 0) {
106: if ((arg = *++argv)[0] != '-')
107: break;
108: if (!strcmp(arg, "-Server"))
109: iamremote++;
110: else while (*++arg)
111: switch (*arg) {
112: case 'f':
113: if (--argc <= 0)
114: usage();
115: distfile = *++argv;
116: if (distfile[0] == '-' && distfile[1] == '\0')
117: fin = stdin;
118: break;
119:
120: case 'm':
121: if (--argc <= 0)
122: usage();
123: if (hp >= &dhosts[NHOSTS-2]) {
124: fprintf(stderr, "rdist: too many destination hosts\n");
125: exit(1);
126: }
127: *hp++ = *++argv;
128: break;
129:
130: case 'd':
131: if (--argc <= 0)
132: usage();
133: define(*++argv);
134: break;
135:
136: case 'D':
137: debug++;
138: break;
139:
140: case 'c':
141: cmdargs++;
142: break;
143:
144: case 'n':
145: if (options & VERIFY) {
146: printf("rdist: -n overrides -v\n");
147: options &= ~VERIFY;
148: }
149: nflag++;
150: break;
151:
152: case 'q':
153: qflag++;
154: break;
155:
156: case 'b':
157: options |= COMPARE;
158: break;
159:
160: case 'R':
161: options |= REMOVE;
162: break;
163:
164: case 'v':
165: if (nflag) {
166: printf("rdist: -n overrides -v\n");
167: break;
168: }
169: options |= VERIFY;
170: break;
171:
172: case 'w':
173: options |= WHOLE;
174: break;
175:
176: case 'y':
177: options |= YOUNGER;
178: break;
179:
180: case 'h':
181: options |= FOLLOW;
182: break;
183:
184: case 'i':
185: options |= IGNLNKS;
186: break;
187:
188: default:
189: usage();
190: }
191: }
192: *hp = NULL;
193:
1.3 millert 194: #if defined(DIRECT_RCMD)
1.1 dm 195: seteuid(userid);
1.3 millert 196: #else /* DIRECT_RCMD */
197: if (!iamremote && getuid() != geteuid()) {
198: error("This version of rdist should not be installed setuid.\n");
199: exit(1);
200: }
201: #endif /* DIRECT_RCMD */
1.5 millert 202:
203: if (mktemp(tempfile) == NULL)
204: fatal("cannot get temp file\n");
1.1 dm 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");
1.3 millert 220: if (fin == NULL) {
1.1 dm 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: {
1.10 mpech 249: struct namelist *nl, *prev;
250: char *cp;
1.1 dm 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];
1.3 millert 272: if ((dest = strchr(cp, ':')) != NULL)
1.1 dm 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)
1.10 mpech 302: struct namelist *nl;
1.1 dm 303: {
304: printf("( ");
305: while (nl != NULL) {
306: printf("%s ", nl->n_name);
307: nl = nl->n_next;
308: }
309: printf(")\n");
310: }