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
37 #include "s_newconf.h"
43 #include "supported.h"
51 #include "blacklist.h"
52 #include "substitution.h"
56 static void report_and_set_user_flags(struct Client
*, struct ConfItem
*);
57 void user_welcome(struct Client
*source_p
);
61 static int orphaned_umodes
= 0;
62 int user_modes
[256] = {
63 /* 0x00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0F */
64 /* 0x10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x1F */
65 /* 0x20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x2F */
66 /* 0x30 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x3F */
84 UMODE_NOFORWARD
, /* Q */
85 UMODE_REGONLYMSG
, /* R */
86 UMODE_SERVICE
, /* S */
93 UMODE_SSLCLIENT
, /* Z */
94 /* 0x5B */ 0, 0, 0, 0, 0, 0, /* 0x60 */
101 UMODE_CALLERID
, /* g */
103 UMODE_INVISIBLE
, /* i */
106 UMODE_LOCOPS
, /* l */
113 UMODE_SERVNOTICE
, /* s */
117 UMODE_WALLOP
, /* w */
120 UMODE_OPERWALL
, /* z */
121 /* 0x7B */ 0, 0, 0, 0, 0, /* 0x7F */
122 /* 0x80 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x9F */
123 /* 0x90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x9F */
124 /* 0xA0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xAF */
125 /* 0xB0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xBF */
126 /* 0xC0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xCF */
127 /* 0xD0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xDF */
128 /* 0xE0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xEF */
129 /* 0xF0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xFF */
136 * inputs - pointer to client
138 * side effects - display to client user counts etc.
141 show_lusers(struct Client
*source_p
)
143 if(rb_dlink_list_length(&lclient_list
) > (unsigned long)MaxClientCount
)
144 MaxClientCount
= rb_dlink_list_length(&lclient_list
);
146 if((rb_dlink_list_length(&lclient_list
) + rb_dlink_list_length(&serv_list
)) >
147 (unsigned long)MaxConnectionCount
)
148 MaxConnectionCount
= rb_dlink_list_length(&lclient_list
) +
149 rb_dlink_list_length(&serv_list
);
151 sendto_one_numeric(source_p
, RPL_LUSERCLIENT
, form_str(RPL_LUSERCLIENT
),
152 (Count
.total
- Count
.invisi
),
154 (int)rb_dlink_list_length(&global_serv_list
));
156 if(rb_dlink_list_length(&oper_list
) > 0)
157 sendto_one_numeric(source_p
, RPL_LUSEROP
,
158 form_str(RPL_LUSEROP
),
159 (int)rb_dlink_list_length(&oper_list
));
161 if(rb_dlink_list_length(&unknown_list
) > 0)
162 sendto_one_numeric(source_p
, RPL_LUSERUNKNOWN
,
163 form_str(RPL_LUSERUNKNOWN
),
164 (int)rb_dlink_list_length(&unknown_list
));
166 if(rb_dlink_list_length(&global_channel_list
) > 0)
167 sendto_one_numeric(source_p
, RPL_LUSERCHANNELS
,
168 form_str(RPL_LUSERCHANNELS
),
169 rb_dlink_list_length(&global_channel_list
));
171 sendto_one_numeric(source_p
, RPL_LUSERME
, form_str(RPL_LUSERME
),
172 (int)rb_dlink_list_length(&lclient_list
),
173 (int)rb_dlink_list_length(&serv_list
));
175 sendto_one_numeric(source_p
, RPL_LOCALUSERS
,
176 form_str(RPL_LOCALUSERS
),
177 (int)rb_dlink_list_length(&lclient_list
),
179 (int)rb_dlink_list_length(&lclient_list
),
182 sendto_one_numeric(source_p
, RPL_GLOBALUSERS
, form_str(RPL_GLOBALUSERS
),
183 Count
.total
, Count
.max_tot
,
184 Count
.total
, Count
.max_tot
);
186 sendto_one_numeric(source_p
, RPL_STATSCONN
,
187 form_str(RPL_STATSCONN
),
188 MaxConnectionCount
, MaxClientCount
,
189 Count
.totalrestartcount
);
193 ** register_local_user
194 ** This function is called when both NICK and USER messages
195 ** have been accepted for the client, in whatever order. Only
196 ** after this, is the USER message propagated.
198 ** NICK's must be propagated at once when received, although
199 ** it would be better to delay them too until full info is
200 ** available. Doing it is not so simple though, would have
201 ** to implement the following:
203 ** (actually it has been implemented already for a while) -orabidoo
205 ** 1) user telnets in and gives only "NICK foobar" and waits
206 ** 2) another user far away logs in normally with the nick
207 ** "foobar" (quite legal, as this server didn't propagate
209 ** 3) now this server gets nick "foobar" from outside, but
210 ** has alread the same defined locally. Current server
211 ** would just issue "KILL foobar" to clean out dups. But,
212 ** this is not fair. It should actually request another
213 ** nick from local user or kill him/her...
217 register_local_user(struct Client
*client_p
, struct Client
*source_p
)
219 struct ConfItem
*aconf
, *xconf
;
220 struct User
*user
= source_p
->user
;
221 char tmpstr2
[BUFSIZE
];
222 char ipaddr
[HOSTIPLEN
];
223 char myusername
[USERLEN
+1];
226 s_assert(NULL
!= source_p
);
227 s_assert(MyConnect(source_p
));
232 if(IsAnyDead(source_p
))
235 if(ConfigFileEntry
.ping_cookie
)
237 if(!(source_p
->flags
& FLAGS_PINGSENT
) && source_p
->localClient
->random_ping
== 0)
239 source_p
->localClient
->random_ping
= (unsigned long) (rand() * rand()) << 1;
240 sendto_one(source_p
, "PING :%08lX",
241 (unsigned long) source_p
->localClient
->random_ping
);
242 source_p
->flags
|= FLAGS_PINGSENT
;
245 if(!(source_p
->flags
& FLAGS_PING_COOKIE
))
251 /* hasnt finished client cap negotiation */
252 if(source_p
->flags
& FLAGS_CLICAP
)
255 /* still has DNSbls to validate against */
256 if(rb_dlink_list_length(&source_p
->preClient
->dnsbl_queries
) > 0)
259 client_p
->localClient
->last
= rb_current_time();
261 /* XXX - fixme. we shouldnt have to build a users buffer twice.. */
262 if(!IsGotId(source_p
) && (strchr(source_p
->username
, '[') != NULL
))
267 p
= source_p
->username
;
269 while(*p
&& i
< USERLEN
)
272 myusername
[i
++] = *p
;
276 myusername
[i
] = '\0';
279 rb_strlcpy(myusername
, source_p
->username
, sizeof myusername
);
281 if((status
= check_client(client_p
, source_p
, myusername
)) < 0)
282 return (CLIENT_EXITED
);
284 /* Apply nick override */
285 if(*source_p
->preClient
->spoofnick
)
287 char note
[NICKLEN
+ 10];
289 del_from_client_hash(source_p
->name
, source_p
);
290 rb_strlcpy(source_p
->name
, source_p
->preClient
->spoofnick
, NICKLEN
+ 1);
291 add_to_client_hash(source_p
->name
, source_p
);
293 snprintf(note
, NICKLEN
+ 10, "Nick: %s", source_p
->name
);
294 rb_note(source_p
->localClient
->F
, note
);
297 if(!valid_hostname(source_p
->host
))
299 sendto_one_notice(source_p
, ":*** Notice -- You have an illegal character in your hostname");
301 rb_strlcpy(source_p
->host
, source_p
->sockhost
, sizeof(source_p
->host
));
305 aconf
= source_p
->localClient
->att_conf
;
309 exit_client(client_p
, source_p
, &me
, "*** Not Authorised");
310 return (CLIENT_EXITED
);
313 if(IsConfSSLNeeded(aconf
) && !IsSSL(source_p
))
315 ServerStats
.is_ref
++;
316 sendto_one_notice(source_p
, ":*** Notice -- You need to use SSL/TLS to use this server");
317 exit_client(client_p
, source_p
, &me
, "Use SSL/TLS");
318 return (CLIENT_EXITED
);
321 if(!IsGotId(source_p
))
326 if(IsNeedIdentd(aconf
))
328 ServerStats
.is_ref
++;
329 sendto_one_notice(source_p
, ":*** Notice -- You need to install identd to use this server");
330 exit_client(client_p
, source_p
, &me
, "Install identd");
331 return (CLIENT_EXITED
);
334 /* dont replace username if its supposed to be spoofed --fl */
335 if(!IsConfDoSpoofIp(aconf
) || !strchr(aconf
->info
.name
, '@'))
339 if(!IsNoTilde(aconf
))
340 source_p
->username
[i
++] = '~';
342 while (*p
&& i
< USERLEN
)
345 source_p
->username
[i
++] = *p
;
349 source_p
->username
[i
] = '\0';
353 if(IsNeedSasl(aconf
) && !*user
->suser
)
355 ServerStats
.is_ref
++;
356 sendto_one_notice(source_p
, ":*** Notice -- You need to identify via SASL to use this server");
357 exit_client(client_p
, source_p
, &me
, "SASL access only");
358 return (CLIENT_EXITED
);
362 if(!EmptyString(aconf
->passwd
))
366 if(EmptyString(source_p
->localClient
->passwd
))
368 else if(IsConfEncrypted(aconf
))
369 encr
= rb_crypt(source_p
->localClient
->passwd
, aconf
->passwd
);
371 encr
= source_p
->localClient
->passwd
;
373 if(encr
== NULL
|| strcmp(encr
, aconf
->passwd
))
375 ServerStats
.is_ref
++;
376 sendto_one(source_p
, form_str(ERR_PASSWDMISMATCH
), me
.name
, source_p
->name
);
377 exit_client(client_p
, source_p
, &me
, "Bad Password");
378 return (CLIENT_EXITED
);
381 /* clear password only if used now, otherwise send it
382 * to services -- jilles */
383 if(source_p
->localClient
->passwd
)
385 memset(source_p
->localClient
->passwd
, 0, strlen(source_p
->localClient
->passwd
));
386 rb_free(source_p
->localClient
->passwd
);
387 source_p
->localClient
->passwd
= NULL
;
391 /* report if user has &^>= etc. and set flags as needed in source_p */
392 report_and_set_user_flags(source_p
, aconf
);
396 * We want to be able to have servers and F-line clients
397 * connect, so save room for "buffer" connections.
398 * Smaller servers may want to decrease this, and it should
399 * probably be just a percentage of the MAXCLIENTS...
402 /* Except "F:" clients */
403 if(rb_dlink_list_length(&lclient_list
) >=
404 (unsigned long)GlobalSetOptions
.maxclients
&& !IsConfExemptLimits(aconf
))
406 sendto_realops_snomask(SNO_FULL
, L_ALL
,
407 "Too many clients, rejecting %s[%s].", source_p
->name
, source_p
->host
);
409 ServerStats
.is_ref
++;
410 exit_client(client_p
, source_p
, &me
, "Sorry, server is full - try later");
411 return (CLIENT_EXITED
);
414 /* kline exemption extends to xline too */
415 if(!IsExemptKline(source_p
) &&
416 (xconf
= find_xline(source_p
->info
, 1)) != NULL
)
418 ServerStats
.is_ref
++;
419 add_reject(source_p
, xconf
->host
, NULL
);
420 exit_client(client_p
, source_p
, &me
, "Bad user info");
421 return CLIENT_EXITED
;
425 if (source_p
->preClient
->dnsbl_listed
!= NULL
)
427 if (IsExemptKline(source_p
) || IsConfExemptDNSBL(aconf
))
428 sendto_one_notice(source_p
, ":*** Your IP address %s is listed in %s, but you are exempt",
429 source_p
->sockhost
, source_p
->preClient
->dnsbl_listed
->host
);
432 sendto_realops_snomask(SNO_REJ
, L_NETWIDE
,
433 "Listed on DNSBL %s: %s (%s@%s) [%s] [%s]",
434 source_p
->preClient
->dnsbl_listed
->host
,
436 source_p
->username
, source_p
->host
,
437 IsIPSpoof(source_p
) ? "255.255.255.255" : source_p
->sockhost
,
440 rb_dlink_list varlist
= { NULL
, NULL
, 0 };
442 substitution_append_var(&varlist
, "nick", source_p
->name
);
443 substitution_append_var(&varlist
, "ip", source_p
->sockhost
);
444 substitution_append_var(&varlist
, "host", source_p
->host
);
445 substitution_append_var(&varlist
, "dnsbl-host", source_p
->preClient
->dnsbl_listed
->host
);
446 substitution_append_var(&varlist
, "network-name", ServerInfo
.network_name
);
448 ServerStats
.is_ref
++;
450 sendto_one(source_p
, form_str(ERR_YOUREBANNEDCREEP
),
451 me
.name
, source_p
->name
,
452 substitution_parse(source_p
->preClient
->dnsbl_listed
->reject_reason
, &varlist
));
454 substitution_free(&varlist
);
456 sendto_one_notice(source_p
, ":*** Your IP address %s is listed in %s",
457 source_p
->sockhost
, source_p
->preClient
->dnsbl_listed
->host
);
458 source_p
->preClient
->dnsbl_listed
->hits
++;
459 add_reject(source_p
, NULL
, NULL
);
460 exit_client(client_p
, source_p
, &me
, "*** Banned (DNS blacklist)");
461 return CLIENT_EXITED
;
465 /* valid user name check */
467 if(!valid_username(source_p
->username
))
469 sendto_realops_snomask(SNO_REJ
, L_ALL
,
470 "Invalid username: %s (%s@%s)",
471 source_p
->name
, source_p
->username
, source_p
->host
);
472 ServerStats
.is_ref
++;
473 sendto_one_notice(source_p
, ":*** Your username is invalid. Please make sure that your username contains "
474 "only alphanumeric characters.");
475 sprintf(tmpstr2
, "Invalid username [%s]", source_p
->username
);
476 exit_client(client_p
, source_p
, &me
, tmpstr2
);
477 return (CLIENT_EXITED
);
480 /* end of valid user name check */
482 /* Store original hostname -- jilles */
483 rb_strlcpy(source_p
->orighost
, source_p
->host
, HOSTLEN
+ 1);
485 /* Spoof user@host */
486 if(*source_p
->preClient
->spoofuser
)
487 rb_strlcpy(source_p
->username
, source_p
->preClient
->spoofuser
, USERLEN
+ 1);
488 if(*source_p
->preClient
->spoofhost
)
490 rb_strlcpy(source_p
->host
, source_p
->preClient
->spoofhost
, HOSTLEN
+ 1);
491 if (irccmp(source_p
->host
, source_p
->orighost
))
492 SetDynSpoof(source_p
);
495 source_p
->umodes
|= ConfigFileEntry
.default_umodes
& ~ConfigFileEntry
.oper_only_umodes
& ~orphaned_umodes
;
497 call_hook(h_new_local_user
, source_p
);
499 /* If they have died in send_* or were thrown out by the
500 * new_local_user hook don't do anything. */
501 if(IsAnyDead(source_p
))
502 return CLIENT_EXITED
;
504 /* To avoid inconsistencies, do not abort the registration
505 * starting from this point -- jilles
507 rb_inet_ntop_sock((struct sockaddr
*)&source_p
->localClient
->ip
, ipaddr
, sizeof(ipaddr
));
509 sendto_realops_snomask(SNO_CCONN
, L_ALL
,
510 "Client connecting: %s (%s@%s) [%s] {%s} [%s]",
511 source_p
->name
, source_p
->username
, source_p
->orighost
,
512 show_ip(NULL
, source_p
) ? ipaddr
: "255.255.255.255",
513 get_client_class(source_p
), source_p
->info
);
515 sendto_realops_snomask(SNO_CCONNEXT
, L_ALL
,
516 "CLICONN %s %s %s %s %s %s 0 %s",
517 source_p
->name
, source_p
->username
, source_p
->orighost
,
518 show_ip(NULL
, source_p
) ? ipaddr
: "255.255.255.255",
519 get_client_class(source_p
),
520 /* mirc can sometimes send ips here */
521 show_ip(NULL
, source_p
) ? source_p
->localClient
->fullcaps
: "<hidden> <hidden>",
524 add_to_hostname_hash(source_p
->orighost
, source_p
);
526 /* Allocate a UID if it was not previously allocated.
527 * If this already occured, it was probably during SASL auth...
531 strcpy(source_p
->id
, generate_uid());
532 add_to_id_hash(source_p
->id
, source_p
);
536 source_p
->umodes
|= UMODE_SSLCLIENT
;
538 if (source_p
->umodes
& UMODE_INVISIBLE
)
541 s_assert(!IsClient(source_p
));
542 rb_dlinkMoveNode(&source_p
->localClient
->tnode
, &unknown_list
, &lclient_list
);
545 source_p
->servptr
= &me
;
546 rb_dlinkAdd(source_p
, &source_p
->lnode
, &source_p
->servptr
->serv
->users
);
548 /* Increment our total user count here */
549 if(++Count
.total
> Count
.max_tot
)
550 Count
.max_tot
= Count
.total
;
552 Count
.totalrestartcount
++;
554 s_assert(source_p
->localClient
!= NULL
);
556 if(rb_dlink_list_length(&lclient_list
) > (unsigned long)Count
.max_loc
)
558 Count
.max_loc
= rb_dlink_list_length(&lclient_list
);
559 if(!(Count
.max_loc
% 10))
560 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
561 "New Max Local Clients: %d", Count
.max_loc
);
564 /* they get a reduced limit */
565 if(find_tgchange(source_p
->sockhost
))
566 source_p
->localClient
->targets_free
= TGCHANGE_INITIAL_LOW
;
568 source_p
->localClient
->targets_free
= TGCHANGE_INITIAL
;
570 monitor_signon(source_p
);
571 user_welcome(source_p
);
573 free_pre_client(source_p
);
575 introduce_client(client_p
, source_p
, user
, source_p
->name
, 1);
584 * side effects - This common function introduces a client to the rest
585 * of the net, either from a local client connect or
586 * from a remote connect.
589 introduce_client(struct Client
*client_p
, struct Client
*source_p
, struct User
*user
, const char *nick
, int use_euid
)
592 struct Client
*identifyservice_p
;
594 hook_data_umode_changed hdata
;
595 hook_data_client hdata2
;
597 if(MyClient(source_p
))
598 send_umode(source_p
, source_p
, 0, ubuf
);
600 send_umode(NULL
, source_p
, 0, ubuf
);
608 s_assert(has_id(source_p
));
611 sendto_server(client_p
, NULL
, CAP_EUID
| CAP_TS6
, NOCAPS
,
612 ":%s EUID %s %d %ld %s %s %s %s %s %s %s :%s",
613 source_p
->servptr
->id
, nick
,
614 source_p
->hopcount
+ 1,
615 (long) source_p
->tsinfo
, ubuf
,
616 source_p
->username
, source_p
->host
,
617 IsIPSpoof(source_p
) ? "0" : source_p
->sockhost
,
619 IsDynSpoof(source_p
) ? source_p
->orighost
: "*",
620 EmptyString(source_p
->user
->suser
) ? "*" : source_p
->user
->suser
,
623 sendto_server(client_p
, NULL
, CAP_TS6
, use_euid
? CAP_EUID
: NOCAPS
,
624 ":%s UID %s %d %ld %s %s %s %s %s :%s",
625 source_p
->servptr
->id
, nick
,
626 source_p
->hopcount
+ 1,
627 (long) source_p
->tsinfo
, ubuf
,
628 source_p
->username
, source_p
->host
,
629 IsIPSpoof(source_p
) ? "0" : source_p
->sockhost
,
630 source_p
->id
, source_p
->info
);
632 if(!EmptyString(source_p
->certfp
))
633 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
634 ":%s ENCAP * CERTFP :%s",
635 use_id(source_p
), source_p
->certfp
);
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
);
643 if (!EmptyString(source_p
->user
->suser
))
645 sendto_server(client_p
, NULL
, CAP_TS6
, use_euid
? CAP_EUID
: NOCAPS
, ":%s ENCAP * LOGIN %s",
646 use_id(source_p
), source_p
->user
->suser
);
649 if(MyConnect(source_p
) && source_p
->localClient
->passwd
)
651 if (!EmptyString(ConfigFileEntry
.identifyservice
) &&
652 !EmptyString(ConfigFileEntry
.identifycommand
))
654 /* use user@server */
655 p
= strchr(ConfigFileEntry
.identifyservice
, '@');
657 identifyservice_p
= find_named_client(p
+ 1);
659 identifyservice_p
= NULL
;
660 if (identifyservice_p
!= NULL
)
662 if (!EmptyString(source_p
->localClient
->auth_user
))
663 sendto_one(identifyservice_p
, ":%s PRIVMSG %s :%s %s %s",
664 get_id(source_p
, identifyservice_p
),
665 ConfigFileEntry
.identifyservice
,
666 ConfigFileEntry
.identifycommand
,
667 source_p
->localClient
->auth_user
,
668 source_p
->localClient
->passwd
);
670 sendto_one(identifyservice_p
, ":%s PRIVMSG %s :%s %s",
671 get_id(source_p
, identifyservice_p
),
672 ConfigFileEntry
.identifyservice
,
673 ConfigFileEntry
.identifycommand
,
674 source_p
->localClient
->passwd
);
677 memset(source_p
->localClient
->passwd
, 0, strlen(source_p
->localClient
->passwd
));
678 rb_free(source_p
->localClient
->passwd
);
679 source_p
->localClient
->passwd
= NULL
;
682 /* let modules providing usermodes know that we've got a new user,
683 * why is this here? -- well, some modules need to be able to send out new
684 * information about a client, so this was the best place to do it
687 hdata
.client
= source_p
;
689 hdata
.oldsnomask
= 0;
690 call_hook(h_umode_changed
, &hdata
);
692 /* On the other hand, some modules need to know when a client is
693 * being introduced, period.
696 hdata2
.client
= client_p
;
697 hdata2
.target
= source_p
;
698 call_hook(h_introduce_client
, &hdata2
);
702 * valid_hostname - check hostname for validity
704 * Inputs - pointer to user
705 * Output - true if valid, false if not
706 * Side effects - NONE
708 * NOTE: this doesn't allow a hostname to begin with a dot and
709 * will not allow more dots than chars.
712 valid_hostname(const char *hostname
)
714 const char *p
= hostname
, *last_slash
= 0;
722 if(!strcmp(hostname
, "localhost"))
725 if('.' == *p
|| ':' == *p
|| '/' == *p
)
732 if(*p
== '.' || *p
== ':')
745 if(last_slash
&& IsDigit(last_slash
[1]))
752 * valid_username - check username for validity
754 * Inputs - pointer to user
755 * Output - true if valid, false if not
756 * Side effects - NONE
758 * Absolutely always reject any '*' '!' '?' '@' in an user name
759 * reject any odd control characters names.
760 * Allow '.' in username to allow for "first.last"
764 valid_username(const char *username
)
767 const char *p
= username
;
777 /* reject usernames that don't start with an alphanum
778 * i.e. reject jokers who have '-@somehost' or '.@somehost'
779 * or "-hi-@somehost", "h-----@somehost" would still be accepted.
786 if((*p
== '.') && ConfigFileEntry
.dots_in_ident
)
789 if(dots
> ConfigFileEntry
.dots_in_ident
)
791 if(!IsUserChar(p
[1]))
794 else if(!IsUserChar(*p
))
800 /* report_and_set_user_flags
802 * Inputs - pointer to source_p
803 * - pointer to aconf for this user
806 * Report to user any special flags they are getting, and set them.
810 report_and_set_user_flags(struct Client
*source_p
, struct ConfItem
*aconf
)
812 /* If this user is being spoofed, tell them so */
813 if(IsConfDoSpoofIp(aconf
))
815 sendto_one_notice(source_p
, ":*** Spoofing your IP");
818 /* If this user is in the exception class, Set it "E lined" */
819 if(IsConfExemptKline(aconf
))
821 SetExemptKline(source_p
);
822 sendto_one_notice(source_p
, ":*** You are exempt from K/X lines");
825 if(IsConfExemptDNSBL(aconf
))
826 /* kline exempt implies this, don't send both */
827 if(!IsConfExemptKline(aconf
))
828 sendto_one_notice(source_p
, ":*** You are exempt from DNS blacklists");
830 /* If this user is exempt from user limits set it F lined" */
831 if(IsConfExemptLimits(aconf
))
833 sendto_one_notice(source_p
, ":*** You are exempt from user limits");
836 if(IsConfExemptFlood(aconf
))
838 SetExemptFlood(source_p
);
839 sendto_one_notice(source_p
, ":*** You are exempt from flood limits");
842 if(IsConfExemptSpambot(aconf
))
844 SetExemptSpambot(source_p
);
845 sendto_one_notice(source_p
, ":*** You are exempt from spambot checks");
848 if(IsConfExemptJupe(aconf
))
850 SetExemptJupe(source_p
);
851 sendto_one_notice(source_p
, ":*** You are exempt from juped channel warnings");
854 if(IsConfExemptResv(aconf
))
856 SetExemptResv(source_p
);
857 sendto_one_notice(source_p
, ":*** You are exempt from resvs");
860 if(IsConfExemptShide(aconf
))
862 SetExemptShide(source_p
);
863 sendto_one_notice(source_p
, ":*** You are exempt from serverhiding");
866 if(IsConfExtendChans(aconf
))
868 SetExtendChans(source_p
);
869 sendto_one_notice(source_p
, ":*** You are exempt from normal channel limits");
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 bool badflag
= false; /* Only send one bad flag notice */
914 bool showsnomask
= false;
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
, 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
, 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
, 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 SetExtendChans(source_p
);
1313 SetExemptKline(source_p
);
1315 source_p
->flags2
|= oper_p
->flags
;
1316 source_p
->localClient
->opername
= rb_strdup(oper_p
->name
);
1317 source_p
->localClient
->privset
= privilegeset_ref(oper_p
->privset
);
1319 rb_dlinkAddAlloc(source_p
, &local_oper_list
);
1320 rb_dlinkAddAlloc(source_p
, &oper_list
);
1322 if(IsOperAdmin(source_p
) && !IsOperHiddenAdmin(source_p
))
1323 source_p
->umodes
|= UMODE_ADMIN
;
1324 if(!IsOperN(source_p
))
1325 source_p
->snomask
&= ~SNO_NCHANGE
;
1326 if(!IsOperOperwall(source_p
))
1327 source_p
->umodes
&= ~UMODE_OPERWALL
;
1328 hdata
.client
= source_p
;
1329 hdata
.oldumodes
= old
;
1330 hdata
.oldsnomask
= oldsnomask
;
1331 call_hook(h_umode_changed
, &hdata
);
1333 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1334 "%s (%s!%s@%s) is now an operator", oper_p
->name
, source_p
->name
,
1335 source_p
->username
, source_p
->host
);
1336 if(!(old
& UMODE_INVISIBLE
) && IsInvisible(source_p
))
1338 if((old
& UMODE_INVISIBLE
) && !IsInvisible(source_p
))
1340 send_umode_out(source_p
, source_p
, old
);
1341 sendto_one_numeric(source_p
, RPL_SNOMASK
, form_str(RPL_SNOMASK
),
1342 construct_snobuf(source_p
->snomask
));
1343 sendto_one(source_p
, form_str(RPL_YOUREOPER
), me
.name
, source_p
->name
);
1344 sendto_one_notice(source_p
, ":*** Oper privilege set is %s", oper_p
->privset
->name
);
1345 sendto_one_notice(source_p
, ":*** Oper privs are %s", oper_p
->privset
->privs
);
1346 send_oper_motd(source_p
);
1353 * outputs - an available umode bitmask or
1354 * 0 if no umodes are available
1355 * side effects - NONE
1358 find_umode_slot(void)
1360 unsigned int all_umodes
= 0, my_umode
= 0, i
;
1362 for (i
= 0; i
< 128; i
++)
1363 all_umodes
|= user_modes
[i
];
1365 for (my_umode
= 1; my_umode
&& (all_umodes
& my_umode
);
1372 construct_umodebuf(void)
1375 char *ptr
= umodebuf
;
1376 static int prev_user_modes
[128];
1380 for (i
= 0; i
< 128; i
++)
1382 if (prev_user_modes
[i
] != 0 && prev_user_modes
[i
] != user_modes
[i
])
1384 if (user_modes
[i
] == 0)
1386 orphaned_umodes
|= prev_user_modes
[i
];
1387 sendto_realops_snomask(SNO_DEBUG
, L_ALL
, "Umode +%c is now orphaned", i
);
1391 orphaned_umodes
&= ~prev_user_modes
[i
];
1392 sendto_realops_snomask(SNO_DEBUG
, L_ALL
, "Orphaned umode +%c is picked up by module", i
);
1394 user_modes
[i
] = prev_user_modes
[i
];
1397 prev_user_modes
[i
] = user_modes
[i
];
1406 change_nick_user_host(struct Client
*target_p
, const char *nick
, const char *user
,
1407 const char *host
, int newts
, const char *format
, ...)
1410 struct Channel
*chptr
;
1411 struct membership
*mscptr
;
1412 int changed
= irccmp(target_p
->name
, nick
);
1413 int changed_case
= strcmp(target_p
->name
, nick
);
1414 int do_qjm
= irccmp(target_p
->username
, user
) || irccmp(target_p
->host
, host
);
1415 char mode
[10], modeval
[NICKLEN
* 2 + 2], reason
[256], *mptr
;
1422 target_p
->tsinfo
= newts
;
1423 monitor_signoff(target_p
);
1425 invalidate_bancache_user(target_p
);
1429 va_start(ap
, format
);
1430 vsnprintf(reason
, 255, format
, ap
);
1433 sendto_common_channels_local_butone(target_p
, NOCAPS
, CLICAP_CHGHOST
, ":%s!%s@%s QUIT :%s",
1434 target_p
->name
, target_p
->username
, target_p
->host
,
1437 RB_DLINK_FOREACH(ptr
, target_p
->user
->channel
.head
)
1440 chptr
= mscptr
->chptr
;
1443 if(is_chanop(mscptr
))
1446 strcat(modeval
, nick
);
1447 strcat(modeval
, " ");
1450 if(is_voiced(mscptr
))
1453 strcat(modeval
, nick
);
1458 sendto_channel_local_with_capability_butone(target_p
, ALL_MEMBERS
, NOCAPS
, CLICAP_EXTENDED_JOIN
| CLICAP_CHGHOST
, chptr
,
1459 ":%s!%s@%s JOIN %s", nick
, user
, host
, chptr
->chname
);
1460 sendto_channel_local_with_capability_butone(target_p
, ALL_MEMBERS
, CLICAP_EXTENDED_JOIN
, CLICAP_CHGHOST
, chptr
,
1461 ":%s!%s@%s JOIN %s %s :%s", nick
, user
, host
, chptr
->chname
,
1462 EmptyString(target_p
->user
->suser
) ? "*" : target_p
->user
->suser
,
1466 sendto_channel_local_with_capability_butone(target_p
, ALL_MEMBERS
, NOCAPS
, CLICAP_CHGHOST
, chptr
,
1467 ":%s MODE %s +%s %s", target_p
->servptr
->name
, chptr
->chname
, mode
, modeval
);
1472 /* Resend away message to away-notify enabled clients. */
1473 if (target_p
->user
->away
)
1474 sendto_common_channels_local_butone(target_p
, CLICAP_AWAY_NOTIFY
, CLICAP_CHGHOST
, ":%s!%s@%s AWAY :%s",
1476 target_p
->user
->away
);
1478 sendto_common_channels_local_butone(target_p
, CLICAP_CHGHOST
, NOCAPS
,
1479 ":%s!%s@%s CHGHOST %s %s",
1480 target_p
->name
, target_p
->username
, target_p
->host
, user
, host
);
1482 if(MyClient(target_p
) && changed_case
)
1483 sendto_one(target_p
, ":%s!%s@%s NICK %s",
1484 target_p
->name
, user
, host
, nick
);
1486 /* TODO: send some snotes to SNO_NCHANGE/SNO_CCONN/SNO_CCONNEXT? */
1488 else if(changed_case
)
1490 sendto_common_channels_local(target_p
, NOCAPS
, NOCAPS
, ":%s!%s@%s NICK :%s",
1491 target_p
->name
, user
, 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 if (user
!= target_p
->username
)
1501 rb_strlcpy(target_p
->username
, user
, sizeof target_p
->username
);
1503 rb_strlcpy(target_p
->host
, host
, sizeof target_p
->host
);
1506 whowas_add_history(target_p
, 1);
1508 del_from_client_hash(target_p
->name
, target_p
);
1509 rb_strlcpy(target_p
->name
, nick
, NICKLEN
);
1510 add_to_client_hash(target_p
->name
, target_p
);
1514 monitor_signon(target_p
);
1515 del_all_accepts(target_p
);