]>
Commit | Line | Data |
---|---|---|
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 | ||
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" | |
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" | |
47 | #include "whowas.h" | |
48 | #include "monitor.h" | |
49 | #include "s_stats.h" | |
50 | #include "snomask.h" | |
51 | #include "match.h" | |
52 | #include "s_user.h" | |
53 | ||
54 | static int me_svslogin(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
55 | static int 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_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 | ||
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 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 0; | |
130 | } | |
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 | ||
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 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); | |
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 | }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 | ||
231 | rb_strlcpy(user_p->suser, login, NICKLEN + 1); | |
232 | } | |
233 | else | |
234 | { | |
235 | char note[NICKLEN + 10]; | |
236 | ||
237 | send_signon(NULL, target_p, nick, user, host, rb_current_time(), login); | |
238 | ||
239 | snprintf(note, NICKLEN + 10, "Nick: %s", target_p->name); | |
240 | rb_note(target_p->localClient->F, note); | |
241 | } | |
242 | ||
243 | return 0; | |
244 | } | |
245 | ||
246 | static int | |
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 0; | |
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 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 | { | |
296 | if (clean_nick(parv[5], 0)) | |
297 | rb_strlcpy(login, parv[5], NICKLEN + 1); | |
298 | else | |
299 | return 0; | |
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 0; | |
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 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); | |
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 | 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"); | |
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 | } |