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