d14ad5057c39b6359fb7d099997c689b64b6b101
[libradsec.git] / lib / radsec.c
1 /* Copyright 2010, 2011 NORDUnet A/S. All rights reserved.
2    See LICENSE 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 struct rs_error *
52 rs_resolve (struct evutil_addrinfo **addr,
53             rs_conn_type_t type,
54             const char *hostname,
55             const char *service)
56 {
57   int err;
58   struct evutil_addrinfo hints, *res = NULL;
59
60   memset (&hints, 0, sizeof(struct evutil_addrinfo));
61   hints.ai_family = AF_UNSPEC;
62   hints.ai_flags = AI_ADDRCONFIG;
63   switch (type)
64     {
65     case RS_CONN_TYPE_NONE:
66       return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
67     case RS_CONN_TYPE_TCP:
68       /* Fall through.  */
69     case RS_CONN_TYPE_TLS:
70       hints.ai_socktype = SOCK_STREAM;
71       hints.ai_protocol = IPPROTO_TCP;
72       break;
73     case RS_CONN_TYPE_UDP:
74       /* Fall through.  */
75     case RS_CONN_TYPE_DTLS:
76       hints.ai_socktype = SOCK_DGRAM;
77       hints.ai_protocol = IPPROTO_UDP;
78       break;
79     default:
80       return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
81     }
82   err = evutil_getaddrinfo (hostname, service, &hints, &res);
83   if (err)
84     return err_create (RSE_BADADDR, __FILE__, __LINE__,
85                        "%s:%s: bad host name or service name (%s)",
86                        hostname, service, evutil_gai_strerror(err));
87   *addr = res;                  /* Simply use first result.  */
88   return NULL;
89 }
90
91 void
92 rs_context_destroy (struct rs_context *ctx)
93 {
94   struct rs_realm *r = NULL;
95   struct rs_peer *p = NULL;
96
97   if (ctx->config)
98     {
99       for (r = ctx->config->realms; r; )
100         {
101           struct rs_realm *tmp = r;
102           for (p = r->peers; p; )
103             {
104               struct rs_peer *tmp = p;
105               if (p->addr_cache)
106                 {
107                   evutil_freeaddrinfo (p->addr_cache);
108                   p->addr_cache = NULL;
109                 }
110               p = p->next;
111               rs_free (ctx, tmp);
112             }
113           free (r->name);
114           rs_free (ctx, r->transport_cred);
115           r = r->next;
116           rs_free (ctx, tmp);
117         }
118     }
119
120   if (ctx->config)
121     {
122       if (ctx->config->cfg)
123         {
124           cfg_free (ctx->config->cfg);
125           ctx->config->cfg = NULL;
126         }
127       rs_free (ctx, ctx->config);
128     }
129
130   free (ctx);
131 }
132
133 int
134 rs_context_set_alloc_scheme (struct rs_context *ctx,
135                              struct rs_alloc_scheme *scheme)
136 {
137   return rs_err_ctx_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__, NULL);
138 }
139