]> jfr.im git - solanum.git/blob - modules/m_signon.c
Merge branch 'master' into authd-framework-2
[solanum.git] / modules / m_signon.c
1 /* modules/m_signon.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
30 #include "stdinc.h"
31
32 #include "send.h"
33 #include "channel.h"
34 #include "client.h"
35 #include "common.h"
36 #include "defaults.h"
37 #include "ircd.h"
38 #include "numeric.h"
39 #include "s_conf.h"
40 #include "s_serv.h"
41 #include "hash.h"
42 #include "msg.h"
43 #include "parse.h"
44 #include "modules.h"
45 #include "whowas.h"
46 #include "monitor.h"
47 #include "s_stats.h"
48 #include "snomask.h"
49 #include "match.h"
50 #include "s_user.h"
51
52 static const char signon_desc[] = "Provides account login/logout support for services";
53
54 static void me_svslogin(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
55 static void ms_signon(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
56
57 static void send_signon(struct Client *, struct Client *, const char *, const char *, const char *, unsigned int, const char *);
58
59 struct Message svslogin_msgtab = {
60 "SVSLOGIN", 0, 0, 0, 0,
61 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_svslogin, 6}, mg_ignore}
62 };
63 struct Message signon_msgtab = {
64 "SIGNON", 0, 0, 0, 0,
65 {mg_ignore, mg_ignore, {ms_signon, 6}, mg_ignore, mg_ignore, mg_ignore}
66 };
67
68 mapi_clist_av1 signon_clist[] = {
69 &svslogin_msgtab, &signon_msgtab, NULL
70 };
71
72 DECLARE_MODULE_AV2(signon, NULL, NULL, signon_clist, NULL, NULL, NULL, NULL, signon_desc);
73
74 #define NICK_VALID 1
75 #define USER_VALID 2
76 #define HOST_VALID 4
77
78 static bool
79 clean_username(const char *username)
80 {
81 int len = 0;
82
83 for (; *username; username++)
84 {
85 len++;
86
87 if(!IsUserChar(*username))
88 return false;
89 }
90
91 if(len > USERLEN)
92 return false;
93
94 return true;
95 }
96
97 static bool
98 clean_host(const char *host)
99 {
100 int len = 0;
101
102 for (; *host; host++)
103 {
104 len++;
105
106 if(!IsHostChar(*host))
107 return false;
108 }
109
110 if(len > HOSTLEN)
111 return false;
112
113 return true;
114 }
115
116 static void
117 me_svslogin(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
118 int parc, const char *parv[])
119 {
120 struct Client *target_p, *exist_p;
121 char nick[NICKLEN+1], login[NICKLEN+1];
122 char user[USERLEN+1], host[HOSTLEN+1];
123 int valid = 0;
124
125 if(!(source_p->flags & FLAGS_SERVICE))
126 {
127 sendto_realops_snomask(SNO_GENERAL, L_ALL,
128 "Non-service server %s attempting to execute services-only command SVSLOGIN", source_p->name);
129 return;
130 }
131
132 if((target_p = find_client(parv[1])) == NULL)
133 return;
134
135 if(!MyClient(target_p) && !IsUnknown(target_p))
136 return;
137
138 if(clean_nick(parv[2], 0))
139 {
140 rb_strlcpy(nick, parv[2], NICKLEN + 1);
141 valid |= NICK_VALID;
142 }
143 else if(*target_p->name)
144 rb_strlcpy(nick, target_p->name, NICKLEN + 1);
145 else
146 strcpy(nick, "*");
147
148 if(clean_username(parv[3]))
149 {
150 rb_strlcpy(user, parv[3], USERLEN + 1);
151 valid |= USER_VALID;
152 }
153 else
154 rb_strlcpy(user, target_p->username, USERLEN + 1);
155
156 if(clean_host(parv[4]))
157 {
158 rb_strlcpy(host, parv[4], HOSTLEN + 1);
159 valid |= HOST_VALID;
160 }
161 else
162 rb_strlcpy(host, target_p->host, HOSTLEN + 1);
163
164 if(*parv[5] == '*')
165 {
166 if(target_p->user)
167 rb_strlcpy(login, target_p->user->suser, NICKLEN + 1);
168 else
169 login[0] = '\0';
170 }
171 else if(!strcmp(parv[5], "0"))
172 login[0] = '\0';
173 else
174 rb_strlcpy(login, parv[5], NICKLEN + 1);
175
176 /* Login (mostly) follows nick rules. */
177 if(*login && !clean_nick(login, 0))
178 return;
179
180 if((exist_p = find_person(nick)) && target_p != exist_p)
181 {
182 char buf[BUFSIZE];
183
184 if(MyClient(exist_p))
185 sendto_one(exist_p, ":%s KILL %s :(Nickname regained by services)",
186 me.name, exist_p->name);
187
188 exist_p->flags |= FLAGS_KILLED;
189 kill_client_serv_butone(NULL, exist_p, "%s (Nickname regained by services)",
190 me.name);
191 sendto_realops_snomask(SNO_SKILL, L_ALL,
192 "Nick collision due to SVSLOGIN on %s",
193 nick);
194
195 snprintf(buf, sizeof(buf), "Killed (%s (Nickname regained by services))",
196 me.name);
197 exit_client(NULL, exist_p, &me, buf);
198 }
199 else if((exist_p = find_client(nick)) && IsUnknown(exist_p) && exist_p != target_p)
200 {
201 exit_client(NULL, exist_p, &me, "Overridden");
202 }
203
204 if(*login)
205 {
206 /* Strip leading digits, unless it's purely numeric. */
207 const char *p = login;
208 while(IsDigit(*p))
209 p++;
210 if(!*p)
211 p = login;
212
213 sendto_one(target_p, form_str(RPL_LOGGEDIN), me.name, EmptyString(target_p->name) ? "*" : target_p->name,
214 nick, user, host, p, p);
215 }
216 else
217 sendto_one(target_p, form_str(RPL_LOGGEDOUT), me.name, EmptyString(target_p->name) ? "*" : target_p->name,
218 nick, user, host);
219
220 if(IsUnknown(target_p))
221 {
222 struct User *user_p = make_user(target_p);
223
224 if(valid & NICK_VALID)
225 strcpy(target_p->preClient->spoofnick, nick);
226
227 if(valid & USER_VALID)
228 strcpy(target_p->preClient->spoofuser, user);
229
230 if(valid & HOST_VALID)
231 strcpy(target_p->preClient->spoofhost, host);
232
233 rb_strlcpy(user_p->suser, login, NICKLEN + 1);
234 }
235 else
236 {
237 char note[NICKLEN + 10];
238
239 send_signon(NULL, target_p, nick, user, host, rb_current_time(), login);
240
241 snprintf(note, NICKLEN + 10, "Nick: %s", target_p->name);
242 rb_note(target_p->localClient->F, note);
243 }
244 }
245
246 static void
247 ms_signon(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
248 int parc, const char *parv[])
249 {
250 struct Client *target_p;
251 int newts, sameuser;
252 char login[NICKLEN+1];
253
254 if(!clean_nick(parv[1], 0))
255 {
256 ServerStats.is_kill++;
257 sendto_realops_snomask(SNO_DEBUG, L_ALL,
258 "Bad Nick from SIGNON: %s From: %s(via %s)",
259 parv[1], source_p->servptr->name, client_p->name);
260 /* if source_p has an id, kill_client_serv_butone() will
261 * send a kill to client_p, otherwise do it here */
262 if (!has_id(source_p))
263 sendto_one(client_p, ":%s KILL %s :%s (Bad nickname from SIGNON)",
264 get_id(&me, client_p), parv[1], me.name);
265 kill_client_serv_butone(client_p, source_p, "%s (Bad nickname from SIGNON)",
266 me.name);
267 source_p->flags |= FLAGS_KILLED;
268 exit_client(NULL, source_p, &me, "Bad nickname from SIGNON");
269 return;
270 }
271
272 if(!clean_username(parv[2]) || !clean_host(parv[3]))
273 {
274 ServerStats.is_kill++;
275 sendto_realops_snomask(SNO_DEBUG, L_ALL,
276 "Bad user@host from SIGNON: %s@%s From: %s(via %s)",
277 parv[2], parv[3], source_p->servptr->name, client_p->name);
278 /* if source_p has an id, kill_client_serv_butone() will
279 * send a kill to client_p, otherwise do it here */
280 if (!has_id(source_p))
281 sendto_one(client_p, ":%s KILL %s :%s (Bad user@host from SIGNON)",
282 get_id(&me, client_p), parv[1], me.name);
283 kill_client_serv_butone(client_p, source_p, "%s (Bad user@host from SIGNON)",
284 me.name);
285 source_p->flags |= FLAGS_KILLED;
286 exit_client(NULL, source_p, &me, "Bad user@host from SIGNON");
287 return;
288 }
289
290 newts = atol(parv[4]);
291
292 if(!strcmp(parv[5], "0"))
293 login[0] = '\0';
294 else if(*parv[5] != '*')
295 {
296 if (clean_nick(parv[5], 0))
297 rb_strlcpy(login, parv[5], NICKLEN + 1);
298 else
299 return;
300 }
301 else
302 login[0] = '\0';
303
304 target_p = find_named_client(parv[1]);
305 if(target_p != NULL && target_p != source_p)
306 {
307 /* In case of collision, follow NICK rules. */
308 /* XXX this is duplicated code and does not do SAVE */
309 if(IsUnknown(target_p))
310 exit_client(NULL, target_p, &me, "Overridden");
311 else
312 {
313 if(!newts || !target_p->tsinfo || (newts == target_p->tsinfo) || !source_p->user)
314 {
315 sendto_realops_snomask(SNO_GENERAL, L_ALL,
316 "Nick change collision from SIGNON from %s to %s(%s <- %s)(both killed)",
317 source_p->name, target_p->name, target_p->from->name,
318 client_p->name);
319
320 ServerStats.is_kill++;
321 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
322 form_str(ERR_NICKCOLLISION), target_p->name);
323
324 kill_client_serv_butone(NULL, source_p, "%s (Nick change collision)", me.name);
325
326 ServerStats.is_kill++;
327
328 kill_client_serv_butone(NULL, target_p, "%s (Nick change collision)", me.name);
329
330 target_p->flags |= FLAGS_KILLED;
331 exit_client(NULL, target_p, &me, "Nick collision(new)");
332 source_p->flags |= FLAGS_KILLED;
333 exit_client(client_p, source_p, &me, "Nick collision(old)");
334 return;
335 }
336 else
337 {
338 sameuser = !irccmp(target_p->username, source_p->username) &&
339 !irccmp(target_p->host, source_p->host);
340
341 if((sameuser && newts < target_p->tsinfo) ||
342 (!sameuser && newts > target_p->tsinfo))
343 {
344 if(sameuser)
345 sendto_realops_snomask(SNO_GENERAL, L_ALL,
346 "Nick change collision from SIGNON from %s to %s(%s <- %s)(older killed)",
347 source_p->name, target_p->name,
348 target_p->from->name, client_p->name);
349 else
350 sendto_realops_snomask(SNO_GENERAL, L_ALL,
351 "Nick change collision from SIGNON from %s to %s(%s <- %s)(newer killed)",
352 source_p->name, target_p->name,
353 target_p->from->name, client_p->name);
354
355 ServerStats.is_kill++;
356
357 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
358 form_str(ERR_NICKCOLLISION), target_p->name);
359
360 /* kill the client issuing the nickchange */
361 kill_client_serv_butone(client_p, source_p,
362 "%s (Nick change collision)", me.name);
363
364 source_p->flags |= FLAGS_KILLED;
365
366 if(sameuser)
367 exit_client(client_p, source_p, &me, "Nick collision(old)");
368 else
369 exit_client(client_p, source_p, &me, "Nick collision(new)");
370 return;
371 }
372 else
373 {
374 if(sameuser)
375 sendto_realops_snomask(SNO_GENERAL, L_ALL,
376 "Nick collision from SIGNON on %s(%s <- %s)(older killed)",
377 target_p->name, target_p->from->name,
378 client_p->name);
379 else
380 sendto_realops_snomask(SNO_GENERAL, L_ALL,
381 "Nick collision from SIGNON on %s(%s <- %s)(newer killed)",
382 target_p->name, target_p->from->name,
383 client_p->name);
384
385 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
386 form_str(ERR_NICKCOLLISION), target_p->name);
387
388 /* kill the client who existed before hand */
389 kill_client_serv_butone(client_p, target_p,
390 "%s (Nick collision)", me.name);
391
392 ServerStats.is_kill++;
393
394 target_p->flags |= FLAGS_KILLED;
395 (void) exit_client(client_p, target_p, &me, "Nick collision");
396 }
397 }
398
399 }
400 }
401
402 send_signon(client_p, source_p, parv[1], parv[2], parv[3], newts, login);
403 }
404
405 static void
406 send_signon(struct Client *client_p, struct Client *target_p,
407 const char *nick, const char *user, const char *host,
408 unsigned int newts, const char *login)
409 {
410 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s SIGNON %s %s %s %ld %s",
411 use_id(target_p), nick, user, host,
412 (long) target_p->tsinfo, *login ? login : "0");
413
414 strcpy(target_p->user->suser, login);
415
416 change_nick_user_host(target_p, nick, user, host, newts, "Signing %s (%s)", *login ? "in" : "out", nick);
417 }