Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / ircd_features.c
1 /*
2  * IRC - Internet Relay Chat, ircd/features.c
3  * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
4  *
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)
8  * any later version.
9  *
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.
14  *
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.
18  *
19  * $Id$
20  */
21 #include "ircd_features.h"
22 #include "client.h"
23 #include "hash.h"
24 #include "ircd.h"
25 #include "ircd_alloc.h"
26 #include "ircd_log.h"
27 #include "ircd_reply.h"
28 #include "ircd_string.h"
29 #include "match.h"
30 #include "msg.h"
31 #include "numeric.h"
32 #include "numnicks.h"
33 #include "s_bsd.h"
34 #include "s_debug.h"
35 #include "s_misc.h"
36 #include "send.h"
37 #include "struct.h"
38 #include "support.h"
39 #include "sys.h"    /* FALSE bleah */
40
41 #include <assert.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 /* List of log output types that can be set */
46 static struct LogTypes {
47   char *type;
48   int (*set)(const char *, const char *);
49   char *(*get)(const char *);
50 } logTypes[] = {
51   { "FILE", log_set_file, log_get_file },
52   { "FACILITY", log_set_facility, log_get_facility },
53   { "SNOMASK", log_set_snomask, log_get_snomask },
54   { "LEVEL", log_set_level, log_get_level },
55   { 0, 0, 0 }
56 };
57
58 /* Look up a struct LogType given the type string */
59 static struct LogTypes *
60 feature_log_desc(struct Client* from, const char *type)
61 {
62   int i;
63
64   assert(0 != type);
65
66   for (i = 0; logTypes[i].type; i++) /* find appropriate descriptor */
67     if (!ircd_strcmp(type, logTypes[i].type))
68       return &logTypes[i];
69
70   Debug((DEBUG_ERROR, "Unknown log feature type \"%s\"", type));
71   if (from) /* send an error; if from is NULL, called from conf parser */
72     send_reply(from, ERR_BADLOGTYPE, type);
73   else
74     log_write(LS_CONFIG, L_ERROR, 0, "Unknown log feature type \"%s\"", type);
75
76   return 0; /* not found */
77 }
78
79 /* Set the value of a log output type for a log subsystem */
80 static int
81 feature_log_set(struct Client* from, const char* const* fields, int count)
82 {
83   struct LogTypes *desc;
84   char *subsys;
85
86   if (count < 2) { /* set default facility */
87     if (log_set_default(count < 1 ? 0 : fields[0])) {
88       assert(count >= 1); /* should always accept default */
89
90       if (from) /* send an error */
91         send_reply(from, ERR_BADLOGVALUE, fields[0]);
92       else
93         log_write(LS_CONFIG, L_ERROR, 0,
94                   "Bad value \"%s\" for default facility", fields[0]);
95     } else
96       return count < 1 ? -1 : 1; /* tell feature to set or clear mark */
97   } else if (!(subsys = log_canon(fields[0]))) { /* no such subsystem */
98     if (from) /* send an error */
99       send_reply(from, ERR_BADLOGSYS, fields[0]);
100     else
101       log_write(LS_CONFIG, L_ERROR, 0,
102                 "No such logging subsystem \"%s\"", fields[0]);
103   } else if ((desc = feature_log_desc(from, fields[1]))) { /* set value */
104     if ((*desc->set)(fields[0], count < 3 ? 0 : fields[2])) {
105       assert(count >= 3); /* should always accept default */
106
107       if (from) /* send an error */
108         send_reply(from, ERR_BADLOGVALUE, fields[2]);
109       else
110         log_write(LS_CONFIG, L_ERROR, 0,
111                   "Bad value \"%s\" for log type %s (subsystem %s)",
112                   fields[2], desc->type, subsys);
113     }
114   }
115
116   return 0;
117 }
118
119 /* reset a log type for a subsystem to its default value */
120 static int
121 feature_log_reset(struct Client* from, const char* const* fields, int count)
122 {
123   struct LogTypes *desc;
124   char *subsys;
125
126   assert(0 != from); /* Never called by the .conf parser */
127
128   if (count < 1) { /* reset default facility */
129     log_set_default(0);
130     return -1; /* unmark this entry */
131   } else if (count < 2)
132     need_more_params(from, "RESET");
133   else if (!(subsys = log_canon(fields[0]))) /* no such subsystem */
134     send_reply(from, ERR_BADLOGSYS, fields[0]);
135   else if ((desc = feature_log_desc(from, fields[1]))) /* reset value */
136     (*desc->set)(fields[0], 0); /* default should always be accepted */
137
138   return 0;
139 }
140
141 /* report the value of a log setting */
142 static void
143 feature_log_get(struct Client* from, const char* const* fields, int count)
144 {
145   struct LogTypes *desc;
146   char *value, *subsys;
147
148   assert(0 != from); /* never called by .conf parser */
149
150   if (count < 1) /* return default facility */
151     send_reply(from, SND_EXPLICIT | RPL_FEATURE, ":Log facility: %s",
152                log_get_default());
153   else if (count < 2)
154     need_more_params(from, "GET");
155   else if (!(subsys = log_canon(fields[0]))) { /* no such subsystem */
156     send_reply(from, ERR_BADLOGSYS, fields[0]);
157   } else if ((desc = feature_log_desc(from, fields[1]))) {
158     if ((value = (*desc->get)(fields[0]))) /* send along value */
159       send_reply(from, SND_EXPLICIT | RPL_FEATURE,
160                  ":Log %s for subsystem %s: %s", desc->type, subsys,
161                  (*desc->get)(subsys));
162     else
163       send_reply(from, SND_EXPLICIT | RPL_FEATURE,
164                  ":No log %s is set for subsystem %s", desc->type, subsys);
165   }
166 }
167
168 /* sets a feature to the given value */
169 typedef int  (*feat_set_call)(struct Client*, const char* const*, int);
170 /* gets the value of a feature */
171 typedef void (*feat_get_call)(struct Client*, const char* const*, int);
172 /* unmarks all sub-feature values prior to reading .conf */
173 typedef void (*feat_unmark_call)(void);
174 /* resets to defaults all currently unmarked values */
175 typedef void (*feat_mark_call)(int);
176 /* reports features as a /stats f list */
177 typedef void (*feat_report_call)(struct Client*, int);
178
179 #define FEAT_NONE   0x0000      /* no value */
180 #define FEAT_INT    0x0001      /* set if entry contains an integer value */
181 #define FEAT_BOOL   0x0002      /* set if entry contains a boolean value */
182 #define FEAT_STR    0x0003      /* set if entry contains a string value */
183 #define FEAT_MASK   0x000f      /* possible value types */
184
185 #define FEAT_MARK   0x0010      /* set if entry has been changed */
186 #define FEAT_NULL   0x0020      /* NULL string is permitted */
187 #define FEAT_CASE   0x0040      /* string is case-sensitive */
188
189 #define FEAT_OPER   0x0100      /* set to display only to opers */
190 #define FEAT_MYOPER 0x0200      /* set to display only to local opers */
191
192 static struct FeatureDesc {
193   enum Feature     feat;    /* feature identifier */
194   char*            type;    /* string describing type */
195   unsigned int     flags;   /* flags for feature */
196   int              v_int;   /* integer value */
197   int              def_int; /* default value */
198   char*            v_str;   /* string value */
199   char*            def_str; /* default value */
200   feat_set_call    set;     /* set feature values */
201   feat_set_call    reset;   /* reset feature values to defaults */
202   feat_get_call    get;     /* get feature values */
203   feat_unmark_call unmark;  /* unmark all feature change values */
204   feat_mark_call   mark;    /* reset to defaults all unchanged features */
205   feat_report_call report;  /* report feature values */
206 } features[] = {
207 #define F(type, flags, v_int, v_str, set, reset, get, unmark, mark, report)   \
208   { FEAT_##type, #type, (flags),                                              \
209     0, (v_int), 0, (v_str),                                                   \
210     (set), (reset), (get),                                                    \
211     (unmark), (mark),                                                         \
212     (report) }
213
214   F(LOG, FEAT_NONE | FEAT_MYOPER, 0, 0,
215     feature_log_set, feature_log_reset, feature_log_get,
216     log_feature_unmark, log_feature_mark, log_feature_report),
217
218   { FEAT_LAST_F, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
219 };
220
221 /* Given a feature's identifier, look up the feature descriptor */
222 static struct FeatureDesc *
223 feature_desc(struct Client* from, const char *feature)
224 {
225   int i;
226
227   assert(0 != feature);
228
229   for (i = 0; features[i].type; i++) /* find appropriate descriptor */
230     if (!ircd_strcmp(feature, features[i].type))
231       return &features[i];
232
233   Debug((DEBUG_ERROR, "Unknown feature \"%s\"", feature));
234   if (from) /* report an error */
235     send_reply(from, ERR_NOFEATURE, feature);
236   else
237     log_write(LS_CONFIG, L_ERROR, 0, "Unknown feature \"%s\"", feature);
238
239   return 0; /* not found */
240 }
241
242 /* Given a feature vector string, set the value of a feature */
243 int
244 feature_set(struct Client* from, const char* const* fields, int count)
245 {
246   int i;
247   struct FeatureDesc *feat;
248
249   if (count < 1) {
250     if (from) /* report an error in the number of arguments */
251       need_more_params(from, "SET");
252     else
253       log_write(LS_CONFIG, L_ERROR, 0, "Not enough fields in F line");
254   } else if ((feat = feature_desc(from, fields[0]))) { /* find feature */
255     if (feat->set && (i = (*feat->set)(from, fields + 1, count - 1))) {
256       if (i > 0) /* call the set callback and do marking */
257         feat->flags |= FEAT_MARK;
258       else /* i < 0 */
259         feat->flags &= ~FEAT_MARK;
260     } else /* Ok, it's a value we can fiddle with */
261       switch (feat->flags & FEAT_MASK) {
262       case FEAT_INT: /* an integer value */
263         if (count < 2) { /* reset value */
264           feat->v_int = feat->def_int;
265           feat->flags &= ~FEAT_MARK;
266         } else { /* ok, figure out the value and whether to mark it */
267           feat->v_int = atoi(fields[1]);
268           if (feat->v_int == feat->def_int)
269             feat->flags &= ~FEAT_MARK;
270           else
271             feat->flags |= FEAT_MARK;
272         }
273         break;
274
275       case FEAT_BOOL: /* it's a boolean value--true or false */
276         if (count < 2) { /* reset value */
277           feat->v_int = feat->def_int;
278           feat->flags &= ~FEAT_MARK;
279         } else { /* figure out the value and whether to mark it */
280           if (!ircd_strncmp(fields[1], "TRUE", strlen(fields[1])))
281             feat->v_int = 1;
282           else if (!ircd_strncmp(fields[1], "YES", strlen(fields[1])))
283             feat->v_int = 1;
284           else if (!ircd_strncmp(fields[1], "FALSE", strlen(fields[1])))
285             feat->v_int = 0;
286           else if (!ircd_strncmp(fields[1], "NO", strlen(fields[1])))
287             feat->v_int = 0;
288           else if (from) /* report an error... */
289             return send_reply(from, ERR_BADFEATVALUE, fields[1], feat->type);
290           else {
291             log_write(LS_CONFIG, L_ERROR, 0, "Bad value \"%s\" for feature %s",
292                       fields[1], feat->type);
293             return 0;
294           }
295
296           if (feat->v_int == feat->def_int) /* figure out whether to mark it */
297             feat->flags &= ~FEAT_MARK;
298           else
299             feat->flags |= FEAT_MARK;
300         }
301         break;
302
303       case FEAT_STR: /* it's a string value */
304         if (count < 2 ||
305             !(feat->flags & FEAT_CASE ? strcmp(fields[1], feat->def_str) :
306               ircd_strcmp(fields[1], feat->def_str))) { /* reset to default */
307           if (feat->v_str && feat->v_str != feat->def_str)
308             MyFree(feat->v_str); /* free old value */
309           feat->v_str = feat->def_str; /* very special... */
310
311           feat->flags &= ~FEAT_MARK; /* unmark it */
312         } else {
313           if (!*fields[1]) { /* empty string translates to NULL */
314             if (feat->flags & FEAT_NULL) { /* permitted? */
315               if (feat->v_str && feat->v_str != feat->def_str)
316                 MyFree(feat->v_str); /* free old value */
317               feat->v_str = 0; /* set it to NULL */
318             } else if (from) /* hmmm...not permitted; report error */
319               return send_reply(from, ERR_BADFEATVALUE, "NULL", feat->type);
320             else {
321               log_write(LS_CONFIG, L_ERROR, 0,
322                         "Bad value \"NULL\" for feature %s", feat->type);
323               return 0;
324             }
325           } else if ((feat->flags & FEAT_CASE ?
326                       strcmp(fields[1], feat->v_str) :
327                       ircd_strcmp(fields[1], feat->v_str))) { /* new value */
328             if (feat->v_str && feat->v_str != feat->def_str)
329               MyFree(feat->v_str); /* free old value */
330             DupString(feat->v_str, fields[1]); /* store new value */
331           }
332
333           feat->flags |= FEAT_MARK; /* mark it as having been touched */
334         }
335         break;
336       }
337   }
338
339   return 0;
340 }
341
342 /* reset a feature to its default values */
343 int
344 feature_reset(struct Client* from, const char* const* fields, int count)
345 {
346   int i;
347   struct FeatureDesc *feat;
348
349   assert(0 != from);
350
351   if (count < 1) /* check arguments */
352     need_more_params(from, "RESET");
353   else if ((feat = feature_desc(from, fields[0]))) { /* get descriptor */
354     if (feat->reset && (i = (*feat->reset)(from, fields + 1, count - 1))) {
355       if (i > 0) /* call reset callback and parse mark return */
356         feat->flags |= FEAT_MARK;
357       else /* i < 0 */
358         feat->flags &= ~FEAT_MARK;
359     } else { /* oh, it's something we own... */
360       switch (feat->flags & FEAT_MASK) {
361       case FEAT_INT:  /* Integer... */
362       case FEAT_BOOL: /* Boolean... */
363         feat->v_int = feat->def_int; /* set the default */
364         break;
365
366       case FEAT_STR: /* string! */
367         if (feat->v_str && feat->v_str != feat->def_str)
368           MyFree(feat->v_str); /* free old value */
369         feat->v_str = feat->def_str; /* set it to default */
370         break;
371       }
372
373       feat->flags &= ~FEAT_MARK; /* unmark it */
374     }
375   }
376
377   return 0;
378 }
379
380 /* Gets the value of a specific feature and reports it to the user */
381 int
382 feature_get(struct Client* from, const char* const* fields, int count)
383 {
384   struct FeatureDesc *feat;
385
386   assert(0 != from);
387
388   if (count < 1) /* check parameters */
389     need_more_params(from, "GET");
390   else if ((feat = feature_desc(from, fields[0]))) {
391     if ((feat->flags & FEAT_MYOPER && !MyOper(from)) ||
392         (feat->flags & FEAT_OPER && !IsAnOper(from))) /* check privs */
393       return send_reply(from, ERR_NOPRIVILEGES);
394
395     if (feat->get) /* if there's a callback, use it */
396       (*feat->get)(from, fields + 1, count - 1);
397     else /* something we own */
398       switch (feat->flags & FEAT_MASK) {
399       case FEAT_INT: /* integer, report integer value */
400         send_reply(from, SND_EXPLICIT | RPL_FEATURE,
401                    ":Integer value of %s: %d", feat->type, feat->v_int);
402         break;
403
404       case FEAT_BOOL: /* boolean, report boolean value */
405         send_reply(from, SND_EXPLICIT | RPL_FEATURE,
406                    ":Boolean value of %s: %s", feat->type,
407                    feat->v_int ? "TRUE" : "FALSE");
408         break;
409
410       case FEAT_STR: /* string, report string value */
411         if (feat->v_str) /* deal with null case */
412           send_reply(from, SND_EXPLICIT | RPL_FEATURE,
413                      ":String value of %s: %s", feat->type, feat->v_str);
414         else
415           send_reply(from, SND_EXPLICIT | RPL_FEATURE,
416                      ":String value for %s not set", feat->type);
417         break;
418       }
419   }
420
421   return 0;
422 }
423
424 /* called before reading the .conf to clear all marks */
425 void
426 feature_unmark(void)
427 {
428   int i;
429
430   for (i = 0; features[i].type; i++) {
431     features[i].flags &= ~FEAT_MARK; /* clear the marks... */
432     if (features[i].unmark) /* call the unmark callback if necessary */
433       (*features[i].unmark)();
434   }
435 }
436
437 /* Called after reading the .conf to reset unmodified values to defaults */
438 void
439 feature_mark(void)
440 {
441   int i;
442
443   for (i = 0; features[i].type; i++) {
444     if (!(features[i].flags & FEAT_MARK)) { /* not changed? */
445       switch (features[i].flags & FEAT_MASK) {
446       case FEAT_INT:  /* Integers or Booleans... */
447       case FEAT_BOOL:
448         features[i].v_int = features[i].def_int;
449         break;
450
451       case FEAT_STR: /* strings... */
452         if (features[i].v_str && features[i].v_str != features[i].def_str)
453           MyFree(features[i].v_str); /* free old value */
454         features[i].v_str = features[i].def_str;
455         break;
456       }
457     }
458
459     if (features[i].mark) /* call the mark callback if necessary */
460       (*features[i].mark)(features[i].flags & FEAT_MARK ? 1 : 0);
461   }
462 }
463
464 /* report all F-lines */
465 void
466 feature_report(struct Client* to)
467 {
468   int i;
469
470   for (i = 0; features[i].type; i++) {
471     if ((features[i].flags & FEAT_MYOPER && !MyOper(to)) ||
472         (features[i].flags & FEAT_OPER && !IsAnOper(to)))
473       continue; /* skip this one */
474
475     if (features[i].report) /* let the callback handle this */
476       (*features[i].report)(to, features[i].flags & FEAT_MARK ? 1 : 0);
477     else if (features[i].flags & FEAT_MARK) { /* it's been changed */
478       switch (features[i].flags & FEAT_MASK) {
479       case FEAT_INT: /* Report an F-line with integer values */
480         send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s %d",
481                    features[i].type, features[i].v_int);
482         break;
483
484       case FEAT_BOOL: /* Report an F-line with boolean values */
485         send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s %s",
486                    features[i].type, features[i].v_int ? "TRUE" : "FALSE");
487         break;
488
489       case FEAT_STR: /* Report an F-line with string values */
490         if (features[i].v_str)
491           send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s %s",
492                      features[i].type, features[i].v_str);
493         else /* Actually, F:<type> would reset it; you want F:<type>: */
494           send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s",
495                      features[i].type);
496         break;
497       }
498     }
499   }
500 }
501
502 /* return a feature's integer value */
503 int
504 feature_int(enum Feature feat)
505 {
506   assert(features[feat].feat == feat);
507   assert((features[feat].flags & FEAT_MASK) == FEAT_INT);
508
509   return features[feat].v_int;
510 }
511
512 /* return a feature's boolean value */
513 int
514 feature_bool(enum Feature feat)
515 {
516   assert(features[feat].feat == feat);
517   assert((features[feat].flags & FEAT_MASK) == FEAT_BOOL);
518
519   return features[feat].v_int;
520 }
521
522 /* return a feature's string value */
523 const char *
524 feature_str(enum Feature feat)
525 {
526   assert(features[feat].feat == feat);
527   assert((features[feat].flags & FEAT_MASK) == FEAT_STR);
528
529   return features[feat].v_str;
530 }