WIP
[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_ctx_err_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__,
73                              "%s: NYI", __func__);
74 }
75
76 int rs_context_config_read(struct rs_handle *ctx, const char *config_file)
77 {
78   return rs_ctx_err_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__,
79                              "%s: NYI", __func__);
80 }
81
82 int rs_conn_create(struct rs_handle *ctx, struct rs_connection **conn)
83 {
84   struct rs_connection *c;
85
86   c = (struct rs_connection *) malloc (sizeof(struct rs_connection));
87   if (c)
88     {
89       memset (c, 0, sizeof(struct rs_connection));
90       c->ctx = ctx;
91     }
92   if (conn)
93     *conn = c;
94   return c ? RSE_OK : rs_ctx_err_push (ctx, RSE_NOMEM, NULL);
95 }
96
97 struct addrinfo *
98 _resolv (struct rs_connection *conn, const char *hostname, int port)
99 {
100   int err;
101   char portstr[6];
102   struct evutil_addrinfo hints, *res = NULL;
103
104   snprintf (portstr, sizeof(portstr), "%d", port);
105   memset (&hints, 0, sizeof(struct evutil_addrinfo));
106   hints.ai_family = AF_UNSPEC;  /* v4 or v6.  */
107   hints.ai_flags = AI_ADDRCONFIG;
108   switch (conn->type)
109     {
110     case RS_CONN_TYPE_NONE:
111       rs_conn_err_push_fl (conn, RSE_INVALID_CONN, __FILE__, __LINE__, NULL);
112       return NULL;
113     case RS_CONN_TYPE_TCP:
114       /* Fall through.  */
115     case RS_CONN_TYPE_TLS:
116       hints.ai_socktype = SOCK_STREAM;
117       hints.ai_protocol = IPPROTO_TCP;
118       break;
119     case RS_CONN_TYPE_UDP:
120       /* Fall through.  */
121     case RS_CONN_TYPE_DTLS:
122       hints.ai_socktype = SOCK_DGRAM;
123       hints.ai_protocol = IPPROTO_UDP;
124       break;
125     }
126   err = evutil_getaddrinfo (hostname, portstr, &hints, &res);
127   if (err)
128     rs_conn_err_push_fl (conn, RSE_BADADDR, __FILE__, __LINE__,
129                          "%s:%d: bad host name or port (%s)",
130                          hostname, port, evutil_gai_strerror(err));
131   return res;                   /* Simply use first result.  */
132 }
133
134 static struct rs_peer *
135 _peer_new (struct rs_connection *conn, const char *hostname, int port)
136 {
137   struct rs_peer *p;
138   struct evutil_addrinfo *addr;
139
140   addr = _resolv (conn, hostname, port);
141   if (!addr)
142     return NULL;
143
144   p = (struct rs_peer *) malloc (sizeof(*p));
145   if (p)
146     {
147       memset (p, 0, sizeof(struct rs_peer));
148       p->conn = conn;
149       p->fd = -1;
150       p->addr = addr;
151       p->next = conn->peers;
152       if (conn->peers)
153         conn->peers->next = p;
154       else
155         conn->peers = p;
156     }
157   else
158     {
159       evutil_freeaddrinfo (addr);
160       rs_conn_err_push_fl (conn, RSE_NOMEM, __FILE__, __LINE__, NULL);
161     }
162   return p;
163 }
164
165 int
166 rs_conn_add_server(struct rs_connection *conn, struct rs_peer **server,
167                    rs_conn_type_t type, const char *hostname, int port)
168 {
169   struct rs_peer *srv;
170
171   if (conn->type == RS_CONN_TYPE_NONE)
172     conn->type = type;
173   else if (conn->type != type)
174     return rs_conn_err_push (conn, RSE_CONN_TYPE_MISMATCH, NULL);
175
176   srv = _peer_new (conn, hostname, port);
177   if (srv)
178     {
179       srv->timeout = 10;
180       srv->tries = 3;
181     }
182   if (*server)
183     *server = srv;
184   return srv ? RSE_OK : -1;
185 }
186
187 void rs_server_set_timeout(struct rs_peer *server, int timeout)
188 {
189   server->timeout = timeout;
190 }
191 void rs_server_set_tries(struct rs_peer *server, int tries)
192 {
193   server->tries = tries;
194 }
195 int rs_server_set_secret(struct rs_peer *server, const char *secret)
196 {
197   if (server->secret)
198     free (server->secret);
199   server->secret = (char *) malloc (strlen(secret) + 1);
200   if (!server->secret)
201     return rs_conn_err_push (server->conn, RSE_NOMEM, NULL);
202   strcpy (server->secret, secret);
203   return RSE_OK;
204 }
205
206 int rs_conn_add_listener(struct rs_connection *conn, rs_conn_type_t type, const char *hostname, int port)
207 {
208   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
209                               "%s: NYI", __func__);
210 }
211
212 void
213 rs_conn_destroy(struct rs_connection *conn)
214 {
215   struct rs_peer *p;
216
217 #warning "TODO: Disconnect active_peer."
218
219   for (p = conn->peers; p; p = p->next)
220     {
221       if (p->addr)
222         evutil_freeaddrinfo (p->addr);
223       if (p->secret)
224         rs_free (conn->ctx, p->secret);
225     }
226
227   if (conn->evb)
228     event_base_free (conn->evb);
229 }
230
231 int rs_conn_set_eventbase(struct rs_connection *conn, struct event_base *eb)
232 {
233   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
234                               "%s: NYI", __func__);
235 }
236
237 int rs_conn_set_callbacks(struct rs_connection *conn, struct rs_conn_callbacks *cb)
238 {
239   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
240                               "%s: NYI", __func__);
241 }
242
243 int rs_conn_set_server(struct rs_connection *conn, const char *name)
244 {
245   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
246                               "%s: NYI", __func__);
247 }
248
249 int rs_conn_get_current_server(struct rs_connection *conn, const char *name, size_t buflen)
250 {
251   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
252                               "%s: NYI", __func__);
253 }
254