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