Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[dragonfly.git] / usr.bin / rdist / main.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1983, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)main.c   8.1 (Berkeley) 6/9/93
31  * $FreeBSD: src/usr.bin/rdist/main.c,v 1.5 1999/08/28 01:05:07 peter Exp $
32  */
33 /*
34  * Remote distribution program.
35  */
36
37 #include <stdarg.h>
38
39 #include "defs.h"
40
41 #define NHOSTS 100
42 #define _RDIST_TMP      "/rdistXXXXXX"
43
44 char    *distfile = NULL;
45 char    tempfile[sizeof _PATH_TMP + sizeof _RDIST_TMP + 1];
46 char    *tempname;
47
48 int     debug;          /* debugging flag */
49 int     nflag;          /* NOP flag, just print commands without executing */
50 int     qflag;          /* Quiet. Don't print messages */
51 int     options;        /* global options */
52 int     iamremote;      /* act as remote server for transfering files */
53
54 FILE    *fin = NULL;    /* input file pointer */
55 int     rem = -1;       /* file descriptor to remote source/sink process */
56 char    host[32];       /* host name */
57 int     nerrs;          /* number of errors while sending/receiving */
58 char    user[10];       /* user's name */
59 char    homedir[128];   /* user's home directory */
60 int     userid;         /* user's user ID */
61 int     groupid;        /* user's group ID */
62 char    *path_rsh = _PATH_RSH;  /* rsh (or equiv command) path */
63
64 struct  passwd *pw;     /* pointer to static area used by getpwent */
65 struct  group *gr;      /* pointer to static area used by getgrent */
66
67 static void usage(void);
68 static void docmdargs(int, char *[]);
69
70 int
71 main(int argc, char **argv)
72 {
73         char *arg;
74         int cmdargs;
75         char *dhosts[NHOSTS], **hp = dhosts;
76
77         cmdargs = 0;
78
79         pw = getpwuid(userid = getuid());
80         if (pw == NULL) {
81                 fprintf(stderr, "%s: Who are you?\n", argv[0]);
82                 exit(1);
83         }
84         strcpy(user, pw->pw_name);
85         strcpy(homedir, pw->pw_dir);
86         groupid = pw->pw_gid;
87         gethostname(host, sizeof(host));
88         strcpy(tempfile, _PATH_TMP);
89         strcat(tempfile, _RDIST_TMP);
90         if ((tempname = rindex(tempfile, '/')) != NULL)
91                 tempname++;
92         else
93                 tempname = tempfile;
94
95         while (--argc > 0) {
96                 if ((arg = *++argv)[0] != '-')
97                         break;
98                 if (!strcmp(arg, "-Server"))
99                         iamremote++;
100                 else while (*++arg)
101                         switch (*arg) {
102                         case 'P':
103                                 if (--argc <= 0)
104                                         usage();
105                                 path_rsh = *++argv;
106                                 break;
107
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
190         seteuid(userid);
191         mktemp(tempfile);
192
193         if (iamremote) {
194                 server();
195                 exit(nerrs != 0);
196         }
197
198         if (cmdargs)
199                 docmdargs(argc, argv);
200         else {
201                 if (fin == NULL) {
202                         if(distfile == NULL) {
203                                 if((fin = fopen("distfile","r")) == NULL)
204                                         fin = fopen("Distfile", "r");
205                         } else
206                                 fin = fopen(distfile, "r");
207                         if(fin == NULL) {
208                                 perror(distfile ? distfile : "distfile");
209                                 exit(1);
210                         }
211                 }
212                 yyparse();
213                 if (nerrs == 0)
214                         docmds(dhosts, argc, argv);
215         }
216
217         exit(nerrs != 0);
218 }
219
220 static void
221 usage(void)
222 {
223         printf("%s\n%s\n%s\n",
224 "usage: rdist [-nqbhirvwyD] [-P /path/to/rsh ] [-f distfile] [-d var=value]",
225 "             [-m host] [file ...]",
226 "       rdist [-nqbhirvwyD] [-P /path/to/rsh ] -c source [...] machine[:dest]");
227         exit(1);
228 }
229
230 /*
231  * rcp like interface for distributing files.
232  */
233 static void
234 docmdargs(int nargs, char **args)
235 {
236         struct namelist *nl, *prev;
237         char *cp;
238         struct namelist *files, *hosts;
239         struct subcmd *cmds;
240         char *dest;
241         static struct namelist tnl = { NULL, NULL };
242         int i;
243
244         files = NULL;
245
246         if (nargs < 2)
247                 usage();
248
249         prev = NULL;
250         for (i = 0; i < nargs - 1; i++) {
251                 nl = makenl(args[i]);
252                 if (prev == NULL)
253                         files = prev = nl;
254                 else {
255                         prev->n_next = nl;
256                         prev = nl;
257                 }
258         }
259
260         cp = args[i];
261         if ((dest = index(cp, ':')) != NULL)
262                 *dest++ = '\0';
263         tnl.n_name = cp;
264         hosts = expand(&tnl, E_ALL);
265         if (nerrs)
266                 exit(1);
267
268         if (dest == NULL || *dest == '\0')
269                 cmds = NULL;
270         else {
271                 cmds = makesubcmd(INSTALL);
272                 cmds->sc_options = options;
273                 cmds->sc_name = dest;
274         }
275
276         if (debug) {
277                 printf("docmdargs()\nfiles = ");
278                 prnames(files);
279                 printf("hosts = ");
280                 prnames(hosts);
281         }
282         insert(NULL, files, hosts, cmds);
283         docmds(NULL, 0, NULL);
284 }
285
286 /*
287  * Print a list of NAME blocks (mostly for debugging).
288  */
289 void
290 prnames(struct namelist *nl)
291 {
292         printf("( ");
293         while (nl != NULL) {
294                 printf("%s ", nl->n_name);
295                 nl = nl->n_next;
296         }
297         printf(")\n");
298 }