]> jfr.im git - irc/freenode/solanum.git/blob - modules/m_signon.c
make more snotes L_NETWIDE
[irc/freenode/solanum.git] / modules / m_signon.c
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
30 #include "stdinc.h"
31
32 #include "send.h"
33 #include "channel.h"
34 #include "client.h"
35 #include "defaults.h"
36 #include "ircd.h"
37 #include "numeric.h"
38 #include "s_conf.h"
39 #include "s_serv.h"
40 #include "hash.h"
41 #include "msg.h"
42 #include "parse.h"
43 #include "modules.h"
44 #include "whowas.h"
45 #include "monitor.h"
46 #include "s_stats.h"
47 #include "snomask.h"
48 #include "match.h"
49 #include "s_user.h"
50
51 static const char signon_desc[] = "Provides account login/logout support for services";
52
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 **);
55
56 static void send_signon(struct Client *, struct Client *, const char *, const char *, const char *, unsigned int, const char *);
57
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}
61 };
62 struct Message signon_msgtab = {
63 "SIGNON", 0, 0, 0, 0,
64 {mg_ignore, mg_ignore, {ms_signon, 6}, mg_ignore, mg_ignore, mg_ignore}
65 };
66
67 mapi_clist_av1 signon_clist[] = {
68 &svslogin_msgtab, &signon_msgtab, NULL
69 };
70
71 DECLARE_MODULE_AV2(signon, NULL, NULL, signon_clist, NULL, NULL, NULL, NULL, signon_desc);
72
73 #define NICK_VALID 1
74 #define USER_VALID 2
75 #define HOST_VALID 4
76
77 static bool
78 clean_username(const char *username)
79 {
80 int len = 0;
81
82 for (; *username; username++)
83 {
84 len++;
85
86 if(!IsUserChar(*username))
87 return false;
88 }
89
90 if(len > USERLEN)
91 return false;
92
93 return true;
94 }
95
96 static bool
97 clean_host(const char *host)
98 {
99 int len = 0;
100
101 for (; *host; host++)
102 {
103 len++;
104
105 if(!IsHostChar(*host))
106 return false;
107 }
108
109 if(len > HOSTLEN)
110 return false;
111
112 return true;
113 }
114
115 static void
116 me_svslogin(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
117 int parc, const char *parv[])
118 {
119 struct Client *target_p, *exist_p;
120 char nick[NICKLEN+1], login[NICKLEN+1];
121 char user[USERLEN+1], host[HOSTLEN+1];
122 int valid = 0;
123
124 if(!(source_p->flags & FLAGS_SERVICE))
125 {
126 sendto_realops_snomask(SNO_GENERAL, L_ALL,
127 "Non-service server %s attempting to execute services-only command SVSLOGIN", source_p->name);
128 return;
129 }
130
131 if((target_p = find_client(parv[1])) == NULL)
132 return;
133
134 if(!MyClient(target_p) && !IsUnknown(target_p))
135 return;
136
137 if(clean_nick(parv[2], 0))
138 {
139 rb_strlcpy(nick, parv[2], NICKLEN + 1);
140 valid |= NICK_VALID;
141 }
142 else if(*target_p->name)
143 rb_strlcpy(nick, target_p->name, NICKLEN + 1);
144 else
145 strcpy(nick, "*");
146
147 if(clean_username(parv[3]))
148 {
149 rb_strlcpy(user, parv[3], USERLEN + 1);
150 valid |= USER_VALID;
151 }
152 else
153 rb_strlcpy(user, target_p->username, USERLEN + 1);
154
155 if(clean_host(parv[4]))
156 {
157 rb_strlcpy(host, parv[4], HOSTLEN + 1);
158 valid |= HOST_VALID;
159 }
160 else
161 rb_strlcpy(host, target_p->host, HOSTLEN + 1);
162
163 if(*parv[5] == '*')
164 {
165 if(target_p->user)
166 rb_strlcpy(login, target_p->user->suser, NICKLEN + 1);
167 else
168 login[0] = '\0';
169 }
170 else if(!strcmp(parv[5], "0"))
171 login[0] = '\0';
172 else
173 rb_strlcpy(login, parv[5], NICKLEN + 1);
174
175 /* Login (mostly) follows nick rules. */
176 if(*login && !clean_nick(login, 0))
177 return;
178
179 if((exist_p = find_person(nick)) && target_p != exist_p)
180 {
181 char buf[BUFSIZE];
182
183 if(MyClient(exist_p))
184 sendto_one(exist_p, ":%s KILL %s :(Nickname regained by services)",
185 me.name, exist_p->name);
186
187 exist_p->flags |= FLAGS_KILLED;
188 kill_client_serv_butone(NULL, exist_p, "%s (Nickname regained by services)",
189 me.name);
190 sendto_realops_snomask(SNO_SKILL, L_ALL,
191 "Nick collision due to SVSLOGIN on %s",
192 nick);
193
194 snprintf(buf, sizeof(buf), "Killed (%s (Nickname regained by services))",
195 me.name);
196 exit_client(NULL, exist_p, &me, buf);
197 }
198 else if((exist_p = find_client(nick)) && IsUnknown(exist_p) && exist_p != target_p)
199 {
200 exit_client(NULL, exist_p, &me, "Overridden");
201 }
202
203 if(*login)
204 {
205 /* Strip leading digits, unless it's purely numeric. */
206 const char *p = login;
207 while(IsDigit(*p))
208 p++;
209 if(!*p)
210 p = login;
211
212 sendto_one(target_p, form_str(RPL_LOGGEDIN), me.name, EmptyString(target_p->name) ? "*" : target_p->name,
213 nick, user, host, p, p);
214 }
215 else
216 sendto_one(target_p, form_str(RPL_LOGGEDOUT), me.name, EmptyString(target_p->name) ? "*" : target_p->name,
217 nick, user, host);
218
219 if(IsUnknown(target_p))
220 {
221 struct User *user_p = make_user(target_p);
222
223 if(valid & NICK_VALID)
224 rb_strlcpy(target_p->preClient->spoofnick, nick, sizeof(target_p->preClient->spoofnick));
225
226 if(valid & USER_VALID)
227 rb_strlcpy(target_p->preClient->spoofuser, user, sizeof(target_p->preClient->spoofuser));
228
229 if(valid & HOST_VALID)
230 rb_strlcpy(target_p->preClient->spoofhost, host, sizeof(target_p->preClient->spoofhost));
231
232 rb_strlcpy(user_p->suser, login, NICKLEN + 1);
233 }
234 else
235 {
236 char note[NAMELEN + 10];
237
238 send_signon(NULL, target_p, nick, user, host, rb_current_time(), login);
239
240 snprintf(note, sizeof(note), "Nick: %s", target_p->name);
241 rb_note(target_p->localClient->F, note);
242 }
243 }
244
245 static void
246 ms_signon(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
247 int parc, const char *parv[])
248 {
249 struct Client *target_p;
250 int newts, sameuser;
251 char login[NICKLEN+1];
252
253 if(!clean_nick(parv[1], 0))
254 {
255 ServerStats.is_kill++;
256 sendto_realops_snomask(SNO_DEBUG, L_NETWIDE,
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)",
265 me.name);
266 source_p->flags |= FLAGS_KILLED;
267 exit_client(NULL, source_p, &me, "Bad nickname from SIGNON");
268 return;
269 }
270
271 if(!clean_username(parv[2]) || !clean_host(parv[3]))
272 {
273 ServerStats.is_kill++;
274 sendto_realops_snomask(SNO_DEBUG, L_NETWIDE,
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)",
283 me.name);
284 source_p->flags |= FLAGS_KILLED;
285 exit_client(NULL, source_p, &me, "Bad user@host from SIGNON");
286 return;
287 }
288
289 newts = atol(parv[4]);
290
291 if(!strcmp(parv[5], "0"))
292 login[0] = '\0';
293 else if(*parv[5] != '*')
294 {
295 if (clean_nick(parv[5], 0))
296 rb_strlcpy(login, parv[5], NICKLEN + 1);
297 else
298 return;
299 }
300 else
301 login[0] = '\0';
302
303 target_p = find_named_client(parv[1]);
304 if(target_p != NULL && target_p != source_p)
305 {
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");
310 else
311 {
312 if(!newts || !target_p->tsinfo || (newts == target_p->tsinfo) || !source_p->user)
313 {
314 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
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,
317 client_p->name);
318
319 ServerStats.is_kill++;
320 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
321 form_str(ERR_NICKCOLLISION), target_p->name);
322
323 kill_client_serv_butone(NULL, source_p, "%s (Nick change collision)", me.name);
324
325 ServerStats.is_kill++;
326
327 kill_client_serv_butone(NULL, target_p, "%s (Nick change collision)", me.name);
328
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)");
333 return;
334 }
335 else
336 {
337 sameuser = !irccmp(target_p->username, source_p->username) &&
338 !irccmp(target_p->host, source_p->host);
339
340 if((sameuser && newts < target_p->tsinfo) ||
341 (!sameuser && newts > target_p->tsinfo))
342 {
343 if(sameuser)
344 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
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);
348 else
349 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
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);
353
354 ServerStats.is_kill++;
355
356 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
357 form_str(ERR_NICKCOLLISION), target_p->name);
358
359 /* kill the client issuing the nickchange */
360 kill_client_serv_butone(client_p, source_p,
361 "%s (Nick change collision)", me.name);
362
363 source_p->flags |= FLAGS_KILLED;
364
365 if(sameuser)
366 exit_client(client_p, source_p, &me, "Nick collision(old)");
367 else
368 exit_client(client_p, source_p, &me, "Nick collision(new)");
369 return;
370 }
371 else
372 {
373 if(sameuser)
374 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
375 "Nick collision from SIGNON on %s(%s <- %s)(older killed)",
376 target_p->name, target_p->from->name,
377 client_p->name);
378 else
379 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
380 "Nick collision from SIGNON on %s(%s <- %s)(newer killed)",
381 target_p->name, target_p->from->name,
382 client_p->name);
383
384 sendto_one_numeric(target_p, ERR_NICKCOLLISION,
385 form_str(ERR_NICKCOLLISION), target_p->name);
386
387 /* kill the client who existed before hand */
388 kill_client_serv_butone(client_p, target_p,
389 "%s (Nick collision)", me.name);
390
391 ServerStats.is_kill++;
392
393 target_p->flags |= FLAGS_KILLED;
394 (void) exit_client(client_p, target_p, &me, "Nick collision");
395 }
396 }
397
398 }
399 }
400
401 send_signon(client_p, source_p, parv[1], parv[2], parv[3], newts, login);
402 }
403
404 static void
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)
408 {
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");
412
413 rb_strlcpy(target_p->user->suser, login, sizeof(target_p->user->suser));
414
415 change_nick_user_host(target_p, nick, user, host, newts, "Signing %s (%s)", *login ? "in" : "out", nick);
416 }