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.
51 static const char signon_desc
[] = "Provides account login/logout support for services";
53 static void me_svslogin(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
54 static void ms_signon(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
56 static void send_signon(struct Client
*, struct Client
*, const char *, const char *, const char *, unsigned int, const char *);
58 struct Message svslogin_msgtab
= {
59 "SVSLOGIN", 0, 0, 0, 0,
60 {mg_ignore
, mg_ignore
, mg_ignore
, mg_ignore
, {me_svslogin
, 6}, mg_ignore
}
62 struct Message signon_msgtab
= {
64 {mg_ignore
, mg_ignore
, {ms_signon
, 6}, mg_ignore
, mg_ignore
, mg_ignore
}
67 mapi_clist_av1 signon_clist
[] = {
68 &svslogin_msgtab
, &signon_msgtab
, NULL
71 DECLARE_MODULE_AV2(signon
, NULL
, NULL
, signon_clist
, NULL
, NULL
, NULL
, NULL
, signon_desc
);
78 clean_username(const char *username
)
82 for (; *username
; username
++)
86 if(!IsUserChar(*username
))
97 clean_host(const char *host
)
101 for (; *host
; host
++)
105 if(!IsHostChar(*host
))
116 me_svslogin(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
,
117 int parc
, const char *parv
[])
119 struct Client
*target_p
, *exist_p
;
120 char nick
[NICKLEN
+1], login
[NICKLEN
+1];
121 char user
[USERLEN
+1], host
[HOSTLEN
+1];
124 if(!(source_p
->flags
& FLAGS_SERVICE
))
126 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
127 "Non-service server %s attempting to execute services-only command SVSLOGIN", source_p
->name
);
131 if((target_p
= find_client(parv
[1])) == NULL
)
134 if(!MyClient(target_p
) && !IsUnknown(target_p
))
137 if(clean_nick(parv
[2], 0))
139 rb_strlcpy(nick
, parv
[2], NICKLEN
+ 1);
142 else if(*target_p
->name
)
143 rb_strlcpy(nick
, target_p
->name
, NICKLEN
+ 1);
147 if(clean_username(parv
[3]))
149 rb_strlcpy(user
, parv
[3], USERLEN
+ 1);
153 rb_strlcpy(user
, target_p
->username
, USERLEN
+ 1);
155 if(clean_host(parv
[4]))
157 rb_strlcpy(host
, parv
[4], HOSTLEN
+ 1);
161 rb_strlcpy(host
, target_p
->host
, HOSTLEN
+ 1);
166 rb_strlcpy(login
, target_p
->user
->suser
, NICKLEN
+ 1);
170 else if(!strcmp(parv
[5], "0"))
173 rb_strlcpy(login
, parv
[5], NICKLEN
+ 1);
175 /* Login (mostly) follows nick rules. */
176 if(*login
&& !clean_nick(login
, 0))
179 if((exist_p
= find_person(nick
)) && target_p
!= exist_p
)
183 if(MyClient(exist_p
))
184 sendto_one(exist_p
, ":%s KILL %s :(Nickname regained by services)",
185 me
.name
, exist_p
->name
);
187 exist_p
->flags
|= FLAGS_KILLED
;
188 kill_client_serv_butone(NULL
, exist_p
, "%s (Nickname regained by services)",
190 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
191 "Nick collision due to SVSLOGIN on %s",
194 snprintf(buf
, sizeof(buf
), "Killed (%s (Nickname regained by services))",
196 exit_client(NULL
, exist_p
, &me
, buf
);
198 else if((exist_p
= find_client(nick
)) && IsUnknown(exist_p
) && exist_p
!= target_p
)
200 exit_client(NULL
, exist_p
, &me
, "Overridden");
205 /* Strip leading digits, unless it's purely numeric. */
206 const char *p
= login
;
212 sendto_one(target_p
, form_str(RPL_LOGGEDIN
), me
.name
, EmptyString(target_p
->name
) ? "*" : target_p
->name
,
213 nick
, user
, host
, p
, p
);
216 sendto_one(target_p
, form_str(RPL_LOGGEDOUT
), me
.name
, EmptyString(target_p
->name
) ? "*" : target_p
->name
,
219 if(IsUnknown(target_p
))
221 struct User
*user_p
= make_user(target_p
);
223 if(valid
& NICK_VALID
)
224 strcpy(target_p
->preClient
->spoofnick
, nick
);
226 if(valid
& USER_VALID
)
227 strcpy(target_p
->preClient
->spoofuser
, user
);
229 if(valid
& HOST_VALID
)
230 strcpy(target_p
->preClient
->spoofhost
, host
);
232 rb_strlcpy(user_p
->suser
, login
, NICKLEN
+ 1);
236 char note
[NICKLEN
+ 10];
238 send_signon(NULL
, target_p
, nick
, user
, host
, rb_current_time(), login
);
240 snprintf(note
, NICKLEN
+ 10, "Nick: %s", target_p
->name
);
241 rb_note(target_p
->localClient
->F
, note
);
246 ms_signon(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
,
247 int parc
, const char *parv
[])
249 struct Client
*target_p
;
251 char login
[NICKLEN
+1];
253 if(!clean_nick(parv
[1], 0))
255 ServerStats
.is_kill
++;
256 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
257 "Bad Nick from SIGNON: %s From: %s(via %s)",
258 parv
[1], source_p
->servptr
->name
, client_p
->name
);
259 /* if source_p has an id, kill_client_serv_butone() will
260 * send a kill to client_p, otherwise do it here */
261 if (!has_id(source_p
))
262 sendto_one(client_p
, ":%s KILL %s :%s (Bad nickname from SIGNON)",
263 get_id(&me
, client_p
), parv
[1], me
.name
);
264 kill_client_serv_butone(client_p
, source_p
, "%s (Bad nickname from SIGNON)",
266 source_p
->flags
|= FLAGS_KILLED
;
267 exit_client(NULL
, source_p
, &me
, "Bad nickname from SIGNON");
271 if(!clean_username(parv
[2]) || !clean_host(parv
[3]))
273 ServerStats
.is_kill
++;
274 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
275 "Bad user@host from SIGNON: %s@%s From: %s(via %s)",
276 parv
[2], parv
[3], source_p
->servptr
->name
, client_p
->name
);
277 /* if source_p has an id, kill_client_serv_butone() will
278 * send a kill to client_p, otherwise do it here */
279 if (!has_id(source_p
))
280 sendto_one(client_p
, ":%s KILL %s :%s (Bad user@host from SIGNON)",
281 get_id(&me
, client_p
), parv
[1], me
.name
);
282 kill_client_serv_butone(client_p
, source_p
, "%s (Bad user@host from SIGNON)",
284 source_p
->flags
|= FLAGS_KILLED
;
285 exit_client(NULL
, source_p
, &me
, "Bad user@host from SIGNON");
289 newts
= atol(parv
[4]);
291 if(!strcmp(parv
[5], "0"))
293 else if(*parv
[5] != '*')
295 if (clean_nick(parv
[5], 0))
296 rb_strlcpy(login
, parv
[5], NICKLEN
+ 1);
303 target_p
= find_named_client(parv
[1]);
304 if(target_p
!= NULL
&& target_p
!= source_p
)
306 /* In case of collision, follow NICK rules. */
307 /* XXX this is duplicated code and does not do SAVE */
308 if(IsUnknown(target_p
))
309 exit_client(NULL
, target_p
, &me
, "Overridden");
312 if(!newts
|| !target_p
->tsinfo
|| (newts
== target_p
->tsinfo
) || !source_p
->user
)
314 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
315 "Nick change collision from SIGNON from %s to %s(%s <- %s)(both killed)",
316 source_p
->name
, target_p
->name
, target_p
->from
->name
,
319 ServerStats
.is_kill
++;
320 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
321 form_str(ERR_NICKCOLLISION
), target_p
->name
);
323 kill_client_serv_butone(NULL
, source_p
, "%s (Nick change collision)", me
.name
);
325 ServerStats
.is_kill
++;
327 kill_client_serv_butone(NULL
, target_p
, "%s (Nick change collision)", me
.name
);
329 target_p
->flags
|= FLAGS_KILLED
;
330 exit_client(NULL
, target_p
, &me
, "Nick collision(new)");
331 source_p
->flags
|= FLAGS_KILLED
;
332 exit_client(client_p
, source_p
, &me
, "Nick collision(old)");
337 sameuser
= !irccmp(target_p
->username
, source_p
->username
) &&
338 !irccmp(target_p
->host
, source_p
->host
);
340 if((sameuser
&& newts
< target_p
->tsinfo
) ||
341 (!sameuser
&& newts
> target_p
->tsinfo
))
344 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
345 "Nick change collision from SIGNON from %s to %s(%s <- %s)(older killed)",
346 source_p
->name
, target_p
->name
,
347 target_p
->from
->name
, client_p
->name
);
349 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
350 "Nick change collision from SIGNON from %s to %s(%s <- %s)(newer killed)",
351 source_p
->name
, target_p
->name
,
352 target_p
->from
->name
, client_p
->name
);
354 ServerStats
.is_kill
++;
356 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
357 form_str(ERR_NICKCOLLISION
), target_p
->name
);
359 /* kill the client issuing the nickchange */
360 kill_client_serv_butone(client_p
, source_p
,
361 "%s (Nick change collision)", me
.name
);
363 source_p
->flags
|= FLAGS_KILLED
;
366 exit_client(client_p
, source_p
, &me
, "Nick collision(old)");
368 exit_client(client_p
, source_p
, &me
, "Nick collision(new)");
374 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
375 "Nick collision from SIGNON on %s(%s <- %s)(older killed)",
376 target_p
->name
, target_p
->from
->name
,
379 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
380 "Nick collision from SIGNON on %s(%s <- %s)(newer killed)",
381 target_p
->name
, target_p
->from
->name
,
384 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
385 form_str(ERR_NICKCOLLISION
), target_p
->name
);
387 /* kill the client who existed before hand */
388 kill_client_serv_butone(client_p
, target_p
,
389 "%s (Nick collision)", me
.name
);
391 ServerStats
.is_kill
++;
393 target_p
->flags
|= FLAGS_KILLED
;
394 (void) exit_client(client_p
, target_p
, &me
, "Nick collision");
401 send_signon(client_p
, source_p
, parv
[1], parv
[2], parv
[3], newts
, login
);
405 send_signon(struct Client
*client_p
, struct Client
*target_p
,
406 const char *nick
, const char *user
, const char *host
,
407 unsigned int newts
, const char *login
)
409 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
, ":%s SIGNON %s %s %s %ld %s",
410 use_id(target_p
), nick
, user
, host
,
411 (long) target_p
->tsinfo
, *login
? login
: "0");
413 strcpy(target_p
->user
->suser
, login
);
415 change_nick_user_host(target_p
, nick
, user
, host
, newts
, "Signing %s (%s)", *login
? "in" : "out", nick
);