Main pokey interface files.
[tinc] / src / pokey / pokey.c
1 /*
2     tincd.c -- the main file for tincd
3     Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>
4                   2000-2002 Guus Sliepen <guus@sliepen.warande.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: pokey.c,v 1.1 2002/04/11 14:23:56 zarq Exp $
21 */
22
23 #include "config.h"
24
25 #include <errno.h>
26 #include <fcntl.h> 
27 #include <getopt.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <syslog.h>
32 #include <unistd.h>
33 #include <signal.h>
34 #include <string.h>
35 #include <termios.h>
36
37 #ifdef HAVE_SYS_IOCTL_H
38 # include <sys/ioctl.h>
39 #endif
40
41 #include <openssl/rand.h>
42 #include <openssl/rsa.h>
43 #include <openssl/pem.h>
44 #include <openssl/evp.h>
45
46 #include <gtk/gtk.h>
47 #include <glade/glade.h>
48
49 #include <utils.h>
50 #include <xalloc.h>
51
52 #include "conf.h"
53 #include "interface.h"
54 #include "net.h"
55 #include "netutl.h"
56 #include "process.h"
57 #include "protocol.h"
58 #include "subnet.h"
59
60 #include "system.h"
61
62 /* The name this program was run with. */
63 char *program_name;
64
65 /* If nonzero, display usage information and exit. */
66 int show_help;
67
68 /* If nonzero, print the version on standard output and exit.  */
69 int show_version;
70
71 /* If nonzero, it will attempt to kill a running tincd and exit. */
72 int kill_tincd = 0;
73
74 /* If nonzero, generate public/private keypair for this host/net. */
75 int generate_keys = 0;
76
77 /* If nonzero, use null ciphers and skip all key exchanges. */
78 int bypass_security = 0;
79
80 char *identname;                 /* program name for syslog */
81 char *pidfilename;               /* pid file location */
82 char **g_argv;                   /* a copy of the cmdline arguments */
83 char **environment;              /* A pointer to the environment on startup */
84
85 /* GTK Interface */
86 GladeXML *xml = NULL;
87
88
89 static struct option const long_options[] =
90 {
91   { "config", required_argument, NULL, 'c' },
92   { "kill", optional_argument, NULL, 'k' },
93   { "net", required_argument, NULL, 'n' },
94   { "help", no_argument, &show_help, 1 },
95   { "version", no_argument, &show_version, 1 },
96   { "no-detach", no_argument, &do_detach, 0 },
97   { "generate-keys", optional_argument, NULL, 'K'},
98   { "debug", optional_argument, NULL, 'd'},
99   { "bypass-security", no_argument, &bypass_security, 1 },
100   { NULL, 0, NULL, 0 }
101 };
102
103 static void
104 usage(int status)
105 {
106   if(status != 0)
107     fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name);
108   else
109     {
110       printf(_("Usage: %s [option]...\n\n"), program_name);
111       printf(_("  -c, --config=DIR           Read configuration options from DIR.\n"
112                "  -D, --no-detach            Don't fork and detach.\n"
113                "  -d, --debug[=LEVEL]        Increase debug level or set it to LEVEL.\n"
114                "  -k, --kill[=SIGNAL]        Attempt to kill a running tincd and exit.\n"
115                "  -n, --net=NETNAME          Connect to net NETNAME.\n"));
116       printf(_("  -K, --generate-keys[=BITS] Generate public/private RSA keypair.\n"
117                "      --help                 Display this help and exit.\n"
118                "      --version              Output version information and exit.\n\n"));
119       printf(_("Report bugs to tinc@nl.linux.org.\n"));
120     }
121   exit(status);
122 }
123
124 void
125 parse_options(int argc, char **argv, char **envp)
126 {
127   int r;
128   int option_index = 0;
129   
130   while((r = getopt_long(argc, argv, "c:Dd::k::n:K::", long_options, &option_index)) != EOF)
131     {
132       switch(r)
133         {
134         case 0: /* long option */
135           break;
136         case 'c': /* config file */
137           confbase = xmalloc(strlen(optarg)+1);
138           strcpy(confbase, optarg);
139           break;
140         case 'D': /* no detach */
141           do_detach = 0;
142           break;
143         case 'd': /* inc debug level */
144           if(optarg)
145             debug_lvl = atoi(optarg);
146           else
147             debug_lvl++;
148           break;
149         case 'k': /* kill old tincds */
150           kill_tincd = optarg?atoi(optarg):SIGTERM;
151           break;
152         case 'n': /* net name given */
153           netname = xmalloc(strlen(optarg)+1);
154           strcpy(netname, optarg);
155           break;
156         case 'K': /* generate public/private keypair */
157           if(optarg)
158             {
159               generate_keys = atoi(optarg);
160               if(generate_keys < 512)
161                 {
162                   fprintf(stderr, _("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"),
163                           optarg);
164                   usage(1);
165                 }
166               generate_keys &= ~7;      /* Round it to bytes */
167             }
168           else
169             generate_keys = 1024;
170           break;
171         case '?':
172           usage(1);
173         default:
174           break;
175         }
176     }
177 }
178
179 /* This function prettyprints the key generation process */
180
181 void indicator(int a, int b, void *p)
182 {
183   switch(a)
184   {
185     case 0:
186       fprintf(stderr, ".");
187       break;
188     case 1:
189       fprintf(stderr, "+");
190       break;
191     case 2:
192       fprintf(stderr, "-");
193       break;
194     case 3:
195       switch(b)
196         {
197           case 0:
198             fprintf(stderr, " p\n");      
199             break;
200           case 1:
201             fprintf(stderr, " q\n");
202             break;
203           default:
204             fprintf(stderr, "?");
205          }
206        break;
207     default:
208       fprintf(stderr, "?");
209   }
210 }
211
212 /*
213   Generate a public/private RSA keypair, and ask for a file to store
214   them in.
215 */
216 int keygen(int bits)
217 {
218   RSA *rsa_key;
219   FILE *f;
220   char *name = NULL;
221   char *filename;
222
223   fprintf(stderr, _("Generating %d bits keys:\n"), bits);
224   rsa_key = RSA_generate_key(bits, 0xFFFF, indicator, NULL);
225
226   if(!rsa_key)
227     {
228       fprintf(stderr, _("Error during key generation!\n"));
229       return -1;
230     }
231   else
232     fprintf(stderr, _("Done.\n"));
233
234   get_config_string(lookup_config(config_tree, "Name"), &name);
235
236   if(name)
237     asprintf(&filename, "%s/hosts/%s", confbase, name);
238   else
239     asprintf(&filename, "%s/rsa_key.pub", confbase);
240
241   if((f = ask_and_safe_open(filename, _("public RSA key"), "a")) == NULL)
242     return -1;
243
244   if(ftell(f))
245     fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
246
247   PEM_write_RSAPublicKey(f, rsa_key);
248   fclose(f);
249   free(filename);
250   
251   asprintf(&filename, "%s/rsa_key.priv", confbase);
252   if((f = ask_and_safe_open(filename, _("private RSA key"), "a")) == NULL)
253     return -1;
254
255   if(ftell(f))
256     fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
257
258   PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
259   fclose(f);
260   free(filename);
261
262   return 0;
263 }
264
265 /*
266   Set all files and paths according to netname
267 */
268 void make_names(void)
269 {
270   if(netname)
271     {
272       if(!pidfilename)
273         asprintf(&pidfilename, LOCALSTATEDIR "/run/tinc.%s.pid", netname);
274       if(!confbase)
275         asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname);
276       else
277         log_message(LOG_INFO, _("Both netname and configuration directory given, using the latter..."));
278       if(!identname)
279         asprintf(&identname, "tinc.%s", netname);
280     }
281   else
282     {
283       if(!pidfilename)
284         pidfilename = LOCALSTATEDIR "/run/tinc.pid";
285       if(!confbase)
286         asprintf(&confbase, "%s/tinc", CONFDIR);
287       if(!identname)
288         identname = "tinc";
289     }
290 }
291
292 int
293 main(int argc, char **argv, char **envp)
294 {
295   char *fake_argv[] = { argv[0], NULL };
296   program_name = argv[0];
297
298   setlocale (LC_ALL, "");
299   bindtextdomain (PACKAGE, LOCALEDIR);
300   textdomain (PACKAGE);
301
302   environment = envp;
303   parse_options(argc, argv, envp);
304
305   if(show_version)
306     {
307       printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
308       printf(_("Copyright (C) 1998-2002 Ivo Timmermans, Guus Sliepen and others.\n"
309                "See the AUTHORS file for a complete list.\n\n"
310                "tinc comes with ABSOLUTELY NO WARRANTY.  This is free software,\n"
311                "and you are welcome to redistribute it under certain conditions;\n"
312                "see the file COPYING for details.\n"));
313
314       return 0;
315     }
316
317   if(show_help)
318     usage(0);
319
320   gnome_init("Pokey", "0.0", 1, fake_argv);
321
322   glade_init();
323
324   xml = glade_xml_new("pokey.glade", NULL);
325
326   if (!xml) {
327     g_warning("something bad happened while creating the interface");
328     return 1;
329   }
330
331 /*   if(geteuid()) */
332 /*     { */
333 /*       fprintf(stderr, _("You must be root to run this program.\n")); */
334 /*       return 1; */
335 /*     } */
336
337   g_argv = argv;
338
339   make_names();
340   init_configuration(&config_tree);
341
342   /* Slllluuuuuuurrrrp! */
343 cp
344   RAND_load_file("/dev/urandom", 1024);
345
346 #ifdef HAVE_SSLEAY_ADD_ALL_ALGORITHMS
347   SSLeay_add_all_algorithms();
348 #else
349   OpenSSL_add_all_algorithms();
350 #endif
351
352 cp
353   if(read_server_config())
354     exit(1);
355 cp
356   setup_signals();
357
358   if(init_interface())
359     {
360       fprintf(stderr, _("Could not setup all necessary interface elements.\n"));
361       exit(1);
362     }
363   
364   if(!setup_network_connections())
365     {
366       main_loop();
367       cleanup_and_exit(1);
368     }
369       
370   log_message(LOG_ERR, _("Unrecoverable error"));
371   cp_trace();
372
373   return 1;
374 }