Rename and move around a few helper functions.
[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   # client config options
17   config NAME {
18       type = "UDP"|"TCP"|"TLS"|"DTLS"
19       timeout = INT
20       retries = INT
21       cacertfile = STRING
22       #cacertpath = STRING
23       certfile = STRING
24       certkeyfile = STRING
25       server {
26           hostname = STRING
27           service = STRING
28           secret = STRING
29       }
30   }
31 #endif
32
33 int
34 rs_context_read_config(struct rs_context *ctx, const char *config_file)
35 {
36   /* FIXME: Missing some error handling in rs_context_read_config().  */
37
38   cfg_t *cfg, *cfg_config, *cfg_server;
39   int i, j;
40   const char *s;
41
42   cfg_opt_t server_opts[] =
43     {
44       CFG_STR ("hostname", NULL, CFGF_NONE),
45       CFG_STR ("service", "radius", CFGF_NONE),
46       CFG_STR ("secret", NULL, CFGF_NONE),
47       CFG_END ()
48     };
49   cfg_opt_t config_opts[] =
50     {
51       CFG_STR ("type", "UDP", CFGF_NONE),
52       CFG_INT ("timeout", 2, CFGF_NONE),
53       CFG_INT ("retries", 2, CFGF_NONE),
54       CFG_STR ("cacertfile", NULL, CFGF_NONE),
55       /*CFG_STR ("cacertpath", NULL, CFGF_NONE),*/
56       CFG_STR ("certfile", NULL, CFGF_NONE),
57       CFG_STR ("certkeyfile", NULL, CFGF_NONE),
58       CFG_SEC ("server", server_opts, CFGF_MULTI),
59       CFG_END ()
60     };
61   cfg_opt_t opts[] =
62     {
63       CFG_SEC ("config", config_opts, CFGF_TITLE | CFGF_MULTI),
64       CFG_END ()
65     };
66
67   cfg = cfg_init (opts, CFGF_NONE);
68   if (cfg_parse (cfg, config_file) == CFG_PARSE_ERROR)
69     return rs_err_ctx_push (ctx, RSE_CONFIG, "%s: invalid configuration file",
70                             config_file);
71   for (i = 0; i < cfg_size (cfg, "config"); i++)
72     {
73       struct rs_realm *r = rs_malloc (ctx, sizeof(*r));
74       const char *typestr;
75
76       if (!r)
77         return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
78       memset (r, 0, sizeof(*r));
79       if (ctx->realms)
80         {
81           r->next = ctx->realms->next;
82           ctx->realms->next = r;
83         }
84       else
85           ctx->realms = r;
86       cfg_config = cfg_getnsec (cfg, "config", i);
87       s = cfg_title (cfg_config);
88       if (s == NULL)
89         return rs_err_ctx_push_fl (ctx, RSE_CONFIG, __FILE__, __LINE__,
90                                    "missing config name");
91       r->name = strdup (s);
92
93       typestr = cfg_getstr (cfg_config, "type");
94       if (!strcmp (typestr, "UDP"))
95         r->type = RS_CONN_TYPE_UDP;
96       else if (!strcmp (typestr, "TCP"))
97         r->type = RS_CONN_TYPE_TCP;
98       else if (!strcmp (typestr, "TLS"))
99         r->type = RS_CONN_TYPE_TLS;
100       else if (!strcmp (typestr, "DTLS"))
101         r->type = RS_CONN_TYPE_DTLS;
102       else
103         return rs_err_ctx_push_fl (ctx, RSE_CONFIG, __FILE__, __LINE__,
104                                    "invalid connection type: %s", typestr);
105       r->timeout = cfg_getint (cfg_config, "timeout");
106       r->retries = cfg_getint (cfg_config, "retries");
107
108       r->cacertfile = cfg_getstr (cfg_config, "cacertfile");
109       /*r->cacertpath = cfg_getstr (cfg_config, "cacertpath");*/
110       r->certfile = cfg_getstr (cfg_config, "certfile");
111       r->certkeyfile = cfg_getstr (cfg_config, "certkeyfile");
112
113       /* Add peers, one per server stanza.  */
114       for (j = 0; j < cfg_size (cfg_config, "server"); j++)
115         {
116           struct rs_peer *p = peer_create (ctx, &r->peers);
117           if (!p)
118             return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__,
119                                        NULL);
120           p->realm = r;
121
122           cfg_server = cfg_getnsec (cfg_config, "server", j);
123           rs_resolv (&p->addr, r->type, cfg_getstr (cfg_server, "hostname"),
124                      cfg_getstr (cfg_server, "service"));
125           p->secret = cfg_getstr (cfg_server, "secret");
126         }
127     }
128
129   /* Save config object in context, for freeing in
130      rs_context_destroy().  */
131   ctx->cfg =  cfg;
132   return RSE_OK;
133 }
134
135 struct rs_realm *
136 rs_conf_find_realm(struct rs_context *ctx, const char *name)
137 {
138   struct rs_realm *r;
139
140   for (r = ctx->realms; r; r = r->next)
141     if (!strcmp (r->name, name))
142         return r;
143   return NULL;
144 }