Restructure code, moving most code out of packet.c
[libradsec.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 #if defined (RS_ENABLE_TLS)
21 #include <regex.h>
22 #include "debug.h"
23 #include "rsp_list.h"
24 #include "../radsecproxy.h"
25 #endif
26 #include "rsp_debug.h"
27
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_peer *
95 _rs_peer_create (struct rs_context *ctx, struct rs_peer **rootp)
96 {
97   struct rs_peer *p;
98
99   p = (struct rs_peer *) rs_malloc (ctx, sizeof(*p));
100   if (p)
101     {
102       memset (p, 0, sizeof(struct rs_peer));
103       if (*rootp)
104         {
105           p->next = (*rootp)->next;
106           (*rootp)->next = p;
107         }
108       else
109         *rootp = p;
110     }
111   return p;
112 }
113
114 static void
115 _rs_peer_destroy (struct rs_peer *p)
116 {
117   assert (p);
118   assert (p->conn);
119   assert (p->conn->ctx);
120   /* NOTE: The peer object doesn't own its connection (conn).  */
121   if (p->addr)
122     {
123       evutil_freeaddrinfo (p->addr);
124       p->addr = NULL;
125     }
126   if (p->secret)
127     rs_free (p->conn->ctx, p->secret);
128   rs_free (p->conn->ctx, p);
129 }
130
131 void
132 rs_context_destroy (struct rs_context *ctx)
133 {
134   struct rs_realm *r = NULL;
135   struct rs_peer *p = NULL;
136
137   for (r = ctx->realms; r; )
138     {
139       struct rs_realm *tmp = r;
140       for (p = r->peers; p; )
141         {
142           struct rs_peer *tmp = p;
143           p = p->next;
144           _rs_peer_destroy (tmp);
145         }
146       r = r->next;
147       rs_free (ctx, tmp);
148     }
149
150   if (ctx->cfg)
151     cfg_free (ctx->cfg);
152   ctx->cfg = NULL;
153
154   rs_free (ctx, ctx);
155 }
156
157 int rs_context_set_alloc_scheme(struct rs_context *ctx,
158                                 struct rs_alloc_scheme *scheme)
159 {
160   return rs_err_ctx_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__, NULL);
161 }