]>
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. | |
28 | * | |
29 | * $Id: m_sasl.c 1409 2006-05-21 14:46:17Z jilles $ | |
30 | */ | |
31 | ||
32 | #include "stdinc.h" | |
33 | ||
34 | #include "client.h" | |
35 | #include "hash.h" | |
36 | #include "send.h" | |
37 | #include "msg.h" | |
38 | #include "modules.h" | |
39 | #include "numeric.h" | |
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 | |
428ca87b AC |
46 | static int m_authenticate(struct MsgBuf *, struct Client *, struct Client *, int, const char **); |
47 | static int me_sasl(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
da3e5fcb | 48 | static int me_mechlist(struct MsgBuf *, struct Client *, struct Client *, int, const char **); |
212380e3 AC |
49 | |
50 | static void abort_sasl(struct Client *); | |
51 | static void abort_sasl_exit(hook_data_client_exit *); | |
52 | ||
12565204 AC |
53 | static void advertise_sasl(struct Client *); |
54 | static void advertise_sasl_exit(hook_data_client_exit *); | |
55 | ||
da3e5fcb AC |
56 | static unsigned int CLICAP_SASL = 0; |
57 | static char mechlist_buf[BUFSIZE]; | |
193d4db3 | 58 | |
212380e3 | 59 | struct Message authenticate_msgtab = { |
7baa37a9 | 60 | "AUTHENTICATE", 0, 0, 0, 0, |
ef3ab8e3 | 61 | {{m_authenticate, 2}, {m_authenticate, 2}, mg_ignore, mg_ignore, mg_ignore, {m_authenticate, 2}} |
212380e3 AC |
62 | }; |
63 | struct Message sasl_msgtab = { | |
7baa37a9 | 64 | "SASL", 0, 0, 0, 0, |
212380e3 AC |
65 | {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_sasl, 5}, mg_ignore} |
66 | }; | |
da3e5fcb AC |
67 | struct Message mechlist_msgtab = { |
68 | "MECHLIST", 0, 0, 0, 0, | |
69 | {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_mechlist, 2}, mg_ignore} | |
70 | }; | |
212380e3 | 71 | |
55abcbb2 | 72 | mapi_clist_av1 sasl_clist[] = { |
da3e5fcb | 73 | &authenticate_msgtab, &sasl_msgtab, &mechlist_msgtab, NULL |
212380e3 AC |
74 | }; |
75 | mapi_hfn_list_av1 sasl_hfnlist[] = { | |
76 | { "new_local_user", (hookfn) abort_sasl }, | |
77 | { "client_exit", (hookfn) abort_sasl_exit }, | |
12565204 AC |
78 | { "new_remote_user", (hookfn) advertise_sasl }, |
79 | { "client_exit", (hookfn) advertise_sasl_exit }, | |
212380e3 AC |
80 | { NULL, NULL } |
81 | }; | |
82 | ||
193d4db3 AC |
83 | static int |
84 | sasl_visible(void) | |
85 | { | |
86 | struct Client *agent_p = NULL; | |
87 | ||
88 | if (ConfigFileEntry.sasl_service) | |
89 | agent_p = find_named_client(ConfigFileEntry.sasl_service); | |
90 | ||
91 | return agent_p != NULL && IsService(agent_p); | |
92 | } | |
93 | ||
da3e5fcb AC |
94 | static const char * |
95 | sasl_data(void) | |
96 | { | |
97 | return *mechlist_buf != 0 ? mechlist_buf : NULL; | |
98 | } | |
99 | ||
193d4db3 AC |
100 | static struct ClientCapability capdata_sasl = { |
101 | .visible = sasl_visible, | |
da3e5fcb | 102 | .data = sasl_data, |
193d4db3 AC |
103 | .flags = CLICAP_FLAGS_STICKY, |
104 | }; | |
105 | ||
106 | static int | |
107 | _modinit(void) | |
108 | { | |
da3e5fcb | 109 | memset(mechlist_buf, 0, sizeof mechlist_buf); |
193d4db3 AC |
110 | CLICAP_SASL = capability_put(cli_capindex, "sasl", &capdata_sasl); |
111 | return 0; | |
112 | } | |
113 | ||
114 | static void | |
115 | _moddeinit(void) | |
116 | { | |
117 | capability_orphan(cli_capindex, "sasl"); | |
118 | } | |
119 | ||
120 | DECLARE_MODULE_AV1(sasl, _modinit, _moddeinit, sasl_clist, NULL, sasl_hfnlist, "$Revision: 1409 $"); | |
212380e3 AC |
121 | |
122 | static int | |
428ca87b | 123 | m_authenticate(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, |
212380e3 AC |
124 | int parc, const char *parv[]) |
125 | { | |
126 | struct Client *agent_p = NULL; | |
7d33cce8 | 127 | struct Client *saslserv_p = NULL; |
212380e3 AC |
128 | |
129 | /* They really should use CAP for their own sake. */ | |
130 | if(!IsCapable(source_p, CLICAP_SASL)) | |
131 | return 0; | |
132 | ||
1b19fe8b JT |
133 | if (strlen(client_p->id) == 3) |
134 | { | |
135 | exit_client(client_p, client_p, client_p, "Mixing client and server protocol"); | |
136 | return 0; | |
137 | } | |
138 | ||
7d33cce8 MT |
139 | saslserv_p = find_named_client(ConfigFileEntry.sasl_service); |
140 | if (saslserv_p == NULL || !IsService(saslserv_p)) | |
141 | { | |
142 | sendto_one(source_p, form_str(ERR_SASLABORTED), me.name, EmptyString(source_p->name) ? "*" : source_p->name); | |
143 | return 0; | |
144 | } | |
145 | ||
c6bc97fd | 146 | if(source_p->localClient->sasl_complete) |
212380e3 | 147 | { |
51535fcb | 148 | *source_p->localClient->sasl_agent = '\0'; |
c6bc97fd | 149 | source_p->localClient->sasl_complete = 0; |
212380e3 AC |
150 | } |
151 | ||
152 | if(strlen(parv[1]) > 400) | |
153 | { | |
154 | sendto_one(source_p, form_str(ERR_SASLTOOLONG), me.name, EmptyString(source_p->name) ? "*" : source_p->name); | |
155 | return 0; | |
156 | } | |
157 | ||
158 | if(!*source_p->id) | |
159 | { | |
160 | /* Allocate a UID. */ | |
161 | strcpy(source_p->id, generate_uid()); | |
162 | add_to_id_hash(source_p->id, source_p); | |
163 | } | |
164 | ||
c6bc97fd AC |
165 | if(*source_p->localClient->sasl_agent) |
166 | agent_p = find_id(source_p->localClient->sasl_agent); | |
212380e3 AC |
167 | |
168 | if(agent_p == NULL) | |
572488e0 | 169 | { |
1cae2411 MM |
170 | sendto_one(saslserv_p, ":%s ENCAP %s SASL %s %s H %s %s", |
171 | me.id, saslserv_p->servptr->name, source_p->id, saslserv_p->id, | |
172 | source_p->host, source_p->sockhost); | |
a3fa9d81 | 173 | |
802710b5 | 174 | if (source_p->certfp != NULL) |
6fb9f214 MM |
175 | sendto_one(saslserv_p, ":%s ENCAP %s SASL %s %s S %s %s", |
176 | me.id, saslserv_p->servptr->name, source_p->id, saslserv_p->id, | |
177 | parv[1], source_p->certfp); | |
572488e0 | 178 | else |
6fb9f214 MM |
179 | sendto_one(saslserv_p, ":%s ENCAP %s SASL %s %s S %s", |
180 | me.id, saslserv_p->servptr->name, source_p->id, saslserv_p->id, | |
181 | parv[1]); | |
7d33cce8 | 182 | |
c6bc97fd | 183 | rb_strlcpy(source_p->localClient->sasl_agent, saslserv_p->id, IDLEN); |
572488e0 | 184 | } |
212380e3 | 185 | else |
6fb9f214 MM |
186 | sendto_one(agent_p, ":%s ENCAP %s SASL %s %s C %s", |
187 | me.id, agent_p->servptr->name, source_p->id, agent_p->id, | |
7d33cce8 | 188 | parv[1]); |
c6bc97fd | 189 | source_p->localClient->sasl_out++; |
212380e3 AC |
190 | |
191 | return 0; | |
192 | } | |
193 | ||
194 | static int | |
428ca87b | 195 | me_sasl(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, |
212380e3 AC |
196 | int parc, const char *parv[]) |
197 | { | |
198 | struct Client *target_p, *agent_p; | |
199 | ||
200 | /* Let propagate if not addressed to us, or if broadcast. | |
201 | * Only SASL agents can answer global requests. | |
202 | */ | |
203 | if(strncmp(parv[2], me.id, 3)) | |
204 | return 0; | |
205 | ||
206 | if((target_p = find_id(parv[2])) == NULL) | |
207 | return 0; | |
208 | ||
212380e3 AC |
209 | if((agent_p = find_id(parv[1])) == NULL) |
210 | return 0; | |
211 | ||
212 | if(source_p != agent_p->servptr) /* WTF?! */ | |
213 | return 0; | |
214 | ||
215 | /* We only accept messages from SASL agents; these must have umode +S | |
216 | * (so the server must be listed in a service{} block). | |
217 | */ | |
218 | if(!IsService(agent_p)) | |
219 | return 0; | |
220 | ||
221 | /* Reject if someone has already answered. */ | |
c6bc97fd | 222 | if(*target_p->localClient->sasl_agent && strncmp(parv[1], target_p->localClient->sasl_agent, IDLEN)) |
212380e3 | 223 | return 0; |
c6bc97fd AC |
224 | else if(!*target_p->localClient->sasl_agent) |
225 | rb_strlcpy(target_p->localClient->sasl_agent, parv[1], IDLEN); | |
212380e3 | 226 | |
f62f94b0 | 227 | if(*parv[3] == 'C') |
212380e3 AC |
228 | sendto_one(target_p, "AUTHENTICATE %s", parv[4]); |
229 | else if(*parv[3] == 'D') | |
230 | { | |
231 | if(*parv[4] == 'F') | |
232 | sendto_one(target_p, form_str(ERR_SASLFAIL), me.name, EmptyString(target_p->name) ? "*" : target_p->name); | |
233 | else if(*parv[4] == 'S') { | |
234 | sendto_one(target_p, form_str(RPL_SASLSUCCESS), me.name, EmptyString(target_p->name) ? "*" : target_p->name); | |
c6bc97fd | 235 | target_p->localClient->sasl_complete = 1; |
47adde3d | 236 | ServerStats.is_ssuc++; |
212380e3 | 237 | } |
c6bc97fd | 238 | *target_p->localClient->sasl_agent = '\0'; /* Blank the stored agent so someone else can answer */ |
212380e3 | 239 | } |
dbd8ca2b MM |
240 | else if(*parv[3] == 'M') |
241 | sendto_one(target_p, form_str(RPL_SASLMECHS), me.name, EmptyString(target_p->name) ? "*" : target_p->name, parv[4]); | |
55abcbb2 | 242 | |
212380e3 AC |
243 | return 0; |
244 | } | |
245 | ||
da3e5fcb AC |
246 | static int |
247 | me_mechlist(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, | |
248 | int parc, const char *parv[]) | |
249 | { | |
250 | rb_strlcpy(mechlist_buf, parv[1], sizeof mechlist_buf); | |
251 | ||
252 | return 0; | |
253 | } | |
254 | ||
212380e3 AC |
255 | /* If the client never finished authenticating but is |
256 | * registering anyway, abort the exchange. | |
257 | */ | |
258 | static void | |
259 | abort_sasl(struct Client *data) | |
260 | { | |
c6bc97fd | 261 | if(data->localClient->sasl_out == 0 || data->localClient->sasl_complete) |
212380e3 AC |
262 | return; |
263 | ||
c6bc97fd | 264 | data->localClient->sasl_out = data->localClient->sasl_complete = 0; |
47adde3d | 265 | ServerStats.is_sbad++; |
212380e3 AC |
266 | |
267 | if(!IsClosing(data)) | |
268 | sendto_one(data, form_str(ERR_SASLABORTED), me.name, EmptyString(data->name) ? "*" : data->name); | |
269 | ||
c6bc97fd | 270 | if(*data->localClient->sasl_agent) |
212380e3 | 271 | { |
c6bc97fd | 272 | struct Client *agent_p = find_id(data->localClient->sasl_agent); |
212380e3 AC |
273 | if(agent_p) |
274 | { | |
275 | sendto_one(agent_p, ":%s ENCAP %s SASL %s %s D A", me.id, agent_p->servptr->name, | |
276 | data->id, agent_p->id); | |
277 | return; | |
278 | } | |
279 | } | |
280 | ||
281 | sendto_server(NULL, NULL, CAP_TS6|CAP_ENCAP, NOCAPS, ":%s ENCAP * SASL %s * D A", me.id, | |
282 | data->id); | |
283 | } | |
284 | ||
285 | static void | |
286 | abort_sasl_exit(hook_data_client_exit *data) | |
287 | { | |
c23902ae AC |
288 | if (data->target->localClient) |
289 | abort_sasl(data->target); | |
212380e3 AC |
290 | } |
291 | ||
12565204 AC |
292 | static void |
293 | advertise_sasl(struct Client *client_p) | |
294 | { | |
295 | if (!ConfigFileEntry.sasl_service) | |
296 | return; | |
297 | ||
298 | if (irccmp(client_p->name, ConfigFileEntry.sasl_service)) | |
299 | return; | |
300 | ||
301 | sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * NEW :sasl", me.name); | |
302 | } | |
303 | ||
304 | static void | |
305 | advertise_sasl_exit(hook_data_client_exit *data) | |
306 | { | |
307 | if (!ConfigFileEntry.sasl_service) | |
308 | return; | |
309 | ||
310 | if (irccmp(data->target->name, ConfigFileEntry.sasl_service)) | |
311 | return; | |
312 | ||
313 | sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * DEL :sasl", me.name); | |
314 | } |