client: Do not leak buffer tree node on exit.
[paraslash.git] / client_common.c
index ba419786ee036745972825ec49629d9f1cf342ab..eb9f9e1fcda2a49ba960582ba67be9c882897a20 100644 (file)
@@ -8,7 +8,6 @@
 
 #include <regex.h>
 #include <sys/types.h>
-#include <dirent.h>
 
 #include "para.h"
 #include "error.h"
 #include "sched.h"
 #include "client.cmdline.h"
 #include "crypt.h"
-#include "rc4.h"
 #include "net.h"
 #include "fd.h"
 #include "string.h"
 #include "client.cmdline.h"
 #include "client.h"
-#include "hash.h"
 #include "buffer_tree.h"
+#include "version.h"
 
 /** The size of the receiving buffer. */
 #define CLIENT_BUFSIZE 4000
@@ -46,6 +44,7 @@ void client_close(struct client_task *ct)
        free(ct->user);
        free(ct->config_file);
        free(ct->key_file);
+       btr_free_node(ct->btrn);
        client_cmdline_parser_free(&ct->conf);
        free(ct);
 }
@@ -183,7 +182,7 @@ static void client_post_select(struct sched *s, struct task *t)
                /* decrypted challenge/session key buffer */
                unsigned char crypt_buf[1024];
                /* the SHA1 of the decrypted challenge */
-               unsigned char challenge_sha1[HASH_SIZE];
+               unsigned char challenge_hash[HASH_SIZE];
 
                ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
                if (ret < 0 || n == 0)
@@ -193,13 +192,13 @@ static void client_post_select(struct sched *s, struct task *t)
                        (unsigned char *)buf, n);
                if (ret < 0)
                        goto out;
-               sha1_hash((char *)crypt_buf, CHALLENGE_SIZE, challenge_sha1);
-               ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, RC4_KEY_LEN);
-               ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + RC4_KEY_LEN,
-                       RC4_KEY_LEN);
-               hash_to_asc(challenge_sha1, buf);
+               hash_function((char *)crypt_buf, CHALLENGE_SIZE, challenge_hash);
+               ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, SESSION_KEY_LEN);
+               ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + SESSION_KEY_LEN,
+                       SESSION_KEY_LEN);
+               hash_to_asc(challenge_hash, buf);
                PARA_INFO_LOG("--> %s\n", buf);
-               ret = send_bin_buffer(ct->scc.fd, (char *)challenge_sha1,
+               ret = send_bin_buffer(ct->scc.fd, (char *)challenge_hash,
                        HASH_SIZE);
                if (ret < 0)
                        goto out;
@@ -396,9 +395,16 @@ int client_open(int argc, char *argv[], struct client_task **ct_ptr,
        ct->user = ct->conf.user_given?
                para_strdup(ct->conf.user_arg) : para_logname();
 
-       ct->key_file = ct->conf.key_file_given?
-               para_strdup(ct->conf.key_file_arg) :
-               make_message("%s/.paraslash/key.%s", home, ct->user);
+       if (ct->conf.key_file_given)
+               ct->key_file = para_strdup(ct->conf.key_file_arg);
+       else {
+               ct->key_file = make_message("%s/.paraslash/key.%s",
+                       home, ct->user);
+               if (!file_exists(ct->key_file)) {
+                       free(ct->key_file);
+                       ct->key_file = make_message("%s/.ssh/id_rsa", home);
+               }
+       }
 
        if (loglevel)
                *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
@@ -413,7 +419,6 @@ out:
        if (ret < 0) {
                PARA_ERROR_LOG("%s\n", para_strerror(-ret));
                btr_remove_node(ct->btrn);
-               btr_free_node(ct->btrn);
                client_close(ct);
                *ct_ptr = NULL;
        }