+static int login2_handler (__attribute__((unused)) ros_connection_t *c, /* {{{ */
+ const ros_reply_t *r,
+ __attribute__((unused)) void *user_data)
+{
+ if (r == NULL)
+ return (EINVAL);
+
+ reply_dump (r);
+
+ if (strcmp (r->status, "trap") == 0)
+ {
+ ros_debug ("login2_handler: Logging in failed: %s.\n",
+ ros_reply_param_val_by_key (r, "message"));
+ return (EACCES);
+ }
+ else if (strcmp (r->status, "done") != 0)
+ {
+ ros_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 (ros_connection_t *c, const ros_reply_t *r, /* {{{ */
+ void *user_data)
+{
+ const char *ret;
+ char challenge_hex[33];
+ char response_hex[33];
+ ros_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 --
+ */
+ reply_dump (r);
+
+ if (strcmp (r->status, "done") != 0)
+ {
+ ros_debug ("login_handler: Unexpected status: %s.\n", r->status);
+ return (EPROTO);
+ }
+
+ login_data = user_data;
+ if (login_data == NULL)
+ return (EINVAL);
+
+ ret = ros_reply_param_val_by_key (r, "ret");
+ if (ret == NULL)
+ {
+ ros_debug ("login_handler: Reply does not have parameter \"ret\".\n");
+ return (EPROTO);
+ }
+ ros_debug ("login_handler: ret = %s;\n", ret);
+
+ if (strlen (ret) != 32)
+ {
+ ros_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 (ros_query (c, "/login", 2, params, login2_handler,
+ /* user data = */ NULL));
+} /* }}} int login_handler */
+