/* * xmlrpc.C: New xmlrpc implementation * * 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: xmlrpc.C,v 1.8 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 "xml/rpc.h" static char const rcsid[] = "$Id: xmlrpc.C,v 1.8 2007/09/22 14:27:28 pippijn Exp $"; static void on_config_ready (void); REGISTER_MODULE ("rpc/xmlrpc", false, "The Ermyth Team "); static void handle_request (connection_t *cptr, void *requestbuf); connection_t *current_cptr; /* XXX: Hack: xml/rpc.c requires us to do this */ E pathvec pathhandlers; static void xmlrpc_command_fail (sourceinfo_t *si, fault::code code, char const *message); static void xmlrpc_command_success_nodata (sourceinfo_t *si, char const *message); static void xmlrpc_command_success_string (sourceinfo_t *si, char const *result, char const *message); static int xmlrpcmethod_login (void *conn, int parc, char *parv[]); static int xmlrpcmethod_logout (void *conn, int parc, char *parv[]); static int xmlrpcmethod_command (void *conn, int parc, char *parv[]); /* Configuration */ ConfTable::list_type conf_xmlrpc_table; path_handler_t handle_xmlrpc = { NULL, handle_request }; static int conf_xmlrpc_path (config_entry_t * ce) { if (!ce->valid ()) return -1; if (handle_xmlrpc.path != NULL) { if (!strcmp (handle_xmlrpc.path, ce->vardata ())) return 0; sfree (handle_xmlrpc.path); } handle_xmlrpc.path = sstrdup (ce->vardata ()); return 0; } static int conf_xmlrpc (config_entry_t * ce) { subblock_handler (ce, conf_xmlrpc_table); return 0; } struct sourceinfo_vtable xmlrpc_vtable = { "xmlrpc", xmlrpc_command_fail, xmlrpc_command_success_nodata, xmlrpc_command_success_string }; static char * dump_buffer (char *buf, int length) { httpddata *hd; char buf1[300]; hd = static_cast (current_cptr->userdata); snprintf (buf1, sizeof buf1, "HTTP/1.1 200 OK\r\n" "%s" // connection_close "Server: " PACKAGE_NAME "/%s\r\n" // version "Content-Type: text/xml\r\n" "Content-Length: %d\r\n\r\n", // length hd->connection_close ? "Connection: close\r\n" : "", version, length); sendq_add (current_cptr, buf1, strlen (buf1)); sendq_add (current_cptr, buf, length); if (hd->connection_close) sendq_add_eof (current_cptr); return buf; } static void handle_request (connection_t *cptr, void *requestbuf) { current_cptr = cptr; xmlrpc_process (static_cast (requestbuf), cptr); current_cptr = NULL; return; } static void on_config_ready (void) { if (handle_xmlrpc.handler != NULL) { if (std::find_if (pathhandlers.begin (), pathhandlers.end (), pathhandler_eq (handle_xmlrpc.path)) != pathhandlers.end ()) { slog (LG_INFO, "xmlrpc/main.C: handler already in the list"); return; } pathhandlers.push_back (handle_xmlrpc); } else slog (LG_ERROR, "on_config_ready (): xmlrpc { } block missing or invalid"); } static void xmlrpc_command_fail (sourceinfo_t *si, fault::code code, char const *message) { connection_t *cptr; httpddata *hd; char newmessage[256]; int i; char const *p; cptr = si->connection; hd = static_cast (cptr->userdata); if (hd->sent_reply) return; i = 0, p = message; while (i < 255 && *p != '\0') if (*p > '\0' && *p < ' ') p++; else newmessage[i++] = *p, p++; newmessage[i] = '\0'; xmlrpc_generic_error (code, newmessage); hd->sent_reply = true; } static void xmlrpc_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 xmlrpc_command_success_string (sourceinfo_t *si, char const *result, char const *message) { connection_t *cptr; httpddata *hd; char buf[XMLRPC_BUFSIZE]; cptr = si->connection; hd = static_cast (cptr->userdata); if (hd->sent_reply) return; xmlrpc_string (buf, result); xmlrpc_send (1, buf); hd->sent_reply = true; } /* These taken from the old modules/xmlrpc/account.c */ /* * ermyth.login * * XML Inputs: * account name, password, source ip (optional) * * XML 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 int xmlrpcmethod_login (void *conn, int parc, char *parv[]) { myuser_t *mu; authcookie_t *ac; char buf[BUFSIZE]; char const *sourceip; connection_t *cptr = static_cast (conn); if (parc < 2) { xmlrpc_generic_error (fault::needmoreparams, "Insufficient parameters."); return 0; } sourceip = parc >= 3 && *parv[2] != '\0' ? parv[2] : NULL; if (!(mu = myuser_t::find (parv[0]))) { xmlrpc_generic_error (fault::nosuch_source, "The account is not registered."); return 0; } if (mu->find_metadata ("private:freeze:freezer") != NULL) { logcommand_external (nicksvs.me, "xmlrpc", cptr, sourceip, NULL, CMDLOG_LOGIN, "failed LOGIN to %s (frozen)", mu->name); xmlrpc_generic_error (fault::noprivs, "The account has been frozen."); return 0; } if (!mu->verify_password (parv[1])) { logcommand_external (nicksvs.me, "xmlrpc", cptr, sourceip, NULL, CMDLOG_LOGIN, "failed LOGIN to %s (bad password)", mu->name); xmlrpc_generic_error (fault::authfail, "The password is not valid for this account."); return 0; } mu->lastlogin = NOW; ac = authcookie_create (mu); logcommand_external (nicksvs.me, "xmlrpc", cptr, sourceip, mu, CMDLOG_LOGIN, "LOGIN"); xmlrpc_string (buf, ac->ticket); xmlrpc_send (1, buf); return 0; } /* * ermyth.logout * * XML inputs: * authcookie, and account name. * * XML outputs: * fault 1 - insufficient parameters * fault 3 - unknown user * fault 5 - validation failed * default - success message * * Side Effects: * an authcookie ticket is destroyed. */ static int xmlrpcmethod_logout (void *conn, int parc, char *parv[]) { authcookie_t *ac; myuser_t *mu; char buf[XMLRPC_BUFSIZE]; connection_t *cptr = static_cast (conn); if (parc < 2) { xmlrpc_generic_error (fault::needmoreparams, "Insufficient parameters."); return 0; } if ((mu = myuser_t::find (parv[1])) == NULL) { xmlrpc_generic_error (fault::nosuch_source, "Unknown user."); return 0; } if (authcookie_validate (parv[0], mu) == false) { xmlrpc_generic_error (fault::authfail, "Invalid authcookie for this account."); return 0; } logcommand_external (nicksvs.me, "xmlrpc", cptr, NULL, mu, CMDLOG_LOGIN, "LOGOUT"); ac = authcookie_find (parv[0], mu); authcookie_destroy (ac); xmlrpc_string (buf, "You are now logged out."); xmlrpc_send (1, buf); return 0; } /* * ermyth.command * * XML inputs: * authcookie, account name, source ip, service name, command name, * parameters. * * XML outputs: * depends on command * * Side Effects: * command is executed */ static int xmlrpcmethod_command (void *conn, int parc, char *parv[]) { myuser_t *mu; service_t *svs; command_t const *cmd; sourceinfo_t si; int newparc; char *newparv[20]; connection_t *cptr = static_cast (conn); httpddata *hd = static_cast (cptr->userdata); char buf[XMLRPC_BUFSIZE]; int i; if (parc < 5) { xmlrpc_generic_error (fault::needmoreparams, "Insufficient parameters."); return 0; } for (i = 0; i < parc; i++) { if (strchr (parv[i], '\r') || strchr (parv[i], '\n')) { xmlrpc_generic_error (fault::badparams, "Invalid parameters."); return 0; } } if (*parv[1] != '\0' && strlen (parv[0]) > 1) { if ((mu = myuser_t::find (parv[1])) == NULL) { xmlrpc_generic_error (fault::nosuch_source, "Unknown user."); return 0; } if (authcookie_validate (parv[0], mu) == false) { xmlrpc_generic_error (fault::authfail, "Invalid authcookie for this account."); return 0; } } else mu = NULL; svs = find_service (parv[3]); if (svs == NULL || svs->cmdtree == NULL) { slog (LG_DEBUG, "xmlrpcmethod_command(): invalid service %s", parv[3]); xmlrpc_generic_error (fault::nosuch_source, "Invalid service name."); return 0; } cmd = svs->cmdtree->find (parv[4]); if (cmd == NULL) { xmlrpc_generic_error (fault::nosuch_source, "Invalid command name."); return 0; } memset (newparv, '\0', sizeof newparv); newparc = parc - 5; if (newparc > 20) newparc = 20; 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 = &xmlrpc_vtable; cmd->exec (svs, &si, newparc, newparv); if (!hd->sent_reply) { if (hd->replybuf != NULL) { xmlrpc_string (buf, hd->replybuf); xmlrpc_send (1, buf); } else xmlrpc_generic_error (fault::unimplemented, "Command did not return a result."); } return 0; } bool _modinit (module *m) { ConfTable::callback.ready.attach (on_config_ready); add_top_conf ("XMLRPC", conf_xmlrpc); add_conf_item ("PATH", conf_xmlrpc_table, conf_xmlrpc_path); xmlrpc_set_buffer (dump_buffer); xmlrpc_register_method (PACKAGE_NAME ".login", xmlrpcmethod_login); xmlrpc_register_method (PACKAGE_NAME ".logout", xmlrpcmethod_logout); xmlrpc_register_method (PACKAGE_NAME ".command", xmlrpcmethod_command); return true; } void _moddeinit (void) { xmlrpc_unregister_method (PACKAGE_NAME ".login"); xmlrpc_unregister_method (PACKAGE_NAME ".logout"); xmlrpc_unregister_method (PACKAGE_NAME ".command"); if (std::find_if (pathhandlers.begin (), pathhandlers.end (), pathhandler_eq (handle_xmlrpc.path)) == pathhandlers.end ()) { slog (LG_INFO, "xmlrpc/main.c: handler was not registered."); return; } pathhandlers.erase (std::find_if (pathhandlers.begin (), pathhandlers.end (), pathhandler_eq (handle_xmlrpc.path))); del_conf_item ("PATH", conf_xmlrpc_table); del_top_conf ("XMLRPC"); if (handle_xmlrpc.path != NULL) sfree (handle_xmlrpc.path); handle_xmlrpc.path = NULL; ConfTable::callback.ready.detach (on_config_ready); }