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