plugins/mod_saslauth.lua

Sun, 05 Oct 2008 19:10:21 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 05 Oct 2008 19:10:21 +0100
branch
tls
changeset 66
018705d57f09
parent 52
93e468eb2ffb
child 99
ba08b8a4eeef
permissions
-rw-r--r--

Working TLS!

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_connhandler = require "net.connhandlers".new;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local new_sasl = require "util.sasl".new;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
48
d0505310aec5 Use xmlns for matching auth tag too
Matthew Wild <mwild1@gmail.com>
parents: 46
diff changeset
19 add_handler("c2s_unauthed", "auth", xmlns_sasl,
38
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 function (session, stanza)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if not session.sasl_handler then
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 session.sasl_handler = new_sasl(stanza.attr.mechanism,
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 function (username, password)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 -- onAuth
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 require "core.usermanager"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if usermanager_validate_credentials(session.host, username, password) then
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 return true;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 return false;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end,
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 function (username)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 -- onSuccess
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local success, err = sessionmanager.make_authenticated(session, username);
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 if not success then
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 sessionmanager.destroy_session(session);
52
93e468eb2ffb Fix for destruction of unauthed SASL sessions
Matthew Wild <mwild1@gmail.com>
parents: 48
diff changeset
36 return;
38
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 session.sasl_handler = nil;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 session.connhandler = new_connhandler("xmpp-client", session);
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 session.notopen = true;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end,
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 function (reason)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 -- onFail
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 log("debug", "SASL failure, reason: %s", reason);
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end,
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 function (stanza)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 -- onWrite
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 log("debug", "SASL writes: %s", tostring(stanza));
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 send(session, stanza);
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 );
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 session.sasl_handler:feed(stanza);
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 else
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 error("Client tried to negotiate SASL again", 0);
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
46
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
57 end);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
58
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
59 add_event_hook("stream-features",
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
60 function (session, features)
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
61 if not session.username then
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
62 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
63 t_insert(features, "<mechanism>PLAIN</mechanism>");
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
64 t_insert(features, "</mechanisms>");
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
65 else
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
66 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
67 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
68 end
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
69 --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
70 end);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
71
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
72 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
73 function (session, stanza)
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
74 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
75 local resource;
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
76 if stanza.attr.type == "set" then
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
77 local bind = stanza.tags[1];
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
78
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
79 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
80 resource = bind:child_with_name("resource");
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
81 if resource then
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
82 resource = resource[1];
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
83 end
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
84 end
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
85 end
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
86 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
87 if not success then
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
88 local reply = st.reply(stanza);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
89 reply.attr.type = "error";
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
90 if err == "conflict" then
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
91 reply:tag("error", { type = "modify" })
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
92 :tag("conflict", { xmlns = xmlns_stanzas });
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
93 elseif err == "constraint" then
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
94 reply:tag("error", { type = "cancel" })
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
95 :tag("resource-constraint", { xmlns = xmlns_stanzas });
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
96 elseif err == "auth" 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("not-allowed", { xmlns = xmlns_stanzas });
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
99 end
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
100 send(session, reply);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
101 else
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
102 local reply = st.reply(stanza);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
103 reply:tag("bind", { xmlns = xmlns_bind})
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
104 :tag("jid"):text(session.full_jid);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
105 send(session, reply);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
106 end
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
107 end);
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
108
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
109 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
110 function (session, stanza)
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
111 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
112 send(session, st.reply(stanza));
d6b3f9dbb624 Resource binding, XMPP sessions (whatever they're for...)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
113 end);

mercurial