Replace asprintf()s not covered by the merge to xasprintf().
[tinc] / src / tincctl.c
index aed1c31..0cc7d06 100644 (file)
 #include "system.h"
 
 #include <sys/un.h>
-#include <openssl/rand.h>
-#include <openssl/rsa.h>
-#include <openssl/pem.h>
-#include <openssl/evp.h>
-#include <openssl/engine.h>
-
 #include <getopt.h>
 
 #include "xalloc.h"
 #include "protocol.h"
 #include "control_common.h"
+#include "rsagen.h"
 
 /* The name this program was run with. */
 char *program_name = NULL;
@@ -192,62 +187,25 @@ FILE *ask_and_open(const char *filename, const char *what, const char *mode) {
        return r;
 }
 
-/* This function prettyprints the key generation process */
-
-static void indicator(int a, int b, void *p) {
-       switch (a) {
-               case 0:
-                       fprintf(stderr, ".");
-                       break;
-
-               case 1:
-                       fprintf(stderr, "+");
-                       break;
-
-               case 2:
-                       fprintf(stderr, "-");
-                       break;
-
-               case 3:
-                       switch (b) {
-                               case 0:
-                                       fprintf(stderr, " p\n");
-                                       break;
-
-                               case 1:
-                                       fprintf(stderr, " q\n");
-                                       break;
-
-                               default:
-                                       fprintf(stderr, "?");
-                       }
-                       break;
-
-               default:
-                       fprintf(stderr, "?");
-       }
-}
-
 /*
   Generate a public/private RSA keypair, and ask for a file to store
   them in.
 */
 static bool keygen(int bits) {
-       RSA *rsa_key;
+       rsa_t key;
        FILE *f;
        char *name = NULL;
        char *filename;
 
        fprintf(stderr, _("Generating %d bits keys:\n"), bits);
-       rsa_key = RSA_generate_key(bits, 0x10001, indicator, NULL);
 
-       if(!rsa_key) {
+       if(!rsa_generate(&key, bits, 0x10001)) {
                fprintf(stderr, _("Error during key generation!\n"));
                return false;
        } else
                fprintf(stderr, _("Done.\n"));
 
-       asprintf(&filename, "%s/rsa_key.priv", confbase);
+       xasprintf(&filename, "%s/rsa_key.priv", confbase);
        f = ask_and_open(filename, _("private RSA key"), "a");
 
        if(!f)
@@ -261,14 +219,15 @@ static bool keygen(int bits) {
        if(ftell(f))
                fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
 
-       PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
+       rsa_write_pem_private_key(&key, f);
+
        fclose(f);
        free(filename);
 
        if(name)
-               asprintf(&filename, "%s/hosts/%s", confbase, name);
+               xasprintf(&filename, "%s/hosts/%s", confbase, name);
        else
-               asprintf(&filename, "%s/rsa_key.pub", confbase);
+               xasprintf(&filename, "%s/rsa_key.pub", confbase);
 
        f = ask_and_open(filename, _("public RSA key"), "a");
 
@@ -278,7 +237,8 @@ static bool keygen(int bits) {
        if(ftell(f))
                fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
 
-       PEM_write_RSAPublicKey(f, rsa_key);
+       rsa_write_pem_public_key(&key, f);
+
        fclose(f);
        free(filename);
 
@@ -292,11 +252,11 @@ static void make_names(void) {
 #ifdef HAVE_MINGW
        HKEY key;
        char installdir[1024] = "";
-       long len = sizeof(installdir);
+       long len = sizeof installdir;
 #endif
 
        if(netname)
-               asprintf(&identname, "tinc.%s", netname);
+               xasprintf(&identname, "tinc.%s", netname);
        else
                identname = xstrdup("tinc");
 
@@ -304,12 +264,12 @@ static void make_names(void) {
        if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
                if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
                        if(!logfilename)
-                               asprintf(&logfilename, "%s/log/%s.log", identname);
+                               xasprintf(&logfilename, "%s/log/%s.log", identname);
                        if(!confbase) {
                                if(netname)
-                                       asprintf(&confbase, "%s/%s", installdir, netname);
+                                       xasprintf(&confbase, "%s/%s", installdir, netname);
                                else
-                                       asprintf(&confbase, "%s", installdir);
+                                       xasprintf(&confbase, "%s", installdir);
                        }
                }
                RegCloseKey(key);
@@ -319,16 +279,16 @@ static void make_names(void) {
 #endif
 
        if(!controlsocketname)
-               asprintf(&controlsocketname, "%s/run/%s.control/socket", LOCALSTATEDIR, identname);
+               xasprintf(&controlsocketname, "%s/run/%s.control/socket", LOCALSTATEDIR, identname);
 
        if(netname) {
                if(!confbase)
-                       asprintf(&confbase, CONFDIR "/tinc/%s", netname);
+                       xasprintf(&confbase, CONFDIR "/tinc/%s", netname);
                else
                        fprintf(stderr, _("Both netname and configuration directory given, using the latter...\n"));
        } else {
                if(!confbase)
-                       asprintf(&confbase, CONFDIR "/tinc");
+                       xasprintf(&confbase, CONFDIR "/tinc");
        }
 }
 
@@ -360,7 +320,7 @@ static int send_ctl_request(int fd, enum request_type type,
        tinc_ctl_request_t req;
        int rv;
        struct iovec vector[2] = {
-               {&req, sizeof(req)},
+               {&req, sizeof req},
                {(void*) outdata, outdatalen}
        };
        void *indata;
@@ -425,7 +385,7 @@ static int send_ctl_request_cooked(int fd, enum request_type type,
        }
 
        if(buf != NULL) {
-               printf("%*s", buflen, buf);
+               printf("%*s", (int)buflen, buf);
                free(buf);
        }
 
@@ -440,9 +400,7 @@ static int send_ctl_request_cooked(int fd, enum request_type type,
 int main(int argc, char *argv[], char *envp[]) {
        struct sockaddr_un addr;
        tinc_ctl_greeting_t greeting;
-       tinc_ctl_request_t req;
        int fd;
-       int len;
        int result;
 
        program_name = argv[0];
@@ -482,12 +440,12 @@ int main(int argc, char *argv[], char *envp[]) {
        // First handle commands that don't involve connecting to a running tinc daemon.
 
        if(!strcasecmp(argv[optind], "generate-keys")) {
-               return !keygen(optind > argc ? atoi(argv[optind + 1]) : 1024);
+               return !keygen(optind > argc ? atoi(argv[optind + 1]) : 2048);
        }
 
        if(!strcasecmp(argv[optind], "start")) {
                argv[optind] = NULL;
-               execve("tincd", argv, envp);
+               execve(SBINDIR "/tincd", argv, envp);
                fprintf(stderr, _("Could not start tincd: %s"), strerror(errno));
                return 1;
        }
@@ -613,7 +571,7 @@ int main(int argc, char *argv[], char *envp[]) {
                }
                debuglevel = atoi(argv[optind+1]);
                return send_ctl_request_cooked(fd, REQ_SET_DEBUG, &debuglevel,
-                                                                          sizeof(debuglevel)) != -1;
+                                                                          sizeof debuglevel) != -1;
        }
 
        if(!strcasecmp(argv[optind], "retry")) {