client: Fix a memory leak in client_post_select().
[paraslash.git] / client_common.c
index ee3a13f75911788f10f325c905a0ac0bc8fd6356..bd5a7bfba0cbce4dfeb9ac75ef5a523f5c6d6e5c 100644 (file)
@@ -1,13 +1,15 @@
 /*
- * Copyright (C) 1997-2008 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 1997-2009 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 /** \file client_common.c Common functions of para_client and para_audiod. */
 
+#include <regex.h>
 #include <sys/types.h>
 #include <dirent.h>
+#include <openssl/rc4.h>
 
 #include "para.h"
 #include "error.h"
 #include "string.h"
 #include "client.cmdline.h"
 #include "client.h"
+#include "hash.h"
+#include "buffer_tree.h"
 
-/*
- * Rc4-encrypt data before sending.
- *
- * \param len The number of bytes to encrypt.
- * \param indata Pointer to the input data of length \a len to be encrypted.
- * \param outdata Result-pointer that holds the encrypted data.
- * \param private_data Contains the rc4 key.
- */
-static void rc4_send(unsigned long len, const unsigned char *indata,
-               unsigned char *outdata, void *private_data)
-{
-       struct client_task *ct = private_data;
-       RC4(&ct->rc4_send_key, len, indata, outdata);
-}
-
-/*
- * Rc4-decrypt received data.
- *
- * Parameters are identical to those of rc4_send.
- */
-static void rc4_recv(unsigned long len, const unsigned char *indata,
-               unsigned char *outdata, void *private_data)
-{
-       struct client_task *ct = private_data;
-       RC4(&ct->rc4_recv_key, len, indata, outdata);
-}
+/** The size of the receiving buffer. */
+#define CLIENT_BUFSIZE 4000
 
 /**
  * Close the connection to para_server and free all resources.
@@ -60,10 +40,8 @@ void client_close(struct client_task *ct)
 {
        if (!ct)
                return;
-       if (ct->fd >= 0) {
-               disable_crypt(ct->fd);
-               close(ct->fd);
-       }
+       if (ct->rc4c.fd >= 0)
+               close(ct->rc4c.fd);
        free(ct->user);
        free(ct->config_file);
        free(ct->key_file);
@@ -87,60 +65,59 @@ void client_close(struct client_task *ct)
  */
 static void client_pre_select(struct sched *s, struct task *t)
 {
+       int ret;
        struct client_task *ct = container_of(t, struct client_task, task);
+       struct btr_node *btrn = ct->btrn;
 
-       ct->check_r = 0;
-       ct->check_w = 0;
-       if (ct->fd < 0)
+       if (ct->rc4c.fd < 0)
                return;
        switch (ct->status) {
        case CL_CONNECTED:
        case CL_SENT_AUTH:
        case CL_SENT_CH_RESPONSE:
        case CL_SENT_COMMAND:
-               para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
-               ct->check_r = 1;
+               para_fd_set(ct->rc4c.fd, &s->rfds, &s->max_fileno);
                return;
 
        case CL_RECEIVED_WELCOME:
-       case CL_RECEIVED_CHALLENGE:
        case CL_RECEIVED_PROCEED:
-               para_fd_set(ct->fd, &s->wfds, &s->max_fileno);
-               ct->check_w = 1;
+               para_fd_set(ct->rc4c.fd, &s->wfds, &s->max_fileno);
                return;
 
        case CL_RECEIVING:
-               if (ct->loaded < CLIENT_BUFSIZE - 1) {
-                       para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
-                       ct->check_r = 1;
+               ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
+               if (ret != 0) {
+                       if (ret < 0)
+                               sched_min_delay(s);
+                       else
+                               para_fd_set(ct->rc4c.fd, &s->rfds,
+                                       &s->max_fileno);
                }
                return;
        case CL_SENDING:
-               if (*ct->in_loaded) {
-                       PARA_INFO_LOG("loaded: %zd\n", *ct->in_loaded);
-                       para_fd_set(ct->fd, &s->wfds, &s->max_fileno);
-                       ct->check_w = 1;
-               } else {
-                       if (*ct->in_error) {
-                               t->error = *ct->in_error;
-                               s->timeout.tv_sec = 0;
-                               s->timeout.tv_usec = 1;
-                       }
+               ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
+               if (ret != 0) {
+                       if (ret < 0)
+                               sched_min_delay(s);
+                       else
+                               para_fd_set(ct->rc4c.fd, &s->wfds,
+                                       &s->max_fileno);
                }
                return;
        }
 }
 
-static ssize_t client_recv_buffer(struct client_task *ct)
+static ssize_t client_recv_buffer(struct client_task *ct, char *buf, size_t len)
 {
-       ssize_t ret = recv_buffer(ct->fd, ct->buf + ct->loaded,
-               CLIENT_BUFSIZE - ct->loaded);
-       if (!ret)
+       ssize_t ret;
+
+       if (ct->status < CL_SENT_CH_RESPONSE)
+               ret = recv_buffer(ct->rc4c.fd, buf, len);
+       else
+               ret = rc4_recv_buffer(&ct->rc4c, buf, len);
+       if (ret == 0)
                return -E_SERVER_EOF;
-       if (ret > 0)
-               ct->loaded += ret;
        return ret;
-
 }
 
 /**
@@ -159,87 +136,91 @@ static ssize_t client_recv_buffer(struct client_task *ct)
 static void client_post_select(struct sched *s, struct task *t)
 {
        struct client_task *ct = container_of(t, struct client_task, task);
+       struct btr_node *btrn = ct->btrn;
+       int ret = 0;
+       char buf[CLIENT_BUFSIZE];
 
        t->error = 0;
-       if (ct->fd < 0)
-               return;
-       if (!ct->check_r && !ct->check_w)
-               return;
-       if (ct->check_r && !FD_ISSET(ct->fd, &s->rfds))
-               return;
-       if (ct->check_w && !FD_ISSET(ct->fd, &s->wfds))
+       if (ct->rc4c.fd < 0)
                return;
        switch (ct->status) {
        case CL_CONNECTED: /* receive welcome message */
-               t->error = client_recv_buffer(ct);
-               if (t->error > 0)
-                       ct->status = CL_RECEIVED_WELCOME;
+               if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
+                       return;
+               ret = client_recv_buffer(ct, buf, sizeof(buf));
+               if (ret < 0)
+                       goto err;
+               ct->status = CL_RECEIVED_WELCOME;
                return;
        case CL_RECEIVED_WELCOME: /* send auth command */
-               sprintf(ct->buf, "auth %s%s", ct->conf.plain_given?
-                       "" : "rc4 ", ct->user);
-               PARA_INFO_LOG("--> %s\n", ct->buf);
-               t->error = send_buffer(ct->fd, ct->buf);
-               if (t->error >= 0)
-                       ct->status = CL_SENT_AUTH;
-               return;
-       case CL_SENT_AUTH: /* receive challenge number */
-               ct->loaded = 0;
-               t->error = client_recv_buffer(ct);
-               if (t->error < 0)
+               sprintf(buf, AUTH_REQUEST_MSG "%s", ct->user);
+               PARA_INFO_LOG("--> %s\n", buf);
+               if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
                        return;
-               if (t->error != 64) {
-                       t->error = -E_INVALID_CHALLENGE;
-                       PARA_ERROR_LOG("received the following: %s\n", ct->buf);
-                       return;
-               }
-               PARA_INFO_LOG("<-- [challenge]\n");
-               /* decrypt challenge number */
-               t->error = para_decrypt_challenge(ct->key_file, &ct->challenge_nr,
-                       (unsigned char *) ct->buf, 64);
-               if (t->error > 0)
-                       ct->status = CL_RECEIVED_CHALLENGE;
+               ret = send_buffer(ct->rc4c.fd, buf);
+               if (ret < 0)
+                       goto err;
+               ct->status = CL_SENT_AUTH;
                return;
-       case CL_RECEIVED_CHALLENGE: /* send decrypted challenge */
-               PARA_INFO_LOG("--> %lu\n", ct->challenge_nr);
-               t->error = send_va_buffer(ct->fd, "%s%lu", CHALLENGE_RESPONSE_MSG,
-                       ct->challenge_nr);
-               if (t->error > 0)
-                       ct->status = CL_SENT_CH_RESPONSE;
+       case CL_SENT_AUTH:
+               /*
+                * Receive challenge and rc4 keys, decrypt the challenge and
+                * send back the hash of the decrypted challenge.
+                */
+               {
+               /* decrypted challenge/rc4 buffer */
+               unsigned char crypt_buf[1024];
+               /* the SHA1 of the decrypted challenge */
+               unsigned char challenge_sha1[HASH_SIZE];
+
+               if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
+                       return;
+               ret = client_recv_buffer(ct, buf, sizeof(buf));
+               if (ret < 0)
+                       goto err;
+               PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", ret);
+               ret = para_decrypt_buffer(ct->key_file, crypt_buf,
+                       (unsigned char *)buf, ret);
+               if (ret < 0)
+                       goto err;
+               sha1_hash((char *)crypt_buf, CHALLENGE_SIZE, challenge_sha1);
+               RC4_set_key(&ct->rc4c.send_key, RC4_KEY_LEN,
+                       crypt_buf + CHALLENGE_SIZE);
+               RC4_set_key(&ct->rc4c.recv_key, RC4_KEY_LEN,
+                       crypt_buf + CHALLENGE_SIZE + RC4_KEY_LEN);
+               hash_to_asc(challenge_sha1, buf);
+               PARA_INFO_LOG("--> %s\n", buf);
+               ret = send_bin_buffer(ct->rc4c.fd, (char *)challenge_sha1,
+                       HASH_SIZE);
+               if (ret < 0)
+                       goto err;
+               ct->status = CL_SENT_CH_RESPONSE;
                return;
+               }
        case CL_SENT_CH_RESPONSE: /* read server response */
                {
                size_t bytes_received;
-               unsigned char rc4_buf[2 * RC4_KEY_LEN] = "";
-               ct->loaded = 0;
-               t->error = client_recv_buffer(ct);
-               if (t->error < 0)
+               if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
                        return;
-               bytes_received = t->error;
-               PARA_DEBUG_LOG("++++ server info ++++\n%s\n++++ end of server "
-                       "info ++++\n", ct->buf);
+               ret = client_recv_buffer(ct, buf, sizeof(buf));
+               if (ret < 0)
+                       goto err;
+               bytes_received = ret;
                /* check if server has sent "Proceed" message */
-               t->error = -E_CLIENT_AUTH;
-               if (!strstr(ct->buf, PROCEED_MSG))
-                       return;
-               t->error = 0;
+               ret = -E_CLIENT_AUTH;
+               if (bytes_received < PROCEED_MSG_LEN)
+                       goto err;
+               if (!strstr(buf, PROCEED_MSG))
+                       goto err;
                ct->status = CL_RECEIVED_PROCEED;
-               if (bytes_received < PROCEED_MSG_LEN + 32)
-                       return;
-               PARA_INFO_LOG("decrypting session key\n");
-               t->error = para_decrypt_buffer(ct->key_file, rc4_buf,
-                       (unsigned char *)ct->buf + PROCEED_MSG_LEN + 1,
-                       bytes_received - PROCEED_MSG_LEN - 1);
-               if (t->error < 0)
-                       return;
-               RC4_set_key(&ct->rc4_send_key, RC4_KEY_LEN, rc4_buf);
-               RC4_set_key(&ct->rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
-               enable_crypt(ct->fd, rc4_recv, rc4_send, ct);
+               return;
                }
        case CL_RECEIVED_PROCEED: /* concat args and send command */
                {
                int i;
                char *command = NULL;
+               if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
+                       return;
                for (i = 0; i < ct->conf.inputs_num; i++) {
                        char *tmp = command;
                        command = make_message("%s\n%s", command?
@@ -248,32 +229,79 @@ static void client_post_select(struct sched *s, struct task *t)
                }
                command = para_strcat(command, EOC_MSG "\n");
                PARA_DEBUG_LOG("--> %s\n", command);
-               t->error = send_buffer(ct->fd, command);
+               ret = rc4_send_buffer(&ct->rc4c, command);
                free(command);
-               if (t->error > 0)
-                       ct->status = CL_SENT_COMMAND;
+               if (ret < 0)
+                       goto err;
+               ct->status = CL_SENT_COMMAND;
                return;
                }
        case CL_SENT_COMMAND:
-               ct->loaded = 0;
-               t->error = client_recv_buffer(ct);
-               if (t->error < 0)
+               {
+               char *buf2;
+               if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
                        return;
-               if (strstr(ct->buf, AWAITING_DATA_MSG))
+               /* can not use "buf" here because we need a malloced buffer */
+               buf2 = para_malloc(CLIENT_BUFSIZE);
+               ret = client_recv_buffer(ct, buf2, CLIENT_BUFSIZE);
+               if (ret < 0) {
+                       free(buf2);
+                       goto err;
+               }
+               if (strstr(buf2, AWAITING_DATA_MSG)) {
+                       free(buf2);
                        ct->status = CL_SENDING;
-               else
-                       ct->status = CL_RECEIVING;
+                       return;
+               }
+               ct->status = CL_RECEIVING;
+               btr_add_output(buf2, ret, btrn);
                return;
-       case CL_SENDING: /* FIXME: might block */
-               PARA_INFO_LOG("loaded: %zd\n", *ct->in_loaded);
-               t->error = send_bin_buffer(ct->fd, ct->inbuf, *ct->in_loaded);
-               if (t->error < 0)
+               }
+       case CL_SENDING:
+               {
+               char *buf2;
+               size_t sz;
+               ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
+               if (ret < 0)
+                       goto err;
+               if (ret == 0)
                        return;
-               *ct->in_loaded = 0;
+               if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
+                       return;
+               sz = btr_next_buffer(btrn, &buf2);
+               ret = rc4_send_bin_buffer(&ct->rc4c, buf2, sz);
+               if (ret < 0)
+                       goto err;
+               btr_consume(btrn, sz);
                return;
+               }
        case CL_RECEIVING:
-               t->error = client_recv_buffer(ct);
+               {
+               char *buf2;
+               ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
+               if (ret < 0)
+                       goto err;
+               if (ret == 0)
+                       return;
+               if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
+                       return;
+               buf2 = para_malloc(CLIENT_BUFSIZE);
+               ret = client_recv_buffer(ct, buf2, CLIENT_BUFSIZE);
+               if (ret < 0) {
+                       free(buf2);
+                       goto err;
+               }
+               buf2 = para_realloc(buf2, ret);
+               btr_add_output(buf2, ret, btrn);
                return;
+               }
+       }
+err:
+       t->error = ret;
+       if (ret < 0) {
+               if (ret != -E_SERVER_EOF && ret != -E_BTR_EOF)
+                       PARA_ERROR_LOG("%s\n", para_strerror(-t->error));
+               btr_remove_node(btrn);
        }
 }
 
@@ -282,14 +310,14 @@ static int client_connect(struct client_task *ct)
 {
        int ret;
 
-       ct->fd = -1;
+       ct->rc4c.fd = -1;
        ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, ct->conf.hostname_arg,
                ct->conf.server_port_arg);
        if (ret < 0)
                return ret;
-       ct->fd = ret;
+       ct->rc4c.fd = ret;
        ct->status = CL_CONNECTED;
-       ret = mark_fd_nonblocking(ct->fd);
+       ret = mark_fd_nonblocking(ct->rc4c.fd);
        if (ret < 0)
                goto err_out;
        ct->task.pre_select = client_pre_select;
@@ -298,8 +326,8 @@ static int client_connect(struct client_task *ct)
        register_task(&ct->task);
        return 1;
 err_out:
-       close(ct->fd);
-       ct->fd = -1;
+       close(ct->rc4c.fd);
+       ct->rc4c.fd = -1;
        return ret;
 }
 
@@ -308,8 +336,9 @@ err_out:
  *
  * \param argc Usual argument count.
  * \param argv Usual argument vector.
- * \param pcd_ptr Points to dynamically allocated and initialized private client data
- * upon successful return.
+ * \param ct_ptr Points to dynamically allocated and initialized client task
+ * struct upon successful return.
+ * \param loglevel If not \p NULL, the number of the loglevel is stored here.
  *
  * Check the command line options given by \a argc and argv, set default values
  * for user name and rsa key file, read further option from the config file.
@@ -317,16 +346,20 @@ err_out:
  *
  * \return Standard.
  */
-int client_open(int argc, char *argv[], struct client_task **ct_ptr)
+int client_open(int argc, char *argv[], struct client_task **ct_ptr,
+               int *loglevel, struct btr_node *parent, struct btr_node *child)
 {
        char *home = para_homedir();
-       struct stat statbuf;
        int ret;
        struct client_task *ct = para_calloc(sizeof(struct client_task));
 
+       ct->btrn = btr_new_node(&(struct btr_node_description)
+               EMBRACE(.name = "client", .parent = parent, .child = child));
        *ct_ptr = ct;
-       ct->fd = -1;
-       ret = client_cmdline_parser(argc, argv, &ct->conf);
+       ct->rc4c.fd = -1;
+       ret = -E_CLIENT_SYNTAX;
+       if (client_cmdline_parser(argc, argv, &ct->conf))
+               goto out;
        HANDLE_VERSION_FLAG("client", ct->conf);
        ret = -E_CLIENT_SYNTAX;
        if (!ct->conf.inputs_num)
@@ -341,23 +374,27 @@ int client_open(int argc, char *argv[], struct client_task **ct_ptr)
        ct->config_file = ct->conf.config_file_given?
                para_strdup(ct->conf.config_file_arg) :
                make_message("%s/.paraslash/client.conf", home);
-       ret = stat(ct->config_file, &statbuf);
-       if (ret && ct->conf.config_file_given) {
+       ret = file_exists(ct->config_file);
+       if (!ret && ct->conf.config_file_given) {
                ret = -E_NO_CONFIG;
                goto out;
        }
-       if (!ret) {
+       if (ret) {
                struct client_cmdline_parser_params params = {
                        .override = 0,
                        .initialize = 0,
                        .check_required = 0,
-                       .check_ambiguity = 0
+                       .check_ambiguity = 0,
+                       .print_errors = 0
                };
-               client_cmdline_parser_config_file(ct->config_file,
-                       &ct->conf, &params);
+               ret = -E_BAD_CONFIG;
+               if (client_cmdline_parser_config_file(ct->config_file,
+                       &ct->conf, &params))
+                       goto out;
        }
-       ret = 1;
-       PARA_INFO_LOG("loglevel: %d\n", ct->conf.loglevel_arg);
+       if (loglevel)
+               *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
+       PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
        PARA_INFO_LOG("config_file: %s\n", ct->config_file);
        PARA_INFO_LOG("key_file: %s\n", ct->key_file);
        PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
@@ -367,9 +404,9 @@ out:
        free(home);
        if (ret < 0) {
                PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+               btr_free_node(ct->btrn);
                client_close(ct);
                *ct_ptr = NULL;
        }
        return ret;
 }
-