Annotation of src/usr.bin/oldrdist/main.c, Revision 1.21
1.21 ! deraadt 1: /* $OpenBSD: main.c,v 1.20 2007/09/27 17:08:58 sobrado 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:
1.12 millert 32: #include <stdarg.h>
1.15 deraadt 33: #include <libgen.h>
1.1 dm 34: #include "defs.h"
35:
36: #define NHOSTS 100
37:
38: /*
39: * Remote distribution program.
40: */
41:
42: char *distfile = NULL;
1.7 deraadt 43: #define _RDIST_TMP "rdistXXXXXXXXXX"
1.17 millert 44: char tempfile[sizeof _PATH_TMP + sizeof _RDIST_TMP - 1];
1.1 dm 45: char *tempname;
46:
47: int debug; /* debugging flag */
48: int nflag; /* NOP flag, just print commands without executing */
49: int qflag; /* Quiet. Don't print messages */
50: int options; /* global options */
51: int iamremote; /* act as remote server for transfering files */
52:
53: FILE *fin = NULL; /* input file pointer */
54: int rem = -1; /* file descriptor to remote source/sink process */
1.17 millert 55: char host[MAXHOSTNAMELEN]; /* host name */
1.1 dm 56: int nerrs; /* number of errors while sending/receiving */
1.17 millert 57: char user[MAXLOGNAME]; /* user's name */
58: char homedir[MAXPATHLEN]; /* user's home directory */
1.14 mpech 59: uid_t userid; /* user's user ID */
60: gid_t groupid; /* user's group ID */
1.1 dm 61:
62: struct passwd *pw; /* pointer to static area used by getpwent */
63: struct group *gr; /* pointer to static area used by getgrent */
64:
1.11 millert 65: static void usage(void);
66: static void docmdargs(int, char *[]);
1.1 dm 67:
68: int
69: main(argc, argv)
70: int argc;
71: char *argv[];
72: {
1.10 mpech 73: char *arg;
1.9 pvalchev 74: int cmdargs = 0;
1.1 dm 75: char *dhosts[NHOSTS], **hp = dhosts;
76:
77: pw = getpwuid(userid = getuid());
78: if (pw == NULL) {
79: fprintf(stderr, "%s: Who are you?\n", argv[0]);
80: exit(1);
81: }
1.16 deraadt 82: strlcpy(user, pw->pw_name, sizeof user);
83: strlcpy(homedir, pw->pw_dir, sizeof homedir);
1.1 dm 84: groupid = pw->pw_gid;
85: gethostname(host, sizeof(host));
1.16 deraadt 86: strlcpy(tempfile, _PATH_TMP, sizeof tempfile);
87: strlcat(tempfile, _RDIST_TMP, sizeof tempfile);
1.13 hin 88: tempname = basename(tempfile);
1.1 dm 89:
90: while (--argc > 0) {
91: if ((arg = *++argv)[0] != '-')
92: break;
93: if (!strcmp(arg, "-Server"))
94: iamremote++;
95: else while (*++arg)
96: switch (*arg) {
97: case 'f':
98: if (--argc <= 0)
99: usage();
100: distfile = *++argv;
101: if (distfile[0] == '-' && distfile[1] == '\0')
102: fin = stdin;
103: break;
104:
105: case 'm':
106: if (--argc <= 0)
107: usage();
108: if (hp >= &dhosts[NHOSTS-2]) {
109: fprintf(stderr, "rdist: too many destination hosts\n");
110: exit(1);
111: }
112: *hp++ = *++argv;
113: break;
114:
115: case 'd':
116: if (--argc <= 0)
117: usage();
118: define(*++argv);
119: break;
120:
121: case 'D':
122: debug++;
123: break;
124:
125: case 'c':
126: cmdargs++;
127: break;
128:
129: case 'n':
130: if (options & VERIFY) {
131: printf("rdist: -n overrides -v\n");
132: options &= ~VERIFY;
133: }
134: nflag++;
135: break;
136:
137: case 'q':
138: qflag++;
139: break;
140:
141: case 'b':
142: options |= COMPARE;
143: break;
144:
145: case 'R':
146: options |= REMOVE;
147: break;
148:
149: case 'v':
150: if (nflag) {
151: printf("rdist: -n overrides -v\n");
152: break;
153: }
154: options |= VERIFY;
155: break;
156:
157: case 'w':
158: options |= WHOLE;
159: break;
160:
161: case 'y':
162: options |= YOUNGER;
163: break;
164:
165: case 'h':
166: options |= FOLLOW;
167: break;
168:
169: case 'i':
170: options |= IGNLNKS;
171: break;
172:
173: default:
174: usage();
175: }
176: }
177: *hp = NULL;
178:
1.3 millert 179: #if defined(DIRECT_RCMD)
1.1 dm 180: seteuid(userid);
1.3 millert 181: #else /* DIRECT_RCMD */
182: if (!iamremote && getuid() != geteuid()) {
183: error("This version of rdist should not be installed setuid.\n");
184: exit(1);
185: }
186: #endif /* DIRECT_RCMD */
1.5 millert 187:
188: if (mktemp(tempfile) == NULL)
189: fatal("cannot get temp file\n");
1.1 dm 190:
191: if (iamremote) {
192: server();
193: exit(nerrs != 0);
194: }
195:
196: if (cmdargs)
197: docmdargs(argc, argv);
198: else {
199: if (fin == NULL) {
1.20 sobrado 200: if (distfile == NULL) {
201: if ((fin = fopen("distfile","r")) == NULL)
1.1 dm 202: fin = fopen("Distfile", "r");
203: } else
204: fin = fopen(distfile, "r");
1.3 millert 205: if (fin == NULL) {
1.1 dm 206: perror(distfile ? distfile : "distfile");
207: exit(1);
208: }
209: }
210: yyparse();
211: if (nerrs == 0)
212: docmds(dhosts, argc, argv);
213: }
214:
215: exit(nerrs != 0);
216: }
217:
218: static void
219: usage()
220: {
1.19 sobrado 221: printf(
222: "usage: rdist [-bhinqRvwy] [-d var=value] [-f distfile] [-m host] [name ...]\n"
223: " rdist [-bhinqRvwy] -c name ... [login@]host[:dest]\n");
1.1 dm 224: exit(1);
225: }
226:
227: /*
228: * rcp like interface for distributing files.
229: */
230: static void
231: docmdargs(nargs, args)
232: int nargs;
233: char *args[];
234: {
1.10 mpech 235: struct namelist *nl, *prev;
236: char *cp;
1.1 dm 237: struct namelist *files, *hosts;
238: struct subcmd *cmds;
239: char *dest;
240: static struct namelist tnl = { NULL, NULL };
241: int i;
242:
243: if (nargs < 2)
244: usage();
245:
246: prev = NULL;
247: for (i = 0; i < nargs - 1; i++) {
248: nl = makenl(args[i]);
249: if (prev == NULL)
250: files = prev = nl;
251: else {
252: prev->n_next = nl;
253: prev = nl;
254: }
255: }
256:
257: cp = args[i];
1.3 millert 258: if ((dest = strchr(cp, ':')) != NULL)
1.1 dm 259: *dest++ = '\0';
260: tnl.n_name = cp;
261: hosts = expand(&tnl, E_ALL);
262: if (nerrs)
263: exit(1);
264:
265: if (dest == NULL || *dest == '\0')
266: cmds = NULL;
267: else {
268: cmds = makesubcmd(INSTALL);
269: cmds->sc_options = options;
270: cmds->sc_name = dest;
271: }
272:
273: if (debug) {
274: printf("docmdargs()\nfiles = ");
275: prnames(files);
276: printf("hosts = ");
277: prnames(hosts);
278: }
279: insert(NULL, files, hosts, cmds);
280: docmds(NULL, 0, NULL);
281: }
282:
283: /*
284: * Print a list of NAME blocks (mostly for debugging).
285: */
286: void
287: prnames(nl)
1.10 mpech 288: struct namelist *nl;
1.1 dm 289: {
290: printf("( ");
291: while (nl != NULL) {
292: printf("%s ", nl->n_name);
293: nl = nl->n_next;
294: }
295: printf(")\n");
296: }