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