2 * IRC - Internet Relay Chat, ircd/features.c
3 * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 1, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "ircd_features.h"
26 #include "ircd_alloc.h"
28 #include "ircd_reply.h"
29 #include "ircd_string.h"
34 #include "random.h" /* random_seed_set */
41 #include "sys.h" /* FALSE bleah */
47 /* List of log output types that can be set */
48 static struct LogTypes {
50 int (*set)(const char *, const char *);
51 char *(*get)(const char *);
53 { "FILE", log_set_file, log_get_file },
54 { "FACILITY", log_set_facility, log_get_facility },
55 { "SNOMASK", log_set_snomask, log_get_snomask },
56 { "LEVEL", log_set_level, log_get_level },
60 /* Look up a struct LogType given the type string */
61 static struct LogTypes *
62 feature_log_desc(struct Client* from, const char *type)
68 for (i = 0; logTypes[i].type; i++) /* find appropriate descriptor */
69 if (!ircd_strcmp(type, logTypes[i].type))
72 Debug((DEBUG_ERROR, "Unknown log feature type \"%s\"", type));
73 if (from) /* send an error; if from is NULL, called from conf parser */
74 send_reply(from, ERR_BADLOGTYPE, type);
76 log_write(LS_CONFIG, L_ERROR, 0, "Unknown log feature type \"%s\"", type);
78 return 0; /* not found */
81 /* Set the value of a log output type for a log subsystem */
83 feature_log_set(struct Client* from, const char* const* fields, int count)
85 struct LogTypes *desc;
88 if (count < 2) { /* set default facility */
89 if (log_set_default(count < 1 ? 0 : fields[0])) {
90 assert(count >= 1); /* should always accept default */
92 if (from) /* send an error */
93 send_reply(from, ERR_BADLOGVALUE, fields[0]);
95 log_write(LS_CONFIG, L_ERROR, 0,
96 "Bad value \"%s\" for default facility", fields[0]);
98 return count < 1 ? -1 : 1; /* tell feature to set or clear mark */
99 } else if (!(subsys = log_canon(fields[0]))) { /* no such subsystem */
100 if (from) /* send an error */
101 send_reply(from, ERR_BADLOGSYS, fields[0]);
103 log_write(LS_CONFIG, L_ERROR, 0,
104 "No such logging subsystem \"%s\"", fields[0]);
105 } else if ((desc = feature_log_desc(from, fields[1]))) { /* set value */
106 if ((*desc->set)(fields[0], count < 3 ? 0 : fields[2])) {
107 assert(count >= 3); /* should always accept default */
109 if (from) /* send an error */
110 send_reply(from, ERR_BADLOGVALUE, fields[2]);
112 log_write(LS_CONFIG, L_ERROR, 0,
113 "Bad value \"%s\" for log type %s (subsystem %s)",
114 fields[2], desc->type, subsys);
121 /* reset a log type for a subsystem to its default value */
123 feature_log_reset(struct Client* from, const char* const* fields, int count)
125 struct LogTypes *desc;
128 assert(0 != from); /* Never called by the .conf parser */
130 if (count < 1) { /* reset default facility */
132 return -1; /* unmark this entry */
133 } else if (count < 2)
134 need_more_params(from, "RESET");
135 else if (!(subsys = log_canon(fields[0]))) /* no such subsystem */
136 send_reply(from, ERR_BADLOGSYS, fields[0]);
137 else if ((desc = feature_log_desc(from, fields[1]))) /* reset value */
138 (*desc->set)(fields[0], 0); /* default should always be accepted */
143 /* report the value of a log setting */
145 feature_log_get(struct Client* from, const char* const* fields, int count)
147 struct LogTypes *desc;
148 char *value, *subsys;
150 assert(0 != from); /* never called by .conf parser */
152 if (count < 1) /* return default facility */
153 send_reply(from, SND_EXPLICIT | RPL_FEATURE, ":Log facility: %s",
156 need_more_params(from, "GET");
157 else if (!(subsys = log_canon(fields[0]))) { /* no such subsystem */
158 send_reply(from, ERR_BADLOGSYS, fields[0]);
159 } else if ((desc = feature_log_desc(from, fields[1]))) {
160 if ((value = (*desc->get)(fields[0]))) /* send along value */
161 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
162 ":Log %s for subsystem %s: %s", desc->type, subsys,
163 (*desc->get)(subsys));
165 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
166 ":No log %s is set for subsystem %s", desc->type, subsys);
170 /* sets a feature to the given value */
171 typedef int (*feat_set_call)(struct Client*, const char* const*, int);
172 /* gets the value of a feature */
173 typedef void (*feat_get_call)(struct Client*, const char* const*, int);
174 /* unmarks all sub-feature values prior to reading .conf */
175 typedef void (*feat_unmark_call)(void);
176 /* resets to defaults all currently unmarked values */
177 typedef void (*feat_mark_call)(int);
178 /* reports features as a /stats f list */
179 typedef void (*feat_report_call)(struct Client*, int);
181 #define FEAT_NONE 0x0000 /* no value */
182 #define FEAT_INT 0x0001 /* set if entry contains an integer value */
183 #define FEAT_BOOL 0x0002 /* set if entry contains a boolean value */
184 #define FEAT_STR 0x0003 /* set if entry contains a string value */
185 #define FEAT_MASK 0x000f /* possible value types */
187 #define FEAT_MARK 0x0010 /* set if entry has been changed */
188 #define FEAT_NULL 0x0020 /* NULL string is permitted */
189 #define FEAT_CASE 0x0040 /* string is case-sensitive */
191 #define FEAT_OPER 0x0100 /* set to display only to opers */
192 #define FEAT_MYOPER 0x0200 /* set to display only to local opers */
193 #define FEAT_NODISP 0x0400 /* feature must never be displayed */
195 #define FEAT_READ 0x1000 /* feature is read-only (for now, perhaps?) */
197 static struct FeatureDesc {
198 enum Feature feat; /* feature identifier */
199 char* type; /* string describing type */
200 unsigned int flags; /* flags for feature */
201 int v_int; /* integer value */
202 int def_int; /* default value */
203 char* v_str; /* string value */
204 char* def_str; /* default value */
205 feat_set_call set; /* set feature values */
206 feat_set_call reset; /* reset feature values to defaults */
207 feat_get_call get; /* get feature values */
208 feat_unmark_call unmark; /* unmark all feature change values */
209 feat_mark_call mark; /* reset to defaults all unchanged features */
210 feat_report_call report; /* report feature values */
212 #define F_N(type, flags, set, reset, get, unmark, mark, report) \
213 { FEAT_ ## type, #type, FEAT_NONE | (flags), 0, 0, 0, 0, \
214 (set), (reset), (get), (unmark), (mark), (report) }
215 #define F_I(type, flags, v_int) \
216 { FEAT_ ## type, #type, FEAT_INT | (flags), 0, (v_int), 0, 0, \
218 #define F_B(type, flags, v_int) \
219 { FEAT_ ## type, #type, FEAT_BOOL | (flags), 0, (v_int), 0, 0, \
221 #define F_S(type, flags, v_str) \
222 { FEAT_ ## type, #type, FEAT_STR | (flags), 0, 0, 0, (v_str), \
226 F_N(LOG, FEAT_MYOPER, feature_log_set, feature_log_reset, feature_log_get,
227 log_feature_unmark, log_feature_mark, log_feature_report),
228 F_S(DOMAINNAME, 0, DOMAINNAME),
229 F_B(RELIABLE_CLOCK, 0, 0),
230 F_I(BUFFERPOOL, 0, 27000000),
231 F_B(HAS_FERGUSON_FLUSHER, 0, 0),
232 F_I(CLIENT_FLOOD, 0, 1024),
233 F_I(SERVER_PORT, FEAT_OPER, 4400),
234 F_B(NODEFAULTMOTD, 0, 1),
235 F_B(KILL_IPMISMATCH, FEAT_OPER, 0),
236 F_B(IDLE_FROM_MSG, 0, 1),
238 F_B(WALLOPS_OPER_ONLY, 0, 0),
240 F_N(RANDOM_SEED, FEAT_NODISP, random_seed_set, 0, 0, 0, 0, 0),
242 /* features that probably should not be touched */
243 F_I(KILLCHASETIMELIMIT, 0, 30),
244 F_I(MAXCHANNELSPERUSER, 0, 10),
245 F_I(AVBANLEN, 0, 40),
247 F_I(MAXSILES, 0, 15),
248 F_I(HANGONGOODLINK, 0, 300),
249 F_I(HANGONRETRYDELAY, 0, 10),
250 F_I(CONNECTTIMEOUT, 0, 90),
252 F_I(MAXIMUM_LINKS, 0, 1),
253 F_I(PINGFREQUENCY, 0, 120),
254 F_I(CONNECTFREQUENCY, 0, 600),
255 F_I(DEFAULTMAXSENDQLENGTH, 0, 40000),
257 /* Some misc. default paths */
258 F_S(MPATH, FEAT_CASE | FEAT_MYOPER, "ircd.motd"),
259 F_S(RPATH, FEAT_CASE | FEAT_MYOPER, "remote.motd"),
260 F_S(PPATH, FEAT_CASE | FEAT_MYOPER | FEAT_READ, "ircd.pid"),
262 /* Networking features */
263 F_B(VIRTUAL_HOST, 0, 0),
264 F_I(TOS_SERVER, 0, 0x08),
265 F_I(TOS_CLIENT, 0, 0x08),
267 /* features that affect all operators */
268 F_B(CRYPT_OPER_PASSWORD, FEAT_MYOPER | FEAT_READ, 1),
269 F_B(OPER_NO_CHAN_LIMIT, 0, 1),
270 F_B(OPER_MODE_LCHAN, 0, 1),
271 F_B(OPER_WALK_THROUGH_LMODES, 0, 0),
272 F_B(NO_OPER_DEOP_LCHAN, 0, 0),
273 F_B(SHOW_INVISIBLE_USERS, 0, 1),
274 F_B(SHOW_ALL_INVISIBLE_USERS, 0, 1),
275 F_B(UNLIMIT_OPER_QUERY, 0, 0),
276 F_B(LOCAL_KILL_ONLY, 0, 0),
277 F_B(CONFIG_OPERCMDS, 0, 1), /* XXX change default before release */
279 /* features that affect global opers on this server */
280 F_B(OPER_KILL, 0, 1),
281 F_B(OPER_REHASH, 0, 1),
282 F_B(OPER_RESTART, 0, 1),
284 F_B(OPER_GLINE, 0, 1),
285 F_B(OPER_LGLINE, 0, 1),
286 F_B(OPER_JUPE, 0, 1),
287 F_B(OPER_LJUPE, 0, 1),
288 F_B(OPER_OPMODE, 0, 1),
289 F_B(OPER_LOPMODE, 0, 1),
290 F_B(OPER_BADCHAN, 0, 0),
291 F_B(OPER_LBADCHAN, 0, 0),
293 F_B(OPERS_SEE_IN_SECRET_CHANNELS, 0, 1),
295 /* features that affect local opers on this server */
296 F_B(LOCOP_KILL, 0, 0),
297 F_B(LOCOP_REHASH, 0, 1),
298 F_B(LOCOP_RESTART, 0, 0),
299 F_B(LOCOP_DIE, 0, 0),
300 F_B(LOCOP_LGLINE, 0, 1),
301 F_B(LOCOP_LJUPE, 0, 1),
302 F_B(LOCOP_LOPMODE, 0, 1),
303 F_B(LOCOP_LBADCHAN, 0, 0),
304 F_B(LOCOP_SET, 0, 0),
305 F_B(LOCOP_SEE_IN_SECRET_CHANNELS, 0, 0),
311 { FEAT_LAST_F, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
314 /* Given a feature's identifier, look up the feature descriptor */
315 static struct FeatureDesc *
316 feature_desc(struct Client* from, const char *feature)
320 assert(0 != feature);
322 for (i = 0; features[i].type; i++) /* find appropriate descriptor */
323 if (!ircd_strcmp(feature, features[i].type))
326 Debug((DEBUG_ERROR, "Unknown feature \"%s\"", feature));
327 if (from) /* report an error */
328 send_reply(from, ERR_NOFEATURE, feature);
330 log_write(LS_CONFIG, L_ERROR, 0, "Unknown feature \"%s\"", feature);
332 return 0; /* not found */
335 /* Given a feature vector string, set the value of a feature */
337 feature_set(struct Client* from, const char* const* fields, int count)
340 struct FeatureDesc *feat;
342 if (from && !HasPriv(from, PRIV_SET))
343 return send_reply(from, ERR_NOPRIVILEGES);
346 if (from) /* report an error in the number of arguments */
347 need_more_params(from, "SET");
349 log_write(LS_CONFIG, L_ERROR, 0, "Not enough fields in F line");
350 } else if ((feat = feature_desc(from, fields[0]))) { /* find feature */
351 if (from && feat->flags & FEAT_READ)
352 return send_reply(from, ERR_NOFEATURE, fields[0]);
354 switch (feat->flags & FEAT_MASK) {
356 if (feat->set && (i = (*feat->set)(from, fields + 1, count - 1))) {
357 if (i > 0) /* call the set callback and do marking */
358 feat->flags |= FEAT_MARK;
360 feat->flags &= ~FEAT_MARK;
364 case FEAT_INT: /* an integer value */
365 if (count < 2) { /* reset value */
366 feat->v_int = feat->def_int;
367 feat->flags &= ~FEAT_MARK;
368 } else { /* ok, figure out the value and whether to mark it */
369 feat->v_int = atoi(fields[1]);
370 if (feat->v_int == feat->def_int)
371 feat->flags &= ~FEAT_MARK;
373 feat->flags |= FEAT_MARK;
377 case FEAT_BOOL: /* it's a boolean value--true or false */
378 if (count < 2) { /* reset value */
379 feat->v_int = feat->def_int;
380 feat->flags &= ~FEAT_MARK;
381 } else { /* figure out the value and whether to mark it */
382 if (!ircd_strncmp(fields[1], "TRUE", strlen(fields[1])) ||
383 !ircd_strncmp(fields[1], "YES", strlen(fields[1])) ||
384 (strlen(fields[1]) >= 2 &&
385 !ircd_strncmp(fields[1], "ON", strlen(fields[1]))))
387 else if (!ircd_strncmp(fields[1], "FALSE", strlen(fields[1])) ||
388 !ircd_strncmp(fields[1], "NO", strlen(fields[1])) ||
389 (strlen(fields[1]) >= 2 &&
390 !ircd_strncmp(fields[1], "OFF", strlen(fields[1]))))
392 else if (from) /* report an error... */
393 return send_reply(from, ERR_BADFEATVALUE, fields[1], feat->type);
395 log_write(LS_CONFIG, L_ERROR, 0, "Bad value \"%s\" for feature %s",
396 fields[1], feat->type);
400 if (feat->v_int == feat->def_int) /* figure out whether to mark it */
401 feat->flags &= ~FEAT_MARK;
403 feat->flags |= FEAT_MARK;
407 case FEAT_STR: /* it's a string value */
409 !(feat->flags & FEAT_CASE ? strcmp(fields[1], feat->def_str) :
410 ircd_strcmp(fields[1], feat->def_str))) { /* reset to default */
411 if (feat->v_str && feat->v_str != feat->def_str)
412 MyFree(feat->v_str); /* free old value */
413 feat->v_str = feat->def_str; /* very special... */
415 feat->flags &= ~FEAT_MARK; /* unmark it */
417 if (!*fields[1]) { /* empty string translates to NULL */
418 if (feat->flags & FEAT_NULL) { /* permitted? */
419 if (feat->v_str && feat->v_str != feat->def_str)
420 MyFree(feat->v_str); /* free old value */
421 feat->v_str = 0; /* set it to NULL */
422 } else if (from) /* hmmm...not permitted; report error */
423 return send_reply(from, ERR_BADFEATVALUE, "NULL", feat->type);
425 log_write(LS_CONFIG, L_ERROR, 0,
426 "Bad value \"NULL\" for feature %s", feat->type);
429 } else if ((feat->flags & FEAT_CASE ?
430 strcmp(fields[1], feat->v_str) :
431 ircd_strcmp(fields[1], feat->v_str))) { /* new value */
432 if (feat->v_str && feat->v_str != feat->def_str)
433 MyFree(feat->v_str); /* free old value */
434 DupString(feat->v_str, fields[1]); /* store new value */
437 feat->flags |= FEAT_MARK; /* mark it as having been touched */
446 /* reset a feature to its default values */
448 feature_reset(struct Client* from, const char* const* fields, int count)
451 struct FeatureDesc *feat;
455 if (!HasPriv(from, PRIV_SET))
456 return send_reply(from, ERR_NOPRIVILEGES);
458 if (count < 1) /* check arguments */
459 need_more_params(from, "RESET");
460 else if ((feat = feature_desc(from, fields[0]))) { /* get descriptor */
461 if (from && feat->flags & FEAT_READ)
462 return send_reply(from, ERR_NOFEATURE, fields[0]);
464 switch (feat->flags & FEAT_MASK) {
465 case FEAT_NONE: /* None... */
466 if (feat->reset && (i = (*feat->reset)(from, fields + 1, count - 1))) {
467 if (i > 0) /* call reset callback and parse mark return */
468 feat->flags |= FEAT_MARK;
470 feat->flags &= ~FEAT_MARK;
474 case FEAT_INT: /* Integer... */
475 case FEAT_BOOL: /* Boolean... */
476 feat->v_int = feat->def_int; /* set the default */
477 feat->flags &= ~FEAT_MARK; /* unmark it */
480 case FEAT_STR: /* string! */
481 if (feat->v_str && feat->v_str != feat->def_str)
482 MyFree(feat->v_str); /* free old value */
483 feat->v_str = feat->def_str; /* set it to default */
484 feat->flags &= ~FEAT_MARK; /* unmark it */
492 /* Gets the value of a specific feature and reports it to the user */
494 feature_get(struct Client* from, const char* const* fields, int count)
496 struct FeatureDesc *feat;
500 if (count < 1) /* check parameters */
501 need_more_params(from, "GET");
502 else if ((feat = feature_desc(from, fields[0]))) {
503 if ((feat->flags & FEAT_NODISP) ||
504 (feat->flags & FEAT_MYOPER && !MyOper(from)) ||
505 (feat->flags & FEAT_OPER && !IsAnOper(from))) /* check privs */
506 return send_reply(from, ERR_NOPRIVILEGES);
508 switch (feat->flags & FEAT_MASK) {
509 case FEAT_NONE: /* none, call the callback... */
510 if (feat->get) /* if there's a callback, use it */
511 (*feat->get)(from, fields + 1, count - 1);
514 case FEAT_INT: /* integer, report integer value */
515 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
516 ":Integer value of %s: %d", feat->type, feat->v_int);
519 case FEAT_BOOL: /* boolean, report boolean value */
520 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
521 ":Boolean value of %s: %s", feat->type,
522 feat->v_int ? "TRUE" : "FALSE");
525 case FEAT_STR: /* string, report string value */
526 if (feat->v_str) /* deal with null case */
527 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
528 ":String value of %s: %s", feat->type, feat->v_str);
530 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
531 ":String value for %s not set", feat->type);
539 /* called before reading the .conf to clear all marks */
545 for (i = 0; features[i].type; i++) {
546 features[i].flags &= ~FEAT_MARK; /* clear the marks... */
547 if (features[i].unmark) /* call the unmark callback if necessary */
548 (*features[i].unmark)();
552 /* Called after reading the .conf to reset unmodified values to defaults */
558 for (i = 0; features[i].type; i++)
559 switch (features[i].flags & FEAT_MASK) {
561 if (features[i].mark) /* call the mark callback if necessary */
562 (*features[i].mark)(features[i].flags & FEAT_MARK ? 1 : 0);
565 case FEAT_INT: /* Integers or Booleans... */
567 if (!(features[i].flags & FEAT_MARK)) /* not changed? */
568 features[i].v_int = features[i].def_int;
571 case FEAT_STR: /* strings... */
572 if (!(features[i].flags & FEAT_MARK)) { /* not changed? */
573 if (features[i].v_str && features[i].v_str != features[i].def_str)
574 MyFree(features[i].v_str); /* free old value */
575 features[i].v_str = features[i].def_str;
581 /* report all F-lines */
583 feature_report(struct Client* to)
587 for (i = 0; features[i].type; i++) {
588 if ((features[i].flags & FEAT_NODISP) ||
589 (features[i].flags & FEAT_MYOPER && !MyOper(to)) ||
590 (features[i].flags & FEAT_OPER && !IsAnOper(to)))
591 continue; /* skip this one */
593 switch (features[i].flags & FEAT_MASK) {
595 if (features[i].report) /* let the callback handle this */
596 (*features[i].report)(to, features[i].flags & FEAT_MARK ? 1 : 0);
600 case FEAT_INT: /* Report an F-line with integer values */
601 if (features[i].flags & FEAT_MARK) /* it's been changed */
602 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s %d",
603 features[i].type, features[i].v_int);
606 case FEAT_BOOL: /* Report an F-line with boolean values */
607 if (features[i].flags & FEAT_MARK) /* it's been changed */
608 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s %s",
609 features[i].type, features[i].v_int ? "TRUE" : "FALSE");
612 case FEAT_STR: /* Report an F-line with string values */
613 if (features[i].flags & FEAT_MARK) { /* it's been changed */
614 if (features[i].v_str)
615 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s %s",
616 features[i].type, features[i].v_str);
617 else /* Actually, F:<type> would reset it; you want F:<type>: */
618 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s",
626 /* return a feature's integer value */
628 feature_int(enum Feature feat)
630 assert(features[feat].feat == feat);
631 assert((features[feat].flags & FEAT_MASK) == FEAT_INT);
633 return features[feat].v_int;
636 /* return a feature's boolean value */
638 feature_bool(enum Feature feat)
640 assert(features[feat].feat == feat);
641 assert((features[feat].flags & FEAT_MASK) == FEAT_BOOL);
643 return features[feat].v_int;
646 /* return a feature's string value */
648 feature_str(enum Feature feat)
650 assert(features[feat].feat == feat);
651 assert((features[feat].flags & FEAT_MASK) == FEAT_STR);
653 return features[feat].v_str;