]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * charybdis: an advanced Internet Relay Chat Daemon(ircd). | |
3 | * m_scan.c: Provides information about various targets on various topics | |
4 | * | |
5 | * Copyright (c) 2006 William Pitcock <nenolod -at- nenolod.net> | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions are | |
9 | * met: | |
10 | * | |
11 | * 1.Redistributions of source code must retain the above copyright notice, | |
12 | * this list of conditions and the following disclaimer. | |
13 | * 2.Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3.The name of the author may not be used to endorse or promote products | |
17 | * derived from this software without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
22 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
28 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
29 | * POSSIBILITY OF SUCH DAMAGE. | |
30 | * | |
31 | * $Id: m_scan.c 1853 2006-08-24 18:30:52Z jilles $ | |
32 | */ | |
33 | ||
34 | #include "stdinc.h" | |
35 | #include "class.h" | |
36 | #include "hook.h" | |
37 | #include "client.h" | |
38 | #include "hash.h" | |
39 | #include "common.h" | |
40 | #include "hash.h" | |
4562c604 | 41 | #include "match.h" |
212380e3 AC |
42 | #include "ircd.h" |
43 | #include "numeric.h" | |
212380e3 AC |
44 | #include "s_serv.h" |
45 | #include "s_conf.h" | |
46 | #include "s_newconf.h" | |
47 | #include "s_user.h" | |
48 | #include "send.h" | |
49 | #include "msg.h" | |
50 | #include "parse.h" | |
51 | #include "modules.h" | |
77d3d2db | 52 | #include "logger.h" |
212380e3 AC |
53 | |
54 | static int mo_scan(struct Client *, struct Client *, int, const char **); | |
55 | static int scan_umodes(struct Client *, struct Client *, int, const char **); | |
56 | /*static int scan_cmodes(struct Client *, struct Client *, int, const char **);*/ | |
57 | ||
58 | struct Message scan_msgtab = { | |
59 | "SCAN", 0, 0, 0, MFLG_SLOW, | |
60 | {mg_ignore, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_scan, 2}} | |
61 | }; | |
62 | ||
63 | mapi_clist_av1 scan_clist[] = { &scan_msgtab, NULL }; | |
64 | DECLARE_MODULE_AV1(scan, NULL, NULL, scan_clist, NULL, NULL, "$Revision: 1853 $"); | |
65 | ||
66 | typedef int (*scan_handler)(struct Client *, struct Client *, int, | |
67 | const char **); | |
68 | ||
69 | struct scan_cmd { | |
70 | const char *name; | |
71 | int operlevel; | |
72 | scan_handler handler; | |
73 | } scan_cmds[] = { | |
74 | {"UMODES", L_OPER, scan_umodes}, | |
75 | {NULL, 0, NULL} | |
76 | }; | |
77 | ||
78 | static const char *empty_sockhost = "255.255.255.255"; | |
79 | static const char *spoofed_sockhost = "0"; | |
80 | ||
81 | /* | |
82 | * m_scan | |
212380e3 AC |
83 | * parv[1] = options [or target] |
84 | * parv[2] = [target] | |
85 | */ | |
86 | static int | |
87 | mo_scan(struct Client *client_p, struct Client *source_p, int parc, | |
88 | const char *parv[]) | |
89 | { | |
90 | struct scan_cmd *sptr; | |
91 | ||
92 | for (sptr = scan_cmds; sptr->name != NULL; sptr++) | |
93 | { | |
94 | if (!irccmp(sptr->name, parv[1])) | |
95 | { | |
96 | if (sptr->operlevel == L_ADMIN && | |
97 | !IsOperAdmin(source_p)) | |
98 | return -1; | |
99 | else | |
100 | return sptr->handler(client_p, source_p, parc, parv); | |
101 | } | |
102 | } | |
103 | ||
104 | sendto_one_notice(source_p, ":*** %s is not an implemented SCAN target", | |
105 | parv[1]); | |
106 | ||
107 | return 0; | |
108 | } | |
109 | ||
110 | static int | |
111 | scan_umodes(struct Client *client_p, struct Client *source_p, int parc, | |
112 | const char *parv[]) | |
113 | { | |
114 | unsigned int allowed_umodes = 0, disallowed_umodes = 0; | |
115 | int what = MODE_ADD; | |
116 | int mode; | |
117 | int list_users = YES; | |
0b5cf476 | 118 | int list_max = 500; |
212380e3 AC |
119 | int list_count = 0, count = 0; |
120 | const char *mask = NULL; | |
121 | const char *c; | |
122 | struct Client *target_p; | |
5b96d9a6 AC |
123 | rb_dlink_list *target_list = &lclient_list; /* local clients only by default */ |
124 | rb_dlink_node *tn; | |
212380e3 AC |
125 | int i; |
126 | const char *sockhost; | |
127 | char buf[512]; | |
128 | ||
129 | if (parc < 3) | |
130 | { | |
131 | if (MyClient(source_p)) | |
132 | sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS), | |
133 | me.name, source_p->name, "SCAN UMODES"); | |
134 | ||
135 | return -1; | |
136 | } | |
137 | ||
94a84cdd JT |
138 | if (parv[2][0] != '+' && parv[2][0] != '-') |
139 | { | |
140 | sendto_one_notice(source_p, ":SCAN UMODES: umodes parameter must start with '+' or '-'"); | |
141 | return -1; | |
142 | } | |
143 | ||
212380e3 AC |
144 | for (c = parv[2]; *c; c++) |
145 | { | |
146 | switch(*c) | |
147 | { | |
148 | case '+': | |
149 | what = MODE_ADD; | |
150 | break; | |
151 | case '-': | |
152 | what = MODE_DEL; | |
153 | break; | |
154 | default: | |
155 | if ((mode = user_modes[(unsigned char) *c]) != 0) | |
156 | { | |
157 | if (what == MODE_ADD) | |
158 | allowed_umodes |= mode; | |
159 | else | |
160 | disallowed_umodes |= mode; | |
161 | } | |
162 | } | |
163 | } | |
164 | ||
165 | for (i = 3; i < parc; i++) | |
166 | { | |
167 | if (!irccmp(parv[i], "no-list")) | |
168 | list_users = NO; | |
169 | else if (!irccmp(parv[i], "list")) | |
170 | list_users = YES; | |
171 | else if (!irccmp(parv[i], "global")) | |
172 | target_list = &global_client_list; | |
173 | else if (i < (parc - 1)) | |
174 | { | |
175 | if (!irccmp(parv[i], "list-max")) | |
176 | list_max = atoi(parv[++i]); | |
177 | else if (!irccmp(parv[i], "mask")) | |
178 | mask = parv[++i]; | |
94a84cdd JT |
179 | else |
180 | { | |
181 | sendto_one_notice(source_p, ":SCAN UMODES: invalid parameters"); | |
182 | return -1; | |
183 | } | |
184 | } | |
185 | else | |
186 | { | |
187 | sendto_one_notice(source_p, ":SCAN UMODES: invalid parameters"); | |
188 | return -1; | |
212380e3 AC |
189 | } |
190 | } | |
5f291021 | 191 | if (target_list == &global_client_list && list_users) |
212380e3 AC |
192 | { |
193 | if (IsOperSpy(source_p)) | |
194 | { | |
195 | if (!ConfigFileEntry.operspy_dont_care_user_info) | |
196 | { | |
f427c8b0 | 197 | rb_strlcpy(buf, "UMODES", sizeof buf); |
212380e3 AC |
198 | for (i = 2; i < parc; i++) |
199 | { | |
1f9de103 VY |
200 | rb_strlcat(buf, " ", sizeof buf); |
201 | rb_strlcat(buf, parv[i], sizeof buf); | |
212380e3 AC |
202 | } |
203 | report_operspy(source_p, "SCAN", buf); | |
204 | } | |
205 | } | |
206 | else | |
207 | { | |
208 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
209 | me.name, source_p->name, "oper_spy"); | |
210 | return -1; | |
211 | } | |
212 | } | |
213 | ||
5b96d9a6 | 214 | RB_DLINK_FOREACH(tn, target_list->head) |
212380e3 AC |
215 | { |
216 | unsigned int working_umodes = 0; | |
217 | char maskbuf[BUFSIZE]; | |
218 | ||
219 | target_p = tn->data; | |
220 | ||
221 | if (!IsClient(target_p)) | |
222 | continue; | |
223 | ||
224 | if(EmptyString(target_p->sockhost)) | |
225 | sockhost = empty_sockhost; | |
226 | else if(!show_ip(source_p, target_p)) | |
227 | sockhost = spoofed_sockhost; | |
228 | else | |
229 | sockhost = target_p->sockhost; | |
230 | ||
231 | working_umodes = target_p->umodes; | |
232 | ||
233 | /* require that we have the allowed umodes... */ | |
234 | if ((working_umodes & allowed_umodes) != allowed_umodes) | |
235 | continue; | |
236 | ||
237 | /* require that we have NONE of the disallowed ones */ | |
238 | if ((working_umodes & disallowed_umodes) != 0) | |
239 | continue; | |
240 | ||
241 | if (mask != NULL) | |
242 | { | |
7cdb0a09 | 243 | rb_snprintf(maskbuf, BUFSIZE, "%s!%s@%s", |
212380e3 AC |
244 | target_p->name, target_p->username, target_p->host); |
245 | ||
246 | if (!match(mask, maskbuf)) | |
247 | continue; | |
248 | } | |
249 | ||
250 | if (list_users && (!list_max || (list_count < list_max))) | |
251 | { | |
252 | char modebuf[BUFSIZE]; | |
253 | char *m = modebuf; | |
254 | ||
255 | *m++ = '+'; | |
256 | ||
257 | for (i = 0; i < 128; i++) | |
258 | { | |
259 | if (target_p->umodes & user_modes[i]) | |
260 | *m++ = (char) i; | |
261 | } | |
262 | ||
263 | *m++ = '\0'; | |
264 | ||
265 | list_count++; | |
266 | ||
267 | sendto_one_numeric(source_p, RPL_SCANUMODES, | |
268 | form_str(RPL_SCANUMODES), | |
269 | target_p->name, target_p->username, | |
270 | target_p->host, sockhost, | |
271 | target_p->servptr->name, modebuf, | |
272 | target_p->info); | |
273 | } | |
274 | count++; | |
275 | } | |
276 | ||
277 | sendto_one_numeric(source_p, RPL_SCANMATCHED, | |
278 | form_str(RPL_SCANMATCHED), count); | |
279 | ||
280 | return 0; | |
281 | } |