]> jfr.im git - irc/rqf/shadowircd.git/blame - modules/m_sasl.c
Removal of ancient SVN ID's part one
[irc/rqf/shadowircd.git] / modules / m_sasl.c
CommitLineData
212380e3 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 *
212380e3 29 */
30
31#include "stdinc.h"
32
33#include "client.h"
34#include "hash.h"
35#include "send.h"
36#include "msg.h"
37#include "modules.h"
38#include "numeric.h"
39#include "s_serv.h"
40#include "s_stats.h"
41#include "string.h"
42
43static int mr_authenticate(struct Client *, struct Client *, int, const char **);
44static int me_sasl(struct Client *, struct Client *, int, const char **);
45
46static void abort_sasl(struct Client *);
47static void abort_sasl_exit(hook_data_client_exit *);
48
49struct Message authenticate_msgtab = {
50 "AUTHENTICATE", 0, 0, 0, MFLG_SLOW,
51 {{mr_authenticate, 2}, mg_reg, mg_ignore, mg_ignore, mg_ignore, mg_reg}
52};
53struct Message sasl_msgtab = {
54 "SASL", 0, 0, 0, MFLG_SLOW,
55 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_sasl, 5}, mg_ignore}
56};
57
58mapi_clist_av1 sasl_clist[] = {
59 &authenticate_msgtab, &sasl_msgtab, NULL
60};
61mapi_hfn_list_av1 sasl_hfnlist[] = {
62 { "new_local_user", (hookfn) abort_sasl },
63 { "client_exit", (hookfn) abort_sasl_exit },
64 { NULL, NULL }
65};
66
67DECLARE_MODULE_AV1(sasl, NULL, NULL, sasl_clist, NULL, sasl_hfnlist, "$Revision: 1409 $");
68
69static int
70mr_authenticate(struct Client *client_p, struct Client *source_p,
71 int parc, const char *parv[])
72{
73 struct Client *agent_p = NULL;
74
75 /* They really should use CAP for their own sake. */
76 if(!IsCapable(source_p, CLICAP_SASL))
77 return 0;
78
79 if (strlen(client_p->id) == 3)
80 {
81 exit_client(client_p, client_p, client_p, "Mixing client and server protocol");
82 return 0;
83 }
84
85 if(source_p->preClient->sasl_complete)
86 {
87 sendto_one(source_p, form_str(ERR_SASLALREADY), me.name, EmptyString(source_p->name) ? "*" : source_p->name);
88 return 0;
89 }
90
91 if(strlen(parv[1]) > 400)
92 {
93 sendto_one(source_p, form_str(ERR_SASLTOOLONG), me.name, EmptyString(source_p->name) ? "*" : source_p->name);
94 return 0;
95 }
96
97 if(!*source_p->id)
98 {
99 /* Allocate a UID. */
100 strcpy(source_p->id, generate_uid());
101 add_to_id_hash(source_p->id, source_p);
102 }
103
104 if(*source_p->preClient->sasl_agent)
105 agent_p = find_id(source_p->preClient->sasl_agent);
106
107 if(agent_p == NULL)
108 sendto_server(NULL, NULL, CAP_TS6|CAP_ENCAP, NOCAPS, ":%s ENCAP * SASL %s * S %s", me.id,
109 source_p->id, parv[1]);
110 else
111 sendto_one(agent_p, ":%s ENCAP %s SASL %s %s C %s", me.id, agent_p->servptr->name,
112 source_p->id, agent_p->id, parv[1]);
113 source_p->preClient->sasl_out++;
114
115 return 0;
116}
117
118static int
119me_sasl(struct Client *client_p, struct Client *source_p,
120 int parc, const char *parv[])
121{
122 struct Client *target_p, *agent_p;
123
124 /* Let propagate if not addressed to us, or if broadcast.
125 * Only SASL agents can answer global requests.
126 */
127 if(strncmp(parv[2], me.id, 3))
128 return 0;
129
130 if((target_p = find_id(parv[2])) == NULL)
131 return 0;
132
133 if(target_p->preClient == NULL)
134 return 0;
135
136 if((agent_p = find_id(parv[1])) == NULL)
137 return 0;
138
139 if(source_p != agent_p->servptr) /* WTF?! */
140 return 0;
141
142 /* We only accept messages from SASL agents; these must have umode +S
143 * (so the server must be listed in a service{} block).
144 */
145 if(!IsService(agent_p))
146 return 0;
147
148 /* Reject if someone has already answered. */
149 if(*target_p->preClient->sasl_agent && strncmp(parv[1], target_p->preClient->sasl_agent, IDLEN))
150 return 0;
151 else if(!*target_p->preClient->sasl_agent)
907468c4 152 rb_strlcpy(target_p->preClient->sasl_agent, parv[1], IDLEN);
212380e3 153
154 if(*parv[3] == 'C')
155 sendto_one(target_p, "AUTHENTICATE %s", parv[4]);
156 else if(*parv[3] == 'D')
157 {
158 if(*parv[4] == 'F')
159 sendto_one(target_p, form_str(ERR_SASLFAIL), me.name, EmptyString(target_p->name) ? "*" : target_p->name);
160 else if(*parv[4] == 'S') {
161 sendto_one(target_p, form_str(RPL_SASLSUCCESS), me.name, EmptyString(target_p->name) ? "*" : target_p->name);
162 target_p->preClient->sasl_complete = 1;
83251205 163 ServerStats.is_ssuc++;
212380e3 164 }
165 *target_p->preClient->sasl_agent = '\0'; /* Blank the stored agent so someone else can answer */
166 }
167
168 return 0;
169}
170
171/* If the client never finished authenticating but is
172 * registering anyway, abort the exchange.
173 */
174static void
175abort_sasl(struct Client *data)
176{
177 if(data->preClient->sasl_out == 0 || data->preClient->sasl_complete)
178 return;
179
180 data->preClient->sasl_out = data->preClient->sasl_complete = 0;
83251205 181 ServerStats.is_sbad++;
212380e3 182
183 if(!IsClosing(data))
184 sendto_one(data, form_str(ERR_SASLABORTED), me.name, EmptyString(data->name) ? "*" : data->name);
185
186 if(*data->preClient->sasl_agent)
187 {
188 struct Client *agent_p = find_id(data->preClient->sasl_agent);
189 if(agent_p)
190 {
191 sendto_one(agent_p, ":%s ENCAP %s SASL %s %s D A", me.id, agent_p->servptr->name,
192 data->id, agent_p->id);
193 return;
194 }
195 }
196
197 sendto_server(NULL, NULL, CAP_TS6|CAP_ENCAP, NOCAPS, ":%s ENCAP * SASL %s * D A", me.id,
198 data->id);
199}
200
201static void
202abort_sasl_exit(hook_data_client_exit *data)
203{
204 if (data->target->preClient)
205 abort_sasl(data->target);
206}
207