chiark / gitweb /
Add __oop-read-copy.c
[inn-innduct.git] / nnrpd / sasl_config.c
1 /* sasl_config.c -- Configuration routines
2    Copyright (C) 2000 Kenichi Okada <okada@opaopa.org>
3
4    Author: Kenichi Okada <okada@opaopa.org>
5    Created: 2000-03-04
6 */
7
8 #include "config.h"
9 #include "clibrary.h"
10 #include <ctype.h>
11 #include <syslog.h>
12
13 #include "inn/innconf.h"
14 #include "nnrpd.h"
15 #include "paths.h"
16 #include "sasl_config.h"
17
18 #ifdef HAVE_SSL
19
20 struct configlist {
21     char *key;
22     char *value;
23 };
24
25 static struct configlist *configlist;
26 static int nconfiglist;
27
28 const char *sasl_config_getstring(key, def)
29 const char *key;
30 const char *def;
31 {
32     int opt;
33
34     for (opt = 0; opt < nconfiglist; opt++) {
35         if (*key == configlist[opt].key[0] &&
36             !strcmp(key, configlist[opt].key))
37           return configlist[opt].value;
38     }
39     return def;
40 }
41
42 int sasl_config_getint(key, def)
43 const char *key;
44 int def;
45 {
46     const char *val = sasl_config_getstring(key, (char *)0);
47
48     if (!val) return def;
49     if (!isdigit(*val) && (*val != '-' || !isdigit(val[1]))) return def;
50     return atoi(val);
51 }
52
53 int sasl_config_getswitch(key, def)
54 const char *key;
55 int def;
56 {
57     const char *val = sasl_config_getstring(key, (char *)0);
58
59     if (!val) return def;
60
61     if (*val == '0' || *val == 'n' ||
62         (*val == 'o' && val[1] == 'f') || *val == 'f') {
63         return 0;
64     }
65     else if (*val == '1' || *val == 'y' ||
66              (*val == 'o' && val[1] == 'n') || *val == 't') {
67         return 1;
68     }
69     return def;
70 }
71
72 const char *sasl_config_partitiondir(partition)
73 const char *partition;
74 {
75     char buf[80];
76
77     if (strlen(partition) > 70) return 0;
78     snprintf(buf, sizeof(buf), "partition-%s", partition);
79
80     return sasl_config_getstring(buf, (char *)0);
81 }
82
83 #define CONFIGLISTGROWSIZE 10 /* 100 */
84 void
85 sasl_config_read()
86 {
87     FILE *infile;
88     int lineno = 0;
89     int alloced = 0;
90     char buf[4096];
91     char *p, *key;
92     static char *SASL_CONFIG = NULL;
93
94     if (!SASL_CONFIG)
95         SASL_CONFIG = concatpath(innconf->pathetc, _PATH_SASL_CONFIG);
96     infile = fopen(SASL_CONFIG, "r");
97     if (!infile) {
98       fprintf(stderr, "can't open configuration file %s\n", SASL_CONFIG);
99       exit(1);
100     }
101     
102     while (fgets(buf, sizeof(buf), infile)) {
103         lineno++;
104
105         if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
106         for (p = buf; *p && isspace(*p); p++);
107         if (!*p || *p == '#') continue;
108
109         key = p;
110         while (*p && (isalnum(*p) || *p == '-' || *p == '_')) {
111             if (isupper(*p)) *p = tolower(*p);
112             p++;
113         }
114         if (*p != ':') {
115             fprintf(stderr,
116                     "invalid option name on line %d of configuration file\n",
117                     lineno);
118             exit(1);
119         }
120         *p++ = '\0';
121
122         while (*p && isspace(*p)) p++;
123         
124         if (!*p) {
125             fprintf(stderr, "empty option value on line %d of configuration file\n",
126                     lineno);
127             exit(1);
128         }
129
130         if (nconfiglist == alloced) {
131             alloced += CONFIGLISTGROWSIZE;
132             configlist = xrealloc(configlist, alloced * sizeof(struct configlist));
133         }
134
135         configlist[nconfiglist].key = xstrdup(key);
136         configlist[nconfiglist].value = xstrdup(p);
137         nconfiglist++;
138     }
139     fclose(infile);
140 }
141
142 #endif /* HAVE_SSL */