]> jfr.im git - irc/rqf/shadowircd.git/blame - src/s_user.c
Fix fd passing on FreeBSD/amd64 and possibly Solaris/sparc.
[irc/rqf/shadowircd.git] / src / s_user.c
CommitLineData
212380e3 1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * s_user.c: User related functions.
4 *
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
8 *
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.
13 *
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.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
0a61720a 24 * $Id: s_user.c 3586 2007-11-20 11:16:43Z nenolod $
212380e3 25 */
26
27#include "stdinc.h"
212380e3 28#include "s_user.h"
29#include "channel.h"
30#include "class.h"
31#include "client.h"
32#include "common.h"
33#include "hash.h"
13ae2f4b 34#include "match.h"
212380e3 35#include "ircd.h"
36#include "listener.h"
37#include "msg.h"
38#include "numeric.h"
212380e3 39#include "s_conf.h"
40#include "s_newconf.h"
d3455e2c 41#include "logger.h"
212380e3 42#include "s_serv.h"
43#include "s_stats.h"
44#include "scache.h"
45#include "send.h"
46#include "supported.h"
47#include "whowas.h"
212380e3 48#include "packet.h"
49#include "reject.h"
50#include "cache.h"
51#include "hook.h"
52#include "monitor.h"
53#include "snomask.h"
54#include "blacklist.h"
92fb5c31 55#include "substitution.h"
2392eb24 56#include "chmode.h"
212380e3 57
58static void report_and_set_user_flags(struct Client *, struct ConfItem *);
59void user_welcome(struct Client *source_p);
60
212380e3 61char umodebuf[128];
62
a8086a7c 63static int orphaned_umodes = 0;
212380e3 64int user_modes[256] = {
65 /* 0x00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0F */
66 /* 0x10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x1F */
67 /* 0x20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x2F */
68 /* 0x30 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x3F */
69 0, /* @ */
70 0, /* A */
71 0, /* B */
72 0, /* C */
73 UMODE_DEAF, /* D */
74 0, /* E */
75 0, /* F */
76 0, /* G */
77 0, /* H */
78 0, /* I */
79 0, /* J */
80 0, /* K */
81 0, /* L */
82 0, /* M */
83 0, /* N */
84 0, /* O */
85 0, /* P */
86 UMODE_NOFORWARD, /* Q */
87 UMODE_REGONLYMSG, /* R */
88 UMODE_SERVICE, /* S */
89 0, /* T */
90 0, /* U */
91 0, /* V */
92 0, /* W */
93 0, /* X */
94 0, /* Y */
16cb94cc 95 UMODE_SSLCLIENT, /* Z */
212380e3 96 /* 0x5B */ 0, 0, 0, 0, 0, 0, /* 0x60 */
97 UMODE_ADMIN, /* a */
98 0, /* b */
99 0, /* c */
100 0, /* d */
101 0, /* e */
102 0, /* f */
103 UMODE_CALLERID, /* g */
104 0, /* h */
105 UMODE_INVISIBLE, /* i */
106 0, /* j */
107 0, /* k */
108 UMODE_LOCOPS, /* l */
109 0, /* m */
110 0, /* n */
111 UMODE_OPER, /* o */
112 0, /* p */
113 0, /* q */
114 0, /* r */
115 UMODE_SERVNOTICE, /* s */
116 0, /* t */
117 0, /* u */
118 0, /* v */
119 UMODE_WALLOP, /* w */
120 0, /* x */
121 0, /* y */
122 UMODE_OPERWALL, /* z */
123 /* 0x7B */ 0, 0, 0, 0, 0, /* 0x7F */
124 /* 0x80 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x9F */
125 /* 0x90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x9F */
126 /* 0xA0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xAF */
127 /* 0xB0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xBF */
128 /* 0xC0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xCF */
129 /* 0xD0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xDF */
130 /* 0xE0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xEF */
131 /* 0xF0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xFF */
132};
133/* *INDENT-ON* */
134
135/*
136 * show_lusers -
137 *
138 * inputs - pointer to client
139 * output -
140 * side effects - display to client user counts etc.
141 */
142int
143show_lusers(struct Client *source_p)
144{
af81d5a0
WP
145 if(rb_dlink_list_length(&lclient_list) > (unsigned long)MaxClientCount)
146 MaxClientCount = rb_dlink_list_length(&lclient_list);
2f15f871 147
af81d5a0 148 if((rb_dlink_list_length(&lclient_list) + rb_dlink_list_length(&serv_list)) >
2f15f871 149 (unsigned long)MaxConnectionCount)
af81d5a0
WP
150 MaxConnectionCount = rb_dlink_list_length(&lclient_list) +
151 rb_dlink_list_length(&serv_list);
2f15f871 152
212380e3 153 sendto_one_numeric(source_p, RPL_LUSERCLIENT, form_str(RPL_LUSERCLIENT),
154 (Count.total - Count.invisi),
af81d5a0 155 Count.invisi, rb_dlink_list_length(&global_serv_list));
212380e3 156
af81d5a0 157 if(rb_dlink_list_length(&oper_list) > 0)
212380e3 158 sendto_one_numeric(source_p, RPL_LUSEROP,
af81d5a0 159 form_str(RPL_LUSEROP), rb_dlink_list_length(&oper_list));
212380e3 160
af81d5a0 161 if(rb_dlink_list_length(&unknown_list) > 0)
212380e3 162 sendto_one_numeric(source_p, RPL_LUSERUNKNOWN,
163 form_str(RPL_LUSERUNKNOWN),
af81d5a0 164 rb_dlink_list_length(&unknown_list));
212380e3 165
af81d5a0 166 if(rb_dlink_list_length(&global_channel_list) > 0)
212380e3 167 sendto_one_numeric(source_p, RPL_LUSERCHANNELS,
168 form_str(RPL_LUSERCHANNELS),
af81d5a0 169 rb_dlink_list_length(&global_channel_list));
212380e3 170
171 sendto_one_numeric(source_p, RPL_LUSERME, form_str(RPL_LUSERME),
af81d5a0
WP
172 rb_dlink_list_length(&lclient_list),
173 rb_dlink_list_length(&serv_list));
212380e3 174
175 sendto_one_numeric(source_p, RPL_LOCALUSERS,
176 form_str(RPL_LOCALUSERS),
af81d5a0 177 rb_dlink_list_length(&lclient_list),
212380e3 178 Count.max_loc,
af81d5a0 179 rb_dlink_list_length(&lclient_list),
212380e3 180 Count.max_loc);
181
182 sendto_one_numeric(source_p, RPL_GLOBALUSERS, form_str(RPL_GLOBALUSERS),
183 Count.total, Count.max_tot,
184 Count.total, Count.max_tot);
185
186 sendto_one_numeric(source_p, RPL_STATSCONN,
187 form_str(RPL_STATSCONN),
188 MaxConnectionCount, MaxClientCount,
189 Count.totalrestartcount);
190
212380e3 191 return 0;
192}
193
194/*
195** register_local_user
196** This function is called when both NICK and USER messages
197** have been accepted for the client, in whatever order. Only
198** after this, is the USER message propagated.
199**
200** NICK's must be propagated at once when received, although
201** it would be better to delay them too until full info is
202** available. Doing it is not so simple though, would have
203** to implement the following:
204**
205** (actually it has been implemented already for a while) -orabidoo
206**
207** 1) user telnets in and gives only "NICK foobar" and waits
208** 2) another user far away logs in normally with the nick
209** "foobar" (quite legal, as this server didn't propagate
210** it).
211** 3) now this server gets nick "foobar" from outside, but
212** has alread the same defined locally. Current server
213** would just issue "KILL foobar" to clean out dups. But,
214** this is not fair. It should actually request another
215** nick from local user or kill him/her...
216*/
217
218int
219register_local_user(struct Client *client_p, struct Client *source_p, const char *username)
220{
35f6f850 221 struct ConfItem *aconf, *xconf;
212380e3 222 struct User *user = source_p->user;
223 char tmpstr2[IRCD_BUFSIZE];
224 char ipaddr[HOSTIPLEN];
225 char myusername[USERLEN+1];
226 int status;
227
228 s_assert(NULL != source_p);
229 s_assert(MyConnect(source_p));
230 s_assert(source_p->username != username);
231
232 if(source_p == NULL)
233 return -1;
234
235 if(IsAnyDead(source_p))
236 return -1;
237
238 if(ConfigFileEntry.ping_cookie)
239 {
240 if(!(source_p->flags & FLAGS_PINGSENT) && source_p->localClient->random_ping == 0)
241 {
242 source_p->localClient->random_ping = (unsigned long) (rand() * rand()) << 1;
243 sendto_one(source_p, "PING :%08lX",
244 (unsigned long) source_p->localClient->random_ping);
245 source_p->flags |= FLAGS_PINGSENT;
246 return -1;
247 }
2d2c402d 248 if(!(source_p->flags & FLAGS_PING_COOKIE))
212380e3 249 {
250 return -1;
251 }
252 }
253
254 /* hasnt finished client cap negotiation */
2d2c402d 255 if(source_p->flags & FLAGS_CLICAP)
212380e3 256 return -1;
257
258 /* still has DNSbls to validate against */
af81d5a0 259 if(rb_dlink_list_length(&source_p->preClient->dnsbl_queries) > 0)
212380e3 260 return -1;
261
9f6bbe3c 262 client_p->localClient->last = rb_current_time();
212380e3 263 /* Straight up the maximum rate of flooding... */
264 source_p->localClient->allow_read = MAX_FLOOD_BURST;
265
266 /* XXX - fixme. we shouldnt have to build a users buffer twice.. */
267 if(!IsGotId(source_p) && (strchr(username, '[') != NULL))
268 {
269 const char *p;
270 int i = 0;
271
272 p = username;
273
274 while(*p && i < USERLEN)
275 {
276 if(*p != '[')
277 myusername[i++] = *p;
278 p++;
279 }
280
281 myusername[i] = '\0';
282 username = myusername;
283 }
284
285 if((status = check_client(client_p, source_p, username)) < 0)
286 return (CLIENT_EXITED);
287
288 /* Apply nick override */
289 if(*source_p->preClient->spoofnick)
290 {
73897182
WP
291 char note[NICKLEN + 10];
292
212380e3 293 del_from_client_hash(source_p->name, source_p);
907468c4 294 rb_strlcpy(source_p->name, source_p->preClient->spoofnick, NICKLEN + 1);
212380e3 295 add_to_client_hash(source_p->name, source_p);
73897182
WP
296
297 rb_snprintf(note, NICKLEN + 10, "Nick: %s", source_p->name);
298 rb_note(source_p->localClient->F, note);
212380e3 299 }
300
301 if(!valid_hostname(source_p->host))
302 {
5366977b 303 sendto_one_notice(source_p, ":*** Notice -- You have an illegal character in your hostname");
212380e3 304
907468c4 305 rb_strlcpy(source_p->host, source_p->sockhost, sizeof(source_p->host));
212380e3 306 }
307
308
309 aconf = source_p->localClient->att_conf;
310
311 if(aconf == NULL)
312 {
313 exit_client(client_p, source_p, &me, "*** Not Authorised");
314 return (CLIENT_EXITED);
315 }
316
663bbb28
JT
317 if(IsConfSSLNeeded(aconf) && !IsSSL(source_p))
318 {
319 ServerStats.is_ref++;
320 sendto_one_notice(source_p, ":*** Notice -- You need to use SSL/TLS to use this server");
321 exit_client(client_p, source_p, &me, "Use SSL/TLS");
322 return (CLIENT_EXITED);
323 }
324
212380e3 325 if(!IsGotId(source_p))
326 {
327 const char *p;
328 int i = 0;
329
330 if(IsNeedIdentd(aconf))
331 {
83251205 332 ServerStats.is_ref++;
5366977b 333 sendto_one_notice(source_p, ":*** Notice -- You need to install identd to use this server");
212380e3 334 exit_client(client_p, source_p, &me, "Install identd");
335 return (CLIENT_EXITED);
336 }
337
338 /* dont replace username if its supposed to be spoofed --fl */
339 if(!IsConfDoSpoofIp(aconf) || !strchr(aconf->name, '@'))
340 {
341 p = username;
342
343 if(!IsNoTilde(aconf))
344 source_p->username[i++] = '~';
345
346 while (*p && i < USERLEN)
347 {
348 if(*p != '[')
349 source_p->username[i++] = *p;
350 p++;
351 }
352
353 source_p->username[i] = '\0';
354 }
355 }
356
357 if(IsNeedSasl(aconf) && !*user->suser)
358 {
83251205 359 ServerStats.is_ref++;
5366977b 360 sendto_one_notice(source_p, ":*** Notice -- You need to identify via SASL to use this server");
212380e3 361 exit_client(client_p, source_p, &me, "SASL access only");
362 return (CLIENT_EXITED);
363 }
364
365 /* password check */
366 if(!EmptyString(aconf->passwd))
367 {
368 const char *encr;
369
370 if(EmptyString(source_p->localClient->passwd))
371 encr = "";
372 else if(IsConfEncrypted(aconf))
cd1dea97 373 encr = rb_crypt(source_p->localClient->passwd, aconf->passwd);
212380e3 374 else
375 encr = source_p->localClient->passwd;
376
377 if(strcmp(encr, aconf->passwd))
378 {
83251205 379 ServerStats.is_ref++;
212380e3 380 sendto_one(source_p, form_str(ERR_PASSWDMISMATCH), me.name, source_p->name);
381 exit_client(client_p, source_p, &me, "Bad Password");
382 return (CLIENT_EXITED);
383 }
384
385 /* clear password only if used now, otherwise send it
386 * to services -- jilles */
387 if(source_p->localClient->passwd)
388 {
389 memset(source_p->localClient->passwd, 0, strlen(source_p->localClient->passwd));
90a3c35b 390 rb_free(source_p->localClient->passwd);
212380e3 391 source_p->localClient->passwd = NULL;
392 }
393 }
394
395 /* report if user has &^>= etc. and set flags as needed in source_p */
396 report_and_set_user_flags(source_p, aconf);
397
398 /* Limit clients */
399 /*
400 * We want to be able to have servers and F-line clients
401 * connect, so save room for "buffer" connections.
402 * Smaller servers may want to decrease this, and it should
403 * probably be just a percentage of the MAXCLIENTS...
404 * -Taner
405 */
406 /* Except "F:" clients */
af81d5a0 407 if(rb_dlink_list_length(&lclient_list) >=
7506208c 408 (unsigned long)GlobalSetOptions.maxclients && !IsConfExemptLimits(aconf))
212380e3 409 {
410 sendto_realops_snomask(SNO_FULL, L_ALL,
411 "Too many clients, rejecting %s[%s].", source_p->name, source_p->host);
412
83251205 413 ServerStats.is_ref++;
212380e3 414 exit_client(client_p, source_p, &me, "Sorry, server is full - try later");
415 return (CLIENT_EXITED);
416 }
417
212380e3 418 /* kline exemption extends to xline too */
419 if(!IsExemptKline(source_p) &&
35f6f850 420 (xconf = find_xline(source_p->info, 1)) != NULL)
212380e3 421 {
83251205 422 ServerStats.is_ref++;
21c9d815 423 add_reject(source_p, xconf->name, NULL);
212380e3 424 exit_client(client_p, source_p, &me, "Bad user info");
425 return CLIENT_EXITED;
426 }
427
428 /* dnsbl check */
429 if (source_p->preClient->dnsbl_listed != NULL)
430 {
431 if (IsExemptKline(source_p) || IsConfExemptDNSBL(aconf))
432 sendto_one_notice(source_p, ":*** Your IP address %s is listed in %s, but you are exempt",
433 source_p->sockhost, source_p->preClient->dnsbl_listed->host);
434 else
435 {
af81d5a0 436 rb_dlink_list varlist = { NULL, NULL, 0 };
92fb5c31 437
438 substitution_append_var(&varlist, "nick", source_p->name);
439 substitution_append_var(&varlist, "ip", source_p->sockhost);
440 substitution_append_var(&varlist, "host", source_p->host);
441 substitution_append_var(&varlist, "dnsbl-host", source_p->preClient->dnsbl_listed->host);
442 substitution_append_var(&varlist, "network-name", ServerInfo.network_name);
443
83251205 444 ServerStats.is_ref++;
92fb5c31 445
212380e3 446 sendto_one(source_p, form_str(ERR_YOUREBANNEDCREEP),
447 me.name, source_p->name,
92fb5c31 448 substitution_parse(source_p->preClient->dnsbl_listed->reject_reason, &varlist));
449
450 substitution_free(&varlist);
451
212380e3 452 sendto_one_notice(source_p, ":*** Your IP address %s is listed in %s",
453 source_p->sockhost, source_p->preClient->dnsbl_listed->host);
454 source_p->preClient->dnsbl_listed->hits++;
21c9d815 455 add_reject(source_p, NULL, NULL);
212380e3 456 exit_client(client_p, source_p, &me, "*** Banned (DNS blacklist)");
457 return CLIENT_EXITED;
458 }
459 }
460
0734f634 461 /* valid user name check */
462
463 if(!valid_username(source_p->username))
464 {
465 sendto_realops_snomask(SNO_REJ, L_ALL,
466 "Invalid username: %s (%s@%s)",
467 source_p->name, source_p->username, source_p->host);
83251205 468 ServerStats.is_ref++;
cc32202d 469 sendto_one_notice(source_p, ":*** Your username is invalid. Please make sure that your username contains "
470 "only alphanumeric characters.");
38e6acdd 471 rb_sprintf(tmpstr2, "Invalid username [%s]", source_p->username);
0734f634 472 exit_client(client_p, source_p, &me, tmpstr2);
473 return (CLIENT_EXITED);
474 }
475
476 /* end of valid user name check */
477
212380e3 478 /* Store original hostname -- jilles */
907468c4 479 rb_strlcpy(source_p->orighost, source_p->host, HOSTLEN + 1);
212380e3 480
481 /* Spoof user@host */
482 if(*source_p->preClient->spoofuser)
907468c4 483 rb_strlcpy(source_p->username, source_p->preClient->spoofuser, USERLEN + 1);
212380e3 484 if(*source_p->preClient->spoofhost)
485 {
907468c4 486 rb_strlcpy(source_p->host, source_p->preClient->spoofhost, HOSTLEN + 1);
212380e3 487 if (irccmp(source_p->host, source_p->orighost))
488 SetDynSpoof(source_p);
489 }
490
e2b44358 491 source_p->umodes |= ConfigFileEntry.default_umodes & ~ConfigFileEntry.oper_only_umodes & ~orphaned_umodes;
492
493 call_hook(h_new_local_user, source_p);
494
495 /* If they have died in send_* or were thrown out by the
496 * new_local_user hook don't do anything. */
497 if(IsAnyDead(source_p))
212380e3 498 return CLIENT_EXITED;
499
e2b44358 500 /* To avoid inconsistencies, do not abort the registration
501 * starting from this point -- jilles
502 */
9879cd59 503 rb_inet_ntop_sock((struct sockaddr *)&source_p->localClient->ip, ipaddr, sizeof(ipaddr));
212380e3 504
505 sendto_realops_snomask(SNO_CCONN, L_ALL,
506 "Client connecting: %s (%s@%s) [%s] {%s} [%s]",
507 source_p->name, source_p->username, source_p->orighost,
508 show_ip(NULL, source_p) ? ipaddr : "255.255.255.255",
509 get_client_class(source_p), source_p->info);
510
511 sendto_realops_snomask(SNO_CCONNEXT, L_ALL,
512 "CLICONN %s %s %s %s %s %s 0 %s",
513 source_p->name, source_p->username, source_p->orighost,
514 show_ip(NULL, source_p) ? ipaddr : "255.255.255.255",
515 get_client_class(source_p),
516 /* mirc can sometimes send ips here */
517 show_ip(NULL, source_p) ? source_p->localClient->fullcaps : "<hidden> <hidden>",
518 source_p->info);
519
212380e3 520 add_to_hostname_hash(source_p->orighost, source_p);
521
522 /* Allocate a UID if it was not previously allocated.
523 * If this already occured, it was probably during SASL auth...
524 */
525 if(!*source_p->id)
526 {
527 strcpy(source_p->id, generate_uid());
528 add_to_id_hash(source_p->id, source_p);
529 }
530
16cb94cc
WP
531 if (IsSSL(source_p))
532 source_p->umodes |= UMODE_SSLCLIENT;
533
212380e3 534 if (source_p->umodes & UMODE_INVISIBLE)
535 Count.invisi++;
536
537 s_assert(!IsClient(source_p));
af81d5a0 538 rb_dlinkMoveNode(&source_p->localClient->tnode, &unknown_list, &lclient_list);
212380e3 539 SetClient(source_p);
540
0a61720a 541 source_p->servptr = &me;
af81d5a0 542 rb_dlinkAdd(source_p, &source_p->lnode, &source_p->servptr->serv->users);
0a61720a 543
212380e3 544 /* Increment our total user count here */
545 if(++Count.total > Count.max_tot)
546 Count.max_tot = Count.total;
0a61720a 547
212380e3 548 source_p->localClient->allow_read = MAX_FLOOD_BURST;
549
550 Count.totalrestartcount++;
551
552 s_assert(source_p->localClient != NULL);
553
af81d5a0 554 if(rb_dlink_list_length(&lclient_list) > (unsigned long)Count.max_loc)
212380e3 555 {
af81d5a0 556 Count.max_loc = rb_dlink_list_length(&lclient_list);
212380e3 557 if(!(Count.max_loc % 10))
558 sendto_realops_snomask(SNO_GENERAL, L_ALL,
559 "New Max Local Clients: %d", Count.max_loc);
560 }
561
562 /* they get a reduced limit */
563 if(find_tgchange(source_p->sockhost))
564 USED_TARGETS(source_p) = 6;
565
212380e3 566 monitor_signon(source_p);
567 user_welcome(source_p);
568
569 free_pre_client(source_p);
570
571 return (introduce_client(client_p, source_p, user, source_p->name, 1));
572}
573
574/*
575 * introduce_clients
576 *
577 * inputs -
578 * output -
579 * side effects - This common function introduces a client to the rest
580 * of the net, either from a local client connect or
581 * from a remote connect.
582 */
583int
584introduce_client(struct Client *client_p, struct Client *source_p, struct User *user, const char *nick, int use_euid)
585{
586 static char ubuf[12];
587 struct Client *identifyservice_p;
588 char *p;
589 hook_data_umode_changed hdata;
590 hook_data_client hdata2;
a4857d78 591 char sockhost[HOSTLEN];
212380e3 592
593 if(MyClient(source_p))
594 send_umode(source_p, source_p, 0, 0, ubuf);
595 else
596 send_umode(NULL, source_p, 0, 0, ubuf);
597
598 if(!*ubuf)
599 {
600 ubuf[0] = '+';
601 ubuf[1] = '\0';
602 }
603
a4857d78
WP
604 s_assert(has_id(source_p));
605
606 if(source_p->sockhost[0] == ':')
212380e3 607 {
a4857d78
WP
608 sockhost[0] = '0';
609 sockhost[1] = '\0';
610 rb_strlcat(sockhost, source_p->sockhost, sizeof(sockhost));
611 } else
612 strcpy(sockhost, source_p->sockhost);
212380e3 613
a4857d78
WP
614 if (use_euid)
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" : sockhost,
622 source_p->id,
623 IsDynSpoof(source_p) ? source_p->orighost : "*",
624 EmptyString(source_p->user->suser) ? "*" : source_p->user->suser,
625 source_p->info);
626
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" : sockhost,
634 source_p->id, source_p->info);
212380e3 635
636 if (IsDynSpoof(source_p))
637 {
638 sendto_server(client_p, NULL, CAP_TS6, use_euid ? CAP_EUID : NOCAPS, ":%s ENCAP * REALHOST %s",
639 use_id(source_p), source_p->orighost);
212380e3 640 }
a4857d78 641
212380e3 642 if (!EmptyString(source_p->user->suser))
643 {
644 sendto_server(client_p, NULL, CAP_TS6, use_euid ? CAP_EUID : NOCAPS, ":%s ENCAP * LOGIN %s",
645 use_id(source_p), source_p->user->suser);
212380e3 646 }
647
648 if(MyConnect(source_p) && source_p->localClient->passwd)
649 {
650 if (ConfigFileEntry.identifyservice[0] != '\0' &&
651 ConfigFileEntry.identifycommand[0] != '\0')
652 {
653 /* use user@server */
654 p = strchr(ConfigFileEntry.identifyservice, '@');
655 if (p != NULL)
656 identifyservice_p = find_named_client(p + 1);
657 else
658 identifyservice_p = NULL;
659 if (identifyservice_p != NULL)
645103ac
SB
660 {
661 if (!EmptyString(source_p->localClient->auth_user))
662 sendto_one(identifyservice_p, ":%s PRIVMSG %s :%s %s %s",
663 get_id(source_p, identifyservice_p),
664 ConfigFileEntry.identifyservice,
665 ConfigFileEntry.identifycommand,
666 source_p->localClient->auth_user,
667 source_p->localClient->passwd);
668 else
669 sendto_one(identifyservice_p, ":%s PRIVMSG %s :%s %s",
670 get_id(source_p, identifyservice_p),
671 ConfigFileEntry.identifyservice,
672 ConfigFileEntry.identifycommand,
673 source_p->localClient->passwd);
674 }
212380e3 675 }
676 memset(source_p->localClient->passwd, 0, strlen(source_p->localClient->passwd));
90a3c35b 677 rb_free(source_p->localClient->passwd);
212380e3 678 source_p->localClient->passwd = NULL;
679 }
680
681 /* let modules providing usermodes know that we've got a new user,
682 * why is this here? -- well, some modules need to be able to send out new
683 * information about a client, so this was the best place to do it
684 * --nenolod
685 */
686 hdata.client = source_p;
687 hdata.oldumodes = 0;
688 hdata.oldsnomask = 0;
689 call_hook(h_umode_changed, &hdata);
690
691 /* On the other hand, some modules need to know when a client is
692 * being introduced, period.
693 * --gxti
694 */
695 hdata2.client = client_p;
696 hdata2.target = source_p;
697 call_hook(h_introduce_client, &hdata2);
698
699 return 0;
700}
701
702/*
703 * valid_hostname - check hostname for validity
704 *
705 * Inputs - pointer to user
706 * Output - YES if valid, NO if not
707 * Side effects - NONE
708 *
709 * NOTE: this doesn't allow a hostname to begin with a dot and
710 * will not allow more dots than chars.
711 */
712int
713valid_hostname(const char *hostname)
714{
59bb8093 715 const char *p = hostname, *last_slash = 0;
212380e3 716 int found_sep = 0;
717
718 s_assert(NULL != p);
719
720 if(hostname == NULL)
721 return NO;
722
59bb8093 723 if('.' == *p || ':' == *p || '/' == *p)
212380e3 724 return NO;
725
726 while (*p)
727 {
728 if(!IsHostChar(*p))
729 return NO;
730 if(*p == '.' || *p == ':')
731 found_sep++;
59bb8093
SB
732 else if(*p == '/')
733 {
734 found_sep++;
735 last_slash = p;
736 }
212380e3 737 p++;
738 }
739
740 if(found_sep == 0)
59bb8093
SB
741 return NO;
742
743 if(last_slash && IsDigit(last_slash[1]))
744 return NO;
212380e3 745
59bb8093 746 return YES;
212380e3 747}
748
749/*
750 * valid_username - check username for validity
751 *
752 * Inputs - pointer to user
753 * Output - YES if valid, NO if not
754 * Side effects - NONE
755 *
756 * Absolutely always reject any '*' '!' '?' '@' in an user name
757 * reject any odd control characters names.
758 * Allow '.' in username to allow for "first.last"
759 * style of username
760 */
761int
762valid_username(const char *username)
763{
764 int dots = 0;
765 const char *p = username;
766
767 s_assert(NULL != p);
768
769 if(username == NULL)
770 return NO;
771
772 if('~' == *p)
773 ++p;
774
775 /* reject usernames that don't start with an alphanum
776 * i.e. reject jokers who have '-@somehost' or '.@somehost'
777 * or "-hi-@somehost", "h-----@somehost" would still be accepted.
778 */
779 if(!IsAlNum(*p))
780 return NO;
781
782 while (*++p)
783 {
784 if((*p == '.') && ConfigFileEntry.dots_in_ident)
785 {
786 dots++;
787 if(dots > ConfigFileEntry.dots_in_ident)
788 return NO;
789 if(!IsUserChar(p[1]))
790 return NO;
791 }
792 else if(!IsUserChar(*p))
793 return NO;
794 }
795 return YES;
796}
797
798/* report_and_set_user_flags
799 *
800 * Inputs - pointer to source_p
801 * - pointer to aconf for this user
802 * Output - NONE
803 * Side effects -
804 * Report to user any special flags they are getting, and set them.
805 */
806
807static void
808report_and_set_user_flags(struct Client *source_p, struct ConfItem *aconf)
809{
810 /* If this user is being spoofed, tell them so */
811 if(IsConfDoSpoofIp(aconf))
812 {
51e1ce90 813 sendto_one_notice(source_p, ":*** Spoofing your IP");
212380e3 814 }
815
816 /* If this user is in the exception class, Set it "E lined" */
817 if(IsConfExemptKline(aconf))
818 {
819 SetExemptKline(source_p);
e7c88922 820 sendto_one_notice(source_p, ":*** You are exempt from K/X lines");
212380e3 821 }
822
212380e3 823 if(IsConfExemptDNSBL(aconf))
824 /* kline exempt implies this, don't send both */
825 if(!IsConfExemptKline(aconf))
51e1ce90 826 sendto_one_notice(source_p, ":*** You are exempt from DNS blacklists");
212380e3 827
828 /* If this user is exempt from user limits set it F lined" */
829 if(IsConfExemptLimits(aconf))
830 {
51e1ce90 831 sendto_one_notice(source_p, ":*** You are exempt from user limits");
212380e3 832 }
833
212380e3 834 if(IsConfExemptFlood(aconf))
835 {
836 SetExemptFlood(source_p);
51e1ce90 837 sendto_one_notice(source_p, ":*** You are exempt from flood limits");
212380e3 838 }
839
840 if(IsConfExemptSpambot(aconf))
841 {
842 SetExemptSpambot(source_p);
51e1ce90 843 sendto_one_notice(source_p, ":*** You are exempt from spambot checks");
212380e3 844 }
845
846 if(IsConfExemptJupe(aconf))
847 {
848 SetExemptJupe(source_p);
51e1ce90 849 sendto_one_notice(source_p, ":*** You are exempt from juped channel warnings");
212380e3 850 }
851
852 if(IsConfExemptResv(aconf))
853 {
854 SetExemptResv(source_p);
51e1ce90 855 sendto_one_notice(source_p, ":*** You are exempt from resvs");
212380e3 856 }
857
858 if(IsConfExemptShide(aconf))
859 {
860 SetExemptShide(source_p);
51e1ce90 861 sendto_one_notice(source_p, ":*** You are exempt from serverhiding");
212380e3 862 }
863}
864
3471ceb5
JT
865static void
866show_other_user_mode(struct Client *source_p, struct Client *target_p)
867{
868 int i;
869 char buf[BUFSIZE];
870 char *m;
871
872 m = buf;
873 *m++ = '+';
874
875 for (i = 0; i < 128; i++) /* >= 127 is extended ascii */
876 if (target_p->umodes & user_modes[i])
877 *m++ = (char) i;
878 *m = '\0';
879
880 if (MyConnect(target_p) && target_p->snomask != 0)
881 sendto_one_notice(source_p, ":Modes for %s are %s %s",
882 target_p->name, buf,
883 construct_snobuf(target_p->snomask));
884 else
885 sendto_one_notice(source_p, ":Modes for %s are %s",
886 target_p->name, buf);
887}
888
212380e3 889/*
890 * user_mode - set get current users mode
891 *
892 * m_umode() added 15/10/91 By Darren Reed.
212380e3 893 * parv[1] - username to change mode for
894 * parv[2] - modes to change
895 */
896int
897user_mode(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
898{
899 int flag;
900 int i;
901 char *m;
902 const char *pm;
903 struct Client *target_p;
904 int what, setflags;
905 int badflag = NO; /* Only send one bad flag notice */
906 int showsnomask = NO;
907 unsigned int setsnomask;
908 char buf[BUFSIZE];
909 hook_data_umode_changed hdata;
910
911 what = MODE_ADD;
912
913 if(parc < 2)
914 {
915 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS), me.name, source_p->name, "MODE");
916 return 0;
917 }
918
919 if((target_p = MyClient(source_p) ? find_named_person(parv[1]) : find_person(parv[1])) == NULL)
920 {
921 if(MyConnect(source_p))
922 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
923 form_str(ERR_NOSUCHCHANNEL), parv[1]);
924 return 0;
925 }
926
927 /* Dont know why these were commented out..
928 * put them back using new sendto() funcs
929 */
930
931 if(IsServer(source_p))
932 {
933 sendto_realops_snomask(SNO_GENERAL, L_ADMIN,
934 "*** Mode for User %s from %s", parv[1], source_p->name);
935 return 0;
936 }
937
53ece38b 938 if(source_p != target_p)
212380e3 939 {
3471ceb5
JT
940 if (MyOper(source_p) && parc < 3)
941 show_other_user_mode(source_p, target_p);
942 else
943 sendto_one(source_p, form_str(ERR_USERSDONTMATCH), me.name, source_p->name);
212380e3 944 return 0;
945 }
946
947 if(parc < 3)
948 {
949 m = buf;
950 *m++ = '+';
951
952 for (i = 0; i < 128; i++) /* >= 127 is extended ascii */
953 if (source_p->umodes & user_modes[i])
954 *m++ = (char) i;
955
956 *m = '\0';
e8a2d50d 957 sendto_one_numeric(source_p, RPL_UMODEIS, form_str(RPL_UMODEIS), buf);
88520303 958
212380e3 959 if (source_p->snomask != 0)
88520303 960 sendto_one_numeric(source_p, RPL_SNOMASK, form_str(RPL_SNOMASK),
212380e3 961 construct_snobuf(source_p->snomask));
88520303 962
212380e3 963 return 0;
964 }
965
966 /* find flags already set for user */
967 setflags = source_p->umodes;
968 setsnomask = source_p->snomask;
969
970 /*
971 * parse mode change string(s)
972 */
973 for (pm = parv[2]; *pm; pm++)
974 switch (*pm)
975 {
976 case '+':
977 what = MODE_ADD;
978 break;
979 case '-':
980 what = MODE_DEL;
981 break;
982
983 case 'o':
984 if(what == MODE_ADD)
985 {
986 if(IsServer(client_p) && !IsOper(source_p))
987 {
988 ++Count.oper;
989 SetOper(source_p);
af81d5a0 990 rb_dlinkAddAlloc(source_p, &oper_list);
212380e3 991 }
992 }
993 else
994 {
995 /* Only decrement the oper counts if an oper to begin with
996 * found by Pat Szuta, Perly , perly@xnet.com
997 */
998
999 if(!IsOper(source_p))
1000 break;
1001
1002 ClearOper(source_p);
1003
1004 Count.oper--;
1005
1006 if(MyConnect(source_p))
1007 {
1008 source_p->umodes &= ~ConfigFileEntry.oper_only_umodes;
1009 if (!(source_p->umodes & UMODE_SERVNOTICE) && source_p->snomask != 0)
1010 {
1011 source_p->snomask = 0;
1012 showsnomask = YES;
1013 }
1014 source_p->flags2 &= ~OPER_FLAGS;
1015
90a3c35b 1016 rb_free(source_p->localClient->opername);
212380e3 1017 source_p->localClient->opername = NULL;
1018
af81d5a0 1019 rb_dlinkFindDestroy(source_p, &local_oper_list);
6acb39cb
JT
1020 privilegeset_unref(source_p->localClient->privset);
1021 source_p->localClient->privset = NULL;
212380e3 1022 }
1023
af81d5a0 1024 rb_dlinkFindDestroy(source_p, &oper_list);
212380e3 1025 }
1026 break;
1027
1028 /* we may not get these,
1029 * but they shouldnt be in default
1030 */
1031
1032 /* can only be set on burst */
1033 case 'S':
16cb94cc 1034 case 'Z':
212380e3 1035 case ' ':
1036 case '\n':
1037 case '\r':
1038 case '\t':
1039 break;
1040
1041 case 's':
1042 if (MyConnect(source_p))
1043 {
1044 if(!IsOper(source_p)
1045 && (ConfigFileEntry.oper_only_umodes & UMODE_SERVNOTICE))
1046 {
1047 if (what == MODE_ADD || source_p->umodes & UMODE_SERVNOTICE)
1048 badflag = YES;
1049 continue;
1050 }
1051 showsnomask = YES;
1052 if(what == MODE_ADD)
1053 {
1054 if (parc > 3)
1055 source_p->snomask = parse_snobuf_to_mask(source_p->snomask, parv[3]);
1056 else
1057 source_p->snomask |= SNO_GENERAL;
1058 }
1059 else
1060 source_p->snomask = 0;
1061 if (source_p->snomask != 0)
1062 source_p->umodes |= UMODE_SERVNOTICE;
1063 else
1064 source_p->umodes &= ~UMODE_SERVNOTICE;
1065 break;
1066 }
1067 /* FALLTHROUGH */
1068 default:
1069 if (MyConnect(source_p) && *pm == 'Q' && !ConfigChannel.use_forward) {
1070 badflag = YES;
1071 break;
1072 }
1073
1074 if((flag = user_modes[(unsigned char) *pm]))
1075 {
1076 if(MyConnect(source_p)
a8086a7c 1077 && ((!IsOper(source_p)
1078 && (ConfigFileEntry.oper_only_umodes & flag))
1079 || (orphaned_umodes & flag)))
212380e3 1080 {
1081 if (what == MODE_ADD || source_p->umodes & flag)
1082 badflag = YES;
1083 }
1084 else
1085 {
1086 if(what == MODE_ADD)
1087 source_p->umodes |= flag;
1088 else
1089 source_p->umodes &= ~flag;
1090 }
1091 }
1092 else
1093 {
1094 if(MyConnect(source_p))
1095 badflag = YES;
1096 }
1097 break;
1098 }
1099
1100 if(badflag)
1101 sendto_one(source_p, form_str(ERR_UMODEUNKNOWNFLAG), me.name, source_p->name);
1102
1103 if(MyClient(source_p) && (source_p->snomask & SNO_NCHANGE) && !IsOperN(source_p))
1104 {
d180319a 1105 sendto_one_notice(source_p, ":*** You need oper and nick_changes flag for +s +n");
212380e3 1106 source_p->snomask &= ~SNO_NCHANGE; /* only tcm's really need this */
1107 }
1108
1109 if(MyClient(source_p) && (source_p->umodes & UMODE_OPERWALL) && !IsOperOperwall(source_p))
1110 {
5366977b 1111 sendto_one_notice(source_p, ":*** You need oper and operwall flag for +z");
212380e3 1112 source_p->umodes &= ~UMODE_OPERWALL;
1113 }
1114
1115 if(MyConnect(source_p) && (source_p->umodes & UMODE_ADMIN) &&
1116 (!IsOperAdmin(source_p) || IsOperHiddenAdmin(source_p)))
1117 {
d180319a 1118 sendto_one_notice(source_p, ":*** You need oper and admin flag for +a");
212380e3 1119 source_p->umodes &= ~UMODE_ADMIN;
1120 }
1121
1122 /* let modules providing usermodes know that we've changed our usermode --nenolod */
1123 hdata.client = source_p;
1124 hdata.oldumodes = setflags;
1125 hdata.oldsnomask = setsnomask;
1126 call_hook(h_umode_changed, &hdata);
1127
1128 if(!(setflags & UMODE_INVISIBLE) && IsInvisible(source_p))
1129 ++Count.invisi;
1130 if((setflags & UMODE_INVISIBLE) && !IsInvisible(source_p))
1131 --Count.invisi;
1132 /*
1133 * compare new flags with old flags and send string which
1134 * will cause servers to update correctly.
1135 */
1136 send_umode_out(client_p, source_p, setflags);
1137 if (showsnomask && MyConnect(source_p))
88520303 1138 sendto_one_numeric(source_p, RPL_SNOMASK, form_str(RPL_SNOMASK),
212380e3 1139 construct_snobuf(source_p->snomask));
1140
1141 return (0);
1142}
1143
1144/*
1145 * send the MODE string for user (user) to connection client_p
1146 * -avalon
1147 */
1148void
1149send_umode(struct Client *client_p, struct Client *source_p, int old, int sendmask, char *umode_buf)
1150{
1151 int i;
1152 int flag;
1153 char *m;
1154 int what = 0;
1155
1156 /*
1157 * build a string in umode_buf to represent the change in the user's
1158 * mode between the new (source_p->flag) and 'old'.
1159 */
1160 m = umode_buf;
1161 *m = '\0';
1162
1163 for (i = 0; i < 128; i++)
1164 {
1165 flag = user_modes[i];
1166
1167 if((flag & old) && !(source_p->umodes & flag))
1168 {
1169 if(what == MODE_DEL)
1170 *m++ = (char) i;
1171 else
1172 {
1173 what = MODE_DEL;
1174 *m++ = '-';
1175 *m++ = (char) i;
1176 }
1177 }
1178 else if(!(flag & old) && (source_p->umodes & flag))
1179 {
1180 if(what == MODE_ADD)
1181 *m++ = (char) i;
1182 else
1183 {
1184 what = MODE_ADD;
1185 *m++ = '+';
1186 *m++ = (char) i;
1187 }
1188 }
1189 }
1190 *m = '\0';
1191
1192 if(*umode_buf && client_p)
1193 sendto_one(client_p, ":%s MODE %s :%s", source_p->name, source_p->name, umode_buf);
1194}
1195
1196/*
1197 * send_umode_out
1198 *
1199 * inputs -
1200 * output - NONE
1201 * side effects -
1202 */
1203void
1204send_umode_out(struct Client *client_p, struct Client *source_p, int old)
1205{
1206 struct Client *target_p;
1207 char buf[BUFSIZE];
af81d5a0 1208 rb_dlink_node *ptr;
212380e3 1209
1210 send_umode(NULL, source_p, old, 0, buf);
1211
8e69bb4e 1212 RB_DLINK_FOREACH(ptr, serv_list.head)
212380e3 1213 {
1214 target_p = ptr->data;
1215
1216 if((target_p != client_p) && (target_p != source_p) && (*buf))
1217 {
1218 sendto_one(target_p, ":%s MODE %s :%s",
1219 get_id(source_p, target_p),
1220 get_id(source_p, target_p), buf);
1221 }
1222 }
1223
1224 if(client_p && MyClient(client_p))
1225 send_umode(client_p, source_p, old, 0, buf);
1226}
1227
1228/*
1229 * user_welcome
1230 *
1231 * inputs - client pointer to client to welcome
1232 * output - NONE
1233 * side effects -
1234 */
1235void
1236user_welcome(struct Client *source_p)
1237{
88520303 1238 sendto_one_numeric(source_p, RPL_WELCOME, form_str(RPL_WELCOME), ServerInfo.network_name, source_p->name);
1239 sendto_one_numeric(source_p, RPL_YOURHOST, form_str(RPL_YOURHOST),
212380e3 1240 get_listener_name(source_p->localClient->listener), ircd_version);
88520303 1241 sendto_one_numeric(source_p, RPL_CREATED, form_str(RPL_CREATED), creation);
2392eb24 1242 sendto_one_numeric(source_p, RPL_MYINFO, form_str(RPL_MYINFO), me.name, ircd_version, umodebuf, cflagsmyinfo);
212380e3 1243
1244 show_isupport(source_p);
1245
1246 show_lusers(source_p);
1247
1248 if(ConfigFileEntry.short_motd)
1249 {
5366977b 1250 sendto_one_notice(source_p, ":*** Notice -- motd was last changed at %s", user_motd_changed);
1251 sendto_one_notice(source_p, ":*** Notice -- Please read the motd if you haven't read it");
212380e3 1252
1253 sendto_one(source_p, form_str(RPL_MOTDSTART),
1254 me.name, source_p->name, me.name);
1255
1256 sendto_one(source_p, form_str(RPL_MOTD),
1257 me.name, source_p->name, "*** This is the short motd ***");
1258
1259 sendto_one(source_p, form_str(RPL_ENDOFMOTD), me.name, source_p->name);
1260 }
1261 else
1262 send_user_motd(source_p);
1263}
1264
1265/* oper_up()
1266 *
1267 * inputs - pointer to given client to oper
1268 * - pointer to ConfItem to use
1269 * output - none
1270 * side effects - opers up source_p using aconf for reference
1271 */
1272int
1273oper_up(struct Client *source_p, struct oper_conf *oper_p)
1274{
1275 unsigned int old = source_p->umodes, oldsnomask = source_p->snomask;
1276 hook_data_umode_changed hdata;
1277
1278 SetOper(source_p);
1279
1280 if(oper_p->umodes)
1281 source_p->umodes |= oper_p->umodes;
1282 else if(ConfigFileEntry.oper_umodes)
1283 source_p->umodes |= ConfigFileEntry.oper_umodes;
1284 else
1285 source_p->umodes |= DEFAULT_OPER_UMODES;
1286
1287 if (oper_p->snomask)
1288 {
1289 source_p->snomask |= oper_p->snomask;
1290 source_p->umodes |= UMODE_SERVNOTICE;
1291 }
1292 else if (source_p->umodes & UMODE_SERVNOTICE)
1293 {
1294 /* Only apply these if +s is already set -- jilles */
1295 if (ConfigFileEntry.oper_snomask)
1296 source_p->snomask |= ConfigFileEntry.oper_snomask;
1297 else
1298 source_p->snomask |= DEFAULT_OPER_SNOMASK;
1299 }
1300
1301 Count.oper++;
1302
1303 SetExemptKline(source_p);
1304
1305 source_p->flags2 |= oper_p->flags;
62d28946 1306 source_p->localClient->opername = rb_strdup(oper_p->name);
34cb09ce 1307 source_p->localClient->privset = privilegeset_ref(oper_p->privset);
212380e3 1308
af81d5a0
WP
1309 rb_dlinkAddAlloc(source_p, &local_oper_list);
1310 rb_dlinkAddAlloc(source_p, &oper_list);
212380e3 1311
1312 if(IsOperAdmin(source_p) && !IsOperHiddenAdmin(source_p))
1313 source_p->umodes |= UMODE_ADMIN;
1314 if(!IsOperN(source_p))
1315 source_p->snomask &= ~SNO_NCHANGE;
1316 if(!IsOperOperwall(source_p))
1317 source_p->umodes &= ~UMODE_OPERWALL;
1318 hdata.client = source_p;
1319 hdata.oldumodes = old;
1320 hdata.oldsnomask = oldsnomask;
1321 call_hook(h_umode_changed, &hdata);
1322
1323 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1324 "%s (%s@%s) is now an operator", source_p->name,
1325 source_p->username, source_p->host);
652b8478 1326 if(!(old & UMODE_INVISIBLE) && IsInvisible(source_p))
1327 ++Count.invisi;
1328 if((old & UMODE_INVISIBLE) && !IsInvisible(source_p))
1329 --Count.invisi;
212380e3 1330 send_umode_out(source_p, source_p, old);
88520303 1331 sendto_one_numeric(source_p, RPL_SNOMASK, form_str(RPL_SNOMASK),
212380e3 1332 construct_snobuf(source_p->snomask));
1333 sendto_one(source_p, form_str(RPL_YOUREOPER), me.name, source_p->name);
b8be4a3f
WP
1334 sendto_one_notice(source_p, ":*** Oper privilege set is %s", oper_p->privset->name);
1335 sendto_one_notice(source_p, ":*** Oper privs are %s", oper_p->privset->privs);
212380e3 1336 send_oper_motd(source_p);
1337
1338 return (1);
1339}
1340
01cebbd8 1341/*
1342 * find_umode_slot
1343 *
1344 * inputs - NONE
1345 * outputs - an available umode bitmask or
1346 * 0 if no umodes are available
1347 * side effects - NONE
1348 */
1349unsigned int
1350find_umode_slot(void)
1351{
1352 unsigned int all_umodes = 0, my_umode = 0, i;
1353
1354 for (i = 0; i < 128; i++)
1355 all_umodes |= user_modes[i];
1356
1357 for (my_umode = 1; my_umode && (all_umodes & my_umode);
1358 my_umode <<= 1);
1359
1360 return my_umode;
1361}
1362
212380e3 1363void
1364construct_umodebuf(void)
1365{
1366 int i;
1367 char *ptr = umodebuf;
a8086a7c 1368 static int prev_user_modes[128];
212380e3 1369
1370 *ptr = '\0';
1371
1372 for (i = 0; i < 128; i++)
a8086a7c 1373 {
1374 if (prev_user_modes[i] != 0 && prev_user_modes[i] != user_modes[i])
1375 {
1376 if (user_modes[i] == 0)
1377 {
1378 orphaned_umodes |= prev_user_modes[i];
1379 sendto_realops_snomask(SNO_DEBUG, L_ALL, "Umode +%c is now orphaned", i);
1380 }
1381 else
1382 {
1383 orphaned_umodes &= ~prev_user_modes[i];
1384 sendto_realops_snomask(SNO_DEBUG, L_ALL, "Orphaned umode +%c is picked up by module", i);
1385 }
1386 user_modes[i] = prev_user_modes[i];
1387 }
1388 else
1389 prev_user_modes[i] = user_modes[i];
212380e3 1390 if (user_modes[i])
1391 *ptr++ = (char) i;
a8086a7c 1392 }
212380e3 1393
1394 *ptr++ = '\0';
1395}
1396
1397void
1398change_nick_user_host(struct Client *target_p, const char *nick, const char *user,
20961eea 1399 const char *host, int newts, const char *format, ...)
212380e3 1400{
af81d5a0 1401 rb_dlink_node *ptr;
212380e3 1402 struct Channel *chptr;
1403 struct membership *mscptr;
1404 int changed = irccmp(target_p->name, nick);
1405 int changed_case = strcmp(target_p->name, nick);
1406 int do_qjm = irccmp(target_p->username, user) || irccmp(target_p->host, host);
1407 char mode[10], modeval[NICKLEN * 2 + 2], reason[256], *mptr;
1408 va_list ap;
1409
1410 modeval[0] = '\0';
1411
1412 if(changed)
1413 {
1414 target_p->tsinfo = newts;
1415 monitor_signoff(target_p);
1416 }
1417 invalidate_bancache_user(target_p);
1418
1419 if(do_qjm)
1420 {
1421 va_start(ap, format);
1422 vsnprintf(reason, 255, format, ap);
1423 va_end(ap);
1424
1425 sendto_common_channels_local_butone(target_p, ":%s!%s@%s QUIT :%s",
1426 target_p->name, target_p->username, target_p->host,
1427 reason);
1428
8e69bb4e 1429 RB_DLINK_FOREACH(ptr, target_p->user->channel.head)
212380e3 1430 {
1431 mscptr = ptr->data;
1432 chptr = mscptr->chptr;
1433 mptr = mode;
1434
1435 if(is_chanop(mscptr))
1436 {
1437 *mptr++ = 'o';
1438 strcat(modeval, nick);
1439 strcat(modeval, " ");
1440 }
1441
1442 if(is_voiced(mscptr))
1443 {
1444 *mptr++ = 'v';
1445 strcat(modeval, nick);
1446 }
1447
1448 *mptr = '\0';
1449
1450 sendto_channel_local_butone(target_p, ALL_MEMBERS, chptr, ":%s!%s@%s JOIN :%s",
1451 nick, user, host, chptr->chname);
1452 if(*mode)
1453 sendto_channel_local_butone(target_p, ALL_MEMBERS, chptr,
1454 ":%s MODE %s +%s %s",
1455 target_p->servptr->name,
1456 chptr->chname, mode, modeval);
1457
1458 *modeval = '\0';
1459 }
1460
1461 if(MyClient(target_p) && changed_case)
1462 sendto_one(target_p, ":%s!%s@%s NICK %s",
1463 target_p->name, target_p->username, target_p->host, nick);
1464 }
1465 else if(changed_case)
1466 {
1467 sendto_common_channels_local(target_p, ":%s!%s@%s NICK :%s",
1468 target_p->name, target_p->username,
1469 target_p->host, nick);
1470 }
1471
907468c4
VY
1472 rb_strlcpy(target_p->username, user, sizeof target_p->username);
1473 rb_strlcpy(target_p->host, host, sizeof target_p->host);
212380e3 1474
1475 if (changed)
1476 add_history(target_p, 1);
1477
1478 del_from_client_hash(target_p->name, target_p);
907468c4 1479 rb_strlcpy(target_p->name, nick, NICKLEN);
212380e3 1480 add_to_client_hash(target_p->name, target_p);
1481
1482 if(changed)
1483 {
1484 monitor_signon(target_p);
1485 del_all_accepts(target_p);
1486 }
1487}