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-2005 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 |
9 | GVPE is free software; you can redistribute it and/or modify it |
10 | it under the terms of the GNU General Public License as published by |
10 | under the terms of the GNU General Public License as published by the |
11 | the Free Software Foundation; either version 2 of the License, or |
11 | Free Software Foundation; either version 3 of the License, or (at your |
12 | (at your option) any later version. |
12 | option) any later version. |
13 | |
13 | |
14 | This program is distributed in the hope that it will be useful, |
14 | This program is distributed in the hope that it will be useful, but |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
17 | GNU General Public License for more details. |
17 | Public License for more details. |
18 | |
18 | |
19 | You should have received a copy of the GNU General Public License |
19 | You should have received a copy of the GNU General Public License along |
20 | along with gvpe; if not, write to the Free Software |
20 | with this program; if not, see <http://www.gnu.org/licenses/>. |
21 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | |
|
|
22 | Additional permission under GNU GPL version 3 section 7 |
|
|
23 | |
|
|
24 | If you modify this Program, or any covered work, by linking or |
|
|
25 | combining it with the OpenSSL project's OpenSSL library (or a modified |
|
|
26 | version of that library), containing parts covered by the terms of the |
|
|
27 | OpenSSL or SSLeay licenses, the licensors of this Program grant you |
|
|
28 | additional permission to convey the resulting work. Corresponding |
|
|
29 | Source for a non-source form of such a combination shall include the |
|
|
30 | source code for the parts of OpenSSL used as well as that of the |
|
|
31 | covered work. |
22 | */ |
32 | */ |
23 | |
33 | |
24 | #include "config.h" |
34 | #include "config.h" |
25 | |
35 | |
26 | #include <cstdio> |
36 | #include <cstdio> |
… | |
… | |
34 | #include <sys/stat.h> |
44 | #include <sys/stat.h> |
35 | #include <sys/types.h> |
45 | #include <sys/types.h> |
36 | #include <unistd.h> |
46 | #include <unistd.h> |
37 | #include <signal.h> |
47 | #include <signal.h> |
38 | |
48 | |
|
|
49 | #include <openssl/bn.h> |
39 | #include <openssl/rand.h> |
50 | #include <openssl/rand.h> |
40 | #include <openssl/rsa.h> |
51 | #include <openssl/rsa.h> |
41 | #include <openssl/pem.h> |
52 | #include <openssl/pem.h> |
42 | #include <openssl/evp.h> |
53 | #include <openssl/evp.h> |
43 | |
54 | |
… | |
… | |
58 | static int kill_gvpe; |
69 | static int kill_gvpe; |
59 | |
70 | |
60 | /* 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. */ |
61 | static int show_config; |
72 | static int show_config; |
62 | |
73 | |
|
|
74 | /* If nonzero, do not output anything but warnings/errors/very unusual conditions */ |
|
|
75 | static int quiet; |
|
|
76 | |
63 | /* If nonzero, generate public/private keypair for this net. */ |
77 | /* If nonzero, generate public/private keypair for this net. */ |
64 | static int generate_keys; |
78 | static int generate_keys; |
65 | |
79 | |
66 | static struct option const long_options[] = |
80 | static struct option const long_options[] = |
67 | { |
81 | { |
68 | {"config", required_argument, NULL, 'c'}, |
82 | {"config", required_argument, NULL, 'c'}, |
69 | {"kill", optional_argument, NULL, 'k'}, |
83 | {"kill", optional_argument, NULL, 'k'}, |
70 | {"help", no_argument, &show_help, 1}, |
84 | {"help", no_argument, &show_help, 1}, |
71 | {"version", no_argument, &show_version, 1}, |
85 | {"version", no_argument, &show_version, 1}, |
72 | {"generate-keys", no_argument, NULL, 'g'}, |
86 | {"generate-keys", no_argument, NULL, 'g'}, |
|
|
87 | {"quiet", no_argument, &quiet, 1}, |
73 | {"show-config", no_argument, &show_config, 's'}, |
88 | {"show-config", no_argument, &show_config, 's'}, |
74 | {NULL, 0, NULL, 0} |
89 | {NULL, 0, NULL, 0} |
75 | }; |
90 | }; |
76 | |
91 | |
77 | static void |
92 | static void |
78 | usage (int status) |
93 | usage (int status) |
79 | { |
94 | { |
80 | if (status != 0) |
95 | if (status != 0) |
… | |
… | |
85 | printf (_ |
100 | printf (_ |
86 | (" -c, --config=DIR Read configuration options from DIR.\n" |
101 | (" -c, --config=DIR Read configuration options from DIR.\n" |
87 | " -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" |
88 | " -g, --generate-keys Generate public/private RSA keypair.\n" |
103 | " -g, --generate-keys Generate public/private RSA keypair.\n" |
89 | " -s, --show-config Display the configuration information.\n" |
104 | " -s, --show-config Display the configuration information.\n" |
|
|
105 | " -q, --quiet Be quite quiet.\n" |
90 | " --help Display this help and exit.\n" |
106 | " --help Display this help and exit.\n" |
91 | " --version Output version information and exit.\n\n")); |
107 | " --version Output version information and exit.\n\n")); |
92 | printf (_("Report bugs to <vpe@plan9.de>.\n")); |
108 | printf (_("Report bugs to <gvpe@schmorp.de>.\n")); |
93 | } |
109 | } |
94 | |
110 | |
95 | exit (status); |
111 | exit (status); |
96 | } |
112 | } |
97 | |
113 | |
98 | void |
114 | static void |
99 | parse_options (int argc, char **argv, char **envp) |
115 | parse_options (int argc, char **argv, char **envp) |
100 | { |
116 | { |
101 | int r; |
117 | int r; |
102 | int option_index = 0; |
118 | int option_index = 0; |
103 | |
119 | |
104 | while ((r = |
120 | while ((r = getopt_long (argc, argv, "c:k::qgs", long_options, &option_index)) != EOF) |
105 | getopt_long (argc, argv, "c:k::gs", long_options, |
|
|
106 | &option_index)) != EOF) |
|
|
107 | { |
121 | { |
108 | switch (r) |
122 | switch (r) |
109 | { |
123 | { |
110 | case 0: /* long option */ |
124 | case 0: /* long option */ |
111 | break; |
125 | break; |
112 | |
126 | |
113 | case 'c': /* config file */ |
127 | case 'c': /* config file */ |
114 | confbase = strdup (optarg); |
128 | confbase = strdup (optarg); |
115 | break; |
129 | break; |
116 | |
130 | |
117 | case 'k': /* kill old gvpes */ |
131 | case 'k': /* kill old gvpes */ |
118 | if (optarg) |
132 | if (optarg) |
119 | { |
133 | { |
120 | if (!strcasecmp (optarg, "HUP")) |
134 | if (!strcasecmp (optarg, "HUP")) |
121 | kill_gvpe = SIGHUP; |
135 | kill_gvpe = SIGHUP; |
122 | else if (!strcasecmp (optarg, "TERM")) |
136 | else if (!strcasecmp (optarg, "TERM")) |
123 | kill_gvpe = SIGTERM; |
137 | kill_gvpe = SIGTERM; |
124 | else if (!strcasecmp (optarg, "KILL")) |
138 | else if (!strcasecmp (optarg, "KILL")) |
125 | kill_gvpe = SIGKILL; |
139 | kill_gvpe = SIGKILL; |
126 | else if (!strcasecmp (optarg, "USR1")) |
140 | else if (!strcasecmp (optarg, "USR1")) |
127 | kill_gvpe = SIGUSR1; |
141 | kill_gvpe = SIGUSR1; |
128 | else if (!strcasecmp (optarg, "USR2")) |
142 | else if (!strcasecmp (optarg, "USR2")) |
129 | kill_gvpe = SIGUSR2; |
143 | kill_gvpe = SIGUSR2; |
130 | else if (!strcasecmp (optarg, "INT")) |
144 | else if (!strcasecmp (optarg, "INT")) |
131 | kill_gvpe = SIGINT; |
145 | kill_gvpe = SIGINT; |
132 | else if (!strcasecmp (optarg, "ALRM")) |
146 | else if (!strcasecmp (optarg, "ALRM")) |
133 | kill_gvpe = SIGALRM; |
147 | kill_gvpe = SIGALRM; |
134 | else |
148 | else |
135 | { |
149 | { |
136 | kill_gvpe = atoi (optarg); |
150 | kill_gvpe = atoi (optarg); |
137 | |
151 | |
138 | if (!kill_gvpe) |
152 | if (!kill_gvpe) |
139 | { |
153 | { |
140 | fprintf (stderr, |
154 | fprintf (stderr, |
141 | _ |
155 | _ |
142 | ("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"), |
143 | optarg); |
157 | optarg); |
144 | usage (1); |
158 | usage (1); |
145 | } |
159 | } |
146 | } |
160 | } |
147 | } |
161 | } |
148 | else |
162 | else |
149 | kill_gvpe = SIGTERM; |
163 | kill_gvpe = SIGTERM; |
150 | |
164 | |
151 | break; |
165 | break; |
152 | |
166 | |
153 | case 'g': /* generate public/private keypair */ |
167 | case 'g': /* generate public/private keypair */ |
154 | generate_keys = RSA_KEYBITS; |
168 | generate_keys = RSA_KEYBITS; |
155 | break; |
169 | break; |
156 | |
170 | |
157 | case 's': |
171 | case 's': |
158 | show_config = 1; |
172 | show_config = 1; |
159 | break; |
173 | break; |
160 | |
174 | |
|
|
175 | case 'q': |
|
|
176 | quiet = 1; |
|
|
177 | break; |
|
|
178 | |
161 | case '?': |
179 | case '?': |
162 | usage (1); |
180 | usage (1); |
163 | |
181 | |
164 | default: |
182 | default: |
165 | break; |
183 | break; |
166 | } |
184 | } |
167 | } |
185 | } |
168 | } |
186 | } |
169 | |
187 | |
170 | /* This function prettyprints the key generation process */ |
188 | // this function prettyprints the key generation process |
171 | |
189 | static int |
172 | void |
|
|
173 | indicator (int a, int b, void *p) |
190 | indicator (int a, int b, BN_GENCB *cb) |
174 | { |
191 | { |
|
|
192 | if (quiet) |
|
|
193 | return 1; |
|
|
194 | |
175 | switch (a) |
195 | switch (a) |
176 | { |
196 | { |
177 | case 0: |
197 | case 0: |
178 | fprintf (stderr, "."); |
198 | fprintf (stderr, "."); |
179 | break; |
199 | break; |
180 | |
200 | |
181 | case 1: |
201 | case 1: |
182 | fprintf (stderr, "+"); |
202 | fprintf (stderr, "+"); |
183 | break; |
203 | break; |
184 | |
204 | |
185 | case 2: |
205 | case 2: |
186 | fprintf (stderr, "-"); |
206 | fprintf (stderr, "-"); |
187 | break; |
207 | break; |
188 | |
208 | |
189 | case 3: |
209 | case 3: |
190 | switch (b) |
210 | switch (b) |
191 | { |
211 | { |
192 | case 0: |
212 | case 0: |
193 | fprintf (stderr, " p\n"); |
213 | fprintf (stderr, " p\n"); |
194 | break; |
214 | break; |
195 | |
215 | |
196 | case 1: |
216 | case 1: |
197 | fprintf (stderr, " q\n"); |
217 | fprintf (stderr, " q\n"); |
198 | break; |
218 | break; |
199 | |
219 | |
200 | default: |
220 | default: |
201 | fprintf (stderr, "?"); |
221 | fprintf (stderr, "?"); |
202 | } |
222 | } |
203 | break; |
223 | break; |
204 | |
224 | |
205 | default: |
225 | default: |
206 | fprintf (stderr, "?"); |
226 | fprintf (stderr, "?"); |
207 | } |
227 | } |
|
|
228 | |
|
|
229 | return 1; |
208 | } |
230 | } |
209 | |
231 | |
210 | /* |
232 | /* |
211 | * 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. |
212 | */ |
234 | */ |
213 | int |
235 | static int |
214 | keygen (int bits) |
236 | keygen (int bits) |
215 | { |
237 | { |
216 | RSA *rsa_key; |
|
|
217 | FILE *f; |
238 | FILE *f; |
218 | char *name = NULL; |
239 | char *name = NULL; |
219 | char *fname; |
240 | char *fname; |
220 | |
241 | |
221 | asprintf (&fname, "%s/hostkeys", confbase); |
242 | asprintf (&fname, "%s/hostkeys", confbase); |
… | |
… | |
232 | |
253 | |
233 | asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename); |
254 | asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename); |
234 | |
255 | |
235 | f = fopen (fname, "a"); |
256 | f = fopen (fname, "a"); |
236 | |
257 | |
237 | if (!f) |
258 | /* some libcs are buggy and require an extra seek to the end */ |
|
|
259 | if (!f || fseek (f, 0, SEEK_END)) |
238 | { |
260 | { |
239 | perror (fname); |
261 | perror (fname); |
240 | exit (EXIT_FAILURE); |
262 | exit (EXIT_FAILURE); |
241 | } |
263 | } |
242 | |
264 | |
243 | if (ftell (f)) |
265 | if (ftell (f)) |
244 | { |
266 | { |
|
|
267 | if (!quiet) |
245 | fprintf (stderr, "'%s' already exists, skipping this node\n", |
268 | fprintf (stderr, "'%s' already exists, skipping this node %d\n", |
246 | fname); |
269 | fname, quiet); |
|
|
270 | |
247 | fclose (f); |
271 | fclose (f); |
248 | continue; |
272 | continue; |
249 | } |
273 | } |
250 | |
274 | |
251 | fprintf (stderr, _("generating %d bits key for %s:\n"), bits, |
275 | fprintf (stderr, _("generating %d bits key for %s:\n"), bits, |
252 | node->nodename); |
276 | node->nodename); |
253 | |
277 | |
254 | 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); |
255 | |
283 | |
256 | if (!rsa_key) |
284 | require (RSA_generate_key_ex (rsa, bits, e, &cb)); |
257 | { |
285 | |
258 | fprintf (stderr, _("error during key generation!\n")); |
|
|
259 | return -1; |
|
|
260 | } |
|
|
261 | else |
|
|
262 | fprintf (stderr, _("Done.\n")); |
286 | fprintf (stderr, _("Done.\n")); |
263 | |
287 | |
264 | require (PEM_write_RSAPublicKey (f, rsa_key)); |
288 | require (PEM_write_RSAPublicKey (f, rsa)); |
265 | fclose (f); |
289 | fclose (f); |
266 | free (fname); |
290 | free (fname); |
267 | |
291 | |
268 | asprintf (&fname, "%s/hostkeys/%s", confbase, node->nodename); |
292 | asprintf (&fname, "%s/hostkeys/%s", confbase, node->nodename); |
269 | |
293 | |
… | |
… | |
272 | { |
296 | { |
273 | perror (fname); |
297 | perror (fname); |
274 | exit (EXIT_FAILURE); |
298 | exit (EXIT_FAILURE); |
275 | } |
299 | } |
276 | |
300 | |
277 | require (PEM_write_RSAPrivateKey (f, rsa_key, NULL, NULL, 0, NULL, NULL)); |
301 | require (PEM_write_RSAPrivateKey (f, rsa, NULL, NULL, 0, NULL, NULL)); |
278 | fclose (f); |
302 | fclose (f); |
279 | free (fname); |
303 | free (fname); |
|
|
304 | |
|
|
305 | BN_free (e); |
|
|
306 | RSA_free (rsa); |
280 | } |
307 | } |
281 | |
308 | |
282 | return 0; |
309 | return 0; |
283 | } |
310 | } |
284 | |
311 | |
… | |
… | |
294 | |
321 | |
295 | parse_options (argc, argv, envp); |
322 | parse_options (argc, argv, envp); |
296 | |
323 | |
297 | if (show_version) |
324 | if (show_version) |
298 | { |
325 | { |
299 | printf (_("%s version %s (built %s %s, protocol %d.%d)\n"), get_identity (), |
326 | printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), |
300 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
327 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
301 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
328 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
302 | printf (_ |
329 | printf (_ |
303 | ("Copyright (C) 2003 Marc Lehmann <vpe@plan9.de> and others.\n" |
330 | ("Copyright (C) 2003-2013 Marc Lehmann <gvpe@schmorp.de> and others.\n" |
304 | "See the AUTHORS file for a complete list.\n\n" |
331 | "See the AUTHORS file for a complete list.\n\n" |
305 | "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
332 | "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
306 | "and you are welcome to redistribute it under certain conditions;\n" |
333 | "and you are welcome to redistribute it under certain conditions;\n" |
307 | "see the file COPYING for details.\n")); |
334 | "see the file COPYING for details.\n")); |
308 | |
335 | |
… | |
… | |
316 | configuration_parser (conf, false, 0, 0); |
343 | configuration_parser (conf, false, 0, 0); |
317 | } |
344 | } |
318 | |
345 | |
319 | if (generate_keys) |
346 | if (generate_keys) |
320 | { |
347 | { |
321 | RAND_load_file ("/dev/urandom", 1024); |
348 | RAND_load_file (conf.seed_dev, SEED_SIZE); |
322 | exit (keygen (generate_keys)); |
349 | exit (keygen (generate_keys)); |
323 | } |
350 | } |
324 | |
351 | |
325 | if (kill_gvpe) |
352 | if (kill_gvpe) |
326 | exit (kill_other (kill_gvpe)); |
353 | exit (kill_other (kill_gvpe)); |