1 | /* |
1 | /* |
2 | gvpectrl.C -- the main file for gvpectrl |
2 | gvpectrl.C -- the main file for gvpectrl |
3 | Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl> |
3 | Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl> |
4 | 2000-2002 Guus Sliepen <guus@sliepen.eu.org> |
4 | 2000-2002 Guus Sliepen <guus@sliepen.eu.org> |
5 | 2003-2008 Marc Lehmann <gvpe@schmorp.de> |
5 | 2003-2013 Marc Lehmann <gvpe@schmorp.de> |
6 | |
6 | |
7 | This file is part of GVPE. |
7 | This file is part of GVPE. |
8 | |
8 | |
9 | GVPE is free software; you can redistribute it and/or modify it |
9 | GVPE is free software; you can redistribute it and/or modify it |
10 | under the terms of the GNU General Public License as published by the |
10 | under the terms of the GNU General Public License as published by the |
… | |
… | |
44 | #include <sys/stat.h> |
44 | #include <sys/stat.h> |
45 | #include <sys/types.h> |
45 | #include <sys/types.h> |
46 | #include <unistd.h> |
46 | #include <unistd.h> |
47 | #include <signal.h> |
47 | #include <signal.h> |
48 | |
48 | |
|
|
49 | #include <openssl/bn.h> |
49 | #include <openssl/rand.h> |
50 | #include <openssl/rand.h> |
50 | #include <openssl/rsa.h> |
51 | #include <openssl/rsa.h> |
51 | #include <openssl/pem.h> |
52 | #include <openssl/pem.h> |
52 | #include <openssl/evp.h> |
53 | #include <openssl/evp.h> |
53 | |
54 | |
… | |
… | |
68 | static int kill_gvpe; |
69 | static int kill_gvpe; |
69 | |
70 | |
70 | /* If nonzero, it will attempt to kill a running gvpe and exit. */ |
71 | /* If nonzero, it will attempt to kill a running gvpe and exit. */ |
71 | static int show_config; |
72 | static int show_config; |
72 | |
73 | |
|
|
74 | /* If nonzero, do not output anything but warnings/errors/very unusual conditions */ |
|
|
75 | static int quiet; |
|
|
76 | |
73 | /* If nonzero, generate public/private keypair for this net. */ |
77 | /* If nonzero, generate public/private keypair for this net. */ |
74 | static int generate_keys; |
78 | static int generate_keys; |
75 | |
79 | |
76 | static struct option const long_options[] = |
80 | static struct option const long_options[] = |
77 | { |
81 | { |
78 | {"config", required_argument, NULL, 'c'}, |
82 | {"config", required_argument, NULL, 'c'}, |
79 | {"kill", optional_argument, NULL, 'k'}, |
83 | {"kill", optional_argument, NULL, 'k'}, |
80 | {"help", no_argument, &show_help, 1}, |
84 | {"help", no_argument, &show_help, 1}, |
81 | {"version", no_argument, &show_version, 1}, |
85 | {"version", no_argument, &show_version, 1}, |
82 | {"generate-keys", no_argument, NULL, 'g'}, |
86 | {"generate-keys", no_argument, NULL, 'g'}, |
|
|
87 | {"quiet", no_argument, &quiet, 1}, |
83 | {"show-config", no_argument, &show_config, 's'}, |
88 | {"show-config", no_argument, &show_config, 's'}, |
84 | {NULL, 0, NULL, 0} |
89 | {NULL, 0, NULL, 0} |
85 | }; |
90 | }; |
86 | |
91 | |
87 | static void |
92 | static void |
88 | usage (int status) |
93 | usage (int status) |
89 | { |
94 | { |
90 | if (status != 0) |
95 | if (status != 0) |
… | |
… | |
95 | printf (_ |
100 | printf (_ |
96 | (" -c, --config=DIR Read configuration options from DIR.\n" |
101 | (" -c, --config=DIR Read configuration options from DIR.\n" |
97 | " -k, --kill[=SIGNAL] Attempt to kill a running gvpe and exit.\n" |
102 | " -k, --kill[=SIGNAL] Attempt to kill a running gvpe and exit.\n" |
98 | " -g, --generate-keys Generate public/private RSA keypair.\n" |
103 | " -g, --generate-keys Generate public/private RSA keypair.\n" |
99 | " -s, --show-config Display the configuration information.\n" |
104 | " -s, --show-config Display the configuration information.\n" |
|
|
105 | " -q, --quiet Be quite quiet.\n" |
100 | " --help Display this help and exit.\n" |
106 | " --help Display this help and exit.\n" |
101 | " --version Output version information and exit.\n\n")); |
107 | " --version Output version information and exit.\n\n")); |
102 | printf (_("Report bugs to <gvpe@schmorp.de>.\n")); |
108 | printf (_("Report bugs to <gvpe@schmorp.de>.\n")); |
103 | } |
109 | } |
104 | |
110 | |
105 | exit (status); |
111 | exit (status); |
106 | } |
112 | } |
107 | |
113 | |
108 | void |
114 | static void |
109 | parse_options (int argc, char **argv, char **envp) |
115 | parse_options (int argc, char **argv, char **envp) |
110 | { |
116 | { |
111 | int r; |
117 | int r; |
112 | int option_index = 0; |
118 | int option_index = 0; |
113 | |
119 | |
114 | while ((r = |
120 | while ((r = getopt_long (argc, argv, "c:k::qgs", long_options, &option_index)) != EOF) |
115 | getopt_long (argc, argv, "c:k::gs", long_options, |
|
|
116 | &option_index)) != EOF) |
|
|
117 | { |
121 | { |
118 | switch (r) |
122 | switch (r) |
119 | { |
123 | { |
120 | case 0: /* long option */ |
124 | case 0: /* long option */ |
121 | break; |
125 | break; |
122 | |
126 | |
123 | case 'c': /* config file */ |
127 | case 'c': /* config file */ |
124 | confbase = strdup (optarg); |
128 | confbase = strdup (optarg); |
125 | break; |
129 | break; |
126 | |
130 | |
127 | case 'k': /* kill old gvpes */ |
131 | case 'k': /* kill old gvpes */ |
128 | if (optarg) |
132 | if (optarg) |
129 | { |
133 | { |
130 | if (!strcasecmp (optarg, "HUP")) |
134 | if (!strcasecmp (optarg, "HUP")) |
131 | kill_gvpe = SIGHUP; |
135 | kill_gvpe = SIGHUP; |
132 | else if (!strcasecmp (optarg, "TERM")) |
136 | else if (!strcasecmp (optarg, "TERM")) |
133 | kill_gvpe = SIGTERM; |
137 | kill_gvpe = SIGTERM; |
134 | else if (!strcasecmp (optarg, "KILL")) |
138 | else if (!strcasecmp (optarg, "KILL")) |
135 | kill_gvpe = SIGKILL; |
139 | kill_gvpe = SIGKILL; |
136 | else if (!strcasecmp (optarg, "USR1")) |
140 | else if (!strcasecmp (optarg, "USR1")) |
137 | kill_gvpe = SIGUSR1; |
141 | kill_gvpe = SIGUSR1; |
138 | else if (!strcasecmp (optarg, "USR2")) |
142 | else if (!strcasecmp (optarg, "USR2")) |
139 | kill_gvpe = SIGUSR2; |
143 | kill_gvpe = SIGUSR2; |
140 | else if (!strcasecmp (optarg, "INT")) |
144 | else if (!strcasecmp (optarg, "INT")) |
141 | kill_gvpe = SIGINT; |
145 | kill_gvpe = SIGINT; |
142 | else if (!strcasecmp (optarg, "ALRM")) |
146 | else if (!strcasecmp (optarg, "ALRM")) |
143 | kill_gvpe = SIGALRM; |
147 | kill_gvpe = SIGALRM; |
144 | else |
148 | else |
145 | { |
149 | { |
146 | kill_gvpe = atoi (optarg); |
150 | kill_gvpe = atoi (optarg); |
147 | |
151 | |
148 | if (!kill_gvpe) |
152 | if (!kill_gvpe) |
149 | { |
153 | { |
150 | fprintf (stderr, |
154 | fprintf (stderr, |
151 | _ |
155 | _ |
152 | ("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"), |
156 | ("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"), |
153 | optarg); |
157 | optarg); |
154 | usage (1); |
158 | usage (1); |
155 | } |
159 | } |
156 | } |
160 | } |
157 | } |
161 | } |
158 | else |
162 | else |
159 | kill_gvpe = SIGTERM; |
163 | kill_gvpe = SIGTERM; |
160 | |
164 | |
161 | break; |
165 | break; |
162 | |
166 | |
163 | case 'g': /* generate public/private keypair */ |
167 | case 'g': /* generate public/private keypair */ |
164 | generate_keys = RSA_KEYBITS; |
168 | generate_keys = RSA_KEYBITS; |
165 | break; |
169 | break; |
166 | |
170 | |
167 | case 's': |
171 | case 's': |
168 | show_config = 1; |
172 | show_config = 1; |
169 | break; |
173 | break; |
170 | |
174 | |
|
|
175 | case 'q': |
|
|
176 | quiet = 1; |
|
|
177 | break; |
|
|
178 | |
171 | case '?': |
179 | case '?': |
172 | usage (1); |
180 | usage (1); |
173 | |
181 | |
174 | default: |
182 | default: |
175 | break; |
183 | break; |
176 | } |
184 | } |
177 | } |
185 | } |
178 | } |
186 | } |
179 | |
187 | |
180 | /* This function prettyprints the key generation process */ |
188 | // this function prettyprints the key generation process |
181 | |
189 | static int |
182 | void |
|
|
183 | indicator (int a, int b, void *p) |
190 | indicator (int a, int b, BN_GENCB *cb) |
184 | { |
191 | { |
|
|
192 | if (quiet) |
|
|
193 | return 1; |
|
|
194 | |
185 | switch (a) |
195 | switch (a) |
186 | { |
196 | { |
187 | case 0: |
197 | case 0: |
188 | fprintf (stderr, "."); |
198 | fprintf (stderr, "."); |
189 | break; |
199 | break; |
190 | |
200 | |
191 | case 1: |
201 | case 1: |
192 | fprintf (stderr, "+"); |
202 | fprintf (stderr, "+"); |
193 | break; |
203 | break; |
194 | |
204 | |
195 | case 2: |
205 | case 2: |
196 | fprintf (stderr, "-"); |
206 | fprintf (stderr, "-"); |
197 | break; |
207 | break; |
198 | |
208 | |
199 | case 3: |
209 | case 3: |
200 | switch (b) |
210 | switch (b) |
201 | { |
211 | { |
202 | case 0: |
212 | case 0: |
203 | fprintf (stderr, " p\n"); |
213 | fprintf (stderr, " p\n"); |
204 | break; |
214 | break; |
205 | |
215 | |
206 | case 1: |
216 | case 1: |
207 | fprintf (stderr, " q\n"); |
217 | fprintf (stderr, " q\n"); |
208 | break; |
218 | break; |
209 | |
219 | |
210 | default: |
220 | default: |
211 | fprintf (stderr, "?"); |
221 | fprintf (stderr, "?"); |
212 | } |
222 | } |
213 | break; |
223 | break; |
214 | |
224 | |
215 | default: |
225 | default: |
216 | fprintf (stderr, "?"); |
226 | fprintf (stderr, "?"); |
217 | } |
227 | } |
|
|
228 | |
|
|
229 | return 1; |
218 | } |
230 | } |
219 | |
231 | |
220 | /* |
232 | /* |
221 | * generate public/private RSA keypairs for all hosts that don't have one. |
233 | * generate public/private RSA keypairs for all hosts that don't have one. |
222 | */ |
234 | */ |
223 | int |
235 | static int |
224 | keygen (int bits) |
236 | keygen (int bits) |
225 | { |
237 | { |
226 | RSA *rsa_key; |
|
|
227 | FILE *f; |
238 | FILE *f; |
228 | char *name = NULL; |
239 | char *name = NULL; |
229 | char *fname; |
240 | char *fname; |
230 | |
241 | |
231 | asprintf (&fname, "%s/hostkeys", confbase); |
242 | asprintf (&fname, "%s/hostkeys", confbase); |
… | |
… | |
242 | |
253 | |
243 | asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename); |
254 | asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename); |
244 | |
255 | |
245 | f = fopen (fname, "a"); |
256 | f = fopen (fname, "a"); |
246 | |
257 | |
247 | if (!f) |
258 | /* some libcs are buggy and require an extra seek to the end */ |
|
|
259 | if (!f || fseek (f, 0, SEEK_END)) |
248 | { |
260 | { |
249 | perror (fname); |
261 | perror (fname); |
250 | exit (EXIT_FAILURE); |
262 | exit (EXIT_FAILURE); |
251 | } |
263 | } |
252 | |
264 | |
253 | if (ftell (f)) |
265 | if (ftell (f)) |
254 | { |
266 | { |
|
|
267 | if (!quiet) |
255 | fprintf (stderr, "'%s' already exists, skipping this node\n", |
268 | fprintf (stderr, "'%s' already exists, skipping this node %d\n", |
256 | fname); |
269 | fname, quiet); |
|
|
270 | |
257 | fclose (f); |
271 | fclose (f); |
258 | continue; |
272 | continue; |
259 | } |
273 | } |
260 | |
274 | |
261 | fprintf (stderr, _("generating %d bits key for %s:\n"), bits, |
275 | fprintf (stderr, _("generating %d bits key for %s:\n"), bits, |
262 | node->nodename); |
276 | node->nodename); |
263 | |
277 | |
264 | rsa_key = RSA_generate_key (bits, 0xFFFF, indicator, NULL); |
278 | RSA *rsa = RSA_new (); |
|
|
279 | BIGNUM *e = BN_new (); |
|
|
280 | BN_set_bit (e, 0); BN_set_bit (e, 16); // 0x10001, 65537 |
|
|
281 | BN_GENCB cb; |
|
|
282 | BN_GENCB_set (&cb, indicator, 0); |
265 | |
283 | |
266 | if (!rsa_key) |
284 | require (RSA_generate_key_ex (rsa, bits, e, &cb)); |
267 | { |
285 | |
268 | fprintf (stderr, _("error during key generation!\n")); |
|
|
269 | return -1; |
|
|
270 | } |
|
|
271 | else |
|
|
272 | fprintf (stderr, _("Done.\n")); |
286 | fprintf (stderr, _("Done.\n")); |
273 | |
287 | |
274 | require (PEM_write_RSAPublicKey (f, rsa_key)); |
288 | require (PEM_write_RSAPublicKey (f, rsa)); |
275 | fclose (f); |
289 | fclose (f); |
276 | free (fname); |
290 | free (fname); |
277 | |
291 | |
278 | asprintf (&fname, "%s/hostkeys/%s", confbase, node->nodename); |
292 | asprintf (&fname, "%s/hostkeys/%s", confbase, node->nodename); |
279 | |
293 | |
… | |
… | |
282 | { |
296 | { |
283 | perror (fname); |
297 | perror (fname); |
284 | exit (EXIT_FAILURE); |
298 | exit (EXIT_FAILURE); |
285 | } |
299 | } |
286 | |
300 | |
287 | require (PEM_write_RSAPrivateKey (f, rsa_key, NULL, NULL, 0, NULL, NULL)); |
301 | require (PEM_write_RSAPrivateKey (f, rsa, NULL, NULL, 0, NULL, NULL)); |
288 | fclose (f); |
302 | fclose (f); |
289 | free (fname); |
303 | free (fname); |
|
|
304 | |
|
|
305 | BN_free (e); |
|
|
306 | RSA_free (rsa); |
290 | } |
307 | } |
291 | |
308 | |
292 | return 0; |
309 | return 0; |
293 | } |
310 | } |
294 | |
311 | |
… | |
… | |
308 | { |
325 | { |
309 | printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), |
326 | printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), |
310 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
327 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
311 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
328 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
312 | printf (_ |
329 | printf (_ |
313 | ("Copyright (C) 2003 Marc Lehmann <gvpe@schmorp.de> and others.\n" |
330 | ("Copyright (C) 2003-2013 Marc Lehmann <gvpe@schmorp.de> and others.\n" |
314 | "See the AUTHORS file for a complete list.\n\n" |
331 | "See the AUTHORS file for a complete list.\n\n" |
315 | "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
332 | "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
316 | "and you are welcome to redistribute it under certain conditions;\n" |
333 | "and you are welcome to redistribute it under certain conditions;\n" |
317 | "see the file COPYING for details.\n")); |
334 | "see the file COPYING for details.\n")); |
318 | |
335 | |
… | |
… | |
326 | configuration_parser (conf, false, 0, 0); |
343 | configuration_parser (conf, false, 0, 0); |
327 | } |
344 | } |
328 | |
345 | |
329 | if (generate_keys) |
346 | if (generate_keys) |
330 | { |
347 | { |
331 | RAND_load_file ("/dev/urandom", 1024); |
348 | RAND_load_file (conf.seed_dev, SEED_SIZE); |
332 | exit (keygen (generate_keys)); |
349 | exit (keygen (generate_keys)); |
333 | } |
350 | } |
334 | |
351 | |
335 | if (kill_gvpe) |
352 | if (kill_gvpe) |
336 | exit (kill_other (kill_gvpe)); |
353 | exit (kill_other (kill_gvpe)); |