ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpectrl.C
Revision: 1.14
Committed: Fri Mar 18 01:53:05 2005 UTC (19 years, 1 month ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.13: +0 -0 lines
State: FILE REMOVED
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 vpectrl.C -- the main file for gvpectrl
3 Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>
4 2000-2002 Guus Sliepen <guus@sliepen.eu.org>
5 2003-2005 Marc Lehmann <gvpe@schmorp.de>
6
7 This file is part of GVPE.
8
9 GVPE is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with gvpe; if not, write to the Free Software
21 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include "config.h"
25
26 #include <cstdio>
27 #include <cstring>
28 #include <cstdlib>
29 #include <locale.h>
30
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <getopt.h>
34 #include <signal.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38 #include <signal.h>
39
40 #include <openssl/rand.h>
41 #include <openssl/rsa.h>
42 #include <openssl/pem.h>
43 #include <openssl/evp.h>
44
45 #include "pidfile.h"
46
47 #include "gettext.h"
48
49 #include "conf.h"
50 #include "slog.h"
51 #include "util.h"
52 #include "vpn.h"
53
54 /* If nonzero, display usage information and exit. */
55 static int show_help;
56
57 /* If nonzero, print the version on standard output and exit. */
58 static int show_version;
59
60 /* If nonzero, it will attempt to kill a running vped and exit. */
61 static int kill_vped;
62
63 /* If nonzero, it will attempt to kill a running vped and exit. */
64 static int show_config;
65
66 /* If nonzero, generate public/private keypair for this net. */
67 static int generate_keys;
68
69 static struct option const long_options[] =
70 {
71 {"config", required_argument, NULL, 'c'},
72 {"kill", optional_argument, NULL, 'k'},
73 {"help", no_argument, &show_help, 1},
74 {"version", no_argument, &show_version, 1},
75 {"generate-keys", no_argument, NULL, 'g'},
76 {"show-config", no_argument, &show_config, 's'},
77 {NULL, 0, NULL, 0}
78 };
79
80 static void
81 usage (int status)
82 {
83 if (status != 0)
84 fprintf (stderr, _("Try `%s --help\' for more information.\n"), get_identity ());
85 else
86 {
87 printf (_("Usage: %s [option]...\n\n"), get_identity ());
88 printf (_
89 (" -c, --config=DIR Read configuration options from DIR.\n"
90 " -k, --kill[=SIGNAL] Attempt to kill a running vped and exit.\n"
91 " -g, --generate-keys Generate public/private RSA keypair.\n"
92 " -s, --show-config Display the configuration information.\n"
93 " --help Display this help and exit.\n"
94 " --version Output version information and exit.\n\n"));
95 printf (_("Report bugs to <vpe@plan9.de>.\n"));
96 }
97
98 exit (status);
99 }
100
101 void
102 parse_options (int argc, char **argv, char **envp)
103 {
104 int r;
105 int option_index = 0;
106
107 while ((r =
108 getopt_long (argc, argv, "c:k::gs", long_options,
109 &option_index)) != EOF)
110 {
111 switch (r)
112 {
113 case 0: /* long option */
114 break;
115
116 case 'c': /* config file */
117 confbase = strdup (optarg);
118 break;
119
120 case 'k': /* kill old vpeds */
121 if (optarg)
122 {
123 if (!strcasecmp (optarg, "HUP"))
124 kill_vped = SIGHUP;
125 else if (!strcasecmp (optarg, "TERM"))
126 kill_vped = SIGTERM;
127 else if (!strcasecmp (optarg, "KILL"))
128 kill_vped = SIGKILL;
129 else if (!strcasecmp (optarg, "USR1"))
130 kill_vped = SIGUSR1;
131 else if (!strcasecmp (optarg, "USR2"))
132 kill_vped = SIGUSR2;
133 else if (!strcasecmp (optarg, "INT"))
134 kill_vped = SIGINT;
135 else if (!strcasecmp (optarg, "ALRM"))
136 kill_vped = SIGALRM;
137 else
138 {
139 kill_vped = atoi (optarg);
140
141 if (!kill_vped)
142 {
143 fprintf (stderr,
144 _
145 ("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"),
146 optarg);
147 usage (1);
148 }
149 }
150 }
151 else
152 kill_vped = SIGTERM;
153
154 break;
155
156 case 'g': /* generate public/private keypair */
157 generate_keys = RSA_KEYBITS;
158 break;
159
160 case 's':
161 show_config = 1;
162 break;
163
164 case '?':
165 usage (1);
166
167 default:
168 break;
169 }
170 }
171 }
172
173 /* This function prettyprints the key generation process */
174
175 void
176 indicator (int a, int b, void *p)
177 {
178 switch (a)
179 {
180 case 0:
181 fprintf (stderr, ".");
182 break;
183
184 case 1:
185 fprintf (stderr, "+");
186 break;
187
188 case 2:
189 fprintf (stderr, "-");
190 break;
191
192 case 3:
193 switch (b)
194 {
195 case 0:
196 fprintf (stderr, " p\n");
197 break;
198
199 case 1:
200 fprintf (stderr, " q\n");
201 break;
202
203 default:
204 fprintf (stderr, "?");
205 }
206 break;
207
208 default:
209 fprintf (stderr, "?");
210 }
211 }
212
213 /*
214 * generate public/private RSA keypairs for all hosts that don't have one.
215 */
216 int
217 keygen (int bits)
218 {
219 RSA *rsa_key;
220 FILE *f;
221 char *name = NULL;
222 char *fname;
223
224 asprintf (&fname, "%s/hostkeys", confbase);
225 mkdir (fname, 0700);
226 free (fname);
227
228 asprintf (&fname, "%s/pubkey", confbase);
229 mkdir (fname, 0700);
230 free (fname);
231
232 for (configuration::node_vector::iterator i = conf.nodes.begin (); i != conf.nodes.end (); ++i)
233 {
234 conf_node *node = *i;
235
236 asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename);
237
238 f = fopen (fname, "a");
239
240 if (!f)
241 {
242 perror (fname);
243 exit (EXIT_FAILURE);
244 }
245
246 if (ftell (f))
247 {
248 fprintf (stderr, "'%s' already exists, skipping this node\n",
249 fname);
250 fclose (f);
251 continue;
252 }
253
254 fprintf (stderr, _("generating %d bits key for %s:\n"), bits,
255 node->nodename);
256
257 rsa_key = RSA_generate_key (bits, 0xFFFF, indicator, NULL);
258
259 if (!rsa_key)
260 {
261 fprintf (stderr, _("error during key generation!\n"));
262 return -1;
263 }
264 else
265 fprintf (stderr, _("Done.\n"));
266
267 require (PEM_write_RSAPublicKey (f, rsa_key));
268 fclose (f);
269 free (fname);
270
271 asprintf (&fname, "%s/hostkeys/%s", confbase, node->nodename);
272
273 f = fopen (fname, "a");
274 if (!f)
275 {
276 perror (fname);
277 exit (EXIT_FAILURE);
278 }
279
280 require (PEM_write_RSAPrivateKey (f, rsa_key, NULL, NULL, 0, NULL, NULL));
281 fclose (f);
282 free (fname);
283 }
284
285 return 0;
286 }
287
288 int
289 main (int argc, char **argv, char **envp)
290 {
291 set_identity (argv[0]);
292 log_to (LOGTO_STDERR);
293
294 setlocale (LC_ALL, "");
295 bindtextdomain (PACKAGE, LOCALEDIR);
296 textdomain (PACKAGE);
297
298 parse_options (argc, argv, envp);
299
300 if (show_version)
301 {
302 printf (_("%s version %s (built %s %s, protocol %d.%d)\n"), get_identity (),
303 VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR);
304 printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE);
305 printf (_
306 ("Copyright (C) 2003 Marc Lehmann <vpe@plan9.de> and others.\n"
307 "See the AUTHORS file for a complete list.\n\n"
308 "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
309 "and you are welcome to redistribute it under certain conditions;\n"
310 "see the file COPYING for details.\n"));
311
312 return 0;
313 }
314
315 if (show_help)
316 usage (0);
317
318 conf.read_config (false);
319
320 if (generate_keys)
321 {
322 RAND_load_file ("/dev/urandom", 1024);
323 exit (keygen (generate_keys));
324 }
325
326 if (kill_vped)
327 exit (kill_other (kill_vped));
328
329 if (show_config)
330 {
331 conf.print ();
332 exit (EXIT_SUCCESS);
333 }
334
335 usage (1);
336 }