1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2017 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file user.c |
23 |
* \brief User related functions. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "user.h" |
30 |
#include "channel.h" |
31 |
#include "channel_mode.h" |
32 |
#include "client.h" |
33 |
#include "hash.h" |
34 |
#include "id.h" |
35 |
#include "irc_string.h" |
36 |
#include "ircd.h" |
37 |
#include "listener.h" |
38 |
#include "motd.h" |
39 |
#include "numeric.h" |
40 |
#include "conf.h" |
41 |
#include "conf_gecos.h" |
42 |
#include "log.h" |
43 |
#include "server.h" |
44 |
#include "send.h" |
45 |
#include "memory.h" |
46 |
#include "packet.h" |
47 |
#include "rng_mt.h" |
48 |
#include "userhost.h" |
49 |
#include "misc.h" |
50 |
#include "parse.h" |
51 |
#include "watch.h" |
52 |
#include "isupport.h" |
53 |
#include "tls.h" |
54 |
|
55 |
static char umode_buffer[IRCD_BUFSIZE]; |
56 |
|
57 |
const struct user_modes *umode_map[256]; |
58 |
const struct user_modes umode_tab[] = |
59 |
{ |
60 |
{ 'D', UMODE_DEAF }, |
61 |
{ 'F', UMODE_FARCONNECT }, |
62 |
{ 'G', UMODE_SOFTCALLERID }, |
63 |
{ 'H', UMODE_HIDDEN }, |
64 |
{ 'R', UMODE_REGONLY }, |
65 |
{ 'S', UMODE_SSL }, |
66 |
{ 'W', UMODE_WEBIRC }, |
67 |
{ 'a', UMODE_ADMIN }, |
68 |
{ 'b', UMODE_BOTS }, |
69 |
{ 'c', UMODE_CCONN }, |
70 |
{ 'd', UMODE_DEBUG }, |
71 |
{ 'e', UMODE_EXTERNAL }, |
72 |
{ 'f', UMODE_FULL }, |
73 |
{ 'g', UMODE_CALLERID }, |
74 |
{ 'i', UMODE_INVISIBLE }, |
75 |
{ 'j', UMODE_REJ }, |
76 |
{ 'k', UMODE_SKILL }, |
77 |
{ 'l', UMODE_LOCOPS }, |
78 |
{ 'n', UMODE_NCHANGE }, |
79 |
{ 'o', UMODE_OPER }, |
80 |
{ 'p', UMODE_HIDECHANS }, |
81 |
{ 'q', UMODE_HIDEIDLE }, |
82 |
{ 'r', UMODE_REGISTERED }, |
83 |
{ 's', UMODE_SERVNOTICE }, |
84 |
{ 'u', UMODE_UNAUTH }, |
85 |
{ 'w', UMODE_WALLOP }, |
86 |
{ 'x', UMODE_HIDDENHOST }, |
87 |
{ 'y', UMODE_SPY }, |
88 |
{ '\0', 0 } |
89 |
}; |
90 |
|
91 |
void |
92 |
user_modes_init(void) |
93 |
{ |
94 |
char *umode_buffer_ptr = umode_buffer; |
95 |
|
96 |
for (const struct user_modes *tab = umode_tab; tab->c; ++tab) |
97 |
{ |
98 |
umode_map[tab->c] = tab; |
99 |
*umode_buffer_ptr++ = tab->c; |
100 |
} |
101 |
|
102 |
*umode_buffer_ptr = '\0'; |
103 |
} |
104 |
|
105 |
/* show_lusers() |
106 |
* |
107 |
* inputs - pointer to client |
108 |
* output - NONE |
109 |
* side effects - display to client user counts etc. |
110 |
*/ |
111 |
void |
112 |
show_lusers(struct Client *client_p) |
113 |
{ |
114 |
if (!ConfigServerHide.hide_servers || HasUMode(client_p, UMODE_OPER)) |
115 |
sendto_one_numeric(client_p, &me, RPL_LUSERCLIENT, (dlink_list_length(&global_client_list) - Count.invisi), |
116 |
Count.invisi, dlink_list_length(&global_server_list)); |
117 |
else |
118 |
sendto_one_numeric(client_p, &me, RPL_LUSERCLIENT, (dlink_list_length(&global_client_list) - Count.invisi), |
119 |
Count.invisi, 1); |
120 |
|
121 |
if (Count.oper) |
122 |
sendto_one_numeric(client_p, &me, RPL_LUSEROP, Count.oper); |
123 |
|
124 |
if (dlink_list_length(&unknown_list)) |
125 |
sendto_one_numeric(client_p, &me, RPL_LUSERUNKNOWN, dlink_list_length(&unknown_list)); |
126 |
|
127 |
if (dlink_list_length(&channel_list)) |
128 |
sendto_one_numeric(client_p, &me, RPL_LUSERCHANNELS, dlink_list_length(&channel_list)); |
129 |
|
130 |
if (!ConfigServerHide.hide_servers || HasUMode(client_p, UMODE_OPER)) |
131 |
{ |
132 |
sendto_one_numeric(client_p, &me, RPL_LUSERME, dlink_list_length(&local_client_list), dlink_list_length(&local_server_list)); |
133 |
sendto_one_numeric(client_p, &me, RPL_LOCALUSERS, dlink_list_length(&local_client_list), Count.max_loc); |
134 |
} |
135 |
else |
136 |
{ |
137 |
sendto_one_numeric(client_p, &me, RPL_LUSERME, dlink_list_length(&global_client_list), 0); |
138 |
sendto_one_numeric(client_p, &me, RPL_LOCALUSERS, dlink_list_length(&global_client_list), Count.max_tot); |
139 |
} |
140 |
|
141 |
sendto_one_numeric(client_p, &me, RPL_GLOBALUSERS, dlink_list_length(&global_client_list), Count.max_tot); |
142 |
|
143 |
if (!ConfigServerHide.hide_servers || HasUMode(client_p, UMODE_OPER)) |
144 |
sendto_one_numeric(client_p, &me, RPL_STATSCONN, Count.max_loc_con, |
145 |
Count.max_loc, Count.totalrestartcount); |
146 |
} |
147 |
|
148 |
/* report_and_set_user_flags() |
149 |
* |
150 |
* inputs - pointer to client_p |
151 |
* - pointer to conf for this user |
152 |
* output - NONE |
153 |
* side effects - Report to user any special flags |
154 |
* they are getting, and set them. |
155 |
*/ |
156 |
static void |
157 |
report_and_set_user_flags(struct Client *client_p, const struct MaskItem *conf) |
158 |
{ |
159 |
/* If this user is being spoofed, tell them so */ |
160 |
if (IsConfDoSpoofIp(conf)) |
161 |
sendto_one_notice(client_p, &me, ":*** Spoofing your IP"); |
162 |
|
163 |
/* If this user is in the exception class, set it "E lined" */ |
164 |
if (IsConfExemptKline(conf)) |
165 |
{ |
166 |
AddFlag(client_p, FLAGS_EXEMPTKLINE); |
167 |
sendto_one_notice(client_p, &me, ":*** You are exempt from K/D lines"); |
168 |
} |
169 |
|
170 |
if (IsConfExemptXline(conf)) |
171 |
{ |
172 |
AddFlag(client_p, FLAGS_EXEMPTXLINE); |
173 |
sendto_one_notice(client_p, &me, ":*** You are exempt from X lines"); |
174 |
} |
175 |
|
176 |
if (IsConfExemptResv(conf)) |
177 |
{ |
178 |
AddFlag(client_p, FLAGS_EXEMPTRESV); |
179 |
sendto_one_notice(client_p, &me, ":*** You are exempt from resvs"); |
180 |
} |
181 |
|
182 |
/* If this user is exempt from user limits set it "F lined" */ |
183 |
if (IsConfExemptLimits(conf)) |
184 |
{ |
185 |
AddFlag(client_p, FLAGS_NOLIMIT); |
186 |
sendto_one_notice(client_p, &me, ":*** You are exempt from user limits"); |
187 |
} |
188 |
|
189 |
if (IsConfCanFlood(conf)) |
190 |
{ |
191 |
AddFlag(client_p, FLAGS_CANFLOOD); |
192 |
sendto_one_notice(client_p, &me, ":*** You are exempt from flood protection"); |
193 |
} |
194 |
} |
195 |
|
196 |
/* introduce_client() |
197 |
* |
198 |
* inputs - client_p |
199 |
* output - NONE |
200 |
* side effects - This common function introduces a client to the rest |
201 |
* of the net, either from a local client connect or |
202 |
* from a remote connect. |
203 |
*/ |
204 |
static void |
205 |
introduce_client(struct Client *client_p) |
206 |
{ |
207 |
dlink_node *node = NULL; |
208 |
char ubuf[IRCD_BUFSIZE] = ""; |
209 |
|
210 |
send_umode(client_p, MyConnect(client_p), 0, ubuf); |
211 |
watch_check_hash(client_p, RPL_LOGON); |
212 |
|
213 |
if (ubuf[0] == '\0') |
214 |
{ |
215 |
ubuf[0] = '+'; |
216 |
ubuf[1] = '\0'; |
217 |
} |
218 |
|
219 |
DLINK_FOREACH(node, local_server_list.head) |
220 |
{ |
221 |
struct Client *server_p = node->data; |
222 |
|
223 |
if (server_p == client_p->from) |
224 |
continue; |
225 |
|
226 |
sendto_one(server_p, ":%s UID %s %u %ju %s %s %s %s %s %s :%s", |
227 |
client_p->servptr->id, |
228 |
client_p->name, client_p->hopcount+1, |
229 |
client_p->tsinfo, |
230 |
ubuf, client_p->username, client_p->host, |
231 |
client_p->sockhost, client_p->id, |
232 |
client_p->account, |
233 |
client_p->info); |
234 |
|
235 |
if (!EmptyString(client_p->certfp)) |
236 |
sendto_one(server_p, ":%s CERTFP %s", client_p->id, client_p->certfp); |
237 |
} |
238 |
} |
239 |
|
240 |
/* user_welcome() |
241 |
* |
242 |
* inputs - client pointer to client to welcome |
243 |
* output - NONE |
244 |
* side effects - |
245 |
*/ |
246 |
static void |
247 |
user_welcome(struct Client *client_p) |
248 |
{ |
249 |
static const char built_date[] = __DATE__ " at " __TIME__; |
250 |
|
251 |
if (HasFlag(client_p, FLAGS_SSL)) |
252 |
{ |
253 |
AddUMode(client_p, UMODE_SSL); |
254 |
sendto_one_notice(client_p, &me, ":*** Connected securely via %s", |
255 |
tls_get_cipher(&client_p->connection->fd.ssl)); |
256 |
} |
257 |
|
258 |
sendto_one_numeric(client_p, &me, RPL_WELCOME, ConfigServerInfo.network_name, |
259 |
client_p->name); |
260 |
sendto_one_numeric(client_p, &me, RPL_YOURHOST, |
261 |
listener_get_name(client_p->connection->listener), ircd_version); |
262 |
sendto_one_numeric(client_p, &me, RPL_CREATED, built_date); |
263 |
sendto_one_numeric(client_p, &me, RPL_MYINFO, me.name, ircd_version, umode_buffer); |
264 |
|
265 |
isupport_show(client_p); |
266 |
show_lusers(client_p); |
267 |
motd_signon(client_p); |
268 |
} |
269 |
|
270 |
/* check_xline() |
271 |
* |
272 |
* inputs - pointer to client to test |
273 |
* outupt - 1 if exiting 0 if ok |
274 |
* side effects - |
275 |
*/ |
276 |
static int |
277 |
check_xline(struct Client *client_p) |
278 |
{ |
279 |
const struct GecosItem *gecos = NULL; |
280 |
|
281 |
if (HasFlag(client_p, FLAGS_EXEMPTXLINE)) |
282 |
return 0; |
283 |
|
284 |
if ((gecos = gecos_find(client_p->info, match))) |
285 |
{ |
286 |
sendto_realops_flags(UMODE_REJ, L_ALL, SEND_NOTICE, |
287 |
"X-line Rejecting [%s] [%s], user %s [%s]", |
288 |
client_p->info, gecos->reason, |
289 |
client_get_name(client_p, HIDE_IP), |
290 |
client_p->sockhost); |
291 |
|
292 |
++ServerStats.is_ref; |
293 |
exit_client(client_p, "Bad user info"); |
294 |
return 1; |
295 |
} |
296 |
|
297 |
return 0; |
298 |
} |
299 |
|
300 |
/*! \brief This function is called when both NICK and USER messages |
301 |
* have been accepted for the client, in whatever order. Only |
302 |
* after this, is the UID message propagated. |
303 |
* \param client_p Pointer to given client to introduce |
304 |
*/ |
305 |
void |
306 |
register_local_user(struct Client *client_p) |
307 |
{ |
308 |
const struct MaskItem *conf = NULL; |
309 |
|
310 |
assert(client_p == client_p->from); |
311 |
assert(MyConnect(client_p)); |
312 |
assert(IsUnknown(client_p)); |
313 |
assert(!client_p->connection->registration); |
314 |
|
315 |
if (ConfigGeneral.ping_cookie) |
316 |
{ |
317 |
if (!HasFlag(client_p, FLAGS_PINGSENT) && !client_p->connection->random_ping) |
318 |
{ |
319 |
do |
320 |
client_p->connection->random_ping = genrand_int32(); |
321 |
while (!client_p->connection->random_ping); |
322 |
|
323 |
sendto_one(client_p, "PING :%u", client_p->connection->random_ping); |
324 |
AddFlag(client_p, FLAGS_PINGSENT); |
325 |
return; |
326 |
} |
327 |
|
328 |
if (!HasFlag(client_p, FLAGS_PING_COOKIE)) |
329 |
return; |
330 |
} |
331 |
|
332 |
client_p->connection->last_privmsg = CurrentTime; |
333 |
/* Straight up the maximum rate of flooding... */ |
334 |
client_p->connection->allow_read = MAX_FLOOD_BURST; |
335 |
|
336 |
if (!check_client(client_p)) |
337 |
return; |
338 |
|
339 |
if (!valid_hostname(client_p->host)) |
340 |
{ |
341 |
sendto_one_notice(client_p, &me, ":*** Notice -- You have an illegal " |
342 |
"character in your hostname"); |
343 |
strlcpy(client_p->host, client_p->sockhost, sizeof(client_p->host)); |
344 |
} |
345 |
|
346 |
conf = client_p->connection->confs.head->data; |
347 |
|
348 |
if (!HasFlag(client_p, FLAGS_GOTID)) |
349 |
{ |
350 |
char username[USERLEN + 1] = ""; |
351 |
unsigned int i = 0; |
352 |
|
353 |
if (IsNeedIdentd(conf)) |
354 |
{ |
355 |
++ServerStats.is_ref; |
356 |
sendto_one_notice(client_p, &me, ":*** Notice -- You need to install " |
357 |
"identd to use this server"); |
358 |
exit_client(client_p, "Install identd"); |
359 |
return; |
360 |
} |
361 |
|
362 |
strlcpy(username, client_p->username, sizeof(username)); |
363 |
|
364 |
if (!IsNoTilde(conf)) |
365 |
client_p->username[i++] = '~'; |
366 |
|
367 |
for (const char *p = username; *p && i < USERLEN; ++p) |
368 |
client_p->username[i++] = *p; |
369 |
|
370 |
client_p->username[i] = '\0'; |
371 |
} |
372 |
|
373 |
/* Password check */ |
374 |
if (!EmptyString(conf->passwd)) |
375 |
{ |
376 |
if (!match_conf_password(client_p->connection->password, conf)) |
377 |
{ |
378 |
++ServerStats.is_ref; |
379 |
|
380 |
sendto_one_numeric(client_p, &me, ERR_PASSWDMISMATCH); |
381 |
exit_client(client_p, "Bad Password"); |
382 |
return; |
383 |
} |
384 |
} |
385 |
|
386 |
/* |
387 |
* Don't free client_p->connection->password here - it can be required |
388 |
* by masked /stats I if there are auth {} blocks with need_password = no; |
389 |
* --adx |
390 |
*/ |
391 |
|
392 |
/* |
393 |
* Report if user has &^>= etc. and set flags as needed in client_p |
394 |
*/ |
395 |
report_and_set_user_flags(client_p, conf); |
396 |
|
397 |
if (IsDead(client_p)) |
398 |
return; |
399 |
|
400 |
/* |
401 |
* Limit clients - |
402 |
* We want to be able to have servers and F-line clients |
403 |
* connect, so save room for "buffer" connections. |
404 |
* Smaller servers may want to decrease this, and it should |
405 |
* probably be just a percentage of the MAXCLIENTS... |
406 |
* -Taner |
407 |
*/ |
408 |
if ((dlink_list_length(&local_client_list) >= GlobalSetOptions.maxclients + MAX_BUFFER) || |
409 |
(dlink_list_length(&local_client_list) >= GlobalSetOptions.maxclients && !HasFlag(client_p, FLAGS_NOLIMIT))) |
410 |
{ |
411 |
sendto_realops_flags(UMODE_FULL, L_ALL, SEND_NOTICE, |
412 |
"Too many clients, rejecting %s[%s].", |
413 |
client_p->name, client_p->host); |
414 |
++ServerStats.is_ref; |
415 |
exit_client(client_p, "Sorry, server is full - try later"); |
416 |
return; |
417 |
} |
418 |
|
419 |
if (!valid_username(client_p->username, 1)) |
420 |
{ |
421 |
char buf[IRCD_BUFSIZE] = ""; |
422 |
|
423 |
sendto_realops_flags(UMODE_REJ, L_ALL, SEND_NOTICE, |
424 |
"Invalid username: %s (%s@%s)", |
425 |
client_p->name, client_p->username, client_p->host); |
426 |
++ServerStats.is_ref; |
427 |
snprintf(buf, sizeof(buf), "Invalid username [%s]", client_p->username); |
428 |
exit_client(client_p, buf); |
429 |
return; |
430 |
} |
431 |
|
432 |
if (check_xline(client_p)) |
433 |
return; |
434 |
|
435 |
const char *id; |
436 |
while (hash_find_id((id = uid_get()))) |
437 |
; |
438 |
|
439 |
strlcpy(client_p->id, id, sizeof(client_p->id)); |
440 |
hash_add_id(client_p); |
441 |
|
442 |
sendto_realops_flags(UMODE_CCONN, L_ALL, SEND_NOTICE, |
443 |
"Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s>", |
444 |
client_p->name, client_p->username, client_p->host, |
445 |
client_p->sockhost, |
446 |
get_client_class(&client_p->connection->confs), |
447 |
client_p->info, client_p->id); |
448 |
|
449 |
if (ConfigGeneral.invisible_on_connect) |
450 |
{ |
451 |
AddUMode(client_p, UMODE_INVISIBLE); |
452 |
++Count.invisi; |
453 |
} |
454 |
|
455 |
assert(client_p->servptr == &me); |
456 |
SetClient(client_p); |
457 |
dlinkAdd(client_p, &client_p->lnode, &client_p->servptr->serv->client_list); |
458 |
dlinkAdd(client_p, &client_p->node, &global_client_list); |
459 |
|
460 |
assert(dlinkFind(&unknown_list, client_p)); |
461 |
|
462 |
dlink_move_node(&client_p->connection->lclient_node, |
463 |
&unknown_list, &local_client_list); |
464 |
|
465 |
if (dlink_list_length(&local_client_list) > Count.max_loc) |
466 |
{ |
467 |
Count.max_loc = dlink_list_length(&local_client_list); |
468 |
|
469 |
if (!(Count.max_loc % 10)) |
470 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
471 |
"New maximum local client connections: %u", |
472 |
Count.max_loc); |
473 |
} |
474 |
|
475 |
if ((dlink_list_length(&local_client_list) + |
476 |
dlink_list_length(&local_server_list)) > Count.max_loc_con) |
477 |
Count.max_loc_con = dlink_list_length(&local_client_list) + |
478 |
dlink_list_length(&local_server_list); |
479 |
|
480 |
if (dlink_list_length(&global_client_list) > Count.max_tot) |
481 |
Count.max_tot = dlink_list_length(&global_client_list); |
482 |
++Count.totalrestartcount; |
483 |
|
484 |
user_welcome(client_p); |
485 |
userhost_add(client_p->sockhost, 0); |
486 |
AddFlag(client_p, FLAGS_USERHOST); |
487 |
|
488 |
introduce_client(client_p); |
489 |
} |
490 |
|
491 |
/* register_remote_user() |
492 |
* |
493 |
* inputs - client_p remote or directly connected client |
494 |
* - username to register as |
495 |
* - host name to register as |
496 |
* - server name |
497 |
* output - NONE |
498 |
* side effects - This function is called when a remote client |
499 |
* is introduced by a server. |
500 |
*/ |
501 |
void |
502 |
register_remote_user(struct Client *client_p) |
503 |
{ |
504 |
assert(client_p->servptr->from == client_p->from); |
505 |
|
506 |
/* |
507 |
* If the nick has been introduced by a services server, |
508 |
* make it a service as well. |
509 |
*/ |
510 |
if (HasFlag(client_p->servptr, FLAGS_SERVICE)) |
511 |
AddFlag(client_p, FLAGS_SERVICE); |
512 |
|
513 |
SetClient(client_p); |
514 |
dlinkAdd(client_p, &client_p->lnode, &client_p->servptr->serv->client_list); |
515 |
dlinkAdd(client_p, &client_p->node, &global_client_list); |
516 |
|
517 |
if (dlink_list_length(&global_client_list) > Count.max_tot) |
518 |
Count.max_tot = dlink_list_length(&global_client_list); |
519 |
|
520 |
userhost_add(client_p->sockhost, 1); |
521 |
AddFlag(client_p, FLAGS_USERHOST); |
522 |
|
523 |
if (HasFlag(client_p->servptr, FLAGS_EOB)) |
524 |
sendto_realops_flags(UMODE_FARCONNECT, L_ALL, SEND_NOTICE, |
525 |
"Client connecting at %s: %s (%s@%s) [%s] [%s] <%s>", |
526 |
client_p->servptr->name, |
527 |
client_p->name, client_p->username, client_p->host, |
528 |
client_p->sockhost, client_p->info, client_p->id); |
529 |
|
530 |
introduce_client(client_p); |
531 |
} |
532 |
|
533 |
/* valid_hostname() |
534 |
* |
535 |
* Inputs - pointer to hostname |
536 |
* Output - 1 if valid, 0 if not |
537 |
* Side effects - check hostname for validity |
538 |
* |
539 |
* NOTE: this doesn't allow a hostname to begin with a dot and |
540 |
* will not allow more dots than chars. |
541 |
*/ |
542 |
int |
543 |
valid_hostname(const char *hostname) |
544 |
{ |
545 |
const char *p = hostname; |
546 |
|
547 |
assert(p); |
548 |
|
549 |
if (EmptyString(p) || *p == '.' || *p == ':') |
550 |
return 0; |
551 |
|
552 |
for (; *p; ++p) |
553 |
if (!IsHostChar(*p)) |
554 |
return 0; |
555 |
|
556 |
return p - hostname <= HOSTLEN; |
557 |
} |
558 |
|
559 |
/* valid_username() |
560 |
* |
561 |
* Inputs - pointer to user |
562 |
* Output - 1 if valid, 0 if not |
563 |
* Side effects - check username for validity |
564 |
* |
565 |
* Absolutely always reject any '*' '!' '?' '@' in an user name |
566 |
* reject any odd control characters names. |
567 |
* Allow '.' in username to allow for "first.last" |
568 |
* style of username |
569 |
*/ |
570 |
int |
571 |
valid_username(const char *username, const int local) |
572 |
{ |
573 |
const char *p = username; |
574 |
|
575 |
assert(p); |
576 |
|
577 |
if (*p == '~') |
578 |
++p; |
579 |
|
580 |
/* |
581 |
* Reject usernames that don't start with an alphanum |
582 |
* i.e. reject jokers who have '-@somehost' or '.@somehost' |
583 |
* or "-hi-@somehost", "h-----@somehost" would still be accepted. |
584 |
*/ |
585 |
if (!IsAlNum(*p)) |
586 |
return 0; |
587 |
|
588 |
if (local) |
589 |
{ |
590 |
unsigned int dots = 0; |
591 |
|
592 |
while (*++p) |
593 |
{ |
594 |
if (*p == '.' && ConfigGeneral.dots_in_ident) |
595 |
{ |
596 |
if (++dots > ConfigGeneral.dots_in_ident) |
597 |
return 0; |
598 |
if (!IsUserChar(*(p + 1))) |
599 |
return 0; |
600 |
} |
601 |
else if (!IsUserChar(*p)) |
602 |
return 0; |
603 |
} |
604 |
} |
605 |
else |
606 |
{ |
607 |
while (*++p) |
608 |
if (!IsUserChar(*p)) |
609 |
return 0; |
610 |
} |
611 |
|
612 |
return p - username <= USERLEN; |
613 |
} |
614 |
|
615 |
/* clean_nick_name() |
616 |
* |
617 |
* input - nickname |
618 |
* - whether it's a local nick (1) or remote (0) |
619 |
* output - none |
620 |
* side effects - walks through the nickname, returning 0 if erroneous |
621 |
*/ |
622 |
int |
623 |
valid_nickname(const char *nickname, const int local) |
624 |
{ |
625 |
const char *p = nickname; |
626 |
|
627 |
assert(p); |
628 |
|
629 |
/* |
630 |
* Nicks can't start with a digit or - or be 0 length. |
631 |
*/ |
632 |
if (EmptyString(p) || *p == '-' || (IsDigit(*p) && local)) |
633 |
return 0; |
634 |
|
635 |
for (; *p; ++p) |
636 |
if (!IsNickChar(*p)) |
637 |
return 0; |
638 |
|
639 |
return p - nickname <= NICKLEN; |
640 |
} |
641 |
|
642 |
/*! \brief Builds a mode change string to buffer pointed by \a buf |
643 |
* \param client_p Pointer to client |
644 |
* \param dispatch Whether to send a MODE message to client_p |
645 |
* \param old Old user mode to compare against when building new mode buffer |
646 |
* \param buf Pointer to buffer to build string in |
647 |
*/ |
648 |
void |
649 |
send_umode(struct Client *client_p, unsigned int dispatch, |
650 |
unsigned int old, char *buf) |
651 |
{ |
652 |
char *m = buf; |
653 |
int what = 0; |
654 |
|
655 |
/* |
656 |
* Build a string in umode_buf to represent the change in the user's |
657 |
* mode between the new (client_p->umodes) and 'old'. |
658 |
*/ |
659 |
for (const struct user_modes *tab = umode_tab; tab->c; ++tab) |
660 |
{ |
661 |
if ((tab->flag & old) && !HasUMode(client_p, tab->flag)) |
662 |
{ |
663 |
if (what == MODE_DEL) |
664 |
*m++ = tab->c; |
665 |
else |
666 |
{ |
667 |
what = MODE_DEL; |
668 |
*m++ = '-'; |
669 |
*m++ = tab->c; |
670 |
} |
671 |
} |
672 |
else if (!(tab->flag & old) && HasUMode(client_p, tab->flag)) |
673 |
{ |
674 |
if (what == MODE_ADD) |
675 |
*m++ = tab->c; |
676 |
else |
677 |
{ |
678 |
what = MODE_ADD; |
679 |
*m++ = '+'; |
680 |
*m++ = tab->c; |
681 |
} |
682 |
} |
683 |
} |
684 |
|
685 |
*m = '\0'; |
686 |
|
687 |
if (dispatch && *buf) |
688 |
sendto_one(client_p, ":%s!%s@%s MODE %s :%s", |
689 |
client_p->name, client_p->username, |
690 |
client_p->host, client_p->name, buf); |
691 |
} |
692 |
|
693 |
/* send_umode_out() |
694 |
* |
695 |
* inputs - |
696 |
* output - NONE |
697 |
* side effects - Only send ubuf out to servers that know about this client |
698 |
*/ |
699 |
void |
700 |
send_umode_out(struct Client *client_p, unsigned int old) |
701 |
{ |
702 |
char buf[IRCD_BUFSIZE] = ""; |
703 |
|
704 |
send_umode(client_p, MyConnect(client_p), old, buf); |
705 |
|
706 |
if (buf[0]) |
707 |
sendto_server(client_p, 0, 0, ":%s MODE %s :%s", |
708 |
client_p->id, client_p->id, buf); |
709 |
} |
710 |
|
711 |
void |
712 |
user_set_hostmask(struct Client *client_p, const char *hostname, const int what) |
713 |
{ |
714 |
dlink_node *node = NULL; |
715 |
|
716 |
if (!strcmp(client_p->host, hostname)) |
717 |
return; |
718 |
|
719 |
switch (what) |
720 |
{ |
721 |
case MODE_ADD: |
722 |
AddUMode(client_p, UMODE_HIDDENHOST); |
723 |
break; |
724 |
case MODE_DEL: |
725 |
DelUMode(client_p, UMODE_HIDDENHOST); |
726 |
break; |
727 |
default: return; |
728 |
} |
729 |
|
730 |
if (ConfigGeneral.cycle_on_host_change) |
731 |
sendto_common_channels_local(client_p, 0, 0, CAP_CHGHOST, ":%s!%s@%s QUIT :Changing hostname", |
732 |
client_p->name, client_p->username, client_p->host); |
733 |
|
734 |
sendto_common_channels_local(client_p, 0, CAP_CHGHOST, 0, ":%s!%s@%s CHGHOST %s %s", |
735 |
client_p->name, client_p->username, |
736 |
client_p->host, client_p->username, hostname); |
737 |
|
738 |
strlcpy(client_p->host, hostname, sizeof(client_p->host)); |
739 |
|
740 |
if (MyConnect(client_p)) |
741 |
{ |
742 |
sendto_one_numeric(client_p, &me, RPL_VISIBLEHOST, client_p->host); |
743 |
clear_ban_cache_list(&client_p->channel); |
744 |
} |
745 |
|
746 |
if (!ConfigGeneral.cycle_on_host_change) |
747 |
return; |
748 |
|
749 |
DLINK_FOREACH(node, client_p->channel.head) |
750 |
{ |
751 |
char modebuf[CMEMBER_STATUS_FLAGS_LEN + 1]; |
752 |
char nickbuf[CMEMBER_STATUS_FLAGS_LEN * NICKLEN + CMEMBER_STATUS_FLAGS_LEN] = ""; |
753 |
char *p = modebuf; |
754 |
int len = 0; |
755 |
const struct Membership *member = node->data; |
756 |
|
757 |
if (has_member_flags(member, CHFL_CHANOP)) |
758 |
{ |
759 |
*p++ = 'o'; |
760 |
len += snprintf(nickbuf + len, sizeof(nickbuf) - len, len ? " %s" : "%s", client_p->name); |
761 |
} |
762 |
|
763 |
if (has_member_flags(member, CHFL_HALFOP)) |
764 |
{ |
765 |
*p++ = 'h'; |
766 |
len += snprintf(nickbuf + len, sizeof(nickbuf) - len, len ? " %s" : "%s", client_p->name); |
767 |
} |
768 |
|
769 |
if (has_member_flags(member, CHFL_VOICE)) |
770 |
{ |
771 |
*p++ = 'v'; |
772 |
len += snprintf(nickbuf + len, sizeof(nickbuf) - len, len ? " %s" : "%s", client_p->name); |
773 |
} |
774 |
|
775 |
*p = '\0'; |
776 |
|
777 |
sendto_channel_local(client_p, member->chptr, 0, CAP_EXTENDED_JOIN, CAP_CHGHOST, ":%s!%s@%s JOIN %s %s :%s", |
778 |
client_p->name, client_p->username, |
779 |
client_p->host, member->chptr->name, |
780 |
client_p->account, client_p->info); |
781 |
sendto_channel_local(client_p, member->chptr, 0, 0, CAP_EXTENDED_JOIN | CAP_CHGHOST, ":%s!%s@%s JOIN :%s", |
782 |
client_p->name, client_p->username, |
783 |
client_p->host, member->chptr->name); |
784 |
|
785 |
if (nickbuf[0]) |
786 |
sendto_channel_local(client_p, member->chptr, 0, 0, CAP_CHGHOST, ":%s MODE %s +%s %s", |
787 |
client_p->servptr->name, member->chptr->name, |
788 |
modebuf, nickbuf); |
789 |
} |
790 |
|
791 |
if (client_p->away[0]) |
792 |
sendto_common_channels_local(client_p, 0, CAP_AWAY_NOTIFY, CAP_CHGHOST, |
793 |
":%s!%s@%s AWAY :%s", |
794 |
client_p->name, client_p->username, |
795 |
client_p->host, client_p->away); |
796 |
} |