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