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 | |
… | |
… | |
74 | static int quiet; |
75 | static int quiet; |
75 | |
76 | |
76 | /* If nonzero, generate public/private keypair for this net. */ |
77 | /* If nonzero, generate public/private keypair for this net. */ |
77 | static int generate_keys; |
78 | static int generate_keys; |
78 | |
79 | |
|
|
80 | // output some debugging info, interna constants &c |
|
|
81 | static int debug_info; |
|
|
82 | |
79 | static struct option const long_options[] = |
83 | static struct option const long_options[] = |
80 | { |
84 | { |
81 | {"config", required_argument, NULL, 'c'}, |
85 | {"config", required_argument, NULL, 'c'}, |
82 | {"kill", optional_argument, NULL, 'k'}, |
86 | {"kill", optional_argument, NULL, 'k'}, |
83 | {"help", no_argument, &show_help, 1}, |
87 | {"help", no_argument, &show_help, 1}, |
84 | {"version", no_argument, &show_version, 1}, |
88 | {"version", no_argument, &show_version, 1}, |
85 | {"generate-keys", no_argument, NULL, 'g'}, |
89 | {"generate-keys", no_argument, NULL, 'g'}, |
86 | {"quiet", no_argument, &quiet, 1}, |
90 | {"quiet", no_argument, &quiet, 1}, |
87 | {"show-config", no_argument, &show_config, 's'}, |
91 | {"show-config", no_argument, &show_config, 's'}, |
|
|
92 | {"debug-info", no_argument, &debug_info, 1}, |
88 | {NULL, 0, NULL, 0} |
93 | {NULL, 0, NULL, 0} |
89 | }; |
94 | }; |
90 | |
95 | |
91 | static void |
96 | static void |
92 | usage (int status) |
97 | usage (int status) |
93 | { |
98 | { |
94 | if (status != 0) |
99 | if (status != 0) |
… | |
… | |
108 | } |
113 | } |
109 | |
114 | |
110 | exit (status); |
115 | exit (status); |
111 | } |
116 | } |
112 | |
117 | |
113 | void |
118 | static void |
114 | parse_options (int argc, char **argv, char **envp) |
119 | parse_options (int argc, char **argv, char **envp) |
115 | { |
120 | { |
116 | int r; |
121 | int r; |
117 | int option_index = 0; |
122 | int option_index = 0; |
118 | |
123 | |
119 | while ((r = getopt_long (argc, argv, "c:k::qgs", long_options, &option_index)) != EOF) |
124 | while ((r = getopt_long (argc, argv, "c:k::qgs", long_options, &option_index)) != EOF) |
120 | { |
125 | { |
121 | switch (r) |
126 | switch (r) |
122 | { |
127 | { |
123 | case 0: /* long option */ |
128 | case 0: /* long option */ |
124 | break; |
129 | break; |
125 | |
130 | |
126 | case 'c': /* config file */ |
131 | case 'c': /* config file */ |
127 | confbase = strdup (optarg); |
132 | confbase = strdup (optarg); |
128 | break; |
133 | break; |
129 | |
134 | |
130 | case 'k': /* kill old gvpes */ |
135 | case 'k': /* kill old gvpes */ |
131 | if (optarg) |
136 | if (optarg) |
132 | { |
137 | { |
133 | if (!strcasecmp (optarg, "HUP")) |
138 | if (!strcasecmp (optarg, "HUP")) |
134 | kill_gvpe = SIGHUP; |
139 | kill_gvpe = SIGHUP; |
135 | else if (!strcasecmp (optarg, "TERM")) |
140 | else if (!strcasecmp (optarg, "TERM")) |
136 | kill_gvpe = SIGTERM; |
141 | kill_gvpe = SIGTERM; |
137 | else if (!strcasecmp (optarg, "KILL")) |
142 | else if (!strcasecmp (optarg, "KILL")) |
138 | kill_gvpe = SIGKILL; |
143 | kill_gvpe = SIGKILL; |
139 | else if (!strcasecmp (optarg, "USR1")) |
144 | else if (!strcasecmp (optarg, "USR1")) |
140 | kill_gvpe = SIGUSR1; |
145 | kill_gvpe = SIGUSR1; |
141 | else if (!strcasecmp (optarg, "USR2")) |
146 | else if (!strcasecmp (optarg, "USR2")) |
142 | kill_gvpe = SIGUSR2; |
147 | kill_gvpe = SIGUSR2; |
143 | else if (!strcasecmp (optarg, "INT")) |
148 | else if (!strcasecmp (optarg, "INT")) |
144 | kill_gvpe = SIGINT; |
149 | kill_gvpe = SIGINT; |
145 | else if (!strcasecmp (optarg, "ALRM")) |
150 | else if (!strcasecmp (optarg, "ALRM")) |
146 | kill_gvpe = SIGALRM; |
151 | kill_gvpe = SIGALRM; |
147 | else |
152 | else |
148 | { |
153 | { |
149 | kill_gvpe = atoi (optarg); |
154 | kill_gvpe = atoi (optarg); |
150 | |
155 | |
151 | if (!kill_gvpe) |
156 | if (!kill_gvpe) |
152 | { |
157 | { |
153 | fprintf (stderr, |
158 | fprintf (stderr, |
154 | _ |
159 | _ |
155 | ("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"), |
160 | ("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"), |
156 | optarg); |
161 | optarg); |
157 | usage (1); |
162 | usage (1); |
158 | } |
163 | } |
159 | } |
164 | } |
160 | } |
165 | } |
161 | else |
166 | else |
162 | kill_gvpe = SIGTERM; |
167 | kill_gvpe = SIGTERM; |
163 | |
168 | |
164 | break; |
169 | break; |
165 | |
170 | |
166 | case 'g': /* generate public/private keypair */ |
171 | case 'g': /* generate public/private keypair */ |
167 | generate_keys = RSA_KEYBITS; |
172 | generate_keys = RSABITS; |
168 | break; |
173 | break; |
169 | |
174 | |
170 | case 's': |
175 | case 's': |
171 | show_config = 1; |
176 | show_config = 1; |
172 | break; |
177 | break; |
173 | |
178 | |
174 | case 'q': |
179 | case 'q': |
175 | quiet = 1; |
180 | quiet = 1; |
176 | break; |
181 | break; |
177 | |
182 | |
178 | case '?': |
183 | case '?': |
179 | usage (1); |
184 | usage (1); |
180 | |
185 | |
181 | default: |
186 | default: |
182 | break; |
187 | break; |
183 | } |
188 | } |
184 | } |
189 | } |
185 | } |
190 | } |
186 | |
191 | |
187 | /* This function prettyprints the key generation process */ |
192 | // this function prettyprints the key generation process |
188 | |
193 | static int |
189 | void |
|
|
190 | indicator (int a, int b, void *p) |
194 | indicator (int a, int b, BN_GENCB *cb) |
191 | { |
195 | { |
192 | if (quiet) |
196 | if (quiet) |
193 | return; |
197 | return 1; |
194 | |
198 | |
195 | switch (a) |
199 | switch (a) |
196 | { |
200 | { |
197 | case 0: |
201 | case 0: |
198 | fprintf (stderr, "."); |
202 | fprintf (stderr, "."); |
199 | break; |
203 | break; |
200 | |
204 | |
201 | case 1: |
205 | case 1: |
202 | fprintf (stderr, "+"); |
206 | fprintf (stderr, "+"); |
203 | break; |
207 | break; |
204 | |
208 | |
205 | case 2: |
209 | case 2: |
206 | fprintf (stderr, "-"); |
210 | fprintf (stderr, "-"); |
207 | break; |
211 | break; |
208 | |
212 | |
209 | case 3: |
213 | case 3: |
210 | switch (b) |
214 | switch (b) |
211 | { |
215 | { |
212 | case 0: |
216 | case 0: |
213 | fprintf (stderr, " p\n"); |
217 | fprintf (stderr, " p\n"); |
214 | break; |
218 | break; |
215 | |
219 | |
216 | case 1: |
220 | case 1: |
217 | fprintf (stderr, " q\n"); |
221 | fprintf (stderr, " q\n"); |
218 | break; |
222 | break; |
219 | |
223 | |
220 | default: |
224 | default: |
221 | fprintf (stderr, "?"); |
225 | fprintf (stderr, "?"); |
222 | } |
226 | } |
223 | break; |
227 | break; |
224 | |
228 | |
225 | default: |
229 | default: |
226 | fprintf (stderr, "?"); |
230 | fprintf (stderr, "?"); |
227 | } |
231 | } |
|
|
232 | |
|
|
233 | return 1; |
228 | } |
234 | } |
229 | |
235 | |
230 | /* |
236 | /* |
231 | * generate public/private RSA keypairs for all hosts that don't have one. |
237 | * generate public/private RSA keypairs for all hosts that don't have one. |
232 | */ |
238 | */ |
233 | int |
239 | static int |
234 | keygen (int bits) |
240 | keygen (int bits) |
235 | { |
241 | { |
236 | RSA *rsa_key; |
242 | FILE *f, *pubf; |
237 | FILE *f; |
|
|
238 | char *name = NULL; |
|
|
239 | char *fname; |
243 | char *fname; |
240 | |
|
|
241 | asprintf (&fname, "%s/hostkeys", confbase); |
|
|
242 | mkdir (fname, 0700); |
|
|
243 | free (fname); |
|
|
244 | |
244 | |
245 | asprintf (&fname, "%s/pubkey", confbase); |
245 | asprintf (&fname, "%s/pubkey", confbase); |
246 | mkdir (fname, 0700); |
246 | mkdir (fname, 0700); |
247 | free (fname); |
247 | free (fname); |
248 | |
248 | |
249 | for (configuration::node_vector::iterator i = conf.nodes.begin (); i != conf.nodes.end (); ++i) |
249 | for (configuration::node_vector::iterator i = conf.nodes.begin (); i != conf.nodes.end (); ++i) |
250 | { |
250 | { |
251 | conf_node *node = *i; |
251 | conf_node *node = *i; |
252 | |
252 | |
253 | asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename); |
253 | ::thisnode = node->nodename; |
254 | |
254 | |
|
|
255 | fname = conf.config_filename (conf.prikeyfile, "hostkey"); |
|
|
256 | |
255 | f = fopen (fname, "a"); |
257 | f = fopen (fname, "ab"); |
256 | |
258 | |
257 | if (!f) |
259 | /* some libcs are buggy and require an extra seek to the end */ |
|
|
260 | if (!f || fseek (f, 0, SEEK_END)) |
258 | { |
261 | { |
259 | perror (fname); |
262 | perror (fname); |
260 | exit (EXIT_FAILURE); |
263 | exit (EXIT_FAILURE); |
261 | } |
264 | } |
262 | |
265 | |
263 | if (ftell (f)) |
266 | if (ftell (f)) |
264 | { |
267 | { |
265 | if (!quiet) |
268 | if (!quiet) |
266 | fprintf (stderr, "'%s' already exists, skipping this node %d\n", |
269 | fprintf (stderr, "'%s' already exists, skipping node %s\n", fname, node->nodename); |
267 | fname, quiet); |
|
|
268 | |
270 | |
|
|
271 | free (fname); |
269 | fclose (f); |
272 | fclose (f); |
270 | continue; |
273 | continue; |
271 | } |
274 | } |
272 | |
275 | |
273 | fprintf (stderr, _("generating %d bits key for %s:\n"), bits, |
|
|
274 | node->nodename); |
|
|
275 | |
|
|
276 | rsa_key = RSA_generate_key (bits, 0xFFFF, indicator, NULL); |
|
|
277 | |
|
|
278 | if (!rsa_key) |
|
|
279 | { |
|
|
280 | fprintf (stderr, _("error during key generation!\n")); |
|
|
281 | return -1; |
|
|
282 | } |
|
|
283 | else |
|
|
284 | fprintf (stderr, _("Done.\n")); |
|
|
285 | |
|
|
286 | require (PEM_write_RSAPublicKey (f, rsa_key)); |
|
|
287 | fclose (f); |
|
|
288 | free (fname); |
276 | free (fname); |
289 | |
277 | |
290 | asprintf (&fname, "%s/hostkeys/%s", confbase, node->nodename); |
278 | fprintf (stderr, _("generating %d bits key for %s:\n"), bits, node->nodename); |
291 | |
279 | |
|
|
280 | RSA *rsa = RSA_new (); |
|
|
281 | BIGNUM *e = BN_new (); |
|
|
282 | BN_set_bit (e, 0); BN_set_bit (e, 16); // 0x10001, 65537 |
|
|
283 | BN_GENCB cb; |
|
|
284 | BN_GENCB_set (&cb, indicator, 0); |
|
|
285 | |
|
|
286 | require (RSA_generate_key_ex (rsa, bits, e, &cb)); |
|
|
287 | |
|
|
288 | fprintf (stderr, _("Done.\n")); |
|
|
289 | |
|
|
290 | fname = conf.config_filename ("pubkey/%s", 0); |
292 | f = fopen (fname, "a"); |
291 | pubf = fopen (fname, "wb"); |
293 | if (!f) |
292 | if (!pubf) |
294 | { |
293 | { |
295 | perror (fname); |
294 | perror (fname); |
296 | exit (EXIT_FAILURE); |
295 | exit (EXIT_FAILURE); |
297 | } |
296 | } |
298 | |
297 | |
|
|
298 | free (fname); |
|
|
299 | |
|
|
300 | require (PEM_write_RSAPublicKey (pubf, rsa)); |
|
|
301 | fclose (pubf); |
|
|
302 | |
299 | require (PEM_write_RSAPrivateKey (f, rsa_key, NULL, NULL, 0, NULL, NULL)); |
303 | require (PEM_write_RSAPrivateKey (f, rsa, NULL, NULL, 0, NULL, NULL)); |
300 | fclose (f); |
304 | fclose (f); |
|
|
305 | |
301 | free (fname); |
306 | BN_free (e); |
|
|
307 | RSA_free (rsa); |
302 | } |
308 | } |
303 | |
309 | |
304 | return 0; |
310 | return 0; |
305 | } |
311 | } |
306 | |
312 | |
… | |
… | |
320 | { |
326 | { |
321 | printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), |
327 | printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), |
322 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
328 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
323 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
329 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
324 | printf (_ |
330 | printf (_ |
325 | ("Copyright (C) 2003 Marc Lehmann <gvpe@schmorp.de> and others.\n" |
331 | ("Copyright (C) 2003-2013 Marc Lehmann <gvpe@schmorp.de> and others.\n" |
326 | "See the AUTHORS file for a complete list.\n\n" |
332 | "See the AUTHORS file for a complete list.\n\n" |
327 | "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
333 | "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
328 | "and you are welcome to redistribute it under certain conditions;\n" |
334 | "and you are welcome to redistribute it under certain conditions;\n" |
329 | "see the file COPYING for details.\n")); |
335 | "see the file COPYING for details.\n")); |
330 | |
336 | |
… | |
… | |
336 | |
342 | |
337 | { |
343 | { |
338 | configuration_parser (conf, false, 0, 0); |
344 | configuration_parser (conf, false, 0, 0); |
339 | } |
345 | } |
340 | |
346 | |
|
|
347 | if (debug_info) |
|
|
348 | { |
|
|
349 | printf ("cipher_nid=%d\n", EVP_CIPHER_nid (CIPHER ())); |
|
|
350 | printf ("mac_nid=%d\n", EVP_MD_type (MAC_DIGEST ())); |
|
|
351 | printf ("auth_nid=%d\n", EVP_MD_type (AUTH_DIGEST ())); |
|
|
352 | printf ("sizeof_auth_data=%d\n", sizeof (auth_data)); |
|
|
353 | printf ("sizeof_rsa_data=%d\n", sizeof (rsa_data)); |
|
|
354 | printf ("sizeof_rsa_data_extra_auth=%d\n", sizeof (((rsa_data *)0)->extra_auth)); |
|
|
355 | printf ("raw_overhead=%d\n", VPE_OVERHEAD); |
|
|
356 | printf ("vpn_overhead=%d\n", VPE_OVERHEAD + 6 + 6); |
|
|
357 | printf ("udp_overhead=%d\n", UDP_OVERHEAD + VPE_OVERHEAD + 6 + 6); |
|
|
358 | exit (EXIT_SUCCESS); |
|
|
359 | } |
|
|
360 | |
341 | if (generate_keys) |
361 | if (generate_keys) |
342 | { |
362 | { |
343 | RAND_load_file ("/dev/urandom", 1024); |
363 | RAND_load_file (conf.seed_dev, SEED_SIZE); |
344 | exit (keygen (generate_keys)); |
364 | exit (keygen (generate_keys)); |
345 | } |
365 | } |
346 | |
366 | |
347 | if (kill_gvpe) |
367 | if (kill_gvpe) |
348 | exit (kill_other (kill_gvpe)); |
368 | exit (kill_other (kill_gvpe)); |
… | |
… | |
353 | exit (EXIT_SUCCESS); |
373 | exit (EXIT_SUCCESS); |
354 | } |
374 | } |
355 | |
375 | |
356 | usage (1); |
376 | usage (1); |
357 | } |
377 | } |
|
|
378 | |