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