Don't miscompile without --enable-tls.
[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, const char *dict)
32 {
33   int err = RSE_OK;
34   struct rs_context *h;
35   char *buf1 = NULL, *buf2 = NULL;
36   char *dir, *fn;
37
38   assert (dict);
39
40   if (ctx)
41     *ctx = NULL;
42   h = (struct rs_context *) malloc (sizeof(struct rs_context));
43   if (!h)
44     return RSE_NOMEM;
45
46   /* Initialize freeradius dictionary.  */
47   buf1 = malloc (strlen (dict) + 1);
48   buf2 = malloc (strlen (dict) + 1);
49   if (!buf1 || !buf2)
50     {
51       err = RSE_NOMEM;
52       goto err_out;
53     }
54   strcpy (buf1, dict);
55   dir = dirname (buf1);
56   strcpy (buf2, dict);
57   fn = basename (buf2);
58   if (dict_init (dir, fn) < 0)
59     {
60       err = RSE_FR;
61       goto err_out;
62     }
63   free (buf1);
64   free (buf2);
65
66 #if defined (RS_ENABLE_TLS)
67   ssl_init ();
68 #endif
69 #if defined (DEBUG)
70   fr_log_fp = stderr;
71   fr_debug_flag = 1;
72 #endif
73   debug_init ("libradsec");     /* radsecproxy compat, FIXME: remove */
74
75   memset (h, 0, sizeof(struct rs_context));
76   fr_randinit (&h->fr_randctx, 0);
77   fr_rand_seed (NULL, 0);
78
79   if (ctx)
80     *ctx = h;
81
82   return RSE_OK;
83
84  err_out:
85   if (buf1)
86     free (buf1);
87   if (buf2)
88     free (buf2);
89   if (h)
90     free (h);
91   return err;
92 }
93
94 struct rs_error *          /* FIXME: Return int as all the others?  */
95 rs_resolv (struct evutil_addrinfo **addr,
96            rs_conn_type_t type,
97            const char *hostname,
98            const char *service)
99 {
100   int err;
101   struct evutil_addrinfo hints, *res = NULL;
102
103   memset (&hints, 0, sizeof(struct evutil_addrinfo));
104   hints.ai_family = AF_INET;   /* IPv4 only.  TODO: Set AF_UNSPEC.  */
105   hints.ai_flags = AI_ADDRCONFIG;
106   switch (type)
107     {
108     case RS_CONN_TYPE_NONE:
109       return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
110     case RS_CONN_TYPE_TCP:
111       /* Fall through.  */
112     case RS_CONN_TYPE_TLS:
113       hints.ai_socktype = SOCK_STREAM;
114       hints.ai_protocol = IPPROTO_TCP;
115       break;
116     case RS_CONN_TYPE_UDP:
117       /* Fall through.  */
118     case RS_CONN_TYPE_DTLS:
119       hints.ai_socktype = SOCK_DGRAM;
120       hints.ai_protocol = IPPROTO_UDP;
121       break;
122     default:
123       return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
124     }
125   err = evutil_getaddrinfo (hostname, service, &hints, &res);
126   if (err)
127     return err_create (RSE_BADADDR, __FILE__, __LINE__,
128                        "%s:%s: bad host name or service name (%s)",
129                        hostname, service, evutil_gai_strerror(err));
130   *addr = res;                  /* Simply use first result.  */
131   return NULL;
132 }
133
134 static void
135 _rs_peer_destroy (struct rs_peer *p)
136 {
137   assert (p);
138   assert (p->conn);
139   assert (p->conn->ctx);
140
141   /* NOTE: The peer object doesn't own conn, nor realm.  */
142   /* NOTE: secret is owned by config */
143   if (p->addr)
144     {
145       evutil_freeaddrinfo (p->addr);
146       p->addr = NULL;
147     }
148   rs_free (p->conn->ctx, p);
149 }
150
151 void
152 rs_context_destroy (struct rs_context *ctx)
153 {
154   struct rs_realm *r = NULL;
155   struct rs_peer *p = NULL;
156
157   for (r = ctx->realms; r; )
158     {
159       struct rs_realm *tmp = r;
160       for (p = r->peers; p; )
161         {
162           struct rs_peer *tmp = p;
163           p = p->next;
164           _rs_peer_destroy (tmp);
165         }
166       r = r->next;
167       rs_free (ctx, tmp);
168     }
169
170   if (ctx->cfg)
171     cfg_free (ctx->cfg);
172   ctx->cfg = NULL;
173
174   rs_free (ctx, ctx);
175 }
176
177 int
178 rs_context_set_alloc_scheme (struct rs_context *ctx,
179                              struct rs_alloc_scheme *scheme)
180 {
181   return rs_err_ctx_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__, NULL);
182 }
183