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
24 * $Id: s_user.c 3586 2007-11-20 11:16:43Z nenolod $
40 #include "s_newconf.h"
46 #include "supported.h"
54 #include "blacklist.h"
55 #include "substitution.h"
59 static void report_and_set_user_flags(struct Client
*, struct ConfItem
*);
60 void user_welcome(struct Client
*source_p
);
64 static int orphaned_umodes
= 0;
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 */
87 UMODE_NOFORWARD
, /* Q */
88 UMODE_REGONLYMSG
, /* R */
89 UMODE_SERVICE
, /* S */
96 UMODE_SSLCLIENT
, /* Z */
97 /* 0x5B */ 0, 0, 0, 0, 0, 0, /* 0x60 */
104 UMODE_CALLERID
, /* g */
106 UMODE_INVISIBLE
, /* i */
109 UMODE_LOCOPS
, /* l */
116 UMODE_SERVNOTICE
, /* s */
120 UMODE_WALLOP
, /* w */
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 */
139 * inputs - pointer to client
141 * side effects - display to client user counts etc.
144 show_lusers(struct Client
*source_p
)
146 if(rb_dlink_list_length(&lclient_list
) > (unsigned long)MaxClientCount
)
147 MaxClientCount
= rb_dlink_list_length(&lclient_list
);
149 if((rb_dlink_list_length(&lclient_list
) + rb_dlink_list_length(&serv_list
)) >
150 (unsigned long)MaxConnectionCount
)
151 MaxConnectionCount
= rb_dlink_list_length(&lclient_list
) +
152 rb_dlink_list_length(&serv_list
);
154 sendto_one_numeric(source_p
, RPL_LUSERCLIENT
, form_str(RPL_LUSERCLIENT
),
155 (Count
.total
- Count
.invisi
),
157 (int)rb_dlink_list_length(&global_serv_list
));
159 if(rb_dlink_list_length(&oper_list
) > 0)
160 sendto_one_numeric(source_p
, RPL_LUSEROP
,
161 form_str(RPL_LUSEROP
),
162 (int)rb_dlink_list_length(&oper_list
));
164 if(rb_dlink_list_length(&unknown_list
) > 0)
165 sendto_one_numeric(source_p
, RPL_LUSERUNKNOWN
,
166 form_str(RPL_LUSERUNKNOWN
),
167 (int)rb_dlink_list_length(&unknown_list
));
169 if(rb_dlink_list_length(&global_channel_list
) > 0)
170 sendto_one_numeric(source_p
, RPL_LUSERCHANNELS
,
171 form_str(RPL_LUSERCHANNELS
),
172 rb_dlink_list_length(&global_channel_list
));
174 sendto_one_numeric(source_p
, RPL_LUSERME
, form_str(RPL_LUSERME
),
175 (int)rb_dlink_list_length(&lclient_list
),
176 (int)rb_dlink_list_length(&serv_list
));
178 sendto_one_numeric(source_p
, RPL_LOCALUSERS
,
179 form_str(RPL_LOCALUSERS
),
180 (int)rb_dlink_list_length(&lclient_list
),
182 (int)rb_dlink_list_length(&lclient_list
),
185 sendto_one_numeric(source_p
, RPL_GLOBALUSERS
, form_str(RPL_GLOBALUSERS
),
186 Count
.total
, Count
.max_tot
,
187 Count
.total
, Count
.max_tot
);
189 sendto_one_numeric(source_p
, RPL_STATSCONN
,
190 form_str(RPL_STATSCONN
),
191 MaxConnectionCount
, MaxClientCount
,
192 Count
.totalrestartcount
);
198 ** register_local_user
199 ** This function is called when both NICK and USER messages
200 ** have been accepted for the client, in whatever order. Only
201 ** after this, is the USER message propagated.
203 ** NICK's must be propagated at once when received, although
204 ** it would be better to delay them too until full info is
205 ** available. Doing it is not so simple though, would have
206 ** to implement the following:
208 ** (actually it has been implemented already for a while) -orabidoo
210 ** 1) user telnets in and gives only "NICK foobar" and waits
211 ** 2) another user far away logs in normally with the nick
212 ** "foobar" (quite legal, as this server didn't propagate
214 ** 3) now this server gets nick "foobar" from outside, but
215 ** has alread the same defined locally. Current server
216 ** would just issue "KILL foobar" to clean out dups. But,
217 ** this is not fair. It should actually request another
218 ** nick from local user or kill him/her...
222 register_local_user(struct Client
*client_p
, struct Client
*source_p
, const char *username
)
224 struct ConfItem
*aconf
, *xconf
;
225 struct User
*user
= source_p
->user
;
226 char tmpstr2
[IRCD_BUFSIZE
];
227 char ipaddr
[HOSTIPLEN
];
228 char myusername
[USERLEN
+1];
231 s_assert(NULL
!= source_p
);
232 s_assert(MyConnect(source_p
));
233 s_assert(source_p
->username
!= username
);
238 if(IsAnyDead(source_p
))
241 if(ConfigFileEntry
.ping_cookie
)
243 if(!(source_p
->flags
& FLAGS_PINGSENT
) && source_p
->localClient
->random_ping
== 0)
245 source_p
->localClient
->random_ping
= (unsigned long) (rand() * rand()) << 1;
246 sendto_one(source_p
, "PING :%08lX",
247 (unsigned long) source_p
->localClient
->random_ping
);
248 source_p
->flags
|= FLAGS_PINGSENT
;
251 if(!(source_p
->flags
& FLAGS_PING_COOKIE
))
257 /* hasnt finished client cap negotiation */
258 if(source_p
->flags
& FLAGS_CLICAP
)
261 /* still has DNSbls to validate against */
262 if(rb_dlink_list_length(&source_p
->preClient
->dnsbl_queries
) > 0)
265 client_p
->localClient
->last
= rb_current_time();
267 /* XXX - fixme. we shouldnt have to build a users buffer twice.. */
268 if(!IsGotId(source_p
) && (strchr(username
, '[') != NULL
))
275 while(*p
&& i
< USERLEN
)
278 myusername
[i
++] = *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 char note
[NICKLEN
+ 10];
294 del_from_client_hash(source_p
->name
, source_p
);
295 rb_strlcpy(source_p
->name
, source_p
->preClient
->spoofnick
, NICKLEN
+ 1);
296 add_to_client_hash(source_p
->name
, source_p
);
298 rb_snprintf(note
, NICKLEN
+ 10, "Nick: %s", source_p
->name
);
299 rb_note(source_p
->localClient
->F
, note
);
302 if(!valid_hostname(source_p
->host
))
304 sendto_one_notice(source_p
, ":*** Notice -- You have an illegal character in your hostname");
306 rb_strlcpy(source_p
->host
, source_p
->sockhost
, sizeof(source_p
->host
));
310 aconf
= source_p
->localClient
->att_conf
;
314 exit_client(client_p
, source_p
, &me
, "*** Not Authorised");
315 return (CLIENT_EXITED
);
318 if(IsConfSSLNeeded(aconf
) && !IsSSL(source_p
))
320 ServerStats
.is_ref
++;
321 sendto_one_notice(source_p
, ":*** Notice -- You need to use SSL/TLS to use this server");
322 exit_client(client_p
, source_p
, &me
, "Use SSL/TLS");
323 return (CLIENT_EXITED
);
326 if(!IsGotId(source_p
))
331 if(IsNeedIdentd(aconf
))
333 ServerStats
.is_ref
++;
334 sendto_one_notice(source_p
, ":*** Notice -- You need to install identd to use this server");
335 exit_client(client_p
, source_p
, &me
, "Install identd");
336 return (CLIENT_EXITED
);
339 /* dont replace username if its supposed to be spoofed --fl */
340 if(!IsConfDoSpoofIp(aconf
) || !strchr(aconf
->info
.name
, '@'))
344 if(!IsNoTilde(aconf
))
345 source_p
->username
[i
++] = '~';
347 while (*p
&& i
< USERLEN
)
350 source_p
->username
[i
++] = *p
;
354 source_p
->username
[i
] = '\0';
358 if(IsNeedSasl(aconf
) && !*user
->suser
)
360 ServerStats
.is_ref
++;
361 sendto_one_notice(source_p
, ":*** Notice -- You need to identify via SASL to use this server");
362 exit_client(client_p
, source_p
, &me
, "SASL access only");
363 return (CLIENT_EXITED
);
367 if(!EmptyString(aconf
->passwd
))
371 if(EmptyString(source_p
->localClient
->passwd
))
373 else if(IsConfEncrypted(aconf
))
374 encr
= rb_crypt(source_p
->localClient
->passwd
, aconf
->passwd
);
376 encr
= source_p
->localClient
->passwd
;
378 if(encr
== NULL
|| strcmp(encr
, aconf
->passwd
))
380 ServerStats
.is_ref
++;
381 sendto_one(source_p
, form_str(ERR_PASSWDMISMATCH
), me
.name
, source_p
->name
);
382 exit_client(client_p
, source_p
, &me
, "Bad Password");
383 return (CLIENT_EXITED
);
386 /* clear password only if used now, otherwise send it
387 * to services -- jilles */
388 if(source_p
->localClient
->passwd
)
390 memset(source_p
->localClient
->passwd
, 0, strlen(source_p
->localClient
->passwd
));
391 rb_free(source_p
->localClient
->passwd
);
392 source_p
->localClient
->passwd
= NULL
;
396 /* report if user has &^>= etc. and set flags as needed in source_p */
397 report_and_set_user_flags(source_p
, aconf
);
401 * We want to be able to have servers and F-line clients
402 * connect, so save room for "buffer" connections.
403 * Smaller servers may want to decrease this, and it should
404 * probably be just a percentage of the MAXCLIENTS...
407 /* Except "F:" clients */
408 if(rb_dlink_list_length(&lclient_list
) >=
409 (unsigned long)GlobalSetOptions
.maxclients
&& !IsConfExemptLimits(aconf
))
411 sendto_realops_snomask(SNO_FULL
, L_ALL
,
412 "Too many clients, rejecting %s[%s].", source_p
->name
, source_p
->host
);
414 ServerStats
.is_ref
++;
415 exit_client(client_p
, source_p
, &me
, "Sorry, server is full - try later");
416 return (CLIENT_EXITED
);
419 /* kline exemption extends to xline too */
420 if(!IsExemptKline(source_p
) &&
421 (xconf
= find_xline(source_p
->info
, 1)) != NULL
)
423 ServerStats
.is_ref
++;
424 add_reject(source_p
, xconf
->host
, NULL
);
425 exit_client(client_p
, source_p
, &me
, "Bad user info");
426 return CLIENT_EXITED
;
430 if (source_p
->preClient
->dnsbl_listed
!= NULL
)
432 if (IsExemptKline(source_p
) || IsConfExemptDNSBL(aconf
))
433 sendto_one_notice(source_p
, ":*** Your IP address %s is listed in %s, but you are exempt",
434 source_p
->sockhost
, source_p
->preClient
->dnsbl_listed
->host
);
437 sendto_realops_snomask(SNO_REJ
, L_NETWIDE
,
438 "Listed on DNSBL %s: %s (%s@%s) [%s] [%s]",
439 source_p
->preClient
->dnsbl_listed
->host
,
441 source_p
->username
, source_p
->host
,
442 IsIPSpoof(source_p
) ? "255.255.255.255" : source_p
->sockhost
,
445 rb_dlink_list varlist
= { NULL
, NULL
, 0 };
447 substitution_append_var(&varlist
, "nick", source_p
->name
);
448 substitution_append_var(&varlist
, "ip", source_p
->sockhost
);
449 substitution_append_var(&varlist
, "host", source_p
->host
);
450 substitution_append_var(&varlist
, "dnsbl-host", source_p
->preClient
->dnsbl_listed
->host
);
451 substitution_append_var(&varlist
, "network-name", ServerInfo
.network_name
);
453 ServerStats
.is_ref
++;
455 sendto_one(source_p
, form_str(ERR_YOUREBANNEDCREEP
),
456 me
.name
, source_p
->name
,
457 substitution_parse(source_p
->preClient
->dnsbl_listed
->reject_reason
, &varlist
));
459 substitution_free(&varlist
);
461 sendto_one_notice(source_p
, ":*** Your IP address %s is listed in %s",
462 source_p
->sockhost
, source_p
->preClient
->dnsbl_listed
->host
);
463 source_p
->preClient
->dnsbl_listed
->hits
++;
464 add_reject(source_p
, NULL
, NULL
);
465 exit_client(client_p
, source_p
, &me
, "*** Banned (DNS blacklist)");
466 return CLIENT_EXITED
;
470 /* valid user name check */
472 if(!valid_username(source_p
->username
))
474 sendto_realops_snomask(SNO_REJ
, L_ALL
,
475 "Invalid username: %s (%s@%s)",
476 source_p
->name
, source_p
->username
, source_p
->host
);
477 ServerStats
.is_ref
++;
478 sendto_one_notice(source_p
, ":*** Your username is invalid. Please make sure that your username contains "
479 "only alphanumeric characters.");
480 rb_sprintf(tmpstr2
, "Invalid username [%s]", source_p
->username
);
481 exit_client(client_p
, source_p
, &me
, tmpstr2
);
482 return (CLIENT_EXITED
);
485 /* end of valid user name check */
487 /* Store original hostname -- jilles */
488 rb_strlcpy(source_p
->orighost
, source_p
->host
, HOSTLEN
+ 1);
490 /* Spoof user@host */
491 if(*source_p
->preClient
->spoofuser
)
492 rb_strlcpy(source_p
->username
, source_p
->preClient
->spoofuser
, USERLEN
+ 1);
493 if(*source_p
->preClient
->spoofhost
)
495 rb_strlcpy(source_p
->host
, source_p
->preClient
->spoofhost
, HOSTLEN
+ 1);
496 if (irccmp(source_p
->host
, source_p
->orighost
))
497 SetDynSpoof(source_p
);
500 source_p
->umodes
|= ConfigFileEntry
.default_umodes
& ~ConfigFileEntry
.oper_only_umodes
& ~orphaned_umodes
;
502 call_hook(h_new_local_user
, source_p
);
504 /* If they have died in send_* or were thrown out by the
505 * new_local_user hook don't do anything. */
506 if(IsAnyDead(source_p
))
507 return CLIENT_EXITED
;
509 /* To avoid inconsistencies, do not abort the registration
510 * starting from this point -- jilles
512 rb_inet_ntop_sock((struct sockaddr
*)&source_p
->localClient
->ip
, ipaddr
, sizeof(ipaddr
));
514 sendto_realops_snomask(SNO_CCONN
, L_ALL
,
515 "Client connecting: %s (%s@%s) [%s] {%s} [%s]",
516 source_p
->name
, source_p
->username
, source_p
->orighost
,
517 show_ip(NULL
, source_p
) ? ipaddr
: "255.255.255.255",
518 get_client_class(source_p
), source_p
->info
);
520 sendto_realops_snomask(SNO_CCONNEXT
, L_ALL
,
521 "CLICONN %s %s %s %s %s %s 0 %s",
522 source_p
->name
, source_p
->username
, source_p
->orighost
,
523 show_ip(NULL
, source_p
) ? ipaddr
: "255.255.255.255",
524 get_client_class(source_p
),
525 /* mirc can sometimes send ips here */
526 show_ip(NULL
, source_p
) ? source_p
->localClient
->fullcaps
: "<hidden> <hidden>",
529 add_to_hostname_hash(source_p
->orighost
, source_p
);
531 /* Allocate a UID if it was not previously allocated.
532 * If this already occured, it was probably during SASL auth...
536 strcpy(source_p
->id
, generate_uid());
537 add_to_id_hash(source_p
->id
, source_p
);
541 source_p
->umodes
|= UMODE_SSLCLIENT
;
543 if (source_p
->umodes
& UMODE_INVISIBLE
)
546 s_assert(!IsClient(source_p
));
547 rb_dlinkMoveNode(&source_p
->localClient
->tnode
, &unknown_list
, &lclient_list
);
550 source_p
->servptr
= &me
;
551 rb_dlinkAdd(source_p
, &source_p
->lnode
, &source_p
->servptr
->serv
->users
);
553 /* Increment our total user count here */
554 if(++Count
.total
> Count
.max_tot
)
555 Count
.max_tot
= Count
.total
;
557 Count
.totalrestartcount
++;
559 s_assert(source_p
->localClient
!= NULL
);
561 if(rb_dlink_list_length(&lclient_list
) > (unsigned long)Count
.max_loc
)
563 Count
.max_loc
= rb_dlink_list_length(&lclient_list
);
564 if(!(Count
.max_loc
% 10))
565 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
566 "New Max Local Clients: %d", Count
.max_loc
);
569 /* they get a reduced limit */
570 if(find_tgchange(source_p
->sockhost
))
571 source_p
->localClient
->targets_free
= TGCHANGE_INITIAL_LOW
;
573 source_p
->localClient
->targets_free
= TGCHANGE_INITIAL
;
575 monitor_signon(source_p
);
576 user_welcome(source_p
);
578 free_pre_client(source_p
);
580 return (introduce_client(client_p
, source_p
, user
, source_p
->name
, 1));
588 * side effects - This common function introduces a client to the rest
589 * of the net, either from a local client connect or
590 * from a remote connect.
593 introduce_client(struct Client
*client_p
, struct Client
*source_p
, struct User
*user
, const char *nick
, int use_euid
)
596 struct Client
*identifyservice_p
;
598 hook_data_umode_changed hdata
;
599 hook_data_client hdata2
;
601 if(MyClient(source_p
))
602 send_umode(source_p
, source_p
, 0, 0, ubuf
);
604 send_umode(NULL
, source_p
, 0, 0, ubuf
);
612 s_assert(has_id(source_p
));
615 sendto_server(client_p
, NULL
, CAP_EUID
| CAP_TS6
, NOCAPS
,
616 ":%s EUID %s %d %ld %s %s %s %s %s %s %s :%s",
617 source_p
->servptr
->id
, nick
,
618 source_p
->hopcount
+ 1,
619 (long) source_p
->tsinfo
, ubuf
,
620 source_p
->username
, source_p
->host
,
621 IsIPSpoof(source_p
) ? "0" : source_p
->sockhost
,
623 IsDynSpoof(source_p
) ? source_p
->orighost
: "*",
624 EmptyString(source_p
->user
->suser
) ? "*" : source_p
->user
->suser
,
627 sendto_server(client_p
, NULL
, CAP_TS6
, use_euid
? CAP_EUID
: NOCAPS
,
628 ":%s UID %s %d %ld %s %s %s %s %s :%s",
629 source_p
->servptr
->id
, nick
,
630 source_p
->hopcount
+ 1,
631 (long) source_p
->tsinfo
, ubuf
,
632 source_p
->username
, source_p
->host
,
633 IsIPSpoof(source_p
) ? "0" : source_p
->sockhost
,
634 source_p
->id
, source_p
->info
);
636 if(!EmptyString(source_p
->certfp
))
637 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
638 ":%s ENCAP * CERTFP :%s",
639 use_id(source_p
), source_p
->certfp
);
641 if (IsDynSpoof(source_p
))
643 sendto_server(client_p
, NULL
, CAP_TS6
, use_euid
? CAP_EUID
: NOCAPS
, ":%s ENCAP * REALHOST %s",
644 use_id(source_p
), source_p
->orighost
);
647 if (!EmptyString(source_p
->user
->suser
))
649 sendto_server(client_p
, NULL
, CAP_TS6
, use_euid
? CAP_EUID
: NOCAPS
, ":%s ENCAP * LOGIN %s",
650 use_id(source_p
), source_p
->user
->suser
);
653 if(MyConnect(source_p
) && source_p
->localClient
->passwd
)
655 if (!EmptyString(ConfigFileEntry
.identifyservice
) &&
656 !EmptyString(ConfigFileEntry
.identifycommand
))
658 /* use user@server */
659 p
= strchr(ConfigFileEntry
.identifyservice
, '@');
661 identifyservice_p
= find_named_client(p
+ 1);
663 identifyservice_p
= NULL
;
664 if (identifyservice_p
!= NULL
)
666 if (!EmptyString(source_p
->localClient
->auth_user
))
667 sendto_one(identifyservice_p
, ":%s PRIVMSG %s :%s %s %s",
668 get_id(source_p
, identifyservice_p
),
669 ConfigFileEntry
.identifyservice
,
670 ConfigFileEntry
.identifycommand
,
671 source_p
->localClient
->auth_user
,
672 source_p
->localClient
->passwd
);
674 sendto_one(identifyservice_p
, ":%s PRIVMSG %s :%s %s",
675 get_id(source_p
, identifyservice_p
),
676 ConfigFileEntry
.identifyservice
,
677 ConfigFileEntry
.identifycommand
,
678 source_p
->localClient
->passwd
);
681 memset(source_p
->localClient
->passwd
, 0, strlen(source_p
->localClient
->passwd
));
682 rb_free(source_p
->localClient
->passwd
);
683 source_p
->localClient
->passwd
= NULL
;
686 /* let modules providing usermodes know that we've got a new user,
687 * why is this here? -- well, some modules need to be able to send out new
688 * information about a client, so this was the best place to do it
691 hdata
.client
= source_p
;
693 hdata
.oldsnomask
= 0;
694 call_hook(h_umode_changed
, &hdata
);
696 /* On the other hand, some modules need to know when a client is
697 * being introduced, period.
700 hdata2
.client
= client_p
;
701 hdata2
.target
= source_p
;
702 call_hook(h_introduce_client
, &hdata2
);
708 * valid_hostname - check hostname for validity
710 * Inputs - pointer to user
711 * Output - YES if valid, NO if not
712 * Side effects - NONE
714 * NOTE: this doesn't allow a hostname to begin with a dot and
715 * will not allow more dots than chars.
718 valid_hostname(const char *hostname
)
720 const char *p
= hostname
, *last_slash
= 0;
728 if(!strcmp(hostname
, "localhost"))
731 if('.' == *p
|| ':' == *p
|| '/' == *p
)
738 if(*p
== '.' || *p
== ':')
751 if(last_slash
&& IsDigit(last_slash
[1]))
758 * valid_username - check username for validity
760 * Inputs - pointer to user
761 * Output - YES if valid, NO if not
762 * Side effects - NONE
764 * Absolutely always reject any '*' '!' '?' '@' in an user name
765 * reject any odd control characters names.
766 * Allow '.' in username to allow for "first.last"
770 valid_username(const char *username
)
773 const char *p
= username
;
783 /* reject usernames that don't start with an alphanum
784 * i.e. reject jokers who have '-@somehost' or '.@somehost'
785 * or "-hi-@somehost", "h-----@somehost" would still be accepted.
792 if((*p
== '.') && ConfigFileEntry
.dots_in_ident
)
795 if(dots
> ConfigFileEntry
.dots_in_ident
)
797 if(!IsUserChar(p
[1]))
800 else if(!IsUserChar(*p
))
806 /* report_and_set_user_flags
808 * Inputs - pointer to source_p
809 * - pointer to aconf for this user
812 * Report to user any special flags they are getting, and set them.
816 report_and_set_user_flags(struct Client
*source_p
, struct ConfItem
*aconf
)
818 /* If this user is being spoofed, tell them so */
819 if(IsConfDoSpoofIp(aconf
))
821 sendto_one_notice(source_p
, ":*** Spoofing your IP");
824 /* If this user is in the exception class, Set it "E lined" */
825 if(IsConfExemptKline(aconf
))
827 SetExemptKline(source_p
);
828 sendto_one_notice(source_p
, ":*** You are exempt from K/X lines");
831 if(IsConfExemptDNSBL(aconf
))
832 /* kline exempt implies this, don't send both */
833 if(!IsConfExemptKline(aconf
))
834 sendto_one_notice(source_p
, ":*** You are exempt from DNS blacklists");
836 /* If this user is exempt from user limits set it F lined" */
837 if(IsConfExemptLimits(aconf
))
839 sendto_one_notice(source_p
, ":*** You are exempt from user limits");
842 if(IsConfExemptFlood(aconf
))
844 SetExemptFlood(source_p
);
845 sendto_one_notice(source_p
, ":*** You are exempt from flood limits");
848 if(IsConfExemptSpambot(aconf
))
850 SetExemptSpambot(source_p
);
851 sendto_one_notice(source_p
, ":*** You are exempt from spambot checks");
854 if(IsConfExemptJupe(aconf
))
856 SetExemptJupe(source_p
);
857 sendto_one_notice(source_p
, ":*** You are exempt from juped channel warnings");
860 if(IsConfExemptResv(aconf
))
862 SetExemptResv(source_p
);
863 sendto_one_notice(source_p
, ":*** You are exempt from resvs");
866 if(IsConfExemptShide(aconf
))
868 SetExemptShide(source_p
);
869 sendto_one_notice(source_p
, ":*** You are exempt from serverhiding");
874 show_other_user_mode(struct Client
*source_p
, struct Client
*target_p
)
883 for (i
= 0; i
< 128; i
++) /* >= 127 is extended ascii */
884 if (target_p
->umodes
& user_modes
[i
])
888 if (MyConnect(target_p
) && target_p
->snomask
!= 0)
889 sendto_one_notice(source_p
, ":Modes for %s are %s %s",
891 construct_snobuf(target_p
->snomask
));
893 sendto_one_notice(source_p
, ":Modes for %s are %s",
894 target_p
->name
, buf
);
898 * user_mode - set get current users mode
900 * m_umode() added 15/10/91 By Darren Reed.
901 * parv[1] - username to change mode for
902 * parv[2] - modes to change
905 user_mode(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
911 struct Client
*target_p
;
913 int badflag
= NO
; /* Only send one bad flag notice */
914 int showsnomask
= NO
;
915 unsigned int setsnomask
;
917 hook_data_umode_changed hdata
;
923 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
), me
.name
, source_p
->name
, "MODE");
927 if((target_p
= MyClient(source_p
) ? find_named_person(parv
[1]) : find_person(parv
[1])) == NULL
)
929 if(MyConnect(source_p
))
930 sendto_one_numeric(source_p
, ERR_NOSUCHCHANNEL
,
931 form_str(ERR_NOSUCHCHANNEL
), parv
[1]);
935 /* Dont know why these were commented out..
936 * put them back using new sendto() funcs
939 if(IsServer(source_p
))
941 sendto_realops_snomask(SNO_GENERAL
, L_ADMIN
,
942 "*** Mode for User %s from %s", parv
[1], source_p
->name
);
946 if(source_p
!= target_p
)
948 if (MyOper(source_p
) && parc
< 3)
949 show_other_user_mode(source_p
, target_p
);
951 sendto_one(source_p
, form_str(ERR_USERSDONTMATCH
), me
.name
, source_p
->name
);
960 for (i
= 0; i
< 128; i
++) /* >= 127 is extended ascii */
961 if (source_p
->umodes
& user_modes
[i
])
965 sendto_one_numeric(source_p
, RPL_UMODEIS
, form_str(RPL_UMODEIS
), buf
);
967 if (source_p
->snomask
!= 0)
968 sendto_one_numeric(source_p
, RPL_SNOMASK
, form_str(RPL_SNOMASK
),
969 construct_snobuf(source_p
->snomask
));
974 /* find flags already set for user */
975 setflags
= source_p
->umodes
;
976 setsnomask
= source_p
->snomask
;
979 * parse mode change string(s)
981 for (pm
= parv
[2]; *pm
; pm
++)
994 if(IsServer(client_p
) && !IsOper(source_p
))
998 rb_dlinkAddAlloc(source_p
, &oper_list
);
1003 /* Only decrement the oper counts if an oper to begin with
1004 * found by Pat Szuta, Perly , perly@xnet.com
1007 if(!IsOper(source_p
))
1010 ClearOper(source_p
);
1014 if(MyConnect(source_p
))
1016 source_p
->umodes
&= ~ConfigFileEntry
.oper_only_umodes
;
1017 if (!(source_p
->umodes
& UMODE_SERVNOTICE
) && source_p
->snomask
!= 0)
1019 source_p
->snomask
= 0;
1022 source_p
->flags2
&= ~OPER_FLAGS
;
1024 rb_free(source_p
->localClient
->opername
);
1025 source_p
->localClient
->opername
= NULL
;
1027 rb_dlinkFindDestroy(source_p
, &local_oper_list
);
1028 privilegeset_unref(source_p
->localClient
->privset
);
1029 source_p
->localClient
->privset
= NULL
;
1032 rb_dlinkFindDestroy(source_p
, &oper_list
);
1036 /* we may not get these,
1037 * but they shouldnt be in default
1040 /* can only be set on burst */
1050 if (MyConnect(source_p
))
1052 if(!IsOper(source_p
)
1053 && (ConfigFileEntry
.oper_only_umodes
& UMODE_SERVNOTICE
))
1055 if (what
== MODE_ADD
|| source_p
->umodes
& UMODE_SERVNOTICE
)
1060 if(what
== MODE_ADD
)
1063 source_p
->snomask
= parse_snobuf_to_mask(source_p
->snomask
, parv
[3]);
1065 source_p
->snomask
|= SNO_GENERAL
;
1068 source_p
->snomask
= 0;
1069 if (source_p
->snomask
!= 0)
1070 source_p
->umodes
|= UMODE_SERVNOTICE
;
1072 source_p
->umodes
&= ~UMODE_SERVNOTICE
;
1077 if (MyConnect(source_p
) && *pm
== 'Q' && !ConfigChannel
.use_forward
)
1083 if((flag
= user_modes
[(unsigned char) *pm
]))
1085 if(MyConnect(source_p
)
1086 && ((!IsOper(source_p
)
1087 && (ConfigFileEntry
.oper_only_umodes
& flag
))
1088 || (orphaned_umodes
& flag
)))
1090 if (what
== MODE_ADD
|| source_p
->umodes
& flag
)
1095 if(what
== MODE_ADD
)
1096 source_p
->umodes
|= flag
;
1098 source_p
->umodes
&= ~flag
;
1103 if(MyConnect(source_p
))
1110 sendto_one(source_p
, form_str(ERR_UMODEUNKNOWNFLAG
), me
.name
, source_p
->name
);
1112 if(MyClient(source_p
) && (source_p
->snomask
& SNO_NCHANGE
) && !IsOperN(source_p
))
1114 sendto_one_notice(source_p
, ":*** You need oper and nick_changes flag for +s +n");
1115 source_p
->snomask
&= ~SNO_NCHANGE
; /* only tcm's really need this */
1118 if(MyClient(source_p
) && (source_p
->umodes
& UMODE_OPERWALL
) && !IsOperOperwall(source_p
))
1120 sendto_one_notice(source_p
, ":*** You need oper and operwall flag for +z");
1121 source_p
->umodes
&= ~UMODE_OPERWALL
;
1124 if(MyConnect(source_p
) && (source_p
->umodes
& UMODE_ADMIN
) &&
1125 (!IsOperAdmin(source_p
) || IsOperHiddenAdmin(source_p
)))
1127 sendto_one_notice(source_p
, ":*** You need oper and admin flag for +a");
1128 source_p
->umodes
&= ~UMODE_ADMIN
;
1131 /* let modules providing usermodes know that we've changed our usermode --nenolod */
1132 hdata
.client
= source_p
;
1133 hdata
.oldumodes
= setflags
;
1134 hdata
.oldsnomask
= setsnomask
;
1135 call_hook(h_umode_changed
, &hdata
);
1137 if(!(setflags
& UMODE_INVISIBLE
) && IsInvisible(source_p
))
1139 if((setflags
& UMODE_INVISIBLE
) && !IsInvisible(source_p
))
1142 * compare new flags with old flags and send string which
1143 * will cause servers to update correctly.
1145 send_umode_out(client_p
, source_p
, setflags
);
1146 if (showsnomask
&& MyConnect(source_p
))
1147 sendto_one_numeric(source_p
, RPL_SNOMASK
, form_str(RPL_SNOMASK
),
1148 construct_snobuf(source_p
->snomask
));
1154 * send the MODE string for user (user) to connection client_p
1158 send_umode(struct Client
*client_p
, struct Client
*source_p
, int old
, int sendmask
, char *umode_buf
)
1166 * build a string in umode_buf to represent the change in the user's
1167 * mode between the new (source_p->flag) and 'old'.
1172 for (i
= 0; i
< 128; i
++)
1174 flag
= user_modes
[i
];
1176 if((flag
& old
) && !(source_p
->umodes
& flag
))
1178 if(what
== MODE_DEL
)
1187 else if(!(flag
& old
) && (source_p
->umodes
& flag
))
1189 if(what
== MODE_ADD
)
1201 if(*umode_buf
&& client_p
)
1202 sendto_one(client_p
, ":%s MODE %s :%s", source_p
->name
, source_p
->name
, umode_buf
);
1213 send_umode_out(struct Client
*client_p
, struct Client
*source_p
, int old
)
1215 struct Client
*target_p
;
1219 send_umode(NULL
, source_p
, old
, 0, buf
);
1221 RB_DLINK_FOREACH(ptr
, serv_list
.head
)
1223 target_p
= ptr
->data
;
1225 if((target_p
!= client_p
) && (target_p
!= source_p
) && (*buf
))
1227 sendto_one(target_p
, ":%s MODE %s :%s",
1228 get_id(source_p
, target_p
),
1229 get_id(source_p
, target_p
), buf
);
1233 if(client_p
&& MyClient(client_p
))
1234 send_umode(client_p
, source_p
, old
, 0, buf
);
1240 * inputs - client pointer to client to welcome
1245 user_welcome(struct Client
*source_p
)
1247 sendto_one_numeric(source_p
, RPL_WELCOME
, form_str(RPL_WELCOME
), ServerInfo
.network_name
, source_p
->name
);
1248 sendto_one_numeric(source_p
, RPL_YOURHOST
, form_str(RPL_YOURHOST
),
1249 get_listener_name(source_p
->localClient
->listener
), ircd_version
);
1250 sendto_one_numeric(source_p
, RPL_CREATED
, form_str(RPL_CREATED
), creation
);
1251 sendto_one_numeric(source_p
, RPL_MYINFO
, form_str(RPL_MYINFO
), me
.name
, ircd_version
, umodebuf
, cflagsmyinfo
);
1253 show_isupport(source_p
);
1255 show_lusers(source_p
);
1257 if(ConfigFileEntry
.short_motd
)
1259 sendto_one_notice(source_p
, ":*** Notice -- motd was last changed at %s", user_motd_changed
);
1260 sendto_one_notice(source_p
, ":*** Notice -- Please read the motd if you haven't read it");
1262 sendto_one(source_p
, form_str(RPL_MOTDSTART
),
1263 me
.name
, source_p
->name
, me
.name
);
1265 sendto_one(source_p
, form_str(RPL_MOTD
),
1266 me
.name
, source_p
->name
, "*** This is the short motd ***");
1268 sendto_one(source_p
, form_str(RPL_ENDOFMOTD
), me
.name
, source_p
->name
);
1271 send_user_motd(source_p
);
1276 * inputs - pointer to given client to oper
1277 * - pointer to ConfItem to use
1279 * side effects - opers up source_p using aconf for reference
1282 oper_up(struct Client
*source_p
, struct oper_conf
*oper_p
)
1284 unsigned int old
= source_p
->umodes
, oldsnomask
= source_p
->snomask
;
1285 hook_data_umode_changed hdata
;
1290 source_p
->umodes
|= oper_p
->umodes
;
1291 else if(ConfigFileEntry
.oper_umodes
)
1292 source_p
->umodes
|= ConfigFileEntry
.oper_umodes
;
1294 source_p
->umodes
|= DEFAULT_OPER_UMODES
;
1296 if (oper_p
->snomask
)
1298 source_p
->snomask
|= oper_p
->snomask
;
1299 source_p
->umodes
|= UMODE_SERVNOTICE
;
1301 else if (source_p
->umodes
& UMODE_SERVNOTICE
)
1303 /* Only apply these if +s is already set -- jilles */
1304 if (ConfigFileEntry
.oper_snomask
)
1305 source_p
->snomask
|= ConfigFileEntry
.oper_snomask
;
1307 source_p
->snomask
|= DEFAULT_OPER_SNOMASK
;
1312 SetExemptKline(source_p
);
1314 source_p
->flags2
|= oper_p
->flags
;
1315 source_p
->localClient
->opername
= rb_strdup(oper_p
->name
);
1316 source_p
->localClient
->privset
= privilegeset_ref(oper_p
->privset
);
1318 rb_dlinkAddAlloc(source_p
, &local_oper_list
);
1319 rb_dlinkAddAlloc(source_p
, &oper_list
);
1321 if(IsOperAdmin(source_p
) && !IsOperHiddenAdmin(source_p
))
1322 source_p
->umodes
|= UMODE_ADMIN
;
1323 if(!IsOperN(source_p
))
1324 source_p
->snomask
&= ~SNO_NCHANGE
;
1325 if(!IsOperOperwall(source_p
))
1326 source_p
->umodes
&= ~UMODE_OPERWALL
;
1327 hdata
.client
= source_p
;
1328 hdata
.oldumodes
= old
;
1329 hdata
.oldsnomask
= oldsnomask
;
1330 call_hook(h_umode_changed
, &hdata
);
1332 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1333 "%s (%s!%s@%s) is now an operator", oper_p
->name
, source_p
->name
,
1334 source_p
->username
, source_p
->host
);
1335 if(!(old
& UMODE_INVISIBLE
) && IsInvisible(source_p
))
1337 if((old
& UMODE_INVISIBLE
) && !IsInvisible(source_p
))
1339 send_umode_out(source_p
, source_p
, old
);
1340 sendto_one_numeric(source_p
, RPL_SNOMASK
, form_str(RPL_SNOMASK
),
1341 construct_snobuf(source_p
->snomask
));
1342 sendto_one(source_p
, form_str(RPL_YOUREOPER
), me
.name
, source_p
->name
);
1343 sendto_one_notice(source_p
, ":*** Oper privilege set is %s", oper_p
->privset
->name
);
1344 sendto_one_notice(source_p
, ":*** Oper privs are %s", oper_p
->privset
->privs
);
1345 send_oper_motd(source_p
);
1354 * outputs - an available umode bitmask or
1355 * 0 if no umodes are available
1356 * side effects - NONE
1359 find_umode_slot(void)
1361 unsigned int all_umodes
= 0, my_umode
= 0, i
;
1363 for (i
= 0; i
< 128; i
++)
1364 all_umodes
|= user_modes
[i
];
1366 for (my_umode
= 1; my_umode
&& (all_umodes
& my_umode
);
1373 construct_umodebuf(void)
1376 char *ptr
= umodebuf
;
1377 static int prev_user_modes
[128];
1381 for (i
= 0; i
< 128; i
++)
1383 if (prev_user_modes
[i
] != 0 && prev_user_modes
[i
] != user_modes
[i
])
1385 if (user_modes
[i
] == 0)
1387 orphaned_umodes
|= prev_user_modes
[i
];
1388 sendto_realops_snomask(SNO_DEBUG
, L_ALL
, "Umode +%c is now orphaned", i
);
1392 orphaned_umodes
&= ~prev_user_modes
[i
];
1393 sendto_realops_snomask(SNO_DEBUG
, L_ALL
, "Orphaned umode +%c is picked up by module", i
);
1395 user_modes
[i
] = prev_user_modes
[i
];
1398 prev_user_modes
[i
] = user_modes
[i
];
1407 change_nick_user_host(struct Client
*target_p
, const char *nick
, const char *user
,
1408 const char *host
, int newts
, const char *format
, ...)
1411 struct Channel
*chptr
;
1412 struct membership
*mscptr
;
1413 int changed
= irccmp(target_p
->name
, nick
);
1414 int changed_case
= strcmp(target_p
->name
, nick
);
1415 int do_qjm
= irccmp(target_p
->username
, user
) || irccmp(target_p
->host
, host
);
1416 char mode
[10], modeval
[NICKLEN
* 2 + 2], reason
[256], *mptr
;
1423 target_p
->tsinfo
= newts
;
1424 monitor_signoff(target_p
);
1426 invalidate_bancache_user(target_p
);
1430 va_start(ap
, format
);
1431 vsnprintf(reason
, 255, format
, ap
);
1434 sendto_common_channels_local_butone(target_p
, NOCAPS
, ":%s!%s@%s QUIT :%s",
1435 target_p
->name
, target_p
->username
, target_p
->host
,
1438 RB_DLINK_FOREACH(ptr
, target_p
->user
->channel
.head
)
1441 chptr
= mscptr
->chptr
;
1444 if(is_chanop(mscptr
))
1447 strcat(modeval
, nick
);
1448 strcat(modeval
, " ");
1451 if(is_voiced(mscptr
))
1454 strcat(modeval
, nick
);
1459 sendto_channel_local_with_capability_butone(target_p
, ALL_MEMBERS
, NOCAPS
, CLICAP_EXTENDED_JOIN
, chptr
,
1460 ":%s!%s@%s JOIN %s", nick
, user
, host
, chptr
->chname
);
1461 sendto_channel_local_with_capability_butone(target_p
, ALL_MEMBERS
, CLICAP_EXTENDED_JOIN
, NOCAPS
, chptr
,
1462 ":%s!%s@%s JOIN %s %s :%s", nick
, user
, host
, chptr
->chname
,
1463 EmptyString(target_p
->user
->suser
) ? "*" : target_p
->user
->suser
,
1467 sendto_channel_local_butone(target_p
, ALL_MEMBERS
, chptr
,
1468 ":%s MODE %s +%s %s",
1469 target_p
->servptr
->name
,
1470 chptr
->chname
, mode
, modeval
);
1475 /* Resend away message to away-notify enabled clients. */
1476 if (target_p
->user
->away
)
1477 sendto_common_channels_local_butone(target_p
, CLICAP_AWAY_NOTIFY
, ":%s!%s@%s AWAY :%s",
1478 target_p
->name
, target_p
->username
, target_p
->host
,
1479 target_p
->user
->away
);
1481 if(MyClient(target_p
) && changed_case
)
1482 sendto_one(target_p
, ":%s!%s@%s NICK %s",
1483 target_p
->name
, target_p
->username
, target_p
->host
, nick
);
1485 /* TODO: send some snotes to SNO_NCHANGE/SNO_CCONN/SNO_CCONNEXT? */
1487 else if(changed_case
)
1489 sendto_common_channels_local(target_p
, NOCAPS
, ":%s!%s@%s NICK :%s",
1490 target_p
->name
, target_p
->username
,
1491 target_p
->host
, nick
);
1493 if(MyConnect(target_p
))
1494 sendto_realops_snomask(SNO_NCHANGE
, L_ALL
,
1495 "Nick change: From %s to %s [%s@%s]",
1496 target_p
->name
, nick
,
1497 target_p
->username
, target_p
->host
);
1500 rb_strlcpy(target_p
->username
, user
, sizeof target_p
->username
);
1501 rb_strlcpy(target_p
->host
, host
, sizeof target_p
->host
);
1504 add_history(target_p
, 1);
1506 del_from_client_hash(target_p
->name
, target_p
);
1507 rb_strlcpy(target_p
->name
, nick
, NICKLEN
);
1508 add_to_client_hash(target_p
->name
, target_p
);
1512 monitor_signon(target_p
);
1513 del_all_accepts(target_p
);