Define RS_FREERADIUS_DICT and use it when missing "dictionary" in config.
[radsecproxy.git] / lib / radsec.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 <stdio.h>
9 #include <stdlib.h>
10 #include <stdint.h>
11 #include <string.h>
12 #include <libgen.h>
13 #include <assert.h>
14
15 #include <freeradius/libradius.h>
16 #include <event2/event.h>
17 #include <event2/util.h>
18 #include <radsec/radsec.h>
19 #include <radsec/radsec-impl.h>
20 #include "err.h"
21 #include "debug.h"
22 #include "rsp_debug.h"
23 #if defined (RS_ENABLE_TLS)
24 #include <regex.h>
25 #include "rsp_list.h"
26 #include "../radsecproxy.h"
27 #endif
28
29 /* Public functions.  */
30 int
31 rs_context_create (struct rs_context **ctx)
32 {
33   struct rs_context *h;
34
35   h = calloc (1, sizeof(*h));
36   if (h == NULL)
37     return RSE_NOMEM;
38
39 #if defined (RS_ENABLE_TLS)
40   ssl_init ();
41 #endif
42 #if defined (DEBUG)
43   fr_log_fp = stderr;
44   fr_debug_flag = 1;
45 #endif
46   debug_init ("libradsec");     /* radsecproxy compat, FIXME: remove */
47
48   fr_randinit (&h->fr_randctx, 0);
49   fr_rand_seed (NULL, 0);
50
51   if (ctx != NULL)
52     *ctx = h;
53
54   return RSE_OK;
55 }
56
57 /** Initialize freeradius dictionary.  */
58 int
59 rs_context_init_freeradius_dict (struct rs_context *ctx, const char *dict)
60 {
61   int r = RSE_OK;
62   size_t dictlen;
63   char *dir = NULL;
64   char *fn = NULL;
65
66   if (dict == NULL)
67     if (ctx->config != NULL)
68       dict = ctx->config->dictionary;
69
70   if (dict == NULL)
71     dict = RS_FREERADIUS_DICT;
72
73   dictlen = strlen (dict);
74   dir = rs_calloc (ctx, 1, dictlen + 1);
75   fn = rs_calloc (ctx, 1, dictlen + 1);
76   if (dir == NULL || fn == NULL)
77     {
78       r = rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
79       goto out;
80     }
81   strncpy (dir, dict, dictlen);
82   strncpy (fn, dict, dictlen);
83
84   if (dict_init (dirname (dir), basename (fn)) < 0)
85     {
86       r = rs_err_ctx_push_fl (ctx, RSE_FR, __FILE__, __LINE__, "dict_init");
87       goto out;
88     }
89
90  out:
91   if (dir)
92     rs_free (ctx, dir);
93   if (fn)
94     rs_free (ctx, fn);
95   return r;
96 }
97
98 struct rs_error *          /* FIXME: Return int as all the others?  */
99 rs_resolv (struct evutil_addrinfo **addr,
100            rs_conn_type_t type,
101            const char *hostname,
102            const char *service)
103 {
104   int err;
105   struct evutil_addrinfo hints, *res = NULL;
106
107   memset (&hints, 0, sizeof(struct evutil_addrinfo));
108   hints.ai_family = AF_INET;   /* IPv4 only.  TODO: Set AF_UNSPEC.  */
109   hints.ai_flags = AI_ADDRCONFIG;
110   switch (type)
111     {
112     case RS_CONN_TYPE_NONE:
113       return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
114     case RS_CONN_TYPE_TCP:
115       /* Fall through.  */
116     case RS_CONN_TYPE_TLS:
117       hints.ai_socktype = SOCK_STREAM;
118       hints.ai_protocol = IPPROTO_TCP;
119       break;
120     case RS_CONN_TYPE_UDP:
121       /* Fall through.  */
122     case RS_CONN_TYPE_DTLS:
123       hints.ai_socktype = SOCK_DGRAM;
124       hints.ai_protocol = IPPROTO_UDP;
125       break;
126     default:
127       return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
128     }
129   err = evutil_getaddrinfo (hostname, service, &hints, &res);
130   if (err)
131     return err_create (RSE_BADADDR, __FILE__, __LINE__,
132                        "%s:%s: bad host name or service name (%s)",
133                        hostname, service, evutil_gai_strerror(err));
134   *addr = res;                  /* Simply use first result.  */
135   return NULL;
136 }
137
138 void
139 rs_context_destroy (struct rs_context *ctx)
140 {
141   struct rs_realm *r = NULL;
142   struct rs_peer *p = NULL;
143
144   if (ctx->config)
145     {
146       for (r = ctx->config->realms; r; )
147         {
148           struct rs_realm *tmp = r;
149           for (p = r->peers; p; )
150             {
151               struct rs_peer *tmp = p;
152               if (p->addr)
153                 evutil_freeaddrinfo (p->addr);
154               p = p->next;
155               rs_free (ctx, tmp);
156             }
157           free (r->name);
158           r = r->next;
159           rs_free (ctx, tmp);
160         }
161     }
162
163   if (ctx->config)
164     {
165       if (ctx->config->cfg)
166         {
167           cfg_free (ctx->config->cfg);
168           ctx->config->cfg = NULL;
169         }
170       rs_free (ctx, ctx->config);
171     }
172
173   free (ctx);
174 }
175
176 int
177 rs_context_set_alloc_scheme (struct rs_context *ctx,
178                              struct rs_alloc_scheme *scheme)
179 {
180   return rs_err_ctx_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__, NULL);
181 }
182