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 status = buffer_add (&buffer_ptr, &buffer_size, command);
355 for (i = 0; i < args_num; i++)
360 status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
365 status = buffer_end (&buffer_ptr, &buffer_size);
370 buffer_size = sizeof (buffer) - buffer_size;
371 while (buffer_size > 0)
373 ssize_t bytes_written;
376 bytes_written = write (c->fd, buffer_ptr, buffer_size);
377 if (bytes_written < 0)
384 assert (bytes_written <= buffer_size);
386 buffer_ptr += bytes_written;
387 buffer_size -= bytes_written;
388 } /* while (buffer_size > 0) */
391 } /* }}} int send_command */
393 static int read_word (mt_connection_t *c, /* {{{ */
394 char *buffer, size_t *buffer_size)
397 uint8_t word_length[5];
400 if ((buffer == NULL) || (*buffer_size < 1))
403 /* read one byte from the socket */
404 status = read_exact (c->fd, word_length, 1);
408 /* Calculate `req_size' */
409 if (((unsigned char) buffer[0]) == 0xF0) /* {{{ */
411 status = read_exact (c->fd, &word_length[1], 4);
415 req_size = (buffer[1] << 24)
420 else if ((buffer[0] & 0xE0) == 0xE0)
422 status = read_exact (c->fd, &word_length[1], 3);
426 req_size = ((buffer[0] & 0x1F) << 24)
431 else if ((buffer[0] & 0xC0) == 0xC0)
433 status = read_exact (c->fd, &word_length[1], 2);
437 req_size = ((buffer[0] & 0x3F) << 16)
441 else if ((buffer[0] & 0x80) == 0x80)
443 status = read_exact (c->fd, &word_length[1], 1);
447 req_size = ((buffer[0] & 0x7F) << 8)
450 else if ((buffer[0] & 0x80) == 0)
452 req_size = (size_t) word_length[0];
456 /* First nibble is `F' but second nibble is not `0'. */
460 if (*buffer_size < req_size)
463 /* Empty word. This ends a `sentence' and must therefore be valid. */
471 status = read_exact (c->fd, buffer, req_size);
474 *buffer_size = req_size;
477 } /* }}} int buffer_decode_next */
479 static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
493 buffer_size = sizeof (buffer) - 1;
494 status = read_word (c, buffer, &buffer_size);
497 assert (buffer_size < sizeof (buffer));
498 buffer[buffer_size] = 0;
500 /* Empty word means end of reply */
501 if (buffer_size == 0)
504 if (buffer[0] == '!') /* {{{ */
508 tmp = reply_alloc ();
515 tmp->status = strdup (&buffer[1]);
516 if (tmp->status == NULL)
533 } /* }}} if (buffer[0] == '!') */
534 else if (buffer[0] == '=') /* {{{ */
536 char *key = &buffer[1];
540 val = strchr (key, '=');
543 fprintf (stderr, "Ignoring misformed word: %s\n", buffer);
549 reply_add_keyval (tail, key, val);
550 } /* }}} if (buffer[0] == '=') */
553 printf ("Ignoring unknown word: %s\n", buffer);
564 } /* }}} mt_reply_t *receive_reply */
566 static int create_socket (const char *node, const char *service) /* {{{ */
568 struct addrinfo ai_hint;
569 struct addrinfo *ai_list;
570 struct addrinfo *ai_ptr;
573 mt_debug ("create_socket (node = %s, service = %s);\n",
576 memset (&ai_hint, 0, sizeof (ai_hint));
578 ai_hint.ai_flags |= AI_ADDRCONFIG;
580 ai_hint.ai_family = AF_UNSPEC;
581 ai_hint.ai_socktype = SOCK_STREAM;
584 status = getaddrinfo (node, service, &ai_hint, &ai_list);
587 assert (ai_list != NULL);
589 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
594 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
595 ai_ptr->ai_protocol);
598 mt_debug ("create_socket: socket(2) failed.\n");
602 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
605 mt_debug ("create_socket: connect(2) failed.\n");
613 freeaddrinfo (ai_list);
616 } /* }}} int create_socket */
618 static int login2_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
624 printf ("login2_handler has been called.\n");
628 } /* }}} int login2_handler */
630 static void hash_binary_to_hex (char hex[33], uint8_t binary[16]) /* {{{ */
634 for (i = 0; i < 16; i++)
637 snprintf (tmp, 3, "%02"PRIx8, binary[i]);
643 } /* }}} void hash_binary_to_hex */
645 static void hash_hex_to_binary (uint8_t binary[16], char hex[33]) /* {{{ */
649 for (i = 0; i < 16; i++)
654 tmp[1] = hex[2*i + 1];
657 binary[i] = (uint8_t) strtoul (tmp, /* endptr = */ NULL, /* base = */ 16);
659 } /* }}} void hash_hex_to_binary */
661 static void make_password_hash (char response_hex[33], /* {{{ */
662 const char *password, size_t password_length, char challenge_hex[33])
664 uint8_t challenge_bin[16];
665 uint8_t response_bin[16];
666 char data_buffer[password_length+17];
667 gcry_md_hd_t md_handle;
669 hash_hex_to_binary (challenge_bin, challenge_hex);
672 memcpy (&data_buffer[1], password, password_length);
673 memcpy (&data_buffer[1+password_length], challenge_bin, 16);
675 gcry_md_open (&md_handle, GCRY_MD_MD5, /* flags = */ 0);
676 gcry_md_write (md_handle, data_buffer, sizeof (data_buffer));
677 memcpy (response_bin, gcry_md_read (md_handle, GCRY_MD_MD5), 16);
678 gcry_md_close (md_handle);
680 hash_binary_to_hex (response_hex, response_bin);
681 } /* }}} void make_password_hash */
683 static int login_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
687 char challenge_hex[33];
688 char response_hex[33];
689 mt_login_data_t *login_data;
691 const char *params[2];
692 char param_name[1024];
693 char param_response[64];
698 printf ("login_handler has been called.\n");
701 login_data = user_data;
702 if (login_data == NULL)
705 ret = mt_reply_param_val_by_key (r, "ret");
708 mt_debug ("login_handler: Reply does not have parameter \"ret\".\n");
711 mt_debug ("login_handler: ret = %s;\n", ret);
713 if (strlen (ret) != 32)
715 mt_debug ("login_handler: Unexpected length of the \"ret\" argument.\n");
718 strcpy (challenge_hex, ret);
720 make_password_hash (response_hex,
721 login_data->password, strlen (login_data->password),
724 snprintf (param_name, sizeof (param_name), "=name=%s", login_data->username);
725 snprintf (param_response, sizeof (param_response),
726 "=response=00%s", response_hex);
727 params[0] = param_name;
728 params[1] = param_response;
730 return (mt_query (c, "/login", 2, params, login2_handler,
731 /* user data = */ NULL));
732 } /* }}} int login_handler */
737 mt_connection_t *mt_connect (const char *node, const char *service, /* {{{ */
738 const char *username, const char *password)
743 mt_login_data_t user_data;
745 if ((node == NULL) || (username == NULL) || (password == NULL))
748 fd = create_socket (node, (service != NULL) ? service : ROUTEROS_API_PORT);
752 c = malloc (sizeof (*c));
758 memset (c, 0, sizeof (*c));
762 user_data.username = username;
763 user_data.password = password;
764 status = mt_query (c, "/login", /* args num = */ 0, /* args = */ NULL,
765 login_handler, &user_data);
768 } /* }}} mt_connection_t *mt_connect */
770 int mt_disconnect (mt_connection_t *c) /* {{{ */
784 } /* }}} int mt_disconnect */
786 int mt_query (mt_connection_t *c, /* {{{ */
788 size_t args_num, const char * const *args,
789 mt_reply_handler_t handler, void *user_data)
794 status = send_command (c, command, args_num, args);
798 r = receive_reply (c);
802 /* Call the callback function with the data we received. */
803 status = (*handler) (c, r, user_data);
805 /* Free the allocated memory ... */
808 /* ... and return. */
810 } /* }}} int mt_query */
812 const mt_reply_t *mt_reply_next (const mt_reply_t *r) /* {{{ */
818 } /* }}} mt_reply_t *mt_reply_next */
820 int mt_reply_num (const mt_reply_t *r) /* {{{ */
823 const mt_reply_t *ptr;
826 for (ptr = r; ptr != NULL; ptr = ptr->next)
830 } /* }}} int mt_reply_num */
832 const char *mt_reply_status (const mt_reply_t *r) /* {{{ */
837 } /* }}} char *mt_reply_status */
839 const char *mt_reply_param_key_by_index (const mt_reply_t *r, /* {{{ */
845 if (index >= r->params_num)
848 return (r->keys[index]);
849 } /* }}} char *mt_reply_param_key_by_index */
851 const char *mt_reply_param_val_by_index (const mt_reply_t *r, /* {{{ */
857 if (index >= r->params_num)
860 return (r->values[index]);
861 } /* }}} char *mt_reply_param_key_by_index */
863 const char *mt_reply_param_val_by_key (const mt_reply_t *r, /* {{{ */
868 if ((r == NULL) || (key == NULL))
871 for (i = 0; i < r->params_num; i++)
872 if (strcmp (r->keys[i], key) == 0)
873 return (r->values[i]);
876 } /* }}} char *mt_reply_param_val_by_key */
878 /* vim: set ts=2 sw=2 noet fdm=marker : */