Merge from vendor branch NTPD:
[dragonfly.git] / sbin / rconfig / rconfig.c
1 /*
2  * rconfig - Remote configurator
3  *
4  *      rconfig [-W workingdir] [server_ip[:tag]]
5  *      rconfig [-f configfile] -s
6  * 
7  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
8  * 
9  * This code is derived from software contributed to The DragonFly Project
10  * by Matthew Dillon <dillon@backplane.com>
11  * 
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in
20  *    the documentation and/or other materials provided with the
21  *    distribution.
22  * 3. Neither the name of The DragonFly Project nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific, prior written permission.
25  * 
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
30  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
32  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
34  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
36  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  * $DragonFly: src/sbin/rconfig/rconfig.c,v 1.3 2004/08/19 23:57:02 dillon Exp $
40  */
41
42 #include "defs.h"
43
44 const char *WorkDir = "/tmp";
45 const char *ConfigFiles = "/etc/defaults/rconfig.conf:/etc/rconfig.conf";
46 const char *TagDir = "/usr/local/etc/rconfig";
47 tag_t AddrBase;
48 tag_t VarBase;
49 int VerboseOpt;
50
51 static void usage(int code);
52 static void addTag(tag_t *basep, const char *tag, int flags);
53
54 int
55 main(int ac, char **av)
56 {
57     int ch;
58     int i;
59     int serverMode = 0;
60     
61     while ((ch = getopt(ac, av, "aD:W:irt:f:sv")) != -1) {
62         switch(ch) {
63         case 'a':       /* auto tag / standard broadcast */
64             addTag(&AddrBase, NULL, 0);
65             break;
66         case 'W':       /* specify working directory */
67             WorkDir = optarg;
68             break;
69         case 'T':
70             TagDir = optarg;
71             break;
72         case 'C':       /* specify server config file(s) (colon delimited) */
73             ConfigFiles = optarg;
74             break;
75         case 's':       /* run as server using config file */
76             serverMode = 1;
77             break;
78         case 'v':
79             VerboseOpt = 1;
80             break;
81         default:
82             usage(1);
83             /* not reached */
84         }
85     }
86     for (i = optind; i < ac; ++i) {
87         if (strchr(av[i], '='))
88             addTag(&VarBase, av[i], 0);
89         else
90             addTag(&AddrBase, av[i], 0);
91     }
92     if (AddrBase == NULL)
93         usage(1);
94     if (AddrBase && AddrBase->name == NULL && AddrBase->next) {
95         fprintf(stderr,
96                 "You cannot specify both -a AND a list of hosts.  If you want\n"
97                 "to use auto-broadcast mode with a tag other then 'auto',\n"
98                 "just specify the tag without a host, e.g. ':<tag>'\n");
99         exit(1);
100     }
101     if (serverMode)
102         doServer();
103     else
104         doClient();
105     return(0);
106 }
107
108 static
109 void
110 addTag(tag_t *basep, const char *name, int flags)
111 {
112     tag_t tag = calloc(sizeof(struct tag), 1);
113
114     while ((*basep) != NULL)
115         basep = &(*basep)->next;
116
117     tag->name = name;
118     tag->flags = flags;
119     *basep = tag;
120 }
121
122 static void
123 usage(int code)
124 {
125     fprintf(stderr, "rconfig [-W workdir] [-f servconfig] "
126                     "[-s] [var=data]* [server_ip[:tag]]* \n");
127     exit(code);
128 }
129