Restore stats_spy hook that was removed in commit 401f2454671ca233e35b0e6e4f3fa4c43cd...
[seven-1.x.git] / src / s_user.c
blob38033e040f8068524b7e23da6d156b5ecca63771
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * s_user.c: User related functions.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
25 #include "stdinc.h"
26 #include "tools.h"
27 #include "s_user.h"
28 #include "channel.h"
29 #include "class.h"
30 #include "client.h"
31 #include "common.h"
32 #include "hash.h"
33 #include "irc_string.h"
34 #include "sprintf_irc.h"
35 #include "ircd.h"
36 #include "listener.h"
37 #include "msg.h"
38 #include "numeric.h"
39 #include "commio.h"
40 #include "s_conf.h"
41 #include "s_newconf.h"
42 #include "s_log.h"
43 #include "s_serv.h"
44 #include "s_stats.h"
45 #include "scache.h"
46 #include "send.h"
47 #include "supported.h"
48 #include "whowas.h"
49 #include "memory.h"
50 #include "packet.h"
51 #include "reject.h"
52 #include "cache.h"
53 #include "hook.h"
54 #include "monitor.h"
55 #include "snomask.h"
56 #include "blacklist.h"
58 static void report_and_set_user_flags(struct Client *, struct ConfItem *);
59 void user_welcome(struct Client *source_p);
61 extern char *crypt();
63 char umodebuf[128];
65 int user_modes[256] = {
66 /* 0x00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0F */
67 /* 0x10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x1F */
68 /* 0x20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x2F */
69 /* 0x30 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x3F */
70 0, /* @ */
71 0, /* A */
72 0, /* B */
73 0, /* C */
74 UMODE_DEAF, /* D */
75 0, /* E */
76 0, /* F */
77 0, /* G */
78 0, /* H */
79 0, /* I */
80 0, /* J */
81 0, /* K */
82 0, /* L */
83 0, /* M */
84 0, /* N */
85 0, /* O */
86 0, /* P */
87 UMODE_NOFORWARD, /* Q */
88 UMODE_REGONLYMSG, /* R */
89 UMODE_SERVICE, /* S */
90 UMODE_HELPER, /* T */
91 0, /* U */
92 0, /* V */
93 0, /* W */
94 0, /* X */
95 0, /* Y */
96 0, /* Z */
97 /* 0x5B */ 0, 0, 0, 0, 0, 0, /* 0x60 */
98 UMODE_ADMIN, /* a */
99 0, /* b */
100 0, /* c */
101 0, /* d */
102 0, /* e */
103 0, /* f */
104 UMODE_CALLERID, /* g */
105 0, /* h */
106 UMODE_INVISIBLE, /* i */
107 0, /* j */
108 0, /* k */
109 0, /* l */
110 0, /* m */
111 0, /* n */
112 UMODE_OPER, /* o */
113 UMODE_OVERRIDE, /* p */
114 0, /* q */
115 0, /* r */
116 UMODE_SERVNOTICE, /* s */
117 0, /* t */
118 0, /* u */
119 0, /* v */
120 UMODE_WALLOP, /* w */
121 0, /* x */
122 0, /* y */
123 UMODE_OPERWALL, /* z */
124 /* 0x7B */ 0, 0, 0, 0, 0, /* 0x7F */
125 /* 0x80 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x9F */
126 /* 0x90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x9F */
127 /* 0xA0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xAF */
128 /* 0xB0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xBF */
129 /* 0xC0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xCF */
130 /* 0xD0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xDF */
131 /* 0xE0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xEF */
132 /* 0xF0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xFF */
134 /* *INDENT-ON* */
137 * show_lusers -
139 * inputs - pointer to client
140 * output -
141 * side effects - display to client user counts etc.
144 show_lusers(struct Client *source_p)
146 sendto_one_numeric(source_p, RPL_LUSERCLIENT, form_str(RPL_LUSERCLIENT),
147 (Count.total - Count.invisi),
148 Count.invisi, dlink_list_length(&global_serv_list));
150 if(dlink_list_length(&oper_list) > 0)
151 sendto_one_numeric(source_p, RPL_LUSEROP,
152 form_str(RPL_LUSEROP), dlink_list_length(&oper_list));
154 if(dlink_list_length(&unknown_list) > 0)
155 sendto_one_numeric(source_p, RPL_LUSERUNKNOWN,
156 form_str(RPL_LUSERUNKNOWN),
157 dlink_list_length(&unknown_list));
159 if(dlink_list_length(&global_channel_list) > 0)
160 sendto_one_numeric(source_p, RPL_LUSERCHANNELS,
161 form_str(RPL_LUSERCHANNELS),
162 dlink_list_length(&global_channel_list));
164 sendto_one_numeric(source_p, RPL_LUSERME, form_str(RPL_LUSERME),
165 dlink_list_length(&lclient_list),
166 dlink_list_length(&serv_list));
168 sendto_one_numeric(source_p, RPL_LOCALUSERS,
169 form_str(RPL_LOCALUSERS),
170 dlink_list_length(&lclient_list),
171 Count.max_loc,
172 dlink_list_length(&lclient_list),
173 Count.max_loc);
175 sendto_one_numeric(source_p, RPL_GLOBALUSERS, form_str(RPL_GLOBALUSERS),
176 Count.total, Count.max_tot,
177 Count.total, Count.max_tot);
179 sendto_one_numeric(source_p, RPL_STATSCONN,
180 form_str(RPL_STATSCONN),
181 MaxConnectionCount, MaxClientCount,
182 Count.totalrestartcount);
184 if(dlink_list_length(&lclient_list) > (unsigned long)MaxClientCount)
185 MaxClientCount = dlink_list_length(&lclient_list);
187 if((dlink_list_length(&lclient_list) + dlink_list_length(&serv_list)) >
188 (unsigned long)MaxConnectionCount)
189 MaxConnectionCount = dlink_list_length(&lclient_list) +
190 dlink_list_length(&serv_list);
192 return 0;
196 ** register_local_user
197 ** This function is called when both NICK and USER messages
198 ** have been accepted for the client, in whatever order. Only
199 ** after this, is the USER message propagated.
201 ** NICK's must be propagated at once when received, although
202 ** it would be better to delay them too until full info is
203 ** available. Doing it is not so simple though, would have
204 ** to implement the following:
206 ** (actually it has been implemented already for a while) -orabidoo
208 ** 1) user telnets in and gives only "NICK foobar" and waits
209 ** 2) another user far away logs in normally with the nick
210 ** "foobar" (quite legal, as this server didn't propagate
211 ** it).
212 ** 3) now this server gets nick "foobar" from outside, but
213 ** has alread the same defined locally. Current server
214 ** would just issue "KILL foobar" to clean out dups. But,
215 ** this is not fair. It should actually request another
216 ** nick from local user or kill him/her...
220 register_local_user(struct Client *client_p, struct Client *source_p, const char *username)
222 struct ConfItem *aconf;
223 struct User *user = source_p->user;
224 char tmpstr2[IRCD_BUFSIZE];
225 char ipaddr[HOSTIPLEN];
226 char myusername[USERLEN+1];
227 int status;
229 s_assert(NULL != source_p);
230 s_assert(MyConnect(source_p));
231 s_assert(source_p->username != username);
233 if(source_p == NULL)
234 return -1;
236 if(IsAnyDead(source_p))
237 return -1;
239 if(ConfigFileEntry.ping_cookie)
241 if(!(source_p->flags & FLAGS_PINGSENT) && source_p->localClient->random_ping == 0)
243 source_p->localClient->random_ping = (unsigned long) (rand() * rand()) << 1;
244 sendto_one(source_p, "PING :%08lX",
245 (unsigned long) source_p->localClient->random_ping);
246 source_p->flags |= FLAGS_PINGSENT;
247 return -1;
249 if(!(source_p->flags2 & FLAGS2_PING_COOKIE))
251 return -1;
255 /* hasnt finished client cap negotiation */
256 if(source_p->flags2 & FLAGS2_CLICAP)
257 return -1;
259 /* still has DNSbls to validate against */
260 if(dlink_list_length(&source_p->preClient->dnsbl_queries) > 0)
261 return -1;
263 client_p->localClient->last = CurrentTime;
264 /* Straight up the maximum rate of flooding... */
265 source_p->localClient->allow_read = MAX_FLOOD_BURST;
267 /* XXX - fixme. we shouldnt have to build a users buffer twice.. */
268 if(!IsGotId(source_p) && (strchr(username, '[') != NULL))
270 const char *p;
271 int i = 0;
273 p = username;
275 while(*p && i < USERLEN)
277 if(*p != '[')
278 myusername[i++] = *p;
279 p++;
282 myusername[i] = '\0';
283 username = myusername;
286 if((status = check_client(client_p, source_p, username)) < 0)
287 return (CLIENT_EXITED);
289 /* Apply nick override */
290 if(*source_p->preClient->spoofnick)
292 del_from_client_hash(source_p->name, source_p);
293 strlcpy(source_p->name, source_p->preClient->spoofnick, NICKLEN + 1);
294 add_to_client_hash(source_p->name, source_p);
295 comm_note(source_p->localClient->fd, "Nick: %s", source_p->name);
298 if(!valid_hostname(source_p->host))
300 sendto_one(source_p,
301 ":%s NOTICE %s :*** Notice -- You have an illegal character in your hostname",
302 me.name, source_p->name);
304 strlcpy(source_p->host, source_p->sockhost, sizeof(source_p->host));
306 #ifdef IPV6
307 if(ConfigFileEntry.dot_in_ip6_addr == 1)
308 strlcat(source_p->host, ".", sizeof(source_p->host));
309 #endif
313 aconf = source_p->localClient->att_conf;
315 if(aconf == NULL)
317 exit_client(client_p, source_p, &me, "*** Not Authorised");
318 return (CLIENT_EXITED);
321 if(!IsGotId(source_p))
323 const char *p;
324 int i = 0;
326 if(IsNeedIdentd(aconf))
328 ServerStats->is_ref++;
329 sendto_one(source_p,
330 ":%s NOTICE %s :*** Notice -- You need to install identd to use this server",
331 me.name, client_p->name);
332 exit_client(client_p, source_p, &me, "Install identd");
333 return (CLIENT_EXITED);
336 /* dont replace username if its supposed to be spoofed --fl */
337 if(!IsConfDoSpoofIp(aconf) || !strchr(aconf->name, '@'))
339 p = username;
341 if(!IsNoTilde(aconf))
342 source_p->username[i++] = '~';
344 while (*p && i < USERLEN)
346 if(*p != '[')
347 source_p->username[i++] = *p;
348 p++;
351 source_p->username[i] = '\0';
355 if(IsNeedSasl(aconf) && !*user->suser)
357 ServerStats->is_ref++;
358 sendto_one(source_p,
359 ":%s NOTICE %s :*** Notice -- You need to identify via SASL to use this server",
360 me.name, client_p->name);
361 exit_client(client_p, source_p, &me, "SASL access only");
362 return (CLIENT_EXITED);
365 /* password check */
366 if(!EmptyString(aconf->passwd))
368 const char *encr;
370 if(EmptyString(source_p->localClient->passwd))
371 encr = "";
372 else if(IsConfEncrypted(aconf))
373 encr = crypt(source_p->localClient->passwd, aconf->passwd);
374 else
375 encr = source_p->localClient->passwd;
377 if(strcmp(encr, aconf->passwd))
379 ServerStats->is_ref++;
380 sendto_one(source_p, form_str(ERR_PASSWDMISMATCH), me.name, source_p->name);
381 exit_client(client_p, source_p, &me, "Bad Password");
382 return (CLIENT_EXITED);
385 /* clear password only if used now, otherwise send it
386 * to services -- jilles */
387 if(source_p->localClient->passwd)
389 memset(source_p->localClient->passwd, 0, strlen(source_p->localClient->passwd));
390 MyFree(source_p->localClient->passwd);
391 source_p->localClient->passwd = NULL;
395 /* report if user has &^>= etc. and set flags as needed in source_p */
396 report_and_set_user_flags(source_p, aconf);
398 /* Limit clients */
400 * We want to be able to have servers and F-line clients
401 * connect, so save room for "buffer" connections.
402 * Smaller servers may want to decrease this, and it should
403 * probably be just a percentage of the MAXCLIENTS...
404 * -Taner
406 /* Except "F:" clients */
407 if(((dlink_list_length(&lclient_list) + 1) >=
408 ((unsigned long)GlobalSetOptions.maxclients + MAX_BUFFER) ||
409 (dlink_list_length(&lclient_list) + 1) >=
410 ((unsigned long)GlobalSetOptions.maxclients - 5)) && !(IsExemptLimits(source_p)))
412 sendto_realops_snomask(SNO_FULL, L_NETWIDE,
413 "Too many clients, rejecting %s[%s].", source_p->name, source_p->host);
415 ServerStats->is_ref++;
416 exit_client(client_p, source_p, &me, "Sorry, server is full - try later");
417 return (CLIENT_EXITED);
420 /* valid user name check */
422 if(!valid_username(source_p->username))
424 sendto_realops_snomask(SNO_REJ, L_NETWIDE,
425 "Invalid username: %s (%s@%s)",
426 source_p->name, source_p->username, source_p->host);
427 ServerStats->is_ref++;
428 ircsprintf(tmpstr2, "Invalid username [%s]", source_p->username);
429 exit_client(client_p, source_p, &me, tmpstr2);
430 return (CLIENT_EXITED);
433 /* end of valid user name check */
435 /* kline exemption extends to xline too */
436 if(!IsExemptKline(source_p) &&
437 find_xline(source_p->info, 1) != NULL)
439 ServerStats->is_ref++;
440 add_reject(source_p);
441 exit_client(client_p, source_p, &me, "Bad user info");
442 return CLIENT_EXITED;
445 /* dnsbl check */
446 if (source_p->preClient->dnsbl_listed != NULL)
448 if (IsExemptKline(source_p) || IsConfExemptDNSBL(aconf))
449 sendto_one_notice(source_p, ":*** Your IP address %s is listed in %s, but you are exempt",
450 source_p->sockhost, source_p->preClient->dnsbl_listed->host);
451 else
453 ServerStats->is_ref++;
454 sendto_one(source_p, form_str(ERR_YOUREBANNEDCREEP),
455 me.name, source_p->name,
456 source_p->preClient->dnsbl_listed->reject_reason);
457 sendto_one_notice(source_p, ":*** Your IP address %s is listed in %s",
458 source_p->sockhost, source_p->preClient->dnsbl_listed->host);
459 source_p->preClient->dnsbl_listed->hits++;
460 add_reject(source_p);
461 exit_client(client_p, source_p, &me, "*** Banned (DNS blacklist)");
462 return CLIENT_EXITED;
466 /* Store original hostname -- jilles */
467 strlcpy(source_p->orighost, source_p->host, HOSTLEN + 1);
469 /* Spoof user@host */
470 if(*source_p->preClient->spoofuser)
471 strlcpy(source_p->username, source_p->preClient->spoofuser, USERLEN + 1);
472 if(*source_p->preClient->spoofhost)
474 strlcpy(source_p->host, source_p->preClient->spoofhost, HOSTLEN + 1);
475 if (irccmp(source_p->host, source_p->orighost))
476 SetDynSpoof(source_p);
479 if(IsAnyDead(client_p))
480 return CLIENT_EXITED;
482 inetntop_sock((struct sockaddr *)&source_p->localClient->ip, ipaddr, sizeof(ipaddr));
484 sendto_realops_snomask(SNO_CCONN, L_NETWIDE,
485 "Client connecting: %s (%s@%s) [%s] {%s} [%s]",
486 source_p->name, source_p->username, source_p->orighost,
487 show_ip(NULL, source_p) ? ipaddr : "255.255.255.255",
488 get_client_class(source_p), source_p->info);
490 sendto_realops_snomask(SNO_CCONNEXT, L_NETWIDE,
491 "CLICONN %s %s %s %s %s %s 0 %s",
492 source_p->name, source_p->username, source_p->orighost,
493 show_ip(NULL, source_p) ? ipaddr : "255.255.255.255",
494 get_client_class(source_p),
495 /* mirc can sometimes send ips here */
496 show_ip(NULL, source_p) ? source_p->localClient->fullcaps : "<hidden> <hidden>",
497 source_p->info);
499 /* If they have died in send_* don't do anything. */
500 if(IsAnyDead(source_p))
501 return CLIENT_EXITED;
503 add_to_hostname_hash(source_p->orighost, source_p);
505 /* Allocate a UID if it was not previously allocated.
506 * If this already occured, it was probably during SASL auth...
508 if(!*source_p->id)
510 strcpy(source_p->id, generate_uid());
511 add_to_id_hash(source_p->id, source_p);
514 source_p->umodes |= ConfigFileEntry.default_umodes & ~ConfigFileEntry.oper_only_umodes;
516 if (source_p->umodes & UMODE_INVISIBLE)
517 Count.invisi++;
519 s_assert(!IsClient(source_p));
520 dlinkMoveNode(&source_p->localClient->tnode, &unknown_list, &lclient_list);
521 SetClient(source_p);
523 /* XXX source_p->servptr is &me, since local client */
524 source_p->servptr = find_server(NULL, user->server);
525 dlinkAdd(source_p, &source_p->lnode, &source_p->servptr->serv->users);
526 /* Increment our total user count here */
527 if(++Count.total > Count.max_tot)
528 Count.max_tot = Count.total;
529 source_p->localClient->allow_read = MAX_FLOOD_BURST;
531 Count.totalrestartcount++;
533 s_assert(source_p->localClient != NULL);
535 if(dlink_list_length(&lclient_list) > (unsigned long)Count.max_loc)
537 Count.max_loc = dlink_list_length(&lclient_list);
538 if(!(Count.max_loc % 10))
539 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
540 "New Max Local Clients: %d", Count.max_loc);
543 /* they get a reduced limit */
544 if(find_tgchange(source_p->sockhost))
545 USED_TARGETS(source_p) = 6;
547 call_hook(h_new_local_user, source_p);
549 monitor_signon(source_p);
550 user_welcome(source_p);
552 free_pre_client(source_p);
554 return (introduce_client(client_p, source_p, user, source_p->name, 1));
558 * introduce_clients
560 * inputs -
561 * output -
562 * side effects - This common function introduces a client to the rest
563 * of the net, either from a local client connect or
564 * from a remote connect.
567 introduce_client(struct Client *client_p, struct Client *source_p, struct User *user, const char *nick, int use_euid)
569 static char ubuf[12];
570 struct Client *identifyservice_p;
571 char *p;
572 hook_data_umode_changed hdata;
573 hook_data_client hdata2;
575 if(MyClient(source_p))
576 send_umode(source_p, source_p, 0, 0, ubuf);
577 else
578 send_umode(NULL, source_p, 0, 0, ubuf);
580 if(!*ubuf)
582 ubuf[0] = '+';
583 ubuf[1] = '\0';
586 /* if it has an ID, introduce it with its id to TS6 servers,
587 * otherwise introduce it normally to all.
589 if(has_id(source_p))
591 char sockhost[HOSTLEN];
592 if(source_p->sockhost[0] == ':')
594 sockhost[0] = '0';
595 sockhost[1] = '\0';
596 strlcat(sockhost, source_p->sockhost, sizeof(sockhost));
597 } else
598 strcpy(sockhost, source_p->sockhost);
600 if (use_euid)
601 sendto_server(client_p, NULL, CAP_EUID | CAP_TS6, NOCAPS,
602 ":%s EUID %s %d %ld %s %s %s %s %s %s %s :%s",
603 source_p->servptr->id, nick,
604 source_p->hopcount + 1,
605 (long) source_p->tsinfo, ubuf,
606 source_p->username, source_p->host,
607 IsIPSpoof(source_p) ? "0" : sockhost,
608 source_p->id,
609 IsDynSpoof(source_p) ? source_p->orighost : "*",
610 EmptyString(source_p->user->suser) ? "*" : source_p->user->suser,
611 source_p->info);
613 sendto_server(client_p, NULL, CAP_TS6, use_euid ? CAP_EUID : NOCAPS,
614 ":%s UID %s %d %ld %s %s %s %s %s :%s",
615 source_p->servptr->id, nick,
616 source_p->hopcount + 1,
617 (long) source_p->tsinfo, ubuf,
618 source_p->username, source_p->host,
619 IsIPSpoof(source_p) ? "0" : sockhost,
620 source_p->id, source_p->info);
622 sendto_server(client_p, NULL, NOCAPS, CAP_TS6,
623 "NICK %s %d %ld %s %s %s %s :%s",
624 nick, source_p->hopcount + 1,
625 (long) source_p->tsinfo,
626 ubuf, source_p->username, source_p->host,
627 user->server, source_p->info);
629 else
630 sendto_server(client_p, NULL, NOCAPS, NOCAPS,
631 "NICK %s %d %ld %s %s %s %s :%s",
632 nick, source_p->hopcount + 1,
633 (long) source_p->tsinfo,
634 ubuf, source_p->username, source_p->host,
635 user->server, source_p->info);
637 if (IsDynSpoof(source_p))
639 sendto_server(client_p, NULL, CAP_TS6, use_euid ? CAP_EUID : NOCAPS, ":%s ENCAP * REALHOST %s",
640 use_id(source_p), source_p->orighost);
641 sendto_server(client_p, NULL, NOCAPS, CAP_TS6, ":%s ENCAP * REALHOST %s",
642 source_p->name, source_p->orighost);
644 if (!EmptyString(source_p->user->suser))
646 sendto_server(client_p, NULL, CAP_TS6, use_euid ? CAP_EUID : NOCAPS, ":%s ENCAP * LOGIN %s",
647 use_id(source_p), source_p->user->suser);
648 sendto_server(client_p, NULL, NOCAPS, CAP_TS6, ":%s ENCAP * LOGIN %s",
649 source_p->name, source_p->user->suser);
652 if(MyConnect(source_p) && source_p->localClient->passwd)
654 if (ConfigFileEntry.identifyservice[0] != '\0' &&
655 ConfigFileEntry.identifycommand[0] != '\0')
657 /* use user@server */
658 p = strchr(ConfigFileEntry.identifyservice, '@');
659 if (p != NULL)
660 identifyservice_p = find_named_client(p + 1);
661 else
662 identifyservice_p = NULL;
663 if (identifyservice_p != NULL)
664 sendto_one(identifyservice_p, ":%s PRIVMSG %s :%s %s",
665 get_id(source_p, identifyservice_p),
666 ConfigFileEntry.identifyservice,
667 ConfigFileEntry.identifycommand,
668 source_p->localClient->passwd);
670 memset(source_p->localClient->passwd, 0, strlen(source_p->localClient->passwd));
671 MyFree(source_p->localClient->passwd);
672 source_p->localClient->passwd = NULL;
675 /* let modules providing usermodes know that we've got a new user,
676 * why is this here? -- well, some modules need to be able to send out new
677 * information about a client, so this was the best place to do it
678 * --nenolod
680 hdata.client = source_p;
681 hdata.oldumodes = 0;
682 hdata.oldsnomask = 0;
683 call_hook(h_umode_changed, &hdata);
685 /* On the other hand, some modules need to know when a client is
686 * being introduced, period.
687 * --gxti
689 hdata2.client = client_p;
690 hdata2.target = source_p;
691 call_hook(h_introduce_client, &hdata2);
693 return 0;
697 * valid_hostname - check hostname for validity
699 * Inputs - pointer to user
700 * Output - YES if valid, NO if not
701 * Side effects - NONE
703 * NOTE: this doesn't allow a hostname to begin with a dot and
704 * will not allow more dots than chars.
707 valid_hostname(const char *hostname)
709 const char *p = hostname;
710 int found_sep = 0;
712 s_assert(NULL != p);
714 if(hostname == NULL)
715 return NO;
717 if('.' == *p || ':' == *p || '/' == *p)
718 return NO;
720 while (*p)
722 if(!IsHostChar(*p))
723 return NO;
724 if(*p == '.' || *p == ':' || *p == '/')
725 found_sep++;
726 p++;
729 if(found_sep == 0)
730 return(NO);
732 return (YES);
736 * valid_username - check username for validity
738 * Inputs - pointer to user
739 * Output - YES if valid, NO if not
740 * Side effects - NONE
742 * Absolutely always reject any '*' '!' '?' '@' in an user name
743 * reject any odd control characters names.
744 * Allow '.' in username to allow for "first.last"
745 * style of username
748 valid_username(const char *username)
750 int dots = 0;
751 const char *p = username;
753 s_assert(NULL != p);
755 if(username == NULL)
756 return NO;
758 if('~' == *p)
759 ++p;
761 /* reject usernames that don't start with an alphanum
762 * i.e. reject jokers who have '-@somehost' or '.@somehost'
763 * or "-hi-@somehost", "h-----@somehost" would still be accepted.
765 if(!IsAlNum(*p))
766 return NO;
768 while (*++p)
770 if((*p == '.') && ConfigFileEntry.dots_in_ident)
772 dots++;
773 if(dots > ConfigFileEntry.dots_in_ident)
774 return NO;
775 if(!IsUserChar(p[1]))
776 return NO;
778 else if(!IsUserChar(*p))
779 return NO;
781 return YES;
784 /* report_and_set_user_flags
786 * Inputs - pointer to source_p
787 * - pointer to aconf for this user
788 * Output - NONE
789 * Side effects -
790 * Report to user any special flags they are getting, and set them.
793 static void
794 report_and_set_user_flags(struct Client *source_p, struct ConfItem *aconf)
796 /* If this user is being spoofed, tell them so */
797 if(IsConfDoSpoofIp(aconf))
799 sendto_one(source_p,
800 ":%s NOTICE %s :*** Spoofing your IP. congrats.",
801 me.name, source_p->name);
804 /* If this user is in the exception class, Set it "E lined" */
805 if(IsConfExemptKline(aconf))
807 SetExemptKline(source_p);
808 sendto_one(source_p,
809 ":%s NOTICE %s :*** You are exempt from K/D/X lines. congrats.",
810 me.name, source_p->name);
813 if(IsConfExemptDNSBL(aconf))
814 /* kline exempt implies this, don't send both */
815 if(!IsConfExemptKline(aconf))
816 sendto_one(source_p,
817 ":%s NOTICE %s :*** You are exempt from DNS blacklists.",
818 me.name, source_p->name);
820 /* If this user is exempt from user limits set it F lined" */
821 if(IsConfExemptLimits(aconf))
823 SetExemptLimits(source_p);
824 sendto_one(source_p,
825 ":%s NOTICE %s :*** You are exempt from user limits. congrats.",
826 me.name, source_p->name);
829 /* If this user is exempt from idle time outs */
830 if(IsConfIdlelined(aconf))
832 SetIdlelined(source_p);
833 sendto_one(source_p,
834 ":%s NOTICE %s :*** You are exempt from idle limits. congrats.",
835 me.name, source_p->name);
838 if(IsConfExemptFlood(aconf))
840 SetExemptFlood(source_p);
841 sendto_one(source_p,
842 ":%s NOTICE %s :*** You are exempt from flood limits.",
843 me.name, source_p->name);
846 if(IsConfExemptSpambot(aconf))
848 SetExemptSpambot(source_p);
849 sendto_one(source_p,
850 ":%s NOTICE %s :*** You are exempt from spambot checks.",
851 me.name, source_p->name);
854 if(IsConfExemptJupe(aconf))
856 SetExemptJupe(source_p);
857 sendto_one(source_p,
858 ":%s NOTICE %s :*** You are exempt from juped channel warnings.",
859 me.name, source_p->name);
862 if(IsConfExemptResv(aconf))
864 SetExemptResv(source_p);
865 sendto_one(source_p,
866 ":%s NOTICE %s :*** You are exempt from resvs.",
867 me.name, source_p->name);
870 if(IsConfExemptShide(aconf))
872 SetExemptShide(source_p);
873 sendto_one(source_p,
874 ":%s NOTICE %s :*** You are exempt from serverhiding.",
875 me.name, source_p->name);
880 * user_mode - set get current users mode
882 * m_umode() added 15/10/91 By Darren Reed.
883 * parv[0] - sender
884 * parv[1] - username to change mode for
885 * parv[2] - modes to change
888 user_mode(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
890 int flag;
891 int i;
892 char *m;
893 const char *pm;
894 struct Client *target_p;
895 int what, setflags;
896 int badflag = NO; /* Only send one bad flag notice */
897 int showsnomask = NO;
898 unsigned int setsnomask;
899 char buf[BUFSIZE];
900 hook_data_umode_changed hdata;
902 what = MODE_ADD;
904 if(parc < 2)
906 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS), me.name, source_p->name, "MODE");
907 return 0;
910 if((target_p = MyClient(source_p) ? find_named_person(parv[1]) : find_person(parv[1])) == NULL)
912 if(MyConnect(source_p))
913 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
914 form_str(ERR_NOSUCHCHANNEL), parv[1]);
915 return 0;
918 /* Dont know why these were commented out..
919 * put them back using new sendto() funcs
922 if(IsServer(source_p))
924 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
925 "*** Mode for User %s from %s", parv[1], source_p->name);
926 return 0;
929 if(source_p != target_p || target_p->from != source_p->from)
931 sendto_one(source_p, form_str(ERR_USERSDONTMATCH), me.name, source_p->name);
932 return 0;
935 if(parc < 3)
937 m = buf;
938 *m++ = '+';
940 for (i = 0; i < 128; i++) /* >= 127 is extended ascii */
941 if (source_p->umodes & user_modes[i])
942 *m++ = (char) i;
944 *m = '\0';
945 sendto_one(source_p, form_str(RPL_UMODEIS), me.name, source_p->name, buf);
946 if (source_p->snomask != 0)
947 sendto_one(source_p, form_str(RPL_SNOMASK), me.name, source_p->name,
948 construct_snobuf(source_p->snomask));
949 return 0;
952 /* find flags already set for user */
953 setflags = source_p->umodes;
954 setsnomask = source_p->snomask;
957 * parse mode change string(s)
959 for (pm = parv[2]; *pm; pm++)
960 switch (*pm)
962 case '+':
963 what = MODE_ADD;
964 break;
965 case '-':
966 what = MODE_DEL;
967 break;
969 case 'o':
970 if(what == MODE_ADD)
972 if(IsServer(client_p) && !IsOper(source_p))
974 ++Count.oper;
975 SetOper(source_p);
976 dlinkAddAlloc(source_p, &oper_list);
979 else
981 /* Only decrement the oper counts if an oper to begin with
982 * found by Pat Szuta, Perly , perly@xnet.com
985 if(!IsOper(source_p))
986 break;
988 ClearOper(source_p);
990 Count.oper--;
992 if(MyConnect(source_p))
994 source_p->umodes &= ~ConfigFileEntry.oper_only_umodes;
995 if (!(source_p->umodes & UMODE_SERVNOTICE) && source_p->snomask != 0)
997 source_p->snomask = 0;
998 showsnomask = YES;
1000 source_p->operflags = 0;
1002 MyFree(source_p->localClient->opername);
1003 source_p->localClient->opername = NULL;
1005 dlinkFindDestroy(source_p, &local_oper_list);
1008 dlinkFindDestroy(source_p, &oper_list);
1010 break;
1012 /* we may not get these,
1013 * but they shouldnt be in default
1016 /* can only be set on burst */
1017 case 'S':
1018 case ' ':
1019 case '\n':
1020 case '\r':
1021 case '\t':
1022 break;
1024 case 's':
1025 if (MyConnect(source_p))
1027 if(!IsOper(source_p)
1028 && (ConfigFileEntry.oper_only_umodes & UMODE_SERVNOTICE))
1030 if (what == MODE_ADD || source_p->umodes & UMODE_SERVNOTICE)
1031 badflag = YES;
1032 continue;
1034 showsnomask = YES;
1035 if(what == MODE_ADD)
1037 if (parc > 3)
1038 source_p->snomask = parse_snobuf_to_mask(source_p->snomask, parv[3]);
1039 else
1040 source_p->snomask |= SNO_GENERAL;
1042 else
1043 source_p->snomask = 0;
1044 if (source_p->snomask != 0)
1045 source_p->umodes |= UMODE_SERVNOTICE;
1046 else
1047 source_p->umodes &= ~UMODE_SERVNOTICE;
1048 break;
1050 /* FALLTHROUGH */
1051 default:
1052 if((flag = user_modes[(unsigned char) *pm]))
1054 if(MyConnect(source_p)
1055 && !IsOper(source_p)
1056 && (ConfigFileEntry.oper_only_umodes & flag))
1058 if (what == MODE_ADD || source_p->umodes & flag)
1059 badflag = YES;
1061 else
1063 if(what == MODE_ADD)
1064 source_p->umodes |= flag;
1065 else
1066 source_p->umodes &= ~flag;
1069 else
1071 if(MyConnect(source_p))
1072 badflag = YES;
1074 break;
1077 if(badflag)
1078 sendto_one(source_p, form_str(ERR_UMODEUNKNOWNFLAG), me.name, source_p->name);
1080 if(MyClient(source_p) && (source_p->umodes & UMODE_OPERWALL) && !IsOperOperwall(source_p))
1082 if(IsOper(source_p))
1083 sendto_one(source_p,
1084 ":%s NOTICE %s :*** You need the operwall flag for +z", me.name, parv[0]);
1085 else
1086 sendto_one(source_p, form_str(ERR_UMODEUNKNOWNFLAG), me.name, source_p->name);
1087 source_p->umodes &= ~UMODE_OPERWALL;
1090 if(MyConnect(source_p) && (source_p->umodes & UMODE_ADMIN) &&
1091 (!IsOperAdmin(source_p) || IsOperHiddenAdmin(source_p)))
1093 if(IsOper(source_p))
1094 sendto_one(source_p,
1095 ":%s NOTICE %s :*** You need the A flag for +a", me.name, parv[0]);
1096 else
1097 sendto_one(source_p, form_str(ERR_UMODEUNKNOWNFLAG), me.name, source_p->name);
1098 source_p->umodes &= ~UMODE_ADMIN;
1101 if(MyClient(source_p) && (source_p->umodes & UMODE_HELPER) && !IsOperHelper(source_p))
1103 if(IsOper(source_p))
1104 sendto_one(source_p,
1105 ":%s NOTICE %s :*** You need the helper flag for +T", me.name, parv[0]);
1106 else
1107 sendto_one(source_p, form_str(ERR_UMODEUNKNOWNFLAG), me.name, source_p->name);
1109 source_p->umodes &= ~UMODE_HELPER;
1112 if(MyClient(source_p) && (source_p->umodes & UMODE_OVERRIDE) && !IsOperOverride(source_p))
1114 if(IsOper(source_p))
1115 sendto_one(source_p,
1116 ":%s NOTICE %s :*** You need the override flag for +p", me.name, parv[0]);
1117 else
1118 sendto_one(source_p, form_str(ERR_UMODEUNKNOWNFLAG), me.name, source_p->name);
1120 source_p->umodes &= ~UMODE_OVERRIDE;
1123 if(MyClient(source_p) &&
1124 ((source_p->snomask & source_p->allowed_snomask) != source_p->snomask))
1126 sendto_one(source_p,
1127 ":%s NOTICE %s :*** Server notices not granted: %s",
1128 me.name, source_p->name,
1129 construct_snobuf(source_p->snomask & ~source_p->allowed_snomask));
1130 source_p->snomask &= source_p->allowed_snomask;
1133 /* let modules providing usermodes know that we've changed our usermode --nenolod */
1134 hdata.client = source_p;
1135 hdata.oldumodes = setflags;
1136 hdata.oldsnomask = setsnomask;
1137 call_hook(h_umode_changed, &hdata);
1139 if(!(setflags & UMODE_INVISIBLE) && IsInvisible(source_p))
1140 ++Count.invisi;
1141 if((setflags & UMODE_INVISIBLE) && !IsInvisible(source_p))
1142 --Count.invisi;
1144 * compare new flags with old flags and send string which
1145 * will cause servers to update correctly.
1147 send_umode_out(client_p, source_p, setflags);
1148 if (showsnomask && MyConnect(source_p))
1149 sendto_one(source_p, form_str(RPL_SNOMASK), me.name, source_p->name,
1150 construct_snobuf(source_p->snomask));
1152 return (0);
1156 * send the MODE string for user (user) to connection client_p
1157 * -avalon
1159 void
1160 send_umode(struct Client *client_p, struct Client *source_p, int old, int sendmask, char *umode_buf)
1162 int i;
1163 int flag;
1164 char *m;
1165 int what = 0;
1168 * build a string in umode_buf to represent the change in the user's
1169 * mode between the new (source_p->flag) and 'old'.
1171 m = umode_buf;
1172 *m = '\0';
1174 for (i = 0; i < 128; i++)
1176 flag = user_modes[i];
1178 if((flag & old) && !(source_p->umodes & flag))
1180 if(what == MODE_DEL)
1181 *m++ = (char) i;
1182 else
1184 what = MODE_DEL;
1185 *m++ = '-';
1186 *m++ = (char) i;
1189 else if(!(flag & old) && (source_p->umodes & flag))
1191 if(what == MODE_ADD)
1192 *m++ = (char) i;
1193 else
1195 what = MODE_ADD;
1196 *m++ = '+';
1197 *m++ = (char) i;
1201 *m = '\0';
1203 if(*umode_buf && client_p)
1204 sendto_one(client_p, ":%s MODE %s :%s", source_p->name, source_p->name, umode_buf);
1208 * send_umode_out
1210 * inputs -
1211 * output - NONE
1212 * side effects -
1214 void
1215 send_umode_out(struct Client *client_p, struct Client *source_p, int old)
1217 struct Client *target_p;
1218 char buf[BUFSIZE];
1219 dlink_node *ptr;
1221 send_umode(NULL, source_p, old, 0, buf);
1223 DLINK_FOREACH(ptr, serv_list.head)
1225 target_p = ptr->data;
1227 if((target_p != client_p) && (target_p != source_p) && (*buf))
1229 sendto_one(target_p, ":%s MODE %s :%s",
1230 get_id(source_p, target_p),
1231 get_id(source_p, target_p), buf);
1235 if(client_p && MyClient(client_p))
1236 send_umode(client_p, source_p, old, 0, buf);
1240 * user_welcome
1242 * inputs - client pointer to client to welcome
1243 * output - NONE
1244 * side effects -
1246 void
1247 user_welcome(struct Client *source_p)
1249 sendto_one(source_p, form_str(RPL_WELCOME), me.name, source_p->name,
1250 ServerInfo.network_name, source_p->name);
1251 sendto_one(source_p, form_str(RPL_YOURHOST), me.name,
1252 source_p->name,
1253 get_listener_name(source_p->localClient->listener), ircd_version);
1255 sendto_one(source_p, form_str(RPL_CREATED), me.name, source_p->name, creation);
1256 sendto_one(source_p, form_str(RPL_MYINFO), me.name, source_p->name, me.name, ircd_version, umodebuf);
1258 show_isupport(source_p);
1260 show_lusers(source_p);
1262 if(ConfigFileEntry.short_motd)
1264 sendto_one(source_p,
1265 "NOTICE %s :*** Notice -- motd was last changed at %s",
1266 source_p->name, user_motd_changed);
1268 sendto_one(source_p,
1269 "NOTICE %s :*** Notice -- Please read the motd if you haven't read it",
1270 source_p->name);
1272 sendto_one(source_p, form_str(RPL_MOTDSTART),
1273 me.name, source_p->name, me.name);
1275 sendto_one(source_p, form_str(RPL_MOTD),
1276 me.name, source_p->name, "*** This is the short motd ***");
1278 sendto_one(source_p, form_str(RPL_ENDOFMOTD), me.name, source_p->name);
1280 else
1281 send_user_motd(source_p);
1284 /* {{{ int oper_up()
1286 * Opers up client pointed to by source_p, using aconf for reference.
1289 oper_up (struct Client *source_p, struct oper_conf *oper_p)
1291 unsigned int old_umodes = source_p->umodes;
1292 unsigned int old_snomask = source_p->snomask;
1293 hook_data_umode_changed hdata;
1295 SetOper(source_p);
1296 SetExemptKline(source_p);
1297 ++Count.oper;
1299 if (oper_p->umodes)
1300 source_p->umodes |= oper_p->umodes;
1301 else if (ConfigFileEntry.oper_umodes)
1302 source_p->umodes |= ConfigFileEntry.oper_umodes;
1303 else
1304 source_p->umodes |= DEFAULT_OPER_UMODES;
1306 if (oper_p->snomask)
1308 source_p->umodes |= UMODE_SERVNOTICE;
1309 source_p->snomask |= oper_p->snomask & oper_p->allowed_snomask;
1311 else if (source_p->umodes & UMODE_SERVNOTICE)
1313 /* Only apply these if +s is already set -- jilles */
1314 if (ConfigFileEntry.oper_snomask)
1315 source_p->snomask |= ConfigFileEntry.oper_snomask;
1316 else
1317 source_p->snomask |= DEFAULT_OPER_SNOMASK;
1320 source_p->allowed_snomask = oper_p->allowed_snomask;
1321 source_p->operflags = oper_p->flags;
1322 DupString(source_p->localClient->opername, oper_p->name);
1324 dlinkAddAlloc(source_p, &local_oper_list);
1325 dlinkAddAlloc(source_p, &oper_list);
1327 if(IsOperAdmin(source_p) && !IsOperHiddenAdmin(source_p))
1328 source_p->umodes |= UMODE_ADMIN;
1329 if(!IsOperOperwall(source_p))
1330 source_p->umodes &= ~UMODE_OPERWALL;
1332 hdata.client = source_p;
1333 hdata.oldumodes = old_umodes;
1334 hdata.oldsnomask = old_snomask;
1335 call_hook(h_umode_changed, &hdata);
1337 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
1338 "%s (%s@%s) is now an operator", source_p->name,
1339 source_p->username, source_p->host);
1341 send_umode_out(source_p, source_p, old_umodes);
1342 sendto_one(source_p, form_str(RPL_SNOMASK), me.name, source_p->name,
1343 construct_snobuf(source_p->snomask));
1345 sendto_one(source_p, form_str(RPL_YOUREOPER), me.name, source_p->name);
1346 sendto_one(source_p, ":%s NOTICE %s :*** Oper privs are %s", me.name,
1347 source_p->name, get_oper_privs(oper_p->flags));
1348 sendto_one(source_p, ":%s NOTICE %s :*** Allowed snomasks are %s", me.name,
1349 source_p->name, construct_snobuf(source_p->allowed_snomask));
1351 send_oper_motd(source_p);
1353 sendto_server(source_p, NULL, CAP_TS6, NOCAPS, ":%s ENCAP * OPER %s",
1354 use_id(source_p), get_oper_privs(oper_p->flags));
1356 sendto_server(source_p, NULL, NOCAPS, CAP_TS6, ":%s ENCAP * OPER %s",
1357 source_p->name, get_oper_privs(oper_p->flags));
1359 return 1;
1362 void
1363 construct_umodebuf(void)
1365 int i;
1366 char *ptr = umodebuf;
1368 *ptr = '\0';
1370 for (i = 0; i < 128; i++)
1371 if (user_modes[i])
1372 *ptr++ = (char) i;
1374 *ptr++ = '\0';
1377 void
1378 change_nick_user_host(struct Client *target_p, const char *nick, const char *user,
1379 const char *host, int newts, char *format, ...)
1381 dlink_node *ptr;
1382 struct Channel *chptr;
1383 struct membership *mscptr;
1384 int changed = irccmp(target_p->name, nick);
1385 int changed_case = strcmp(target_p->name, nick);
1386 int do_qjm = irccmp(target_p->username, user) || irccmp(target_p->host, host);
1387 char mode[10], modeval[NICKLEN * 2 + 2], reason[256], *mptr;
1388 va_list ap;
1390 modeval[0] = '\0';
1392 if(changed)
1394 target_p->tsinfo = newts;
1395 monitor_signoff(target_p);
1397 invalidate_bancache_user(target_p);
1399 if(do_qjm)
1401 va_start(ap, format);
1402 vsnprintf(reason, 255, format, ap);
1403 va_end(ap);
1405 sendto_common_channels_local_butone(target_p, ":%s!%s@%s QUIT :%s",
1406 target_p->name, target_p->username, target_p->host,
1407 reason);
1409 DLINK_FOREACH(ptr, target_p->user->channel.head)
1411 mscptr = ptr->data;
1412 chptr = mscptr->chptr;
1413 mptr = mode;
1415 if(is_real_chanop(mscptr))
1417 *mptr++ = 'o';
1418 strcat(modeval, nick);
1419 strcat(modeval, " ");
1422 if(is_voiced(mscptr))
1424 *mptr++ = 'v';
1425 strcat(modeval, nick);
1428 *mptr = '\0';
1430 sendto_channel_local_butone(target_p, ALL_MEMBERS, chptr, ":%s!%s@%s JOIN :%s",
1431 nick, user, host, chptr->chname);
1432 if(*mode)
1433 sendto_channel_local_butone(target_p, ALL_MEMBERS, chptr,
1434 ":%s MODE %s +%s %s",
1435 target_p->servptr->name,
1436 chptr->chname, mode, modeval);
1438 *modeval = '\0';
1441 if(MyClient(target_p) && changed_case)
1442 sendto_one(target_p, ":%s!%s@%s NICK %s",
1443 target_p->name, target_p->username, target_p->host, nick);
1445 else if(changed_case)
1447 sendto_common_channels_local(target_p, ":%s!%s@%s NICK :%s",
1448 target_p->name, target_p->username,
1449 target_p->host, nick);
1452 strlcpy(target_p->username, user, USERLEN);
1453 strlcpy(target_p->host, host, HOSTLEN);
1455 if (changed)
1456 add_history(target_p, 1);
1458 del_from_client_hash(target_p->name, target_p);
1459 strlcpy(target_p->name, nick, NICKLEN);
1460 add_to_client_hash(target_p->name, target_p);
1462 if(changed)
1464 monitor_signon(target_p);
1465 del_all_accepts(target_p);