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