309c305605a2ecf2cd2b3d505f7f7527693901ec
[radsecproxy.git] / lib / radsec.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <libgen.h>
6
7 #include <freeradius/libradius.h>
8 #include "libradsec.h"
9 #include "libradsec-impl.h"
10
11 int
12 rs_context_create(struct rs_handle **ctx, const char *dict)
13 {
14   struct rs_handle *h;
15
16   if (ctx)
17     *ctx = NULL;
18   h = (struct rs_handle *) malloc (sizeof(struct rs_handle));
19   if (h)
20     {
21       char *buf1 = NULL, *buf2 = NULL;
22       char *dir, *fn;
23
24       buf1 = malloc (strlen (dict) + 1);
25       buf2 = malloc (strlen (dict) + 1);
26       if (!buf1 || !buf2)
27         {
28           free (h);
29           if (buf1)
30             free (buf1);
31           if (buf2)
32             free (buf2);
33           return RSE_NOMEM;
34         }
35       strcpy (buf1, dict);
36       dir = dirname (buf1);
37       strcpy (buf2, dict);
38       fn = basename (buf2);
39       if (dict_init (dir, fn) < 0)
40         {
41           free (h);
42           return RSE_SOME_ERROR;
43         }
44       free (buf1);
45       free (buf2);
46 #if defined (DEBUG)
47       fr_log_fp = stderr;
48       fr_debug_flag = 1;
49 #endif
50
51       memset (h, 0, sizeof(struct rs_handle));
52       fr_randinit (&h->fr_randctx, 0);
53       fr_rand_seed (NULL, 0);
54
55       if (ctx)
56         *ctx = h;
57     }
58   return h ? RSE_OK : RSE_NOMEM;
59 }
60
61 void rs_context_destroy(struct rs_handle *ctx)
62 {
63   free (ctx);
64 }
65
66 int rs_context_set_alloc_scheme(struct rs_handle *ctx, struct rs_alloc_scheme *scheme)
67 {
68   return rs_ctx_err_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__,
69                              "%s: NYI", __func__);
70 }
71
72 int rs_context_config_read(struct rs_handle *ctx, const char *config_file)
73 {
74   return rs_ctx_err_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__,
75                              "%s: NYI", __func__);
76 }
77
78 int rs_conn_create(struct rs_handle *ctx, struct rs_connection **conn)
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       c->peers.next = &c->peers;
88     }
89   if (conn)
90     *conn = c;
91   return c ? RSE_OK : rs_ctx_err_push (ctx, RSE_NOMEM, NULL);
92 }
93
94 struct addrinfo *
95 _resolv (const char *host, int port)
96 {
97   return NULL;
98 }
99
100 int rs_conn_add_server(struct rs_connection *conn, struct rs_peer **server, rs_conn_type_t type, const char *host, int port)
101 {
102   struct rs_peer *srv;
103
104   if (conn->type == RS_CONN_TYPE_NONE)
105     conn->type = type;
106   else if (conn->type != type)
107     return rs_conn_err_push (conn, RSE_CONN_TYPE_MISMATCH, NULL);
108
109   srv = (struct rs_peer *) malloc (sizeof(struct rs_peer));
110   if (srv)
111     {
112       memset (srv, 0, sizeof(struct rs_peer));
113       srv->conn = conn;
114       srv->addr = _resolv (host, port);
115       srv->timeout = 10;
116       srv->tries = 3;
117       srv->next = conn->peers.next;
118       conn->peers.next = srv;
119     }
120   if (*server)
121     *server = srv;
122   return srv ? RSE_OK : rs_conn_err_push (conn, RSE_NOMEM, NULL);
123 }
124
125 void rs_server_set_timeout(struct rs_peer *server, int timeout)
126 {
127   server->timeout = timeout;
128 }
129 void rs_server_set_tries(struct rs_peer *server, int tries)
130 {
131   server->tries = tries;
132 }
133 int rs_server_set_secret(struct rs_peer *server, const char *secret)
134 {
135   if (server->secret)
136     free (server->secret);
137   server->secret = (char *) malloc (strlen(secret) + 1);
138   if (!server->secret)
139     return rs_conn_err_push (server->conn, RSE_NOMEM, NULL);
140   strcpy (server->secret, secret);
141   return RSE_OK;
142 }
143
144 int rs_conn_add_listener(struct rs_connection *conn, rs_conn_type_t type, const char *host, int port)
145 {
146   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
147                               "%s: NYI", __func__);
148 }
149
150 int rs_conn_destroy(struct rs_connection  *conn)
151 {
152   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
153                               "%s: NYI", __func__);
154 }
155
156 int rs_conn_set_eventbase(struct rs_connection *conn, struct event_base *eb)
157 {
158   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
159                               "%s: NYI", __func__);
160 }
161
162 int rs_conn_set_callbacks(struct rs_connection *conn, struct rs_conn_callbacks *cb)
163 {
164   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
165                               "%s: NYI", __func__);
166 }
167
168 int rs_conn_set_server(struct rs_connection *conn, const char *name)
169 {
170   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
171                               "%s: NYI", __func__);
172 }
173
174 int rs_conn_get_current_server(struct rs_connection *conn, const char *name, size_t buflen)
175 {
176   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
177                               "%s: NYI", __func__);
178 }
179
180 int rs_conn_open(struct rs_connection *conn)
181 {
182   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
183                               "%s: NYI", __func__);
184 }