Radcli library 2.0.2
A simple radius library -- legacy API reference
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#include "rc-random.h"
14#include "options.h"
15
16/*- Returns the current monotonic time in seconds, for Acct-Delay-Time.
17 *
18 * @return seconds since an arbitrary fixed point, or a negative value on
19 * failure.
20 -*/
21static double rc_getmtime(void)
22{
23 return rc_getmtime_ms() / 1000.0;
24}
25
32
48void rc_buildreq(rc_handle const *rh, SEND_DATA * data, int code, char *server,
49 unsigned short port, char *secret, int timeout, int retries)
50{
51 data->server = server;
52 data->secret = secret;
53 data->svc_port = port;
54 data->seq_nbr = rc_get_random_byte();
55 data->timeout = timeout;
56 data->retries = retries;
57 data->code = code;
58}
59
60/*- Select the server list and rc_type for a request, based on transport
61 * and request type. Internal helper shared by rc_aaa_ctx() and
62 * rc_acct_async().
63 *
64 * @param rh a handle to parsed configuration.
65 * @param aaaserver receives the selected SERVER list from configuration.
66 * @param type receives AUTH or ACCT, matching the selected server list.
67 * @param request_type one of the standard RADIUS codes (e.g., PW_ACCESS_REQUEST).
68 * @return OK_RC (0) on success, ERROR_RC if no matching servers are configured.
69 -*/
70static int rc_select_aaa_server(rc_handle *rh, SERVER **aaaserver,
71 rc_type *type, rc_standard_codes request_type)
72{
73 if (RC_USES_SESSION(rh) || request_type != PW_ACCOUNTING_REQUEST) {
74 *aaaserver = rc_conf_srv(rh, "authserver");
75 *type = AUTH;
76 } else {
77 *aaaserver = rc_conf_srv(rh, "acctserver");
78 *type = ACCT;
79 }
80
81 if (*aaaserver == NULL)
82 return ERROR_RC;
83
84 return OK_RC;
85}
86
87/*- Fill in NAS-Port and Acct-Delay-Time on a request being built.
88 * Internal helper shared by rc_aaa_ctx_server() and
89 * rc_aaa_ctx_server_async().
90 *
91 * @param rh a handle to parsed configuration.
92 * @param data the request being built; send_pairs is extended in place.
93 * @param nas_port the physical NAS port number to include (may be zero).
94 * @param add_nas_port if non-zero, PW_NAS_PORT is added to the sent pairs.
95 * @param request_type one of the standard RADIUS codes (e.g., PW_ACCESS_REQUEST).
96 * @param adt_vp receives the Acct-Delay-Time pair when request_type is
97 * PW_ACCOUNTING_REQUEST, so the caller can update it on each retransmission.
98 * @param start_time receives the time the delay is measured from.
99 * @return OK_RC (0) on success, ERROR_RC on failure.
100 -*/
101static int rc_fill_acct_pairs(rc_handle const *rh, SEND_DATA *data,
102 uint32_t nas_port, int add_nas_port,
103 rc_standard_codes request_type,
104 VALUE_PAIR **adt_vp, double *start_time)
105{
106 time_t dtime;
107 double now;
108
109 if (add_nas_port != 0
110 && rc_avpair_get(data->send_pairs, PW_NAS_PORT, 0) == NULL) {
111 /*
112 * Fill in NAS-Port
113 */
114 if (rc_avpair_add(rh, &(data->send_pairs), PW_NAS_PORT,
115 &nas_port, 0, 0) == NULL)
116 return ERROR_RC;
117 }
118
119 if (request_type == PW_ACCOUNTING_REQUEST) {
120 /*
121 * Fill in Acct-Delay-Time
122 */
123 dtime = 0;
124 now = rc_getmtime();
125 *adt_vp = rc_avpair_get(data->send_pairs, PW_ACCT_DELAY_TIME, 0);
126 if (*adt_vp == NULL) {
127 *adt_vp = rc_avpair_add(rh, &(data->send_pairs),
128 PW_ACCT_DELAY_TIME, &dtime, 0,
129 0);
130 if (*adt_vp == NULL)
131 return ERROR_RC;
132 *start_time = now;
133 } else {
134 *start_time = now - (*adt_vp)->lvalue;
135 }
136 }
137
138 return OK_RC;
139}
140
165int rc_aaa_ctx(rc_handle * rh, RC_AAA_CTX ** ctx, uint32_t nas_port,
166 VALUE_PAIR * send, VALUE_PAIR ** received, char *msg,
167 int add_nas_port, rc_standard_codes request_type)
168{
169 SERVER *aaaserver;
170 rc_type type;
171
172 if (rc_select_aaa_server(rh, &aaaserver, &type, request_type) != OK_RC)
173 return ERROR_RC;
174
175 return rc_aaa_ctx_server(rh, ctx, aaaserver, type,
176 nas_port, send, received, msg,
177 add_nas_port, request_type);
178}
179
204int rc_aaa_ctx_server(rc_handle * rh, RC_AAA_CTX ** ctx, SERVER * aaaserver,
205 rc_type type,
206 uint32_t nas_port,
207 VALUE_PAIR * send, VALUE_PAIR ** received,
208 char *msg, int add_nas_port,
209 rc_standard_codes request_type)
210{
211 SEND_DATA data;
212 VALUE_PAIR *adt_vp = NULL;
213 int result;
214 int timeout = rc_conf_int_id(rh, OPT_RADIUS_TIMEOUT);
215 int retries = rc_conf_int_id(rh, OPT_RADIUS_RETRIES);
216 double start_time = 0;
217 time_t dtime;
218 int servernum;
219
220 data.send_pairs = send;
221 data.receive_pairs = NULL;
222
223 if (rc_fill_acct_pairs(rh, &data, nas_port, add_nas_port, request_type,
224 &adt_vp, &start_time) != OK_RC)
225 return ERROR_RC;
226
227 if (data.receive_pairs != NULL) {
229 data.receive_pairs = NULL;
230 }
231
232 servernum = 0;
233 do {
234 rc_buildreq(rh, &data, request_type, aaaserver->name[servernum],
235 aaaserver->port[servernum],
236 aaaserver->secret[servernum], timeout, retries);
237
238 if (request_type == PW_ACCOUNTING_REQUEST) {
239 dtime = rc_getmtime() - start_time;
240 rc_avpair_assign(adt_vp, &dtime, 0);
241 }
242
243 result = rc_send_server_ctx(rh, ctx, &data, msg, type, 0);
244
245 if ((result == OK_RC) || (result == CHALLENGE_RC) || (result == REJECT_RC)) {
246 if (request_type != PW_ACCOUNTING_REQUEST) {
247 *received = data.receive_pairs;
248 } else {
250 }
251
252 DEBUG(rh, LOG_INFO,
253 "rc_send_server_ctx returned success for server %u", servernum);
254 return result;
255 }
256
258 data.receive_pairs = NULL;
259
260 DEBUG(rh, LOG_INFO, "rc_send_server_ctx returned error (%d) for server %u: (remaining: %d)",
261 result, servernum, aaaserver->max-servernum);
262 servernum++;
263 } while (servernum < aaaserver->max && ((result == TIMEOUT_RC) || (result == NETUNREACH_RC)));
264
265 return result;
266}
267
282int rc_aaa(rc_handle * rh, uint32_t nas_port, VALUE_PAIR * send,
283 VALUE_PAIR ** received, char *msg, int add_nas_port,
284 rc_standard_codes request_type)
285{
286 return rc_aaa_ctx(rh, NULL, nas_port, send, received, msg,
287 add_nas_port, request_type);
288}
289
302int rc_auth(rc_handle * rh, uint32_t nas_port, VALUE_PAIR * send,
303 VALUE_PAIR ** received, char *msg)
304{
305
306 return rc_aaa(rh, nas_port, send, received, msg, 1,
307 PW_ACCESS_REQUEST);
308}
309
324int rc_auth_proxy(rc_handle * rh, VALUE_PAIR * send, VALUE_PAIR ** received,
325 char *msg)
326{
327 return rc_aaa(rh, 0, send, received, msg, 0, PW_ACCESS_REQUEST);
328}
329
341int rc_acct(rc_handle * rh, uint32_t nas_port, VALUE_PAIR * send)
342{
343 return rc_aaa(rh, nas_port, send, NULL, NULL, 1,
344 PW_ACCOUNTING_REQUEST);
345}
346
354int rc_acct_proxy(rc_handle * rh, VALUE_PAIR * send)
355{
356
357 return rc_aaa(rh, 0, send, NULL, NULL, 0, PW_ACCOUNTING_REQUEST);
358}
359
360/*- Send an accounting request to every server in aaaserver without
361 * waiting for a reply. Internal helper behind rc_acct_async().
362 *
363 * @param rh a handle to parsed configuration.
364 * @param aaaserver a non-NULL SERVER describing the target server(s).
365 * @param type AUTH or ACCT, selects the destination port.
366 * @param nas_port the physical NAS port number to include (may be zero).
367 * @param send VALUE_PAIR list of attributes to send.
368 * @return OK_RC (0) if the packet was handed to the socket layer for at
369 * least one server, ERROR_RC on failure.
370 -*/
371static int rc_aaa_ctx_server_async(rc_handle * rh, SERVER * aaaserver,
372 rc_type type, uint32_t nas_port,
373 VALUE_PAIR * send)
374{
375 SEND_DATA data;
376 VALUE_PAIR *adt_vp = NULL;
377 double start_time = 0;
378 time_t dtime;
379 int timeout = rc_conf_int_id(rh, OPT_RADIUS_TIMEOUT);
380 int retries = rc_conf_int_id(rh, OPT_RADIUS_RETRIES);
381 int servernum;
382 int result;
383 int sent = 0;
384
385 data.send_pairs = send;
386 data.receive_pairs = NULL;
387
388 if (rc_fill_acct_pairs(rh, &data, nas_port, 1, PW_ACCOUNTING_REQUEST,
389 &adt_vp, &start_time) != OK_RC)
390 return ERROR_RC;
391
392 for (servernum = 0; servernum < aaaserver->max; servernum++) {
393 dtime = rc_getmtime() - start_time;
394 rc_avpair_assign(adt_vp, &dtime, 0);
395
396 /* timeout/retries are not consulted by rc_send_server_ctx()
397 * when no_wait is set below; passed through unchanged only
398 * so SEND_DATA/DEBUG output reflect the real configuration. */
399 rc_buildreq(rh, &data, PW_ACCOUNTING_REQUEST,
400 aaaserver->name[servernum],
401 aaaserver->port[servernum],
402 aaaserver->secret[servernum], timeout, retries);
403
404 result = rc_send_server_ctx(rh, NULL, &data, NULL, type, 1);
405
406 if (data.receive_pairs != NULL) {
408 data.receive_pairs = NULL;
409 }
410
411 if (result == OK_RC) {
412 sent++;
413 } else {
414 DEBUG(rh, LOG_INFO,
415 "rc_send_server_ctx returned error (%d) for server %u",
416 result, servernum);
417 }
418 }
419
420 return sent > 0 ? OK_RC : ERROR_RC;
421}
422
441int rc_acct_async(rc_handle * rh, uint32_t nas_port, VALUE_PAIR * send)
442{
443 SERVER *aaaserver;
444 rc_type type;
445
446 if (rc_select_aaa_server(rh, &aaaserver, &type, PW_ACCOUNTING_REQUEST) != OK_RC)
447 return ERROR_RC;
448
449 return rc_aaa_ctx_server_async(rh, aaaserver, type, nas_port, send);
450}
451
462int rc_check(rc_handle * rh, char *host, char *secret, unsigned short port,
463 char *msg)
464{
465 SEND_DATA data;
466 int result;
467 uint32_t service_type;
468 int timeout = rc_conf_int_id(rh, OPT_RADIUS_TIMEOUT);
469 int retries = rc_conf_int_id(rh, OPT_RADIUS_RETRIES);
470 rc_type type;
471
472 data.send_pairs = data.receive_pairs = NULL;
473
474 if (RC_USES_SESSION(rh))
475 type = AUTH;
476 else
477 type = ACCT;
478
479 /*
480 * Fill in Service-Type
481 */
482
483 service_type = PW_ADMINISTRATIVE;
484 rc_avpair_add(rh, &(data.send_pairs), PW_SERVICE_TYPE, &service_type, 0,
485 0);
486
487 rc_buildreq(rh, &data, PW_STATUS_SERVER, host, port, secret, timeout,
488 retries);
489 result = rc_send_server(rh, &data, msg, type);
490
492
493 return result;
494}
495
int rc_acct(rc_handle *rh, uint32_t nas_port, VALUE_PAIR *send)
Builds an accounting request for port id nas_port with the value_pairs at send.
Definition buildreq.c:341
rc_type
Definition radcli.h:81
int rc_auth_proxy(rc_handle *rh, VALUE_PAIR *send, VALUE_PAIR **received, char *msg)
Builds an authentication request for proxying.
Definition buildreq.c:324
int rc_auth(rc_handle *rh, uint32_t nas_port, VALUE_PAIR *send, VALUE_PAIR **received, char *msg)
Builds an authentication request for port id nas_port with the value_pairs send and submits it to a s...
Definition buildreq.c:302
struct rc_aaa_ctx_st RC_AAA_CTX
Definition radcli.h:295
void rc_buildreq(rc_handle const *rh, SEND_DATA *data, int code, char *server, unsigned short port, char *secret, int timeout, int retries)
Build a skeleton RADIUS request using information from the config file.
Definition buildreq.c:48
int rc_acct_proxy(rc_handle *rh, VALUE_PAIR *send)
Builds an accounting request with the value_pairs at send.
Definition buildreq.c:354
void rc_avpair_free(VALUE_PAIR *pair)
Frees all value_pairs in the list.
Definition avpair.c:569
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)
Builds an authentication/accounting request and submits it to a server, optionally returning context.
Definition buildreq.c:165
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)
Builds an authentication/accounting request for port id nas_port with the value_pairs send and submit...
Definition buildreq.c:282
int rc_avpair_assign(VALUE_PAIR *vp, void const *pval, int len)
Assigns the given value to an attribute-value pair.
Definition avpair.c:136
int rc_send_server(rc_handle *rh, SEND_DATA *data, char *msg, rc_type type)
Sends a request to a RADIUS server and waits for the reply.
Definition send.c:238
int rc_acct_async(rc_handle *rh, uint32_t nas_port, VALUE_PAIR *send)
Sends an accounting request to every configured accounting server without waiting for a reply.
Definition buildreq.c:441
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)
Builds an authentication/accounting request and submits it to a specific server.
Definition buildreq.c:204
int rc_check(rc_handle *rh, char *host, char *secret, unsigned short port, char *msg)
Asks the server hostname on the specified port for a status message.
Definition buildreq.c:462
rc_standard_codes
Definition radcli.h:139
VALUE_PAIR * rc_avpair_add(rc_handle const *rh, VALUE_PAIR **list, uint32_t attrid, void const *pval, int len, uint32_t vendorspec)
Adds an attribute-value pair to the given list.
Definition avpair.c:47
VALUE_PAIR * rc_avpair_get(VALUE_PAIR *vp, uint32_t attrid, uint32_t vendorspec)
Find the first attribute value-pair (which matches the given attribute) from the specified value-pair...
Definition avpair.c:465
@ ACCT
Request for accounting server.
Definition radcli.h:83
@ AUTH
Request for authentication server.
Definition radcli.h:82
int timeout
Session timeout in seconds.
Definition radcli.h:277
char * secret
Shared secret of RADIUS server.
Definition radcli.h:276
uint8_t seq_nbr
Packet sequence number.
Definition radcli.h:273
int svc_port
RADIUS protocol destination port.
Definition radcli.h:275
char * server
Name/address of RADIUS server.
Definition radcli.h:274
VALUE_PAIR * send_pairs
More a/v pairs to send.
Definition radcli.h:279
VALUE_PAIR * receive_pairs
Where to place received a/v pairs.
Definition radcli.h:280
uint8_t code
RADIUS packet code.
Definition radcli.h:272