1 /* Copyright 2010, 2011 NORDUnet A/S. All rights reserved.
2 See the file COPYING for licensing information. */
4 #if defined HAVE_CONFIG_H
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>
22 #include "rsp_debug.h"
23 #if defined (RS_ENABLE_TLS)
26 #include "../radsecproxy.h"
29 /* Public functions. */
31 rs_context_create (struct rs_context **ctx)
35 h = calloc (1, sizeof(*h));
39 #if defined (RS_ENABLE_TLS)
46 debug_init ("libradsec"); /* radsecproxy compat, FIXME: remove */
48 fr_randinit (&h->fr_randctx, 0);
49 fr_rand_seed (NULL, 0);
57 /** Initialize freeradius dictionary. */
59 rs_context_init_freeradius_dict (struct rs_context *ctx, const char *dict)
67 if (ctx->config != NULL && ctx->config->dictionary)
68 dict = ctx->config->dictionary;
71 dict = RS_FREERADIUS_DICT;
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)
78 r = rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
81 strncpy (dir, dict, dictlen);
82 strncpy (fn, dict, dictlen);
84 if (dict_init (dirname (dir), basename (fn)) < 0)
86 r = rs_err_ctx_push_fl (ctx, RSE_FR, __FILE__, __LINE__,
87 "failing dict_init(\"%s\")", dict);
100 rs_resolv (struct evutil_addrinfo **addr,
102 const char *hostname,
106 struct evutil_addrinfo hints, *res = NULL;
108 memset (&hints, 0, sizeof(struct evutil_addrinfo));
109 hints.ai_family = AF_UNSPEC;
110 hints.ai_flags = AI_ADDRCONFIG;
113 case RS_CONN_TYPE_NONE:
114 return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
115 case RS_CONN_TYPE_TCP:
117 case RS_CONN_TYPE_TLS:
118 hints.ai_socktype = SOCK_STREAM;
119 hints.ai_protocol = IPPROTO_TCP;
121 case RS_CONN_TYPE_UDP:
123 case RS_CONN_TYPE_DTLS:
124 hints.ai_socktype = SOCK_DGRAM;
125 hints.ai_protocol = IPPROTO_UDP;
128 return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
130 err = evutil_getaddrinfo (hostname, service, &hints, &res);
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. */
140 rs_context_destroy (struct rs_context *ctx)
142 struct rs_realm *r = NULL;
143 struct rs_peer *p = NULL;
147 for (r = ctx->config->realms; r; )
149 struct rs_realm *tmp = r;
150 for (p = r->peers; p; )
152 struct rs_peer *tmp = p;
154 evutil_freeaddrinfo (p->addr);
159 rs_free (ctx, r->transport_cred);
167 if (ctx->config->cfg)
169 cfg_free (ctx->config->cfg);
170 ctx->config->cfg = NULL;
172 rs_free (ctx, ctx->config);
179 rs_context_set_alloc_scheme (struct rs_context *ctx,
180 struct rs_alloc_scheme *scheme)
182 return rs_err_ctx_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__, NULL);