Verify certificate CN against configured hostname.
[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 "util.h"
15 #include "debug.h"
16
17 #if 0
18   # common config options
19   dictionary = STRING
20
21   # common realm config options
22   realm NAME {
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 NAME {
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_STR ("dictionary", NULL, CFGF_NONE),
82       CFG_SEC ("realm", realm_opts, CFGF_TITLE | CFGF_MULTI),
83       CFG_END ()
84     };
85
86   cfg = cfg_init (opts, CFGF_NONE);
87   if (cfg == NULL)
88     return rs_err_ctx_push (ctx, RSE_CONFIG, "unable to initialize libconfuse");
89   err = cfg_parse (cfg, config_file);
90   switch (err)
91     {
92     case  CFG_SUCCESS:
93       break;
94     case CFG_FILE_ERROR:
95       return rs_err_ctx_push (ctx, RSE_CONFIG,
96                               "%s: unable to open configuration file",
97                               config_file);
98     case CFG_PARSE_ERROR:
99       return rs_err_ctx_push (ctx, RSE_CONFIG, "%s: invalid configuration file",
100                               config_file);
101     default:
102         return rs_err_ctx_push (ctx, RSE_CONFIG, "%s: unknown parse error",
103                                 config_file);
104     }
105
106   config = rs_calloc (ctx, 1, sizeof (*config));
107   if (config == NULL)
108     return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
109   ctx->config = config;
110   config->dictionary = cfg_getstr (cfg, "dictionary");
111
112   for (i = 0; i < cfg_size (cfg, "realm"); i++)
113     {
114       struct rs_realm *r = NULL;
115       const char *typestr;
116       char *pskstr = NULL, *pskhexstr = NULL;
117
118       r = rs_calloc (ctx, 1, sizeof(*r));
119       if (r == NULL)
120         return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
121       if (config->realms != NULL)
122         {
123           r->next = config->realms->next;
124           config->realms->next = r;
125         }
126       else
127         {
128           config->realms = r;
129         }
130       cfg_realm = cfg_getnsec (cfg, "realm", i);
131       s = cfg_title (cfg_realm);
132       if (s == NULL)
133         return rs_err_ctx_push_fl (ctx, RSE_CONFIG, __FILE__, __LINE__,
134                                    "missing realm name");
135       /* We use a copy of the return value of cfg_title() since it's const.  */
136       r->name = rs_strdup (ctx, s);
137       if (r->name == NULL)
138         return RSE_NOMEM;
139
140       typestr = cfg_getstr (cfg_realm, "type");
141       if (strcmp (typestr, "UDP") == 0)
142         r->type = RS_CONN_TYPE_UDP;
143       else if (strcmp (typestr, "TCP") == 0)
144         r->type = RS_CONN_TYPE_TCP;
145       else if (strcmp (typestr, "TLS") == 0)
146         r->type = RS_CONN_TYPE_TLS;
147       else if (strcmp (typestr, "DTLS") == 0)
148         r->type = RS_CONN_TYPE_DTLS;
149       else
150         return rs_err_ctx_push (ctx, RSE_CONFIG,
151                                 "%s: invalid connection type: %s",
152                                 r->name, typestr);
153       r->timeout = cfg_getint (cfg_realm, "timeout");
154       r->retries = cfg_getint (cfg_realm, "retries");
155
156       r->cacertfile = cfg_getstr (cfg_realm, "cacertfile");
157       /*r->cacertpath = cfg_getstr (cfg_realm, "cacertpath");*/
158       r->certfile = cfg_getstr (cfg_realm, "certfile");
159       r->certkeyfile = cfg_getstr (cfg_realm, "certkeyfile");
160
161       pskstr = cfg_getstr (cfg_realm, "pskstr");
162       pskhexstr = cfg_getstr (cfg_realm, "pskhexstr");
163       if (pskstr || pskhexstr)
164         {
165 #if defined RS_ENABLE_TLS_PSK
166           char *kex = cfg_getstr (cfg_realm, "pskex");
167           rs_cred_type_t type = RS_CRED_NONE;
168           struct rs_credentials *cred = NULL;
169           assert (kex != NULL);
170
171           if (!strcmp (kex, "PSK"))
172             type = RS_CRED_TLS_PSK;
173           else
174             {
175               /* TODO: push a warning on the error stack:*/
176               /*rs_err_ctx_push (ctx, RSE_WARN, "%s: unsupported PSK key exchange"
177                                " algorithm -- PSK not used", kex);*/
178             }
179
180           if (type != RS_CRED_NONE)
181             {
182               cred = rs_calloc (ctx, 1, sizeof (*cred));
183               if (cred == NULL)
184                 return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__,
185                                            NULL);
186               cred->type = type;
187               cred->identity = cfg_getstr (cfg_realm, "pskid");
188               if (pskhexstr)
189                 {
190                   cred->secret_encoding = RS_KEY_ENCODING_ASCII_HEX;
191                   cred->secret = pskhexstr;
192                   if (pskstr)
193                     ;      /* TODO: warn that we're ignoring pskstr */
194                 }
195               else
196                 {
197                   cred->secret_encoding = RS_KEY_ENCODING_UTF8;
198                   cred->secret = pskstr;
199                 }
200
201               r->transport_cred = cred;
202             }
203 #else  /* !RS_ENABLE_TLS_PSK */
204           /* TODO: push a warning on the error stack: */
205           /* rs_err_ctx_push (ctx, RSE_WARN, "libradsec wasn't configured with "
206                            "support for TLS preshared keys, ignoring pskstr "
207                            "and pskhexstr");*/
208 #endif  /* RS_ENABLE_TLS_PSK */
209         }
210
211       /* For TLS and DTLS realms, validate that we either have (i) CA
212          cert file or path or (ii) PSK.  */
213       if ((r->type == RS_CONN_TYPE_TLS || r->type == RS_CONN_TYPE_DTLS)
214           && (r->cacertfile == NULL && r->cacertpath == NULL)
215           && r->transport_cred == NULL)
216         return rs_err_ctx_push (ctx, RSE_CONFIG,
217                                 "%s: missing both CA file/path and PSK",
218                                 r->name);
219
220       /* Add peers, one per server stanza.  */
221       for (j = 0; j < cfg_size (cfg_realm, "server"); j++)
222         {
223           struct rs_peer *p = peer_create (ctx, &r->peers);
224           if (p == NULL)
225             return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__,
226                                        NULL);
227           p->realm = r;
228
229           cfg_server = cfg_getnsec (cfg_realm, "server", j);
230           p->hostname = cfg_getstr (cfg_server, "hostname");
231           p->service = cfg_getstr (cfg_server, "service");
232           p->secret = cfg_getstr (cfg_server, "secret");
233         }
234     }
235
236   /* Save config object in context, for freeing in rs_context_destroy().  */
237   ctx->config->cfg = cfg;
238
239   return RSE_OK;
240 }
241
242 struct rs_realm *
243 rs_conf_find_realm(struct rs_context *ctx, const char *name)
244 {
245   struct rs_realm *r;
246
247   for (r = ctx->config->realms; r; r = r->next)
248     if (strcmp (r->name, name) == 0)
249         return r;
250
251   return NULL;
252 }