]> jfr.im git - solanum.git/blame - modules/m_sasl.c
Make new_local_user hooks handle dead clients
[solanum.git] / modules / m_sasl.c
CommitLineData
212380e3
AC
1/* modules/m_sasl.c
2 * Copyright (C) 2006 Michael Tharp <gxti@partiallystapled.com>
f62f94b0 3 * Copyright (C) 2006 charybdis development team
15b05f95 4 * Copyright (C) 2016 ChatLounge IRC Network Development Team
212380e3
AC
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * 1.Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2.Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3.The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
212380e3
AC
29 */
30
31#include "stdinc.h"
32
33#include "client.h"
34#include "hash.h"
35#include "send.h"
36#include "msg.h"
37#include "modules.h"
38#include "numeric.h"
37289346 39#include "reject.h"
212380e3
AC
40#include "s_serv.h"
41#include "s_stats.h"
42#include "string.h"
7d33cce8
MT
43#include "s_newconf.h"
44#include "s_conf.h"
212380e3 45
eeabf33a
EM
46static const char sasl_desc[] = "Provides SASL authentication support";
47
3c7d6fcc
EM
48static void m_authenticate(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
49static void me_sasl(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
50static void me_mechlist(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
212380e3
AC
51
52static void abort_sasl(struct Client *);
53static void abort_sasl_exit(hook_data_client_exit *);
54
da3e5fcb
AC
55static unsigned int CLICAP_SASL = 0;
56static char mechlist_buf[BUFSIZE];
193d4db3 57
212380e3 58struct Message authenticate_msgtab = {
7baa37a9 59 "AUTHENTICATE", 0, 0, 0, 0,
ef3ab8e3 60 {{m_authenticate, 2}, {m_authenticate, 2}, mg_ignore, mg_ignore, mg_ignore, {m_authenticate, 2}}
212380e3
AC
61};
62struct Message sasl_msgtab = {
7baa37a9 63 "SASL", 0, 0, 0, 0,
212380e3
AC
64 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_sasl, 5}, mg_ignore}
65};
da3e5fcb
AC
66struct Message mechlist_msgtab = {
67 "MECHLIST", 0, 0, 0, 0,
68 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_mechlist, 2}, mg_ignore}
69};
212380e3 70
55abcbb2 71mapi_clist_av1 sasl_clist[] = {
da3e5fcb 72 &authenticate_msgtab, &sasl_msgtab, &mechlist_msgtab, NULL
212380e3
AC
73};
74mapi_hfn_list_av1 sasl_hfnlist[] = {
75 { "new_local_user", (hookfn) abort_sasl },
76 { "client_exit", (hookfn) abort_sasl_exit },
77 { NULL, NULL }
78};
79
da3e5fcb 80static const char *
38ffccf8 81sasl_data(struct Client *client_p)
da3e5fcb
AC
82{
83 return *mechlist_buf != 0 ? mechlist_buf : NULL;
84}
85
193d4db3 86static struct ClientCapability capdata_sasl = {
da3e5fcb 87 .data = sasl_data,
738b5d29 88 .flags = CLICAP_FLAGS_STICKY | CLICAP_FLAGS_PRIORITY,
193d4db3
AC
89};
90
3792c63d
AC
91mapi_cap_list_av2 sasl_cap_list[] = {
92 { MAPI_CAP_CLIENT, "sasl", &capdata_sasl, &CLICAP_SASL },
93 { 0, NULL, NULL, NULL },
94};
95
193d4db3
AC
96static int
97_modinit(void)
98{
da3e5fcb 99 memset(mechlist_buf, 0, sizeof mechlist_buf);
193d4db3
AC
100 return 0;
101}
102
e83449d5 103DECLARE_MODULE_AV2(sasl, _modinit, NULL, sasl_clist, NULL, sasl_hfnlist, sasl_cap_list, NULL, sasl_desc);
212380e3 104
3c7d6fcc 105static void
428ca87b 106m_authenticate(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
107 int parc, const char *parv[])
108{
109 struct Client *agent_p = NULL;
7d33cce8 110 struct Client *saslserv_p = NULL;
212380e3
AC
111
112 /* They really should use CAP for their own sake. */
113 if(!IsCapable(source_p, CLICAP_SASL))
3c7d6fcc 114 return;
212380e3 115
9d07a42d
MM
116 if(source_p->localClient->sasl_next_retry > rb_current_time())
117 {
118 sendto_one(source_p, form_str(RPL_LOAD2HI), me.name, EmptyString(source_p->name) ? "*" : source_p->name, msgbuf_p->cmd);
119 return;
120 }
121
d5d52a99 122 if (strlen(client_p->id) == 3 || (source_p->preClient && !EmptyString(source_p->preClient->id)))
1b19fe8b
JT
123 {
124 exit_client(client_p, client_p, client_p, "Mixing client and server protocol");
3c7d6fcc 125 return;
1b19fe8b
JT
126 }
127
ac88154f
AJ
128 if (*parv[1] == ':' || strchr(parv[1], ' '))
129 {
130 exit_client(client_p, client_p, client_p, "Malformed AUTHENTICATE");
131 return;
132 }
133
7d33cce8 134 saslserv_p = find_named_client(ConfigFileEntry.sasl_service);
9d07a42d 135 if(saslserv_p == NULL || !IsService(saslserv_p))
7d33cce8
MT
136 {
137 sendto_one(source_p, form_str(ERR_SASLABORTED), me.name, EmptyString(source_p->name) ? "*" : source_p->name);
3c7d6fcc 138 return;
7d33cce8
MT
139 }
140
c6bc97fd 141 if(source_p->localClient->sasl_complete)
212380e3 142 {
51535fcb 143 *source_p->localClient->sasl_agent = '\0';
c6bc97fd 144 source_p->localClient->sasl_complete = 0;
212380e3
AC
145 }
146
147 if(strlen(parv[1]) > 400)
148 {
149 sendto_one(source_p, form_str(ERR_SASLTOOLONG), me.name, EmptyString(source_p->name) ? "*" : source_p->name);
3c7d6fcc 150 return;
212380e3
AC
151 }
152
153 if(!*source_p->id)
154 {
155 /* Allocate a UID. */
4d5a902f 156 rb_strlcpy(source_p->id, generate_uid(), sizeof(source_p->id));
212380e3
AC
157 add_to_id_hash(source_p->id, source_p);
158 }
159
c6bc97fd
AC
160 if(*source_p->localClient->sasl_agent)
161 agent_p = find_id(source_p->localClient->sasl_agent);
212380e3
AC
162
163 if(agent_p == NULL)
572488e0 164 {
280ce6a9
AJ
165 if (!strcmp(parv[1], "*"))
166 {
167 sendto_one(source_p, form_str(ERR_SASLABORTED), me.name, EmptyString(source_p->name) ? "*" : source_p->name);
6d8a8851 168 source_p->localClient->sasl_out = 0;
631c3089 169 return;
280ce6a9
AJ
170 }
171
0ee833da 172 sendto_one(saslserv_p, ":%s ENCAP %s SASL %s %s H %s %s %c",
1cae2411 173 me.id, saslserv_p->servptr->name, source_p->id, saslserv_p->id,
0ee833da 174 source_p->host, source_p->sockhost,
8b7110d6 175 IsSecure(source_p) ? 'S' : 'P');
a3fa9d81 176
802710b5 177 if (source_p->certfp != NULL)
6fb9f214
MM
178 sendto_one(saslserv_p, ":%s ENCAP %s SASL %s %s S %s %s",
179 me.id, saslserv_p->servptr->name, source_p->id, saslserv_p->id,
180 parv[1], source_p->certfp);
572488e0 181 else
6fb9f214
MM
182 sendto_one(saslserv_p, ":%s ENCAP %s SASL %s %s S %s",
183 me.id, saslserv_p->servptr->name, source_p->id, saslserv_p->id,
184 parv[1]);
7d33cce8 185
c6bc97fd 186 rb_strlcpy(source_p->localClient->sasl_agent, saslserv_p->id, IDLEN);
572488e0 187 }
212380e3 188 else
280ce6a9
AJ
189 {
190 if (!strcmp(parv[1], "*"))
191 {
192 sendto_one(source_p, form_str(ERR_SASLABORTED), me.name, EmptyString(source_p->name) ? "*" : source_p->name);
193 sendto_one(agent_p, ":%s ENCAP %s SASL %s %s D A", me.id, agent_p->servptr->name, source_p->id, agent_p->id);
6d8a8851 194 source_p->localClient->sasl_out = 0;
631c3089 195 return;
280ce6a9
AJ
196 }
197
6fb9f214
MM
198 sendto_one(agent_p, ":%s ENCAP %s SASL %s %s C %s",
199 me.id, agent_p->servptr->name, source_p->id, agent_p->id,
7d33cce8 200 parv[1]);
280ce6a9
AJ
201 }
202
c6bc97fd 203 source_p->localClient->sasl_out++;
212380e3
AC
204}
205
3c7d6fcc 206static void
428ca87b 207me_sasl(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
208 int parc, const char *parv[])
209{
210 struct Client *target_p, *agent_p;
40a766a0 211 bool in_progress;
212380e3
AC
212
213 /* Let propagate if not addressed to us, or if broadcast.
214 * Only SASL agents can answer global requests.
215 */
216 if(strncmp(parv[2], me.id, 3))
3c7d6fcc 217 return;
212380e3
AC
218
219 if((target_p = find_id(parv[2])) == NULL)
3c7d6fcc 220 return;
212380e3 221
212380e3 222 if((agent_p = find_id(parv[1])) == NULL)
3c7d6fcc 223 return;
212380e3
AC
224
225 if(source_p != agent_p->servptr) /* WTF?! */
3c7d6fcc 226 return;
212380e3
AC
227
228 /* We only accept messages from SASL agents; these must have umode +S
229 * (so the server must be listed in a service{} block).
230 */
231 if(!IsService(agent_p))
3c7d6fcc 232 return;
212380e3 233
40a766a0
SA
234 /* If SASL has been aborted, we only want to track authentication failures. */
235 in_progress = target_p->localClient->sasl_out != 0;
236
212380e3 237 /* Reject if someone has already answered. */
c6bc97fd 238 if(*target_p->localClient->sasl_agent && strncmp(parv[1], target_p->localClient->sasl_agent, IDLEN))
3c7d6fcc 239 return;
40a766a0 240 else if(!*target_p->localClient->sasl_agent && in_progress)
c6bc97fd 241 rb_strlcpy(target_p->localClient->sasl_agent, parv[1], IDLEN);
212380e3 242
f62f94b0 243 if(*parv[3] == 'C')
37289346 244 {
40a766a0
SA
245 if (in_progress) {
246 sendto_one(target_p, "AUTHENTICATE %s", parv[4]);
247 target_p->localClient->sasl_messages++;
248 }
37289346 249 }
212380e3
AC
250 else if(*parv[3] == 'D')
251 {
834579ce
MM
252 if(*parv[4] == 'F')
253 {
40a766a0
SA
254 if (in_progress) {
255 sendto_one(target_p, form_str(ERR_SASLFAIL), me.name, EmptyString(target_p->name) ? "*" : target_p->name);
256 }
9d07a42d 257 /* Failures with zero messages are just "unknown mechanism" errors; don't count those. */
834579ce 258 if(target_p->localClient->sasl_messages > 0)
37289346 259 {
9d07a42d
MM
260 if(*target_p->name)
261 {
46ef49c3
X
262 /* Allow 2 tries before rate-limiting as some clients try EXTERNAL
263 * then PLAIN right after it if the auth failed, causing the client to be
264 * rate-limited immediately and not being able to login with SASL.
265 */
266 if (target_p->localClient->sasl_failures++ > 0)
23f5c317 267 target_p->localClient->sasl_next_retry = rb_current_time() + (1 << MIN(target_p->localClient->sasl_failures + 1, 8));
9d07a42d
MM
268 }
269 else if(throttle_add((struct sockaddr*)&target_p->localClient->ip))
37289346
MM
270 {
271 exit_client(target_p, target_p, &me, "Too many failed authentication attempts");
272 return;
273 }
274 }
275 }
834579ce
MM
276 else if(*parv[4] == 'S')
277 {
40a766a0
SA
278 if (in_progress) {
279 sendto_one(target_p, form_str(RPL_SASLSUCCESS), me.name, EmptyString(target_p->name) ? "*" : target_p->name);
280 target_p->localClient->sasl_failures = 0;
281 target_p->localClient->sasl_complete = 1;
282 ServerStats.is_ssuc++;
283 }
212380e3 284 }
c6bc97fd 285 *target_p->localClient->sasl_agent = '\0'; /* Blank the stored agent so someone else can answer */
37289346 286 target_p->localClient->sasl_messages = 0;
212380e3 287 }
dbd8ca2b 288 else if(*parv[3] == 'M')
40a766a0
SA
289 {
290 if (in_progress) {
291 sendto_one(target_p, form_str(RPL_SASLMECHS), me.name, EmptyString(target_p->name) ? "*" : target_p->name, parv[4]);
292 }
293 }
212380e3
AC
294}
295
3c7d6fcc 296static void
da3e5fcb
AC
297me_mechlist(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
298 int parc, const char *parv[])
299{
300 rb_strlcpy(mechlist_buf, parv[1], sizeof mechlist_buf);
da3e5fcb
AC
301}
302
212380e3
AC
303/* If the client never finished authenticating but is
304 * registering anyway, abort the exchange.
305 */
306static void
307abort_sasl(struct Client *data)
308{
c6bc97fd 309 if(data->localClient->sasl_out == 0 || data->localClient->sasl_complete)
212380e3
AC
310 return;
311
c6bc97fd 312 data->localClient->sasl_out = data->localClient->sasl_complete = 0;
47adde3d 313 ServerStats.is_sbad++;
212380e3
AC
314
315 if(!IsClosing(data))
316 sendto_one(data, form_str(ERR_SASLABORTED), me.name, EmptyString(data->name) ? "*" : data->name);
317
c6bc97fd 318 if(*data->localClient->sasl_agent)
212380e3 319 {
c6bc97fd 320 struct Client *agent_p = find_id(data->localClient->sasl_agent);
212380e3
AC
321 if(agent_p)
322 {
323 sendto_one(agent_p, ":%s ENCAP %s SASL %s %s D A", me.id, agent_p->servptr->name,
324 data->id, agent_p->id);
325 return;
326 }
327 }
328
329 sendto_server(NULL, NULL, CAP_TS6|CAP_ENCAP, NOCAPS, ":%s ENCAP * SASL %s * D A", me.id,
330 data->id);
331}
332
333static void
334abort_sasl_exit(hook_data_client_exit *data)
335{
c23902ae
AC
336 if (data->target->localClient)
337 abort_sasl(data->target);
212380e3 338}