Implemented a readline() function that will read an entire line into a
[oweals/tinc.git] / src / tincd.c
1 /*
2     tincd.c -- the main file for tincd
3     Copyright (C) 1998,1999,2000 Ivo Timmermans <itimmermans@bigfoot.com>
4                             2000 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: tincd.c,v 1.10.4.37 2000/11/29 14:24:40 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 #ifdef HAVE_OPENSSL_RAND_H
42 # include <openssl/rand.h>
43 #else
44 # include <rand.h>
45 #endif
46
47 #ifdef HAVE_OPENSSL_RSA_H
48 # include <openssl/rsa.h>
49 #else
50 # include <rsa.h>
51 #endif
52
53 #ifdef HAVE_OPENSSL_ERR_H
54 # include <openssl/err.h>
55 #else
56 # include <err.h>
57 #endif
58
59 #ifdef HAVE_OPENSSL_PEM_H
60 # include <openssl/pem.h>
61 #else
62 # include <pem.h>
63 #endif
64
65
66
67 #include <utils.h>
68 #include <xalloc.h>
69
70 #include "conf.h"
71 #include "net.h"
72 #include "netutl.h"
73 #include "process.h"
74 #include "protocol.h"
75 #include "subnet.h"
76
77 #include "system.h"
78
79 /* The name this program was run with. */
80 char *program_name;
81
82 /* If nonzero, display usage information and exit. */
83 static int show_help;
84
85 /* If nonzero, print the version on standard output and exit.  */
86 static int show_version;
87
88 /* If nonzero, it will attempt to kill a running tincd and exit. */
89 static int kill_tincd = 0;
90
91 /* If zero, don't detach from the terminal. */
92 extern int do_detach;
93
94 /* If nonzero, generate public/private keypair for this host/net. */
95 static int generate_keys = 0;
96
97 char *identname;                 /* program name for syslog */
98 char *pidfilename;               /* pid file location */
99 char **g_argv;                   /* a copy of the cmdline arguments */
100 char **environment;              /* A pointer to the environment on
101                                     startup */
102
103 static struct option const long_options[] =
104 {
105   { "config", required_argument, NULL, 'c' },
106   { "kill", no_argument, NULL, 'k' },
107   { "net", required_argument, NULL, 'n' },
108   { "help", no_argument, &show_help, 1 },
109   { "version", no_argument, &show_version, 1 },
110   { "no-detach", no_argument, &do_detach, 0 },
111   { "generate-keys", optional_argument, NULL, 'K'},
112   { NULL, 0, NULL, 0 }
113 };
114
115 static void
116 usage(int status)
117 {
118   if(status != 0)
119     fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name);
120   else
121     {
122       printf(_("Usage: %s [option]...\n\n"), program_name);
123       printf(_("  -c, --config=DIR           Read configuration options from DIR.\n"
124                "  -D, --no-detach            Don't fork and detach.\n"
125                "  -d                         Increase debug level.\n"
126                "  -k, --kill                 Attempt to kill a running tincd and exit.\n"
127                "  -n, --net=NETNAME          Connect to net NETNAME.\n"));
128       printf(_("  -K, --generate-keys[=BITS] Generate public/private RSA keypair.\n"
129                "      --help                 Display this help and exit.\n"
130                "      --version              Output version information and exit.\n\n"));
131       printf(_("Report bugs to tinc@nl.linux.org.\n"));
132     }
133   exit(status);
134 }
135
136 void
137 parse_options(int argc, char **argv, char **envp)
138 {
139   int r;
140   int option_index = 0;
141   
142   while((r = getopt_long(argc, argv, "c:Ddkn:K::", long_options, &option_index)) != EOF)
143     {
144       switch(r)
145         {
146         case 0: /* long option */
147           break;
148         case 'c': /* config file */
149           confbase = xmalloc(strlen(optarg)+1);
150           strcpy(confbase, optarg);
151           break;
152         case 'D': /* no detach */
153           do_detach = 0;
154           break;
155         case 'd': /* inc debug level */
156           debug_lvl++;
157           break;
158         case 'k': /* kill old tincds */
159           kill_tincd = 1;
160           break;
161         case 'n': /* net name given */
162           netname = xmalloc(strlen(optarg)+1);
163           strcpy(netname, optarg);
164           break;
165         case 'K': /* generate public/private keypair */
166           if(optarg)
167             {
168               generate_keys = atoi(optarg);
169               if(generate_keys < 512)
170                 {
171                   fprintf(stderr, _("Invalid argument! BITS must be a number equal to or greater than 512.\n"));
172                   usage(1);
173                 }
174               generate_keys &= ~7;      /* Round it to bytes */
175             }
176           else
177             generate_keys = 1024;
178           break;
179         case '?':
180           usage(1);
181         default:
182           break;
183         }
184     }
185 }
186
187 /* This function prettyprints the key generation process */
188
189 void indicator(int a, int b, void *p)
190 {
191   switch(a)
192   {
193     case 0:
194       fprintf(stderr, ".");
195       break;
196     case 1:
197       fprintf(stderr, "+");
198       break;
199     case 2:
200       fprintf(stderr, "-");
201       break;
202     case 3:
203       switch(b)
204         {
205           case 0:
206             fprintf(stderr, " p\n");      
207             break;
208           case 1:
209             fprintf(stderr, " q\n");
210             break;
211           default:
212             fprintf(stderr, "?");
213          }
214        break;
215     default:
216       fprintf(stderr, "?");
217   }
218 }
219
220 /*
221   Generate a public/private RSA keypair, and ask for a file to store
222   them in.
223 */
224 int keygen(int bits)
225 {
226   RSA *rsa_key;
227   FILE *f;
228
229   fprintf(stderr, _("Generating %d bits keys:\n"), bits);
230   rsa_key = RSA_generate_key(bits, 0xFFFF, indicator, NULL);
231   if(!rsa_key)
232     {
233       fprintf(stderr, _("Error during key generation!"));
234       return -1;
235      }
236   else
237     fprintf(stderr, _("Done.\n"));
238
239   if((f = ask_and_safe_open("rsa_key.pub", _("public RSA key"))) == NULL)
240     return -1;
241   PEM_write_RSAPublicKey(f, rsa_key);
242   fclose(f);
243   
244   if((f = ask_and_safe_open("rsa_key.priv", _("private RSA key"))) == NULL)
245     return -1;
246   PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
247   fclose(f);
248
249   return 0;
250 }
251
252 /*
253   Set all files and paths according to netname
254 */
255 void make_names(void)
256 {
257   if(netname)
258     {
259       if(!pidfilename)
260         asprintf(&pidfilename, LOCALSTATEDIR "/run/tinc.%s.pid", netname);
261       if(!confbase)
262         asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname);
263       else
264         fprintf(stderr, _("Both netname and configuration directory given, using the latter...\n"));
265       if(!identname)
266         asprintf(&identname, "tinc.%s", netname);
267     }
268   else
269     {
270       if(!pidfilename)
271         pidfilename = LOCALSTATEDIR "/run/tinc.pid";
272       if(!confbase)
273         asprintf(&confbase, "%s/tinc", CONFDIR);
274       if(!identname)
275         identname = "tinc";
276     }
277 }
278
279 int
280 main(int argc, char **argv, char **envp)
281 {
282   program_name = argv[0];
283
284   setlocale (LC_ALL, "");
285   bindtextdomain (PACKAGE, LOCALEDIR);
286   textdomain (PACKAGE);
287
288   /* Do some intl stuff right now */
289   
290   unknown = _("unknown");
291
292   environment = envp;
293   parse_options(argc, argv, envp);
294
295   if(show_version)
296     {
297       printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
298       printf(_("Copyright (C) 1998,1999,2000 Ivo Timmermans, Guus Sliepen and others.\n"
299                "See the AUTHORS file for a complete list.\n\n"
300                "tinc comes with ABSOLUTELY NO WARRANTY.  This is free software,\n"
301                "and you are welcome to redistribute it under certain conditions;\n"
302                "see the file COPYING for details.\n"));
303
304       return 0;
305     }
306
307   if(show_help)
308     usage(0);
309
310   if(geteuid())
311     {
312       fprintf(stderr, _("You must be root to run this program. Sorry.\n"));
313       return 1;
314     }
315
316   g_argv = argv;
317
318   make_names();
319
320   /* Slllluuuuuuurrrrp! */
321
322   RAND_load_file("/dev/urandom", 1024);
323
324   if(generate_keys)
325     exit(keygen(generate_keys));
326
327   if(kill_tincd)
328     exit(kill_other());
329
330   if(read_server_config())
331     return 1;
332
333   if(detach())
334     exit(0);
335
336   if(debug_lvl >= DEBUG_ERROR)
337     ERR_load_crypto_strings();
338     
339   for(;;)
340     {
341       if(!setup_network_connections())
342         {
343           main_loop();
344           cleanup_and_exit(1);
345         }
346       
347       syslog(LOG_ERR, _("Unrecoverable error"));
348       cp_trace();
349
350       if(do_detach)
351         {
352           syslog(LOG_NOTICE, _("Restarting in %d seconds!"), MAXTIMEOUT);
353           sleep(MAXTIMEOUT);
354         }
355       else
356         {
357           syslog(LOG_ERR, _("Not restarting."));
358           exit(0);
359         }
360     }
361 }
362