]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
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 | * $Id: m_signon.c 1192 2006-04-21 16:21:02Z jilles $ | |
30 | */ | |
31 | ||
32 | #include "stdinc.h" | |
33 | ||
212380e3 AC |
34 | #include "send.h" |
35 | #include "channel.h" | |
36 | #include "client.h" | |
37 | #include "common.h" | |
38 | #include "config.h" | |
39 | #include "ircd.h" | |
40 | #include "numeric.h" | |
212380e3 AC |
41 | #include "s_conf.h" |
42 | #include "s_serv.h" | |
43 | #include "hash.h" | |
44 | #include "msg.h" | |
45 | #include "parse.h" | |
46 | #include "modules.h" | |
212380e3 AC |
47 | #include "whowas.h" |
48 | #include "monitor.h" | |
49 | #include "s_stats.h" | |
50 | #include "snomask.h" | |
4562c604 | 51 | #include "match.h" |
212380e3 AC |
52 | #include "s_user.h" |
53 | ||
54 | static int me_svslogin(struct Client *, struct Client *, int, const char **); | |
55 | static int ms_signon(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, MFLG_SLOW, | |
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, MFLG_SLOW, | |
65 | {mg_ignore, mg_ignore, {ms_signon, 6}, mg_ignore, mg_ignore, mg_ignore} | |
66 | }; | |
67 | ||
55abcbb2 | 68 | mapi_clist_av1 signon_clist[] = { |
212380e3 AC |
69 | &svslogin_msgtab, &signon_msgtab, NULL |
70 | }; | |
71 | ||
72 | DECLARE_MODULE_AV1(signon, NULL, NULL, signon_clist, NULL, NULL, "$Revision: 1192 $"); | |
73 | ||
74 | #define NICK_VALID 1 | |
75 | #define USER_VALID 2 | |
76 | #define HOST_VALID 4 | |
77 | ||
212380e3 AC |
78 | static int |
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 0; | |
89 | } | |
90 | ||
91 | if(len > USERLEN) | |
92 | return 0; | |
93 | ||
94 | return 1; | |
95 | } | |
96 | ||
97 | static int | |
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 0; | |
108 | } | |
109 | ||
110 | if(len > HOSTLEN) | |
111 | return 0; | |
112 | ||
113 | return 1; | |
114 | } | |
115 | ||
116 | static int | |
117 | me_svslogin(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)) | |
ec57fe67 KB |
126 | { |
127 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
128 | "Non-service server %s attempting to execute services-only command SVSLOGIN", source_p->name); | |
212380e3 | 129 | return 0; |
ec57fe67 | 130 | } |
212380e3 AC |
131 | |
132 | if((target_p = find_client(parv[1])) == NULL) | |
133 | return 0; | |
134 | ||
135 | if(!MyClient(target_p) && !IsUnknown(target_p)) | |
136 | return 0; | |
137 | ||
2d28539c | 138 | if(clean_nick(parv[2], 0)) |
212380e3 | 139 | { |
f427c8b0 | 140 | rb_strlcpy(nick, parv[2], NICKLEN + 1); |
212380e3 AC |
141 | valid |= NICK_VALID; |
142 | } | |
143 | else if(*target_p->name) | |
f427c8b0 | 144 | rb_strlcpy(nick, target_p->name, NICKLEN + 1); |
212380e3 AC |
145 | else |
146 | strcpy(nick, "*"); | |
147 | ||
148 | if(clean_username(parv[3])) | |
149 | { | |
f427c8b0 | 150 | rb_strlcpy(user, parv[3], USERLEN + 1); |
212380e3 AC |
151 | valid |= USER_VALID; |
152 | } | |
153 | else | |
f427c8b0 | 154 | rb_strlcpy(user, target_p->username, USERLEN + 1); |
212380e3 AC |
155 | |
156 | if(clean_host(parv[4])) | |
157 | { | |
f427c8b0 | 158 | rb_strlcpy(host, parv[4], HOSTLEN + 1); |
212380e3 AC |
159 | valid |= HOST_VALID; |
160 | } | |
161 | else | |
f427c8b0 | 162 | rb_strlcpy(host, target_p->host, HOSTLEN + 1); |
212380e3 AC |
163 | |
164 | if(*parv[5] == '*') | |
165 | { | |
166 | if(target_p->user) | |
f427c8b0 | 167 | rb_strlcpy(login, target_p->user->suser, NICKLEN + 1); |
212380e3 AC |
168 | else |
169 | login[0] = '\0'; | |
170 | } | |
171 | else if(!strcmp(parv[5], "0")) | |
172 | login[0] = '\0'; | |
173 | else | |
f427c8b0 | 174 | rb_strlcpy(login, parv[5], NICKLEN + 1); |
212380e3 AC |
175 | |
176 | /* Login (mostly) follows nick rules. */ | |
2d28539c | 177 | if(*login && !clean_nick(login, 0)) |
212380e3 AC |
178 | return 0; |
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); | |
45ed8835 JT |
191 | sendto_realops_snomask(SNO_SKILL, L_ALL, |
192 | "Nick collision due to SVSLOGIN on %s", | |
193 | nick); | |
212380e3 | 194 | |
c2f73e5d | 195 | rb_snprintf(buf, sizeof(buf), "Killed (%s (Nickname regained by services))", |
212380e3 AC |
196 | me.name); |
197 | exit_client(NULL, exist_p, &me, buf); | |
198 | }else if((exist_p = find_client(nick)) && IsUnknown(exist_p) && exist_p != target_p) { | |
199 | exit_client(NULL, exist_p, &me, "Overridden"); | |
200 | } | |
201 | ||
202 | if(*login) | |
203 | { | |
204 | /* Strip leading digits, unless it's purely numeric. */ | |
205 | const char *p = login; | |
206 | while(IsDigit(*p)) | |
207 | p++; | |
208 | if(!*p) | |
209 | p = login; | |
210 | ||
211 | sendto_one(target_p, form_str(RPL_LOGGEDIN), me.name, EmptyString(target_p->name) ? "*" : target_p->name, | |
212 | nick, user, host, p, p); | |
213 | } | |
214 | else | |
215 | sendto_one(target_p, form_str(RPL_LOGGEDOUT), me.name, EmptyString(target_p->name) ? "*" : target_p->name, | |
216 | nick, user, host); | |
217 | ||
218 | if(IsUnknown(target_p)) | |
219 | { | |
220 | struct User *user_p = make_user(target_p); | |
221 | ||
222 | if(valid & NICK_VALID) | |
223 | strcpy(target_p->preClient->spoofnick, nick); | |
224 | ||
225 | if(valid & USER_VALID) | |
226 | strcpy(target_p->preClient->spoofuser, user); | |
227 | ||
228 | if(valid & HOST_VALID) | |
229 | strcpy(target_p->preClient->spoofhost, host); | |
230 | ||
f427c8b0 | 231 | rb_strlcpy(user_p->suser, login, NICKLEN + 1); |
212380e3 AC |
232 | } |
233 | else | |
234 | { | |
cf9a6b75 AC |
235 | char note[NICKLEN + 10]; |
236 | ||
e3354945 | 237 | send_signon(NULL, target_p, nick, user, host, rb_current_time(), login); |
cf9a6b75 AC |
238 | |
239 | rb_snprintf(note, NICKLEN + 10, "Nick: %s", target_p->name); | |
240 | rb_note(target_p->localClient->F, note); | |
212380e3 AC |
241 | } |
242 | ||
243 | return 0; | |
244 | } | |
245 | ||
246 | static int | |
247 | ms_signon(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 | ||
2d28539c | 254 | if(!clean_nick(parv[1], 0)) |
212380e3 | 255 | { |
47adde3d | 256 | ServerStats.is_kill++; |
212380e3 AC |
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 0; | |
270 | } | |
271 | ||
272 | if(!clean_username(parv[2]) || !clean_host(parv[3])) | |
273 | { | |
47adde3d | 274 | ServerStats.is_kill++; |
212380e3 AC |
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 0; | |
288 | } | |
289 | ||
290 | newts = atol(parv[4]); | |
291 | ||
292 | if(!strcmp(parv[5], "0")) | |
293 | login[0] = '\0'; | |
294 | else if(*parv[5] != '*') | |
295 | { | |
2d28539c | 296 | if (clean_nick(parv[5], 0)) |
f427c8b0 | 297 | rb_strlcpy(login, parv[5], NICKLEN + 1); |
212380e3 AC |
298 | else |
299 | return 0; | |
300 | } | |
299e25a6 JT |
301 | else |
302 | login[0] = '\0'; | |
212380e3 AC |
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); | |
55abcbb2 | 319 | |
47adde3d | 320 | ServerStats.is_kill++; |
212380e3 AC |
321 | sendto_one_numeric(target_p, ERR_NICKCOLLISION, |
322 | form_str(ERR_NICKCOLLISION), target_p->name); | |
55abcbb2 | 323 | |
212380e3 | 324 | kill_client_serv_butone(NULL, source_p, "%s (Nick change collision)", me.name); |
55abcbb2 | 325 | |
47adde3d | 326 | ServerStats.is_kill++; |
55abcbb2 | 327 | |
212380e3 | 328 | kill_client_serv_butone(NULL, target_p, "%s (Nick change collision)", me.name); |
55abcbb2 | 329 | |
212380e3 AC |
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 0; | |
335 | } | |
336 | else | |
337 | { | |
338 | sameuser = !irccmp(target_p->username, source_p->username) && | |
339 | !irccmp(target_p->host, source_p->host); | |
55abcbb2 | 340 | |
212380e3 AC |
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); | |
55abcbb2 | 354 | |
47adde3d | 355 | ServerStats.is_kill++; |
55abcbb2 | 356 | |
212380e3 AC |
357 | sendto_one_numeric(target_p, ERR_NICKCOLLISION, |
358 | form_str(ERR_NICKCOLLISION), target_p->name); | |
55abcbb2 | 359 | |
212380e3 AC |
360 | /* kill the client issuing the nickchange */ |
361 | kill_client_serv_butone(client_p, source_p, | |
362 | "%s (Nick change collision)", me.name); | |
55abcbb2 | 363 | |
212380e3 | 364 | source_p->flags |= FLAGS_KILLED; |
55abcbb2 | 365 | |
212380e3 AC |
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 0; | |
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); | |
55abcbb2 | 384 | |
212380e3 AC |
385 | sendto_one_numeric(target_p, ERR_NICKCOLLISION, |
386 | form_str(ERR_NICKCOLLISION), target_p->name); | |
55abcbb2 | 387 | |
212380e3 | 388 | /* kill the client who existed before hand */ |
55abcbb2 | 389 | kill_client_serv_butone(client_p, target_p, |
212380e3 | 390 | "%s (Nick collision)", me.name); |
55abcbb2 | 391 | |
47adde3d | 392 | ServerStats.is_kill++; |
55abcbb2 | 393 | |
212380e3 AC |
394 | target_p->flags |= FLAGS_KILLED; |
395 | (void) exit_client(client_p, target_p, &me, "Nick collision"); | |
396 | } | |
397 | } | |
55abcbb2 | 398 | |
212380e3 AC |
399 | } |
400 | } | |
401 | ||
402 | send_signon(client_p, source_p, parv[1], parv[2], parv[3], newts, login); | |
403 | return 0; | |
404 | } | |
405 | ||
406 | static void | |
407 | send_signon(struct Client *client_p, struct Client *target_p, | |
408 | const char *nick, const char *user, const char *host, | |
409 | unsigned int newts, const char *login) | |
410 | { | |
411 | sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s SIGNON %s %s %s %ld %s", | |
412 | use_id(target_p), nick, user, host, | |
413 | (long) target_p->tsinfo, *login ? login : "0"); | |
212380e3 AC |
414 | |
415 | strcpy(target_p->user->suser, login); | |
416 | ||
417 | change_nick_user_host(target_p, nick, user, host, newts, "Signing %s (%s)", *login ? "in" : "out", nick); | |
418 | } | |
419 |