X-Git-Url: https://tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=src%2Ftincd.c;h=535fdb32a75aa60548458ac82beeca4ad84f434a;hp=9f229b30e41ff2b8e3bd8f23bc8aafc72cad7564;hb=e924096f62655d711cd2d114a8d1ef0fecbb593b;hpb=2764532ea72200d0a27ad2d79e6e299c00c62404 diff --git a/src/tincd.c b/src/tincd.c index 9f229b30..535fdb32 100644 --- a/src/tincd.c +++ b/src/tincd.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: tincd.c,v 1.10.4.29 2000/11/16 17:54:29 zarq Exp $ + $Id: tincd.c,v 1.10.4.39 2001/01/05 23:53:53 guus Exp $ */ #include "config.h" @@ -56,15 +56,21 @@ # include #endif +#ifdef HAVE_OPENSSL_PEM_H +# include +#else +# include +#endif + -#include #include #include #include "conf.h" #include "net.h" #include "netutl.h" +#include "process.h" #include "protocol.h" #include "subnet.h" @@ -90,19 +96,10 @@ static int generate_keys = 0; char *identname; /* program name for syslog */ char *pidfilename; /* pid file location */ -static pid_t ppid; /* pid of non-detached part */ char **g_argv; /* a copy of the cmdline arguments */ char **environment; /* A pointer to the environment on startup */ -void cleanup_and_exit(int); -int detach(void); -int kill_other(void); -void make_names(void); -RETSIGTYPE parent_exit(int a); -void setup_signals(void); -int write_pidfile(void); - static struct option const long_options[] = { { "config", required_argument, NULL, 'c' }, @@ -156,7 +153,10 @@ parse_options(int argc, char **argv, char **envp) do_detach = 0; break; case 'd': /* inc debug level */ - debug_lvl++; + if(optarg) + debug_lvl = atoi(optarg); + else + debug_lvl++; break; case 'k': /* kill old tincds */ kill_tincd = 1; @@ -171,7 +171,8 @@ parse_options(int argc, char **argv, char **envp) generate_keys = atoi(optarg); if(generate_keys < 512) { - fprintf(stderr, _("Invalid argument! BITS must be a number equal to or greater than 512.\n")); + fprintf(stderr, _("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"), + optarg); usage(1); } generate_keys &= ~7; /* Round it to bytes */ @@ -220,11 +221,14 @@ void indicator(int a, int b, void *p) } } -/* Generate a public/private RSA keypair, and possibly store it into the configuration file. */ - +/* + Generate a public/private RSA keypair, and ask for a file to store + them in. +*/ int keygen(int bits) { RSA *rsa_key; + FILE *f; fprintf(stderr, _("Generating %d bits keys:\n"), bits); rsa_key = RSA_generate_key(bits, 0xFFFF, indicator, NULL); @@ -236,82 +240,15 @@ int keygen(int bits) else fprintf(stderr, _("Done.\n")); - fprintf(stderr, _("Please copy the private key to tinc.conf and the\npublic key to your host configuration file:\n\n")); - printf("PublicKey = %s\n", BN_bn2hex(rsa_key->n)); - printf("PrivateKey = %s\n", BN_bn2hex(rsa_key->d)); + if((f = ask_and_safe_open("rsa_key.pub", _("public RSA key"))) == NULL) + return -1; + PEM_write_RSAPublicKey(f, rsa_key); + fclose(f); - fflush(stdin); - return 0; -} - -void memory_full(int size) -{ - syslog(LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exiting."), size); - cp_trace(); - exit(1); -} - -/* - Close network connections, and terminate neatly -*/ -void cleanup_and_exit(int c) -{ - close_network_connections(); - - if(debug_lvl > DEBUG_NOTHING) - syslog(LOG_INFO, _("Total bytes written: tap %d, socket %d; bytes read: tap %d, socket %d"), - total_tap_out, total_socket_out, total_tap_in, total_socket_in); - - closelog(); - kill(ppid, SIGTERM); - exit(c); -} - -/* - check for an existing tinc for this net, and write pid to pidfile -*/ -int write_pidfile(void) -{ - int pid; - - if((pid = check_pid(pidfilename))) - { - if(netname) - fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"), - netname, pid); - else - fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid); - return 1; - } - - /* if it's locked, write-protected, or whatever */ - if(!write_pid(pidfilename)) - return 1; - - return 0; -} - -/* - kill older tincd for this net -*/ -int kill_other(void) -{ - int pid; - - if(!(pid = read_pid(pidfilename))) - { - if(netname) - fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname); - else - fprintf(stderr, _("No other tincd is running.\n")); - return 1; - } - - errno = 0; /* No error, sometimes errno is only changed on error */ - /* ESRCH is returned when no process with that pid is found */ - if(kill(pid, SIGTERM) && errno == ESRCH) - fprintf(stderr, _("Removing stale lock file.\n")); - remove_pid(pidfilename); + if((f = ask_and_safe_open("rsa_key.priv", _("private RSA key"))) == NULL) + return -1; + PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL); + fclose(f); return 0; } @@ -324,7 +261,7 @@ void make_names(void) if(netname) { if(!pidfilename) - asprintf(&pidfilename, "/var/run/tinc.%s.pid", netname); + asprintf(&pidfilename, LOCALSTATEDIR "/run/tinc.%s.pid", netname); if(!confbase) asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname); else @@ -335,7 +272,7 @@ void make_names(void) else { if(!pidfilename) - pidfilename = "/var/run/tinc.pid"; + pidfilename = LOCALSTATEDIR "/run/tinc.pid"; if(!confbase) asprintf(&confbase, "%s/tinc", CONFDIR); if(!identname) @@ -346,6 +283,8 @@ void make_names(void) int main(int argc, char **argv, char **envp) { + openlog("tinc", LOG_PERROR, LOG_DAEMON); /* Catch all syslog() calls issued before detaching */ + program_name = argv[0]; setlocale (LC_ALL, ""); @@ -385,9 +324,9 @@ main(int argc, char **argv, char **envp) make_names(); /* Slllluuuuuuurrrrp! */ - +cp RAND_load_file("/dev/urandom", 1024); - +cp if(generate_keys) exit(keygen(generate_keys)); @@ -396,12 +335,10 @@ main(int argc, char **argv, char **envp) if(read_server_config()) return 1; - - setup_signals(); - +cp if(detach()) exit(0); - +cp if(debug_lvl >= DEBUG_ERROR) ERR_load_crypto_strings();