1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 2000-2018 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 m_accept.c |
23 |
* \brief Includes required functions for processing the ACCEPT command. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "client.h" |
29 |
#include "irc_string.h" |
30 |
#include "ircd.h" |
31 |
#include "list.h" |
32 |
#include "numeric.h" |
33 |
#include "conf.h" |
34 |
#include "send.h" |
35 |
#include "parse.h" |
36 |
#include "modules.h" |
37 |
#include "memory.h" |
38 |
|
39 |
|
40 |
/*! \brief Creates and sends a list of nick!user\@host masks a Client |
41 |
* has on its acceptlist. |
42 |
* |
43 |
* \param source_p The actual Client the list will be sent to. |
44 |
*/ |
45 |
static void |
46 |
list_accepts(struct Client *source_p) |
47 |
{ |
48 |
char nicks[IRCD_BUFSIZE] = ""; |
49 |
char *t = nicks; |
50 |
dlink_node *node; |
51 |
|
52 |
/* :me.name 281 source_p->name :n1!u1@h1 n2!u2@h2 ...\r\n */ |
53 |
/* 1 23456 78 9 10 */ |
54 |
int len = strlen(me.name) + strlen(source_p->name) + 10; |
55 |
|
56 |
DLINK_FOREACH(node, source_p->connection->acceptlist.head) |
57 |
{ |
58 |
const struct split_nuh_item *accept_p = node->data; |
59 |
size_t masklen = strlen(accept_p->nickptr) + |
60 |
strlen(accept_p->userptr) + |
61 |
strlen(accept_p->hostptr) + 3; /* +3 for ! + @ + space */ |
62 |
|
63 |
if ((t - nicks) + masklen + len > IRCD_BUFSIZE) |
64 |
{ |
65 |
*(t - 1) = '\0'; |
66 |
sendto_one_numeric(source_p, &me, RPL_ACCEPTLIST, nicks); |
67 |
t = nicks; |
68 |
} |
69 |
|
70 |
t += sprintf(t, "%s!%s@%s ", |
71 |
accept_p->nickptr, |
72 |
accept_p->userptr, accept_p->hostptr); |
73 |
} |
74 |
|
75 |
if (nicks[0]) |
76 |
{ |
77 |
*(t - 1) = '\0'; |
78 |
sendto_one_numeric(source_p, &me, RPL_ACCEPTLIST, nicks); |
79 |
} |
80 |
|
81 |
sendto_one_numeric(source_p, &me, RPL_ENDOFACCEPT); |
82 |
} |
83 |
|
84 |
/*! \brief Allocates and adds a split_nuh_item holding a nick!user\@host |
85 |
* mask to a Client's acceptlist. |
86 |
* |
87 |
* \param nuh A split_nuh_item already prepared with required masks. |
88 |
* \param source_p The actual Client the new accept is added to. |
89 |
*/ |
90 |
static void |
91 |
add_accept(const struct split_nuh_item *nuh, struct Client *source_p) |
92 |
{ |
93 |
struct split_nuh_item *accept_p = xcalloc(sizeof(*accept_p)); |
94 |
|
95 |
accept_p->nickptr = xstrdup(nuh->nickptr); |
96 |
accept_p->userptr = xstrdup(nuh->userptr); |
97 |
accept_p->hostptr = xstrdup(nuh->hostptr); |
98 |
|
99 |
dlinkAdd(accept_p, &accept_p->node, &source_p->connection->acceptlist); |
100 |
|
101 |
list_accepts(source_p); |
102 |
} |
103 |
|
104 |
/*! \brief ACCEPT command handler |
105 |
* |
106 |
* \param source_p Pointer to allocated Client struct from which the message |
107 |
* originally comes from. This can be a local or remote client. |
108 |
* \param parc Integer holding the number of supplied arguments. |
109 |
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL |
110 |
* pointers. |
111 |
* \note Valid arguments for this command are: |
112 |
* - parv[0] = command |
113 |
* - parv[1] = comma-separated list of masks to be accepted or removed |
114 |
*/ |
115 |
static int |
116 |
m_accept(struct Client *source_p, int parc, char *parv[]) |
117 |
{ |
118 |
struct split_nuh_item nuh; |
119 |
struct split_nuh_item *accept_p = NULL; |
120 |
char nick[NICKLEN + 1] = ""; |
121 |
char user[USERLEN + 1] = ""; |
122 |
char host[HOSTLEN + 1] = ""; |
123 |
char *p = NULL; |
124 |
char *mask = collapse(parv[1]); |
125 |
|
126 |
if (EmptyString(mask) || !strcmp(mask, "*")) |
127 |
{ |
128 |
list_accepts(source_p); |
129 |
return 0; |
130 |
} |
131 |
|
132 |
for (mask = strtok_r(mask, ",", &p); mask; |
133 |
mask = strtok_r(NULL, ",", &p)) |
134 |
{ |
135 |
if (*mask == '-' && *++mask) |
136 |
{ |
137 |
nuh.nuhmask = mask; |
138 |
nuh.nickptr = nick; |
139 |
nuh.userptr = user; |
140 |
nuh.hostptr = host; |
141 |
|
142 |
nuh.nicksize = sizeof(nick); |
143 |
nuh.usersize = sizeof(user); |
144 |
nuh.hostsize = sizeof(host); |
145 |
|
146 |
split_nuh(&nuh); |
147 |
|
148 |
if ((accept_p = find_accept(nick, user, host, source_p, irccmp)) == NULL) |
149 |
{ |
150 |
sendto_one_numeric(source_p, &me, ERR_ACCEPTNOT, nick, user, host); |
151 |
continue; |
152 |
} |
153 |
|
154 |
del_accept(accept_p, source_p); |
155 |
} |
156 |
else if (*mask) |
157 |
{ |
158 |
if (dlink_list_length(&source_p->connection->acceptlist) >= ConfigGeneral.max_accept) |
159 |
{ |
160 |
sendto_one_numeric(source_p, &me, ERR_ACCEPTFULL); |
161 |
return 0; |
162 |
} |
163 |
|
164 |
nuh.nuhmask = mask; |
165 |
nuh.nickptr = nick; |
166 |
nuh.userptr = user; |
167 |
nuh.hostptr = host; |
168 |
|
169 |
nuh.nicksize = sizeof(nick); |
170 |
nuh.usersize = sizeof(user); |
171 |
nuh.hostsize = sizeof(host); |
172 |
|
173 |
split_nuh(&nuh); |
174 |
|
175 |
if ((accept_p = find_accept(nick, user, host, source_p, irccmp))) |
176 |
{ |
177 |
sendto_one_numeric(source_p, &me, ERR_ACCEPTEXIST, nick, user, host); |
178 |
continue; |
179 |
} |
180 |
|
181 |
add_accept(&nuh, source_p); |
182 |
} |
183 |
} |
184 |
|
185 |
return 0; |
186 |
} |
187 |
|
188 |
static struct Message accept_msgtab = |
189 |
{ |
190 |
.cmd = "ACCEPT", |
191 |
.args_max = MAXPARA, |
192 |
.handlers[UNREGISTERED_HANDLER] = m_unregistered, |
193 |
.handlers[CLIENT_HANDLER] = m_accept, |
194 |
.handlers[SERVER_HANDLER] = m_ignore, |
195 |
.handlers[ENCAP_HANDLER] = m_ignore, |
196 |
.handlers[OPER_HANDLER] = m_accept |
197 |
}; |
198 |
|
199 |
static void |
200 |
module_init(void) |
201 |
{ |
202 |
mod_add_cmd(&accept_msgtab); |
203 |
} |
204 |
|
205 |
static void |
206 |
module_exit(void) |
207 |
{ |
208 |
mod_del_cmd(&accept_msgtab); |
209 |
} |
210 |
|
211 |
struct module module_entry = |
212 |
{ |
213 |
.version = "$Revision$", |
214 |
.modinit = module_init, |
215 |
.modexit = module_exit, |
216 |
}; |