Enable tls psk
[radsecproxy.git] / radsec.c
1 /* Copyright 2010-2013 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 "radsecproxy/debug.h"
23 #if defined (RS_ENABLE_TLS)
24 #include "tls.h"
25 #include <regex.h>
26 #include "radsecproxy/list.h"
27 #include "radsecproxy/radsecproxy.h"
28 #endif
29
30 /* Public functions.  */
31 int
32 rs_context_create (struct rs_context **ctx)
33 {
34   struct rs_context *h;
35
36 #if defined (RS_ENABLE_TLS)
37   if (tls_init ())
38     return RSE_SSLERR;
39 #endif
40
41   h = calloc (1, sizeof(*h));
42   if (h == NULL)
43     return RSE_NOMEM;
44
45   debug_init ("libradsec");     /* radsecproxy compat, FIXME: remove */
46
47   if (ctx != NULL)
48     *ctx = h;
49
50   return RSE_OK;
51 }
52
53 struct rs_error *
54 rs_resolve (struct evutil_addrinfo **addr,
55             rs_conn_type_t type,
56             const char *hostname,
57             const char *service)
58 {
59   int err;
60   struct evutil_addrinfo hints, *res = NULL;
61
62   memset (&hints, 0, sizeof(struct evutil_addrinfo));
63   hints.ai_family = AF_UNSPEC;
64   hints.ai_flags = AI_ADDRCONFIG;
65   switch (type)
66     {
67     case RS_CONN_TYPE_NONE:
68       return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
69     case RS_CONN_TYPE_TCP:
70       /* Fall through.  */
71     case RS_CONN_TYPE_TLS:
72       hints.ai_socktype = SOCK_STREAM;
73       hints.ai_protocol = IPPROTO_TCP;
74       break;
75     case RS_CONN_TYPE_UDP:
76       /* Fall through.  */
77     case RS_CONN_TYPE_DTLS:
78       hints.ai_socktype = SOCK_DGRAM;
79       hints.ai_protocol = IPPROTO_UDP;
80       break;
81     default:
82       return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
83     }
84   err = evutil_getaddrinfo (hostname, service, &hints, &res);
85   if (err)
86     return err_create (RSE_BADADDR, __FILE__, __LINE__,
87                        "%s:%s: bad host name or service name (%s)",
88                        hostname, service, evutil_gai_strerror(err));
89   *addr = res;                  /* Simply use first result.  */
90   return NULL;
91 }
92
93 void
94 rs_context_destroy (struct rs_context *ctx)
95 {
96   struct rs_realm *r = NULL;
97   struct rs_peer *p = NULL;
98
99   if (ctx->config)
100     {
101       for (r = ctx->config->realms; r; )
102         {
103           struct rs_realm *tmp = r;
104           for (p = r->peers; p; )
105             {
106               struct rs_peer *tmp = p;
107               if (p->addr_cache)
108                 {
109                   evutil_freeaddrinfo (p->addr_cache);
110                   p->addr_cache = NULL;
111                 }
112               p = p->next;
113               rs_free (ctx, tmp);
114             }
115           free (r->name);
116           rs_free (ctx, r->transport_cred);
117           r = r->next;
118           rs_free (ctx, tmp);
119         }
120     }
121
122   if (ctx->config)
123     {
124       if (ctx->config->cfg)
125         {
126           cfg_free (ctx->config->cfg);
127           ctx->config->cfg = NULL;
128         }
129       rs_free (ctx, ctx->config);
130     }
131
132   free (ctx);
133 }
134
135 int
136 rs_context_set_alloc_scheme (struct rs_context *ctx,
137                              struct rs_alloc_scheme *scheme)
138 {
139   return rs_err_ctx_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__, NULL);
140 }
141