]>
Commit | Line | Data |
---|---|---|
212380e3 | 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 | * | |
212380e3 | 31 | */ |
32 | ||
33 | #include "stdinc.h" | |
34 | #include "class.h" | |
35 | #include "hook.h" | |
36 | #include "client.h" | |
37 | #include "hash.h" | |
38 | #include "common.h" | |
39 | #include "hash.h" | |
13ae2f4b | 40 | #include "match.h" |
212380e3 | 41 | #include "ircd.h" |
42 | #include "numeric.h" | |
212380e3 | 43 | #include "s_serv.h" |
44 | #include "s_conf.h" | |
45 | #include "s_newconf.h" | |
46 | #include "s_user.h" | |
47 | #include "send.h" | |
48 | #include "msg.h" | |
49 | #include "parse.h" | |
50 | #include "modules.h" | |
51 | ||
52 | static int mo_scan(struct Client *, struct Client *, int, const char **); | |
53 | static int scan_umodes(struct Client *, struct Client *, int, const char **); | |
54 | /*static int scan_cmodes(struct Client *, struct Client *, int, const char **);*/ | |
55 | ||
56 | struct Message scan_msgtab = { | |
57 | "SCAN", 0, 0, 0, MFLG_SLOW, | |
58 | {mg_ignore, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_scan, 2}} | |
59 | }; | |
60 | ||
61 | mapi_clist_av1 scan_clist[] = { &scan_msgtab, NULL }; | |
62 | DECLARE_MODULE_AV1(scan, NULL, NULL, scan_clist, NULL, NULL, "$Revision: 1853 $"); | |
63 | ||
64 | typedef int (*scan_handler)(struct Client *, struct Client *, int, | |
65 | const char **); | |
66 | ||
67 | struct scan_cmd { | |
68 | const char *name; | |
69 | int operlevel; | |
70 | scan_handler handler; | |
71 | } scan_cmds[] = { | |
72 | {"UMODES", L_OPER, scan_umodes}, | |
73 | {NULL, 0, NULL} | |
74 | }; | |
75 | ||
76 | static const char *empty_sockhost = "255.255.255.255"; | |
77 | static const char *spoofed_sockhost = "0"; | |
78 | ||
79 | /* | |
80 | * m_scan | |
212380e3 | 81 | * parv[1] = options [or target] |
82 | * parv[2] = [target] | |
83 | */ | |
84 | static int | |
85 | mo_scan(struct Client *client_p, struct Client *source_p, int parc, | |
86 | const char *parv[]) | |
87 | { | |
88 | struct scan_cmd *sptr; | |
89 | ||
90 | for (sptr = scan_cmds; sptr->name != NULL; sptr++) | |
91 | { | |
92 | if (!irccmp(sptr->name, parv[1])) | |
93 | { | |
94 | if (sptr->operlevel == L_ADMIN && | |
95 | !IsOperAdmin(source_p)) | |
96 | return -1; | |
97 | else | |
98 | return sptr->handler(client_p, source_p, parc, parv); | |
99 | } | |
100 | } | |
101 | ||
102 | sendto_one_notice(source_p, ":*** %s is not an implemented SCAN target", | |
103 | parv[1]); | |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
108 | static int | |
109 | scan_umodes(struct Client *client_p, struct Client *source_p, int parc, | |
110 | const char *parv[]) | |
111 | { | |
112 | unsigned int allowed_umodes = 0, disallowed_umodes = 0; | |
113 | int what = MODE_ADD; | |
114 | int mode; | |
115 | int list_users = YES; | |
9e8b3b43 | 116 | int list_max = 500; |
212380e3 | 117 | int list_count = 0, count = 0; |
118 | const char *mask = NULL; | |
119 | const char *c; | |
120 | struct Client *target_p; | |
08d11e34 WP |
121 | rb_dlink_list *target_list = &lclient_list; /* local clients only by default */ |
122 | rb_dlink_node *tn; | |
212380e3 | 123 | int i; |
124 | const char *sockhost; | |
125 | char buf[512]; | |
126 | ||
127 | if (parc < 3) | |
128 | { | |
129 | if (MyClient(source_p)) | |
130 | sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS), | |
131 | me.name, source_p->name, "SCAN UMODES"); | |
132 | ||
133 | return -1; | |
134 | } | |
135 | ||
9f811fce JT |
136 | if (parv[2][0] != '+' && parv[2][0] != '-') |
137 | { | |
138 | sendto_one_notice(source_p, ":SCAN UMODES: umodes parameter must start with '+' or '-'"); | |
139 | return -1; | |
140 | } | |
141 | ||
212380e3 | 142 | for (c = parv[2]; *c; c++) |
143 | { | |
144 | switch(*c) | |
145 | { | |
146 | case '+': | |
147 | what = MODE_ADD; | |
148 | break; | |
149 | case '-': | |
150 | what = MODE_DEL; | |
151 | break; | |
152 | default: | |
153 | if ((mode = user_modes[(unsigned char) *c]) != 0) | |
154 | { | |
155 | if (what == MODE_ADD) | |
156 | allowed_umodes |= mode; | |
157 | else | |
158 | disallowed_umodes |= mode; | |
159 | } | |
160 | } | |
161 | } | |
162 | ||
163 | for (i = 3; i < parc; i++) | |
164 | { | |
165 | if (!irccmp(parv[i], "no-list")) | |
166 | list_users = NO; | |
167 | else if (!irccmp(parv[i], "list")) | |
168 | list_users = YES; | |
169 | else if (!irccmp(parv[i], "global")) | |
170 | target_list = &global_client_list; | |
171 | else if (i < (parc - 1)) | |
172 | { | |
173 | if (!irccmp(parv[i], "list-max")) | |
174 | list_max = atoi(parv[++i]); | |
175 | else if (!irccmp(parv[i], "mask")) | |
176 | mask = parv[++i]; | |
9f811fce JT |
177 | else |
178 | { | |
179 | sendto_one_notice(source_p, ":SCAN UMODES: invalid parameters"); | |
180 | return -1; | |
181 | } | |
182 | } | |
183 | else | |
184 | { | |
185 | sendto_one_notice(source_p, ":SCAN UMODES: invalid parameters"); | |
186 | return -1; | |
212380e3 | 187 | } |
188 | } | |
c80b76f0 | 189 | if (target_list == &global_client_list && list_users) |
212380e3 | 190 | { |
191 | if (IsOperSpy(source_p)) | |
192 | { | |
193 | if (!ConfigFileEntry.operspy_dont_care_user_info) | |
194 | { | |
907468c4 | 195 | rb_strlcpy(buf, "UMODES", sizeof buf); |
212380e3 | 196 | for (i = 2; i < parc; i++) |
197 | { | |
a64c5173 VY |
198 | rb_strlcat(buf, " ", sizeof buf); |
199 | rb_strlcat(buf, parv[i], sizeof buf); | |
212380e3 | 200 | } |
201 | report_operspy(source_p, "SCAN", buf); | |
202 | } | |
203 | } | |
204 | else | |
205 | { | |
206 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
207 | me.name, source_p->name, "oper_spy"); | |
208 | return -1; | |
209 | } | |
210 | } | |
211 | ||
08d11e34 | 212 | RB_DLINK_FOREACH(tn, target_list->head) |
212380e3 | 213 | { |
214 | unsigned int working_umodes = 0; | |
215 | char maskbuf[BUFSIZE]; | |
216 | ||
217 | target_p = tn->data; | |
218 | ||
219 | if (!IsClient(target_p)) | |
220 | continue; | |
221 | ||
222 | if(EmptyString(target_p->sockhost)) | |
223 | sockhost = empty_sockhost; | |
224 | else if(!show_ip(source_p, target_p)) | |
225 | sockhost = spoofed_sockhost; | |
226 | else | |
227 | sockhost = target_p->sockhost; | |
228 | ||
229 | working_umodes = target_p->umodes; | |
230 | ||
231 | /* require that we have the allowed umodes... */ | |
232 | if ((working_umodes & allowed_umodes) != allowed_umodes) | |
233 | continue; | |
234 | ||
235 | /* require that we have NONE of the disallowed ones */ | |
236 | if ((working_umodes & disallowed_umodes) != 0) | |
237 | continue; | |
238 | ||
239 | if (mask != NULL) | |
240 | { | |
581fa5c4 | 241 | rb_snprintf(maskbuf, BUFSIZE, "%s!%s@%s", |
212380e3 | 242 | target_p->name, target_p->username, target_p->host); |
243 | ||
244 | if (!match(mask, maskbuf)) | |
245 | continue; | |
246 | } | |
247 | ||
248 | if (list_users && (!list_max || (list_count < list_max))) | |
249 | { | |
250 | char modebuf[BUFSIZE]; | |
251 | char *m = modebuf; | |
252 | ||
253 | *m++ = '+'; | |
254 | ||
255 | for (i = 0; i < 128; i++) | |
256 | { | |
257 | if (target_p->umodes & user_modes[i]) | |
258 | *m++ = (char) i; | |
259 | } | |
260 | ||
261 | *m++ = '\0'; | |
262 | ||
263 | list_count++; | |
264 | ||
265 | sendto_one_numeric(source_p, RPL_SCANUMODES, | |
266 | form_str(RPL_SCANUMODES), | |
267 | target_p->name, target_p->username, | |
268 | target_p->host, sockhost, | |
269 | target_p->servptr->name, modebuf, | |
270 | target_p->info); | |
271 | } | |
272 | count++; | |
273 | } | |
274 | ||
275 | sendto_one_numeric(source_p, RPL_SCANMATCHED, | |
276 | form_str(RPL_SCANMATCHED), count); | |
277 | ||
278 | return 0; | |
279 | } |