1 /*
2  *  ircd-ratbox: A slightly useful ircd.
3  *  m_whois.c: Shows who a user is.
4  *
5  *  Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6  *  Copyright (C) 1996-2002 Hybrid Development Team
7  *  Copyright (C) 2002-2012 ircd-ratbox development team
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 2 of the License, or
12  *  (at your option) 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
22  *  USA
23  *
24  *  $Id: m_whois.c 27442 2013-03-03 15:52:13Z jilles $
25  */
26 
27 #include "stdinc.h"
28 #include "struct.h"
29 #include "client.h"
30 #include "hash.h"
31 #include "channel.h"
32 #include "ircd.h"
33 #include "numeric.h"
34 #include "s_conf.h"
35 #include "s_serv.h"
36 #include "send.h"
37 #include "match.h"
38 #include "s_conf.h"
39 #include "s_log.h"
40 #include "parse.h"
41 #include "hook.h"
42 #include "modules.h"
43 #include "s_newconf.h"
44 
45 static void do_whois(struct Client *client_p, struct Client *source_p, int parc,
46 		     const char *parv[]);
47 static void single_whois(struct Client *source_p, struct Client *target_p, int operspy);
48 
49 static int m_whois(struct Client *, struct Client *, int, const char **);
50 static int ms_whois(struct Client *, struct Client *, int, const char **);
51 
52 struct Message whois_msgtab = {
53 	"WHOIS", 0, 0, 0, MFLG_SLOW,
54 	{mg_unreg, {m_whois, 2}, {ms_whois, 2}, mg_ignore, mg_ignore, {m_whois, 2}}
55 };
56 
57 int doing_whois_hook;
58 int doing_whois_global_hook;
59 
60 mapi_clist_av1 whois_clist[] = { &whois_msgtab, NULL };
61 
62 mapi_hlist_av1 whois_hlist[] = {
63 	{"doing_whois", &doing_whois_hook},
64 	{"doing_whois_global", &doing_whois_global_hook},
65 	{NULL, NULL}
66 };
67 
68 DECLARE_MODULE_AV1(whois, NULL, NULL, whois_clist, whois_hlist, NULL, "$Revision: 27442 $");
69 
70 /*
71  * m_whois
72  *      parv[0] = sender prefix
73  *      parv[1] = nickname masklist
74  */
75 static int
m_whois(struct Client * client_p,struct Client * source_p,int parc,const char * parv[])76 m_whois(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
77 {
78 	static time_t last_used = 0;
79 
80 	if(parc > 2)
81 	{
82 		if(EmptyString(parv[2]))
83 		{
84 			sendto_one(source_p, form_str(ERR_NONICKNAMEGIVEN),
85 				   me.name, source_p->name);
86 			return 0;
87 		}
88 
89 		if(!IsOper(source_p))
90 		{
91 			/* seeing as this is going across servers, we should limit it */
92 			if((last_used + ConfigFileEntry.pace_wait_simple) > rb_current_time())
93 			{
94 				sendto_one(source_p, form_str(RPL_LOAD2HI),
95 					   me.name, source_p->name, "WHOIS");
96 				sendto_one_numeric(source_p, RPL_ENDOFWHOIS,
97 						   form_str(RPL_ENDOFWHOIS), parv[1]);
98 				return 0;
99 			}
100 			else
101 				last_used = rb_current_time();
102 		}
103 
104 		if(hunt_server(client_p, source_p, ":%s WHOIS %s :%s", 1, parc, parv) !=
105 		   HUNTED_ISME)
106 			return 0;
107 
108 		parv[1] = parv[2];
109 
110 	}
111 	do_whois(client_p, source_p, parc, parv);
112 
113 	return 0;
114 }
115 
116 /*
117  * ms_whois
118  *      parv[0] = sender prefix
119  *      parv[1] = server to reply
120  *      parv[2] = nickname to whois
121  */
122 static int
ms_whois(struct Client * client_p,struct Client * source_p,int parc,const char * parv[])123 ms_whois(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
124 {
125 	struct Client *target_p;
126 
127 	/* note: early versions of ratbox allowed users to issue a remote
128 	 * whois with a blank parv[2], so we cannot treat it as a protocol
129 	 * violation. --anfl
130 	 */
131 	if(parc < 3 || EmptyString(parv[2]))
132 	{
133 		sendto_one(source_p, form_str(ERR_NONICKNAMEGIVEN), me.name, source_p->name);
134 		return 0;
135 	}
136 
137 
138 	/* check if parv[1] exists */
139 	if((target_p = find_client(parv[1])) == NULL)
140 	{
141 		sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
142 				   form_str(ERR_NOSUCHSERVER), IsDigit(parv[1][0]) ? "*" : parv[1]);
143 		return 0;
144 	}
145 
146 	/* if parv[1] isnt my client, or me, someone else is supposed
147 	 * to be handling the request.. so send it to them
148 	 */
149 	if(!MyClient(target_p) && !IsMe(target_p))
150 	{
151 		sendto_one(target_p, ":%s WHOIS %s :%s",
152 			   get_id(source_p, target_p), get_id(target_p, target_p), parv[2]);
153 		return 0;
154 	}
155 
156 	/* ok, the target is either us, or a client on our server, so perform the whois
157 	 * but first, parv[1] == server to perform the whois on, parv[2] == person
158 	 * to whois, so make parv[1] = parv[2] so do_whois is ok -- fl_
159 	 */
160 	parv[1] = parv[2];
161 	do_whois(client_p, source_p, parc, parv);
162 
163 	return 0;
164 }
165 
166 /* do_whois
167  *
168  * inputs	- pointer to
169  * output	-
170  * side effects -
171  */
172 static void
do_whois(struct Client * client_p,struct Client * source_p,int parc,const char * parv[])173 do_whois(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
174 {
175 	struct Client *target_p;
176 	char *nick;
177 	char *p = NULL;
178 	int operspy = 0;
179 
180 	nick = LOCAL_COPY(parv[1]);
181 	if((p = strchr(parv[1], ',')))
182 		*p = '\0';
183 
184 	if(IsOperSpy(source_p) && *nick == '!')
185 	{
186 		operspy = 1;
187 		nick++;
188 	}
189 
190 	target_p = find_named_person(nick);
191 	SetCork(source_p);
192 	if(target_p != NULL)
193 	{
194 		if(operspy)
195 		{
196 			char buffer[BUFSIZE];
197 
198 			rb_snprintf(buffer, sizeof(buffer), "%s!%s@%s %s",
199 				    target_p->name, target_p->username,
200 				    target_p->host, target_p->servptr->name);
201 			report_operspy(source_p, "WHOIS", buffer);
202 		}
203 
204 		single_whois(source_p, target_p, operspy);
205 	}
206 	else
207 		sendto_one_numeric(source_p, ERR_NOSUCHNICK,
208 				   form_str(ERR_NOSUCHNICK), IsDigit(*nick) ? "*" : parv[1]);
209 	ClearCork(source_p);
210 	sendto_one_numeric(source_p, RPL_ENDOFWHOIS, form_str(RPL_ENDOFWHOIS), parv[1]);
211 	return;
212 }
213 
214 /*
215  * single_whois()
216  *
217  * Inputs	- source_p client to report to
218  *		- target_p client to report on
219  * Output	- if found return 1
220  * Side Effects	- do a single whois on given client
221  * 		  writing results to source_p
222  */
223 static void
single_whois(struct Client * source_p,struct Client * target_p,int operspy)224 single_whois(struct Client *source_p, struct Client *target_p, int operspy)
225 {
226 	char buf[BUFSIZE];
227 	rb_dlink_node *ptr;
228 	struct Client *a2client_p;
229 	struct membership *msptr;
230 	struct Channel *chptr;
231 	int cur_len = 0;
232 	int mlen;
233 	char *t;
234 	int tlen;
235 	hook_data_client hdata;
236 	const char *name;
237 	char quest[] = "?";
238 	int visible;
239 	int extra_space = 0;
240 
241 	if(target_p->name)
242 		name = quest;
243 	else
244 		name = target_p->name;
245 
246 	if(target_p->user == NULL)
247 	{
248 		s_assert(0);
249 		return;
250 	}
251 
252 	a2client_p = target_p->servptr;
253 
254 	sendto_one_numeric(source_p, RPL_WHOISUSER, form_str(RPL_WHOISUSER),
255 			   target_p->name, target_p->username, target_p->host, target_p->info);
256 
257 	cur_len = mlen = rb_sprintf(buf, form_str(RPL_WHOISCHANNELS),
258 				    get_id(&me, source_p), get_id(source_p, source_p),
259 				    target_p->name);
260 	/* Make sure it won't overflow when sending it to the client
261 	 * in full names; note that serverhiding may require more space
262 	 * for a different server name (not done here) -- jilles */
263 	if(!MyConnect(source_p))
264 	{
265 		extra_space = strlen(source_p->name) - 9;
266 		if(extra_space < 0)
267 			extra_space = 0;
268 		extra_space += strlen(me.name) - 2;	/* make sure >= 0 */
269 		cur_len += extra_space;
270 	}
271 
272 	t = buf + mlen;
273 
274 #ifdef ENABLE_SERVICES
275 	if(!IsService(target_p))
276 #endif
277 	{
278 		RB_DLINK_FOREACH(ptr, target_p->user->channel.head)
279 		{
280 			msptr = ptr->data;
281 			chptr = msptr->chptr;
282 
283 			visible = ShowChannel(source_p, chptr);
284 
285 			if(visible || operspy)
286 			{
287 				if((cur_len + strlen(chptr->chname) + 3) > (BUFSIZE - 5))
288 				{
289 					sendto_one_buffer(source_p, buf);
290 					cur_len = mlen + extra_space;
291 					t = buf + mlen;
292 				}
293 
294 				tlen = rb_sprintf(t, "%s%s%s ",
295 						  visible ? "" : "!",
296 						  find_channel_status(msptr, 1), chptr->chname);
297 				t += tlen;
298 				cur_len += tlen;
299 			}
300 		}
301 	}
302 
303 	if(cur_len > mlen + extra_space)
304 		sendto_one_buffer(source_p, buf);
305 
306 	sendto_one_numeric(source_p, RPL_WHOISSERVER, form_str(RPL_WHOISSERVER),
307 			   target_p->name, target_p->servptr->name,
308 			   a2client_p ? a2client_p->info : "*Not On This Net*");
309 
310 	if(target_p->user->away)
311 		sendto_one_numeric(source_p, RPL_AWAY, form_str(RPL_AWAY),
312 				   target_p->name, target_p->user->away);
313 
314 	if(IsOper(target_p))
315 	{
316 		sendto_one_numeric(source_p, RPL_WHOISOPERATOR, form_str(RPL_WHOISOPERATOR),
317 				   target_p->name,
318 				   IsAdmin(target_p) ? GlobalSetOptions.adminstring :
319 				   GlobalSetOptions.operstring);
320 	}
321 
322 	if(MyClient(target_p))
323 	{
324 		if(IsSSL(target_p))
325 			sendto_one_numeric(source_p, RPL_WHOISSECURE,
326 					   form_str(RPL_WHOISSECURE), target_p->name);
327 
328 		if(ConfigFileEntry.use_whois_actually && show_ip(source_p, target_p))
329 			sendto_one_numeric(source_p, RPL_WHOISACTUALLY,
330 					   form_str(RPL_WHOISACTUALLY),
331 					   target_p->name, target_p->sockhost);
332 
333 		sendto_one_numeric(source_p, RPL_WHOISIDLE, form_str(RPL_WHOISIDLE),
334 				   target_p->name,
335 				   rb_current_time() - target_p->localClient->last,
336 				   target_p->localClient->firsttime);
337 	}
338 	else
339 	{
340 		if(ConfigFileEntry.use_whois_actually && show_ip(source_p, target_p) &&
341 		   !EmptyString(target_p->sockhost) && strcmp(target_p->sockhost, "0"))
342 		{
343 			sendto_one_numeric(source_p, RPL_WHOISACTUALLY,
344 					   form_str(RPL_WHOISACTUALLY),
345 					   target_p->name, target_p->sockhost);
346 
347 		}
348 
349 	}
350 
351 	send_pop_queue(source_p);
352 	hdata.client = source_p;
353 	hdata.target = target_p;
354 
355 	/* doing_whois_hook must only be called for local clients,
356 	 * doing_whois_global_hook must only be called for local targets
357 	 */
358 	/* it is important that these are called *before* RPL_ENDOFWHOIS is
359 	 * sent, services compatibility code depends on it. --anfl
360 	 */
361 	if(MyClient(source_p))
362 		call_hook(doing_whois_hook, &hdata);
363 	else
364 		call_hook(doing_whois_global_hook, &hdata);
365 
366 	return;
367 }
368