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