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