]>
Commit | Line | Data |
---|---|---|
189935b1 | 1 | /* |
2 | * IRC - Internet Relay Chat, ircd/s_user.c (formerly ircd/s_msg.c) | |
3 | * Copyright (C) 1990 Jarkko Oikarinen and | |
4 | * University of Oulu, Computing Center | |
5 | * | |
6 | * See file AUTHORS in IRC package for additional names of | |
7 | * the programmers. | |
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 1, or (at your option) | |
12 | * 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., 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | */ | |
23 | /** @file | |
24 | * @brief Miscellaneous user-related helper functions. | |
25 | * @version $Id: s_user.c,v 1.99 2005/08/17 01:57:03 entrope Exp $ | |
26 | */ | |
27 | #include "config.h" | |
28 | ||
29 | #include "s_user.h" | |
30 | #include "IPcheck.h" | |
31 | #include "channel.h" | |
32 | #include "class.h" | |
33 | #include "client.h" | |
34 | #include "hash.h" | |
35 | #include "ircd.h" | |
36 | #include "ircd_alloc.h" | |
37 | #include "ircd_auth.h" | |
38 | #include "ircd_chattr.h" | |
39 | #include "ircd_features.h" | |
40 | #include "ircd_log.h" | |
41 | #include "ircd_reply.h" | |
42 | #include "ircd_snprintf.h" | |
43 | #include "ircd_string.h" | |
44 | #include "list.h" | |
45 | #include "match.h" | |
46 | #include "motd.h" | |
47 | #include "msg.h" | |
48 | #include "msgq.h" | |
49 | #include "numeric.h" | |
50 | #include "numnicks.h" | |
51 | #include "parse.h" | |
52 | #include "querycmds.h" | |
53 | #include "random.h" | |
54 | #include "s_bsd.h" | |
55 | #include "s_conf.h" | |
56 | #include "s_debug.h" | |
57 | #include "s_misc.h" | |
58 | #include "s_serv.h" /* max_client_count */ | |
59 | #include "send.h" | |
60 | #include "struct.h" | |
61 | #include "supported.h" | |
62 | #include "sys.h" | |
63 | #include "userload.h" | |
64 | #include "version.h" | |
65 | #include "whowas.h" | |
66 | ||
67 | #include "handlers.h" /* m_motd and m_lusers */ | |
68 | ||
69 | /* #include <assert.h> -- Now using assert in ircd_log.h */ | |
70 | #include <fcntl.h> | |
71 | #include <stdio.h> | |
72 | #include <stdlib.h> | |
73 | #include <string.h> | |
74 | #include <sys/stat.h> | |
75 | ||
1d4df40c | 76 | static char *IsVhost(char *hostmask, int oper); |
77 | static char *IsVhostPass(char *hostmask); | |
78 | ||
189935b1 | 79 | /** Count of allocated User structures. */ |
80 | static int userCount = 0; | |
81 | ||
82 | /** Makes sure that \a cptr has a User information block. | |
83 | * If cli_user(cptr) != NULL, does nothing. | |
84 | * @param[in] cptr Client to attach User struct to. | |
85 | * @return User struct associated with \a cptr. | |
86 | */ | |
87 | struct User *make_user(struct Client *cptr) | |
88 | { | |
89 | assert(0 != cptr); | |
90 | ||
91 | if (!cli_user(cptr)) { | |
92 | cli_user(cptr) = (struct User*) MyMalloc(sizeof(struct User)); | |
93 | assert(0 != cli_user(cptr)); | |
94 | ||
95 | /* All variables are 0 by default */ | |
96 | memset(cli_user(cptr), 0, sizeof(struct User)); | |
97 | ++userCount; | |
98 | cli_user(cptr)->refcnt = 1; | |
99 | } | |
100 | return cli_user(cptr); | |
101 | } | |
102 | ||
103 | /** Dereference \a user. | |
104 | * User structures are reference-counted; if the refcount of \a user | |
105 | * becomes zero, free it. | |
106 | * @param[in] user User to dereference. | |
107 | */ | |
108 | void free_user(struct User* user) | |
109 | { | |
110 | assert(0 != user); | |
111 | assert(0 < user->refcnt); | |
112 | ||
113 | if (--user->refcnt == 0) { | |
114 | if (user->away) | |
115 | MyFree(user->away); | |
116 | /* | |
117 | * sanity check | |
118 | */ | |
119 | assert(0 == user->joined); | |
120 | assert(0 == user->invited); | |
121 | assert(0 == user->channel); | |
122 | ||
123 | MyFree(user); | |
124 | --userCount; | |
125 | } | |
126 | } | |
127 | ||
128 | /** Find number of User structs allocated and memory used by them. | |
129 | * @param[out] count_out Receives number of User structs allocated. | |
130 | * @param[out] bytes_out Receives number of bytes used by User structs. | |
131 | */ | |
132 | void user_count_memory(size_t* count_out, size_t* bytes_out) | |
133 | { | |
134 | assert(0 != count_out); | |
135 | assert(0 != bytes_out); | |
136 | *count_out = userCount; | |
137 | *bytes_out = userCount * sizeof(struct User); | |
138 | } | |
139 | ||
140 | ||
141 | /** Find the next client (starting at \a next) with a name that matches \a ch. | |
142 | * Normal usage loop is: | |
143 | * for (x = client; x = next_client(x,mask); x = x->next) | |
144 | * HandleMatchingClient; | |
145 | * | |
146 | * @param[in] next First client to check. | |
147 | * @param[in] ch Name mask to check against. | |
148 | * @return Next matching client found, or NULL if none. | |
149 | */ | |
150 | struct Client *next_client(struct Client *next, const char* ch) | |
151 | { | |
152 | struct Client *tmp = next; | |
153 | ||
154 | if (!tmp) | |
155 | return NULL; | |
156 | ||
157 | next = FindClient(ch); | |
158 | next = next ? next : tmp; | |
159 | if (cli_prev(tmp) == next) | |
160 | return NULL; | |
161 | if (next != tmp) | |
162 | return next; | |
163 | for (; next; next = cli_next(next)) | |
164 | if (!match(ch, cli_name(next))) | |
165 | break; | |
166 | return next; | |
167 | } | |
168 | ||
169 | /** Find the destination server for a command, and forward it if that is not us. | |
170 | * | |
171 | * \a server may be a nickname, server name, server mask (if \a from | |
172 | * is a local user) or server numnick (if \a is a server or remote | |
173 | * user). | |
174 | * | |
175 | * @param[in] from Client that sent the command to us. | |
176 | * @param[in] cmd Long-form command text. | |
177 | * @param[in] tok Token-form command text. | |
178 | * @param[in] one Client that originated the command (ignored). | |
179 | * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH. | |
180 | * @param[in] pattern Format string of arguments to command. | |
181 | * @param[in] server Index of target name or mask in \a parv. | |
182 | * @param[in] parc Number of valid elements in \a parv (must be less than 9). | |
183 | * @param[in] parv Array of arguments to command. | |
184 | * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS. | |
185 | */ | |
186 | int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok, | |
187 | struct Client *one, int MustBeOper, const char *pattern, | |
188 | int server, int parc, char *parv[]) | |
189 | { | |
190 | struct Client *acptr; | |
191 | char *to; | |
192 | ||
193 | /* Assume it's me, if no server or an unregistered client */ | |
194 | if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from)) | |
195 | return (HUNTED_ISME); | |
196 | ||
197 | if (MustBeOper && !IsPrivileged(from)) | |
198 | { | |
199 | send_reply(from, ERR_NOPRIVILEGES); | |
200 | return HUNTED_NOSUCH; | |
201 | } | |
202 | ||
203 | /* Make sure it's a server */ | |
204 | if (MyUser(from)) { | |
205 | /* Make sure it's a server */ | |
206 | if (!strchr(to, '*')) { | |
207 | if (0 == (acptr = FindClient(to))) { | |
208 | send_reply(from, ERR_NOSUCHSERVER, to); | |
209 | return HUNTED_NOSUCH; | |
210 | } | |
211 | ||
212 | if (cli_user(acptr)) | |
213 | acptr = cli_user(acptr)->server; | |
214 | } else if (!(acptr = find_match_server(to))) { | |
215 | send_reply(from, ERR_NOSUCHSERVER, to); | |
216 | return (HUNTED_NOSUCH); | |
217 | } | |
218 | } else if (!(acptr = FindNServer(to))) { | |
219 | send_reply(from, SND_EXPLICIT | ERR_NOSUCHSERVER, "* :Server has disconnected"); | |
220 | return (HUNTED_NOSUCH); /* Server broke off in the meantime */ | |
221 | } | |
222 | ||
223 | if (IsMe(acptr)) | |
224 | return (HUNTED_ISME); | |
225 | ||
226 | if (MustBeOper && !IsPrivileged(from)) { | |
227 | send_reply(from, ERR_NOPRIVILEGES); | |
228 | return HUNTED_NOSUCH; | |
229 | } | |
230 | ||
231 | /* assert(!IsServer(from)); */ | |
232 | ||
233 | parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */ | |
234 | ||
235 | sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3], | |
236 | parv[4], parv[5], parv[6], parv[7], parv[8]); | |
237 | ||
238 | return (HUNTED_PASS); | |
239 | } | |
240 | ||
241 | /** Find the destination server for a command, and forward it (as a | |
242 | * high-priority command) if that is not us. | |
243 | * | |
244 | * \a server may be a nickname, server name, server mask (if \a from | |
245 | * is a local user) or server numnick (if \a is a server or remote | |
246 | * user). | |
247 | * Unlike hunt_server_cmd(), this appends the message to the | |
248 | * high-priority message queue for the destination server. | |
249 | * | |
250 | * @param[in] from Client that sent the command to us. | |
251 | * @param[in] cmd Long-form command text. | |
252 | * @param[in] tok Token-form command text. | |
253 | * @param[in] one Client that originated the command (ignored). | |
254 | * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH. | |
255 | * @param[in] pattern Format string of arguments to command. | |
256 | * @param[in] server Index of target name or mask in \a parv. | |
257 | * @param[in] parc Number of valid elements in \a parv (must be less than 9). | |
258 | * @param[in] parv Array of arguments to command. | |
259 | * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS. | |
260 | */ | |
261 | int hunt_server_prio_cmd(struct Client *from, const char *cmd, const char *tok, | |
262 | struct Client *one, int MustBeOper, | |
263 | const char *pattern, int server, int parc, | |
264 | char *parv[]) | |
265 | { | |
266 | struct Client *acptr; | |
267 | char *to; | |
268 | ||
269 | /* Assume it's me, if no server or an unregistered client */ | |
270 | if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from)) | |
271 | return (HUNTED_ISME); | |
272 | ||
273 | /* Make sure it's a server */ | |
274 | if (MyUser(from)) { | |
275 | /* Make sure it's a server */ | |
276 | if (!strchr(to, '*')) { | |
277 | if (0 == (acptr = FindClient(to))) { | |
278 | send_reply(from, ERR_NOSUCHSERVER, to); | |
279 | return HUNTED_NOSUCH; | |
280 | } | |
281 | ||
282 | if (cli_user(acptr)) | |
283 | acptr = cli_user(acptr)->server; | |
284 | } else if (!(acptr = find_match_server(to))) { | |
285 | send_reply(from, ERR_NOSUCHSERVER, to); | |
286 | return (HUNTED_NOSUCH); | |
287 | } | |
288 | } else if (!(acptr = FindNServer(to))) | |
289 | return (HUNTED_NOSUCH); /* Server broke off in the meantime */ | |
290 | ||
291 | if (IsMe(acptr)) | |
292 | return (HUNTED_ISME); | |
293 | ||
294 | if (MustBeOper && !IsPrivileged(from)) { | |
295 | send_reply(from, ERR_NOPRIVILEGES); | |
296 | return HUNTED_NOSUCH; | |
297 | } | |
298 | ||
299 | /* assert(!IsServer(from)); SETTIME to particular destinations permitted */ | |
300 | ||
301 | parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */ | |
302 | ||
303 | sendcmdto_prio_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3], | |
304 | parv[4], parv[5], parv[6], parv[7], parv[8]); | |
305 | ||
306 | return (HUNTED_PASS); | |
307 | } | |
308 | ||
309 | ||
310 | /** Copy a cleaned-up version of a username. | |
311 | * Replace all instances of '~' and "invalid" username characters | |
312 | * (!isIrcUi()) with underscores, truncating at USERLEN or the first | |
313 | * control character. \a dest and \a source may be the same buffer. | |
314 | * @param[out] dest Destination buffer. | |
315 | * @param[in] source Source buffer. | |
316 | * @param[in] tilde If non-zero, prepend a '~' to \a dest. | |
317 | */ | |
318 | static char *clean_user_id(char *dest, char *source, int tilde) | |
319 | { | |
320 | char ch; | |
321 | char *d = dest; | |
322 | char *s = source; | |
323 | int rlen = USERLEN; | |
324 | ||
325 | ch = *s++; /* Store first character to copy: */ | |
326 | if (tilde) | |
327 | { | |
328 | *d++ = '~'; /* If `dest' == `source', then this overwrites `ch' */ | |
329 | --rlen; | |
330 | } | |
331 | while (ch && !IsCntrl(ch) && rlen--) | |
332 | { | |
333 | char nch = *s++; /* Store next character to copy */ | |
334 | *d++ = IsUserChar(ch) ? ch : '_'; /* This possibly overwrites it */ | |
335 | if (nch == '~') | |
336 | ch = '_'; | |
337 | else | |
338 | ch = nch; | |
339 | } | |
340 | *d = 0; | |
341 | return dest; | |
342 | } | |
343 | ||
344 | /* | |
345 | * register_user | |
346 | * | |
347 | * This function is called when both NICK and USER messages | |
348 | * have been accepted for the client, in whatever order. Only | |
349 | * after this the USER message is propagated. | |
350 | * | |
351 | * NICK's must be propagated at once when received, although | |
352 | * it would be better to delay them too until full info is | |
353 | * available. Doing it is not so simple though, would have | |
354 | * to implement the following: | |
355 | * | |
356 | * 1) user telnets in and gives only "NICK foobar" and waits | |
357 | * 2) another user far away logs in normally with the nick | |
358 | * "foobar" (quite legal, as this server didn't propagate it). | |
359 | * 3) now this server gets nick "foobar" from outside, but | |
360 | * has already the same defined locally. Current server | |
361 | * would just issue "KILL foobar" to clean out dups. But, | |
362 | * this is not fair. It should actually request another | |
363 | * nick from local user or kill him/her... | |
364 | */ | |
365 | /** Finish registering a user who has sent both NICK and USER. | |
366 | * For local connections, possibly check IAuth; make sure there is a | |
367 | * matching Client config block; clean the username field; check | |
368 | * K/k-lines; check for "hacked" looking usernames; assign a numnick; | |
369 | * and send greeting (WELCOME, ISUPPORT, MOTD, etc). | |
370 | * For all connections, update the invisible user and operator counts; | |
371 | * run IPcheck against their address; and forward the NICK. | |
372 | * | |
373 | * @param[in] cptr Client who introduced the user. | |
374 | * @param[in,out] sptr Client who has been fully introduced. | |
375 | * @param[in] nick Client's new nickname. | |
376 | * @param[in] username Client's username. | |
377 | * @return Zero or CPTR_KILLED. | |
378 | */ | |
379 | int register_user(struct Client *cptr, struct Client *sptr, | |
380 | const char *nick, char *username) | |
381 | { | |
382 | struct ConfItem* aconf; | |
383 | char* parv[4]; | |
384 | char* tmpstr; | |
385 | char* tmpstr2; | |
386 | char c = 0; /* not alphanum */ | |
387 | char d = 'a'; /* not a digit */ | |
388 | short upper = 0; | |
389 | short lower = 0; | |
390 | short pos = 0; | |
391 | short leadcaps = 0; | |
392 | short other = 0; | |
393 | short digits = 0; | |
394 | short badid = 0; | |
395 | short digitgroups = 0; | |
396 | struct User* user = cli_user(sptr); | |
397 | int killreason; | |
398 | char ip_base64[25]; | |
399 | ||
400 | user->last = CurrentTime; | |
401 | parv[0] = cli_name(sptr); | |
402 | parv[1] = parv[2] = NULL; | |
403 | ||
404 | if (MyConnect(sptr)) | |
405 | { | |
406 | static time_t last_too_many1; | |
407 | static time_t last_too_many2; | |
408 | ||
409 | assert(cptr == sptr); | |
410 | assert(cli_unreg(sptr) == 0); | |
411 | if (!IsIAuthed(sptr)) { | |
412 | if (iauth_active) | |
413 | return iauth_start_client(iauth_active, sptr); | |
414 | else | |
415 | SetIAuthed(sptr); | |
416 | } | |
417 | switch (conf_check_client(sptr)) | |
418 | { | |
419 | case ACR_OK: | |
420 | break; | |
421 | case ACR_NO_AUTHORIZATION: | |
422 | sendto_opmask_butone(0, SNO_UNAUTH, "Unauthorized connection from %s.", | |
423 | get_client_name(sptr, HIDE_IP)); | |
424 | ++ServerStats->is_ref; | |
425 | return exit_client(cptr, sptr, &me, | |
426 | "No Authorization - use another server"); | |
427 | case ACR_TOO_MANY_IN_CLASS: | |
428 | if (CurrentTime - last_too_many1 >= (time_t) 60) | |
429 | { | |
430 | last_too_many1 = CurrentTime; | |
431 | sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections in " | |
432 | "class %s for %s.", get_client_class(sptr), | |
433 | get_client_name(sptr, SHOW_IP)); | |
434 | } | |
435 | ++ServerStats->is_ref; | |
436 | IPcheck_connect_fail(sptr); | |
437 | return exit_client(cptr, sptr, &me, | |
438 | "Sorry, your connection class is full - try " | |
439 | "again later or try another server"); | |
440 | case ACR_TOO_MANY_FROM_IP: | |
441 | if (CurrentTime - last_too_many2 >= (time_t) 60) | |
442 | { | |
443 | last_too_many2 = CurrentTime; | |
444 | sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections from " | |
445 | "same IP for %s.", | |
446 | get_client_name(sptr, SHOW_IP)); | |
447 | } | |
448 | ++ServerStats->is_ref; | |
449 | return exit_client(cptr, sptr, &me, | |
450 | "Too many connections from your host"); | |
451 | case ACR_ALREADY_AUTHORIZED: | |
452 | /* Can this ever happen? */ | |
453 | case ACR_BAD_SOCKET: | |
454 | ++ServerStats->is_ref; | |
455 | IPcheck_connect_fail(sptr); | |
456 | return exit_client(cptr, sptr, &me, "Unknown error -- Try again"); | |
457 | } | |
458 | ircd_strncpy(user->host, cli_sockhost(sptr), HOSTLEN); | |
459 | ircd_strncpy(user->realhost, cli_sockhost(sptr), HOSTLEN); | |
460 | aconf = cli_confs(sptr)->value.aconf; | |
461 | ||
462 | clean_user_id(user->username, | |
463 | HasFlag(sptr, FLAG_GOTID) ? cli_username(sptr) : username, | |
1d4df40c | 464 | HasFlag(sptr, FLAG_DOID) && !HasFlag(sptr, FLAG_GOTID) |
465 | && !(HasSetHost(sptr))); /* No tilde for S-lined users. */ | |
466 | ||
467 | /* Have to set up "realusername" before doing the gline check below */ | |
468 | ircd_strncpy(user->realusername, user->username, USERLEN); | |
189935b1 | 469 | |
470 | if ((user->username[0] == '\0') | |
471 | || ((user->username[0] == '~') && (user->username[1] == '\000'))) | |
472 | return exit_client(cptr, sptr, &me, "USER: Bogus userid."); | |
473 | ||
474 | if (!EmptyString(aconf->passwd) | |
475 | && strcmp(cli_passwd(sptr), aconf->passwd)) | |
476 | { | |
477 | ServerStats->is_ref++; | |
478 | send_reply(sptr, ERR_PASSWDMISMATCH); | |
479 | return exit_client(cptr, sptr, &me, "Bad Password"); | |
480 | } | |
481 | memset(cli_passwd(sptr), 0, sizeof(cli_passwd(sptr))); | |
482 | /* | |
483 | * following block for the benefit of time-dependent K:-lines | |
484 | */ | |
8b897318 | 485 | killreason = find_kill(sptr, 1); |
189935b1 | 486 | if (killreason) { |
487 | ServerStats->is_ref++; | |
488 | return exit_client(cptr, sptr, &me, | |
489 | (killreason == -1 ? "K-lined" : "G-lined")); | |
490 | } | |
491 | /* | |
492 | * Check for mixed case usernames, meaning probably hacked. Jon2 3-94 | |
493 | * Summary of rules now implemented in this patch: Ensor 11-94 | |
494 | * In a mixed-case name, if first char is upper, one more upper may | |
495 | * appear anywhere. (A mixed-case name *must* have an upper first | |
496 | * char, and may have one other upper.) | |
497 | * A third upper may appear if all 3 appear at the beginning of the | |
498 | * name, separated only by "others" (-/_/.). | |
499 | * A single group of digits is allowed anywhere. | |
500 | * Two groups of digits are allowed if at least one of the groups is | |
501 | * at the beginning or the end. | |
502 | * Only one '-', '_', or '.' is allowed (or two, if not consecutive). | |
503 | * But not as the first or last char. | |
504 | * No other special characters are allowed. | |
505 | * Name must contain at least one letter. | |
506 | */ | |
507 | tmpstr2 = tmpstr = (username[0] == '~' ? &username[1] : username); | |
508 | while (*tmpstr && !badid) | |
509 | { | |
510 | pos++; | |
511 | c = *tmpstr; | |
512 | tmpstr++; | |
513 | if (IsLower(c)) | |
514 | { | |
515 | lower++; | |
516 | } | |
517 | else if (IsUpper(c)) | |
518 | { | |
519 | upper++; | |
520 | if ((leadcaps || pos == 1) && !lower && !digits) | |
521 | leadcaps++; | |
522 | } | |
523 | else if (IsDigit(c)) | |
524 | { | |
525 | digits++; | |
526 | if (pos == 1 || !IsDigit(d)) | |
527 | { | |
528 | digitgroups++; | |
529 | if (digitgroups > 2) | |
530 | badid = 1; | |
531 | } | |
532 | } | |
533 | else if (c == '-' || c == '_' || c == '.') | |
534 | { | |
535 | other++; | |
536 | if (pos == 1) | |
537 | badid = 1; | |
538 | else if (d == '-' || d == '_' || d == '.' || other > 2) | |
539 | badid = 1; | |
540 | } | |
541 | else | |
542 | badid = 1; | |
543 | d = c; | |
544 | } | |
545 | if (!badid) | |
546 | { | |
547 | if (lower && upper && (!leadcaps || leadcaps > 3 || | |
548 | (upper > 2 && upper > leadcaps))) | |
549 | badid = 1; | |
550 | else if (digitgroups == 2 && !(IsDigit(tmpstr2[0]) || IsDigit(c))) | |
551 | badid = 1; | |
552 | else if ((!lower && !upper) || !IsAlnum(c)) | |
553 | badid = 1; | |
554 | } | |
555 | if (badid && (!HasFlag(sptr, FLAG_GOTID) || | |
556 | strcmp(cli_username(sptr), username) != 0)) | |
557 | { | |
558 | ServerStats->is_ref++; | |
559 | ||
560 | send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME, | |
561 | ":Your username is invalid."); | |
562 | send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME, | |
563 | ":Connect with your real username, in lowercase."); | |
564 | send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME, | |
565 | ":If your mail address were foo@bar.com, your username " | |
566 | "would be foo."); | |
567 | return exit_client(cptr, sptr, &me, "USER: Bad username"); | |
568 | } | |
569 | Count_unknownbecomesclient(sptr, UserStats); | |
570 | ||
fc04776e | 571 | if (MyConnect(sptr) && feature_bool(FEAT_AUTOINVISIBLE)) |
572 | SetInvisible(sptr); | |
573 | ||
1d4df40c | 574 | if(MyConnect(sptr) && feature_bool(FEAT_SETHOST_AUTO)) { |
575 | if (conf_check_slines(sptr)) { | |
576 | send_reply(sptr, RPL_USINGSLINE); | |
577 | SetSetHost(sptr); | |
578 | } | |
579 | } | |
580 | ||
189935b1 | 581 | SetUser(sptr); |
582 | cli_handler(sptr) = CLIENT_HANDLER; | |
583 | SetLocalNumNick(sptr); | |
584 | send_reply(sptr, | |
585 | RPL_WELCOME, | |
586 | feature_str(FEAT_NETWORK), | |
587 | feature_str(FEAT_PROVIDER) ? " via " : "", | |
588 | feature_str(FEAT_PROVIDER) ? feature_str(FEAT_PROVIDER) : "", | |
589 | nick); | |
590 | /* | |
591 | * This is a duplicate of the NOTICE but see below... | |
592 | */ | |
593 | send_reply(sptr, RPL_YOURHOST, cli_name(&me), version); | |
594 | send_reply(sptr, RPL_CREATED, creation); | |
595 | send_reply(sptr, RPL_MYINFO, cli_name(&me), version, infousermodes, | |
596 | infochanmodes, infochanmodeswithparams); | |
597 | send_supported(sptr); | |
598 | m_lusers(sptr, sptr, 1, parv); | |
599 | update_load(); | |
600 | motd_signon(sptr); | |
601 | if (cli_snomask(sptr) & SNO_NOISY) | |
602 | set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD); | |
603 | if (feature_bool(FEAT_CONNEXIT_NOTICES)) | |
604 | sendto_opmask_butone(0, SNO_CONNEXIT, | |
605 | "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>", | |
606 | cli_name(sptr), user->username, user->host, | |
607 | cli_sock_ip(sptr), get_client_class(sptr), | |
608 | cli_info(sptr), NumNick(cptr) /* two %s's */); | |
609 | ||
610 | IPcheck_connect_succeeded(sptr); | |
611 | /* | |
612 | * Set user's initial modes | |
613 | */ | |
614 | tmpstr = (char*)client_get_default_umode(sptr); | |
615 | if (tmpstr) for (; *tmpstr; ++tmpstr) { | |
616 | switch (*tmpstr) { | |
617 | case 's': | |
618 | if (!feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY)) { | |
619 | SetServNotice(sptr); | |
620 | set_snomask(sptr, SNO_DEFAULT, SNO_SET); | |
621 | } | |
622 | break; | |
623 | case 'w': | |
624 | if (!feature_bool(FEAT_WALLOPS_OPER_ONLY)) | |
625 | SetWallops(sptr); | |
626 | break; | |
627 | case 'i': | |
628 | SetInvisible(sptr); | |
629 | break; | |
630 | case 'd': | |
631 | SetDeaf(sptr); | |
632 | break; | |
633 | case 'g': | |
634 | if (!feature_bool(FEAT_HIS_DEBUG_OPER_ONLY)) | |
635 | SetDebug(sptr); | |
636 | break; | |
637 | } | |
638 | } | |
639 | } | |
640 | else { | |
641 | struct Client *acptr; | |
642 | ||
643 | ircd_strncpy(user->username, username, USERLEN); | |
644 | Count_newremoteclient(UserStats, user->server); | |
645 | ||
646 | acptr = user->server; | |
647 | if (cli_from(acptr) != cli_from(sptr)) | |
648 | { | |
649 | sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])", | |
650 | sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)), | |
651 | cli_sockhost(cli_from(acptr))); | |
652 | SetFlag(sptr, FLAG_KILLED); | |
653 | return exit_client(cptr, sptr, &me, "NICK server wrong direction"); | |
654 | } | |
655 | else if (HasFlag(acptr, FLAG_TS8)) | |
656 | SetFlag(sptr, FLAG_TS8); | |
657 | ||
658 | /* | |
659 | * Check to see if this user is being propagated | |
660 | * as part of a net.burst, or is using protocol 9. | |
661 | * FIXME: This can be sped up - its stupid to check it for | |
662 | * every NICK message in a burst again --Run. | |
663 | */ | |
664 | for (; acptr != &me; acptr = cli_serv(acptr)->up) | |
665 | { | |
666 | if (IsBurst(acptr) || Protocol(acptr) < 10) | |
667 | break; | |
668 | } | |
669 | if (!IPcheck_remote_connect(sptr, (acptr != &me))) | |
670 | { | |
671 | /* | |
672 | * We ran out of bits to count this | |
673 | */ | |
674 | sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)", | |
675 | sptr, cli_name(&me)); | |
676 | return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled"); | |
677 | } | |
1d4df40c | 678 | |
679 | if(MyConnect(sptr) && feature_bool(FEAT_SETHOST_AUTO)) { | |
680 | if (conf_check_slines(sptr)) { | |
681 | send_reply(sptr, RPL_USINGSLINE); | |
682 | SetSetHost(sptr); | |
683 | } | |
684 | } | |
685 | ||
189935b1 | 686 | SetUser(sptr); |
687 | } | |
688 | ||
689 | if (IsInvisible(sptr)) | |
690 | ++UserStats.inv_clients; | |
691 | if (IsOper(sptr)) | |
692 | ++UserStats.opers; | |
693 | ||
694 | tmpstr = umode_str(sptr); | |
695 | /* Send full IP address to IPv6-grokking servers. */ | |
696 | sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr, | |
697 | FLAG_IPV6, FLAG_LAST_FLAG, | |
698 | "%s %d %Tu %s %s %s%s%s%s %s%s :%s", | |
699 | nick, cli_hopcount(sptr) + 1, cli_lastnick(sptr), | |
1d4df40c | 700 | user->realusername, user->realhost, |
189935b1 | 701 | *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "", |
702 | iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 1), | |
703 | NumNick(sptr), cli_info(sptr)); | |
704 | /* Send fake IPv6 addresses to pre-IPv6 servers. */ | |
705 | sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr, | |
706 | FLAG_LAST_FLAG, FLAG_IPV6, | |
707 | "%s %d %Tu %s %s %s%s%s%s %s%s :%s", | |
708 | nick, cli_hopcount(sptr) + 1, cli_lastnick(sptr), | |
1d4df40c | 709 | user->realusername, user->realhost, |
189935b1 | 710 | *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "", |
711 | iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 0), | |
712 | NumNick(sptr), cli_info(sptr)); | |
713 | ||
714 | /* Send user mode to client */ | |
715 | if (MyUser(sptr)) | |
716 | { | |
717 | static struct Flags flags; /* automatically initialized to zeros */ | |
718 | send_umode(cptr, sptr, &flags, ALL_UMODES); | |
719 | if ((cli_snomask(sptr) != SNO_DEFAULT) && HasFlag(sptr, FLAG_SERVNOTICE)) | |
720 | send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr)); | |
721 | } | |
722 | return 0; | |
723 | } | |
724 | ||
725 | /** List of user mode characters. */ | |
726 | static const struct UserMode { | |
727 | unsigned int flag; /**< User mode constant. */ | |
728 | char c; /**< Character corresponding to the mode. */ | |
729 | } userModeList[] = { | |
730 | { FLAG_OPER, 'o' }, | |
731 | { FLAG_LOCOP, 'O' }, | |
732 | { FLAG_INVISIBLE, 'i' }, | |
733 | { FLAG_WALLOP, 'w' }, | |
734 | { FLAG_SERVNOTICE, 's' }, | |
735 | { FLAG_DEAF, 'd' }, | |
736 | { FLAG_CHSERV, 'k' }, | |
737 | { FLAG_DEBUG, 'g' }, | |
738 | { FLAG_ACCOUNT, 'r' }, | |
08d23967 | 739 | { FLAG_HIDDENHOST, 'x' }, |
943fcf70 | 740 | { FLAG_ACCOUNTONLY, 'R' }, |
741 | { FLAG_XTRAOP, 'X' }, | |
742 | { FLAG_NOCHAN, 'n' }, | |
1d4df40c | 743 | { FLAG_NOIDLE, 'I' }, |
ebebfa5f | 744 | { FLAG_SETHOST, 'h' }, |
745 | { FLAG_PARANOID, 'P' } | |
189935b1 | 746 | }; |
747 | ||
748 | /** Length of #userModeList. */ | |
749 | #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode) | |
750 | ||
751 | /* | |
752 | * XXX - find a way to get rid of this | |
753 | */ | |
754 | /** Nasty global buffer used for communications with umode_str() and others. */ | |
755 | static char umodeBuf[BUFSIZE]; | |
756 | ||
757 | /** Try to set a user's nickname. | |
758 | * If \a sptr is a server, the client is being introduced for the first time. | |
759 | * @param[in] cptr Client to set nickname. | |
760 | * @param[in] sptr Client sending the NICK. | |
761 | * @param[in] nick New nickname. | |
762 | * @param[in] parc Number of arguments to NICK. | |
763 | * @param[in] parv Argument list to NICK. | |
764 | * @return CPTR_KILLED if \a cptr was killed, else 0. | |
765 | */ | |
766 | int set_nick_name(struct Client* cptr, struct Client* sptr, | |
767 | const char* nick, int parc, char* parv[]) | |
768 | { | |
769 | if (IsServer(sptr)) { | |
770 | int i; | |
771 | const char* account = 0; | |
1d4df40c | 772 | char* hostmask = 0; |
773 | char* host = 0; | |
189935b1 | 774 | const char* p; |
775 | ||
776 | /* | |
777 | * A server introducing a new client, change source | |
778 | */ | |
779 | struct Client* new_client = make_client(cptr, STAT_UNKNOWN); | |
780 | assert(0 != new_client); | |
781 | ||
782 | cli_hopcount(new_client) = atoi(parv[2]); | |
783 | cli_lastnick(new_client) = atoi(parv[3]); | |
784 | if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+') | |
785 | { | |
786 | for (p = parv[6] + 1; *p; p++) | |
787 | { | |
788 | for (i = 0; i < USERMODELIST_SIZE; ++i) | |
789 | { | |
790 | if (userModeList[i].c == *p) | |
791 | { | |
792 | SetFlag(new_client, userModeList[i].flag); | |
793 | if (userModeList[i].flag == FLAG_ACCOUNT) | |
794 | account = parv[7]; | |
1d4df40c | 795 | if (userModeList[i].flag == FLAG_SETHOST) |
796 | hostmask = parv[parc - 4]; | |
189935b1 | 797 | break; |
798 | } | |
799 | } | |
800 | } | |
801 | } | |
802 | client_set_privs(new_client, NULL); /* set privs on user */ | |
803 | /* | |
804 | * Set new nick name. | |
805 | */ | |
806 | strcpy(cli_name(new_client), nick); | |
807 | cli_user(new_client) = make_user(new_client); | |
808 | cli_user(new_client)->server = sptr; | |
809 | SetRemoteNumNick(new_client, parv[parc - 2]); | |
810 | /* | |
811 | * IP# of remote client | |
812 | */ | |
813 | base64toip(parv[parc - 3], &cli_ip(new_client)); | |
814 | ||
815 | add_client_to_list(new_client); | |
816 | hAddClient(new_client); | |
817 | ||
818 | cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */ | |
819 | ircd_strncpy(cli_username(new_client), parv[4], USERLEN); | |
1d4df40c | 820 | ircd_strncpy(cli_user(new_client)->realusername, parv[4], USERLEN); |
189935b1 | 821 | ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN); |
822 | ircd_strncpy(cli_user(new_client)->realhost, parv[5], HOSTLEN); | |
823 | ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN); | |
824 | if (account) { | |
825 | int len = ACCOUNTLEN; | |
826 | if ((p = strchr(account, ':'))) { | |
827 | len = (p++) - account; | |
828 | cli_user(new_client)->acc_create = atoi(p); | |
829 | Debug((DEBUG_DEBUG, "Received timestamped account in user mode; " | |
830 | "account \"%s\", timestamp %Tu", account, | |
831 | cli_user(new_client)->acc_create)); | |
832 | } | |
833 | ircd_strncpy(cli_user(new_client)->account, account, len); | |
834 | } | |
835 | if (HasHiddenHost(new_client)) | |
836 | ircd_snprintf(0, cli_user(new_client)->host, HOSTLEN, "%s.%s", | |
837 | account, feature_str(FEAT_HIDDEN_HOST)); | |
1d4df40c | 838 | if (HasSetHost(new_client)) { |
839 | if ((host = strrchr(hostmask, '@')) != NULL) { | |
840 | *host++ = '\0'; | |
841 | ircd_strncpy(cli_username(new_client), hostmask, USERLEN); | |
842 | ircd_strncpy(cli_user(new_client)->host, host, HOSTLEN); | |
843 | } | |
844 | } | |
189935b1 | 845 | |
1d4df40c | 846 | return register_user(cptr, new_client, cli_name(new_client), cli_username(new_client)); |
189935b1 | 847 | } |
848 | else if ((cli_name(sptr))[0]) { | |
849 | /* | |
850 | * Client changing its nick | |
851 | * | |
852 | * If the client belongs to me, then check to see | |
853 | * if client is on any channels where it is currently | |
854 | * banned. If so, do not allow the nick change to occur. | |
855 | */ | |
856 | if (MyUser(sptr)) { | |
857 | const char* channel_name; | |
858 | struct Membership *member; | |
943fcf70 | 859 | if ((channel_name = find_no_nickchange_channel(sptr)) && !IsXtraOp(sptr)) { |
189935b1 | 860 | return send_reply(cptr, ERR_BANNICKCHANGE, channel_name); |
861 | } | |
862 | /* | |
863 | * Refuse nick change if the last nick change was less | |
864 | * then 30 seconds ago. This is intended to get rid of | |
865 | * clone bots doing NICK FLOOD. -SeKs | |
866 | * If someone didn't change their nick for more then 60 seconds | |
867 | * however, allow to do two nick changes immediately after another | |
868 | * before limiting the nick flood. -Run | |
869 | */ | |
870 | if (CurrentTime < cli_nextnick(cptr)) | |
871 | { | |
872 | cli_nextnick(cptr) += 2; | |
873 | send_reply(cptr, ERR_NICKTOOFAST, parv[1], | |
874 | cli_nextnick(cptr) - CurrentTime); | |
875 | /* Send error message */ | |
876 | sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr)); | |
877 | /* bounce NICK to user */ | |
878 | return 0; /* ignore nick change! */ | |
879 | } | |
880 | else { | |
881 | /* Limit total to 1 change per NICK_DELAY seconds: */ | |
882 | cli_nextnick(cptr) += NICK_DELAY; | |
883 | /* However allow _maximal_ 1 extra consecutive nick change: */ | |
884 | if (cli_nextnick(cptr) < CurrentTime) | |
885 | cli_nextnick(cptr) = CurrentTime; | |
886 | } | |
887 | /* Invalidate all bans against the user so we check them again */ | |
888 | for (member = (cli_user(cptr))->channel; member; | |
889 | member = member->next_channel) | |
890 | ClearBanValid(member); | |
891 | } | |
892 | /* | |
893 | * Also set 'lastnick' to current time, if changed. | |
894 | */ | |
895 | if (0 != ircd_strcmp(parv[0], nick)) | |
896 | cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]); | |
897 | ||
898 | /* | |
899 | * Client just changing his/her nick. If he/she is | |
900 | * on a channel, send note of change to all clients | |
901 | * on that channel. Propagate notice to other servers. | |
902 | */ | |
903 | if (IsUser(sptr)) { | |
904 | sendcmdto_common_channels_butone(sptr, CMD_NICK, NULL, ":%s", nick); | |
905 | add_history(sptr, 1); | |
906 | sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick, | |
907 | cli_lastnick(sptr)); | |
908 | } | |
909 | else | |
910 | sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick); | |
911 | ||
912 | if ((cli_name(sptr))[0]) | |
913 | hRemClient(sptr); | |
914 | strcpy(cli_name(sptr), nick); | |
915 | hAddClient(sptr); | |
916 | } | |
917 | else { | |
918 | /* Local client setting NICK the first time */ | |
919 | ||
920 | strcpy(cli_name(sptr), nick); | |
921 | if (!cli_user(sptr)) { | |
922 | cli_user(sptr) = make_user(sptr); | |
923 | cli_user(sptr)->server = &me; | |
924 | } | |
925 | hAddClient(sptr); | |
926 | ||
927 | cli_unreg(sptr) &= ~CLIREG_NICK; /* nickname now set */ | |
928 | ||
929 | /* | |
930 | * If the client hasn't gotten a cookie-ping yet, | |
931 | * choose a cookie and send it. -record!jegelhof@cloud9.net | |
932 | */ | |
933 | if (!cli_cookie(sptr)) { | |
934 | do { | |
935 | cli_cookie(sptr) = (ircrandom() & 0x7fffffff); | |
936 | } while (!cli_cookie(sptr)); | |
937 | sendrawto_one(cptr, MSG_PING " :%u", cli_cookie(sptr)); | |
938 | } | |
939 | else if (!cli_unreg(sptr)) { | |
940 | /* | |
941 | * USER and PONG already received, now we have NICK. | |
942 | * register_user may reject the client and call exit_client | |
943 | * for it - must test this and exit m_nick too ! | |
944 | */ | |
945 | cli_lastnick(sptr) = TStime(); /* Always local client */ | |
946 | if (register_user(cptr, sptr, nick, cli_user(sptr)->username) == CPTR_KILLED) | |
947 | return CPTR_KILLED; | |
948 | } | |
949 | } | |
950 | return 0; | |
951 | } | |
952 | ||
953 | /** Calculate the hash value for a target. | |
954 | * @param[in] target Pointer to target, cast to unsigned int. | |
955 | * @return Hash value constructed from the pointer. | |
956 | */ | |
957 | static unsigned char hash_target(unsigned int target) | |
958 | { | |
959 | return (unsigned char) (target >> 16) ^ (target >> 8); | |
960 | } | |
961 | ||
962 | /** Records \a target as a recent target for \a sptr. | |
963 | * @param[in] sptr User who has sent to a new target. | |
964 | * @param[in] target Target to add. | |
965 | */ | |
966 | void | |
967 | add_target(struct Client *sptr, void *target) | |
968 | { | |
969 | /* Ok, this shouldn't work esp on alpha | |
970 | */ | |
971 | unsigned char hash = hash_target((unsigned long) target); | |
972 | unsigned char* targets; | |
973 | int i; | |
974 | assert(0 != sptr); | |
975 | assert(cli_local(sptr)); | |
976 | ||
977 | targets = cli_targets(sptr); | |
978 | ||
979 | /* | |
980 | * Already in table? | |
981 | */ | |
982 | for (i = 0; i < MAXTARGETS; ++i) { | |
983 | if (targets[i] == hash) | |
984 | return; | |
985 | } | |
986 | /* | |
987 | * New target | |
988 | */ | |
989 | memmove(&targets[RESERVEDTARGETS + 1], | |
990 | &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1); | |
991 | targets[RESERVEDTARGETS] = hash; | |
992 | } | |
993 | ||
994 | /** Check whether \a sptr can send to or join \a target yet. | |
995 | * @param[in] sptr User trying to join a channel or send a message. | |
996 | * @param[in] target Target of the join or message. | |
997 | * @param[in] name Name of the target. | |
998 | * @param[in] created If non-zero, trying to join a new channel. | |
999 | * @return Non-zero if too many target changes; zero if okay to send. | |
1000 | */ | |
1001 | int check_target_limit(struct Client *sptr, void *target, const char *name, | |
1002 | int created) | |
1003 | { | |
1004 | unsigned char hash = hash_target((unsigned long) target); | |
1005 | int i; | |
1006 | unsigned char* targets; | |
1007 | ||
1008 | assert(0 != sptr); | |
1009 | assert(cli_local(sptr)); | |
1010 | targets = cli_targets(sptr); | |
1011 | ||
1012 | /* If user is invited to channel, give him/her a free target */ | |
1013 | if (IsChannelName(name) && IsInvited(sptr, target)) | |
1014 | return 0; | |
1015 | ||
ac1f857f | 1016 | /* opers always have a free target */ |
1017 | if (IsAnOper(sptr)) | |
1018 | return 0; | |
1019 | ||
189935b1 | 1020 | /* |
1021 | * Same target as last time? | |
1022 | */ | |
1023 | if (targets[0] == hash) | |
1024 | return 0; | |
1025 | for (i = 1; i < MAXTARGETS; ++i) { | |
1026 | if (targets[i] == hash) { | |
1027 | memmove(&targets[1], &targets[0], i); | |
1028 | targets[0] = hash; | |
1029 | return 0; | |
1030 | } | |
1031 | } | |
1032 | /* | |
1033 | * New target | |
1034 | */ | |
1035 | if (!created) { | |
1036 | if (CurrentTime < cli_nexttarget(sptr)) { | |
1037 | if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) { | |
1038 | /* | |
1039 | * No server flooding | |
1040 | */ | |
1041 | cli_nexttarget(sptr) += 2; | |
1042 | send_reply(sptr, ERR_TARGETTOOFAST, name, | |
1043 | cli_nexttarget(sptr) - CurrentTime); | |
1044 | } | |
1045 | return 1; | |
1046 | } | |
1047 | else { | |
1048 | cli_nexttarget(sptr) += TARGET_DELAY; | |
1049 | if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1))) | |
1050 | cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)); | |
1051 | } | |
1052 | } | |
1053 | memmove(&targets[1], &targets[0], MAXTARGETS - 1); | |
1054 | targets[0] = hash; | |
1055 | return 0; | |
1056 | } | |
1057 | ||
1058 | /** Allows a channel operator to avoid target change checks when | |
1059 | * sending messages to users on their channel. | |
1060 | * @param[in] source User sending the message. | |
1061 | * @param[in] nick Destination of the message. | |
1062 | * @param[in] channel Name of channel being sent to. | |
1063 | * @param[in] text Message to send. | |
1064 | * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG. | |
1065 | */ | |
1066 | /* Added 971023 by Run. */ | |
1067 | int whisper(struct Client* source, const char* nick, const char* channel, | |
1068 | const char* text, int is_notice) | |
1069 | { | |
1070 | struct Client* dest; | |
1071 | struct Channel* chptr; | |
1072 | struct Membership* membership; | |
1073 | ||
1074 | assert(0 != source); | |
1075 | assert(0 != nick); | |
1076 | assert(0 != channel); | |
1077 | assert(MyUser(source)); | |
1078 | ||
1079 | if (!(dest = FindUser(nick))) { | |
1080 | return send_reply(source, ERR_NOSUCHNICK, nick); | |
1081 | } | |
1082 | if (!(chptr = FindChannel(channel))) { | |
1083 | return send_reply(source, ERR_NOSUCHCHANNEL, channel); | |
1084 | } | |
1085 | /* | |
1086 | * compare both users channel lists, instead of the channels user list | |
1087 | * since the link is the same, this should be a little faster for channels | |
1088 | * with a lot of users | |
1089 | */ | |
1090 | for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) { | |
1091 | if (chptr == membership->channel) | |
1092 | break; | |
1093 | } | |
1094 | if (0 == membership) { | |
1095 | return send_reply(source, ERR_NOTONCHANNEL, chptr->chname); | |
1096 | } | |
1097 | if (!IsVoicedOrOpped(membership)) { | |
1098 | return send_reply(source, ERR_VOICENEEDED, chptr->chname); | |
1099 | } | |
1100 | /* | |
1101 | * lookup channel in destination | |
1102 | */ | |
1103 | assert(0 != cli_user(dest)); | |
1104 | for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) { | |
1105 | if (chptr == membership->channel) | |
1106 | break; | |
1107 | } | |
1108 | if (0 == membership || IsZombie(membership)) { | |
1109 | return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname); | |
1110 | } | |
1111 | if (is_silenced(source, dest)) | |
1112 | return 0; | |
1113 | ||
1114 | if (cli_user(dest)->away) | |
1115 | send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away); | |
1116 | if (is_notice) | |
1117 | sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text); | |
1118 | else | |
1119 | sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text); | |
1120 | return 0; | |
1121 | } | |
1122 | ||
1123 | ||
1124 | /** Send a user mode change for \a cptr to neighboring servers. | |
1125 | * @param[in] cptr User whose mode is changing. | |
1126 | * @param[in] sptr Client who sent us the mode change message. | |
1127 | * @param[in] old Prior set of user flags. | |
1128 | * @param[in] prop If non-zero, also include FLAG_OPER. | |
1129 | */ | |
1130 | void send_umode_out(struct Client *cptr, struct Client *sptr, | |
1131 | struct Flags *old, int prop) | |
1132 | { | |
1133 | int i; | |
1134 | struct Client *acptr; | |
1135 | ||
1136 | send_umode(NULL, sptr, old, prop ? SEND_UMODES : SEND_UMODES_BUT_OPER); | |
1137 | ||
1138 | for (i = HighestFd; i >= 0; i--) | |
1139 | { | |
1140 | if ((acptr = LocalClientArray[i]) && IsServer(acptr) && | |
1141 | (acptr != cptr) && (acptr != sptr) && *umodeBuf) | |
1d4df40c | 1142 | sendcmdto_one(sptr, CMD_MODE, acptr, "%s %s", cli_name(sptr), umodeBuf); |
189935b1 | 1143 | } |
1144 | if (cptr && MyUser(cptr)) | |
1145 | send_umode(cptr, sptr, old, ALL_UMODES); | |
1146 | } | |
1147 | ||
1148 | ||
1149 | /** Call \a fmt for each Client named in \a names. | |
1150 | * @param[in] sptr Client requesting information. | |
1151 | * @param[in] names Space-delimited list of nicknames. | |
1152 | * @param[in] rpl Base reply string for messages. | |
1153 | * @param[in] fmt Formatting callback function. | |
1154 | */ | |
1155 | void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt) | |
1156 | { | |
1157 | char* name; | |
1158 | char* p = 0; | |
1159 | int arg_count = 0; | |
1160 | int users_found = 0; | |
1161 | struct Client* acptr; | |
1162 | struct MsgBuf* mb; | |
1163 | ||
1164 | assert(0 != sptr); | |
1165 | assert(0 != names); | |
1166 | assert(0 != fmt); | |
1167 | ||
1168 | mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr)); | |
1169 | ||
1170 | for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) { | |
1171 | if ((acptr = FindUser(name))) { | |
1172 | if (users_found++) | |
1173 | msgq_append(0, mb, " "); | |
1174 | (*fmt)(acptr, sptr, mb); | |
1175 | } | |
1176 | if (5 == ++arg_count) | |
1177 | break; | |
1178 | } | |
1179 | send_buffer(sptr, mb, 0); | |
1180 | msgq_clean(mb); | |
1181 | } | |
1182 | ||
1183 | /** Set \a flag on \a cptr and possibly hide the client's hostmask. | |
1184 | * @param[in,out] cptr User who is getting a new flag. | |
1185 | * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT). | |
1186 | * @return Zero. | |
1187 | */ | |
1188 | int | |
1189 | hide_hostmask(struct Client *cptr, unsigned int flag) | |
1190 | { | |
1191 | struct Membership *chan; | |
1192 | ||
1193 | switch (flag) { | |
1194 | case FLAG_HIDDENHOST: | |
1195 | /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */ | |
1196 | if (MyConnect(cptr) && !feature_bool(FEAT_HOST_HIDING)) | |
1197 | return 0; | |
1d4df40c | 1198 | /* If the user is +h, we don't hide the hostmask. Set the flag to keep sync though */ |
1199 | if (HasSetHost(cptr)) { | |
1200 | SetFlag(cptr, flag); | |
1201 | return 0; | |
1202 | } | |
189935b1 | 1203 | break; |
1204 | case FLAG_ACCOUNT: | |
1205 | /* Invalidate all bans against the user so we check them again */ | |
1206 | for (chan = (cli_user(cptr))->channel; chan; | |
1207 | chan = chan->next_channel) | |
1208 | ClearBanValid(chan); | |
1209 | break; | |
1210 | default: | |
1211 | return 0; | |
1212 | } | |
1213 | ||
1214 | SetFlag(cptr, flag); | |
1215 | if (!HasFlag(cptr, FLAG_HIDDENHOST) || !HasFlag(cptr, FLAG_ACCOUNT)) | |
1216 | return 0; | |
1217 | ||
1218 | sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Registered"); | |
1219 | ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s", | |
1220 | cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST)); | |
1221 | ||
1222 | /* ok, the client is now fully hidden, so let them know -- hikari */ | |
1223 | if (MyConnect(cptr)) | |
1224 | send_reply(cptr, RPL_HOSTHIDDEN, cli_user(cptr)->host); | |
1225 | ||
1226 | /* | |
1227 | * Go through all channels the client was on, rejoin him | |
1228 | * and set the modes, if any | |
1229 | */ | |
1230 | for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel) | |
1231 | { | |
1232 | if (IsZombie(chan)) | |
1233 | continue; | |
1234 | /* Send a JOIN unless the user's join has been delayed. */ | |
1235 | if (!IsDelayedJoin(chan)) | |
1236 | sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0, | |
1237 | "%H", chan->channel); | |
1238 | if (IsChanOp(chan) && HasVoice(chan)) | |
1239 | sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0, | |
1240 | "%H +ov %C %C", chan->channel, cptr, | |
1241 | cptr); | |
1242 | else if (IsChanOp(chan) || HasVoice(chan)) | |
1243 | sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0, | |
1244 | "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr); | |
1245 | } | |
1246 | return 0; | |
1247 | } | |
1248 | ||
1d4df40c | 1249 | /* |
1250 | * set_hostmask() - derived from hide_hostmask() | |
1251 | * | |
1252 | */ | |
1253 | int set_hostmask(struct Client *cptr, char *hostmask, char *password) | |
1254 | { | |
1255 | int restore = 0; | |
1256 | int freeform = 0; | |
1257 | char *host, *new_vhost, *vhost_pass; | |
1258 | char hiddenhost[USERLEN + HOSTLEN + 2]; | |
1259 | struct Membership *chan; | |
1260 | ||
1261 | Debug((DEBUG_INFO, "set_hostmask() %C, %s, %s", cptr, hostmask, password)); | |
1262 | ||
1263 | /* sethost enabled? */ | |
1264 | if (MyConnect(cptr) && !feature_bool(FEAT_SETHOST)) { | |
1265 | send_reply(cptr, ERR_DISABLED, "SETHOST"); | |
1266 | return 0; | |
1267 | } | |
1268 | ||
1269 | /* sethost enabled for users? */ | |
1270 | if (MyConnect(cptr) && !IsAnOper(cptr) && !feature_bool(FEAT_SETHOST_USER)) { | |
1271 | send_reply(cptr, ERR_NOPRIVILEGES); | |
1272 | return 0; | |
1273 | } | |
1274 | ||
1275 | /* MODE_DEL: restore original hostmask */ | |
1276 | if (EmptyString(hostmask)) { | |
1277 | /* is already sethost'ed? */ | |
1278 | if (IsSetHost(cptr)) { | |
1279 | restore = 1; | |
1280 | sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Host change"); | |
1281 | /* If they are +rx, we need to return to their +x host, not their "real" host */ | |
1282 | if (HasHiddenHost(cptr)) | |
1283 | ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s", | |
1284 | cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST)); | |
1285 | else | |
1286 | strncpy(cli_user(cptr)->host, cli_user(cptr)->realhost, HOSTLEN); | |
1287 | strncpy(cli_user(cptr)->username, cli_user(cptr)->realusername, USERLEN); | |
1288 | /* log it */ | |
1289 | if (MyConnect(cptr)) | |
1290 | log_write(LS_SETHOST, L_INFO, LOG_NOSNOTICE, | |
1291 | "SETHOST (%s@%s) by (%#R): restoring real hostmask", | |
1292 | cli_user(cptr)->username, cli_user(cptr)->host, cptr); | |
1293 | } else | |
1294 | return 0; | |
1295 | /* MODE_ADD: set a new hostmask */ | |
1296 | } else { | |
1297 | /* chop up ident and host.cc */ | |
1298 | if ((host = strrchr(hostmask, '@'))) /* oper can specifiy ident@host.cc */ | |
1299 | *host++ = '\0'; | |
1300 | else /* user can only specifiy host.cc [password] */ | |
1301 | host = hostmask; | |
1302 | /* | |
1303 | * Oper sethost | |
1304 | */ | |
1305 | if (MyConnect(cptr)) { | |
1306 | if (IsAnOper(cptr)) { | |
1307 | if ((new_vhost = IsVhost(host, 1)) == NULL) { | |
1308 | if (!feature_bool(FEAT_SETHOST_FREEFORM)) { | |
1309 | send_reply(cptr, ERR_HOSTUNAVAIL, hostmask); | |
1310 | log_write(LS_SETHOST, L_INFO, LOG_NOSNOTICE, | |
1311 | "SETHOST (%s@%s) by (%#R): no such s-line", | |
1312 | (host != hostmask) ? hostmask : cli_user(cptr)->username, host, cptr); | |
1313 | return 0; | |
1314 | } else /* freeform active, log and go */ | |
1315 | freeform = 1; | |
1316 | } | |
1317 | sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Host change"); | |
1318 | /* set the new ident and host */ | |
1319 | if (host != hostmask) /* oper only specified host.cc */ | |
1320 | strncpy(cli_user(cptr)->username, hostmask, USERLEN); | |
1321 | strncpy(cli_user(cptr)->host, host, HOSTLEN); | |
1322 | /* log it */ | |
1323 | log_write(LS_SETHOST, (freeform) ? L_NOTICE : L_INFO, | |
1324 | (freeform) ? 0 : LOG_NOSNOTICE, "SETHOST (%s@%s) by (%#R)%s", | |
1325 | cli_user(cptr)->username, cli_user(cptr)->host, cptr, | |
1326 | (freeform) ? ": using freeform" : ""); | |
1327 | /* | |
1328 | * plain user sethost, handled here | |
1329 | */ | |
1330 | } else { | |
1331 | /* empty password? */ | |
1332 | if (EmptyString(password)) { | |
1333 | send_reply(cptr, ERR_NEEDMOREPARAMS, "MODE"); | |
1334 | return 0; | |
1335 | } | |
1336 | /* no such s-line */ | |
1337 | if ((new_vhost = IsVhost(host, 0)) == NULL) { | |
1338 | send_reply(cptr, ERR_HOSTUNAVAIL, hostmask); | |
1339 | log_write(LS_SETHOST, L_INFO, LOG_NOSNOTICE, "SETHOST (%s@%s %s) by (%#R): no such s-line", | |
1340 | cli_user(cptr)->username, host, password, cptr); | |
1341 | return 0; | |
1342 | } | |
1343 | /* no password */ | |
1344 | if ((vhost_pass = IsVhostPass(new_vhost)) == NULL) { | |
1345 | send_reply(cptr, ERR_PASSWDMISMATCH); | |
1346 | log_write(LS_SETHOST, L_INFO, 0, "SETHOST (%s@%s %s) by (%#R): trying to use an oper s-line", | |
1347 | cli_user(cptr)->username, host, password, cptr); | |
1348 | return 0; | |
1349 | } | |
1350 | /* incorrect password */ | |
1351 | if (strCasediff(vhost_pass, password)) { | |
1352 | send_reply(cptr, ERR_PASSWDMISMATCH); | |
1353 | log_write(LS_SETHOST, L_NOTICE, 0, "SETHOST (%s@%s %s) by (%#R): incorrect password", | |
1354 | cli_user(cptr)->username, host, password, cptr); | |
1355 | return 0; | |
1356 | } | |
1357 | sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Host change"); | |
1358 | /* set the new host */ | |
1359 | strncpy(cli_user(cptr)->host, new_vhost, HOSTLEN); | |
1360 | /* log it */ | |
1361 | log_write(LS_SETHOST, L_INFO, LOG_NOSNOTICE, "SETHOST (%s@%s) by (%#R)", | |
1362 | cli_user(cptr)->username, cli_user(cptr)->host, cptr); | |
1363 | } | |
1364 | } else { /* remote user */ | |
1365 | sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Host change"); | |
1366 | if (host != hostmask) /* oper only specified host.cc */ | |
1367 | strncpy(cli_user(cptr)->username, hostmask, USERLEN); | |
1368 | strncpy(cli_user(cptr)->host, host, HOSTLEN); | |
1369 | } | |
1370 | } | |
1371 | ||
1372 | if (restore) | |
1373 | ClearSetHost(cptr); | |
1374 | else | |
1375 | SetSetHost(cptr); | |
1376 | ||
1377 | if (MyConnect(cptr)) { | |
1378 | ircd_snprintf(0, hiddenhost, HOSTLEN + USERLEN + 2, "%s@%s", | |
1379 | cli_user(cptr)->username, cli_user(cptr)->host); | |
1380 | send_reply(cptr, RPL_HOSTHIDDEN, hiddenhost); | |
1381 | } | |
1382 | ||
1383 | #if 0 | |
1384 | /* Code copied from hide_hostmask(). This is the old (pre-delayedjoin) | |
1385 | * version. Switch this in if you're not using the delayed join patch. */ | |
1386 | /* | |
1387 | * Go through all channels the client was on, rejoin him | |
1388 | * and set the modes, if any | |
1389 | */ | |
1390 | for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel) { | |
1391 | if (IsZombie(chan)) | |
1392 | continue; | |
1393 | sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, | |
1394 | "%H", chan->channel); | |
1395 | if (IsChanOp(chan) && HasVoice(chan)) { | |
1396 | sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, | |
1397 | "%H +ov %C %C", chan->channel, cptr, cptr); | |
1398 | } else if (IsChanOp(chan) || HasVoice(chan)) { | |
1399 | sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, | |
1400 | "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr); | |
1401 | } | |
1402 | } | |
1403 | #endif | |
1404 | ||
1405 | /* | |
1406 | * Go through all channels the client was on, rejoin him | |
1407 | * and set the modes, if any | |
1408 | */ | |
1409 | for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel) { | |
1410 | if (IsZombie(chan)) | |
1411 | continue; | |
1412 | /* If this channel has delayed joins and the user has no modes, just set | |
1413 | * the delayed join flag rather than showing the join, even if the user | |
1414 | * was visible before */ | |
1415 | if (!IsChanOp(chan) && !HasVoice(chan) | |
1416 | && (chan->channel->mode.mode & MODE_DELJOINS)) { | |
1417 | SetDelayedJoin(chan); | |
1418 | } else { | |
1419 | sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0, | |
1420 | "%H", chan->channel); | |
1421 | } | |
1422 | if (IsChanOp(chan) && HasVoice(chan)) { | |
1423 | sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, 0, | |
1424 | "%H +ov %C %C", chan->channel, cptr, cptr); | |
1425 | } else if (IsChanOp(chan) || HasVoice(chan)) { | |
1426 | sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, 0, | |
1427 | "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr); | |
1428 | } | |
1429 | } | |
1430 | return 1; | |
1431 | } | |
1432 | ||
189935b1 | 1433 | /** Set a user's mode. This function checks that \a cptr is trying to |
1434 | * set his own mode, prevents local users from setting inappropriate | |
1435 | * modes through this function, and applies any other side effects of | |
1436 | * a successful mode change. | |
1437 | * | |
1438 | * @param[in,out] cptr User setting someone's mode. | |
1439 | * @param[in] sptr Client who sent the mode change message. | |
1440 | * @param[in] parc Number of parameters in \a parv. | |
1441 | * @param[in] parv Parameters to MODE. | |
1442 | * @return Zero. | |
1443 | */ | |
1444 | int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[]) | |
1445 | { | |
1446 | char** p; | |
1447 | char* m; | |
1448 | struct Client *acptr; | |
1449 | int what; | |
1450 | int i; | |
1451 | struct Flags setflags; | |
1452 | unsigned int tmpmask = 0; | |
1453 | int snomask_given = 0; | |
1454 | char buf[BUFSIZE]; | |
1d4df40c | 1455 | char *hostmask, *password; |
189935b1 | 1456 | int prop = 0; |
1457 | int do_host_hiding = 0; | |
1d4df40c | 1458 | int do_set_host = 0; |
189935b1 | 1459 | |
1d4df40c | 1460 | hostmask = password = NULL; |
189935b1 | 1461 | what = MODE_ADD; |
1462 | ||
1463 | if (parc < 2) | |
1464 | return need_more_params(sptr, "MODE"); | |
1465 | ||
1466 | if (!(acptr = FindUser(parv[1]))) | |
1467 | { | |
1468 | if (MyConnect(sptr)) | |
1469 | send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]); | |
1470 | return 0; | |
1471 | } | |
1472 | ||
1473 | if (IsServer(sptr) || sptr != acptr) | |
1474 | { | |
1475 | if (IsServer(cptr)) | |
1476 | sendwallto_group_butone(&me, WALL_WALLOPS, 0, | |
1477 | "MODE for User %s from %s!%s", parv[1], | |
1478 | cli_name(cptr), cli_name(sptr)); | |
1479 | else | |
1480 | send_reply(sptr, ERR_USERSDONTMATCH); | |
1481 | return 0; | |
1482 | } | |
1483 | ||
1484 | if (parc < 3) | |
1485 | { | |
1486 | m = buf; | |
1487 | *m++ = '+'; | |
1488 | for (i = 0; i < USERMODELIST_SIZE; i++) | |
1489 | { | |
1490 | if (HasFlag(sptr, userModeList[i].flag) && | |
1d4df40c | 1491 | ((userModeList[i].flag != FLAG_ACCOUNT) && |
1492 | (userModeList[i].flag != FLAG_SETHOST))) | |
189935b1 | 1493 | *m++ = userModeList[i].c; |
1494 | } | |
1495 | *m = '\0'; | |
1496 | send_reply(sptr, RPL_UMODEIS, buf); | |
1497 | if (HasFlag(sptr, FLAG_SERVNOTICE) && MyConnect(sptr) | |
1498 | && cli_snomask(sptr) != | |
1499 | (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT)) | |
1500 | send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr)); | |
1501 | return 0; | |
1502 | } | |
1503 | ||
1504 | /* | |
1505 | * find flags already set for user | |
1506 | * why not just copy them? | |
1507 | */ | |
1508 | setflags = cli_flags(sptr); | |
1509 | ||
1510 | if (MyConnect(sptr)) | |
1511 | tmpmask = cli_snomask(sptr); | |
1512 | ||
1513 | /* | |
1514 | * parse mode change string(s) | |
1515 | */ | |
1516 | for (p = &parv[2]; *p; p++) { /* p is changed in loop too */ | |
1517 | for (m = *p; *m; m++) { | |
1518 | switch (*m) { | |
1519 | case '+': | |
1520 | what = MODE_ADD; | |
1521 | break; | |
1522 | case '-': | |
1523 | what = MODE_DEL; | |
1524 | break; | |
1525 | case 's': | |
1526 | if (*(p + 1) && is_snomask(*(p + 1))) { | |
1527 | snomask_given = 1; | |
1528 | tmpmask = umode_make_snomask(tmpmask, *++p, what); | |
1529 | tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER); | |
1530 | } | |
1531 | else | |
1532 | tmpmask = (what == MODE_ADD) ? | |
1533 | (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0; | |
1534 | if (tmpmask) | |
1535 | SetServNotice(sptr); | |
1536 | else | |
1537 | ClearServNotice(sptr); | |
1538 | break; | |
1539 | case 'w': | |
1540 | if (what == MODE_ADD) | |
1541 | SetWallops(sptr); | |
1542 | else | |
1543 | ClearWallops(sptr); | |
1544 | break; | |
1545 | case 'o': | |
1546 | if (what == MODE_ADD) | |
1547 | SetOper(sptr); | |
1548 | else { | |
1549 | ClrFlag(sptr, FLAG_OPER); | |
1550 | ClrFlag(sptr, FLAG_LOCOP); | |
1551 | if (MyConnect(sptr)) | |
1552 | { | |
1553 | tmpmask = cli_snomask(sptr) & ~SNO_OPER; | |
1554 | cli_handler(sptr) = CLIENT_HANDLER; | |
1555 | } | |
1556 | } | |
1557 | break; | |
1558 | case 'O': | |
1559 | if (what == MODE_ADD) | |
1560 | SetLocOp(sptr); | |
1561 | else | |
1562 | { | |
1563 | ClrFlag(sptr, FLAG_OPER); | |
1564 | ClrFlag(sptr, FLAG_LOCOP); | |
1565 | if (MyConnect(sptr)) | |
1566 | { | |
1567 | tmpmask = cli_snomask(sptr) & ~SNO_OPER; | |
1568 | cli_handler(sptr) = CLIENT_HANDLER; | |
1569 | } | |
1570 | } | |
1571 | break; | |
1572 | case 'i': | |
1573 | if (what == MODE_ADD) | |
1574 | SetInvisible(sptr); | |
1575 | else | |
fc04776e | 1576 | if (!feature_bool(FEAT_AUTOINVISIBLE) || IsOper(sptr)) /* Don't allow non-opers to -i if FEAT_AUTOINVISIBLE is set */ |
1577 | ClearInvisible(sptr); | |
189935b1 | 1578 | break; |
1579 | case 'd': | |
1580 | if (what == MODE_ADD) | |
1581 | SetDeaf(sptr); | |
1582 | else | |
1583 | ClearDeaf(sptr); | |
1584 | break; | |
1585 | case 'k': | |
1586 | if (what == MODE_ADD) | |
1587 | SetChannelService(sptr); | |
1588 | else | |
1589 | ClearChannelService(sptr); | |
1590 | break; | |
943fcf70 | 1591 | case 'X': |
1592 | if (what == MODE_ADD) | |
1593 | SetXtraOp(sptr); | |
1594 | else | |
1595 | ClearXtraOp(sptr); | |
1596 | break; | |
1597 | case 'n': | |
1598 | if (what == MODE_ADD) | |
1599 | SetNoChan(sptr); | |
1600 | else | |
1601 | ClearNoChan(sptr); | |
1602 | break; | |
1603 | case 'I': | |
1604 | if (what == MODE_ADD) | |
1605 | SetNoIdle(sptr); | |
1606 | else | |
1607 | ClearNoIdle(sptr); | |
1608 | break; | |
189935b1 | 1609 | case 'g': |
1610 | if (what == MODE_ADD) | |
1611 | SetDebug(sptr); | |
1612 | else | |
1613 | ClearDebug(sptr); | |
1614 | break; | |
1615 | case 'x': | |
1616 | if (what == MODE_ADD) | |
1d4df40c | 1617 | do_host_hiding = 1; |
1618 | break; | |
1619 | case 'h': | |
1620 | if (what == MODE_ADD) { | |
1621 | if (*(p + 1) && is_hostmask(*(p + 1))) { | |
1622 | do_set_host = 1; | |
1623 | hostmask = *++p; | |
1624 | /* DON'T step p onto the trailing NULL in the parameter array! - splidge */ | |
1625 | if (*(p+1)) | |
1626 | password = *++p; | |
1627 | else | |
1628 | password = NULL; | |
1629 | } else { | |
1630 | if (!*(p+1)) | |
1631 | send_reply(sptr, ERR_NEEDMOREPARAMS, "SETHOST"); | |
1632 | else { | |
1633 | send_reply(sptr, ERR_BADHOSTMASK, *(p+1)); | |
1634 | p++; /* Swallow the arg anyway */ | |
1635 | } | |
1636 | } | |
1637 | } else { /* MODE_DEL */ | |
1638 | do_set_host = 1; | |
1639 | hostmask = NULL; | |
1640 | password = NULL; | |
1641 | } | |
1642 | break; | |
08d23967 | 1643 | case 'R': |
1644 | if (what == MODE_ADD) | |
1645 | SetAccountOnly(sptr); | |
1646 | else | |
1647 | ClearAccountOnly(sptr); | |
1648 | break; | |
ebebfa5f | 1649 | case 'P': |
1650 | if (what == MODE_ADD) | |
1651 | SetParanoid(sptr); | |
1652 | else | |
1653 | ClearParanoid(sptr); | |
1654 | break; | |
189935b1 | 1655 | default: |
1656 | send_reply(sptr, ERR_UMODEUNKNOWNFLAG, *m); | |
1657 | break; | |
1658 | } | |
1659 | } | |
1660 | } | |
1661 | /* | |
1662 | * Evaluate rules for new user mode | |
1663 | * Stop users making themselves operators too easily: | |
1664 | */ | |
1665 | if (!IsServer(cptr)) | |
1666 | { | |
1667 | if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr)) | |
1668 | ClearOper(sptr); | |
1669 | if (!FlagHas(&setflags, FLAG_LOCOP) && IsLocOp(sptr)) | |
1670 | ClearLocOp(sptr); | |
1671 | /* | |
1672 | * new umode; servers can set it, local users cannot; | |
1673 | * prevents users from /kick'ing or /mode -o'ing | |
1674 | */ | |
943fcf70 | 1675 | if (!FlagHas(&setflags, FLAG_CHSERV) && !IsOper(sptr)) |
189935b1 | 1676 | ClearChannelService(sptr); |
943fcf70 | 1677 | if (!FlagHas(&setflags, FLAG_XTRAOP) && !IsOper(sptr)) |
1678 | ClearXtraOp(sptr); | |
1679 | if (!FlagHas(&setflags, FLAG_NOCHAN) && !(IsOper(sptr) || feature_bool(FEAT_USER_HIDECHANS))) | |
1680 | ClearNoChan(sptr); | |
1681 | if (!FlagHas(&setflags, FLAG_NOIDLE) && !IsOper(sptr)) | |
1682 | ClearNoIdle(sptr); | |
ebebfa5f | 1683 | if (!FlagHas(&setflags, FLAG_PARANOID) && !IsOper(sptr)) |
1684 | ClearParanoid(sptr); | |
943fcf70 | 1685 | |
189935b1 | 1686 | /* |
1687 | * only send wallops to opers | |
1688 | */ | |
1689 | if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) && | |
1690 | !FlagHas(&setflags, FLAG_WALLOP)) | |
1691 | ClearWallops(sptr); | |
1692 | if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY) && MyConnect(sptr) && | |
1693 | !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_SERVNOTICE)) | |
1694 | { | |
1695 | ClearServNotice(sptr); | |
1696 | set_snomask(sptr, 0, SNO_SET); | |
1697 | } | |
1698 | if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY) && | |
1699 | !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_DEBUG)) | |
1700 | ClearDebug(sptr); | |
1701 | } | |
1702 | if (MyConnect(sptr)) | |
1703 | { | |
1704 | if ((FlagHas(&setflags, FLAG_OPER) || FlagHas(&setflags, FLAG_LOCOP)) && | |
1705 | !IsAnOper(sptr)) | |
1706 | det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPERATOR); | |
1707 | ||
1708 | if (SendServNotice(sptr)) | |
1709 | { | |
1710 | if (tmpmask != cli_snomask(sptr)) | |
1711 | set_snomask(sptr, tmpmask, SNO_SET); | |
1712 | if (cli_snomask(sptr) && snomask_given) | |
1713 | send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr)); | |
1714 | } | |
1715 | else | |
1716 | set_snomask(sptr, 0, SNO_SET); | |
1717 | } | |
1718 | /* | |
1719 | * Compare new flags with old flags and send string which | |
1720 | * will cause servers to update correctly. | |
1721 | */ | |
1722 | if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr)) | |
1723 | { | |
1724 | /* user now oper */ | |
1725 | ++UserStats.opers; | |
1726 | client_set_privs(sptr, NULL); /* may set propagate privilege */ | |
1727 | } | |
1728 | /* remember propagate privilege setting */ | |
1729 | if (HasPriv(sptr, PRIV_PROPAGATE)) | |
1730 | prop = 1; | |
1731 | if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr)) | |
1732 | { | |
1733 | /* user no longer oper */ | |
1734 | --UserStats.opers; | |
1735 | client_set_privs(sptr, NULL); /* will clear propagate privilege */ | |
1736 | } | |
1737 | if (FlagHas(&setflags, FLAG_INVISIBLE) && !IsInvisible(sptr)) | |
1738 | --UserStats.inv_clients; | |
1739 | if (!FlagHas(&setflags, FLAG_INVISIBLE) && IsInvisible(sptr)) | |
1740 | ++UserStats.inv_clients; | |
1741 | if (!FlagHas(&setflags, FLAG_HIDDENHOST) && do_host_hiding) | |
1742 | hide_hostmask(sptr, FLAG_HIDDENHOST); | |
1d4df40c | 1743 | if (do_set_host) { |
1744 | /* We clear the flag in the old mask, so that the +h will be sent */ | |
1745 | /* Only do this if we're SETTING +h and it succeeded */ | |
1746 | if (set_hostmask(sptr, hostmask, password) && hostmask) | |
1747 | FlagClr(&setflags, FLAG_SETHOST); | |
1748 | } | |
189935b1 | 1749 | send_umode_out(cptr, sptr, &setflags, prop); |
1750 | ||
1751 | return 0; | |
1752 | } | |
1753 | ||
1754 | /** Build a mode string to describe modes for \a cptr. | |
1755 | * @param[in] cptr Some user. | |
1756 | * @return Pointer to a static buffer. | |
1757 | */ | |
1758 | char *umode_str(struct Client *cptr) | |
1759 | { | |
1760 | /* Maximum string size: "owidgrx\0" */ | |
1761 | char *m = umodeBuf; | |
1762 | int i; | |
1763 | struct Flags c_flags = cli_flags(cptr); | |
1764 | ||
1765 | if (!HasPriv(cptr, PRIV_PROPAGATE)) | |
1766 | FlagClr(&c_flags, FLAG_OPER); | |
1767 | ||
1768 | for (i = 0; i < USERMODELIST_SIZE; ++i) | |
1769 | { | |
1770 | if (FlagHas(&c_flags, userModeList[i].flag) && | |
1771 | userModeList[i].flag >= FLAG_GLOBAL_UMODES) | |
1772 | *m++ = userModeList[i].c; | |
1773 | } | |
1774 | ||
1775 | if (IsAccount(cptr)) | |
1776 | { | |
1777 | char* t = cli_user(cptr)->account; | |
1778 | ||
1779 | *m++ = ' '; | |
1780 | while ((*m++ = *t++)) | |
1781 | ; /* Empty loop */ | |
1782 | ||
1783 | if (cli_user(cptr)->acc_create) { | |
1784 | char nbuf[20]; | |
1785 | Debug((DEBUG_DEBUG, "Sending timestamped account in user mode for " | |
1786 | "account \"%s\"; timestamp %Tu", cli_user(cptr)->account, | |
1787 | cli_user(cptr)->acc_create)); | |
1788 | ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu", | |
1789 | cli_user(cptr)->acc_create); | |
1790 | m--; /* back up over previous nul-termination */ | |
1791 | while ((*m++ = *t++)) | |
1792 | ; /* Empty loop */ | |
1793 | } | |
1d4df40c | 1794 | m--; /* Step back over the '\0' */ |
189935b1 | 1795 | } |
1796 | ||
1d4df40c | 1797 | if (IsSetHost(cptr)) { |
1798 | *m++ = ' '; | |
1799 | ircd_snprintf(0, m, USERLEN + HOSTLEN + 2, "%s@%s", cli_user(cptr)->username, | |
1800 | cli_user(cptr)->host); | |
1801 | } else | |
1802 | *m = '\0'; | |
189935b1 | 1803 | return umodeBuf; /* Note: static buffer, gets |
1804 | overwritten by send_umode() */ | |
1805 | } | |
1806 | ||
1807 | /** Send a mode change string for \a sptr to \a cptr. | |
1808 | * @param[in] cptr Destination of mode change message. | |
1809 | * @param[in] sptr User whose mode has changed. | |
1810 | * @param[in] old Pre-change set of modes for \a sptr. | |
1811 | * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER, | |
1812 | * SEND_UMODES, to select which changed user modes to send. | |
1813 | */ | |
1814 | void send_umode(struct Client *cptr, struct Client *sptr, struct Flags *old, | |
1815 | int sendset) | |
1816 | { | |
1817 | int i; | |
1818 | int flag; | |
1d4df40c | 1819 | int needhost = 0; |
189935b1 | 1820 | char *m; |
1821 | int what = MODE_NULL; | |
1822 | ||
1823 | /* | |
1824 | * Build a string in umodeBuf to represent the change in the user's | |
1825 | * mode between the new (cli_flags(sptr)) and 'old', but skipping | |
1826 | * the modes indicated by sendset. | |
1827 | */ | |
1828 | m = umodeBuf; | |
1829 | *m = '\0'; | |
1830 | for (i = 0; i < USERMODELIST_SIZE; ++i) | |
1831 | { | |
1832 | flag = userModeList[i].flag; | |
1833 | if (FlagHas(old, flag) | |
1834 | == HasFlag(sptr, flag)) | |
1835 | continue; | |
1836 | switch (sendset) | |
1837 | { | |
1838 | case ALL_UMODES: | |
1839 | break; | |
1840 | case SEND_UMODES_BUT_OPER: | |
1841 | if (flag == FLAG_OPER) | |
1842 | continue; | |
1843 | /* and fall through */ | |
1844 | case SEND_UMODES: | |
1845 | if (flag < FLAG_GLOBAL_UMODES) | |
1846 | continue; | |
1847 | break; | |
1848 | } | |
1d4df40c | 1849 | /* Special case for SETHOST.. */ |
1850 | if (flag == FLAG_SETHOST) { | |
1851 | /* Don't send to users */ | |
1852 | if (cptr && MyUser(cptr)) | |
1853 | continue; | |
1854 | ||
1855 | /* If we're setting +h, add the parameter later */ | |
1856 | if (!FlagHas(old, flag)) | |
1857 | needhost++; | |
1858 | } | |
189935b1 | 1859 | if (FlagHas(old, flag)) |
1860 | { | |
1861 | if (what == MODE_DEL) | |
1862 | *m++ = userModeList[i].c; | |
1863 | else | |
1864 | { | |
1865 | what = MODE_DEL; | |
1866 | *m++ = '-'; | |
1867 | *m++ = userModeList[i].c; | |
1868 | } | |
1869 | } | |
1870 | else /* !FlagHas(old, flag) */ | |
1871 | { | |
1872 | if (what == MODE_ADD) | |
1873 | *m++ = userModeList[i].c; | |
1874 | else | |
1875 | { | |
1876 | what = MODE_ADD; | |
1877 | *m++ = '+'; | |
1878 | *m++ = userModeList[i].c; | |
1879 | } | |
1880 | } | |
1881 | } | |
1d4df40c | 1882 | if (needhost) { |
1883 | *m++ = ' '; | |
1884 | ircd_snprintf(0, m, USERLEN + HOSTLEN + 1, "%s@%s", cli_user(sptr)->username, | |
1885 | cli_user(sptr)->host); | |
1886 | } else | |
1887 | *m = '\0'; | |
189935b1 | 1888 | if (*umodeBuf && cptr) |
1d4df40c | 1889 | sendcmdto_one(sptr, CMD_MODE, cptr, "%s %s", cli_name(sptr), umodeBuf); |
189935b1 | 1890 | } |
1891 | ||
1892 | /** | |
1893 | * Check to see if this resembles a sno_mask. It is if 1) there is | |
1894 | * at least one digit and 2) The first digit occurs before the first | |
1895 | * alphabetic character. | |
1896 | * @param[in] word Word to check for sno_mask-ness. | |
1897 | * @return Non-zero if \a word looks like a server notice mask; zero if not. | |
1898 | */ | |
1899 | int is_snomask(char *word) | |
1900 | { | |
1901 | if (word) | |
1902 | { | |
1903 | for (; *word; word++) | |
1904 | if (IsDigit(*word)) | |
1905 | return 1; | |
1906 | else if (IsAlpha(*word)) | |
1907 | return 0; | |
1908 | } | |
1909 | return 0; | |
1910 | } | |
1911 | ||
1d4df40c | 1912 | /* |
1913 | * Check to see if it resembles a valid hostmask. | |
1914 | */ | |
1915 | int is_hostmask(char *word) | |
1916 | { | |
1917 | int i = 0; | |
1918 | char *host; | |
1919 | ||
1920 | Debug((DEBUG_INFO, "is_hostmask() %s", word)); | |
1921 | ||
1922 | if (strlen(word) > (HOSTLEN + USERLEN + 1) || strlen(word) <= 0) | |
1923 | return 0; | |
1924 | ||
1925 | /* if a host is specified, make sure it's valid */ | |
1926 | host = strrchr(word, '@'); | |
1927 | if (host) { | |
1928 | if (strlen(++host) < 1) | |
1929 | return 0; | |
1930 | if (strlen(host) > HOSTLEN) | |
1931 | return 0; | |
1932 | } | |
1933 | ||
1934 | if (word) { | |
1935 | if ('@' == *word) /* no leading @'s */ | |
1936 | return 0; | |
1937 | ||
1938 | if ('#' == *word) { /* numeric index given? */ | |
1939 | for (word++; *word; word++) { | |
1940 | if (!IsDigit(*word)) | |
1941 | return 0; | |
1942 | } | |
1943 | return 1; | |
1944 | } | |
1945 | ||
1946 | /* normal hostmask, account for at most one '@' */ | |
1947 | for (; *word; word++) { | |
1948 | if ('@' == *word) { | |
1949 | i++; | |
1950 | continue; | |
1951 | } | |
1952 | if (!IsHostChar(*word)) | |
1953 | return 0; | |
1954 | } | |
1955 | return (1 < i) ? 0 : 1; /* no more than on '@' */ | |
1956 | } | |
1957 | return 0; | |
1958 | } | |
1959 | ||
1960 | /* | |
1961 | * IsVhost() - Check if given host is a valid spoofhost | |
1962 | * (ie: configured thru a S:line) | |
1963 | */ | |
1964 | static char *IsVhost(char *hostmask, int oper) | |
1965 | { | |
1966 | unsigned int i = 0, y = 0; | |
1967 | struct sline *sconf; | |
1968 | ||
1969 | Debug((DEBUG_INFO, "IsVhost() %s", hostmask)); | |
1970 | ||
1971 | if (EmptyString(hostmask)) | |
1972 | return NULL; | |
1973 | ||
1974 | /* spoofhost specified as index, ie: #27 */ | |
1975 | if ('#' == hostmask[0]) { | |
1976 | y = atoi(hostmask + 1); | |
1977 | for (i = 0, sconf = GlobalSList; sconf; sconf = sconf->next) { | |
1978 | if (!oper && EmptyString(sconf->passwd)) | |
1979 | continue; | |
1980 | if (y == ++i) | |
1981 | return sconf->spoofhost; | |
1982 | } | |
1983 | return NULL; | |
1984 | } | |
1985 | ||
1986 | /* spoofhost specified as host, ie: host.cc */ | |
1987 | for (sconf = GlobalSList; sconf; sconf = sconf->next) | |
1988 | if (strCasediff(hostmask, sconf->spoofhost) == 0) | |
1989 | return sconf->spoofhost; | |
1990 | ||
1991 | return NULL; | |
1992 | } | |
1993 | ||
1994 | /* | |
1995 | * IsVhostPass() - Check if given spoofhost has a password | |
1996 | * associated with it, and if, return the password (cleartext) | |
1997 | */ | |
1998 | static char *IsVhostPass(char *hostmask) | |
1999 | { | |
2000 | struct sline *sconf; | |
2001 | ||
2002 | Debug((DEBUG_INFO, "IsVhostPass() %s", hostmask)); | |
2003 | ||
2004 | if (EmptyString(hostmask)) | |
2005 | return NULL; | |
2006 | ||
2007 | for (sconf = GlobalSList; sconf; sconf = sconf->next) | |
2008 | if (strCasediff(hostmask, sconf->spoofhost) == 0) { | |
2009 | Debug((DEBUG_INFO, "sconf->passwd %s", sconf->passwd)); | |
2010 | return EmptyString(sconf->passwd) ? NULL : sconf->passwd; | |
2011 | } | |
2012 | ||
2013 | return NULL; | |
2014 | } | |
2015 | ||
189935b1 | 2016 | /** Update snomask \a oldmask according to \a arg and \a what. |
2017 | * @param[in] oldmask Original user mask. | |
2018 | * @param[in] arg Update string (either a number or '+'/'-' followed by a number). | |
2019 | * @param[in] what MODE_ADD if adding the mask. | |
2020 | * @return New value of service notice mask. | |
2021 | */ | |
2022 | unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what) | |
2023 | { | |
2024 | unsigned int sno_what; | |
2025 | unsigned int newmask; | |
2026 | if (*arg == '+') | |
2027 | { | |
2028 | arg++; | |
2029 | if (what == MODE_ADD) | |
2030 | sno_what = SNO_ADD; | |
2031 | else | |
2032 | sno_what = SNO_DEL; | |
2033 | } | |
2034 | else if (*arg == '-') | |
2035 | { | |
2036 | arg++; | |
2037 | if (what == MODE_ADD) | |
2038 | sno_what = SNO_DEL; | |
2039 | else | |
2040 | sno_what = SNO_ADD; | |
2041 | } | |
2042 | else | |
2043 | sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL; | |
2044 | /* pity we don't have strtoul everywhere */ | |
2045 | newmask = (unsigned int)atoi(arg); | |
2046 | if (sno_what == SNO_DEL) | |
2047 | newmask = oldmask & ~newmask; | |
2048 | else if (sno_what == SNO_ADD) | |
2049 | newmask |= oldmask; | |
2050 | return newmask; | |
2051 | } | |
2052 | ||
2053 | /** Remove \a cptr from the singly linked list \a list. | |
2054 | * @param[in] cptr Client to remove from list. | |
2055 | * @param[in,out] list Pointer to head of list containing \a cptr. | |
2056 | */ | |
2057 | static void delfrom_list(struct Client *cptr, struct SLink **list) | |
2058 | { | |
2059 | struct SLink* tmp; | |
2060 | struct SLink* prv = NULL; | |
2061 | ||
2062 | for (tmp = *list; tmp; tmp = tmp->next) { | |
2063 | if (tmp->value.cptr == cptr) { | |
2064 | if (prv) | |
2065 | prv->next = tmp->next; | |
2066 | else | |
2067 | *list = tmp->next; | |
2068 | free_link(tmp); | |
2069 | break; | |
2070 | } | |
2071 | prv = tmp; | |
2072 | } | |
2073 | } | |
2074 | ||
2075 | /** Set \a cptr's server notice mask, according to \a what. | |
2076 | * @param[in,out] cptr Client whose snomask is updating. | |
2077 | * @param[in] newmask Base value for new snomask. | |
2078 | * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation. | |
2079 | */ | |
2080 | void set_snomask(struct Client *cptr, unsigned int newmask, int what) | |
2081 | { | |
2082 | unsigned int oldmask, diffmask; /* unsigned please */ | |
2083 | int i; | |
2084 | struct SLink *tmp; | |
2085 | ||
2086 | oldmask = cli_snomask(cptr); | |
2087 | ||
2088 | if (what == SNO_ADD) | |
2089 | newmask |= oldmask; | |
2090 | else if (what == SNO_DEL) | |
2091 | newmask = oldmask & ~newmask; | |
2092 | else if (what != SNO_SET) /* absolute set, no math needed */ | |
2093 | sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what); | |
2094 | ||
2095 | newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER); | |
2096 | ||
2097 | diffmask = oldmask ^ newmask; | |
2098 | ||
2099 | for (i = 0; diffmask >> i; i++) { | |
2100 | if (((diffmask >> i) & 1)) | |
2101 | { | |
2102 | if (((newmask >> i) & 1)) | |
2103 | { | |
2104 | tmp = make_link(); | |
2105 | tmp->next = opsarray[i]; | |
2106 | tmp->value.cptr = cptr; | |
2107 | opsarray[i] = tmp; | |
2108 | } | |
2109 | else | |
2110 | /* not real portable :( */ | |
2111 | delfrom_list(cptr, &opsarray[i]); | |
2112 | } | |
2113 | } | |
2114 | cli_snomask(cptr) = newmask; | |
2115 | } | |
2116 | ||
2117 | /** Check whether \a sptr is allowed to send a message to \a acptr. | |
2118 | * If \a sptr is a remote user, it means some server has an outdated | |
2119 | * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back | |
2120 | * in the direction of \a sptr. Skip the check if \a sptr is a server. | |
2121 | * @param[in] sptr Client trying to send a message. | |
2122 | * @param[in] acptr Destination of message. | |
2123 | * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not. | |
2124 | */ | |
2125 | int is_silenced(struct Client *sptr, struct Client *acptr) | |
2126 | { | |
2127 | struct Ban *found; | |
2128 | struct User *user; | |
2129 | size_t buf_used, slen; | |
2130 | char buf[BUFSIZE]; | |
2131 | ||
2132 | if (IsServer(sptr) || !(user = cli_user(acptr)) | |
2133 | || !(found = find_ban(sptr, user->silence))) | |
2134 | return 0; | |
2135 | assert(!(found->flags & BAN_EXCEPTION)); | |
2136 | if (!MyConnect(sptr)) { | |
2137 | /* Buffer positive silence to send back. */ | |
2138 | buf_used = strlen(found->banstr); | |
2139 | memcpy(buf, found->banstr, buf_used); | |
2140 | /* Add exceptions to buffer. */ | |
2141 | for (found = user->silence; found; found = found->next) { | |
2142 | if (!(found->flags & BAN_EXCEPTION)) | |
2143 | continue; | |
2144 | slen = strlen(found->banstr); | |
2145 | if (buf_used + slen + 4 > 400) { | |
2146 | buf[buf_used] = '\0'; | |
2147 | sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf); | |
2148 | buf_used = 0; | |
2149 | } | |
2150 | if (buf_used) | |
2151 | buf[buf_used++] = ','; | |
2152 | buf[buf_used++] = '+'; | |
2153 | buf[buf_used++] = '~'; | |
2154 | memcpy(buf + buf_used, found->banstr, slen); | |
2155 | buf_used += slen; | |
2156 | } | |
2157 | /* Flush silence buffer. */ | |
2158 | if (buf_used) { | |
2159 | buf[buf_used] = '\0'; | |
2160 | sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf); | |
2161 | buf_used = 0; | |
2162 | } | |
2163 | } | |
2164 | return 1; | |
2165 | } | |
2166 | ||
2167 | /** Send RPL_ISUPPORT lines to \a cptr. | |
2168 | * @param[in] cptr Client to send ISUPPORT to. | |
2169 | * @return Zero. | |
2170 | */ | |
2171 | int | |
2172 | send_supported(struct Client *cptr) | |
2173 | { | |
2174 | char featurebuf[512]; | |
2175 | ||
2176 | ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES1, FEATURESVALUES1); | |
2177 | send_reply(cptr, RPL_ISUPPORT, featurebuf); | |
2178 | ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES2, FEATURESVALUES2); | |
2179 | send_reply(cptr, RPL_ISUPPORT, featurebuf); | |
2180 | ||
2181 | return 0; /* convenience return, if it's ever needed */ | |
2182 | } |