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