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 rb_dlink_list varlist
= { NULL
, NULL
, 0 };
439 substitution_append_var(&varlist
, "nick", source_p
->name
);
440 substitution_append_var(&varlist
, "ip", source_p
->sockhost
);
441 substitution_append_var(&varlist
, "host", source_p
->host
);
442 substitution_append_var(&varlist
, "dnsbl-host", source_p
->preClient
->dnsbl_listed
->host
);
443 substitution_append_var(&varlist
, "network-name", ServerInfo
.network_name
);
445 ServerStats
.is_ref
++;
447 sendto_one(source_p
, form_str(ERR_YOUREBANNEDCREEP
),
448 me
.name
, source_p
->name
,
449 substitution_parse(source_p
->preClient
->dnsbl_listed
->reject_reason
, &varlist
));
451 substitution_free(&varlist
);
453 sendto_one_notice(source_p
, ":*** Your IP address %s is listed in %s",
454 source_p
->sockhost
, source_p
->preClient
->dnsbl_listed
->host
);
455 source_p
->preClient
->dnsbl_listed
->hits
++;
456 add_reject(source_p
, NULL
, NULL
);
457 exit_client(client_p
, source_p
, &me
, "*** Banned (DNS blacklist)");
458 return CLIENT_EXITED
;
462 /* valid user name check */
464 if(!valid_username(source_p
->username
))
466 sendto_realops_snomask(SNO_REJ
, L_ALL
,
467 "Invalid username: %s (%s@%s)",
468 source_p
->name
, source_p
->username
, source_p
->host
);
469 ServerStats
.is_ref
++;
470 sendto_one_notice(source_p
, ":*** Your username is invalid. Please make sure that your username contains "
471 "only alphanumeric characters.");
472 rb_sprintf(tmpstr2
, "Invalid username [%s]", source_p
->username
);
473 exit_client(client_p
, source_p
, &me
, tmpstr2
);
474 return (CLIENT_EXITED
);
477 /* end of valid user name check */
479 /* Store original hostname -- jilles */
480 rb_strlcpy(source_p
->orighost
, source_p
->host
, HOSTLEN
+ 1);
482 /* Spoof user@host */
483 if(*source_p
->preClient
->spoofuser
)
484 rb_strlcpy(source_p
->username
, source_p
->preClient
->spoofuser
, USERLEN
+ 1);
485 if(*source_p
->preClient
->spoofhost
)
487 rb_strlcpy(source_p
->host
, source_p
->preClient
->spoofhost
, HOSTLEN
+ 1);
488 if (irccmp(source_p
->host
, source_p
->orighost
))
489 SetDynSpoof(source_p
);
492 source_p
->umodes
|= ConfigFileEntry
.default_umodes
& ~ConfigFileEntry
.oper_only_umodes
& ~orphaned_umodes
;
494 call_hook(h_new_local_user
, source_p
);
496 /* If they have died in send_* or were thrown out by the
497 * new_local_user hook don't do anything. */
498 if(IsAnyDead(source_p
))
499 return CLIENT_EXITED
;
501 /* To avoid inconsistencies, do not abort the registration
502 * starting from this point -- jilles
504 rb_inet_ntop_sock((struct sockaddr
*)&source_p
->localClient
->ip
, ipaddr
, sizeof(ipaddr
));
506 sendto_realops_snomask(SNO_CCONN
, L_ALL
,
507 "Client connecting: %s (%s@%s) [%s] {%s} [%s]",
508 source_p
->name
, source_p
->username
, source_p
->orighost
,
509 show_ip(NULL
, source_p
) ? ipaddr
: "255.255.255.255",
510 get_client_class(source_p
), source_p
->info
);
512 sendto_realops_snomask(SNO_CCONNEXT
, L_ALL
,
513 "CLICONN %s %s %s %s %s %s 0 %s",
514 source_p
->name
, source_p
->username
, source_p
->orighost
,
515 show_ip(NULL
, source_p
) ? ipaddr
: "255.255.255.255",
516 get_client_class(source_p
),
517 /* mirc can sometimes send ips here */
518 show_ip(NULL
, source_p
) ? source_p
->localClient
->fullcaps
: "<hidden> <hidden>",
521 add_to_hostname_hash(source_p
->orighost
, source_p
);
523 /* Allocate a UID if it was not previously allocated.
524 * If this already occured, it was probably during SASL auth...
528 strcpy(source_p
->id
, generate_uid());
529 add_to_id_hash(source_p
->id
, source_p
);
533 source_p
->umodes
|= UMODE_SSLCLIENT
;
535 if (source_p
->umodes
& UMODE_INVISIBLE
)
538 s_assert(!IsClient(source_p
));
539 rb_dlinkMoveNode(&source_p
->localClient
->tnode
, &unknown_list
, &lclient_list
);
542 source_p
->servptr
= &me
;
543 rb_dlinkAdd(source_p
, &source_p
->lnode
, &source_p
->servptr
->serv
->users
);
545 /* Increment our total user count here */
546 if(++Count
.total
> Count
.max_tot
)
547 Count
.max_tot
= Count
.total
;
549 Count
.totalrestartcount
++;
551 s_assert(source_p
->localClient
!= NULL
);
553 if(rb_dlink_list_length(&lclient_list
) > (unsigned long)Count
.max_loc
)
555 Count
.max_loc
= rb_dlink_list_length(&lclient_list
);
556 if(!(Count
.max_loc
% 10))
557 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
558 "New Max Local Clients: %d", Count
.max_loc
);
561 /* they get a reduced limit */
562 if(find_tgchange(source_p
->sockhost
))
563 source_p
->localClient
->targets_free
= TGCHANGE_INITIAL_LOW
;
565 source_p
->localClient
->targets_free
= TGCHANGE_INITIAL
;
567 monitor_signon(source_p
);
568 user_welcome(source_p
);
570 free_pre_client(source_p
);
572 return (introduce_client(client_p
, source_p
, user
, source_p
->name
, 1));
580 * side effects - This common function introduces a client to the rest
581 * of the net, either from a local client connect or
582 * from a remote connect.
585 introduce_client(struct Client
*client_p
, struct Client
*source_p
, struct User
*user
, const char *nick
, int use_euid
)
587 static char ubuf
[12];
588 struct Client
*identifyservice_p
;
590 hook_data_umode_changed hdata
;
591 hook_data_client hdata2
;
592 char sockhost
[HOSTLEN
];
594 if(MyClient(source_p
))
595 send_umode(source_p
, source_p
, 0, 0, ubuf
);
597 send_umode(NULL
, source_p
, 0, 0, ubuf
);
605 s_assert(has_id(source_p
));
607 if(source_p
->sockhost
[0] == ':')
611 rb_strlcat(sockhost
, source_p
->sockhost
, sizeof(sockhost
));
613 strcpy(sockhost
, source_p
->sockhost
);
616 sendto_server(client_p
, NULL
, CAP_EUID
| CAP_TS6
, NOCAPS
,
617 ":%s EUID %s %d %ld %s %s %s %s %s %s %s :%s",
618 source_p
->servptr
->id
, nick
,
619 source_p
->hopcount
+ 1,
620 (long) source_p
->tsinfo
, ubuf
,
621 source_p
->username
, source_p
->host
,
622 IsIPSpoof(source_p
) ? "0" : sockhost
,
624 IsDynSpoof(source_p
) ? source_p
->orighost
: "*",
625 EmptyString(source_p
->user
->suser
) ? "*" : source_p
->user
->suser
,
628 sendto_server(client_p
, NULL
, CAP_TS6
, use_euid
? CAP_EUID
: NOCAPS
,
629 ":%s UID %s %d %ld %s %s %s %s %s :%s",
630 source_p
->servptr
->id
, nick
,
631 source_p
->hopcount
+ 1,
632 (long) source_p
->tsinfo
, ubuf
,
633 source_p
->username
, source_p
->host
,
634 IsIPSpoof(source_p
) ? "0" : sockhost
,
635 source_p
->id
, source_p
->info
);
637 if(!EmptyString(source_p
->certfp
))
638 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
639 ":%s ENCAP * CERTFP :%s",
640 use_id(source_p
), source_p
->certfp
);
642 if (IsDynSpoof(source_p
))
644 sendto_server(client_p
, NULL
, CAP_TS6
, use_euid
? CAP_EUID
: NOCAPS
, ":%s ENCAP * REALHOST %s",
645 use_id(source_p
), source_p
->orighost
);
648 if (!EmptyString(source_p
->user
->suser
))
650 sendto_server(client_p
, NULL
, CAP_TS6
, use_euid
? CAP_EUID
: NOCAPS
, ":%s ENCAP * LOGIN %s",
651 use_id(source_p
), source_p
->user
->suser
);
654 if(MyConnect(source_p
) && source_p
->localClient
->passwd
)
656 if (!EmptyString(ConfigFileEntry
.identifyservice
) &&
657 !EmptyString(ConfigFileEntry
.identifycommand
))
659 /* use user@server */
660 p
= strchr(ConfigFileEntry
.identifyservice
, '@');
662 identifyservice_p
= find_named_client(p
+ 1);
664 identifyservice_p
= NULL
;
665 if (identifyservice_p
!= NULL
)
667 if (!EmptyString(source_p
->localClient
->auth_user
))
668 sendto_one(identifyservice_p
, ":%s PRIVMSG %s :%s %s %s",
669 get_id(source_p
, identifyservice_p
),
670 ConfigFileEntry
.identifyservice
,
671 ConfigFileEntry
.identifycommand
,
672 source_p
->localClient
->auth_user
,
673 source_p
->localClient
->passwd
);
675 sendto_one(identifyservice_p
, ":%s PRIVMSG %s :%s %s",
676 get_id(source_p
, identifyservice_p
),
677 ConfigFileEntry
.identifyservice
,
678 ConfigFileEntry
.identifycommand
,
679 source_p
->localClient
->passwd
);
682 memset(source_p
->localClient
->passwd
, 0, strlen(source_p
->localClient
->passwd
));
683 rb_free(source_p
->localClient
->passwd
);
684 source_p
->localClient
->passwd
= NULL
;
687 /* let modules providing usermodes know that we've got a new user,
688 * why is this here? -- well, some modules need to be able to send out new
689 * information about a client, so this was the best place to do it
692 hdata
.client
= source_p
;
694 hdata
.oldsnomask
= 0;
695 call_hook(h_umode_changed
, &hdata
);
697 /* On the other hand, some modules need to know when a client is
698 * being introduced, period.
701 hdata2
.client
= client_p
;
702 hdata2
.target
= source_p
;
703 call_hook(h_introduce_client
, &hdata2
);
709 * valid_hostname - check hostname for validity
711 * Inputs - pointer to user
712 * Output - YES if valid, NO if not
713 * Side effects - NONE
715 * NOTE: this doesn't allow a hostname to begin with a dot and
716 * will not allow more dots than chars.
719 valid_hostname(const char *hostname
)
721 const char *p
= hostname
, *last_slash
= 0;
729 if('.' == *p
|| ':' == *p
|| '/' == *p
)
736 if(*p
== '.' || *p
== ':')
749 if(last_slash
&& IsDigit(last_slash
[1]))
756 * valid_username - check username for validity
758 * Inputs - pointer to user
759 * Output - YES if valid, NO if not
760 * Side effects - NONE
762 * Absolutely always reject any '*' '!' '?' '@' in an user name
763 * reject any odd control characters names.
764 * Allow '.' in username to allow for "first.last"
768 valid_username(const char *username
)
771 const char *p
= username
;
781 /* reject usernames that don't start with an alphanum
782 * i.e. reject jokers who have '-@somehost' or '.@somehost'
783 * or "-hi-@somehost", "h-----@somehost" would still be accepted.
790 if((*p
== '.') && ConfigFileEntry
.dots_in_ident
)
793 if(dots
> ConfigFileEntry
.dots_in_ident
)
795 if(!IsUserChar(p
[1]))
798 else if(!IsUserChar(*p
))
804 /* report_and_set_user_flags
806 * Inputs - pointer to source_p
807 * - pointer to aconf for this user
810 * Report to user any special flags they are getting, and set them.
814 report_and_set_user_flags(struct Client
*source_p
, struct ConfItem
*aconf
)
816 /* If this user is being spoofed, tell them so */
817 if(IsConfDoSpoofIp(aconf
))
819 sendto_one_notice(source_p
, ":*** Spoofing your IP");
822 /* If this user is in the exception class, Set it "E lined" */
823 if(IsConfExemptKline(aconf
))
825 SetExemptKline(source_p
);
826 sendto_one_notice(source_p
, ":*** You are exempt from K/X lines");
829 if(IsConfExemptDNSBL(aconf
))
830 /* kline exempt implies this, don't send both */
831 if(!IsConfExemptKline(aconf
))
832 sendto_one_notice(source_p
, ":*** You are exempt from DNS blacklists");
834 /* If this user is exempt from user limits set it F lined" */
835 if(IsConfExemptLimits(aconf
))
837 sendto_one_notice(source_p
, ":*** You are exempt from user limits");
840 if(IsConfExemptFlood(aconf
))
842 SetExemptFlood(source_p
);
843 sendto_one_notice(source_p
, ":*** You are exempt from flood limits");
846 if(IsConfExemptSpambot(aconf
))
848 SetExemptSpambot(source_p
);
849 sendto_one_notice(source_p
, ":*** You are exempt from spambot checks");
852 if(IsConfExemptJupe(aconf
))
854 SetExemptJupe(source_p
);
855 sendto_one_notice(source_p
, ":*** You are exempt from juped channel warnings");
858 if(IsConfExemptResv(aconf
))
860 SetExemptResv(source_p
);
861 sendto_one_notice(source_p
, ":*** You are exempt from resvs");
864 if(IsConfExemptShide(aconf
))
866 SetExemptShide(source_p
);
867 sendto_one_notice(source_p
, ":*** You are exempt from serverhiding");
872 show_other_user_mode(struct Client
*source_p
, struct Client
*target_p
)
881 for (i
= 0; i
< 128; i
++) /* >= 127 is extended ascii */
882 if (target_p
->umodes
& user_modes
[i
])
886 if (MyConnect(target_p
) && target_p
->snomask
!= 0)
887 sendto_one_notice(source_p
, ":Modes for %s are %s %s",
889 construct_snobuf(target_p
->snomask
));
891 sendto_one_notice(source_p
, ":Modes for %s are %s",
892 target_p
->name
, buf
);
896 * user_mode - set get current users mode
898 * m_umode() added 15/10/91 By Darren Reed.
899 * parv[1] - username to change mode for
900 * parv[2] - modes to change
903 user_mode(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
909 struct Client
*target_p
;
911 int badflag
= NO
; /* Only send one bad flag notice */
912 int showsnomask
= NO
;
913 unsigned int setsnomask
;
915 hook_data_umode_changed hdata
;
921 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
), me
.name
, source_p
->name
, "MODE");
925 if((target_p
= MyClient(source_p
) ? find_named_person(parv
[1]) : find_person(parv
[1])) == NULL
)
927 if(MyConnect(source_p
))
928 sendto_one_numeric(source_p
, ERR_NOSUCHCHANNEL
,
929 form_str(ERR_NOSUCHCHANNEL
), parv
[1]);
933 /* Dont know why these were commented out..
934 * put them back using new sendto() funcs
937 if(IsServer(source_p
))
939 sendto_realops_snomask(SNO_GENERAL
, L_ADMIN
,
940 "*** Mode for User %s from %s", parv
[1], source_p
->name
);
944 if(source_p
!= target_p
)
946 if (MyOper(source_p
) && parc
< 3)
947 show_other_user_mode(source_p
, target_p
);
949 sendto_one(source_p
, form_str(ERR_USERSDONTMATCH
), me
.name
, source_p
->name
);
958 for (i
= 0; i
< 128; i
++) /* >= 127 is extended ascii */
959 if (source_p
->umodes
& user_modes
[i
])
963 sendto_one_numeric(source_p
, RPL_UMODEIS
, form_str(RPL_UMODEIS
), buf
);
965 if (source_p
->snomask
!= 0)
966 sendto_one_numeric(source_p
, RPL_SNOMASK
, form_str(RPL_SNOMASK
),
967 construct_snobuf(source_p
->snomask
));
972 /* find flags already set for user */
973 setflags
= source_p
->umodes
;
974 setsnomask
= source_p
->snomask
;
977 * parse mode change string(s)
979 for (pm
= parv
[2]; *pm
; pm
++)
992 if(IsServer(client_p
) && !IsOper(source_p
))
996 rb_dlinkAddAlloc(source_p
, &oper_list
);
1001 /* Only decrement the oper counts if an oper to begin with
1002 * found by Pat Szuta, Perly , perly@xnet.com
1005 if(!IsOper(source_p
))
1008 ClearOper(source_p
);
1012 if(MyConnect(source_p
))
1014 source_p
->umodes
&= ~ConfigFileEntry
.oper_only_umodes
;
1015 if (!(source_p
->umodes
& UMODE_SERVNOTICE
) && source_p
->snomask
!= 0)
1017 source_p
->snomask
= 0;
1020 source_p
->flags2
&= ~OPER_FLAGS
;
1022 rb_free(source_p
->localClient
->opername
);
1023 source_p
->localClient
->opername
= NULL
;
1025 rb_dlinkFindDestroy(source_p
, &local_oper_list
);
1026 privilegeset_unref(source_p
->localClient
->privset
);
1027 source_p
->localClient
->privset
= NULL
;
1030 rb_dlinkFindDestroy(source_p
, &oper_list
);
1034 /* we may not get these,
1035 * but they shouldnt be in default
1038 /* can only be set on burst */
1048 if (MyConnect(source_p
))
1050 if(!IsOper(source_p
)
1051 && (ConfigFileEntry
.oper_only_umodes
& UMODE_SERVNOTICE
))
1053 if (what
== MODE_ADD
|| source_p
->umodes
& UMODE_SERVNOTICE
)
1058 if(what
== MODE_ADD
)
1061 source_p
->snomask
= parse_snobuf_to_mask(source_p
->snomask
, parv
[3]);
1063 source_p
->snomask
|= SNO_GENERAL
;
1066 source_p
->snomask
= 0;
1067 if (source_p
->snomask
!= 0)
1068 source_p
->umodes
|= UMODE_SERVNOTICE
;
1070 source_p
->umodes
&= ~UMODE_SERVNOTICE
;
1075 if (MyConnect(source_p
) && *pm
== 'Q' && !ConfigChannel
.use_forward
)
1081 if((flag
= user_modes
[(unsigned char) *pm
]))
1083 if(MyConnect(source_p
)
1084 && ((!IsOper(source_p
)
1085 && (ConfigFileEntry
.oper_only_umodes
& flag
))
1086 || (orphaned_umodes
& flag
)))
1088 if (what
== MODE_ADD
|| source_p
->umodes
& flag
)
1093 if(what
== MODE_ADD
)
1094 source_p
->umodes
|= flag
;
1096 source_p
->umodes
&= ~flag
;
1101 if(MyConnect(source_p
))
1108 sendto_one(source_p
, form_str(ERR_UMODEUNKNOWNFLAG
), me
.name
, source_p
->name
);
1110 if(MyClient(source_p
) && (source_p
->snomask
& SNO_NCHANGE
) && !IsOperN(source_p
))
1112 sendto_one_notice(source_p
, ":*** You need oper and nick_changes flag for +s +n");
1113 source_p
->snomask
&= ~SNO_NCHANGE
; /* only tcm's really need this */
1116 if(MyClient(source_p
) && (source_p
->umodes
& UMODE_OPERWALL
) && !IsOperOperwall(source_p
))
1118 sendto_one_notice(source_p
, ":*** You need oper and operwall flag for +z");
1119 source_p
->umodes
&= ~UMODE_OPERWALL
;
1122 if(MyConnect(source_p
) && (source_p
->umodes
& UMODE_ADMIN
) &&
1123 (!IsOperAdmin(source_p
) || IsOperHiddenAdmin(source_p
)))
1125 sendto_one_notice(source_p
, ":*** You need oper and admin flag for +a");
1126 source_p
->umodes
&= ~UMODE_ADMIN
;
1129 /* let modules providing usermodes know that we've changed our usermode --nenolod */
1130 hdata
.client
= source_p
;
1131 hdata
.oldumodes
= setflags
;
1132 hdata
.oldsnomask
= setsnomask
;
1133 call_hook(h_umode_changed
, &hdata
);
1135 if(!(setflags
& UMODE_INVISIBLE
) && IsInvisible(source_p
))
1137 if((setflags
& UMODE_INVISIBLE
) && !IsInvisible(source_p
))
1140 * compare new flags with old flags and send string which
1141 * will cause servers to update correctly.
1143 send_umode_out(client_p
, source_p
, setflags
);
1144 if (showsnomask
&& MyConnect(source_p
))
1145 sendto_one_numeric(source_p
, RPL_SNOMASK
, form_str(RPL_SNOMASK
),
1146 construct_snobuf(source_p
->snomask
));
1152 * send the MODE string for user (user) to connection client_p
1156 send_umode(struct Client
*client_p
, struct Client
*source_p
, int old
, int sendmask
, char *umode_buf
)
1164 * build a string in umode_buf to represent the change in the user's
1165 * mode between the new (source_p->flag) and 'old'.
1170 for (i
= 0; i
< 128; i
++)
1172 flag
= user_modes
[i
];
1174 if((flag
& old
) && !(source_p
->umodes
& flag
))
1176 if(what
== MODE_DEL
)
1185 else if(!(flag
& old
) && (source_p
->umodes
& flag
))
1187 if(what
== MODE_ADD
)
1199 if(*umode_buf
&& client_p
)
1200 sendto_one(client_p
, ":%s MODE %s :%s", source_p
->name
, source_p
->name
, umode_buf
);
1211 send_umode_out(struct Client
*client_p
, struct Client
*source_p
, int old
)
1213 struct Client
*target_p
;
1217 send_umode(NULL
, source_p
, old
, 0, buf
);
1219 RB_DLINK_FOREACH(ptr
, serv_list
.head
)
1221 target_p
= ptr
->data
;
1223 if((target_p
!= client_p
) && (target_p
!= source_p
) && (*buf
))
1225 sendto_one(target_p
, ":%s MODE %s :%s",
1226 get_id(source_p
, target_p
),
1227 get_id(source_p
, target_p
), buf
);
1231 if(client_p
&& MyClient(client_p
))
1232 send_umode(client_p
, source_p
, old
, 0, buf
);
1238 * inputs - client pointer to client to welcome
1243 user_welcome(struct Client
*source_p
)
1245 sendto_one_numeric(source_p
, RPL_WELCOME
, form_str(RPL_WELCOME
), ServerInfo
.network_name
, source_p
->name
);
1246 sendto_one_numeric(source_p
, RPL_YOURHOST
, form_str(RPL_YOURHOST
),
1247 get_listener_name(source_p
->localClient
->listener
), ircd_version
);
1248 sendto_one_numeric(source_p
, RPL_CREATED
, form_str(RPL_CREATED
), creation
);
1249 sendto_one_numeric(source_p
, RPL_MYINFO
, form_str(RPL_MYINFO
), me
.name
, ircd_version
, umodebuf
, cflagsmyinfo
);
1251 show_isupport(source_p
);
1253 show_lusers(source_p
);
1255 if(ConfigFileEntry
.short_motd
)
1257 sendto_one_notice(source_p
, ":*** Notice -- motd was last changed at %s", user_motd_changed
);
1258 sendto_one_notice(source_p
, ":*** Notice -- Please read the motd if you haven't read it");
1260 sendto_one(source_p
, form_str(RPL_MOTDSTART
),
1261 me
.name
, source_p
->name
, me
.name
);
1263 sendto_one(source_p
, form_str(RPL_MOTD
),
1264 me
.name
, source_p
->name
, "*** This is the short motd ***");
1266 sendto_one(source_p
, form_str(RPL_ENDOFMOTD
), me
.name
, source_p
->name
);
1269 send_user_motd(source_p
);
1274 * inputs - pointer to given client to oper
1275 * - pointer to ConfItem to use
1277 * side effects - opers up source_p using aconf for reference
1280 oper_up(struct Client
*source_p
, struct oper_conf
*oper_p
)
1282 unsigned int old
= source_p
->umodes
, oldsnomask
= source_p
->snomask
;
1283 hook_data_umode_changed hdata
;
1288 source_p
->umodes
|= oper_p
->umodes
;
1289 else if(ConfigFileEntry
.oper_umodes
)
1290 source_p
->umodes
|= ConfigFileEntry
.oper_umodes
;
1292 source_p
->umodes
|= DEFAULT_OPER_UMODES
;
1294 if (oper_p
->snomask
)
1296 source_p
->snomask
|= oper_p
->snomask
;
1297 source_p
->umodes
|= UMODE_SERVNOTICE
;
1299 else if (source_p
->umodes
& UMODE_SERVNOTICE
)
1301 /* Only apply these if +s is already set -- jilles */
1302 if (ConfigFileEntry
.oper_snomask
)
1303 source_p
->snomask
|= ConfigFileEntry
.oper_snomask
;
1305 source_p
->snomask
|= DEFAULT_OPER_SNOMASK
;
1310 SetExemptKline(source_p
);
1312 source_p
->flags2
|= oper_p
->flags
;
1313 source_p
->localClient
->opername
= rb_strdup(oper_p
->name
);
1314 source_p
->localClient
->privset
= privilegeset_ref(oper_p
->privset
);
1316 rb_dlinkAddAlloc(source_p
, &local_oper_list
);
1317 rb_dlinkAddAlloc(source_p
, &oper_list
);
1319 if(IsOperAdmin(source_p
) && !IsOperHiddenAdmin(source_p
))
1320 source_p
->umodes
|= UMODE_ADMIN
;
1321 if(!IsOperN(source_p
))
1322 source_p
->snomask
&= ~SNO_NCHANGE
;
1323 if(!IsOperOperwall(source_p
))
1324 source_p
->umodes
&= ~UMODE_OPERWALL
;
1325 hdata
.client
= source_p
;
1326 hdata
.oldumodes
= old
;
1327 hdata
.oldsnomask
= oldsnomask
;
1328 call_hook(h_umode_changed
, &hdata
);
1330 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1331 "%s (%s!%s@%s) is now an operator", oper_p
->name
, source_p
->name
,
1332 source_p
->username
, source_p
->host
);
1333 if(!(old
& UMODE_INVISIBLE
) && IsInvisible(source_p
))
1335 if((old
& UMODE_INVISIBLE
) && !IsInvisible(source_p
))
1337 send_umode_out(source_p
, source_p
, old
);
1338 sendto_one_numeric(source_p
, RPL_SNOMASK
, form_str(RPL_SNOMASK
),
1339 construct_snobuf(source_p
->snomask
));
1340 sendto_one(source_p
, form_str(RPL_YOUREOPER
), me
.name
, source_p
->name
);
1341 sendto_one_notice(source_p
, ":*** Oper privilege set is %s", oper_p
->privset
->name
);
1342 sendto_one_notice(source_p
, ":*** Oper privs are %s", oper_p
->privset
->privs
);
1343 send_oper_motd(source_p
);
1352 * outputs - an available umode bitmask or
1353 * 0 if no umodes are available
1354 * side effects - NONE
1357 find_umode_slot(void)
1359 unsigned int all_umodes
= 0, my_umode
= 0, i
;
1361 for (i
= 0; i
< 128; i
++)
1362 all_umodes
|= user_modes
[i
];
1364 for (my_umode
= 1; my_umode
&& (all_umodes
& my_umode
);
1371 construct_umodebuf(void)
1374 char *ptr
= umodebuf
;
1375 static int prev_user_modes
[128];
1379 for (i
= 0; i
< 128; i
++)
1381 if (prev_user_modes
[i
] != 0 && prev_user_modes
[i
] != user_modes
[i
])
1383 if (user_modes
[i
] == 0)
1385 orphaned_umodes
|= prev_user_modes
[i
];
1386 sendto_realops_snomask(SNO_DEBUG
, L_ALL
, "Umode +%c is now orphaned", i
);
1390 orphaned_umodes
&= ~prev_user_modes
[i
];
1391 sendto_realops_snomask(SNO_DEBUG
, L_ALL
, "Orphaned umode +%c is picked up by module", i
);
1393 user_modes
[i
] = prev_user_modes
[i
];
1396 prev_user_modes
[i
] = user_modes
[i
];
1405 change_nick_user_host(struct Client
*target_p
, const char *nick
, const char *user
,
1406 const char *host
, int newts
, const char *format
, ...)
1409 struct Channel
*chptr
;
1410 struct membership
*mscptr
;
1411 int changed
= irccmp(target_p
->name
, nick
);
1412 int changed_case
= strcmp(target_p
->name
, nick
);
1413 int do_qjm
= irccmp(target_p
->username
, user
) || irccmp(target_p
->host
, host
);
1414 char mode
[10], modeval
[NICKLEN
* 2 + 2], reason
[256], *mptr
;
1421 target_p
->tsinfo
= newts
;
1422 monitor_signoff(target_p
);
1424 invalidate_bancache_user(target_p
);
1428 va_start(ap
, format
);
1429 vsnprintf(reason
, 255, format
, ap
);
1432 sendto_common_channels_local_butone(target_p
, NOCAPS
, ":%s!%s@%s QUIT :%s",
1433 target_p
->name
, target_p
->username
, target_p
->host
,
1436 RB_DLINK_FOREACH(ptr
, target_p
->user
->channel
.head
)
1439 chptr
= mscptr
->chptr
;
1442 if(is_chanop(mscptr
))
1445 strcat(modeval
, nick
);
1446 strcat(modeval
, " ");
1449 if(is_voiced(mscptr
))
1452 strcat(modeval
, nick
);
1457 sendto_channel_local_with_capability_butone(target_p
, ALL_MEMBERS
, NOCAPS
, CLICAP_EXTENDED_JOIN
, chptr
,
1458 ":%s!%s@%s JOIN %s", nick
, user
, host
, chptr
->chname
);
1459 sendto_channel_local_with_capability_butone(target_p
, ALL_MEMBERS
, CLICAP_EXTENDED_JOIN
, NOCAPS
, chptr
,
1460 ":%s!%s@%s JOIN %s %s :%s", nick
, user
, host
, chptr
->chname
,
1461 EmptyString(target_p
->user
->suser
) ? "*" : target_p
->user
->suser
,
1465 sendto_channel_local_butone(target_p
, ALL_MEMBERS
, chptr
,
1466 ":%s MODE %s +%s %s",
1467 target_p
->servptr
->name
,
1468 chptr
->chname
, mode
, modeval
);
1473 /* Resend away message to away-notify enabled clients. */
1474 if (target_p
->user
->away
)
1475 sendto_common_channels_local_butone(target_p
, CLICAP_AWAY_NOTIFY
, ":%s!%s@%s AWAY :%s",
1476 target_p
->name
, target_p
->username
, target_p
->host
,
1477 target_p
->user
->away
);
1479 if(MyClient(target_p
) && changed_case
)
1480 sendto_one(target_p
, ":%s!%s@%s NICK %s",
1481 target_p
->name
, target_p
->username
, target_p
->host
, nick
);
1483 /* TODO: send some snotes to SNO_NCHANGE/SNO_CCONN/SNO_CCONNEXT? */
1485 else if(changed_case
)
1487 sendto_common_channels_local(target_p
, NOCAPS
, ":%s!%s@%s NICK :%s",
1488 target_p
->name
, target_p
->username
,
1489 target_p
->host
, nick
);
1491 if(MyConnect(target_p
))
1492 sendto_realops_snomask(SNO_NCHANGE
, L_ALL
,
1493 "Nick change: From %s to %s [%s@%s]",
1494 target_p
->name
, nick
,
1495 target_p
->username
, target_p
->host
);
1498 rb_strlcpy(target_p
->username
, user
, sizeof target_p
->username
);
1499 rb_strlcpy(target_p
->host
, host
, sizeof target_p
->host
);
1502 add_history(target_p
, 1);
1504 del_from_client_hash(target_p
->name
, target_p
);
1505 rb_strlcpy(target_p
->name
, nick
, NICKLEN
);
1506 add_to_client_hash(target_p
->name
, target_p
);
1510 monitor_signon(target_p
);
1511 del_all_accepts(target_p
);