You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mod_external_services.lua 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. local dt = require "util.datetime";
  2. local base64 = require "util.encodings".base64;
  3. local hashes = require "util.hashes";
  4. local st = require "util.stanza";
  5. local jid = require "util.jid";
  6. local array = require "util.array";
  7. local default_host = module:get_option_string("external_service_host", module.host);
  8. local default_port = module:get_option_number("external_service_port");
  9. local default_secret = module:get_option_string("external_service_secret");
  10. local default_ttl = module:get_option_number("external_service_ttl", 86400);
  11. local configured_services = module:get_option_array("external_services", {});
  12. local access = module:get_option_set("external_service_access", {});
  13. -- https://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
  14. local function behave_turn_rest_credentials(srv, item, secret)
  15. local ttl = default_ttl;
  16. if type(item.ttl) == "number" then
  17. ttl = item.ttl;
  18. end
  19. local expires = srv.expires or os.time() + ttl;
  20. local username;
  21. if type(item.username) == "string" then
  22. username = string.format("%d:%s", expires, item.username);
  23. else
  24. username = string.format("%d", expires);
  25. end
  26. srv.username = username;
  27. srv.password = base64.encode(hashes.hmac_sha1(secret, srv.username));
  28. end
  29. local algorithms = {
  30. turn = behave_turn_rest_credentials;
  31. }
  32. -- filter config into well-defined service records
  33. local function prepare(item)
  34. if type(item) ~= "table" then
  35. module:log("error", "Service definition is not a table: %q", item);
  36. return nil;
  37. end
  38. local srv = {
  39. type = nil;
  40. transport = nil;
  41. host = default_host;
  42. port = default_port;
  43. username = nil;
  44. password = nil;
  45. restricted = nil;
  46. expires = nil;
  47. };
  48. if type(item.type) == "string" then
  49. srv.type = item.type;
  50. else
  51. module:log("error", "Service missing mandatory 'type' field: %q", item);
  52. return nil;
  53. end
  54. if type(item.transport) == "string" then
  55. srv.transport = item.transport;
  56. end
  57. if type(item.host) == "string" then
  58. srv.host = item.host;
  59. end
  60. if type(item.port) == "number" then
  61. srv.port = item.port;
  62. end
  63. if type(item.username) == "string" then
  64. srv.username = item.username;
  65. end
  66. if type(item.password) == "string" then
  67. srv.password = item.password;
  68. srv.restricted = true;
  69. end
  70. if item.restricted == true then
  71. srv.restricted = true;
  72. end
  73. if type(item.expires) == "number" then
  74. srv.expires = item.expires;
  75. elseif type(item.ttl) == "number" then
  76. srv.expires = os.time() + item.ttl;
  77. end
  78. if (item.secret == true and default_secret) or type(item.secret) == "string" then
  79. local secret_cb = item.credentials_cb or algorithms[item.algorithm] or algorithms[srv.type];
  80. local secret = item.secret;
  81. if secret == true then
  82. secret = default_secret;
  83. end
  84. if secret_cb then
  85. secret_cb(srv, item, secret);
  86. srv.restricted = true;
  87. end
  88. end
  89. return srv;
  90. end
  91. function module.load()
  92. -- Trigger errors on startup
  93. local services = configured_services / prepare;
  94. if #services == 0 then
  95. module:log("warn", "No services configured or all had errors");
  96. end
  97. end
  98. -- Ensure only valid items are added in events
  99. local services_mt = {
  100. __index = getmetatable(array()).__index;
  101. __newindex = function (self, i, v)
  102. rawset(self, i, assert(prepare(v), "Invalid service entry added"));
  103. end;
  104. }
  105. function get_services(requested_type, origin, stanza, reply)
  106. local extras = module:get_host_items("external_service");
  107. local services = ( configured_services + extras ) / prepare;
  108. if requested_type then
  109. services:filter(function(item)
  110. return item.type == requested_type;
  111. end);
  112. end
  113. setmetatable(services, services_mt);
  114. if origin and stanza and reply then
  115. module:fire_event("external_service/services", {
  116. origin = origin;
  117. stanza = stanza;
  118. reply = reply;
  119. requested_type = requested_type;
  120. services = services;
  121. });
  122. end
  123. local res_services = {};
  124. for _, srv in ipairs(services) do
  125. table.insert(res_services, {
  126. type = srv.type;
  127. transport = srv.transport;
  128. host = srv.host;
  129. port = srv.port and string.format("%d", srv.port) or nil;
  130. username = srv.username;
  131. password = srv.password;
  132. expires = srv.expires and dt.datetime(srv.expires) or nil;
  133. restricted = srv.restricted and "1" or nil;
  134. })
  135. end
  136. return res_services;
  137. end
  138. local function handle_services(event)
  139. local origin, stanza = event.origin, event.stanza;
  140. local action = stanza.tags[1];
  141. local user_bare = jid.bare(stanza.attr.from);
  142. local user_host = jid.host(user_bare);
  143. if not ((access:empty() and origin.type == "c2s") or access:contains(user_bare) or access:contains(user_host)) then
  144. origin.send(st.error_reply(stanza, "auth", "forbidden"));
  145. return true;
  146. end
  147. local reply = st.reply(stanza):tag("services", { xmlns = action.attr.xmlns });
  148. for _, srv in ipairs(get_services(action.attr.type, origin, stanza, reply)) do
  149. reply:tag("service", srv):up();
  150. end
  151. origin.send(reply);
  152. return true;
  153. end
  154. local function handle_credentials(event)
  155. local origin, stanza = event.origin, event.stanza;
  156. local action = stanza.tags[1];
  157. if origin.type ~= "c2s" then
  158. origin.send(st.error_reply(stanza, "auth", "forbidden"));
  159. return true;
  160. end
  161. local reply = st.reply(stanza):tag("credentials", { xmlns = action.attr.xmlns });
  162. local extras = module:get_host_items("external_service");
  163. local services = ( configured_services + extras ) / prepare;
  164. services:filter(function (item)
  165. return item.restricted;
  166. end)
  167. local requested_credentials = {};
  168. for service in action:childtags("service") do
  169. table.insert(requested_credentials, {
  170. type = service.attr.type;
  171. host = service.attr.host;
  172. port = tonumber(service.attr.port);
  173. });
  174. end
  175. setmetatable(services, services_mt);
  176. setmetatable(requested_credentials, services_mt);
  177. module:fire_event("external_service/credentials", {
  178. origin = origin;
  179. stanza = stanza;
  180. reply = reply;
  181. requested_credentials = requested_credentials;
  182. services = services;
  183. });
  184. for req_srv in action:childtags("service") do
  185. for _, srv in ipairs(services) do
  186. if srv.type == req_srv.attr.type and srv.host == req_srv.attr.host
  187. and not req_srv.attr.port or srv.port == tonumber(req_srv.attr.port) then
  188. reply:tag("service", {
  189. type = srv.type;
  190. transport = srv.transport;
  191. host = srv.host;
  192. port = srv.port and string.format("%d", srv.port) or nil;
  193. username = srv.username;
  194. password = srv.password;
  195. expires = srv.expires and dt.datetime(srv.expires) or nil;
  196. restricted = srv.restricted and "1" or nil;
  197. }):up();
  198. end
  199. end
  200. end
  201. origin.send(reply);
  202. return true;
  203. end
  204. -- XEP-0215 v0.7
  205. module:add_feature("urn:xmpp:extdisco:2");
  206. module:hook("iq-get/host/urn:xmpp:extdisco:2:services", handle_services);
  207. module:hook("iq-get/host/urn:xmpp:extdisco:2:credentials", handle_credentials);
  208. -- COMPAT XEP-0215 v0.6
  209. -- Those still on the old version gets to deal with undefined attributes until they upgrade.
  210. module:add_feature("urn:xmpp:extdisco:1");
  211. module:hook("iq-get/host/urn:xmpp:extdisco:1:services", handle_services);
  212. module:hook("iq-get/host/urn:xmpp:extdisco:1:credentials", handle_credentials);