Handle configuration errors better.
[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 <radsec/radsec.h>
11 #include <radsec/radsec-impl.h>
12 #include "peer.h"
13 #include "debug.h"
14
15 #if 0
16   # common config options
17   dictionary = STRING
18
19   # common realm config options
20   realm NAME {
21       type = "UDP"|"TCP"|"TLS"|"DTLS"
22       timeout = INT
23       retries = INT
24       cacertfile = STRING
25       #cacertpath = STRING
26       certfile = STRING
27       certkeyfile = STRING
28   }
29
30   # client specific realm config options
31   realm NAME {
32       server {
33           hostname = STRING
34           service = STRING
35           secret = STRING
36       }
37   }
38 #endif
39
40 int
41 rs_context_read_config(struct rs_context *ctx, const char *config_file)
42 {
43   cfg_t *cfg, *cfg_realm, *cfg_server;
44   int i, j;
45   const char *s;
46   struct rs_config *config = NULL;
47
48   cfg_opt_t server_opts[] =
49     {
50       CFG_STR ("hostname", NULL, CFGF_NONE),
51       CFG_STR ("service", "2083", CFGF_NONE),
52       CFG_STR ("secret", "radsec", CFGF_NONE),
53       CFG_END ()
54     };
55   cfg_opt_t realm_opts[] =
56     {
57       CFG_STR ("type", "UDP", CFGF_NONE),
58       CFG_INT ("timeout", 2, CFGF_NONE), /* FIXME: Remove?  */
59       CFG_INT ("retries", 2, CFGF_NONE), /* FIXME: Remove?  */
60       CFG_STR ("cacertfile", NULL, CFGF_NONE),
61       /*CFG_STR ("cacertpath", NULL, CFGF_NONE),*/
62       CFG_STR ("certfile", NULL, CFGF_NONE),
63       CFG_STR ("certkeyfile", NULL, CFGF_NONE),
64       CFG_SEC ("server", server_opts, CFGF_MULTI),
65       CFG_END ()
66     };
67   cfg_opt_t opts[] =
68     {
69       CFG_STR ("dictionary", NULL, CFGF_NONE),
70       CFG_SEC ("realm", realm_opts, CFGF_TITLE | CFGF_MULTI),
71       CFG_END ()
72     };
73
74   cfg = cfg_init (opts, CFGF_NONE);
75   if (cfg == NULL)
76     return rs_err_ctx_push (ctx, RSE_CONFIG, "unable to initialize libconfuse");
77   if (cfg_parse (cfg, config_file) == CFG_PARSE_ERROR)
78     return rs_err_ctx_push (ctx, RSE_CONFIG, "%s: invalid configuration file",
79                             config_file);
80
81   config = rs_calloc (ctx, 1, sizeof (*config));
82   if (config == NULL)
83     return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
84   ctx->config = config;
85   config->dictionary = cfg_getstr (cfg, "dictionary");
86
87   for (i = 0; i < cfg_size (cfg, "realm"); i++)
88     {
89       struct rs_realm *r = rs_calloc (ctx, 1, sizeof(*r));
90       const char *typestr;
91
92       if (!r)
93         return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
94       if (config->realms)
95         {
96           r->next = config->realms->next;
97           config->realms->next = r;
98         }
99       else
100           config->realms = r;
101       cfg_realm = cfg_getnsec (cfg, "realm", i);
102       /* We use a copy of return value of cfg_title since it's a
103          const.  */
104       s = cfg_title (cfg_realm);
105       if (s == NULL)
106         return rs_err_ctx_push_fl (ctx, RSE_CONFIG, __FILE__, __LINE__,
107                                    "missing realm name");
108       r->name = strdup (s);     /* FIXME: Don't strdup.  */
109       if (!r->name)
110         return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
111
112       typestr = cfg_getstr (cfg_realm, "type");
113       if (!strcmp (typestr, "UDP"))
114         r->type = RS_CONN_TYPE_UDP;
115       else if (!strcmp (typestr, "TCP"))
116         r->type = RS_CONN_TYPE_TCP;
117       else if (!strcmp (typestr, "TLS"))
118         r->type = RS_CONN_TYPE_TLS;
119       else if (!strcmp (typestr, "DTLS"))
120         r->type = RS_CONN_TYPE_DTLS;
121       else
122         return rs_err_ctx_push_fl (ctx, RSE_CONFIG, __FILE__, __LINE__,
123                                    "invalid connection type: %s", typestr);
124       r->timeout = cfg_getint (cfg_realm, "timeout");
125       r->retries = cfg_getint (cfg_realm, "retries");
126
127       r->cacertfile = cfg_getstr (cfg_realm, "cacertfile");
128       /*r->cacertpath = cfg_getstr (cfg_realm, "cacertpath");*/
129       r->certfile = cfg_getstr (cfg_realm, "certfile");
130       r->certkeyfile = cfg_getstr (cfg_realm, "certkeyfile");
131
132       /* Add peers, one per server stanza.  */
133       for (j = 0; j < cfg_size (cfg_realm, "server"); j++)
134         {
135           struct rs_peer *p = peer_create (ctx, &r->peers);
136           if (!p)
137             return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__,
138                                        NULL);
139           p->realm = r;
140
141           cfg_server = cfg_getnsec (cfg_realm, "server", j);
142           /* FIXME: Handle resolve errors, possibly by postponing name
143              resolution.  */
144           rs_resolv (&p->addr, r->type, cfg_getstr (cfg_server, "hostname"),
145                      cfg_getstr (cfg_server, "service"));
146           p->secret = cfg_getstr (cfg_server, "secret");
147         }
148     }
149
150   /* Save config object in context, for freeing in
151      rs_context_destroy().  */
152   ctx->config->cfg =  cfg;
153   return RSE_OK;
154 }
155
156 struct rs_realm *
157 rs_conf_find_realm(struct rs_context *ctx, const char *name)
158 {
159   struct rs_realm *r;
160
161   for (r = ctx->config->realms; r; r = r->next)
162     if (!strcmp (r->name, name))
163         return r;
164   return NULL;
165 }