Annotate

plugins/mod_saslauth.lua @ 278:770a78cd38d7

Forward response stanzas to sasl.lua and some other stuff.
author Tobias Markmann <tm@ayena.de>
date Mon, 10 Nov 2008 16:28:15 +0100
parent 120:ef964468f174
child 282:80e7de32b618
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local st = require "util.stanza";
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local send = require "core.sessionmanager".send_to_session;
46
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
4 local sm_bind_resource = require "core.sessionmanager".bind_resource;
38
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local usermanager_validate_credentials = require "core.usermanager".validate_credentials;
46
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
7 local t_concat, t_insert = table.concat, table.insert;
38
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local tostring = tostring;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local log = require "util.logger".init("mod_saslauth");
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local xmlns_sasl ='urn:ietf:params:xml:ns:xmpp-sasl';
46
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
13 local xmlns_bind ='urn:ietf:params:xml:ns:xmpp-bind';
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
14 local xmlns_stanzas ='urn:ietf:params:xml:ns:xmpp-stanzas';
38
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local new_sasl = require "util.sasl".new;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
278
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
18 add_handler("c2s_unauthed", "auth", xmlns_sasl, function (session, stanza)
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
19 if not session.sasl_handler then
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
20 session.sasl_handler = new_sasl(stanza.attr.mechanism,
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
21 function (username, password)
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
22 -- onAuth
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
23 require "core.usermanager"
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
24 if usermanager_validate_credentials(session.host, username, password) then
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
25 return true;
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
26 end
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
27 return false;
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
28 end,
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
29 function (username)
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
30 -- onSuccess
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
31 local success, err = sessionmanager.make_authenticated(session, username);
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
32 if not success then
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
33 sessionmanager.destroy_session(session);
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
34 return;
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
35 end
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
36 session.sasl_handler = nil;
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
37 session:reset_stream();
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
38 end,
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
39 function (reason)
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
40 -- onFail
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
41 log("debug", "SASL failure, reason: %s", reason);
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
42 end,
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
43 function (stanza)
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
44 -- onWrite
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
45 log("debug", "SASL writes: %s", tostring(stanza));
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
46 send(session, stanza);
38
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
278
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
48 );
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
49 session.sasl_handler:feed(stanza);
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
50 else
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
51 error("Client tried to negotiate SASL again", 0);
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
52 end
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
53 end);
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
54
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
55 add_handler("c2s_unauthed", "response", xmlns_sasl, function (session, stanza)
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
56 if session.sasl_handler then
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
57 session.sasl_handler:feed(stanza);
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
58 end
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
59 end);
46
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
60
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
61 add_event_hook("stream-features",
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
62 function (session, features)
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
63 if not session.username then
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
64 t_insert(features, "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>");
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
65 t_insert(features, "<mechanism>PLAIN</mechanism>");
278
770a78cd38d7 Forward response stanzas to sasl.lua and some other stuff.
Tobias Markmann <tm@ayena.de>
parents: 120
diff changeset
66 t_insert(features, "<mechanism>DIGEST-MD5</mechanism>");
46
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
67 t_insert(features, "</mechanisms>");
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
68 else
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
69 t_insert(features, "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><required/></bind>");
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
70 t_insert(features, "<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>");
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
71 end
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
72 --send [[<register xmlns="http://jabber.org/features/iq-register"/> ]]
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
73 end);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
74
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
75 add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
76 function (session, stanza)
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
77 log("debug", "Client tried to bind to a resource");
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
78 local resource;
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
79 if stanza.attr.type == "set" then
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
80 local bind = stanza.tags[1];
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
81
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
82 if bind and bind.attr.xmlns == xmlns_bind then
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
83 resource = bind:child_with_name("resource");
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
84 if resource then
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
85 resource = resource[1];
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
86 end
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
87 end
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
88 end
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
89 local success, err = sm_bind_resource(session, resource);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
90 if not success then
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
91 local reply = st.reply(stanza);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
92 reply.attr.type = "error";
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
93 if err == "conflict" then
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
94 reply:tag("error", { type = "modify" })
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
95 :tag("conflict", { xmlns = xmlns_stanzas });
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
96 elseif err == "constraint" then
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
97 reply:tag("error", { type = "cancel" })
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
98 :tag("resource-constraint", { xmlns = xmlns_stanzas });
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
99 elseif err == "auth" then
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
100 reply:tag("error", { type = "cancel" })
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
101 :tag("not-allowed", { xmlns = xmlns_stanzas });
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
102 end
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
103 send(session, reply);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
104 else
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
105 local reply = st.reply(stanza);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
106 reply:tag("bind", { xmlns = xmlns_bind})
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
107 :tag("jid"):text(session.full_jid);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
108 send(session, reply);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
109 end
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
110 end);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
111
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
112 add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session",
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
113 function (session, stanza)
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
114 log("debug", "Client tried to bind to a resource");
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
115 send(session, st.reply(stanza));
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
116 end);