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