port to new RADIUS client library
[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 <radius/client.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
43   debug_init ("libradsec");     /* radsecproxy compat, FIXME: remove */
44
45   if (ctx != NULL)
46     *ctx = h;
47
48   return RSE_OK;
49 }
50
51 /** Initialize freeradius dictionary.  */
52 int
53 rs_context_init_freeradius_dict (struct rs_context *ctx, const char *dict)
54 {
55   int r = RSE_OK;
56   size_t dictlen;
57   char *dir = NULL;
58   char *fn = NULL;
59
60   if (dict == NULL)
61     if (ctx->config != NULL && ctx->config->dictionary)
62       dict = ctx->config->dictionary;
63
64   dictlen = strlen (dict);
65   dir = rs_calloc (ctx, 1, dictlen + 1);
66   fn = rs_calloc (ctx, 1, dictlen + 1);
67   if (dir == NULL || fn == NULL)
68     {
69       r = rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
70       goto out;
71     }
72   strncpy (dir, dict, dictlen);
73   strncpy (fn, dict, dictlen);
74
75  out:
76   if (dir)
77     rs_free (ctx, dir);
78   if (fn)
79     rs_free (ctx, fn);
80   return r;
81 }
82
83 struct rs_error *
84 rs_resolv (struct evutil_addrinfo **addr,
85            rs_conn_type_t type,
86            const char *hostname,
87            const char *service)
88 {
89   int err;
90   struct evutil_addrinfo hints, *res = NULL;
91
92   memset (&hints, 0, sizeof(struct evutil_addrinfo));
93   hints.ai_family = AF_UNSPEC;
94   hints.ai_flags = AI_ADDRCONFIG;
95   switch (type)
96     {
97     case RS_CONN_TYPE_NONE:
98       return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
99     case RS_CONN_TYPE_TCP:
100       /* Fall through.  */
101     case RS_CONN_TYPE_TLS:
102       hints.ai_socktype = SOCK_STREAM;
103       hints.ai_protocol = IPPROTO_TCP;
104       break;
105     case RS_CONN_TYPE_UDP:
106       /* Fall through.  */
107     case RS_CONN_TYPE_DTLS:
108       hints.ai_socktype = SOCK_DGRAM;
109       hints.ai_protocol = IPPROTO_UDP;
110       break;
111     default:
112       return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
113     }
114   err = evutil_getaddrinfo (hostname, service, &hints, &res);
115   if (err)
116     return err_create (RSE_BADADDR, __FILE__, __LINE__,
117                        "%s:%s: bad host name or service name (%s)",
118                        hostname, service, evutil_gai_strerror(err));
119   *addr = res;                  /* Simply use first result.  */
120   return NULL;
121 }
122
123 void
124 rs_context_destroy (struct rs_context *ctx)
125 {
126   struct rs_realm *r = NULL;
127   struct rs_peer *p = NULL;
128
129   if (ctx->config)
130     {
131       for (r = ctx->config->realms; r; )
132         {
133           struct rs_realm *tmp = r;
134           for (p = r->peers; p; )
135             {
136               struct rs_peer *tmp = p;
137               if (p->addr)
138                 evutil_freeaddrinfo (p->addr);
139               p = p->next;
140               rs_free (ctx, tmp);
141             }
142           free (r->name);
143           r = r->next;
144           rs_free (ctx, tmp);
145         }
146     }
147
148   if (ctx->config)
149     {
150       if (ctx->config->cfg)
151         {
152           cfg_free (ctx->config->cfg);
153           ctx->config->cfg = NULL;
154         }
155       rs_free (ctx, ctx->config);
156     }
157
158   free (ctx);
159 }
160
161 int
162 rs_context_set_alloc_scheme (struct rs_context *ctx,
163                              struct rs_alloc_scheme *scheme)
164 {
165   return rs_err_ctx_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__, NULL);
166 }
167