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 MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
55 static int ms_signon(struct MsgBuf
*, 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, 0,
61 {mg_ignore
, mg_ignore
, mg_ignore
, mg_ignore
, {me_svslogin
, 6}, mg_ignore
}
63 struct Message signon_msgtab
= {
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_username(const char *username
)
83 for (; *username
; username
++)
87 if(!IsUserChar(*username
))
98 clean_host(const char *host
)
102 for (; *host
; host
++)
106 if(!IsHostChar(*host
))
117 me_svslogin(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
,
118 int parc
, const char *parv
[])
120 struct Client
*target_p
, *exist_p
;
121 char nick
[NICKLEN
+1], login
[NICKLEN
+1];
122 char user
[USERLEN
+1], host
[HOSTLEN
+1];
125 if(!(source_p
->flags
& FLAGS_SERVICE
))
127 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
128 "Non-service server %s attempting to execute services-only command SVSLOGIN", source_p
->name
);
132 if((target_p
= find_client(parv
[1])) == NULL
)
135 if(!MyClient(target_p
) && !IsUnknown(target_p
))
138 if(clean_nick(parv
[2], 0))
140 rb_strlcpy(nick
, parv
[2], NICKLEN
+ 1);
143 else if(*target_p
->name
)
144 rb_strlcpy(nick
, target_p
->name
, NICKLEN
+ 1);
148 if(clean_username(parv
[3]))
150 rb_strlcpy(user
, parv
[3], USERLEN
+ 1);
154 rb_strlcpy(user
, target_p
->username
, USERLEN
+ 1);
156 if(clean_host(parv
[4]))
158 rb_strlcpy(host
, parv
[4], HOSTLEN
+ 1);
162 rb_strlcpy(host
, target_p
->host
, HOSTLEN
+ 1);
167 rb_strlcpy(login
, target_p
->user
->suser
, NICKLEN
+ 1);
171 else if(!strcmp(parv
[5], "0"))
174 rb_strlcpy(login
, parv
[5], NICKLEN
+ 1);
176 /* Login (mostly) follows nick rules. */
177 if(*login
&& !clean_nick(login
, 0))
180 if((exist_p
= find_person(nick
)) && target_p
!= exist_p
)
184 if(MyClient(exist_p
))
185 sendto_one(exist_p
, ":%s KILL %s :(Nickname regained by services)",
186 me
.name
, exist_p
->name
);
188 exist_p
->flags
|= FLAGS_KILLED
;
189 kill_client_serv_butone(NULL
, exist_p
, "%s (Nickname regained by services)",
191 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
192 "Nick collision due to SVSLOGIN on %s",
195 snprintf(buf
, sizeof(buf
), "Killed (%s (Nickname regained by services))",
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");
204 /* Strip leading digits, unless it's purely numeric. */
205 const char *p
= login
;
211 sendto_one(target_p
, form_str(RPL_LOGGEDIN
), me
.name
, EmptyString(target_p
->name
) ? "*" : target_p
->name
,
212 nick
, user
, host
, p
, p
);
215 sendto_one(target_p
, form_str(RPL_LOGGEDOUT
), me
.name
, EmptyString(target_p
->name
) ? "*" : target_p
->name
,
218 if(IsUnknown(target_p
))
220 struct User
*user_p
= make_user(target_p
);
222 if(valid
& NICK_VALID
)
223 strcpy(target_p
->preClient
->spoofnick
, nick
);
225 if(valid
& USER_VALID
)
226 strcpy(target_p
->preClient
->spoofuser
, user
);
228 if(valid
& HOST_VALID
)
229 strcpy(target_p
->preClient
->spoofhost
, host
);
231 rb_strlcpy(user_p
->suser
, login
, NICKLEN
+ 1);
235 char note
[NICKLEN
+ 10];
237 send_signon(NULL
, target_p
, nick
, user
, host
, rb_current_time(), login
);
239 snprintf(note
, NICKLEN
+ 10, "Nick: %s", target_p
->name
);
240 rb_note(target_p
->localClient
->F
, note
);
247 ms_signon(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
,
248 int parc
, const char *parv
[])
250 struct Client
*target_p
;
252 char login
[NICKLEN
+1];
254 if(!clean_nick(parv
[1], 0))
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)",
267 source_p
->flags
|= FLAGS_KILLED
;
268 exit_client(NULL
, source_p
, &me
, "Bad nickname from SIGNON");
272 if(!clean_username(parv
[2]) || !clean_host(parv
[3]))
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)",
285 source_p
->flags
|= FLAGS_KILLED
;
286 exit_client(NULL
, source_p
, &me
, "Bad user@host from SIGNON");
290 newts
= atol(parv
[4]);
292 if(!strcmp(parv
[5], "0"))
294 else if(*parv
[5] != '*')
296 if (clean_nick(parv
[5], 0))
297 rb_strlcpy(login
, parv
[5], NICKLEN
+ 1);
304 target_p
= find_named_client(parv
[1]);
305 if(target_p
!= NULL
&& target_p
!= source_p
)
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");
313 if(!newts
|| !target_p
->tsinfo
|| (newts
== target_p
->tsinfo
) || !source_p
->user
)
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
,
320 ServerStats
.is_kill
++;
321 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
322 form_str(ERR_NICKCOLLISION
), target_p
->name
);
324 kill_client_serv_butone(NULL
, source_p
, "%s (Nick change collision)", me
.name
);
326 ServerStats
.is_kill
++;
328 kill_client_serv_butone(NULL
, target_p
, "%s (Nick change collision)", me
.name
);
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)");
338 sameuser
= !irccmp(target_p
->username
, source_p
->username
) &&
339 !irccmp(target_p
->host
, source_p
->host
);
341 if((sameuser
&& newts
< target_p
->tsinfo
) ||
342 (!sameuser
&& newts
> target_p
->tsinfo
))
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
);
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
);
355 ServerStats
.is_kill
++;
357 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
358 form_str(ERR_NICKCOLLISION
), target_p
->name
);
360 /* kill the client issuing the nickchange */
361 kill_client_serv_butone(client_p
, source_p
,
362 "%s (Nick change collision)", me
.name
);
364 source_p
->flags
|= FLAGS_KILLED
;
367 exit_client(client_p
, source_p
, &me
, "Nick collision(old)");
369 exit_client(client_p
, source_p
, &me
, "Nick collision(new)");
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
,
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
,
385 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
386 form_str(ERR_NICKCOLLISION
), target_p
->name
);
388 /* kill the client who existed before hand */
389 kill_client_serv_butone(client_p
, target_p
,
390 "%s (Nick collision)", me
.name
);
392 ServerStats
.is_kill
++;
394 target_p
->flags
|= FLAGS_KILLED
;
395 (void) exit_client(client_p
, target_p
, &me
, "Nick collision");
402 send_signon(client_p
, source_p
, parv
[1], parv
[2], parv
[3], newts
, login
);
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
)
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");
415 strcpy(target_p
->user
->suser
, login
);
417 change_nick_user_host(target_p
, nick
, user
, host
, newts
, "Signing %s (%s)", *login
? "in" : "out", nick
);