Radcli library 1.3.1
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
25static unsigned char rc_get_id()
26{
27 return (unsigned char)(random() & UCHAR_MAX);
28}
29
41void rc_buildreq(rc_handle const *rh, SEND_DATA * data, int code, char *server,
42 unsigned short port, char *secret, int timeout, int retries)
43{
44 data->server = server;
45 data->secret = secret;
46 data->svc_port = port;
47 data->seq_nbr = rc_get_id();
48 data->timeout = timeout;
49 data->retries = retries;
50 data->code = code;
51}
52
70int rc_aaa_ctx(rc_handle * rh, RC_AAA_CTX ** ctx, uint32_t nas_port,
71 VALUE_PAIR * send, VALUE_PAIR ** received, char *msg,
72 int add_nas_port, rc_standard_codes request_type)
73{
74 SERVER *aaaserver;
75 rc_type type;
76
77 if (rh->so_type == RC_SOCKET_TLS || rh->so_type == RC_SOCKET_DTLS ||
78 request_type != PW_ACCOUNTING_REQUEST) {
79 aaaserver = rc_conf_srv(rh, "authserver");
80 type = AUTH;
81 } else {
82 aaaserver = rc_conf_srv(rh, "acctserver");
83 type = ACCT;
84 }
85 if (aaaserver == NULL)
86 return ERROR_RC;
87
88 return rc_aaa_ctx_server(rh, ctx, aaaserver, type,
89 nas_port, send, received, msg,
90 add_nas_port, request_type);
91}
92
111int rc_aaa_ctx_server(rc_handle * rh, RC_AAA_CTX ** ctx, SERVER * aaaserver,
112 rc_type type,
113 uint32_t nas_port,
114 VALUE_PAIR * send, VALUE_PAIR ** received,
115 char *msg, int add_nas_port,
116 rc_standard_codes request_type)
117{
118 SEND_DATA data;
119 VALUE_PAIR *adt_vp = NULL;
120 int result;
121 int timeout = rc_conf_int(rh, "radius_timeout");
122 int retries = rc_conf_int(rh, "radius_retries");
123 double start_time = 0;
124 double now = 0;
125 time_t dtime;
126 int servernum;
127
128 data.send_pairs = send;
129 data.receive_pairs = NULL;
130
131 if (add_nas_port != 0
132 && rc_avpair_get(data.send_pairs, PW_NAS_PORT, 0) == NULL) {
133 /*
134 * Fill in NAS-Port
135 */
136 if (rc_avpair_add(rh, &(data.send_pairs), PW_NAS_PORT,
137 &nas_port, 0, 0) == NULL)
138 return ERROR_RC;
139 }
140
141 if (request_type == PW_ACCOUNTING_REQUEST) {
142 /*
143 * Fill in Acct-Delay-Time
144 */
145 dtime = 0;
146 now = rc_getmtime();
148 if (adt_vp == NULL) {
149 adt_vp = rc_avpair_add(rh, &(data.send_pairs),
150 PW_ACCT_DELAY_TIME, &dtime, 0,
151 0);
152 if (adt_vp == NULL)
153 return ERROR_RC;
154 start_time = now;
155 } else {
156 start_time = now - adt_vp->lvalue;
157 }
158 }
159
160 if (data.receive_pairs != NULL) {
162 data.receive_pairs = NULL;
163 }
164
165 servernum = 0;
166 do {
167 rc_buildreq(rh, &data, request_type, aaaserver->name[servernum],
168 aaaserver->port[servernum],
169 aaaserver->secret[servernum], timeout, retries);
170
171 if (request_type == PW_ACCOUNTING_REQUEST) {
172 dtime = rc_getmtime() - start_time;
173 rc_avpair_assign(adt_vp, &dtime, 0);
174 }
175
176 result = rc_send_server_ctx(rh, ctx, &data, msg, type);
177
178 if ((result == OK_RC) || (result == CHALLENGE_RC) || (result == REJECT_RC)) {
179 if (request_type != PW_ACCOUNTING_REQUEST) {
180 *received = data.receive_pairs;
181 } else {
183 }
184
185 DEBUG(LOG_INFO,
186 "rc_send_server_ctx returned success for server %u", servernum);
187 return result;
188 }
189
191 data.receive_pairs = NULL;
192
193 DEBUG(LOG_INFO, "rc_send_server_ctx returned error (%d) for server %u: (remaining: %d)",
194 result, servernum, aaaserver->max-servernum);
195 servernum++;
196 } while (servernum < aaaserver->max && ((result == TIMEOUT_RC) || (result == NETUNREACH_RC)));
197
198 return result;
199}
200
215int rc_aaa(rc_handle * rh, uint32_t nas_port, VALUE_PAIR * send,
216 VALUE_PAIR ** received, char *msg, int add_nas_port,
217 rc_standard_codes request_type)
218{
219 return rc_aaa_ctx(rh, NULL, nas_port, send, received, msg,
220 add_nas_port, request_type);
221}
222
235int rc_auth(rc_handle * rh, uint32_t nas_port, VALUE_PAIR * send,
236 VALUE_PAIR ** received, char *msg)
237{
238
239 return rc_aaa(rh, nas_port, send, received, msg, 1,
240 PW_ACCESS_REQUEST);
241}
242
257int rc_auth_proxy(rc_handle * rh, VALUE_PAIR * send, VALUE_PAIR ** received,
258 char *msg)
259{
260 return rc_aaa(rh, 0, send, received, msg, 0, PW_ACCESS_REQUEST);
261}
262
274int rc_acct(rc_handle * rh, uint32_t nas_port, VALUE_PAIR * send)
275{
276 return rc_aaa(rh, nas_port, send, NULL, NULL, 1,
277 PW_ACCOUNTING_REQUEST);
278}
279
287int rc_acct_proxy(rc_handle * rh, VALUE_PAIR * send)
288{
289
290 return rc_aaa(rh, 0, send, NULL, NULL, 0, PW_ACCOUNTING_REQUEST);
291}
292
303int rc_check(rc_handle * rh, char *host, char *secret, unsigned short port,
304 char *msg)
305{
306 SEND_DATA data;
307 int result;
308 uint32_t service_type;
309 int timeout = rc_conf_int(rh, "radius_timeout");
310 int retries = rc_conf_int(rh, "radius_retries");
311 rc_type type;
312
313 data.send_pairs = data.receive_pairs = NULL;
314
315 if (rh->so_type == RC_SOCKET_TLS || rh->so_type == RC_SOCKET_DTLS)
316 type = AUTH;
317 else
318 type = ACCT;
319
320 /*
321 * Fill in Service-Type
322 */
323
324 service_type = PW_ADMINISTRATIVE;
325 rc_avpair_add(rh, &(data.send_pairs), PW_SERVICE_TYPE, &service_type, 0,
326 0);
327
328 rc_buildreq(rh, &data, PW_STATUS_SERVER, host, port, secret, timeout,
329 retries);
330 result = rc_send_server(rh, &data, msg, type);
331
333
334 return result;
335}
336
int rc_acct(rc_handle *rh, uint32_t nas_port, VALUE_PAIR *send)
Definition: buildreq.c:274
rc_type
Definition: radcli.h:66
int rc_auth_proxy(rc_handle *rh, VALUE_PAIR *send, VALUE_PAIR **received, char *msg)
Definition: buildreq.c:257
int rc_auth(rc_handle *rh, uint32_t nas_port, VALUE_PAIR *send, VALUE_PAIR **received, char *msg)
Definition: buildreq.c:235
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:41
int rc_acct_proxy(rc_handle *rh, VALUE_PAIR *send)
Definition: buildreq.c:287
void rc_avpair_free(VALUE_PAIR *pair)
Definition: avpair.c:537
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:70
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:215
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:232
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:111
int rc_check(rc_handle *rh, char *host, char *secret, unsigned short port, char *msg)
Definition: buildreq.c:303
rc_standard_codes
Definition: radcli.h:124
SERVER * rc_conf_srv(rc_handle const *rh, char const *optname)
Definition: config.c:758
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:433
@ ACCT
Request for accounting server.
Definition: radcli.h:68
@ AUTH
Request for authentication server.
Definition: radcli.h:67
@ PW_NAS_PORT
Its type is integer.
Definition: radcli.h:147
@ PW_SERVICE_TYPE
Its type is integer.
Definition: radcli.h:148
@ PW_ACCT_DELAY_TIME
Its type is integer.
Definition: radcli.h:183
@ RC_SOCKET_DTLS
DTLS socket.
Definition: radcli.h:100
@ RC_SOCKET_TLS
TLS socket.
Definition: radcli.h:99
uint32_t lvalue
attribute value if type is PW_TYPE_INTEGER, PW_TYPE_DATE or PW_TYPE_IPADDR.
Definition: radcli.h:480
int timeout
Session timeout in seconds.
Definition: radcli.h:493
char * secret
Shared secret of RADIUS server.
Definition: radcli.h:492
uint8_t seq_nbr
Packet sequence number.
Definition: radcli.h:489
int svc_port
RADIUS protocol destination port.
Definition: radcli.h:491
char * server
Name/addrress of RADIUS server.
Definition: radcli.h:490
VALUE_PAIR * send_pairs
More a/v pairs to send.
Definition: radcli.h:495
VALUE_PAIR * receive_pairs
Where to place received a/v pairs.
Definition: radcli.h:496
uint8_t code
RADIUS packet code.
Definition: radcli.h:488
Definition: radcli.h:87