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