2 * libmikrotik - src/main.c
3 * Copyright (C) 2009 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
26 #ifndef _POSIX_C_SOURCE
27 # define _POSIX_C_SOURCE 200112L
39 #include <sys/types.h>
40 #include <sys/socket.h>
45 #include "routeros_api.h"
48 # define mt_debug(...) fprintf (stdout, __VA_ARGS__)
50 # define mt_debug(...) /**/
54 char *strdup (const char *);
59 struct mt_connection_s
66 unsigned int params_num;
74 struct mt_login_data_s
79 typedef struct mt_login_data_s mt_login_data_t;
84 static int read_exact (int fd, void *buffer, size_t buffer_size) /* {{{ */
89 if ((fd < 0) || (buffer == NULL))
97 while (have_bytes < buffer_size)
102 want_bytes = buffer_size - have_bytes;
104 status = read (fd, buffer_ptr, want_bytes);
113 assert (status <= want_bytes);
114 have_bytes += status;
115 buffer_ptr += status;
119 } /* }}} int read_exact */
121 static mt_reply_t *reply_alloc (void) /* {{{ */
125 r = malloc (sizeof (*r));
129 memset (r, 0, sizeof (*r));
135 } /* }}} mt_reply_s *reply_alloc */
137 static int reply_add_keyval (mt_reply_t *r, const char *key, /* {{{ */
142 tmp = realloc (r->keys, (r->params_num + 1) * sizeof (*tmp));
147 tmp = realloc (r->values, (r->params_num + 1) * sizeof (*tmp));
152 r->keys[r->params_num] = strdup (key);
153 if (r->keys[r->params_num] == NULL)
156 r->values[r->params_num] = strdup (val);
157 if (r->values[r->params_num] == NULL)
159 free (r->keys[r->params_num]);
160 r->keys[r->params_num] = NULL;
166 } /* }}} int reply_add_keyval */
168 static void reply_free (mt_reply_t *r) /* {{{ */
178 for (i = 0; i < r->params_num; i++)
190 } /* }}} void reply_free */
192 static int buffer_init (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
194 if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
197 if (*ret_buffer_size < 1)
201 } /* }}} int buffer_init */
203 static int buffer_add (char **ret_buffer, size_t *ret_buffer_size, /* {{{ */
211 if ((ret_buffer == NULL) || (ret_buffer_size == NULL) || (string == NULL))
214 buffer = *ret_buffer;
215 buffer_size = *ret_buffer_size;
217 string_size = strlen (string);
218 if (string_size == 0)
221 if (string_size >= 0x10000000)
222 req_size = 5 + string_size;
223 else if (string_size >= 0x200000)
224 req_size = 4 + string_size;
225 else if (string_size >= 0x4000)
226 req_size = 3 + string_size;
227 else if (string_size >= 0x80)
228 req_size = 2 + string_size;
230 req_size = 1 + string_size;
232 if (*ret_buffer_size < req_size)
235 if (string_size >= 0x10000000)
238 buffer[1] = (string_size >> 24) & 0xff;
239 buffer[2] = (string_size >> 16) & 0xff;
240 buffer[3] = (string_size >> 8) & 0xff;
241 buffer[4] = (string_size ) & 0xff;
245 else if (string_size >= 0x200000)
247 buffer[0] = (string_size >> 24) & 0x1f;
249 buffer[1] = (string_size >> 16) & 0xff;
250 buffer[2] = (string_size >> 8) & 0xff;
251 buffer[3] = (string_size ) & 0xff;
255 else if (string_size >= 0x4000)
257 buffer[0] = (string_size >> 16) & 0x3f;
259 buffer[1] = (string_size >> 8) & 0xff;
260 buffer[2] = (string_size ) & 0xff;
264 else if (string_size >= 0x80)
266 buffer[0] = (string_size >> 8) & 0x7f;
268 buffer[1] = (string_size ) & 0xff;
272 else /* if (string_size <= 0x7f) */
274 buffer[0] = (char) string_size;
279 assert (buffer_size >= string_size);
280 memcpy (buffer, string, string_size);
281 buffer += string_size;
282 buffer_size -= string_size;
284 *ret_buffer = buffer;
285 *ret_buffer_size = buffer_size;
288 } /* }}} int buffer_add */
290 static int buffer_end (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
292 if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
295 if (*ret_buffer_size < 1)
298 /* Add empty word. */
299 (*ret_buffer)[0] = 0;
301 (*ret_buffer_size)--;
304 } /* }}} int buffer_end */
306 static int send_command (mt_connection_t *c, /* {{{ */
308 size_t args_num, const char * const *args)
317 /* FIXME: For debugging only */
318 memset (buffer, 0, sizeof (buffer));
321 buffer_size = sizeof (buffer);
323 status = buffer_init (&buffer_ptr, &buffer_size);
327 status = buffer_add (&buffer_ptr, &buffer_size, command);
331 for (i = 0; i < args_num; i++)
336 status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
341 status = buffer_end (&buffer_ptr, &buffer_size);
346 buffer_size = sizeof (buffer) - buffer_size;
347 while (buffer_size > 0)
349 ssize_t bytes_written;
352 bytes_written = write (c->fd, buffer_ptr, buffer_size);
353 if (bytes_written < 0)
360 assert (bytes_written <= buffer_size);
362 buffer_ptr += bytes_written;
363 buffer_size -= bytes_written;
364 } /* while (buffer_size > 0) */
367 } /* }}} int send_command */
369 static int read_word (mt_connection_t *c, /* {{{ */
370 char *buffer, size_t *buffer_size)
373 uint8_t word_length[5];
376 if ((buffer == NULL) || (*buffer_size < 1))
379 /* read one byte from the socket */
380 status = read_exact (c->fd, word_length, 1);
384 /* Calculate `req_size' */
385 if (((unsigned char) buffer[0]) == 0xF0) /* {{{ */
387 status = read_exact (c->fd, &word_length[1], 4);
391 req_size = (buffer[1] << 24)
396 else if ((buffer[0] & 0xE0) == 0xE0)
398 status = read_exact (c->fd, &word_length[1], 3);
402 req_size = ((buffer[0] & 0x1F) << 24)
407 else if ((buffer[0] & 0xC0) == 0xC0)
409 status = read_exact (c->fd, &word_length[1], 2);
413 req_size = ((buffer[0] & 0x3F) << 16)
417 else if ((buffer[0] & 0x80) == 0x80)
419 status = read_exact (c->fd, &word_length[1], 1);
423 req_size = ((buffer[0] & 0x7F) << 8)
426 else if ((buffer[0] & 0x80) == 0)
428 req_size = (size_t) word_length[0];
432 /* First nibble is `F' but second nibble is not `0'. */
436 if (*buffer_size < req_size)
439 /* Empty word. This ends a `sentence' and must therefore be valid. */
447 status = read_exact (c->fd, buffer, req_size);
450 *buffer_size = req_size;
453 } /* }}} int buffer_decode_next */
455 static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
469 buffer_size = sizeof (buffer) - 1;
470 status = read_word (c, buffer, &buffer_size);
473 assert (buffer_size < sizeof (buffer));
474 buffer[buffer_size] = 0;
476 /* Empty word means end of reply */
477 if (buffer_size == 0)
480 if (buffer[0] == '!') /* {{{ */
484 tmp = reply_alloc ();
491 tmp->status = strdup (&buffer[1]);
492 if (tmp->status == NULL)
509 } /* }}} if (buffer[0] == '!') */
510 else if (buffer[0] == '=') /* {{{ */
512 char *key = &buffer[1];
516 val = strchr (key, '=');
519 fprintf (stderr, "Ignoring misformed word: %s\n", buffer);
525 reply_add_keyval (tail, key, val);
526 } /* }}} if (buffer[0] == '=') */
529 printf ("Ignoring unknown word: %s\n", buffer);
540 } /* }}} mt_reply_t *receive_reply */
542 static int create_socket (const char *node, const char *service) /* {{{ */
544 struct addrinfo ai_hint;
545 struct addrinfo *ai_list;
546 struct addrinfo *ai_ptr;
549 mt_debug ("create_socket (node = %s, service = %s);\n",
552 memset (&ai_hint, 0, sizeof (ai_hint));
554 ai_hint.ai_flags |= AI_ADDRCONFIG;
556 ai_hint.ai_family = AF_UNSPEC;
557 ai_hint.ai_socktype = SOCK_STREAM;
560 status = getaddrinfo (node, service, &ai_hint, &ai_list);
563 assert (ai_list != NULL);
565 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
570 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
571 ai_ptr->ai_protocol);
574 mt_debug ("create_socket: socket(2) failed.\n");
578 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
581 mt_debug ("create_socket: connect(2) failed.\n");
589 freeaddrinfo (ai_list);
592 } /* }}} int create_socket */
594 static int login2_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
600 printf ("login2_handler has been called.\n");
603 } /* }}} int login2_handler */
605 static void hash_binary_to_hex (char hex[33], uint8_t binary[16]) /* {{{ */
609 for (i = 0; i < 16; i++)
612 snprintf (tmp, 3, "%02"PRIx8, binary[i]);
618 } /* }}} void hash_binary_to_hex */
620 static void hash_hex_to_binary (uint8_t binary[16], char hex[33]) /* {{{ */
624 for (i = 0; i < 16; i++)
629 tmp[1] = hex[2*i + 1];
632 binary[i] = (uint8_t) strtoul (tmp, /* endptr = */ NULL, /* base = */ 16);
634 } /* }}} void hash_hex_to_binary */
636 static void make_password_hash (char response_hex[33], /* {{{ */
637 const char *password, size_t password_length, char challenge_hex[33])
639 uint8_t challenge_bin[16];
640 uint8_t response_bin[16];
641 char data_buffer[password_length+17];
642 gcry_md_hd_t md_handle;
644 hash_hex_to_binary (challenge_bin, challenge_hex);
647 memcpy (&data_buffer[1], password, password_length);
648 memcpy (&data_buffer[1+password_length], challenge_bin, 16);
650 gcry_md_open (&md_handle, GCRY_MD_MD5, /* flags = */ 0);
651 gcry_md_write (md_handle, data_buffer, sizeof (data_buffer));
652 memcpy (response_bin, gcry_md_read (md_handle, GCRY_MD_MD5), 16);
653 gcry_md_close (md_handle);
655 hash_binary_to_hex (response_hex, response_bin);
656 } /* }}} void make_password_hash */
658 static int login_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
662 char challenge_hex[33];
663 char response_hex[33];
664 mt_login_data_t *login_data;
666 const char *params[2];
667 char param_name[1024];
668 char param_response[64];
673 printf ("login_handler has been called.\n");
675 login_data = user_data;
676 if (login_data == NULL)
679 ret = mt_reply_param_val_by_key (r, "ret");
682 mt_debug ("login_handler: Reply does not have parameter \"ret\".\n");
685 mt_debug ("login_handler: ret = %s;\n", ret);
687 if (strlen (ret) != 32)
689 mt_debug ("login_handler: Unexpected length of the \"ret\" argument.\n");
692 strcpy (challenge_hex, ret);
694 make_password_hash (response_hex,
695 login_data->password, strlen (login_data->password),
698 snprintf (param_name, sizeof (param_name), "=name=%s", login_data->username);
699 snprintf (param_response, sizeof (param_response),
700 "=response=00%s", response_hex);
701 params[0] = param_name;
702 params[1] = param_response;
704 return (mt_query (c, "/login", 2, params, login2_handler,
705 /* user data = */ NULL));
706 } /* }}} int login_handler */
711 mt_connection_t *mt_connect (const char *node, const char *service, /* {{{ */
712 const char *username, const char *password)
717 mt_login_data_t user_data;
719 if ((node == NULL) || (username == NULL) || (password == NULL))
722 fd = create_socket (node, (service != NULL) ? service : ROUTEROS_API_PORT);
726 c = malloc (sizeof (*c));
732 memset (c, 0, sizeof (*c));
736 user_data.username = username;
737 user_data.password = password;
738 status = mt_query (c, "/login", /* args num = */ 0, /* args = */ NULL,
739 login_handler, &user_data);
742 } /* }}} mt_connection_t *mt_connect */
744 int mt_disconnect (mt_connection_t *c) /* {{{ */
758 } /* }}} int mt_disconnect */
760 int mt_query (mt_connection_t *c, /* {{{ */
762 size_t args_num, const char * const *args,
763 mt_reply_handler_t handler, void *user_data)
768 status = send_command (c, command, args_num, args);
772 r = receive_reply (c);
776 /* Call the callback function with the data we received. */
777 status = (*handler) (c, r, user_data);
779 /* Free the allocated memory ... */
782 /* ... and return. */
784 } /* }}} int mt_query */
786 const mt_reply_t *mt_reply_next (const mt_reply_t *r) /* {{{ */
792 } /* }}} mt_reply_t *mt_reply_next */
794 int mt_reply_num (const mt_reply_t *r) /* {{{ */
797 const mt_reply_t *ptr;
800 for (ptr = r; ptr != NULL; ptr = ptr->next)
804 } /* }}} int mt_reply_num */
806 const char *mt_reply_status (const mt_reply_t *r) /* {{{ */
811 } /* }}} char *mt_reply_status */
813 const char *mt_reply_param_key_by_index (const mt_reply_t *r, /* {{{ */
819 if (index >= r->params_num)
822 return (r->keys[index]);
823 } /* }}} char *mt_reply_param_key_by_index */
825 const char *mt_reply_param_val_by_index (const mt_reply_t *r, /* {{{ */
831 if (index >= r->params_num)
834 return (r->values[index]);
835 } /* }}} char *mt_reply_param_key_by_index */
837 const char *mt_reply_param_val_by_key (const mt_reply_t *r, /* {{{ */
842 if ((r == NULL) || (key == NULL))
845 for (i = 0; i < r->params_num; i++)
846 if (strcmp (r->keys[i], key) == 0)
847 return (r->values[i]);
850 } /* }}} char *mt_reply_param_val_by_key */
852 /* vim: set ts=2 sw=2 noet fdm=marker : */