WIP -- reading configuration.
[radsecproxy.git] / lib / radsec.c
1 /* See the file COPYING for licensing information.  */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdint.h>
6 #include <string.h>
7 #include <libgen.h>
8
9 #include <freeradius/libradius.h>
10 #include <event2/event.h>
11 #include <event2/util.h>
12 #include <radsec/radsec.h>
13 #include <radsec/radsec-impl.h>
14
15 int
16 rs_context_create(struct rs_handle **ctx, const char *dict)
17 {
18   struct rs_handle *h;
19
20   if (ctx)
21     *ctx = NULL;
22   h = (struct rs_handle *) malloc (sizeof(struct rs_handle));
23   if (h)
24     {
25       char *buf1 = NULL, *buf2 = NULL;
26       char *dir, *fn;
27
28       buf1 = malloc (strlen (dict) + 1);
29       buf2 = malloc (strlen (dict) + 1);
30       if (!buf1 || !buf2)
31         {
32           free (h);
33           if (buf1)
34             free (buf1);
35           if (buf2)
36             free (buf2);
37           return RSE_NOMEM;
38         }
39       strcpy (buf1, dict);
40       dir = dirname (buf1);
41       strcpy (buf2, dict);
42       fn = basename (buf2);
43       if (dict_init (dir, fn) < 0)
44         {
45           free (h);
46           return RSE_SOME_ERROR;
47         }
48       free (buf1);
49       free (buf2);
50 #if defined (DEBUG)
51       fr_log_fp = stderr;
52       fr_debug_flag = 1;
53 #endif
54
55       memset (h, 0, sizeof(struct rs_handle));
56       fr_randinit (&h->fr_randctx, 0);
57       fr_rand_seed (NULL, 0);
58
59       if (ctx)
60         *ctx = h;
61     }
62   return h ? RSE_OK : RSE_NOMEM;
63 }
64
65 void rs_context_destroy(struct rs_handle *ctx)
66 {
67   free (ctx);
68 }
69
70 int rs_context_set_alloc_scheme(struct rs_handle *ctx, struct rs_alloc_scheme *scheme)
71 {
72   return rs_err_ctx_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__,
73                              "%s: NYI", __func__);
74 }
75
76 int
77 rs_conn_create(struct rs_handle *ctx, struct rs_connection **conn,
78                const char *config)
79 {
80   struct rs_connection *c;
81
82   c = (struct rs_connection *) malloc (sizeof(struct rs_connection));
83   if (c)
84     {
85       memset (c, 0, sizeof(struct rs_connection));
86       c->ctx = ctx;
87       if (config)
88         {
89           struct rs_realm *r = rs_conf_find_realm (ctx, config);
90           if (r)
91             {
92               struct rs_peer *p;
93
94               c->type = r->type;
95               c->peers = r->peers; /* FIXME: Copy instead?  */
96               for (p = c->peers; p; p = p->next)
97                 p->conn = c;
98             }
99         }
100     }
101   if (conn)
102     *conn = c;
103   return c ? RSE_OK : rs_err_ctx_push (ctx, RSE_NOMEM, NULL);
104 }
105
106 void
107 rs_conn_set_type(struct rs_connection *conn, rs_conn_type_t type)
108 {
109   conn->type = type;
110 }
111
112
113 struct rs_error *
114 _rs_resolv (struct evutil_addrinfo **addr, rs_conn_type_t type,
115             const char *hostname, const char *service)
116 {
117   int err;
118   struct evutil_addrinfo hints, *res = NULL;
119
120   memset (&hints, 0, sizeof(struct evutil_addrinfo));
121   hints.ai_family = AF_UNSPEC;  /* v4 or v6.  */
122   hints.ai_flags = AI_ADDRCONFIG;
123   switch (type)
124     {
125     case RS_CONN_TYPE_NONE:
126       return _rs_err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
127     case RS_CONN_TYPE_TCP:
128       /* Fall through.  */
129     case RS_CONN_TYPE_TLS:
130       hints.ai_socktype = SOCK_STREAM;
131       hints.ai_protocol = IPPROTO_TCP;
132       break;
133     case RS_CONN_TYPE_UDP:
134       /* Fall through.  */
135     case RS_CONN_TYPE_DTLS:
136       hints.ai_socktype = SOCK_DGRAM;
137       hints.ai_protocol = IPPROTO_UDP;
138       break;
139     }
140   err = evutil_getaddrinfo (hostname, service, &hints, &res);
141   if (err)
142     return _rs_err_create (RSE_BADADDR, __FILE__, __LINE__,
143                            "%s:%s: bad host name or service name (%s)",
144                            hostname, service, evutil_gai_strerror(err));
145   *addr = res;                  /* Simply use first result.  */
146   return NULL;
147 }
148
149 struct rs_peer *
150 _rs_peer_create (struct rs_handle *ctx, struct rs_peer **rootp)
151 {
152   struct rs_peer *p;
153
154   p = (struct rs_peer *) rs_malloc (ctx, sizeof(*p));
155   if (p)
156     {
157       memset (p, 0, sizeof(struct rs_peer));
158       p->fd = -1;
159       if (*rootp)
160         (*rootp)->next = p;
161       else
162         *rootp = p;
163     }
164   return p;
165 }
166
167 int
168 rs_server_create (struct rs_connection *conn, struct rs_peer **server)
169 {
170   struct rs_peer *srv;
171
172   srv = _rs_peer_create (conn->ctx, &conn->peers);
173   if (srv)
174     {
175       srv->conn = conn;
176       srv->timeout = 1;
177       srv->tries = 3;
178     }
179   else
180     return rs_err_conn_push_fl (conn, RSE_NOMEM, __FILE__, __LINE__, NULL);
181   if (*server)
182     *server = srv;
183   return RSE_OK;
184 }
185
186 int
187 rs_server_set_address (struct rs_peer *server, const char *hostname,
188                        const char *service)
189 {
190   struct rs_error *err;
191
192   err = _rs_resolv (&server->addr, server->conn->type, hostname, service);
193   if (err)
194     return _rs_err_conn_push_err (server->conn, err);
195   return RSE_OK;
196 }
197
198 void
199 rs_server_set_timeout (struct rs_peer *server, int timeout)
200 {
201   server->timeout = timeout;
202 }
203 void
204 rs_server_set_tries (struct rs_peer *server, int tries)
205 {
206   server->tries = tries;
207 }
208
209 int
210 rs_server_set_secret (struct rs_peer *server, const char *secret)
211 {
212   if (server->secret)
213     free (server->secret);
214   server->secret = (char *) malloc (strlen(secret) + 1);
215   if (!server->secret)
216     return rs_err_conn_push (server->conn, RSE_NOMEM, NULL);
217   strcpy (server->secret, secret);
218   return RSE_OK;
219 }
220
221 int
222 rs_conn_add_listener (struct rs_connection *conn, rs_conn_type_t type,
223                       const char *hostname, int port)
224 {
225   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
226                               "%s: NYI", __func__);
227 }
228
229 void
230 rs_conn_destroy (struct rs_connection *conn)
231 {
232   struct rs_peer *p;
233
234 #warning "TODO: Disconnect active_peer."
235
236   for (p = conn->peers; p; p = p->next)
237     {
238       if (p->addr)
239         evutil_freeaddrinfo (p->addr);
240       if (p->secret)
241         rs_free (conn->ctx, p->secret);
242     }
243
244   if (conn->evb)
245     event_base_free (conn->evb);
246 }
247
248 int rs_conn_set_eventbase(struct rs_connection *conn, struct event_base *eb)
249 {
250   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
251                               "%s: NYI", __func__);
252 }
253
254 int rs_conn_set_callbacks(struct rs_connection *conn, struct rs_conn_callbacks *cb)
255 {
256   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
257                               "%s: NYI", __func__);
258 }
259
260 int rs_conn_get_current_server(struct rs_connection *conn, const char *name, size_t buflen)
261 {
262   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
263                               "%s: NYI", __func__);
264 }
265