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