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_dump (const mt_reply_t *r) /* {{{ */
173 printf ("=== BEGIN REPLY ===\n"
176 (void *) r, r->status);
177 if (r->params_num > 0)
181 printf ("Arguments:\n");
182 for (i = 0; i < r->params_num; i++)
183 printf (" %3u: %s = %s\n", i, r->keys[i], r->values[i]);
186 printf ("Next: %p\n", (void *) r->next);
187 printf ("=== END REPLY ===\n");
189 reply_dump (r->next);
190 } /* }}} void reply_dump */
192 static void reply_free (mt_reply_t *r) /* {{{ */
202 for (i = 0; i < r->params_num; i++)
214 } /* }}} void reply_free */
216 static int buffer_init (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
218 if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
221 if (*ret_buffer_size < 1)
225 } /* }}} int buffer_init */
227 static int buffer_add (char **ret_buffer, size_t *ret_buffer_size, /* {{{ */
235 if ((ret_buffer == NULL) || (ret_buffer_size == NULL) || (string == NULL))
238 buffer = *ret_buffer;
239 buffer_size = *ret_buffer_size;
241 string_size = strlen (string);
242 if (string_size == 0)
245 if (string_size >= 0x10000000)
246 req_size = 5 + string_size;
247 else if (string_size >= 0x200000)
248 req_size = 4 + string_size;
249 else if (string_size >= 0x4000)
250 req_size = 3 + string_size;
251 else if (string_size >= 0x80)
252 req_size = 2 + string_size;
254 req_size = 1 + string_size;
256 if (*ret_buffer_size < req_size)
259 if (string_size >= 0x10000000)
262 buffer[1] = (string_size >> 24) & 0xff;
263 buffer[2] = (string_size >> 16) & 0xff;
264 buffer[3] = (string_size >> 8) & 0xff;
265 buffer[4] = (string_size ) & 0xff;
269 else if (string_size >= 0x200000)
271 buffer[0] = (string_size >> 24) & 0x1f;
273 buffer[1] = (string_size >> 16) & 0xff;
274 buffer[2] = (string_size >> 8) & 0xff;
275 buffer[3] = (string_size ) & 0xff;
279 else if (string_size >= 0x4000)
281 buffer[0] = (string_size >> 16) & 0x3f;
283 buffer[1] = (string_size >> 8) & 0xff;
284 buffer[2] = (string_size ) & 0xff;
288 else if (string_size >= 0x80)
290 buffer[0] = (string_size >> 8) & 0x7f;
292 buffer[1] = (string_size ) & 0xff;
296 else /* if (string_size <= 0x7f) */
298 buffer[0] = (char) string_size;
303 assert (buffer_size >= string_size);
304 memcpy (buffer, string, string_size);
305 buffer += string_size;
306 buffer_size -= string_size;
308 *ret_buffer = buffer;
309 *ret_buffer_size = buffer_size;
312 } /* }}} int buffer_add */
314 static int buffer_end (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
316 if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
319 if (*ret_buffer_size < 1)
322 /* Add empty word. */
323 (*ret_buffer)[0] = 0;
325 (*ret_buffer_size)--;
328 } /* }}} int buffer_end */
330 static int send_command (mt_connection_t *c, /* {{{ */
332 size_t args_num, const char * const *args)
341 /* FIXME: For debugging only */
342 memset (buffer, 0, sizeof (buffer));
345 buffer_size = sizeof (buffer);
347 status = buffer_init (&buffer_ptr, &buffer_size);
351 mt_debug ("send_command: command = %s;\n", command);
352 status = buffer_add (&buffer_ptr, &buffer_size, command);
356 for (i = 0; i < args_num; i++)
361 mt_debug ("send_command: arg[%zu] = %s;\n", i, args[i]);
362 status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
367 status = buffer_end (&buffer_ptr, &buffer_size);
372 buffer_size = sizeof (buffer) - buffer_size;
373 while (buffer_size > 0)
375 ssize_t bytes_written;
378 bytes_written = write (c->fd, buffer_ptr, buffer_size);
379 if (bytes_written < 0)
386 assert (bytes_written <= buffer_size);
388 buffer_ptr += bytes_written;
389 buffer_size -= bytes_written;
390 } /* while (buffer_size > 0) */
393 } /* }}} int send_command */
395 static int read_word (mt_connection_t *c, /* {{{ */
396 char *buffer, size_t *buffer_size)
399 uint8_t word_length[5];
402 if ((buffer == NULL) || (*buffer_size < 1))
405 /* read one byte from the socket */
406 status = read_exact (c->fd, word_length, 1);
410 /* Calculate `req_size' */
411 if (((unsigned char) buffer[0]) == 0xF0) /* {{{ */
413 status = read_exact (c->fd, &word_length[1], 4);
417 req_size = (buffer[1] << 24)
422 else if ((buffer[0] & 0xE0) == 0xE0)
424 status = read_exact (c->fd, &word_length[1], 3);
428 req_size = ((buffer[0] & 0x1F) << 24)
433 else if ((buffer[0] & 0xC0) == 0xC0)
435 status = read_exact (c->fd, &word_length[1], 2);
439 req_size = ((buffer[0] & 0x3F) << 16)
443 else if ((buffer[0] & 0x80) == 0x80)
445 status = read_exact (c->fd, &word_length[1], 1);
449 req_size = ((buffer[0] & 0x7F) << 8)
452 else if ((buffer[0] & 0x80) == 0)
454 req_size = (size_t) word_length[0];
458 /* First nibble is `F' but second nibble is not `0'. */
462 if (*buffer_size < req_size)
465 /* Empty word. This ends a `sentence' and must therefore be valid. */
473 status = read_exact (c->fd, buffer, req_size);
476 *buffer_size = req_size;
479 } /* }}} int buffer_decode_next */
481 static mt_reply_t *receive_sentence (mt_connection_t *c) /* {{{ */
495 buffer_size = sizeof (buffer) - 1;
496 status = read_word (c, buffer, &buffer_size);
499 assert (buffer_size < sizeof (buffer));
500 buffer[buffer_size] = 0;
502 /* Empty word means end of reply */
503 if (buffer_size == 0)
506 if (buffer[0] == '!') /* {{{ */
508 if (r->status != NULL)
510 r->status = strdup (&buffer[1]);
511 if (r->status == NULL)
513 } /* }}} if (buffer[0] == '!') */
514 else if (buffer[0] == '=') /* {{{ */
516 char *key = &buffer[1];
520 val = strchr (key, '=');
523 fprintf (stderr, "Ignoring misformed word: %s\n", buffer);
529 reply_add_keyval (r, key, val);
530 } /* }}} if (buffer[0] == '=') */
533 mt_debug ("receive_sentence: Ignoring unknown word: %s\n", buffer);
537 if (r->status == NULL)
544 } /* }}} mt_reply_t *receive_sentence */
546 static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
558 tmp = receive_sentence (c);
573 if (strcmp ("done", tmp->status) == 0)
578 } /* }}} mt_reply_t *receive_reply */
580 static int create_socket (const char *node, const char *service) /* {{{ */
582 struct addrinfo ai_hint;
583 struct addrinfo *ai_list;
584 struct addrinfo *ai_ptr;
587 mt_debug ("create_socket (node = %s, service = %s);\n",
590 memset (&ai_hint, 0, sizeof (ai_hint));
592 ai_hint.ai_flags |= AI_ADDRCONFIG;
594 ai_hint.ai_family = AF_UNSPEC;
595 ai_hint.ai_socktype = SOCK_STREAM;
598 status = getaddrinfo (node, service, &ai_hint, &ai_list);
601 assert (ai_list != NULL);
603 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
608 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
609 ai_ptr->ai_protocol);
612 mt_debug ("create_socket: socket(2) failed.\n");
616 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
619 mt_debug ("create_socket: connect(2) failed.\n");
627 freeaddrinfo (ai_list);
630 } /* }}} int create_socket */
632 static int login2_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
638 printf ("login2_handler has been called.\n");
642 } /* }}} int login2_handler */
644 static void hash_binary_to_hex (char hex[33], uint8_t binary[16]) /* {{{ */
648 for (i = 0; i < 16; i++)
651 snprintf (tmp, 3, "%02"PRIx8, binary[i]);
657 } /* }}} void hash_binary_to_hex */
659 static void hash_hex_to_binary (uint8_t binary[16], char hex[33]) /* {{{ */
663 for (i = 0; i < 16; i++)
668 tmp[1] = hex[2*i + 1];
671 binary[i] = (uint8_t) strtoul (tmp, /* endptr = */ NULL, /* base = */ 16);
673 } /* }}} void hash_hex_to_binary */
675 static void make_password_hash (char response_hex[33], /* {{{ */
676 const char *password, size_t password_length, char challenge_hex[33])
678 uint8_t challenge_bin[16];
679 uint8_t response_bin[16];
680 char data_buffer[password_length+17];
681 gcry_md_hd_t md_handle;
683 hash_hex_to_binary (challenge_bin, challenge_hex);
686 memcpy (&data_buffer[1], password, password_length);
687 memcpy (&data_buffer[1+password_length], challenge_bin, 16);
689 gcry_md_open (&md_handle, GCRY_MD_MD5, /* flags = */ 0);
690 gcry_md_write (md_handle, data_buffer, sizeof (data_buffer));
691 memcpy (response_bin, gcry_md_read (md_handle, GCRY_MD_MD5), 16);
692 gcry_md_close (md_handle);
694 hash_binary_to_hex (response_hex, response_bin);
695 } /* }}} void make_password_hash */
697 static int login_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
701 char challenge_hex[33];
702 char response_hex[33];
703 mt_login_data_t *login_data;
705 const char *params[2];
706 char param_name[1024];
707 char param_response[64];
712 printf ("login_handler has been called.\n");
715 login_data = user_data;
716 if (login_data == NULL)
719 ret = mt_reply_param_val_by_key (r, "ret");
722 mt_debug ("login_handler: Reply does not have parameter \"ret\".\n");
725 mt_debug ("login_handler: ret = %s;\n", ret);
727 if (strlen (ret) != 32)
729 mt_debug ("login_handler: Unexpected length of the \"ret\" argument.\n");
732 strcpy (challenge_hex, ret);
734 make_password_hash (response_hex,
735 login_data->password, strlen (login_data->password),
738 snprintf (param_name, sizeof (param_name), "=name=%s", login_data->username);
739 snprintf (param_response, sizeof (param_response),
740 "=response=00%s", response_hex);
741 params[0] = param_name;
742 params[1] = param_response;
744 return (mt_query (c, "/login", 2, params, login2_handler,
745 /* user data = */ NULL));
746 } /* }}} int login_handler */
751 mt_connection_t *mt_connect (const char *node, const char *service, /* {{{ */
752 const char *username, const char *password)
757 mt_login_data_t user_data;
759 if ((node == NULL) || (username == NULL) || (password == NULL))
762 fd = create_socket (node, (service != NULL) ? service : ROUTEROS_API_PORT);
766 c = malloc (sizeof (*c));
772 memset (c, 0, sizeof (*c));
776 user_data.username = username;
777 user_data.password = password;
778 status = mt_query (c, "/login", /* args num = */ 0, /* args = */ NULL,
779 login_handler, &user_data);
782 } /* }}} mt_connection_t *mt_connect */
784 int mt_disconnect (mt_connection_t *c) /* {{{ */
798 } /* }}} int mt_disconnect */
800 int mt_query (mt_connection_t *c, /* {{{ */
802 size_t args_num, const char * const *args,
803 mt_reply_handler_t handler, void *user_data)
808 status = send_command (c, command, args_num, args);
812 r = receive_reply (c);
816 /* Call the callback function with the data we received. */
817 status = (*handler) (c, r, user_data);
819 /* Free the allocated memory ... */
822 /* ... and return. */
824 } /* }}} int mt_query */
826 const mt_reply_t *mt_reply_next (const mt_reply_t *r) /* {{{ */
832 } /* }}} mt_reply_t *mt_reply_next */
834 int mt_reply_num (const mt_reply_t *r) /* {{{ */
837 const mt_reply_t *ptr;
840 for (ptr = r; ptr != NULL; ptr = ptr->next)
844 } /* }}} int mt_reply_num */
846 const char *mt_reply_status (const mt_reply_t *r) /* {{{ */
851 } /* }}} char *mt_reply_status */
853 const char *mt_reply_param_key_by_index (const mt_reply_t *r, /* {{{ */
859 if (index >= r->params_num)
862 return (r->keys[index]);
863 } /* }}} char *mt_reply_param_key_by_index */
865 const char *mt_reply_param_val_by_index (const mt_reply_t *r, /* {{{ */
871 if (index >= r->params_num)
874 return (r->values[index]);
875 } /* }}} char *mt_reply_param_key_by_index */
877 const char *mt_reply_param_val_by_key (const mt_reply_t *r, /* {{{ */
882 if ((r == NULL) || (key == NULL))
885 for (i = 0; i < r->params_num; i++)
886 if (strcmp (r->keys[i], key) == 0)
887 return (r->values[i]);
890 } /* }}} char *mt_reply_param_val_by_key */
892 /* vim: set ts=2 sw=2 noet fdm=marker : */