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