]>
Commit | Line | Data |
---|---|---|
1 | /* modules/m_sasl.c | |
2 | * Copyright (C) 2006 Michael Tharp <gxti@partiallystapled.com> | |
3 | * Copyright (C) 2006 charybdis development team | |
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" | |
43 | #include "s_newconf.h" | |
44 | #include "s_conf.h" | |
45 | ||
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 **); | |
48 | ||
49 | static void abort_sasl(struct Client *); | |
50 | static void abort_sasl_exit(hook_data_client_exit *); | |
51 | ||
52 | static void advertise_sasl(struct Client *); | |
53 | static void advertise_sasl_exit(hook_data_client_exit *); | |
54 | ||
55 | struct Message authenticate_msgtab = { | |
56 | "AUTHENTICATE", 0, 0, 0, 0, | |
57 | {{m_authenticate, 2}, {m_authenticate, 2}, mg_ignore, mg_ignore, mg_ignore, {m_authenticate, 2}} | |
58 | }; | |
59 | struct Message sasl_msgtab = { | |
60 | "SASL", 0, 0, 0, 0, | |
61 | {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_sasl, 5}, mg_ignore} | |
62 | }; | |
63 | ||
64 | mapi_clist_av1 sasl_clist[] = { | |
65 | &authenticate_msgtab, &sasl_msgtab, NULL | |
66 | }; | |
67 | mapi_hfn_list_av1 sasl_hfnlist[] = { | |
68 | { "new_local_user", (hookfn) abort_sasl }, | |
69 | { "client_exit", (hookfn) abort_sasl_exit }, | |
70 | { "new_remote_user", (hookfn) advertise_sasl }, | |
71 | { "client_exit", (hookfn) advertise_sasl_exit }, | |
72 | { NULL, NULL } | |
73 | }; | |
74 | ||
75 | DECLARE_MODULE_AV1(sasl, NULL, NULL, sasl_clist, NULL, sasl_hfnlist, "$Revision: 1409 $"); | |
76 | ||
77 | static int | |
78 | m_authenticate(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, | |
79 | int parc, const char *parv[]) | |
80 | { | |
81 | struct Client *agent_p = NULL; | |
82 | struct Client *saslserv_p = NULL; | |
83 | ||
84 | /* They really should use CAP for their own sake. */ | |
85 | if(!IsCapable(source_p, CLICAP_SASL)) | |
86 | return 0; | |
87 | ||
88 | if (strlen(client_p->id) == 3) | |
89 | { | |
90 | exit_client(client_p, client_p, client_p, "Mixing client and server protocol"); | |
91 | return 0; | |
92 | } | |
93 | ||
94 | saslserv_p = find_named_client(ConfigFileEntry.sasl_service); | |
95 | if (saslserv_p == NULL || !IsService(saslserv_p)) | |
96 | { | |
97 | sendto_one(source_p, form_str(ERR_SASLABORTED), me.name, EmptyString(source_p->name) ? "*" : source_p->name); | |
98 | return 0; | |
99 | } | |
100 | ||
101 | if(source_p->localClient->sasl_complete) | |
102 | { | |
103 | *source_p->localClient->sasl_agent = '\0'; | |
104 | source_p->localClient->sasl_complete = 0; | |
105 | } | |
106 | ||
107 | if(strlen(parv[1]) > 400) | |
108 | { | |
109 | sendto_one(source_p, form_str(ERR_SASLTOOLONG), me.name, EmptyString(source_p->name) ? "*" : source_p->name); | |
110 | return 0; | |
111 | } | |
112 | ||
113 | if(!*source_p->id) | |
114 | { | |
115 | /* Allocate a UID. */ | |
116 | strcpy(source_p->id, generate_uid()); | |
117 | add_to_id_hash(source_p->id, source_p); | |
118 | } | |
119 | ||
120 | if(*source_p->localClient->sasl_agent) | |
121 | agent_p = find_id(source_p->localClient->sasl_agent); | |
122 | ||
123 | if(agent_p == NULL) | |
124 | { | |
125 | sendto_one(saslserv_p, ":%s ENCAP %s SASL %s %s H %s %s", | |
126 | me.id, saslserv_p->servptr->name, source_p->id, saslserv_p->id, | |
127 | source_p->host, source_p->sockhost); | |
128 | ||
129 | if (!strcmp(parv[1], "EXTERNAL") && source_p->certfp != NULL) | |
130 | sendto_one(saslserv_p, ":%s ENCAP %s SASL %s %s S %s %s", | |
131 | me.id, saslserv_p->servptr->name, source_p->id, saslserv_p->id, | |
132 | parv[1], source_p->certfp); | |
133 | else | |
134 | sendto_one(saslserv_p, ":%s ENCAP %s SASL %s %s S %s", | |
135 | me.id, saslserv_p->servptr->name, source_p->id, saslserv_p->id, | |
136 | parv[1]); | |
137 | ||
138 | rb_strlcpy(source_p->localClient->sasl_agent, saslserv_p->id, IDLEN); | |
139 | } | |
140 | else | |
141 | sendto_one(agent_p, ":%s ENCAP %s SASL %s %s C %s", | |
142 | me.id, agent_p->servptr->name, source_p->id, agent_p->id, | |
143 | parv[1]); | |
144 | source_p->localClient->sasl_out++; | |
145 | ||
146 | return 0; | |
147 | } | |
148 | ||
149 | static int | |
150 | me_sasl(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, | |
151 | int parc, const char *parv[]) | |
152 | { | |
153 | struct Client *target_p, *agent_p; | |
154 | ||
155 | /* Let propagate if not addressed to us, or if broadcast. | |
156 | * Only SASL agents can answer global requests. | |
157 | */ | |
158 | if(strncmp(parv[2], me.id, 3)) | |
159 | return 0; | |
160 | ||
161 | if((target_p = find_id(parv[2])) == NULL) | |
162 | return 0; | |
163 | ||
164 | if((agent_p = find_id(parv[1])) == NULL) | |
165 | return 0; | |
166 | ||
167 | if(source_p != agent_p->servptr) /* WTF?! */ | |
168 | return 0; | |
169 | ||
170 | /* We only accept messages from SASL agents; these must have umode +S | |
171 | * (so the server must be listed in a service{} block). | |
172 | */ | |
173 | if(!IsService(agent_p)) | |
174 | return 0; | |
175 | ||
176 | /* Reject if someone has already answered. */ | |
177 | if(*target_p->localClient->sasl_agent && strncmp(parv[1], target_p->localClient->sasl_agent, IDLEN)) | |
178 | return 0; | |
179 | else if(!*target_p->localClient->sasl_agent) | |
180 | rb_strlcpy(target_p->localClient->sasl_agent, parv[1], IDLEN); | |
181 | ||
182 | if(*parv[3] == 'C') | |
183 | sendto_one(target_p, "AUTHENTICATE %s", parv[4]); | |
184 | else if(*parv[3] == 'D') | |
185 | { | |
186 | if(*parv[4] == 'F') | |
187 | sendto_one(target_p, form_str(ERR_SASLFAIL), me.name, EmptyString(target_p->name) ? "*" : target_p->name); | |
188 | else if(*parv[4] == 'S') { | |
189 | sendto_one(target_p, form_str(RPL_SASLSUCCESS), me.name, EmptyString(target_p->name) ? "*" : target_p->name); | |
190 | target_p->localClient->sasl_complete = 1; | |
191 | ServerStats.is_ssuc++; | |
192 | } | |
193 | *target_p->localClient->sasl_agent = '\0'; /* Blank the stored agent so someone else can answer */ | |
194 | } | |
195 | else if(*parv[3] == 'M') | |
196 | sendto_one(target_p, form_str(RPL_SASLMECHS), me.name, EmptyString(target_p->name) ? "*" : target_p->name, parv[4]); | |
197 | ||
198 | return 0; | |
199 | } | |
200 | ||
201 | /* If the client never finished authenticating but is | |
202 | * registering anyway, abort the exchange. | |
203 | */ | |
204 | static void | |
205 | abort_sasl(struct Client *data) | |
206 | { | |
207 | if(data->localClient->sasl_out == 0 || data->localClient->sasl_complete) | |
208 | return; | |
209 | ||
210 | data->localClient->sasl_out = data->localClient->sasl_complete = 0; | |
211 | ServerStats.is_sbad++; | |
212 | ||
213 | if(!IsClosing(data)) | |
214 | sendto_one(data, form_str(ERR_SASLABORTED), me.name, EmptyString(data->name) ? "*" : data->name); | |
215 | ||
216 | if(*data->localClient->sasl_agent) | |
217 | { | |
218 | struct Client *agent_p = find_id(data->localClient->sasl_agent); | |
219 | if(agent_p) | |
220 | { | |
221 | sendto_one(agent_p, ":%s ENCAP %s SASL %s %s D A", me.id, agent_p->servptr->name, | |
222 | data->id, agent_p->id); | |
223 | return; | |
224 | } | |
225 | } | |
226 | ||
227 | sendto_server(NULL, NULL, CAP_TS6|CAP_ENCAP, NOCAPS, ":%s ENCAP * SASL %s * D A", me.id, | |
228 | data->id); | |
229 | } | |
230 | ||
231 | static void | |
232 | abort_sasl_exit(hook_data_client_exit *data) | |
233 | { | |
234 | if (data->target->localClient) | |
235 | abort_sasl(data->target); | |
236 | } | |
237 | ||
238 | static void | |
239 | advertise_sasl(struct Client *client_p) | |
240 | { | |
241 | if (!ConfigFileEntry.sasl_service) | |
242 | return; | |
243 | ||
244 | if (irccmp(client_p->name, ConfigFileEntry.sasl_service)) | |
245 | return; | |
246 | ||
247 | sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * NEW :sasl", me.name); | |
248 | } | |
249 | ||
250 | static void | |
251 | advertise_sasl_exit(hook_data_client_exit *data) | |
252 | { | |
253 | if (!ConfigFileEntry.sasl_service) | |
254 | return; | |
255 | ||
256 | if (irccmp(data->target->name, ConfigFileEntry.sasl_service)) | |
257 | return; | |
258 | ||
259 | sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * DEL :sasl", me.name); | |
260 | } |