2 * IRC - Internet Relay Chat, ircd/m_gline.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
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 1, or (at your option)
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * m_functions execute protocol messages on this server:
29 * cptr is always NON-NULL, pointing to a *LOCAL* client
30 * structure (with an open socket connected!). This
31 * identifies the physical socket where the message
32 * originated (or which caused the m_function to be
33 * executed--some m_functions may call others...).
35 * sptr is the source of the message, defined by the
36 * prefix part of the message if present. If not
37 * or prefix not found, then sptr==cptr.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
63 * 2) MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64 * *cannot* be a local connection, unless it's
65 * actually cptr!). [MyConnect(x) should probably
66 * be defined as (x == x->from) --msa ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
73 * parv[0], sender (prefix string), if not present
74 * this points to an empty string.
75 * parv[1]...parv[parc-1]
76 * pointers to additional parameters
77 * parv[parc] == NULL, *always*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
88 #include "ircd_features.h"
90 #include "ircd_reply.h"
91 #include "ircd_string.h"
101 /* #include <assert.h> -- Now using assert in ircd_log.h */
106 * ms_gline - server message handler
108 * parv[0] = Sender prefix
109 * parv[1] = Target: server numeric
110 * parv[2] = (+|-)<G-line mask>
112 * For other parameters, see doc/readme.gline.
115 ms_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
117 struct Client *acptr = 0;
118 struct Gline *agline = 0;
119 unsigned int flags = 0;
120 enum GlineAction action = GLINE_MODIFY;
121 time_t expire_off = 0, lastmod = 0, lifetime = 0;
122 char *mask = parv[2], *target = parv[1], *reason = "No reason", *tmp = 0;
125 return need_more_params(sptr, "GLINE");
128 flags |= GLINE_FORCE;
132 flags |= GLINE_OPERFORCE; /* assume oper had WIDE_GLINE */
135 switch (*mask) { /* handle +, -, <, and > */
136 case '+': /* activate the G-line */
137 action = GLINE_ACTIVATE;
141 case '-': /* deactivate the G-line */
142 action = GLINE_DEACTIVATE;
146 case '>': /* locally activate the G-line */
147 action = GLINE_LOCAL_ACTIVATE;
151 case '<': /* locally deactivate the G-line */
152 action = GLINE_LOCAL_DEACTIVATE;
157 /* Now, let's figure out if it's a local or global G-line */
158 if (action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE ||
159 (target[0] == '*' && target[1] == '\0'))
160 flags |= GLINE_GLOBAL;
162 flags |= GLINE_LOCAL;
164 /* now figure out if we need to resolve a server */
165 if ((action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE ||
166 (flags & GLINE_LOCAL)) && !(acptr = FindNServer(target)))
167 return 0; /* no such server, jump out */
169 /* If it's a local activate/deactivate and server isn't me, propagate it */
170 if ((action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE) &&
172 Debug((DEBUG_DEBUG, "I am forwarding a local change to a global gline "
173 "to a remote server; target %s, mask %s, operforce %s, action %s",
174 target, mask, flags & GLINE_OPERFORCE ? "YES" : "NO",
175 action == GLINE_LOCAL_ACTIVATE ? ">" : "<"));
177 sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%c%s", acptr,
178 flags & GLINE_OPERFORCE ? "!" : "",
179 action == GLINE_LOCAL_ACTIVATE ? ">" : "<", mask);
181 return 0; /* all done */
184 /* Next, try to find the G-line... */
185 if ((flags & GLINE_GLOBAL) || IsMe(acptr)) /* don't bother if it's not me! */
186 agline = gline_find(mask, flags | GLINE_ANY | GLINE_EXACT);
188 /* We now have all the pieces to tell us what we've got; let's put
189 * it all together and convert the rest of the arguments.
192 /* Handle the local G-lines first... */
193 if (flags & GLINE_LOCAL) {
196 /* normalize the action, first */
197 if (action == GLINE_LOCAL_ACTIVATE || action == GLINE_MODIFY)
198 action = GLINE_ACTIVATE;
199 else if (action == GLINE_LOCAL_DEACTIVATE)
200 action = GLINE_DEACTIVATE;
202 if (action == GLINE_ACTIVATE) { /* get expiration and reason */
203 if (parc < 5) /* check parameter count... */
204 return need_more_params(sptr, "GLINE");
206 expire_off = atoi(parv[3]); /* get expiration... */
207 reason = parv[parc - 1]; /* and reason */
210 if (agline) /* G-line already exists, so let's ignore it... */
213 /* OK, create the local G-line */
214 Debug((DEBUG_DEBUG, "I am creating a local G-line here; target %s, "
215 "mask %s, operforce %s, action %s, expire %Tu, reason: %s",
216 target, mask, flags & GLINE_OPERFORCE ? "YES" : "NO",
217 action == GLINE_ACTIVATE ? "+" : "-", expire_off, reason));
219 return gline_add(cptr, sptr, mask, reason, expire_off, lastmod,
220 lifetime, flags | GLINE_ACTIVE);
222 } else if (IsMe(acptr)) { /* destroying a local G-line */
223 if (!agline) /* G-line doesn't exist, so let's complain... */
224 return send_reply(sptr, ERR_NOSUCHGLINE, mask);
226 /* Let's now destroy the G-line */;
227 Debug((DEBUG_DEBUG, "I am destroying a local G-line here; target %s, "
228 "mask %s, operforce %s, action %s", target, mask,
229 flags & GLINE_OPERFORCE ? "YES" : "NO",
230 action == GLINE_ACTIVATE ? "+" : "-"));
232 return gline_destroy(cptr, sptr, agline);
235 /* OK, we've converted arguments; if it's not for us, forward */
236 /* UPDATE NOTE: Once all servers are updated to u2.10.12.11, the
237 * format string in this sendcmdto_one() may be updated to omit
238 * <lastmod> for GLINE_ACTIVATE and to omit <expire>, <lastmod>,
239 * and <reason> for GLINE_DEACTIVATE.
241 assert(!IsMe(acptr));
243 Debug((DEBUG_DEBUG, "I am forwarding a local G-line to a remote server; "
244 "target %s, mask %s, operforce %s, action %s, expire %Tu, "
245 "lastmod %Tu, reason: %s", target, mask,
246 flags & GLINE_OPERFORCE ? "YES" : "NO",
247 action == GLINE_ACTIVATE ? "+" : "-", expire_off, CurrentTime,
250 sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%c%s %Tu %Tu :%s",
251 acptr, flags & GLINE_OPERFORCE ? "!" : "",
252 action == GLINE_ACTIVATE ? '+' : '-', mask, expire_off,
253 CurrentTime, reason);
255 return 0; /* all done */
258 /* can't modify a G-line that doesn't exist, so remap to activate */
259 if (!agline && action == GLINE_MODIFY)
260 action = GLINE_ACTIVATE;
262 /* OK, let's figure out what other parameters we may have... */
264 case GLINE_LOCAL_ACTIVATE: /* locally activating a G-line */
265 case GLINE_LOCAL_DEACTIVATE: /* locally deactivating a G-line */
266 if (!agline) /* no G-line to locally activate or deactivate? */
267 return send_reply(sptr, ERR_NOSUCHGLINE, mask);
268 break; /* no additional parameters to manipulate */
270 case GLINE_ACTIVATE: /* activating a G-line */
271 case GLINE_DEACTIVATE: /* deactivating a G-line */
272 /* in either of these cases, we have at least a lastmod parameter */
274 return need_more_params(sptr, "GLINE");
275 else if (parc == 4) /* lastmod only form... */
276 lastmod = atoi(parv[3]);
278 case GLINE_MODIFY: /* modifying a G-line */
279 /* convert expire and lastmod, look for lifetime and reason */
280 if (parc > 4) { /* protect against fall-through from 4-param form */
282 return need_more_params(sptr, "GLINE");
284 expire_off = atoi(parv[3]); /* convert expiration and lastmod */
285 lastmod = atoi(parv[4]);
287 flags |= GLINE_EXPIRE; /* we have an expiration time update */
289 if (parc > 6) { /* no question, have a lifetime and reason */
290 lifetime = atoi(parv[5]);
291 reason = parv[parc - 1];
293 flags |= GLINE_LIFETIME | GLINE_REASON;
294 } else if (parc == 6) { /* either a lifetime or a reason */
295 if (!agline || /* gline creation, has to be the reason */
296 /* trial-convert as lifetime, and if it doesn't fully convert,
297 * it must be the reason */
298 ((lifetime = strtoul(parv[5], &tmp, 10)) && !*tmp)) {
302 flags |= GLINE_REASON; /* have a reason update */
304 flags |= GLINE_LIFETIME; /* have a lifetime update */
309 Debug((DEBUG_DEBUG, "I have a global G-line I am acting upon now; "
310 "target %s, mask %s, operforce %s, action %s, expire %Tu, "
311 "lastmod %Tu, lifetime %Tu, reason: %s; gline %s! (fields "
312 "present: %s %s %s)", target, mask,
313 flags & GLINE_OPERFORCE ? "YES" : "NO",
314 action == GLINE_ACTIVATE ? "+" :
315 (action == GLINE_DEACTIVATE ? "-" :
316 (action == GLINE_LOCAL_ACTIVATE ? ">" :
317 (action == GLINE_LOCAL_DEACTIVATE ? "<" : "(MODIFY)"))),
318 expire_off, lastmod, lifetime, reason,
319 agline ? "EXISTS" : "does not exist",
320 flags & GLINE_EXPIRE ? "expire" : "",
321 flags & GLINE_LIFETIME ? "lifetime" : "",
322 flags & GLINE_REASON ? "reason" : ""));
324 /* OK, at this point, we have converted all available parameters.
325 * Let's actually do the action!
328 return gline_modify(cptr, sptr, agline, action, reason, expire_off,
329 lastmod, lifetime, flags);
331 assert(action != GLINE_LOCAL_ACTIVATE);
332 assert(action != GLINE_LOCAL_DEACTIVATE);
333 assert(action != GLINE_MODIFY);
335 return gline_add(cptr, sptr, mask, reason, expire_off, lastmod, lifetime,
336 flags | ((action == GLINE_ACTIVATE) ? GLINE_ACTIVE : 0));
340 * mo_gline - oper message handler
342 * parv[0] = Sender prefix
343 * parv[1] = [[+|-]<G-line mask>]
345 * For other parameters, see doc/readme.gline.
348 mo_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
350 struct Client *acptr = 0;
351 struct Gline *agline = 0;
352 unsigned int flags = 0;
353 enum GlineAction action = GLINE_MODIFY;
354 time_t expire_off = 0;
355 char *mask = parv[1], *target = 0, *reason = 0;
358 return gline_list(sptr, 0);
363 if (HasPriv(sptr, PRIV_WIDE_GLINE))
364 flags |= GLINE_OPERFORCE;
367 switch (*mask) { /* handle +, -, <, and > */
368 case '+': /* activate the G-line */
369 action = GLINE_ACTIVATE;
373 case '-': /* deactivate the G-line */
374 action = GLINE_DEACTIVATE;
378 case '>': /* locally activate the G-line */
379 action = GLINE_LOCAL_ACTIVATE;
383 case '<': /* locally deactivate the G-line */
384 action = GLINE_LOCAL_DEACTIVATE;
389 /* OK, let's figure out the parameters... */
391 case GLINE_MODIFY: /* no specific action on the G-line... */
392 if (parc == 2) /* user wants a listing of a specific G-line */
393 return gline_list(sptr, mask);
394 else if (parc < 4) /* must have target and expire, minimum */
395 return need_more_params(sptr, "GLINE");
397 target = parv[2]; /* get the target... */
398 expire_off = atoi(parv[3]); /* and the expiration */
400 flags |= GLINE_EXPIRE; /* remember that we got an expire time */
402 if (parc > 4) { /* also got a reason... */
404 flags |= GLINE_REASON;
407 /* target is not global, interpolate action and require reason */
408 if (target[0] != '*' || target[1] != '\0') {
409 if (!reason) /* have to have a reason for this */
410 return need_more_params(sptr, "GLINE");
412 action = GLINE_ACTIVATE;
416 case GLINE_LOCAL_ACTIVATE: /* locally activate a G-line */
417 case GLINE_LOCAL_DEACTIVATE: /* locally deactivate a G-line */
418 if (parc > 2) /* if target is available, pick it */
422 case GLINE_ACTIVATE: /* activating/adding a G-line */
423 case GLINE_DEACTIVATE: /* deactivating/removing a G-line */
425 return need_more_params(sptr, "GLINE");
428 /* get expiration and target */
429 expire_off = atoi(parv[parc - 2]);
430 reason = parv[parc - 1];
432 flags |= GLINE_EXPIRE | GLINE_REASON; /* remember that we got 'em */
434 if (parc > 4) /* also have a target! */
437 target = parv[2]; /* target has to be present, and has to be '*' */
439 if (target[0] != '*' || target[1] != '\0')
440 return need_more_params(sptr, "GLINE");
445 /* Now let's figure out which is the target server */
446 if (!target) /* no target, has to be me... */
448 /* if it's not '*', look up the server */
449 else if ((target[0] != '*' || target[1] != '\0') &&
450 !(acptr = find_match_server(target)))
451 return send_reply(sptr, ERR_NOSUCHSERVER, target);
453 /* Now, is the G-line local or global? */
454 if (action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE ||
456 flags |= GLINE_GLOBAL;
457 else /* it's some form of local G-line */
458 flags |= GLINE_LOCAL;
460 /* If it's a local activate/deactivate and server isn't me, propagate it */
461 if ((action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE) &&
463 /* check for permissions... */
464 if (!feature_bool(FEAT_CONFIG_OPERCMDS))
465 return send_reply(sptr, ERR_DISABLED, "GLINE");
466 else if (!HasPriv(sptr, PRIV_GLINE))
467 return send_reply(sptr, ERR_NOPRIVILEGES);
469 Debug((DEBUG_DEBUG, "I am forwarding a local change to a global gline "
470 "to a remote server; target %s, mask %s, operforce %s, action %s",
471 cli_name(acptr), mask, flags & GLINE_OPERFORCE ? "YES" : "NO",
472 action == GLINE_LOCAL_ACTIVATE ? ">" : "<"));
474 sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%c%s", acptr,
475 flags & GLINE_OPERFORCE ? "!" : "",
476 action == GLINE_LOCAL_ACTIVATE ? ">" : "<", mask);
478 return 0; /* all done */
481 /* Next, try to find the G-line... */
482 if ((flags & GLINE_GLOBAL) || IsMe(acptr)) /* don't bother if it's not me! */
483 agline = gline_find(mask, flags | GLINE_ANY | GLINE_EXACT);
485 /* We now have all the pieces to tell us what we've got; let's put
486 * it all together and convert the rest of the arguments.
489 /* Handle the local G-lines first... */
490 if (flags & GLINE_LOCAL) {
493 /* normalize the action, first */
494 if (action == GLINE_LOCAL_ACTIVATE || action == GLINE_MODIFY)
495 action = GLINE_ACTIVATE;
496 else if (action == GLINE_LOCAL_DEACTIVATE)
497 action = GLINE_DEACTIVATE;
499 /* If it's not for us, forward */
500 /* UPDATE NOTE: Once all servers are updated to u2.10.12.11, the
501 * format string in this sendcmdto_one() may be updated to omit
502 * <lastmod> for GLINE_ACTIVATE and to omit <expire>, <lastmod>,
503 * and <reason> for GLINE_DEACTIVATE.
507 /* check for permissions... */
508 if (!feature_bool(FEAT_CONFIG_OPERCMDS))
509 return send_reply(sptr, ERR_DISABLED, "GLINE");
510 else if (!HasPriv(sptr, PRIV_GLINE))
511 return send_reply(sptr, ERR_NOPRIVILEGES);
513 Debug((DEBUG_DEBUG, "I am forwarding a local G-line to a remote "
514 "server; target %s, mask %s, operforce %s, action %s, "
515 "expire %Tu, reason %s", target, mask,
516 flags & GLINE_OPERFORCE ? "YES" : "NO",
517 action == GLINE_ACTIVATE ? "+" : "-", expire_off, reason));
519 sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%c%s %Tu %Tu :%s",
520 acptr, flags & GLINE_OPERFORCE ? "!" : "",
521 action == GLINE_ACTIVATE ? "+" : "-", mask, expire_off,
522 CurrentTime, reason);
524 return 0; /* all done */
527 /* check local G-line permissions... */
528 if (!HasPriv(sptr, PRIV_LOCAL_GLINE))
529 return send_reply(sptr, ERR_NOPRIVILEGES);
531 /* let's handle activation... */
532 if (action == GLINE_ACTIVATE) {
533 if (agline) /* G-line already exists, so let's ignore it... */
536 /* OK, create the local G-line */
537 Debug((DEBUG_DEBUG, "I am creating a local G-line here; target %s, "
538 "mask %s, operforce %s, action %s, expire %Tu, reason: %s",
539 target, mask, flags & GLINE_OPERFORCE ? "YES" : "NO",
540 action == GLINE_ACTIVATE ? "+" : "-", expire_off, reason));
542 return gline_add(cptr, sptr, mask, reason, expire_off, 0, 0,
543 flags | GLINE_ACTIVE);
544 } else { /* OK, it's a deactivation/destruction */
545 if (!agline) /* G-line doesn't exist, so let's complain... */
546 return send_reply(sptr, ERR_NOSUCHGLINE, mask);
548 /* Let's now destroy the G-line */
549 Debug((DEBUG_DEBUG, "I am destroying a local G-line here; target %s, "
550 "mask %s, operforce %s, action %s", target, mask,
551 flags & GLINE_OPERFORCE ? "YES" : "NO",
552 action == GLINE_ACTIVATE ? "+" : "-"));
554 return gline_destroy(cptr, sptr, agline);
558 /* can't modify a G-line that doesn't exist... */
560 (action == GLINE_MODIFY || action == GLINE_LOCAL_ACTIVATE ||
561 action == GLINE_LOCAL_DEACTIVATE))
562 return send_reply(sptr, ERR_NOSUCHGLINE, mask);
564 /* check for G-line permissions... */
565 if (action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE) {
566 /* only need local privileges for locally-limited status changes */
567 if (!HasPriv(sptr, PRIV_LOCAL_GLINE))
568 return send_reply(sptr, ERR_NOPRIVILEGES);
569 } else { /* global privileges required */
570 if (!feature_bool(FEAT_CONFIG_OPERCMDS))
571 return send_reply(sptr, ERR_DISABLED, "GLINE");
572 else if (!HasPriv(sptr, PRIV_GLINE))
573 return send_reply(sptr, ERR_NOPRIVILEGES);
576 Debug((DEBUG_DEBUG, "I have a global G-line I am acting upon now; "
577 "target %s, mask %s, operforce %s, action %s, expire %Tu, "
578 "reason: %s; gline %s! (fields present: %s %s)", target,
579 mask, flags & GLINE_OPERFORCE ? "YES" : "NO",
580 action == GLINE_ACTIVATE ? "+" :
581 (action == GLINE_DEACTIVATE ? "-" :
582 (action == GLINE_LOCAL_ACTIVATE ? ">" :
583 (action == GLINE_LOCAL_DEACTIVATE ? "<" : "(MODIFY)"))),
584 expire_off, reason, agline ? "EXISTS" : "does not exist",
585 flags & GLINE_EXPIRE ? "expire" : "",
586 flags & GLINE_REASON ? "reason" : ""));
588 if (agline) /* modifying an existing G-line */
589 return gline_modify(cptr, sptr, agline, action, reason, expire_off,
590 CurrentTime, 0, flags);
592 assert(action != GLINE_LOCAL_ACTIVATE);
593 assert(action != GLINE_LOCAL_DEACTIVATE);
594 assert(action != GLINE_MODIFY);
596 /* create a new G-line */
597 return gline_add(cptr, sptr, mask, reason, expire_off, CurrentTime, 0,
598 flags | ((action == GLINE_ACTIVATE) ? GLINE_ACTIVE : 0));
602 * m_gline - user message handler
604 * parv[0] = Sender prefix
605 * parv[1] = [<server name>]
609 m_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
612 return send_reply(sptr, ERR_NOSUCHGLINE, "");
614 return gline_list(sptr, parv[1]);