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