Radcli library 1.5.2
A simple radius library
Loading...
Searching...
No Matches
buildreq.c
1/*
2 * Copyright (C) 1995,1997 Lars Fenneberg
3 *
4 * See the file COPYRIGHT for the respective terms and conditions.
5 * If the file is missing contact me at lf@elemental.net
6 * and I'll send you a copy.
7 *
8 */
9#include <config.h>
10#include <includes.h>
11#include <radcli/radcli.h>
12#include "util.h"
13
20
25static unsigned char rc_get_id()
26{
27 return (unsigned char)(random() & UCHAR_MAX);
28}
29
45void rc_buildreq(rc_handle const *rh, SEND_DATA * data, int code, char *server,
46 unsigned short port, char *secret, int timeout, int retries)
47{
48 data->server = server;
49 data->secret = secret;
50 data->svc_port = port;
51 data->seq_nbr = rc_get_id();
52 data->timeout = timeout;
53 data->retries = retries;
54 data->code = code;
55}
56
81int rc_aaa_ctx(rc_handle * rh, RC_AAA_CTX ** ctx, uint32_t nas_port,
82 VALUE_PAIR * send, VALUE_PAIR ** received, char *msg,
83 int add_nas_port, rc_standard_codes request_type)
84{
85 SERVER *aaaserver;
86 rc_type type;
87
88 if (rh->so_type == RC_SOCKET_TLS || rh->so_type == RC_SOCKET_DTLS ||
89 request_type != PW_ACCOUNTING_REQUEST) {
90 aaaserver = rc_conf_srv(rh, "authserver");
91 type = AUTH;
92 } else {
93 aaaserver = rc_conf_srv(rh, "acctserver");
94 type = ACCT;
95 }
96 if (aaaserver == NULL)
97 return ERROR_RC;
98
99 return rc_aaa_ctx_server(rh, ctx, aaaserver, type,
100 nas_port, send, received, msg,
101 add_nas_port, request_type);
102}
103
128int rc_aaa_ctx_server(rc_handle * rh, RC_AAA_CTX ** ctx, SERVER * aaaserver,
129 rc_type type,
130 uint32_t nas_port,
131 VALUE_PAIR * send, VALUE_PAIR ** received,
132 char *msg, int add_nas_port,
133 rc_standard_codes request_type)
134{
135 SEND_DATA data;
136 VALUE_PAIR *adt_vp = NULL;
137 int result;
138 int timeout = rc_conf_int(rh, "radius_timeout");
139 int retries = rc_conf_int(rh, "radius_retries");
140 double start_time = 0;
141 double now = 0;
142 time_t dtime;
143 int servernum;
144
145 data.send_pairs = send;
146 data.receive_pairs = NULL;
147
148 if (add_nas_port != 0
149 && rc_avpair_get(data.send_pairs, PW_NAS_PORT, 0) == NULL) {
150 /*
151 * Fill in NAS-Port
152 */
153 if (rc_avpair_add(rh, &(data.send_pairs), PW_NAS_PORT,
154 &nas_port, 0, 0) == NULL)
155 return ERROR_RC;
156 }
157
158 if (request_type == PW_ACCOUNTING_REQUEST) {
159 /*
160 * Fill in Acct-Delay-Time
161 */
162 dtime = 0;
163 now = rc_getmtime();
165 if (adt_vp == NULL) {
166 adt_vp = rc_avpair_add(rh, &(data.send_pairs),
167 PW_ACCT_DELAY_TIME, &dtime, 0,
168 0);
169 if (adt_vp == NULL)
170 return ERROR_RC;
171 start_time = now;
172 } else {
173 start_time = now - adt_vp->lvalue;
174 }
175 }
176
177 if (data.receive_pairs != NULL) {
179 data.receive_pairs = NULL;
180 }
181
182 servernum = 0;
183 do {
184 rc_buildreq(rh, &data, request_type, aaaserver->name[servernum],
185 aaaserver->port[servernum],
186 aaaserver->secret[servernum], timeout, retries);
187
188 if (request_type == PW_ACCOUNTING_REQUEST) {
189 dtime = rc_getmtime() - start_time;
190 rc_avpair_assign(adt_vp, &dtime, 0);
191 }
192
193 result = rc_send_server_ctx(rh, ctx, &data, msg, type);
194
195 if ((result == OK_RC) || (result == CHALLENGE_RC) || (result == REJECT_RC)) {
196 if (request_type != PW_ACCOUNTING_REQUEST) {
197 *received = data.receive_pairs;
198 } else {
200 }
201
202 DEBUG(LOG_INFO,
203 "rc_send_server_ctx returned success for server %u", servernum);
204 return result;
205 }
206
208 data.receive_pairs = NULL;
209
210 DEBUG(LOG_INFO, "rc_send_server_ctx returned error (%d) for server %u: (remaining: %d)",
211 result, servernum, aaaserver->max-servernum);
212 servernum++;
213 } while (servernum < aaaserver->max && ((result == TIMEOUT_RC) || (result == NETUNREACH_RC)));
214
215 return result;
216}
217
232int rc_aaa(rc_handle * rh, uint32_t nas_port, VALUE_PAIR * send,
233 VALUE_PAIR ** received, char *msg, int add_nas_port,
234 rc_standard_codes request_type)
235{
236 return rc_aaa_ctx(rh, NULL, nas_port, send, received, msg,
237 add_nas_port, request_type);
238}
239
252int rc_auth(rc_handle * rh, uint32_t nas_port, VALUE_PAIR * send,
253 VALUE_PAIR ** received, char *msg)
254{
255
256 return rc_aaa(rh, nas_port, send, received, msg, 1,
257 PW_ACCESS_REQUEST);
258}
259
274int rc_auth_proxy(rc_handle * rh, VALUE_PAIR * send, VALUE_PAIR ** received,
275 char *msg)
276{
277 return rc_aaa(rh, 0, send, received, msg, 0, PW_ACCESS_REQUEST);
278}
279
291int rc_acct(rc_handle * rh, uint32_t nas_port, VALUE_PAIR * send)
292{
293 return rc_aaa(rh, nas_port, send, NULL, NULL, 1,
294 PW_ACCOUNTING_REQUEST);
295}
296
304int rc_acct_proxy(rc_handle * rh, VALUE_PAIR * send)
305{
306
307 return rc_aaa(rh, 0, send, NULL, NULL, 0, PW_ACCOUNTING_REQUEST);
308}
309
320int rc_check(rc_handle * rh, char *host, char *secret, unsigned short port,
321 char *msg)
322{
323 SEND_DATA data;
324 int result;
325 uint32_t service_type;
326 int timeout = rc_conf_int(rh, "radius_timeout");
327 int retries = rc_conf_int(rh, "radius_retries");
328 rc_type type;
329
330 data.send_pairs = data.receive_pairs = NULL;
331
332 if (rh->so_type == RC_SOCKET_TLS || rh->so_type == RC_SOCKET_DTLS)
333 type = AUTH;
334 else
335 type = ACCT;
336
337 /*
338 * Fill in Service-Type
339 */
340
341 service_type = PW_ADMINISTRATIVE;
342 rc_avpair_add(rh, &(data.send_pairs), PW_SERVICE_TYPE, &service_type, 0,
343 0);
344
345 rc_buildreq(rh, &data, PW_STATUS_SERVER, host, port, secret, timeout,
346 retries);
347 result = rc_send_server(rh, &data, msg, type);
348
350
351 return result;
352}
353
int rc_acct(rc_handle *rh, uint32_t nas_port, VALUE_PAIR *send)
Definition buildreq.c:291
rc_type
Definition radcli.h:70
int rc_auth_proxy(rc_handle *rh, VALUE_PAIR *send, VALUE_PAIR **received, char *msg)
Definition buildreq.c:274
int rc_auth(rc_handle *rh, uint32_t nas_port, VALUE_PAIR *send, VALUE_PAIR **received, char *msg)
Definition buildreq.c:252
struct rc_aaa_ctx_st RC_AAA_CTX
Definition radcli.h:524
void rc_buildreq(rc_handle const *rh, SEND_DATA *data, int code, char *server, unsigned short port, char *secret, int timeout, int retries)
Definition buildreq.c:45
int rc_acct_proxy(rc_handle *rh, VALUE_PAIR *send)
Definition buildreq.c:304
void rc_avpair_free(VALUE_PAIR *pair)
Definition avpair.c:593
int rc_aaa_ctx(rc_handle *rh, RC_AAA_CTX **ctx, uint32_t nas_port, VALUE_PAIR *send, VALUE_PAIR **received, char *msg, int add_nas_port, rc_standard_codes request_type)
Definition buildreq.c:81
int rc_aaa(rc_handle *rh, uint32_t nas_port, VALUE_PAIR *send, VALUE_PAIR **received, char *msg, int add_nas_port, rc_standard_codes request_type)
Definition buildreq.c:232
int rc_avpair_assign(VALUE_PAIR *vp, void const *pval, int len)
Definition avpair.c:135
int rc_send_server(rc_handle *rh, SEND_DATA *data, char *msg, rc_type type)
Definition sendserver.c:221
int rc_aaa_ctx_server(rc_handle *rh, RC_AAA_CTX **ctx, SERVER *aaaserver, rc_type type, uint32_t nas_port, VALUE_PAIR *send, VALUE_PAIR **received, char *msg, int add_nas_port, rc_standard_codes request_type)
Definition buildreq.c:128
int rc_check(rc_handle *rh, char *host, char *secret, unsigned short port, char *msg)
Definition buildreq.c:320
rc_standard_codes
Definition radcli.h:128
SERVER * rc_conf_srv(rc_handle const *rh, char const *optname)
Definition config.c:815
VALUE_PAIR * rc_avpair_add(rc_handle const *rh, VALUE_PAIR **list, uint32_t attrid, void const *pval, int len, uint32_t vendorspec)
Definition avpair.c:46
VALUE_PAIR * rc_avpair_get(VALUE_PAIR *vp, uint32_t attrid, uint32_t vendorspec)
Definition avpair.c:489
@ ACCT
Request for accounting server.
Definition radcli.h:72
@ AUTH
Request for authentication server.
Definition radcli.h:71
@ PW_NAS_PORT
Its type is integer.
Definition radcli.h:151
@ PW_SERVICE_TYPE
Its type is integer.
Definition radcli.h:152
@ PW_ACCT_DELAY_TIME
Its type is integer.
Definition radcli.h:187
@ RC_SOCKET_DTLS
DTLS socket.
Definition radcli.h:104
@ RC_SOCKET_TLS
TLS socket.
Definition radcli.h:103
uint32_t lvalue
attribute value if type is PW_TYPE_INTEGER, PW_TYPE_DATE or PW_TYPE_IPADDR.
Definition radcli.h:488
int timeout
Session timeout in seconds.
Definition radcli.h:506
char * secret
Shared secret of RADIUS server.
Definition radcli.h:505
uint8_t seq_nbr
Packet sequence number.
Definition radcli.h:502
int svc_port
RADIUS protocol destination port.
Definition radcli.h:504
char * server
Name/address of RADIUS server.
Definition radcli.h:503
VALUE_PAIR * send_pairs
More a/v pairs to send.
Definition radcli.h:508
VALUE_PAIR * receive_pairs
Where to place received a/v pairs.
Definition radcli.h:509
uint8_t code
RADIUS packet code.
Definition radcli.h:501