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