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