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 ros_debug(...) fprintf (stdout, __VA_ARGS__)
50 # define ros_debug(...) /**/
54 char *strdup (const char *);
59 struct ros_connection_s
66 unsigned int params_num;
74 struct ros_login_data_s
79 typedef struct ros_login_data_s ros_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 ros_reply_t *reply_alloc (void) /* {{{ */
125 r = malloc (sizeof (*r));
129 memset (r, 0, sizeof (*r));
135 } /* }}} ros_reply_s *reply_alloc */
137 static int reply_add_keyval (ros_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 ros_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 (ros_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 (ros_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 ros_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 ros_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 (ros_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) word_length[0]) == 0xF0) /* {{{ */
413 status = read_exact (c->fd, &word_length[1], 4);
417 req_size = (word_length[1] << 24)
418 | (word_length[2] << 16)
419 | (word_length[3] << 8)
422 else if ((word_length[0] & 0xE0) == 0xE0)
424 status = read_exact (c->fd, &word_length[1], 3);
428 req_size = ((word_length[0] & 0x1F) << 24)
429 | (word_length[1] << 16)
430 | (word_length[2] << 8)
433 else if ((word_length[0] & 0xC0) == 0xC0)
435 status = read_exact (c->fd, &word_length[1], 2);
439 req_size = ((word_length[0] & 0x3F) << 16)
440 | (word_length[1] << 8)
443 else if ((word_length[0] & 0x80) == 0x80)
445 status = read_exact (c->fd, &word_length[1], 1);
449 req_size = ((word_length[0] & 0x7F) << 8)
452 else if ((word_length[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 ros_reply_t *receive_sentence (ros_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 ros_debug ("receive_sentence: Ignoring unknown word: %s\n", buffer);
537 if (r->status == NULL)
544 } /* }}} ros_reply_t *receive_sentence */
546 static ros_reply_t *receive_reply (ros_connection_t *c) /* {{{ */
558 tmp = receive_sentence (c);
573 if (strcmp ("done", tmp->status) == 0)
578 } /* }}} ros_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 ros_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 ros_debug ("create_socket: socket(2) failed.\n");
616 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
619 ros_debug ("create_socket: connect(2) failed.\n");
627 freeaddrinfo (ai_list);
630 } /* }}} int create_socket */
632 static int login2_handler (ros_connection_t *c, const ros_reply_t *r, /* {{{ */
638 printf ("login2_handler has been called.\n");
641 if (strcmp (r->status, "done") != 0)
643 ros_debug ("login2_handler: Unexpected status: %s.\n", r->status);
648 } /* }}} int login2_handler */
650 static void hash_binary_to_hex (char hex[33], uint8_t binary[16]) /* {{{ */
654 for (i = 0; i < 16; i++)
657 snprintf (tmp, 3, "%02"PRIx8, binary[i]);
663 } /* }}} void hash_binary_to_hex */
665 static void hash_hex_to_binary (uint8_t binary[16], char hex[33]) /* {{{ */
669 for (i = 0; i < 16; i++)
674 tmp[1] = hex[2*i + 1];
677 binary[i] = (uint8_t) strtoul (tmp, /* endptr = */ NULL, /* base = */ 16);
679 } /* }}} void hash_hex_to_binary */
681 static void make_password_hash (char response_hex[33], /* {{{ */
682 const char *password, size_t password_length, char challenge_hex[33])
684 uint8_t challenge_bin[16];
685 uint8_t response_bin[16];
686 char data_buffer[password_length+17];
687 gcry_md_hd_t md_handle;
689 hash_hex_to_binary (challenge_bin, challenge_hex);
692 memcpy (&data_buffer[1], password, password_length);
693 memcpy (&data_buffer[1+password_length], challenge_bin, 16);
695 gcry_md_open (&md_handle, GCRY_MD_MD5, /* flags = */ 0);
696 gcry_md_write (md_handle, data_buffer, sizeof (data_buffer));
697 memcpy (response_bin, gcry_md_read (md_handle, GCRY_MD_MD5), 16);
698 gcry_md_close (md_handle);
700 hash_binary_to_hex (response_hex, response_bin);
701 } /* }}} void make_password_hash */
703 static int login_handler (ros_connection_t *c, const ros_reply_t *r, /* {{{ */
707 char challenge_hex[33];
708 char response_hex[33];
709 ros_login_data_t *login_data;
711 const char *params[2];
712 char param_name[1024];
713 char param_response[64];
718 /* The expected result looks like this:
721 * =ret=ebddd18303a54111e2dea05a92ab46b4
725 printf ("login_handler has been called.\n");
728 if (strcmp (r->status, "done") != 0)
730 ros_debug ("login_handler: Unexpected status: %s.\n", r->status);
734 login_data = user_data;
735 if (login_data == NULL)
738 ret = ros_reply_param_val_by_key (r, "ret");
741 ros_debug ("login_handler: Reply does not have parameter \"ret\".\n");
744 ros_debug ("login_handler: ret = %s;\n", ret);
746 if (strlen (ret) != 32)
748 ros_debug ("login_handler: Unexpected length of the \"ret\" argument.\n");
751 strcpy (challenge_hex, ret);
753 make_password_hash (response_hex,
754 login_data->password, strlen (login_data->password),
757 snprintf (param_name, sizeof (param_name), "=name=%s", login_data->username);
758 snprintf (param_response, sizeof (param_response),
759 "=response=00%s", response_hex);
760 params[0] = param_name;
761 params[1] = param_response;
763 return (ros_query (c, "/login", 2, params, login2_handler,
764 /* user data = */ NULL));
765 } /* }}} int login_handler */
770 ros_connection_t *ros_connect (const char *node, const char *service, /* {{{ */
771 const char *username, const char *password)
776 ros_login_data_t user_data;
778 if ((node == NULL) || (username == NULL) || (password == NULL))
781 fd = create_socket (node, (service != NULL) ? service : ROUTEROS_API_PORT);
785 c = malloc (sizeof (*c));
791 memset (c, 0, sizeof (*c));
795 user_data.username = username;
796 user_data.password = password;
797 status = ros_query (c, "/login", /* args num = */ 0, /* args = */ NULL,
798 login_handler, &user_data);
801 } /* }}} ros_connection_t *ros_connect */
803 int ros_disconnect (ros_connection_t *c) /* {{{ */
817 } /* }}} int ros_disconnect */
819 int ros_query (ros_connection_t *c, /* {{{ */
821 size_t args_num, const char * const *args,
822 ros_reply_handler_t handler, void *user_data)
827 status = send_command (c, command, args_num, args);
831 r = receive_reply (c);
835 /* Call the callback function with the data we received. */
836 status = (*handler) (c, r, user_data);
838 /* Free the allocated memory ... */
841 /* ... and return. */
843 } /* }}} int ros_query */
845 const ros_reply_t *ros_reply_next (const ros_reply_t *r) /* {{{ */
851 } /* }}} ros_reply_t *ros_reply_next */
853 int ros_reply_num (const ros_reply_t *r) /* {{{ */
856 const ros_reply_t *ptr;
859 for (ptr = r; ptr != NULL; ptr = ptr->next)
863 } /* }}} int ros_reply_num */
865 const char *ros_reply_status (const ros_reply_t *r) /* {{{ */
870 } /* }}} char *ros_reply_status */
872 const char *ros_reply_param_key_by_index (const ros_reply_t *r, /* {{{ */
878 if (index >= r->params_num)
881 return (r->keys[index]);
882 } /* }}} char *ros_reply_param_key_by_index */
884 const char *ros_reply_param_val_by_index (const ros_reply_t *r, /* {{{ */
890 if (index >= r->params_num)
893 return (r->values[index]);
894 } /* }}} char *ros_reply_param_key_by_index */
896 const char *ros_reply_param_val_by_key (const ros_reply_t *r, /* {{{ */
901 if ((r == NULL) || (key == NULL))
904 for (i = 0; i < r->params_num; i++)
905 if (strcmp (r->keys[i], key) == 0)
906 return (r->values[i]);
909 } /* }}} char *ros_reply_param_val_by_key */
911 /* vim: set ts=2 sw=2 noet fdm=marker : */