Fixed a nasty typo when calculating word length.
[routeros-api.git] / src / main.c
index 3f844ab..9232fd9 100644 (file)
@@ -40,6 +40,8 @@
 #include <sys/socket.h>
 #include <netdb.h>
 
+#include <gcrypt.h>
+
 #include "routeros_api.h"
 
 #if 1
@@ -69,6 +71,13 @@ struct mt_reply_s
        mt_reply_t *next;
 };
 
+struct mt_login_data_s
+{
+       const char *username;
+       const char *password;
+};
+typedef struct mt_login_data_s mt_login_data_t;
+
 /*
  * Private functions
  */
@@ -156,6 +165,30 @@ static int reply_add_keyval (mt_reply_t *r, const char *key, /* {{{ */
        return (0);
 } /* }}} int reply_add_keyval */
 
+static void reply_dump (const mt_reply_t *r) /* {{{ */
+{
+       if (r == NULL)
+               return;
+
+       printf ("=== BEGIN REPLY ===\n"
+                       "Address: %p\n"
+                       "Status: %s\n",
+                       (void *) r, r->status);
+       if (r->params_num > 0)
+       {
+               unsigned int i;
+
+               printf ("Arguments:\n");
+               for (i = 0; i < r->params_num; i++)
+                       printf (" %3u: %s = %s\n", i, r->keys[i], r->values[i]);
+       }
+       if (r->next != NULL)
+               printf ("Next: %p\n", (void *) r->next);
+       printf ("=== END REPLY ===\n");
+
+       reply_dump (r->next);
+} /* }}} void reply_dump */
+
 static void reply_free (mt_reply_t *r) /* {{{ */
 {
        mt_reply_t *next;
@@ -315,6 +348,7 @@ static int send_command (mt_connection_t *c, /* {{{ */
        if (status != 0)
                return (status);
 
+       mt_debug ("send_command: command = %s;\n", command);
        status = buffer_add (&buffer_ptr, &buffer_size, command);
        if (status != 0)
                return (status);
@@ -324,6 +358,7 @@ static int send_command (mt_connection_t *c, /* {{{ */
                if (args[i] == NULL)
                        return (EINVAL);
 
+               mt_debug ("send_command: arg[%zu] = %s;\n", i, args[i]);
                status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
                if (status != 0)
                        return (status);
@@ -373,48 +408,48 @@ static int read_word (mt_connection_t *c, /* {{{ */
                return (status);
 
        /* Calculate `req_size' */
-       if (((unsigned char) buffer[0]) == 0xF0) /* {{{ */
+       if (((unsigned char) word_length[0]) == 0xF0) /* {{{ */
        {
                status = read_exact (c->fd, &word_length[1], 4);
                if (status != 0)
                        return (status);
 
-               req_size = (buffer[1] << 24)
-                       | (buffer[2] << 16)
-                       | (buffer[3] << 8)
-                       | buffer[4];
+               req_size = (word_length[1] << 24)
+                       | (word_length[2] << 16)
+                       | (word_length[3] << 8)
+                       | word_length[4];
        }
-       else if ((buffer[0] & 0xE0) == 0xE0)
+       else if ((word_length[0] & 0xE0) == 0xE0)
        {
                status = read_exact (c->fd, &word_length[1], 3);
                if (status != 0)
                        return (status);
 
-               req_size = ((buffer[0] & 0x1F) << 24)
-                       | (buffer[1] << 16)
-                       | (buffer[2] << 8)
-                       | buffer[3];
+               req_size = ((word_length[0] & 0x1F) << 24)
+                       | (word_length[1] << 16)
+                       | (word_length[2] << 8)
+                       | word_length[3];
        }
-       else if ((buffer[0] & 0xC0) == 0xC0)
+       else if ((word_length[0] & 0xC0) == 0xC0)
        {
                status = read_exact (c->fd, &word_length[1], 2);
                if (status != 0)
                        return (status);
 
-               req_size = ((buffer[0] & 0x3F) << 16)
-                       | (buffer[1] << 8)
-                       | buffer[2];
+               req_size = ((word_length[0] & 0x3F) << 16)
+                       | (word_length[1] << 8)
+                       | word_length[2];
        }
-       else if ((buffer[0] & 0x80) == 0x80)
+       else if ((word_length[0] & 0x80) == 0x80)
        {
                status = read_exact (c->fd, &word_length[1], 1);
                if (status != 0)
                        return (status);
 
-               req_size = ((buffer[0] & 0x7F) << 8)
-                       | buffer[1];
+               req_size = ((word_length[0] & 0x7F) << 8)
+                       | word_length[1];
        }
-       else if ((buffer[0] & 0x80) == 0)
+       else if ((word_length[0] & 0x80) == 0)
        {
                req_size = (size_t) word_length[0];
        }
@@ -443,17 +478,17 @@ static int read_word (mt_connection_t *c, /* {{{ */
        return (0);
 } /* }}} int buffer_decode_next */
 
-static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
+static mt_reply_t *receive_sentence (mt_connection_t *c) /* {{{ */
 {
        char buffer[4096];
        size_t buffer_size;
        int status;
 
-       mt_reply_t *head;
-       mt_reply_t *tail;
+       mt_reply_t *r;
 
-       head = NULL;
-       tail = NULL;
+       r = reply_alloc ();
+       if (r == NULL)
+               return (NULL);
 
        while (42)
        {
@@ -470,33 +505,11 @@ static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
 
                if (buffer[0] == '!') /* {{{ */
                {
-                       mt_reply_t *tmp;
-
-                       tmp = reply_alloc ();
-                       if (tmp == NULL)
-                       {
-                               status = ENOMEM;
+                       if (r->status != NULL)
+                               free (r->status);
+                       r->status = strdup (&buffer[1]);
+                       if (r->status == NULL)
                                break;
-                       }
-
-                       tmp->status = strdup (&buffer[1]);
-                       if (tmp->status == NULL)
-                       {
-                               reply_free (tmp);
-                               status = ENOMEM;
-                               break;
-                       }
-
-                       if (tail == NULL)
-                       {
-                               head = tmp;
-                               tail = tmp;
-                       }
-                       else
-                       {
-                               tail->next = tmp;
-                               tail = tmp;
-                       }
                } /* }}} if (buffer[0] == '!') */
                else if (buffer[0] == '=') /* {{{ */
                {
@@ -513,20 +526,54 @@ static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
                        *val = 0;
                        val++;
 
-                       reply_add_keyval (tail, key, val);
+                       reply_add_keyval (r, key, val);
                } /* }}} if (buffer[0] == '=') */
                else
                {
-                       printf ("Ignoring unknown word: %s\n", buffer);
+                       mt_debug ("receive_sentence: Ignoring unknown word: %s\n", buffer);
                }
        } /* while (42) */
        
-       if (status != 0)
+       if (r->status == NULL)
        {
-               reply_free (head);
+               reply_free (r);
                return (NULL);
        }
 
+       return (r);
+} /* }}} mt_reply_t *receive_sentence */
+
+static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
+{
+       mt_reply_t *head;
+       mt_reply_t *tail;
+
+       head = NULL;
+       tail = NULL;
+
+       while (42)
+       {
+               mt_reply_t *tmp;
+
+               tmp = receive_sentence (c);
+               if (tmp == NULL)
+                       break;
+
+               if (tail == NULL)
+               {
+                       head = tmp;
+                       tail = tmp;
+               }
+               else
+               {
+                       tail->next = tmp;
+                       tail = tmp;
+               }
+
+               if (strcmp ("done", tmp->status) == 0)
+                       break;
+       } /* while (42) */
+       
        return (head);
 } /* }}} mt_reply_t *receive_reply */
 
@@ -582,6 +629,141 @@ static int create_socket (const char *node, const char *service) /* {{{ */
        return (-1);
 } /* }}} int create_socket */
 
+static int login2_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
+               void *user_data)
+{
+       if (r == NULL)
+               return (EINVAL);
+
+       printf ("login2_handler has been called.\n");
+       reply_dump (r);
+
+       if (strcmp (r->status, "done") != 0)
+       {
+               mt_debug ("login2_handler: Unexpected status: %s.\n", r->status);
+               return (EPROTO);
+       }
+
+       return (0);
+} /* }}} int login2_handler */
+
+static void hash_binary_to_hex (char hex[33], uint8_t binary[16]) /* {{{ */
+{
+       int i;
+
+       for (i = 0; i < 16; i++)
+       {
+               char tmp[3];
+               snprintf (tmp, 3, "%02"PRIx8, binary[i]);
+               tmp[2] = 0;
+               hex[2*i] = tmp[0];
+               hex[2*i+1] = tmp[1];
+       }
+       hex[32] = 0;
+} /* }}} void hash_binary_to_hex */
+
+static void hash_hex_to_binary (uint8_t binary[16], char hex[33]) /* {{{ */
+{
+       int i;
+
+       for (i = 0; i < 16; i++)
+       {
+               char tmp[3];
+
+               tmp[0] = hex[2*i];
+               tmp[1] = hex[2*i + 1];
+               tmp[2] = 0;
+
+               binary[i] = (uint8_t) strtoul (tmp, /* endptr = */ NULL, /* base = */ 16);
+       }
+} /* }}} void hash_hex_to_binary */
+
+static void make_password_hash (char response_hex[33], /* {{{ */
+               const char *password, size_t password_length, char challenge_hex[33])
+{
+       uint8_t challenge_bin[16];
+       uint8_t response_bin[16];
+       char data_buffer[password_length+17];
+       gcry_md_hd_t md_handle;
+
+       hash_hex_to_binary (challenge_bin, challenge_hex);
+
+       data_buffer[0] = 0;
+       memcpy (&data_buffer[1], password, password_length);
+       memcpy (&data_buffer[1+password_length], challenge_bin, 16);
+
+       gcry_md_open (&md_handle, GCRY_MD_MD5, /* flags = */ 0);
+       gcry_md_write (md_handle, data_buffer, sizeof (data_buffer));
+       memcpy (response_bin, gcry_md_read (md_handle, GCRY_MD_MD5), 16);
+       gcry_md_close (md_handle);
+
+       hash_binary_to_hex (response_hex, response_bin);
+} /* }}} void make_password_hash */
+
+static int login_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
+               void *user_data)
+{
+       const char *ret;
+       char challenge_hex[33];
+       char response_hex[33];
+       mt_login_data_t *login_data;
+
+       const char *params[2];
+       char param_name[1024];
+       char param_response[64];
+
+       if (r == NULL)
+               return (EINVAL);
+
+       /* The expected result looks like this:
+        * -- 8< --
+        *  !done 
+        *  =ret=ebddd18303a54111e2dea05a92ab46b4
+        * -- >8 --
+        */
+
+       printf ("login_handler has been called.\n");
+       reply_dump (r);
+
+       if (strcmp (r->status, "done") != 0)
+       {
+               mt_debug ("login_handler: Unexpected status: %s.\n", r->status);
+               return (EPROTO);
+       }
+
+       login_data = user_data;
+       if (login_data == NULL)
+               return (EINVAL);
+
+       ret = mt_reply_param_val_by_key (r, "ret");
+       if (ret == NULL)
+       {
+               mt_debug ("login_handler: Reply does not have parameter \"ret\".\n");
+               return (EPROTO);
+       }
+       mt_debug ("login_handler: ret = %s;\n", ret);
+
+       if (strlen (ret) != 32)
+       {
+               mt_debug ("login_handler: Unexpected length of the \"ret\" argument.\n");
+               return (EPROTO);
+       }
+       strcpy (challenge_hex, ret);
+
+       make_password_hash (response_hex, 
+                       login_data->password, strlen (login_data->password),
+                       challenge_hex);
+
+       snprintf (param_name, sizeof (param_name), "=name=%s", login_data->username);
+       snprintf (param_response, sizeof (param_response),
+                       "=response=00%s", response_hex);
+       params[0] = param_name;
+       params[1] = param_response;
+
+       return (mt_query (c, "/login", 2, params, login2_handler,
+                               /* user data = */ NULL));
+} /* }}} int login_handler */
+
 /*
  * Public functions
  */
@@ -590,6 +772,8 @@ mt_connection_t *mt_connect (const char *node, const char *service, /* {{{ */
 {
        int fd;
        mt_connection_t *c;
+       int status;
+       mt_login_data_t user_data;
 
        if ((node == NULL) || (username == NULL) || (password == NULL))
                return (NULL);
@@ -608,6 +792,11 @@ mt_connection_t *mt_connect (const char *node, const char *service, /* {{{ */
 
        c->fd = fd;
 
+       user_data.username = username;
+       user_data.password = password;
+       status = mt_query (c, "/login", /* args num = */ 0, /* args = */ NULL,
+                       login_handler, &user_data);
+
        return (c);
 } /* }}} mt_connection_t *mt_connect */
 
@@ -673,6 +862,13 @@ int mt_reply_num (const mt_reply_t *r) /* {{{ */
        return (ret);
 } /* }}} int mt_reply_num */
 
+const char *mt_reply_status (const mt_reply_t *r) /* {{{ */
+{
+       if (r == NULL)
+               return (NULL);
+       return (r->status);
+} /* }}} char *mt_reply_status */
+
 const char *mt_reply_param_key_by_index (const mt_reply_t *r, /* {{{ */
                unsigned int index)
 {