71bd169060240250a9401937ac733ad57387ca59
[radsecproxy.git] / lib / conf.c
1 /* Copyright 2010, 2011 NORDUnet A/S. All rights reserved.
2    See the file COPYING for licensing information.  */
3
4 #if defined HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7
8 #include <confuse.h>
9 #include <string.h>
10 #include <assert.h>
11 #include <radsec/radsec.h>
12 #include <radsec/radsec-impl.h>
13 #include "peer.h"
14 #include "debug.h"
15
16 #if 0
17   # common config options
18   dictionary = STRING
19
20   # common realm config options
21   realm NAME {
22       type = "UDP"|"TCP"|"TLS"|"DTLS"
23       timeout = INT
24       retries = INT
25       cacertfile = STRING
26       #cacertpath = STRING
27       certfile = STRING
28       certkeyfile = STRING
29       pskstr = STRING   # Transport pre-shared key, UTF-8 form.
30       pskhexstr = STRING # Transport pre-shared key, ASCII hex form.
31       pskid = STRING
32       pskex = "PSK"|"DHE_PSK"|"RSA_PSK"
33   }
34
35   # client specific realm config options
36   realm NAME {
37       server {
38           hostname = STRING
39           service = STRING
40           secret = STRING       # RADIUS secret
41       }
42   }
43 #endif
44
45 /* FIXME: Leaking memory in error cases.  */
46 int
47 rs_context_read_config(struct rs_context *ctx, const char *config_file)
48 {
49   cfg_t *cfg, *cfg_realm, *cfg_server;
50   int err = 0;
51   int i, j;
52   const char *s;
53   struct rs_config *config = NULL;
54
55   cfg_opt_t server_opts[] =
56     {
57       CFG_STR ("hostname", NULL, CFGF_NONE),
58       CFG_STR ("service", "2083", CFGF_NONE),
59       CFG_STR ("secret", "radsec", CFGF_NONE),
60       CFG_END ()
61     };
62   cfg_opt_t realm_opts[] =
63     {
64       CFG_STR ("type", "UDP", CFGF_NONE),
65       CFG_INT ("timeout", 2, CFGF_NONE), /* FIXME: Remove?  */
66       CFG_INT ("retries", 2, CFGF_NONE), /* FIXME: Remove?  */
67       CFG_STR ("cacertfile", NULL, CFGF_NONE),
68       /*CFG_STR ("cacertpath", NULL, CFGF_NONE),*/
69       CFG_STR ("certfile", NULL, CFGF_NONE),
70       CFG_STR ("certkeyfile", NULL, CFGF_NONE),
71       CFG_STR ("pskstr", NULL, CFGF_NONE),
72       CFG_STR ("pskhexstr", NULL, CFGF_NONE),
73       CFG_STR ("pskid", NULL, CFGF_NONE),
74       CFG_STR ("pskex", "PSK", CFGF_NONE),
75       CFG_SEC ("server", server_opts, CFGF_MULTI),
76       CFG_END ()
77     };
78   cfg_opt_t opts[] =
79     {
80       CFG_STR ("dictionary", NULL, CFGF_NONE),
81       CFG_SEC ("realm", realm_opts, CFGF_TITLE | CFGF_MULTI),
82       CFG_END ()
83     };
84
85   cfg = cfg_init (opts, CFGF_NONE);
86   if (cfg == NULL)
87     return rs_err_ctx_push (ctx, RSE_CONFIG, "unable to initialize libconfuse");
88   err = cfg_parse (cfg, config_file);
89   switch (err)
90     {
91     case  CFG_SUCCESS:
92       break;
93     case CFG_FILE_ERROR:
94       return rs_err_ctx_push (ctx, RSE_CONFIG,
95                               "%s: unable to open configuration file",
96                               config_file);
97     case CFG_PARSE_ERROR:
98       return rs_err_ctx_push (ctx, RSE_CONFIG, "%s: invalid configuration file",
99                               config_file);
100     default:
101         return rs_err_ctx_push (ctx, RSE_CONFIG, "%s: unknown parse error",
102                                 config_file);
103     }
104
105   config = rs_calloc (ctx, 1, sizeof (*config));
106   if (config == NULL)
107     return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
108   ctx->config = config;
109   config->dictionary = cfg_getstr (cfg, "dictionary");
110
111   for (i = 0; i < cfg_size (cfg, "realm"); i++)
112     {
113       struct rs_realm *r = NULL;
114       const char *typestr;
115       char *pskstr = NULL, *pskhexstr = NULL;
116
117       r = rs_calloc (ctx, 1, sizeof(*r));
118       if (r == NULL)
119         return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
120       if (config->realms != NULL)
121         {
122           r->next = config->realms->next;
123           config->realms->next = r;
124         }
125       else
126         {
127           config->realms = r;
128         }
129       cfg_realm = cfg_getnsec (cfg, "realm", i);
130       s = cfg_title (cfg_realm);
131       if (s == NULL)
132         return rs_err_ctx_push_fl (ctx, RSE_CONFIG, __FILE__, __LINE__,
133                                    "missing realm name");
134       /* We use a copy of the return value of cfg_title() since it's const.  */
135       r->name = strdup (s);
136       if (r->name == NULL)
137         return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
138
139       typestr = cfg_getstr (cfg_realm, "type");
140       if (strcmp (typestr, "UDP") == 0)
141         r->type = RS_CONN_TYPE_UDP;
142       else if (strcmp (typestr, "TCP") == 0)
143         r->type = RS_CONN_TYPE_TCP;
144       else if (strcmp (typestr, "TLS") == 0)
145         r->type = RS_CONN_TYPE_TLS;
146       else if (strcmp (typestr, "DTLS") == 0)
147         r->type = RS_CONN_TYPE_DTLS;
148       else
149         return rs_err_ctx_push (ctx, RSE_CONFIG,
150                                 "%s: invalid connection type: %s",
151                                 r->name, typestr);
152       r->timeout = cfg_getint (cfg_realm, "timeout");
153       r->retries = cfg_getint (cfg_realm, "retries");
154
155       r->cacertfile = cfg_getstr (cfg_realm, "cacertfile");
156       /*r->cacertpath = cfg_getstr (cfg_realm, "cacertpath");*/
157       r->certfile = cfg_getstr (cfg_realm, "certfile");
158       r->certkeyfile = cfg_getstr (cfg_realm, "certkeyfile");
159
160       pskstr = cfg_getstr (cfg_realm, "pskstr");
161       pskhexstr = cfg_getstr (cfg_realm, "pskhexstr");
162       if (pskstr || pskhexstr)
163         {
164 #if defined RS_ENABLE_TLS_PSK
165           char *kex = cfg_getstr (cfg_realm, "pskex");
166           rs_cred_type_t type = RS_CRED_NONE;
167           struct rs_credentials *cred = NULL;
168           assert (kex != NULL);
169
170           if (!strcmp (kex, "PSK"))
171             type = RS_CRED_TLS_PSK;
172           else
173             {
174               /* TODO: push a warning on the error stack:*/
175               /*rs_err_ctx_push (ctx, RSE_WARN, "%s: unsupported PSK key exchange"
176                                " algorithm -- PSK not used", kex);*/
177             }
178
179           if (type != RS_CRED_NONE)
180             {
181               cred = rs_calloc (ctx, 1, sizeof (*cred));
182               if (cred == NULL)
183                 return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__,
184                                            NULL);
185               cred->type = type;
186               cred->identity = cfg_getstr (cfg_realm, "pskid");
187               if (pskhexstr)
188                 {
189                   cred->secret_encoding = RS_KEY_ENCODING_ASCII_HEX;
190                   cred->secret = pskhexstr;
191                   if (pskstr)
192                     ;      /* TODO: warn that we're ignoring pskstr */
193                 }
194               else
195                 {
196                   cred->secret_encoding = RS_KEY_ENCODING_UTF8;
197                   cred->secret = pskstr;
198                 }
199
200               r->transport_cred = cred;
201             }
202 #else  /* !RS_ENABLE_TLS_PSK */
203           /* TODO: push a warning on the error stack: */
204           /* rs_err_ctx_push (ctx, RSE_WARN, "libradsec wasn't configured with "
205                            "support for TLS preshared keys, ignoring pskstr "
206                            "and pskhexstr");*/
207 #endif  /* RS_ENABLE_TLS_PSK */
208         }
209
210       /* For TLS and DTLS realms, validate that we either have (i) CA
211          cert file or path or (ii) PSK.  */
212       if ((r->type == RS_CONN_TYPE_TLS || r->type == RS_CONN_TYPE_DTLS)
213           && (r->cacertfile == NULL && r->cacertpath == NULL)
214           && r->transport_cred == NULL)
215         return rs_err_ctx_push (ctx, RSE_CONFIG,
216                                 "%s: missing both CA file/path and PSK",
217                                 r->name);
218
219       /* Add peers, one per server stanza.  */
220       for (j = 0; j < cfg_size (cfg_realm, "server"); j++)
221         {
222           struct rs_peer *p = peer_create (ctx, &r->peers);
223           if (p == NULL)
224             return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__,
225                                        NULL);
226           p->realm = r;
227
228           cfg_server = cfg_getnsec (cfg_realm, "server", j);
229           /* FIXME: Handle resolve errors, possibly by postponing name
230              resolution.  */
231           rs_resolv (&p->addr, r->type, cfg_getstr (cfg_server, "hostname"),
232                      cfg_getstr (cfg_server, "service"));
233           p->secret = cfg_getstr (cfg_server, "secret");
234         }
235     }
236
237   /* Save config object in context, for freeing in rs_context_destroy().  */
238   ctx->config->cfg = cfg;
239
240   return RSE_OK;
241 }
242
243 struct rs_realm *
244 rs_conf_find_realm(struct rs_context *ctx, const char *name)
245 {
246   struct rs_realm *r;
247
248   for (r = ctx->config->realms; r; r = r->next)
249     if (strcmp (r->name, name) == 0)
250         return r;
251
252   return NULL;
253 }