/** * jsonrpc.C: JSON-RPC for Ermyth * * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team * Rights to this code are as documented in COPYING. * * * Portions of this file were derived from sources bearing the following license: * Copyright © 2005-2006 Jilles Tjoelker et al. * Rights to this code are as documented in doc/pod/license.pod. * * $Id: jsonrpc.C,v 1.10 2007/09/22 14:27:28 pippijn Exp $ */ #include "atheme.h" #include #include #include #include "datastream.h" #include "authcookie.h" #include "connection.h" #include "confparse.h" #include "httpd.h" #include "json/json.h" #include "json/rpc.h" using namespace json; static char const rcsid[] = "$Id: jsonrpc.C,v 1.10 2007/09/22 14:27:28 pippijn Exp $"; static void on_config_ready (void); #define JSONRPC_BUFSIZE 2048 #define MAX_CMD_PARC 20 REGISTER_MODULE ("rpc/jsonrpc", false, "The Ermyth Team "); static void handle_request (connection_t *cptr, void *requestbuf); Value current_response; // XXX: Hack E pathvec pathhandlers; static void jsonrpc_command_fail (sourceinfo_t *si, fault::code code, char const *message); static void jsonrpc_command_success_nodata (sourceinfo_t *si, char const *message); static void jsonrpc_command_success_string (sourceinfo_t *si, char const *result, char const *message); static void jsonrpcmethod_login (void *conn, Value &request, Value &response); static void jsonrpcmethod_logout (void *conn, Value &request, Value &response); static void jsonrpcmethod_command (void *conn, Value &request, Value &response); /* Configuration */ ConfTable::list_type conf_jsonrpc_table; path_handler_t handle_jsonrpc = { NULL, handle_request }; static int conf_jsonrpc_path (config_entry_t *ce) { if (!ce->valid ()) return -1; if (handle_jsonrpc.path != NULL) { if (!strcmp (handle_jsonrpc.path, ce->vardata ())) return 0; sfree (handle_jsonrpc.path); } handle_jsonrpc.path = sstrdup (ce->vardata ()); return 0; } static int conf_jsonrpc (config_entry_t *ce) { subblock_handler (ce, conf_jsonrpc_table); return 0; } struct sourceinfo_vtable jsonrpc_vtable = { "jsonrpc", jsonrpc_command_fail, jsonrpc_command_success_nodata, jsonrpc_command_success_string }; static char * dump_buffer (connection_t *cptr, char *buf, int length) { struct httpddata *hd; char buf1[300]; hd = static_cast (cptr->userdata); snprintf (buf1, sizeof buf1, "HTTP/1.1 200 OK\r\n" "%s" // connection_close? "Server: " PACKAGE_NAME "/%s\r\n" "Content-Type: application/json\r\n" "Content-Length: %d\r\n\r\n", hd->connection_close ? "Connection: close\r\n" : "", version, length); sendq_add (cptr, buf1, strlen (buf1)); sendq_add (cptr, buf, length); if (hd->connection_close) sendq_add_eof (cptr); return buf; } void jsonrpc_generic_error (Value &response, int code, char const *string) { Value error (objectValue); error["name"] = "JSONRPCError"; error["code"] = Value (code); error["message"] = Value (string); response["version"] = "1.1"; response["error"] = error; } static void handle_request (connection_t *cptr, void *requestbuf) { char response[JSONRPC_BUFSIZE]; jsonrpc_process (&cptr, response, static_cast (requestbuf), JSONRPC_BUFSIZE); dump_buffer (cptr, response, strlen (response)); current_response = Value (nullValue); } static void on_config_ready (void) { if (handle_jsonrpc.handler != NULL) { if (std::find_if (pathhandlers.begin (), pathhandlers.end (), pathhandler_eq (handle_jsonrpc.path)) != pathhandlers.end ()) { slog (LG_INFO, "jsonrpc/main.C: handler already in the list"); return; } pathhandlers.push_back (handle_jsonrpc); } else slog (LG_ERROR, "on_config_ready (): jsonrpc { } block missing or invalid"); } static void jsonrpc_generic_reply (Value &response, char const *message) { Value entry (objectValue); entry["result"] = Value (message); response["result"] = entry; } static void jsonrpc_command_fail (sourceinfo_t *si, fault::code code, char const *message) { connection_t *cptr; struct httpddata *hd; cptr = si->connection; hd = static_cast (cptr->userdata); if (hd->sent_reply) return; jsonrpc_generic_error (current_response, code, message); hd->sent_reply = true; } static void jsonrpc_command_success_nodata (sourceinfo_t *si, char const *message) { char *p; char const *q; size_t msglen; connection_t *cptr; httpddata *hd; cptr = si->connection; hd = static_cast (cptr->userdata); msglen = strlen (message); if (hd->sent_reply) return; if (hd->replybuf == NULL) { hd->replybuf = salloc (msglen + 1); p = hd->replybuf; } else { size_t replybuf_len = strlen (hd->replybuf); hd->replybuf = salloc (replybuf_len + msglen + 2, hd->replybuf); p = hd->replybuf + replybuf_len; *p++ = '\n'; } q = message; /* What this code does is the following: * - set *p to *q * - increment q (go to the next character in q * - if *p is below 0x1f (the space character ' '), then * do not increment p * if *p is above or the same as the space character, then * do increment p * * This incrementing/not incrementing effectively skips any character below the * space character, as by not incrementing p, *p gets overwritten in the next cycle. */ while (msglen--) { *p = *q++; p += !!(*p & ~0x1f); } *p = '\0'; } static void jsonrpc_command_success_string (sourceinfo_t *si, char const *result, char const *message) { connection_t *cptr; struct httpddata *hd; cptr = si->connection; hd = static_cast (cptr->userdata); if (hd->sent_reply) return; jsonrpc_generic_reply (current_response, result); hd->sent_reply = true; } /* These taken from the old modules/jsonrpc/account.c */ /* * ermyth.login * * JSON Inputs: * account name, password, source ip (optional) * * JSON Outputs: * fault 1 - insufficient parameters * fault 3 - account is not registered * fault 5 - invalid username and password * fault 6 - account is frozen * default - success (authcookie) * * Side Effects: * an authcookie ticket is created for the myuser_t. * the user's lastlogin is updated */ static void jsonrpcmethod_login (void *conn, Value &request, Value &response) { myuser_t *mu; authcookie_t *ac; char const *sourceip; Value params = request["params"]; int parc, i; char **parv = salloc (MAX_CMD_PARC); connection_t *cptr = static_cast (conn); parc = params.size (); if (parc < 2) { jsonrpc_generic_error (response, fault::needmoreparams, "Insufficient parameters."); sfree (parv); return; } for (i = 0; i < parc; i++) parv[i] = const_cast (static_cast ((params[i]))); sourceip = parc >= 3 && *parv[2] != '\0' ? parv[2] : NULL; if (! (mu = myuser_t::find (parv[0]))) { jsonrpc_generic_error (response, fault::nosuch_source, "The account is not registered."); sfree (parv); return; } if (mu->find_metadata ("private:freeze:freezer") != NULL) { logcommand_external (nicksvs.me, "jsonrpc", cptr, sourceip, NULL, CMDLOG_LOGIN, "failed LOGIN to %s (frozen)", mu->name); jsonrpc_generic_error (response, fault::noprivs, "The account has been frozen."); sfree (parv); return; } if (!mu->verify_password (parv[1])) { logcommand_external (nicksvs.me, "jsonrpc", cptr, sourceip, NULL, CMDLOG_LOGIN, "failed LOGIN to %s (bad password)", mu->name); jsonrpc_generic_error (response, fault::authfail, "The password is not valid for this account."); sfree (parv); return; } mu->lastlogin = NOW; ac = authcookie_create (mu); logcommand_external (nicksvs.me, "jsonrpc", cptr, sourceip, mu, CMDLOG_LOGIN, "LOGIN"); jsonrpc_generic_reply (response, ac->ticket); sfree (parv); } /* * ermyth.logout * * JSON inputs: * authcookie, and account name. * * JSON outputs: * fault 1 - insufficient parameters * fault 3 - unknown user * fault 5 - validation failed * default - success message * * Side Effects: * an authcookie ticket is destroyed. */ static void jsonrpcmethod_logout (void *conn, Value &request, Value &response) { authcookie_t *ac; myuser_t *mu; Value params = request["params"]; int parc, i; char **parv = salloc (MAX_CMD_PARC); connection_t *cptr = static_cast (conn); parc = params.size (); if (parc < 2) { jsonrpc_generic_error (response, fault::needmoreparams, "Insufficient parameters."); sfree (parv); return; } for (i = 0; i < parc; i++) parv[i] = const_cast (static_cast ((params[i]))); if ( (mu = myuser_t::find (parv[1])) == NULL) { jsonrpc_generic_error (response, fault::nosuch_source, "Unknown user."); sfree (parv); return; } if (authcookie_validate (parv[0], mu) == false) { jsonrpc_generic_error (response, fault::authfail, "Invalid authcookie for this account."); sfree (parv); return; } logcommand_external (nicksvs.me, "jsonrpc", cptr, NULL, mu, CMDLOG_LOGIN, "LOGOUT"); ac = authcookie_find (parv[0], mu); authcookie_destroy (ac); jsonrpc_generic_reply (response, "You are now logged out."); sfree (parv); } /* * ermyth.command * * JSON inputs: * authcookie, account name, source ip, service name, command name, * parameters. * * JSON outputs: * depends on command * * Side Effects: * command is executed */ static void jsonrpcmethod_command (void *conn, Value &request, Value &response) { myuser_t *mu; service_t *svs; command_t const *cmd; sourceinfo_t si; int newparc; char *newparv[MAX_CMD_PARC]; connection_t *cptr = static_cast (conn); struct httpddata *hd = static_cast (cptr->userdata); Value params = request["params"]; int parc, i; char **parv = salloc (MAX_CMD_PARC); parc = params.size (); if (parc < 5) { jsonrpc_generic_error (response, fault::needmoreparams, "Insufficient parameters."); sfree (parv); return; } for (i = 0; i < parc; i++) parv[i] = const_cast (static_cast (params[i])); for (i = 0; i < parc; i++) { if (strchr (parv[i], '\r') || strchr (parv[i], '\n')) { jsonrpc_generic_error (response, fault::badparams, "Invalid parameters."); return; } } if (*parv[1] != '\0' && strlen (parv[0]) > 1) { if ( (mu = myuser_t::find (parv[1])) == NULL) { jsonrpc_generic_error (response, fault::nosuch_source, "Unknown user."); sfree (parv); return; } if (authcookie_validate (parv[0], mu) == false) { jsonrpc_generic_error (response, fault::authfail, "Invalid authcookie for this account."); sfree (parv); return; } } else mu = NULL; svs = find_service (parv[3]); if (svs == NULL || svs->cmdtree == NULL) { slog (LG_DEBUG, "jsonrpcmethod_command (): invalid service %s", parv[3]); jsonrpc_generic_error (response, fault::nosuch_source, "Invalid service name."); sfree (parv); return; } cmd = svs->cmdtree->find (parv[4]); if (cmd == NULL) { jsonrpc_generic_error (response, fault::nosuch_source, "Invalid command name."); sfree (parv); return; } memset (newparv, '\0', sizeof newparv); newparc = parc - 5; if (newparc > MAX_CMD_PARC) newparc = MAX_CMD_PARC; if (newparc > 0) memcpy (newparv, parv + 5, newparc * sizeof (parv[0])); memset (&si, '\0', sizeof si); si.smu = mu; si.service = svs; si.sourcedesc = parv[2][0] != '\0' ? parv[2] : NULL; si.connection = cptr; si.v = &jsonrpc_vtable; cmd->exec (svs, &si, newparc, newparv); response = current_response; if (!hd->sent_reply) { if (hd->replybuf != NULL) jsonrpc_generic_reply (response, hd->replybuf); else jsonrpc_generic_error (response, fault::unimplemented, "Command did not return a result."); } sfree (parv); } bool _modinit (module *m) { ConfTable::callback.ready.attach (on_config_ready); add_top_conf ("JSONRPC", conf_jsonrpc); add_conf_item ("PATH", conf_jsonrpc_table, conf_jsonrpc_path); jsonrpc_add_method (PACKAGE_NAME ".login", jsonrpcmethod_login); jsonrpc_add_method (PACKAGE_NAME ".logout", jsonrpcmethod_logout); jsonrpc_add_method (PACKAGE_NAME ".command", jsonrpcmethod_command); return true; } void _moddeinit (void) { #if 0 jsonrpc_unregister_method (PACKAGE_NAME ".login"); jsonrpc_unregister_method (PACKAGE_NAME ".logout"); jsonrpc_unregister_method (PACKAGE_NAME ".command"); #endif if (std::find_if (pathhandlers.begin (), pathhandlers.end (), pathhandler_eq (handle_jsonrpc.path)) == pathhandlers.end ()) { slog (LG_INFO, "jsonrpc/main.c: handler was not registered."); return; } pathhandlers.erase (std::find_if (pathhandlers.begin (), pathhandlers.end (), pathhandler_eq (handle_jsonrpc.path))); del_conf_item ("PATH", conf_jsonrpc_table); del_top_conf ("JSONRPC"); if (handle_jsonrpc.path != NULL) sfree (handle_jsonrpc.path); handle_jsonrpc.path = NULL; ConfTable::callback.ready.detach (on_config_ready); }