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