2 * Copyright (C) 2006 Michael Tharp <gxti@partiallystapled.com>
3 * Copyright (C) 2006 charybdis development team
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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.
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.
29 * $Id: m_signon.c 1192 2006-04-21 16:21:02Z jilles $
54 static int me_svslogin(struct Client
*, struct Client
*, int, const char **);
55 static int ms_signon(struct Client
*, struct Client
*, int, const char **);
57 static void send_signon(struct Client
*, struct Client
*, const char *, const char *, const char *, unsigned int, const char *);
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
}
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
}
68 mapi_clist_av1 signon_clist
[] = {
69 &svslogin_msgtab
, &signon_msgtab
, NULL
72 DECLARE_MODULE_AV1(signon
, NULL
, NULL
, signon_clist
, NULL
, NULL
, "$Revision: 1192 $");
79 clean_nick(const char *nick
)
86 /* This is used to check logins, which are often
87 * numeric. Don't check for leading digits, if
88 * services wants to set someone's nick to something
89 * starting with a number, let it try.
96 if(!IsNickChar(*nick
))
108 clean_username(const char *username
)
112 for (; *username
; username
++)
116 if(!IsUserChar(*username
))
127 clean_host(const char *host
)
131 for (; *host
; host
++)
135 if(!IsHostChar(*host
))
146 me_svslogin(struct Client
*client_p
, struct Client
*source_p
,
147 int parc
, const char *parv
[])
149 struct Client
*target_p
, *exist_p
;
150 char nick
[NICKLEN
+1], login
[NICKLEN
+1];
151 char user
[USERLEN
+1], host
[HOSTLEN
+1];
154 if(!(source_p
->flags
& FLAGS_SERVICE
))
157 if((target_p
= find_client(parv
[1])) == NULL
)
160 if(!MyClient(target_p
) && !IsUnknown(target_p
))
163 if(clean_nick(parv
[2]))
165 rb_strlcpy(nick
, parv
[2], NICKLEN
+ 1);
168 else if(*target_p
->name
)
169 rb_strlcpy(nick
, target_p
->name
, NICKLEN
+ 1);
173 if(clean_username(parv
[3]))
175 rb_strlcpy(user
, parv
[3], USERLEN
+ 1);
179 rb_strlcpy(user
, target_p
->username
, USERLEN
+ 1);
181 if(clean_host(parv
[4]))
183 rb_strlcpy(host
, parv
[4], HOSTLEN
+ 1);
187 rb_strlcpy(host
, target_p
->host
, HOSTLEN
+ 1);
192 rb_strlcpy(login
, target_p
->user
->suser
, NICKLEN
+ 1);
196 else if(!strcmp(parv
[5], "0"))
199 rb_strlcpy(login
, parv
[5], NICKLEN
+ 1);
201 /* Login (mostly) follows nick rules. */
202 if(*login
&& !clean_nick(login
))
205 if((exist_p
= find_person(nick
)) && target_p
!= exist_p
)
209 if(MyClient(exist_p
))
210 sendto_one(exist_p
, ":%s KILL %s :(Nickname regained by services)",
211 me
.name
, exist_p
->name
);
213 exist_p
->flags
|= FLAGS_KILLED
;
214 kill_client_serv_butone(NULL
, exist_p
, "%s (Nickname regained by services)",
217 rb_snprintf(buf
, sizeof(buf
), "Killed (%s (Nickname regained by services))",
219 exit_client(NULL
, exist_p
, &me
, buf
);
220 }else if((exist_p
= find_client(nick
)) && IsUnknown(exist_p
) && exist_p
!= target_p
) {
221 exit_client(NULL
, exist_p
, &me
, "Overridden");
226 /* Strip leading digits, unless it's purely numeric. */
227 const char *p
= login
;
233 sendto_one(target_p
, form_str(RPL_LOGGEDIN
), me
.name
, EmptyString(target_p
->name
) ? "*" : target_p
->name
,
234 nick
, user
, host
, p
, p
);
237 sendto_one(target_p
, form_str(RPL_LOGGEDOUT
), me
.name
, EmptyString(target_p
->name
) ? "*" : target_p
->name
,
240 if(IsUnknown(target_p
))
242 struct User
*user_p
= make_user(target_p
);
244 if(valid
& NICK_VALID
)
245 strcpy(target_p
->preClient
->spoofnick
, nick
);
247 if(valid
& USER_VALID
)
248 strcpy(target_p
->preClient
->spoofuser
, user
);
250 if(valid
& HOST_VALID
)
251 strcpy(target_p
->preClient
->spoofhost
, host
);
253 rb_strlcpy(user_p
->suser
, login
, NICKLEN
+ 1);
257 char note
[NICKLEN
+ 10];
259 send_signon(NULL
, target_p
, nick
, user
, host
, rb_current_time(), login
);
261 rb_snprintf(note
, NICKLEN
+ 10, "Nick: %s", target_p
->name
);
262 rb_note(target_p
->localClient
->F
, note
);
269 ms_signon(struct Client
*client_p
, struct Client
*source_p
,
270 int parc
, const char *parv
[])
272 struct Client
*target_p
;
274 char login
[NICKLEN
+1];
276 if(!clean_nick(parv
[1]))
278 ServerStats
.is_kill
++;
279 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
280 "Bad Nick from SIGNON: %s From: %s(via %s)",
281 parv
[1], source_p
->servptr
->name
, client_p
->name
);
282 /* if source_p has an id, kill_client_serv_butone() will
283 * send a kill to client_p, otherwise do it here */
284 if (!has_id(source_p
))
285 sendto_one(client_p
, ":%s KILL %s :%s (Bad nickname from SIGNON)",
286 get_id(&me
, client_p
), parv
[1], me
.name
);
287 kill_client_serv_butone(client_p
, source_p
, "%s (Bad nickname from SIGNON)",
289 source_p
->flags
|= FLAGS_KILLED
;
290 exit_client(NULL
, source_p
, &me
, "Bad nickname from SIGNON");
294 if(!clean_username(parv
[2]) || !clean_host(parv
[3]))
296 ServerStats
.is_kill
++;
297 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
298 "Bad user@host from SIGNON: %s@%s From: %s(via %s)",
299 parv
[2], parv
[3], source_p
->servptr
->name
, client_p
->name
);
300 /* if source_p has an id, kill_client_serv_butone() will
301 * send a kill to client_p, otherwise do it here */
302 if (!has_id(source_p
))
303 sendto_one(client_p
, ":%s KILL %s :%s (Bad user@host from SIGNON)",
304 get_id(&me
, client_p
), parv
[1], me
.name
);
305 kill_client_serv_butone(client_p
, source_p
, "%s (Bad user@host from SIGNON)",
307 source_p
->flags
|= FLAGS_KILLED
;
308 exit_client(NULL
, source_p
, &me
, "Bad user@host from SIGNON");
312 newts
= atol(parv
[4]);
314 if(!strcmp(parv
[5], "0"))
316 else if(*parv
[5] != '*')
318 if (clean_nick(parv
[5]))
319 rb_strlcpy(login
, parv
[5], NICKLEN
+ 1);
324 target_p
= find_named_client(parv
[1]);
325 if(target_p
!= NULL
&& target_p
!= source_p
)
327 /* In case of collision, follow NICK rules. */
328 /* XXX this is duplicated code and does not do SAVE */
329 if(IsUnknown(target_p
))
330 exit_client(NULL
, target_p
, &me
, "Overridden");
333 if(!newts
|| !target_p
->tsinfo
|| (newts
== target_p
->tsinfo
) || !source_p
->user
)
335 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
336 "Nick change collision from SIGNON from %s to %s(%s <- %s)(both killed)",
337 source_p
->name
, target_p
->name
, target_p
->from
->name
,
340 ServerStats
.is_kill
++;
341 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
342 form_str(ERR_NICKCOLLISION
), target_p
->name
);
344 kill_client_serv_butone(NULL
, source_p
, "%s (Nick change collision)", me
.name
);
346 ServerStats
.is_kill
++;
348 kill_client_serv_butone(NULL
, target_p
, "%s (Nick change collision)", me
.name
);
350 target_p
->flags
|= FLAGS_KILLED
;
351 exit_client(NULL
, target_p
, &me
, "Nick collision(new)");
352 source_p
->flags
|= FLAGS_KILLED
;
353 exit_client(client_p
, source_p
, &me
, "Nick collision(old)");
358 sameuser
= !irccmp(target_p
->username
, source_p
->username
) &&
359 !irccmp(target_p
->host
, source_p
->host
);
361 if((sameuser
&& newts
< target_p
->tsinfo
) ||
362 (!sameuser
&& newts
> target_p
->tsinfo
))
365 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
366 "Nick change collision from SIGNON from %s to %s(%s <- %s)(older killed)",
367 source_p
->name
, target_p
->name
,
368 target_p
->from
->name
, client_p
->name
);
370 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
371 "Nick change collision from SIGNON from %s to %s(%s <- %s)(newer killed)",
372 source_p
->name
, target_p
->name
,
373 target_p
->from
->name
, client_p
->name
);
375 ServerStats
.is_kill
++;
377 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
378 form_str(ERR_NICKCOLLISION
), target_p
->name
);
380 /* kill the client issuing the nickchange */
381 kill_client_serv_butone(client_p
, source_p
,
382 "%s (Nick change collision)", me
.name
);
384 source_p
->flags
|= FLAGS_KILLED
;
387 exit_client(client_p
, source_p
, &me
, "Nick collision(old)");
389 exit_client(client_p
, source_p
, &me
, "Nick collision(new)");
395 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
396 "Nick collision from SIGNON on %s(%s <- %s)(older killed)",
397 target_p
->name
, target_p
->from
->name
,
400 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
401 "Nick collision from SIGNON on %s(%s <- %s)(newer killed)",
402 target_p
->name
, target_p
->from
->name
,
405 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
406 form_str(ERR_NICKCOLLISION
), target_p
->name
);
408 /* kill the client who existed before hand */
409 kill_client_serv_butone(client_p
, target_p
,
410 "%s (Nick collision)", me
.name
);
412 ServerStats
.is_kill
++;
414 target_p
->flags
|= FLAGS_KILLED
;
415 (void) exit_client(client_p
, target_p
, &me
, "Nick collision");
422 send_signon(client_p
, source_p
, parv
[1], parv
[2], parv
[3], newts
, login
);
427 send_signon(struct Client
*client_p
, struct Client
*target_p
,
428 const char *nick
, const char *user
, const char *host
,
429 unsigned int newts
, const char *login
)
431 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
, ":%s SIGNON %s %s %s %ld %s",
432 use_id(target_p
), nick
, user
, host
,
433 (long) target_p
->tsinfo
, *login
? login
: "0");
435 strcpy(target_p
->user
->suser
, login
);
437 change_nick_user_host(target_p
, nick
, user
, host
, newts
, "Signing %s (%s)", *login
? "in" : "out", nick
);