]> jfr.im git - solanum.git/blob - modules/m_services.c
Replace RPL_WHOISTEXT(337) with RPL_WHOISSPECIAL(320) (#419)
[solanum.git] / modules / m_services.c
1 /* modules/m_services.c
2 * Copyright (C) 2005 Lee Hardy <lee -at- leeh.co.uk>
3 * Copyright (C) 2005 ircd-ratbox development team
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * 1.Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2.Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3.The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "stdinc.h"
31
32 #include "send.h"
33 #include "channel.h"
34 #include "client.h"
35 #include "defaults.h"
36 #include "ircd.h"
37 #include "numeric.h"
38 #include "s_conf.h"
39 #include "s_newconf.h"
40 #include "s_serv.h"
41 #include "hash.h"
42 #include "msg.h"
43 #include "parse.h"
44 #include "modules.h"
45 #include "whowas.h"
46 #include "monitor.h"
47 #include "supported.h"
48
49 static const char services_desc[] = "Provides support for running a services daemon";
50
51 static int _modinit(void);
52 static void _moddeinit(void);
53
54 static void mark_services(void);
55 static void unmark_services(void);
56
57 static void me_su(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
58 static void me_login(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
59 static void me_rsfnc(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
60 static void me_nickdelay(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
61
62 static void h_svc_server_introduced(void *);
63 static void h_svc_whois(void *);
64 static void h_svc_stats(void *);
65 static void h_svc_conf_read_start(void *);
66 static void h_svc_conf_read_end(void *);
67
68 struct Message su_msgtab = {
69 "SU", 0, 0, 0, 0,
70 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_su, 2}, mg_ignore}
71 };
72 struct Message login_msgtab = {
73 "LOGIN", 0, 0, 0, 0,
74 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_login, 2}, mg_ignore}
75 };
76 struct Message rsfnc_msgtab = {
77 "RSFNC", 0, 0, 0, 0,
78 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_rsfnc, 4}, mg_ignore}
79 };
80 struct Message nickdelay_msgtab = {
81 "NICKDELAY", 0, 0, 0, 0,
82 {mg_unreg, mg_ignore, mg_ignore, mg_ignore, {me_nickdelay, 3}, mg_ignore}
83 };
84
85 mapi_clist_av1 services_clist[] = {
86 &su_msgtab, &login_msgtab, &rsfnc_msgtab, &nickdelay_msgtab, NULL
87 };
88 mapi_hfn_list_av1 services_hfnlist[] = {
89 { "doing_stats", h_svc_stats },
90 { "doing_whois", h_svc_whois },
91 { "doing_whois_global", h_svc_whois },
92 { "server_introduced", h_svc_server_introduced },
93 { "conf_read_start", h_svc_conf_read_start },
94 { "conf_read_end", h_svc_conf_read_end },
95 { NULL, NULL }
96 };
97
98 DECLARE_MODULE_AV2(services, _modinit, _moddeinit, services_clist, NULL, services_hfnlist, NULL, NULL, services_desc);
99
100 static int
101 _modinit(void)
102 {
103 mark_services();
104 add_isupport("FNC", isupport_string, "");
105 return 0;
106 }
107
108 static void
109 _moddeinit(void)
110 {
111 delete_isupport("FNC");
112 unmark_services();
113 }
114
115 static void
116 me_su(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
117 int parc, const char *parv[])
118 {
119 struct Client *target_p;
120
121 if(!(source_p->flags & FLAGS_SERVICE))
122 {
123 sendto_realops_snomask(SNO_GENERAL, L_ALL,
124 "Non-service server %s attempting to execute services-only command SU", source_p->name);
125 return;
126 }
127
128 if((target_p = find_client(parv[1])) == NULL)
129 return;
130
131 if(!target_p->user)
132 return;
133
134 if(EmptyString(parv[2]))
135 target_p->user->suser[0] = '\0';
136 else
137 rb_strlcpy(target_p->user->suser, parv[2], sizeof(target_p->user->suser));
138
139 sendto_common_channels_local(target_p, CLICAP_ACCOUNT_NOTIFY, NOCAPS, ":%s!%s@%s ACCOUNT %s",
140 target_p->name, target_p->username, target_p->host,
141 EmptyString(target_p->user->suser) ? "*" : target_p->user->suser);
142
143 if (MyClient(target_p))
144 {
145 if (EmptyString(target_p->user->suser))
146 sendto_one(target_p, form_str(RPL_LOGGEDOUT), me.name, target_p->name,
147 target_p->name, target_p->username, target_p->host);
148 else
149 sendto_one(target_p, form_str(RPL_LOGGEDIN), me.name, target_p->name,
150 target_p->name, target_p->username, target_p->host, parv[2], parv[2]);
151 }
152
153 invalidate_bancache_user(target_p);
154 }
155
156 static void
157 me_login(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
158 int parc, const char *parv[])
159 {
160 if(!IsPerson(source_p))
161 return;
162
163 rb_strlcpy(source_p->user->suser, parv[1], sizeof(source_p->user->suser));
164 }
165
166 /* me_rsfnc()
167 * parv[1] = current user nickname
168 * parv[2] = target nickname
169 * parv[3] = new nickts
170 * parv[4] = current nickts
171 * parv[5] = optional; 0 (don't override RESVs) or 1 (override RESVs)
172 */
173 static void
174 me_rsfnc(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
175 int parc, const char *parv[])
176 {
177 struct Client *target_p;
178 struct Client *exist_p;
179 time_t newts, curts;
180 struct nd_entry *nd;
181 char note[NAMELEN + 10];
182
183 if(!(source_p->flags & FLAGS_SERVICE))
184 {
185 sendto_realops_snomask(SNO_GENERAL, L_ALL,
186 "Non-service server %s attempting to execute services-only command RSFNC", source_p->name);
187 return;
188 }
189
190 if((target_p = find_person(parv[1])) == NULL)
191 return;
192
193 if(!MyClient(target_p))
194 return;
195
196 if(!clean_nick(parv[2], 0) || IsDigit(parv[2][0]))
197 return;
198
199 curts = atol(parv[4]);
200
201 /* if tsinfo is different from what it was when services issued the
202 * RSFNC, then we ignore it. This can happen when a client changes
203 * nicknames before the RSFNC arrives.. --anfl
204 */
205 if(target_p->tsinfo != curts)
206 return;
207
208 /* received a non-forced RSFNC for a nickname that is RESV.
209 * silently ignore it. ~jess
210 */
211 if(parc > 5 && atoi(parv[5]) == 0 && find_nick_resv(parv[2]))
212 return;
213
214 if((exist_p = find_named_client(parv[2])))
215 {
216 char buf[BUFSIZE];
217
218 /* this would be one hell of a race condition to trigger
219 * this one given the tsinfo check above, but its here for
220 * safety --anfl
221 */
222 if(target_p == exist_p)
223 goto doit;
224
225 if(MyClient(exist_p))
226 sendto_one(exist_p, ":%s KILL %s :(Nickname regained by services)",
227 me.name, exist_p->name);
228
229 exist_p->flags |= FLAGS_KILLED;
230 /* Do not send kills to servers for unknowns -- jilles */
231 if(IsClient(exist_p))
232 {
233 kill_client_serv_butone(NULL, exist_p, "%s (Nickname regained by services)",
234 me.name);
235 sendto_realops_snomask(SNO_SKILL, L_ALL,
236 "Nick collision due to services forced nick change on %s",
237 parv[2]);
238 }
239
240 snprintf(buf, sizeof(buf), "Killed (%s (Nickname regained by services))",
241 me.name);
242 exit_client(NULL, exist_p, &me, buf);
243 }
244
245 doit:
246 newts = atol(parv[3]);
247
248 /* timestamp is older than 15mins, ignore it */
249 if(newts < (rb_current_time() - 900))
250 newts = rb_current_time() - 900;
251
252 target_p->tsinfo = newts;
253
254 monitor_signoff(target_p);
255
256 invalidate_bancache_user(target_p);
257
258 sendto_realops_snomask(SNO_NCHANGE, L_ALL,
259 "Nick change: From %s to %s [%s@%s]",
260 target_p->name, parv[2], target_p->username,
261 target_p->host);
262
263 sendto_common_channels_local(target_p, NOCAPS, NOCAPS, ":%s!%s@%s NICK :%s",
264 target_p->name, target_p->username,
265 target_p->host, parv[2]);
266
267 whowas_add_history(target_p, 1);
268 sendto_server(NULL, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld",
269 use_id(target_p), parv[2], (long) target_p->tsinfo);
270
271 del_from_client_hash(target_p->name, target_p);
272
273 /* invalidate nick delay because we're forcing this nick to be used */
274 nd = rb_dictionary_retrieve(nd_dict, parv[2]);
275 if (nd != NULL)
276 free_nd_entry(nd);
277
278 rb_strlcpy(target_p->name, parv[2], NICKLEN);
279 add_to_client_hash(target_p->name, target_p);
280
281 monitor_signon(target_p);
282
283 del_all_accepts(target_p);
284
285 snprintf(note, sizeof(note), "Nick: %s", target_p->name);
286 rb_note(target_p->localClient->F, note);
287 }
288
289 /*
290 ** me_nickdelay
291 ** parv[1] = duration in seconds (0 to remove)
292 ** parv[2] = nick
293 */
294 static void
295 me_nickdelay(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
296 {
297 int duration;
298 struct nd_entry *nd;
299
300 if(!(source_p->flags & FLAGS_SERVICE))
301 {
302 sendto_realops_snomask(SNO_GENERAL, L_ALL,
303 "Non-service server %s attempting to execute services-only command NICKDELAY", source_p->name);
304 return;
305 }
306
307 duration = atoi(parv[1]);
308 if (duration <= 0)
309 {
310 nd = rb_dictionary_retrieve(nd_dict, parv[2]);
311 if (nd != NULL)
312 free_nd_entry(nd);
313 }
314 else
315 {
316 if (duration > 86400)
317 duration = 86400;
318 add_nd_entry(parv[2]);
319 nd = rb_dictionary_retrieve(nd_dict, parv[2]);
320 if (nd != NULL)
321 nd->expire = rb_current_time() + duration;
322 }
323 }
324
325 static void
326 h_svc_server_introduced(void *data)
327 {
328 hook_data_client *hdata = data;
329 rb_dlink_node *ptr;
330
331 RB_DLINK_FOREACH(ptr, service_list.head)
332 {
333 if(!irccmp((const char *) ptr->data, hdata->target->name))
334 {
335 hdata->target->flags |= FLAGS_SERVICE;
336 return;
337 }
338 }
339 }
340
341 static void
342 h_svc_whois(void *data_)
343 {
344 hook_data_client *data = data_;
345 char *p = data->target->user->suser;
346 if(!EmptyString(p))
347 {
348 /* Try to strip off any leading digits as this may be used to
349 * store both an ID number and an account name in one field.
350 * If only digits are present, leave as is.
351 */
352 while(IsDigit(*p))
353 p++;
354 if(*p == '\0')
355 p = data->target->user->suser;
356
357 sendto_one_numeric(data->client, RPL_WHOISLOGGEDIN,
358 form_str(RPL_WHOISLOGGEDIN),
359 data->target->name, p);
360 }
361 }
362
363 static void
364 h_svc_stats(void *data_)
365 {
366 hook_data_int *data = data_;
367 char statchar = (char) data->arg2;
368 rb_dlink_node *ptr;
369
370 if (statchar == 'U' && IsOperGeneral(data->client))
371 {
372 RB_DLINK_FOREACH(ptr, service_list.head)
373 {
374 sendto_one_numeric(data->client, RPL_STATSULINE,
375 form_str(RPL_STATSULINE),
376 (const char *)ptr->data, "*", "*", "s");
377 }
378 }
379 }
380
381 static void
382 h_svc_conf_read_start(void *dummy)
383 {
384 unmark_services();
385 }
386
387 static void
388 unmark_services(void)
389 {
390 struct Client *target_p;
391 rb_dlink_node *ptr;
392
393 RB_DLINK_FOREACH(ptr, global_serv_list.head)
394 {
395 target_p = ptr->data;
396
397 target_p->flags &= ~FLAGS_SERVICE;
398 }
399 }
400
401 static void
402 h_svc_conf_read_end(void *dummy)
403 {
404 mark_services();
405 }
406
407 static void
408 mark_services(void)
409 {
410 struct Client *target_p;
411 rb_dlink_node *ptr;
412
413 RB_DLINK_FOREACH(ptr, service_list.head)
414 {
415 target_p = find_server(NULL, (const char *)ptr->data);
416
417 if (target_p)
418 target_p->flags |= FLAGS_SERVICE;
419 }
420 }