Clarify comments about PSK string encoding.
[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_fl (ctx, RSE_CONFIG, __FILE__, __LINE__,
150                                    "invalid connection type: %s", 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           char *kex = cfg_getstr (cfg_realm, "pskex");
164           rs_cred_type_t type = RS_CRED_NONE;
165           struct rs_credentials *cred = NULL;
166           assert (kex != NULL);
167
168           if (!strcmp (kex, "PSK"))
169             type = RS_CRED_TLS_PSK;
170           else
171             {
172               /* TODO: push a warning, using a separate warn stack or
173                  onto the ordinary error stack?  */
174               /* rs_err_ctx_push (ctx, FIXME, "%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         }
202
203       /* Add peers, one per server stanza.  */
204       for (j = 0; j < cfg_size (cfg_realm, "server"); j++)
205         {
206           struct rs_peer *p = peer_create (ctx, &r->peers);
207           if (p == NULL)
208             return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__,
209                                        NULL);
210           p->realm = r;
211
212           cfg_server = cfg_getnsec (cfg_realm, "server", j);
213           /* FIXME: Handle resolve errors, possibly by postponing name
214              resolution.  */
215           rs_resolv (&p->addr, r->type, cfg_getstr (cfg_server, "hostname"),
216                      cfg_getstr (cfg_server, "service"));
217           p->secret = cfg_getstr (cfg_server, "secret");
218         }
219     }
220
221   /* Save config object in context, for freeing in rs_context_destroy().  */
222   ctx->config->cfg = cfg;
223
224   return RSE_OK;
225 }
226
227 struct rs_realm *
228 rs_conf_find_realm(struct rs_context *ctx, const char *name)
229 {
230   struct rs_realm *r;
231
232   for (r = ctx->config->realms; r; r = r->next)
233     if (strcmp (r->name, name) == 0)
234         return r;
235
236   return NULL;
237 }