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