Software /
code /
prosody
Annotate
plugins/mod_xmlrpc.lua @ 971:dd736391bd41
Merge
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 08 Apr 2009 23:20:45 +0100 |
parent | 896:2c0b9e3c11c3 |
child | 1523:841d61be198f |
rev | line source |
---|---|
896 | 1 -- Prosody IM v0.4 |
875 | 2 -- Copyright (C) 2008-2009 Matthew Wild |
3 -- Copyright (C) 2008-2009 Waqas Hussain | |
4 -- | |
5 -- This project is MIT/X11 licensed. Please see the | |
6 -- COPYING file in the source package for more information. | |
7 -- | |
8 | |
9 | |
10 module.host = "*" -- Global module | |
11 | |
12 local httpserver = require "net.httpserver"; | |
13 local st = require "util.stanza"; | |
14 local pcall = pcall; | |
15 local unpack = unpack; | |
16 local tostring = tostring; | |
892
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
17 local is_admin = require "core.usermanager".is_admin; |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
18 local jid_split = require "util.jid".split; |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
19 local b64_decode = require "util.encodings".base64.decode; |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
20 local get_method = require "core.objectmanager".get_object; |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
21 local validate_credentials = require "core.usermanager".validate_credentials; |
875 | 22 |
23 local translate_request = require "util.xmlrpc".translate_request; | |
24 local create_response = require "util.xmlrpc".create_response; | |
25 local create_error_response = require "util.xmlrpc".create_error_response; | |
26 | |
27 local entity_map = setmetatable({ | |
28 ["amp"] = "&"; | |
29 ["gt"] = ">"; | |
30 ["lt"] = "<"; | |
31 ["apos"] = "'"; | |
32 ["quot"] = "\""; | |
33 }, {__index = function(_, s) | |
34 if s:sub(1,1) == "#" then | |
35 if s:sub(2,2) == "x" then | |
36 return string.char(tonumber(s:sub(3), 16)); | |
37 else | |
38 return string.char(tonumber(s:sub(2))); | |
39 end | |
40 end | |
41 end | |
42 }); | |
43 local function xml_unescape(str) | |
44 return (str:gsub("&(.-);", entity_map)); | |
45 end | |
46 local function parse_xml(xml) | |
47 local stanza = st.stanza("root"); | |
48 local regexp = "<([^>]*)>([^<]*)"; | |
49 for elem, text in xml:gmatch(regexp) do | |
50 --print("[<"..elem..">|"..text.."]"); | |
51 if elem:sub(1,1) == "!" or elem:sub(1,1) == "?" then -- neglect comments and processing-instructions | |
52 elseif elem:sub(1,1) == "/" then -- end tag | |
53 elem = elem:sub(2); | |
54 stanza:up(); -- TODO check for start-end tag name match | |
55 elseif elem:sub(-1,-1) == "/" then -- empty tag | |
56 elem = elem:sub(1,-2); | |
57 stanza:tag(elem):up(); | |
58 else -- start tag | |
59 stanza:tag(elem); | |
60 end | |
61 if #text ~= 0 then -- text | |
62 stanza:text(xml_unescape(text)); | |
63 end | |
64 end | |
65 return stanza.tags[1]; | |
66 end | |
67 | |
68 local function handle_xmlrpc_request(method, args) | |
69 method = get_method(method); | |
70 if not method then return create_error_response(404, "method not found"); end | |
71 args = args or {}; | |
72 local success, result = pcall(method, unpack(args)); | |
73 if success then | |
74 success, result = pcall(create_response, result or "nil"); | |
75 if success then | |
76 return result; | |
77 end | |
78 return create_error_response(500, "Error in creating response: "..result); | |
79 end | |
80 return create_error_response(0, result or "nil"); | |
81 end | |
82 | |
877
0bababc930dd
mod_xmlrpc: Handle RPC stanzas sent over XMPP (XEP-0009: Jabber-RPC)
Waqas Hussain <waqas20@gmail.com>
parents:
875
diff
changeset
|
83 local function handle_xmpp_request(origin, stanza) |
875 | 84 local query = stanza.tags[1]; |
85 if query.name == "query" then | |
86 if #query.tags == 1 then | |
892
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
87 if is_admin(stanza.attr.from) then |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
88 local success, method, args = pcall(translate_request, query.tags[1]); |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
89 if success then |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
90 local result = handle_xmlrpc_request(method, args); |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
91 origin.send(st.reply(stanza):tag('query', {xmlns='jabber:iq:rpc'}):add_child(result)); |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
92 else |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
93 origin.send(st.error_reply(stanza, "modify", "bad-request", method)); |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
94 end |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
95 else origin.send(st.error_reply(stanza, "auth", "forbidden", "No content in XML-RPC request")); end |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
96 else origin.send(st.error_reply(stanza, "modify", "bad-request", "No content in XML-RPC request")); end |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
97 else origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); end |
875 | 98 end |
877
0bababc930dd
mod_xmlrpc: Handle RPC stanzas sent over XMPP (XEP-0009: Jabber-RPC)
Waqas Hussain <waqas20@gmail.com>
parents:
875
diff
changeset
|
99 module:add_iq_handler({"c2s", "s2sin"}, "jabber:iq:rpc", handle_xmpp_request); |
0bababc930dd
mod_xmlrpc: Handle RPC stanzas sent over XMPP (XEP-0009: Jabber-RPC)
Waqas Hussain <waqas20@gmail.com>
parents:
875
diff
changeset
|
100 module:add_feature("jabber:iq:rpc"); |
892
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
101 -- TODO add <identity category='automation' type='rpc'/> to disco replies |
875 | 102 |
892
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
103 local default_headers = { ['Content-Type'] = 'text/xml' }; |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
104 local unauthorized_response = { status = '401 UNAUTHORIZED', headers = {['Content-Type']='text/html', ['WWW-Authenticate']='Basic realm="WallyWorld"'}; body = "<html><body>Authentication required</body></html>"; }; |
875 | 105 local function handle_http_request(method, body, request) |
892
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
106 -- authenticate user |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
107 local username, password = b64_decode(request['authorization'] or ''):gmatch('([^:]*):(.*)')(); -- TODO digest auth |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
108 local node, host = jid_split(username); |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
109 if not validate_credentials(host, node, password) and is_admin(username) then |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
110 return unauthorized_response; |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
111 end |
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
112 -- parse request |
875 | 113 local stanza = body and parse_xml(body); |
114 if (not stanza) or request.method ~= "POST" then | |
115 return "<html><body>You really don't look like an XML-RPC client to me... what do you want?</body></html>"; | |
116 end | |
892
2128891180b7
mod_xmlrpc: Limit usage to admins
Waqas Hussain <waqas20@gmail.com>
parents:
889
diff
changeset
|
117 -- execute request |
875 | 118 local success, method, args = pcall(translate_request, stanza); |
119 if success then | |
880
ff4a08d73772
XML-RPC: Set appropriate Content-Type header in HTTP response
Waqas Hussain <waqas20@gmail.com>
parents:
877
diff
changeset
|
120 return { headers = default_headers; body = tostring(handle_xmlrpc_request(method, args)) }; |
875 | 121 end |
880
ff4a08d73772
XML-RPC: Set appropriate Content-Type header in HTTP response
Waqas Hussain <waqas20@gmail.com>
parents:
877
diff
changeset
|
122 return "<html><body>Error parsing XML-RPC request: "..tostring(method).."</body></html>"; |
875 | 123 end |
124 httpserver.new{ port = 9000, base = "xmlrpc", handler = handle_http_request } |