Commit | Line | Data |
---|---|---|
7172eb74 DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
8 | char copyright[] = | |
9 | "@(#) Copyright (c) 1983 Regents of the University of California.\n\ | |
10 | All rights reserved.\n"; | |
11 | #endif not lint | |
12 | ||
29e83471 | 13 | #ifndef lint |
7172eb74 DF |
14 | static char sccsid[] = "@(#)main.c 5.1 (Berkeley) %G%"; |
15 | #endif not lint | |
29e83471 RC |
16 | |
17 | #include "defs.h" | |
18 | ||
78a18ca3 RC |
19 | #define NHOSTS 100 |
20 | ||
29e83471 RC |
21 | /* |
22 | * Remote distribution program. | |
23 | */ | |
24 | ||
be5872f6 | 25 | char *distfile = NULL; |
d6bccb44 | 26 | char tmpfile[] = "/tmp/rdistXXXXXX"; |
82572cb6 | 27 | char *tmpname = &tmpfile[5]; |
29e83471 RC |
28 | |
29 | int debug; /* debugging flag */ | |
30 | int nflag; /* NOP flag, just print commands without executing */ | |
31 | int qflag; /* Quiet. Don't print messages */ | |
f7770429 | 32 | int options; /* global options */ |
29e83471 RC |
33 | int iamremote; /* act as remote server for transfering files */ |
34 | ||
29e83471 | 35 | FILE *fin = NULL; /* input file pointer */ |
0fccdfef | 36 | int rem = -1; /* file descriptor to remote source/sink process */ |
29e83471 | 37 | char host[32]; /* host name */ |
0fccdfef | 38 | int nerrs; /* number of errors while sending/receiving */ |
29e83471 RC |
39 | char user[10]; /* user's name */ |
40 | char homedir[128]; /* user's home directory */ | |
41 | int userid; /* user's user ID */ | |
82572cb6 | 42 | int groupid; /* user's group ID */ |
29e83471 | 43 | |
e8109cf8 RC |
44 | struct passwd *pw; /* pointer to static area used by getpwent */ |
45 | struct group *gr; /* pointer to static area used by getgrent */ | |
46 | ||
29e83471 RC |
47 | main(argc, argv) |
48 | int argc; | |
49 | char *argv[]; | |
50 | { | |
51 | register char *arg; | |
f7770429 | 52 | int cmdargs = 0; |
78a18ca3 | 53 | char *dhosts[NHOSTS], **hp = dhosts; |
29e83471 | 54 | |
29e83471 | 55 | pw = getpwuid(userid = getuid()); |
29e83471 | 56 | if (pw == NULL) { |
82572cb6 | 57 | fprintf(stderr, "%s: Who are you?\n", argv[0]); |
29e83471 RC |
58 | exit(1); |
59 | } | |
60 | strcpy(user, pw->pw_name); | |
61 | strcpy(homedir, pw->pw_dir); | |
82572cb6 | 62 | groupid = pw->pw_gid; |
29e83471 RC |
63 | gethostname(host, sizeof(host)); |
64 | ||
65 | while (--argc > 0) { | |
66 | if ((arg = *++argv)[0] != '-') | |
67 | break; | |
68 | if (!strcmp(arg, "-Server")) | |
69 | iamremote++; | |
70 | else while (*++arg) | |
71 | switch (*arg) { | |
72 | case 'f': | |
73 | if (--argc <= 0) | |
74 | usage(); | |
75 | distfile = *++argv; | |
76 | if (distfile[0] == '-' && distfile[1] == '\0') | |
77 | fin = stdin; | |
78 | break; | |
79 | ||
78a18ca3 RC |
80 | case 'm': |
81 | if (--argc <= 0) | |
82 | usage(); | |
83 | if (hp >= &dhosts[NHOSTS-2]) { | |
84 | fprintf(stderr, "rdist: too many destination hosts\n"); | |
85 | exit(1); | |
86 | } | |
87 | *hp++ = *++argv; | |
88 | break; | |
89 | ||
29e83471 | 90 | case 'd': |
82572cb6 RC |
91 | if (--argc <= 0) |
92 | usage(); | |
93 | define(*++argv); | |
94 | break; | |
95 | ||
96 | case 'D': | |
29e83471 RC |
97 | debug++; |
98 | break; | |
99 | ||
f7770429 RC |
100 | case 'c': |
101 | cmdargs++; | |
102 | break; | |
103 | ||
29e83471 | 104 | case 'n': |
d6bccb44 RC |
105 | if (options & VERIFY) { |
106 | printf("rdist: -n overrides -v\n"); | |
107 | options &= ~VERIFY; | |
108 | } | |
29e83471 RC |
109 | nflag++; |
110 | break; | |
111 | ||
112 | case 'q': | |
113 | qflag++; | |
114 | break; | |
115 | ||
024fde5b RC |
116 | case 'b': |
117 | options |= COMPARE; | |
118 | break; | |
119 | ||
d6bccb44 | 120 | case 'R': |
d1dee8e8 RC |
121 | options |= REMOVE; |
122 | break; | |
123 | ||
29e83471 | 124 | case 'v': |
d6bccb44 RC |
125 | if (nflag) { |
126 | printf("rdist: -n overrides -v\n"); | |
127 | break; | |
128 | } | |
f7770429 RC |
129 | options |= VERIFY; |
130 | break; | |
131 | ||
132 | case 'w': | |
133 | options |= WHOLE; | |
29e83471 RC |
134 | break; |
135 | ||
136 | case 'y': | |
f7770429 | 137 | options |= YOUNGER; |
29e83471 RC |
138 | break; |
139 | ||
a3e6fd64 RC |
140 | case 'h': |
141 | options |= FOLLOW; | |
142 | break; | |
143 | ||
144 | case 'i': | |
145 | options |= IGNLNKS; | |
146 | break; | |
147 | ||
29e83471 RC |
148 | default: |
149 | usage(); | |
150 | } | |
151 | } | |
78a18ca3 | 152 | *hp = NULL; |
82572cb6 | 153 | |
80babee1 | 154 | setreuid(0, userid); |
82572cb6 | 155 | mktemp(tmpfile); |
0fccdfef | 156 | |
29e83471 RC |
157 | if (iamremote) { |
158 | server(); | |
80babee1 | 159 | exit(nerrs != 0); |
29e83471 | 160 | } |
29e83471 | 161 | |
f7770429 RC |
162 | if (cmdargs) |
163 | docmdargs(argc, argv); | |
164 | else { | |
be5872f6 JB |
165 | if (fin == NULL) { |
166 | if(distfile == NULL) { | |
167 | if((fin = fopen("distfile","r")) == NULL) | |
168 | fin = fopen("Distfile", "r"); | |
169 | } else | |
170 | fin = fopen(distfile, "r"); | |
171 | if(fin == NULL) { | |
172 | perror(distfile ? distfile : "distfile"); | |
173 | exit(1); | |
174 | } | |
f7770429 RC |
175 | } |
176 | yyparse(); | |
0fccdfef | 177 | if (nerrs == 0) |
78a18ca3 | 178 | docmds(dhosts, argc, argv); |
82572cb6 RC |
179 | } |
180 | ||
80babee1 | 181 | exit(nerrs != 0); |
29e83471 RC |
182 | } |
183 | ||
184 | usage() | |
185 | { | |
78a18ca3 | 186 | printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n"); |
a3e6fd64 | 187 | printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n"); |
82572cb6 RC |
188 | exit(1); |
189 | } | |
190 | ||
f7770429 RC |
191 | /* |
192 | * rcp like interface for distributing files. | |
193 | */ | |
194 | docmdargs(nargs, args) | |
195 | int nargs; | |
196 | char *args[]; | |
197 | { | |
0fccdfef RC |
198 | register struct namelist *nl, *prev; |
199 | register char *cp; | |
200 | struct namelist *files, *hosts; | |
201 | struct subcmd *cmds; | |
202 | char *dest; | |
203 | static struct namelist tnl = { NULL, NULL }; | |
f7770429 | 204 | int i; |
f7770429 RC |
205 | |
206 | if (nargs < 2) | |
207 | usage(); | |
208 | ||
209 | prev = NULL; | |
0fccdfef RC |
210 | for (i = 0; i < nargs - 1; i++) { |
211 | nl = makenl(args[i]); | |
212 | if (prev == NULL) | |
213 | files = prev = nl; | |
214 | else { | |
215 | prev->n_next = nl; | |
216 | prev = nl; | |
217 | } | |
f7770429 RC |
218 | } |
219 | ||
0fccdfef RC |
220 | cp = args[i]; |
221 | if ((dest = index(cp, ':')) != NULL) | |
222 | *dest++ = '\0'; | |
223 | tnl.n_name = cp; | |
224 | hosts = expand(&tnl, E_ALL); | |
78a18ca3 RC |
225 | if (nerrs) |
226 | exit(1); | |
f7770429 | 227 | |
0fccdfef | 228 | if (dest == NULL || *dest == '\0') |
f7770429 RC |
229 | cmds = NULL; |
230 | else { | |
0fccdfef RC |
231 | cmds = makesubcmd(INSTALL); |
232 | cmds->sc_options = options; | |
233 | cmds->sc_name = dest; | |
f7770429 RC |
234 | } |
235 | ||
236 | if (debug) { | |
237 | printf("docmdargs()\nfiles = "); | |
238 | prnames(files); | |
239 | printf("hosts = "); | |
240 | prnames(hosts); | |
241 | } | |
032b4373 | 242 | insert(NULL, files, hosts, cmds); |
78a18ca3 | 243 | docmds(NULL, 0, NULL); |
29e83471 RC |
244 | } |
245 | ||
246 | /* | |
247 | * Print a list of NAME blocks (mostly for debugging). | |
248 | */ | |
0fccdfef RC |
249 | prnames(nl) |
250 | register struct namelist *nl; | |
29e83471 RC |
251 | { |
252 | printf("( "); | |
0fccdfef RC |
253 | while (nl != NULL) { |
254 | printf("%s ", nl->n_name); | |
255 | nl = nl->n_next; | |
29e83471 RC |
256 | } |
257 | printf(")\n"); | |
258 | } | |
259 | ||
260 | /*VARARGS*/ | |
261 | warn(fmt, a1, a2,a3) | |
262 | char *fmt; | |
263 | { | |
264 | extern int yylineno; | |
265 | ||
266 | fprintf(stderr, "rdist: line %d: Warning: ", yylineno); | |
267 | fprintf(stderr, fmt, a1, a2, a3); | |
268 | fputc('\n', stderr); | |
269 | } |