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"
40 #include "sys.h" /* FALSE bleah */
46 /* List of log output types that can be set */
47 static struct LogTypes {
49 int (*set)(const char *, const char *);
50 char *(*get)(const char *);
52 { "FILE", log_set_file, log_get_file },
53 { "FACILITY", log_set_facility, log_get_facility },
54 { "SNOMASK", log_set_snomask, log_get_snomask },
55 { "LEVEL", log_set_level, log_get_level },
59 /* Look up a struct LogType given the type string */
60 static struct LogTypes *
61 feature_log_desc(struct Client* from, const char *type)
67 for (i = 0; logTypes[i].type; i++) /* find appropriate descriptor */
68 if (!ircd_strcmp(type, logTypes[i].type))
71 Debug((DEBUG_ERROR, "Unknown log feature type \"%s\"", type));
72 if (from) /* send an error; if from is NULL, called from conf parser */
73 send_reply(from, ERR_BADLOGTYPE, type);
75 log_write(LS_CONFIG, L_ERROR, 0, "Unknown log feature type \"%s\"", type);
77 return 0; /* not found */
80 /* Set the value of a log output type for a log subsystem */
82 feature_log_set(struct Client* from, const char* const* fields, int count)
84 struct LogTypes *desc;
87 if (count < 2) { /* set default facility */
88 if (log_set_default(count < 1 ? 0 : fields[0])) {
89 assert(count >= 1); /* should always accept default */
91 if (from) /* send an error */
92 send_reply(from, ERR_BADLOGVALUE, fields[0]);
94 log_write(LS_CONFIG, L_ERROR, 0,
95 "Bad value \"%s\" for default facility", fields[0]);
97 return count < 1 ? -1 : 1; /* tell feature to set or clear mark */
98 } else if (!(subsys = log_canon(fields[0]))) { /* no such subsystem */
99 if (from) /* send an error */
100 send_reply(from, ERR_BADLOGSYS, fields[0]);
102 log_write(LS_CONFIG, L_ERROR, 0,
103 "No such logging subsystem \"%s\"", fields[0]);
104 } else if ((desc = feature_log_desc(from, fields[1]))) { /* set value */
105 if ((*desc->set)(fields[0], count < 3 ? 0 : fields[2])) {
106 assert(count >= 3); /* should always accept default */
108 if (from) /* send an error */
109 send_reply(from, ERR_BADLOGVALUE, fields[2]);
111 log_write(LS_CONFIG, L_ERROR, 0,
112 "Bad value \"%s\" for log type %s (subsystem %s)",
113 fields[2], desc->type, subsys);
120 /* reset a log type for a subsystem to its default value */
122 feature_log_reset(struct Client* from, const char* const* fields, int count)
124 struct LogTypes *desc;
127 assert(0 != from); /* Never called by the .conf parser */
129 if (count < 1) { /* reset default facility */
131 return -1; /* unmark this entry */
132 } else if (count < 2)
133 need_more_params(from, "RESET");
134 else if (!(subsys = log_canon(fields[0]))) /* no such subsystem */
135 send_reply(from, ERR_BADLOGSYS, fields[0]);
136 else if ((desc = feature_log_desc(from, fields[1]))) /* reset value */
137 (*desc->set)(fields[0], 0); /* default should always be accepted */
142 /* report the value of a log setting */
144 feature_log_get(struct Client* from, const char* const* fields, int count)
146 struct LogTypes *desc;
147 char *value, *subsys;
149 assert(0 != from); /* never called by .conf parser */
151 if (count < 1) /* return default facility */
152 send_reply(from, SND_EXPLICIT | RPL_FEATURE, ":Log facility: %s",
155 need_more_params(from, "GET");
156 else if (!(subsys = log_canon(fields[0]))) { /* no such subsystem */
157 send_reply(from, ERR_BADLOGSYS, fields[0]);
158 } else if ((desc = feature_log_desc(from, fields[1]))) {
159 if ((value = (*desc->get)(fields[0]))) /* send along value */
160 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
161 ":Log %s for subsystem %s: %s", desc->type, subsys,
162 (*desc->get)(subsys));
164 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
165 ":No log %s is set for subsystem %s", desc->type, subsys);
169 /* sets a feature to the given value */
170 typedef int (*feat_set_call)(struct Client*, const char* const*, int);
171 /* gets the value of a feature */
172 typedef void (*feat_get_call)(struct Client*, const char* const*, int);
173 /* unmarks all sub-feature values prior to reading .conf */
174 typedef void (*feat_unmark_call)(void);
175 /* resets to defaults all currently unmarked values */
176 typedef void (*feat_mark_call)(int);
177 /* reports features as a /stats f list */
178 typedef void (*feat_report_call)(struct Client*, int);
180 #define FEAT_NONE 0x0000 /* no value */
181 #define FEAT_INT 0x0001 /* set if entry contains an integer value */
182 #define FEAT_BOOL 0x0002 /* set if entry contains a boolean value */
183 #define FEAT_STR 0x0003 /* set if entry contains a string value */
184 #define FEAT_MASK 0x000f /* possible value types */
186 #define FEAT_MARK 0x0010 /* set if entry has been changed */
187 #define FEAT_NULL 0x0020 /* NULL string is permitted */
188 #define FEAT_CASE 0x0040 /* string is case-sensitive */
190 #define FEAT_OPER 0x0100 /* set to display only to opers */
191 #define FEAT_MYOPER 0x0200 /* set to display only to local opers */
193 #define FEAT_READ 0x1000 /* feature is read-only (for now, perhaps?) */
195 static struct FeatureDesc {
196 enum Feature feat; /* feature identifier */
197 char* type; /* string describing type */
198 unsigned int flags; /* flags for feature */
199 int v_int; /* integer value */
200 int def_int; /* default value */
201 char* v_str; /* string value */
202 char* def_str; /* default value */
203 feat_set_call set; /* set feature values */
204 feat_set_call reset; /* reset feature values to defaults */
205 feat_get_call get; /* get feature values */
206 feat_unmark_call unmark; /* unmark all feature change values */
207 feat_mark_call mark; /* reset to defaults all unchanged features */
208 feat_report_call report; /* report feature values */
210 #define F_N(type, flags, set, reset, get, unmark, mark, report) \
211 { FEAT_ ## type, #type, FEAT_NONE | (flags), 0, 0, 0, 0, \
212 (set), (reset), (get), (unmark), (mark), (report) }
213 #define F_I(type, flags, v_int) \
214 { FEAT_ ## type, #type, FEAT_INT | (flags), 0, (v_int), 0, 0, \
216 #define F_B(type, flags, v_int) \
217 { FEAT_ ## type, #type, FEAT_BOOL | (flags), 0, (v_int), 0, 0, \
219 #define F_S(type, flags, v_str) \
220 { FEAT_ ## type, #type, FEAT_STR | (flags), 0, 0, 0, (v_str), \
224 F_N(LOG, FEAT_MYOPER, feature_log_set, feature_log_reset, feature_log_get,
225 log_feature_unmark, log_feature_mark, log_feature_report),
226 F_S(DOMAINNAME, 0, DOMAINNAME),
227 F_B(RELIABLE_CLOCK, 0, 0),
228 F_I(BUFFERPOOL, 0, 27000000),
229 F_B(HAS_FERGUSON_FLUSHER, 0, 0),
230 F_I(CLIENT_FLOOD, 0, 1024),
231 F_I(SERVER_PORT, FEAT_OPER, 4400),
232 F_B(NODEFAULTMOTD, 0, 1),
233 F_B(KILL_IPMISMATCH, FEAT_OPER, 0),
234 F_B(IDLE_FROM_MSG, 0, 1),
236 F_B(WALLOPS_OPER_ONLY, 0, 0),
238 /* Some misc. default paths */
239 F_S(MPATH, FEAT_CASE | FEAT_MYOPER, "ircd.motd"),
240 F_S(RPATH, FEAT_CASE | FEAT_MYOPER, "remote.motd"),
241 F_S(PPATH, FEAT_CASE | FEAT_MYOPER | FEAT_READ, "ircd.pid"),
243 /* Networking features */
244 F_B(VIRTUAL_HOST, 0, 0),
245 F_I(TOS_SERVER, 0, 0x08),
246 F_I(TOS_CLIENT, 0, 0x08),
248 /* features that affect all operators */
249 F_B(CRYPT_OPER_PASSWORD, FEAT_MYOPER | FEAT_READ, 1),
250 F_B(OPER_NO_CHAN_LIMIT, 0, 1),
251 F_B(OPER_MODE_LCHAN, 0, 1),
252 F_B(OPER_WALK_THROUGH_LMODES, 0, 0),
253 F_B(NO_OPER_DEOP_LCHAN, 0, 0),
254 F_B(SHOW_INVISIBLE_USERS, 0, 1),
255 F_B(SHOW_ALL_INVISIBLE_USERS, 0, 1),
256 F_B(UNLIMIT_OPER_QUERY, 0, 0),
257 F_B(LOCAL_KILL_ONLY, 0, 0),
258 F_B(CONFIG_OPERCMDS, 0, 1), /* XXX change default before release */
260 /* features that affect global opers on this server */
261 F_B(OPER_KILL, 0, 1),
262 F_B(OPER_REHASH, 0, 1),
263 F_B(OPER_RESTART, 0, 1),
265 F_B(OPER_GLINE, 0, 1),
266 F_B(OPER_LGLINE, 0, 1),
267 F_B(OPER_JUPE, 0, 1),
268 F_B(OPER_LJUPE, 0, 1),
269 F_B(OPER_OPMODE, 0, 1),
270 F_B(OPER_LOPMODE, 0, 1),
271 F_B(OPER_BADCHAN, 0, 0),
272 F_B(OPER_LBADCHAN, 0, 0),
274 F_B(OPERS_SEE_IN_SECRET_CHANNELS, 0, 1),
276 /* features that affect local opers on this server */
277 F_B(LOCOP_KILL, 0, 0),
278 F_B(LOCOP_REHASH, 0, 1),
279 F_B(LOCOP_RESTART, 0, 0),
280 F_B(LOCOP_DIE, 0, 0),
281 F_B(LOCOP_LGLINE, 0, 1),
282 F_B(LOCOP_LJUPE, 0, 1),
283 F_B(LOCOP_LOPMODE, 0, 1),
284 F_B(LOCOP_LBADCHAN, 0, 0),
285 F_B(LOCOP_SET, 0, 0),
286 F_B(LOCOP_SEE_IN_SECRET_CHANNELS, 0, 0),
292 { FEAT_LAST_F, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
295 /* Given a feature's identifier, look up the feature descriptor */
296 static struct FeatureDesc *
297 feature_desc(struct Client* from, const char *feature)
301 assert(0 != feature);
303 for (i = 0; features[i].type; i++) /* find appropriate descriptor */
304 if (!ircd_strcmp(feature, features[i].type))
307 Debug((DEBUG_ERROR, "Unknown feature \"%s\"", feature));
308 if (from) /* report an error */
309 send_reply(from, ERR_NOFEATURE, feature);
311 log_write(LS_CONFIG, L_ERROR, 0, "Unknown feature \"%s\"", feature);
313 return 0; /* not found */
316 /* Given a feature vector string, set the value of a feature */
318 feature_set(struct Client* from, const char* const* fields, int count)
321 struct FeatureDesc *feat;
323 if (from && !HasPriv(from, PRIV_SET))
324 return send_reply(from, ERR_NOPRIVILEGES);
327 if (from) /* report an error in the number of arguments */
328 need_more_params(from, "SET");
330 log_write(LS_CONFIG, L_ERROR, 0, "Not enough fields in F line");
331 } else if ((feat = feature_desc(from, fields[0]))) { /* find feature */
332 if (from && feat->flags & FEAT_READ)
333 return send_reply(from, ERR_NOFEATURE, fields[0]);
335 switch (feat->flags & FEAT_MASK) {
337 if (feat->set && (i = (*feat->set)(from, fields + 1, count - 1))) {
338 if (i > 0) /* call the set callback and do marking */
339 feat->flags |= FEAT_MARK;
341 feat->flags &= ~FEAT_MARK;
345 case FEAT_INT: /* an integer value */
346 if (count < 2) { /* reset value */
347 feat->v_int = feat->def_int;
348 feat->flags &= ~FEAT_MARK;
349 } else { /* ok, figure out the value and whether to mark it */
350 feat->v_int = atoi(fields[1]);
351 if (feat->v_int == feat->def_int)
352 feat->flags &= ~FEAT_MARK;
354 feat->flags |= FEAT_MARK;
358 case FEAT_BOOL: /* it's a boolean value--true or false */
359 if (count < 2) { /* reset value */
360 feat->v_int = feat->def_int;
361 feat->flags &= ~FEAT_MARK;
362 } else { /* figure out the value and whether to mark it */
363 if (!ircd_strncmp(fields[1], "TRUE", strlen(fields[1])) ||
364 !ircd_strncmp(fields[1], "YES", strlen(fields[1])) ||
365 (strlen(fields[1]) >= 2 &&
366 !ircd_strncmp(fields[1], "ON", strlen(fields[1]))))
368 else if (!ircd_strncmp(fields[1], "FALSE", strlen(fields[1])) ||
369 !ircd_strncmp(fields[1], "NO", strlen(fields[1])) ||
370 (strlen(fields[1]) >= 2 &&
371 !ircd_strncmp(fields[1], "OFF", strlen(fields[1]))))
373 else if (from) /* report an error... */
374 return send_reply(from, ERR_BADFEATVALUE, fields[1], feat->type);
376 log_write(LS_CONFIG, L_ERROR, 0, "Bad value \"%s\" for feature %s",
377 fields[1], feat->type);
381 if (feat->v_int == feat->def_int) /* figure out whether to mark it */
382 feat->flags &= ~FEAT_MARK;
384 feat->flags |= FEAT_MARK;
388 case FEAT_STR: /* it's a string value */
390 !(feat->flags & FEAT_CASE ? strcmp(fields[1], feat->def_str) :
391 ircd_strcmp(fields[1], feat->def_str))) { /* reset to default */
392 if (feat->v_str && feat->v_str != feat->def_str)
393 MyFree(feat->v_str); /* free old value */
394 feat->v_str = feat->def_str; /* very special... */
396 feat->flags &= ~FEAT_MARK; /* unmark it */
398 if (!*fields[1]) { /* empty string translates to NULL */
399 if (feat->flags & FEAT_NULL) { /* permitted? */
400 if (feat->v_str && feat->v_str != feat->def_str)
401 MyFree(feat->v_str); /* free old value */
402 feat->v_str = 0; /* set it to NULL */
403 } else if (from) /* hmmm...not permitted; report error */
404 return send_reply(from, ERR_BADFEATVALUE, "NULL", feat->type);
406 log_write(LS_CONFIG, L_ERROR, 0,
407 "Bad value \"NULL\" for feature %s", feat->type);
410 } else if ((feat->flags & FEAT_CASE ?
411 strcmp(fields[1], feat->v_str) :
412 ircd_strcmp(fields[1], feat->v_str))) { /* new value */
413 if (feat->v_str && feat->v_str != feat->def_str)
414 MyFree(feat->v_str); /* free old value */
415 DupString(feat->v_str, fields[1]); /* store new value */
418 feat->flags |= FEAT_MARK; /* mark it as having been touched */
427 /* reset a feature to its default values */
429 feature_reset(struct Client* from, const char* const* fields, int count)
432 struct FeatureDesc *feat;
436 if (!HasPriv(from, PRIV_SET))
437 return send_reply(from, ERR_NOPRIVILEGES);
439 if (count < 1) /* check arguments */
440 need_more_params(from, "RESET");
441 else if ((feat = feature_desc(from, fields[0]))) { /* get descriptor */
442 if (from && feat->flags & FEAT_READ)
443 return send_reply(from, ERR_NOFEATURE, fields[0]);
445 switch (feat->flags & FEAT_MASK) {
446 case FEAT_NONE: /* None... */
447 if (feat->reset && (i = (*feat->reset)(from, fields + 1, count - 1))) {
448 if (i > 0) /* call reset callback and parse mark return */
449 feat->flags |= FEAT_MARK;
451 feat->flags &= ~FEAT_MARK;
455 case FEAT_INT: /* Integer... */
456 case FEAT_BOOL: /* Boolean... */
457 feat->v_int = feat->def_int; /* set the default */
458 feat->flags &= ~FEAT_MARK; /* unmark it */
461 case FEAT_STR: /* string! */
462 if (feat->v_str && feat->v_str != feat->def_str)
463 MyFree(feat->v_str); /* free old value */
464 feat->v_str = feat->def_str; /* set it to default */
465 feat->flags &= ~FEAT_MARK; /* unmark it */
473 /* Gets the value of a specific feature and reports it to the user */
475 feature_get(struct Client* from, const char* const* fields, int count)
477 struct FeatureDesc *feat;
481 if (count < 1) /* check parameters */
482 need_more_params(from, "GET");
483 else if ((feat = feature_desc(from, fields[0]))) {
484 if ((feat->flags & FEAT_MYOPER && !MyOper(from)) ||
485 (feat->flags & FEAT_OPER && !IsAnOper(from))) /* check privs */
486 return send_reply(from, ERR_NOPRIVILEGES);
488 switch (feat->flags & FEAT_MASK) {
489 case FEAT_NONE: /* none, call the callback... */
490 if (feat->get) /* if there's a callback, use it */
491 (*feat->get)(from, fields + 1, count - 1);
494 case FEAT_INT: /* integer, report integer value */
495 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
496 ":Integer value of %s: %d", feat->type, feat->v_int);
499 case FEAT_BOOL: /* boolean, report boolean value */
500 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
501 ":Boolean value of %s: %s", feat->type,
502 feat->v_int ? "TRUE" : "FALSE");
505 case FEAT_STR: /* string, report string value */
506 if (feat->v_str) /* deal with null case */
507 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
508 ":String value of %s: %s", feat->type, feat->v_str);
510 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
511 ":String value for %s not set", feat->type);
519 /* called before reading the .conf to clear all marks */
525 for (i = 0; features[i].type; i++) {
526 features[i].flags &= ~FEAT_MARK; /* clear the marks... */
527 if (features[i].unmark) /* call the unmark callback if necessary */
528 (*features[i].unmark)();
532 /* Called after reading the .conf to reset unmodified values to defaults */
538 for (i = 0; features[i].type; i++)
539 switch (features[i].flags & FEAT_MASK) {
541 if (features[i].mark) /* call the mark callback if necessary */
542 (*features[i].mark)(features[i].flags & FEAT_MARK ? 1 : 0);
545 case FEAT_INT: /* Integers or Booleans... */
547 if (!(features[i].flags & FEAT_MARK)) /* not changed? */
548 features[i].v_int = features[i].def_int;
551 case FEAT_STR: /* strings... */
552 if (!(features[i].flags & FEAT_MARK)) { /* not changed? */
553 if (features[i].v_str && features[i].v_str != features[i].def_str)
554 MyFree(features[i].v_str); /* free old value */
555 features[i].v_str = features[i].def_str;
561 /* report all F-lines */
563 feature_report(struct Client* to)
567 for (i = 0; features[i].type; i++) {
568 if ((features[i].flags & FEAT_MYOPER && !MyOper(to)) ||
569 (features[i].flags & FEAT_OPER && !IsAnOper(to)))
570 continue; /* skip this one */
572 switch (features[i].flags & FEAT_MASK) {
574 if (features[i].report) /* let the callback handle this */
575 (*features[i].report)(to, features[i].flags & FEAT_MARK ? 1 : 0);
579 case FEAT_INT: /* Report an F-line with integer values */
580 if (features[i].flags & FEAT_MARK) /* it's been changed */
581 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s %d",
582 features[i].type, features[i].v_int);
585 case FEAT_BOOL: /* Report an F-line with boolean values */
586 if (features[i].flags & FEAT_MARK) /* it's been changed */
587 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s %s",
588 features[i].type, features[i].v_int ? "TRUE" : "FALSE");
591 case FEAT_STR: /* Report an F-line with string values */
592 if (features[i].flags & FEAT_MARK) { /* it's been changed */
593 if (features[i].v_str)
594 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s %s",
595 features[i].type, features[i].v_str);
596 else /* Actually, F:<type> would reset it; you want F:<type>: */
597 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s",
605 /* return a feature's integer value */
607 feature_int(enum Feature feat)
609 assert(features[feat].feat == feat);
610 assert((features[feat].flags & FEAT_MASK) == FEAT_INT);
612 return features[feat].v_int;
615 /* return a feature's boolean value */
617 feature_bool(enum Feature feat)
619 assert(features[feat].feat == feat);
620 assert((features[feat].flags & FEAT_MASK) == FEAT_BOOL);
622 return features[feat].v_int;
625 /* return a feature's string value */
627 feature_str(enum Feature feat)
629 assert(features[feat].feat == feat);
630 assert((features[feat].flags & FEAT_MASK) == FEAT_STR);
632 return features[feat].v_str;