Radcli library 2.0.2
A simple radius library -- new API reference
Loading...
Searching...
No Matches
request.c
1/*
2 * Copyright (C) 2026 Nikos Mavrogiannopoulos
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
28
29/* radcli2.h's request/reply entry point (radcli_request_new()/_perform()/
30 * etc.): builds a wire packet with radcli_avp_encode(), sends it with the
31 * same radcli_transport_exchange() rc_send_server_ctx() uses (so failover,
32 * retries, Response Authenticator, and Message-Authenticator/Blast-RADIUS
33 * verification are exactly the code already proven against rc_send_server_ctx(),
34 * not a second copy), and decodes the reply with radcli_avp_decode().
35 *
36 * Uses only the first configured server for the request's type, by design:
37 * the new API carries one
38 * server per context, redundancy delegated to DNS/address-level fail-over
39 * (already inside radcli_transport_exchange()) rather than a configured
40 * list of distinct servers each with its own secret -- that remains
41 * rc_auth()/rc_acct()'s job alone. radcli_request_new() logs a warning,
42 * not an error, if more than one entry is configured. */
43
44#include <config.h>
45#include <includes.h>
46#include <radcli/radcli.h>
47#include <radcli/radcli2.h>
48#include "avp.h"
49#include "util.h"
50#include "options.h"
51#include "rc-crypto.h"
52#include "rc-random.h"
53
54struct radcli_request_st {
55 rc_handle *rh;
56 uint8_t code;
57 radcli_avp_list *send;
58 char server[AUTH_ID_LEN + 1];
59 uint16_t svc_port;
60 char secret[MAX_SECRET_LENGTH + 1];
61 int timeout_ms;
62 int retries;
63 rc_type type;
64
65 int performed;
66 uint8_t reply_code;
67 radcli_avp_list *reply_attrs;
68
69 /* Set by radcli_request_perform(r, RADCLI_REQUEST_SENDONLY); driven to
70 * completion by radcli_ctx_get_poll()/radcli_ctx_dispatch() (lib/dae.c),
71 * read back with radcli_request_done(). See lib/includes.h's struct
72 * radcli_async_send_st. */
73 struct radcli_async_send_st async;
74};
75
97radcli_request *radcli_request_new(radcli_ctx *ctx, radcli_code code, const radcli_avp_list *send)
98{
99 rc_handle *rh = (rc_handle *)ctx;
100 struct radcli_request_st *r;
101 SERVER *servers;
102 const char *optname;
103 rc_option_id optid;
104 rc_type type;
105 radcli_avp_list *send_copy;
107 const radcli_avp *a;
108
109 if (rh == NULL)
110 return NULL;
111
112 if (code != RADCLI_CODE_ACCESS_REQUEST && code != RADCLI_CODE_ACCOUNTING_REQUEST) {
113 rc_log(LOG_ERR, "radcli_request_new: code must be RADCLI_CODE_ACCESS_REQUEST "
114 "or RADCLI_CODE_ACCOUNTING_REQUEST");
115 return NULL;
116 }
117
118 /* Matches rc_select_aaa_server()'s (lib/buildreq.c) rule: an
119 * Accounting-Request goes to authserver, not acctserver, over
120 * TCP/TLS/DTLS, which carry both request types over one connection. */
121 if (RC_USES_SESSION(rh) || code == RADCLI_CODE_ACCESS_REQUEST) {
122 optname = "authserver";
123 optid = OPT_AUTHSERVER;
124 type = AUTH;
125 } else {
126 optname = "acctserver";
127 optid = OPT_ACCTSERVER;
128 type = ACCT;
129 }
130
131 servers = rc_conf_srv_id(rh, optid);
132 if (servers == NULL || servers->max == 0) {
133 rc_log(LOG_ERR, "radcli_request_new: no %s configured", optname);
134 return NULL;
135 }
136 /* By design, the new API carries one server per handle, redundancy
137 * delegated to DNS/address-level
138 * fail-over (already inside radcli_transport_exchange()), not a
139 * second, per-server-list failover loop -- that job is rc_auth()/
140 * rc_acct()'s alone. Only the first configured entry is ever used;
141 * this only warns, rather than rejecting outright, so a caller
142 * migrating a legacy multi-server config one entry point at a time
143 * isn't broken by the leftover entries. */
144 if (servers->max > 1)
145 rc_log(LOG_WARNING, "radcli_request_new: %d %s entries configured; "
146 "the new API uses only the first (%s) -- redundancy is "
147 "address-level (DNS), not server-list-level",
148 servers->max, optname, servers->name[0]);
149
150 /* Copied in -- radcli2.h documents that "send" may be freed or reused
151 * by the caller immediately after this call returns; only aliasing
152 * the caller's pointer here would make that a use-after-free the
153 * first time radcli_request_perform() later reads it. */
154 send_copy = radcli_avp_list_new();
155 if (send_copy == NULL)
156 return NULL;
157 it = radcli_avp_list_iter(send);
158 while ((a = radcli_avp_iter_next(&it)) != NULL) {
159 const void *data;
160 size_t len;
161
162 if (radcli_avp_get_bytes(a, &data, &len) != 0 ||
163 radcli_avp_add_bytes(send_copy, radcli_avp_def(a), data, len) != 0) {
164 radcli_avp_list_free(send_copy);
165 return NULL;
166 }
167 }
168
169 r = calloc(1, sizeof(*r));
170 if (r == NULL) {
171 radcli_avp_list_free(send_copy);
172 return NULL;
173 }
174
175 r->type = type;
176
177 strlcpy(r->server, servers->name[0], sizeof(r->server));
178 r->svc_port = servers->port[0];
179 if (servers->secret[0] != NULL)
180 strlcpy(r->secret, servers->secret[0], sizeof(r->secret));
181
182 r->timeout_ms = rc_timeout_sec_to_ms(rc_conf_int_id(rh, OPT_RADIUS_TIMEOUT));
183 r->retries = rc_conf_int_id(rh, OPT_RADIUS_RETRIES);
184
185 r->rh = rh;
186 r->code = (uint8_t)code;
187 r->send = send_copy;
188
189 return r;
190}
191
208int radcli_request_set_timeout(radcli_request *r, int timeout_ms, int retries)
209{
210 if (r == NULL || r->performed || timeout_ms <= 0 || retries < 0)
211 return -1;
212
213 r->timeout_ms = timeout_ms;
214 r->retries = retries;
215 return 0;
216}
217
218/*- Builds a wire packet for (code, send) into send_buffer (capacity
219 * RC_BUFFER_LEN): Request Authenticator (or the Accounting-Request
220 * zero-vector/trailing-MD5 variant), and -- for any code other than
221 * Accounting-Request -- a Message-Authenticator via add_msg_auth_attr().
222 * Factored out of radcli_do_exchange() so radcli_request_perform()'s
223 * RADCLI_REQUEST_SENDONLY path (which hands the packet to
224 * radcli_transport_send_async() instead of radcli_transport_exchange())
225 * builds the exact same Response Authenticator / Message-Authenticator wire
226 * logic, not a second copy -- and shared, too, by lib/dae.c's
227 * radcli2_priv_dae_send_watchdog() (RFC 5997 Status-Server over an
228 * established RadSec session). send may be an empty list (Status-Server
229 * needs no attributes but Message-Authenticator) but not NULL --
230 * radcli_avp_encode() rejects that outright. For a TLS/DTLS rh, secret is
231 * overridden with the RFC 6614/7360 fixed RadSec secret before it is used
232 * for anything (REQ-NET2-SEND-015) -- the caller's resolved secret is
233 * irrelevant for that transport regardless of what it is.
234 *
235 * @param rh a handle to parsed configuration.
236 * @param code the RADIUS packet code to send.
237 * @param send the attributes to encode; must not be NULL.
238 * @param secret the shared secret (overridden for TLS/DTLS, see above).
239 * @param send_buffer destination, capacity RC_BUFFER_LEN.
240 * @param id the packet's Identifier, always supplied by the caller -- this
241 * function does not draw one itself, so every call site states explicitly
242 * where its id comes from (REQ-NET2-SEND-010): the in-flight registry's
243 * reservation for RADCLI_REQUEST_SENDONLY, radcli2_priv_reqreg_pick_id()
244 * for blocking requests and the watchdog -- always before this call, never
245 * patched into the wire packet afterward, since id is itself covered by
246 * the Message-Authenticator HMAC.
247 * @param vector_out set to the request authenticator vector used.
248 * @param out_len set to the total encoded length, including the appended
249 * Message-Authenticator attribute.
250 * @return 0 on success, -1 on encoding failure.
251 -*/
252int radcli_encode_request(rc_handle *rh, uint8_t code, const radcli_avp_list *send,
253 char secret[MAX_SECRET_LENGTH + 1],
254 uint8_t send_buffer[RC_BUFFER_LEN], uint8_t id,
255 unsigned char vector_out[AUTH_VECTOR_LEN], int *out_len)
256{
257 AUTH_HDR *auth = (AUTH_HDR *)send_buffer;
258 int encoded_len, total_length;
259
260 /* RFC 6614 SS2.3/RFC 7360 SS3.2 fix the RadSec shared secret; whatever
261 * the caller resolved from authserver/acctserver's configured secret
262 * (empty, ordinarily -- REQ-CONFIG-CFG-019) is not it. Overriding here,
263 * before secret is used for anything below, is the same override
264 * lib/legacy/send.c's rc_send_server_ctx() and lib/sendserver.c's
265 * radcli_transport_exchange()/radcli_transport_send_async() already
266 * apply -- those three just apply it too late to affect an already-
267 * encoded packet, since encoding (this function) always runs first. */
268 if (rh->so.static_secret)
269 strlcpy(secret, rh->so.static_secret, MAX_SECRET_LENGTH + 1);
270
271 auth->code = code;
272 auth->id = id;
273
274 if (code == RADCLI_CODE_ACCOUNTING_REQUEST) {
275 size_t secretlen;
276 uint16_t tlen;
277
278 memset(vector_out, 0, AUTH_VECTOR_LEN);
279 memcpy(auth->vector, vector_out, AUTH_VECTOR_LEN);
280
281 encoded_len = radcli_avp_encode(rh, send, secret, vector_out,
282 send_buffer + AUTH_HDR_LEN,
283 RC_MAX_PACKET_LEN - AUTH_HDR_LEN, NULL);
284 if (encoded_len < 0)
285 return -1;
286 total_length = AUTH_HDR_LEN + encoded_len;
287
288 tlen = htons((uint16_t)total_length);
289 memcpy(&auth->length, &tlen, sizeof(tlen));
290
291 secretlen = rc_secret_len(secret);
292 memcpy(send_buffer + total_length, secret, secretlen);
293 rc_md5_calc(vector_out, send_buffer, (size_t)total_length + secretlen);
294 memcpy(auth->vector, vector_out, AUTH_VECTOR_LEN);
295 } else {
296 rc_get_random_bytes(vector_out, AUTH_VECTOR_LEN);
297 memcpy(auth->vector, vector_out, AUTH_VECTOR_LEN);
298
299 /* Leave 2+MD5_DIGEST_SIZE bytes for Message-Authenticator (added below). */
300 encoded_len = radcli_avp_encode(rh, send, secret, vector_out,
301 send_buffer + AUTH_HDR_LEN,
302 RC_MAX_PACKET_LEN - AUTH_HDR_LEN - (2 + MD5_DIGEST_SIZE), NULL);
303 if (encoded_len < 0)
304 return -1;
305 total_length = AUTH_HDR_LEN + encoded_len;
306
307 total_length = add_msg_auth_attr(rh, secret, auth, total_length);
308 auth->length = htons((uint16_t)total_length);
309 }
310
311 *out_len = total_length;
312 return 0;
313}
314
315/*- Builds a wire packet for (code, send) and hands it to
316 * radcli_transport_exchange() against (server, svc_port, secret). Shared by
317 * radcli_request_perform() and lib/aaa2.c's radcli_aaa(), so both build the
318 * exact same Response Authenticator / Message-Authenticator wire logic
319 * instead of two independently-written copies of security-sensitive code.
320 *
321 * @param rh a handle to parsed configuration.
322 * @param code the RADIUS packet code to send.
323 * @param send the attributes to encode.
324 * @param server the server to resolve and contact.
325 * @param svc_port overrides the resolved port when non-zero.
326 * @param secret the shared secret.
327 * @param timeout_ms per-attempt reply wait, in milliseconds.
328 * @param retries additional retransmit attempts after the first.
329 * @param no_wait nonzero to send once and return without waiting for a
330 * reply.
331 * @param type AUTH or ACCT.
332 * @param recv_buffer filled with the reply's attributes on a terminal
333 * result.
334 * @param recv_buffer_cap recv_buffer's capacity in bytes.
335 * @param recv_len set to the number of attribute bytes written to
336 * recv_buffer.
337 * @param vector_out set to the Request Authenticator vector used, for the
338 * caller to decode the reply against.
339 * @param out_reply_code if non-NULL, set to the reply's RADIUS code on a
340 * terminal result.
341 * @return whatever radcli_transport_exchange() itself returns.
342 -*/
343int radcli_do_exchange(rc_handle *rh, uint8_t code, const radcli_avp_list *send,
344 char *server, uint16_t svc_port, char secret[MAX_SECRET_LENGTH + 1],
345 int timeout_ms, int retries, int no_wait, rc_type type,
346 uint8_t *recv_buffer, size_t recv_buffer_cap, size_t *recv_len,
347 unsigned char vector_out[AUTH_VECTOR_LEN], uint8_t *out_reply_code)
348{
349 uint8_t send_buffer[RC_BUFFER_LEN];
350 int total_length;
351 uint8_t id;
352 int slot;
353 int result;
354
355 if (radcli2_priv_reqreg_pick_id(rh, &id, &slot) != 0)
356 return ERROR_RC;
357
358 if (radcli_encode_request(rh, code, send, secret, send_buffer, id,
359 vector_out, &total_length) < 0) {
360 radcli2_priv_reqreg_release(rh, slot);
361 return ERROR_RC;
362 }
363
364 result = radcli_transport_exchange(rh, NULL, server, svc_port,
365 secret, 0, timeout_ms, retries, no_wait, type,
366 send_buffer, total_length,
367 recv_buffer, recv_buffer_cap, recv_len, out_reply_code);
368 radcli2_priv_reqreg_release(rh, slot);
369 return result;
370}
371
403int radcli_request_perform(radcli_request *r, unsigned flags)
404{
405 uint8_t recv_buffer[RC_BUFFER_LEN];
406 unsigned char vector[AUTH_VECTOR_LEN];
407 size_t recv_len = 0;
408 uint8_t reply_code = 0;
409 int result;
410
411 if (r == NULL || r->performed)
412 return RADCLI_ERROR;
413 if (flags != RADCLI_REQUEST_NONE && flags != RADCLI_REQUEST_SENDONLY &&
415 rc_log(LOG_ERR, "invalid flags 0x%x", flags); /* REQ-NET2-SEND-019 */
416 return RADCLI_ERROR;
417 }
418 r->performed = 1;
419
420 if (flags & RADCLI_REQUEST_SENDONLY) {
421 uint8_t send_buffer[RC_BUFFER_LEN];
422 int send_len;
423 uint8_t id;
424 int slot;
425
426 /* Reserve the Identifier before encoding: it is itself covered
427 * by the Message-Authenticator HMAC below, so it cannot be
428 * patched in afterward (REQ-NET2-SEND-016). */
429 slot = radcli2_priv_reqreg_reserve(r->rh, &r->async, &id);
430 if (slot < 0)
431 return RADCLI_ERROR;
432
433 if (radcli_encode_request(r->rh, r->code, r->send, r->secret,
434 send_buffer, id, vector, &send_len) < 0) {
435 radcli2_priv_reqreg_release(r->rh, slot);
436 return RADCLI_ERROR;
437 }
438
439 result = radcli_transport_send_async(r->rh, slot, r->server, r->svc_port, r->secret,
440 r->type, send_buffer, send_len, r->timeout_ms, r->retries,
443 if (result != OK_RC) {
444 radcli2_priv_reqreg_release(r->rh, slot);
445 return RADCLI_ERROR;
446 }
447 return RADCLI_OK;
448 }
449
450 result = radcli_do_exchange(r->rh, r->code, r->send, r->server, r->svc_port, r->secret,
451 r->timeout_ms, r->retries, 0, r->type,
452 recv_buffer, sizeof(recv_buffer), &recv_len, vector, &reply_code);
453
454 /* reply_code is set even for a reply that is then rejected; only a
455 * RADCLI_OK outcome reports it (REQ-NET2-RECV-015). */
456 switch (result) {
457 case OK_RC:
458 case REJECT_RC:
459 case CHALLENGE_RC:
460 if (recv_len > 0) {
461 if (radcli_avp_decode(r->rh, r->secret, vector, recv_buffer, recv_len, 0,
462 &r->reply_attrs) != 0)
463 return RADCLI_ERROR;
464 }
465 r->reply_code = reply_code;
466 return RADCLI_OK;
467 case TIMEOUT_RC:
468 return RADCLI_TIMEOUT;
469 default:
470 return RADCLI_ERROR;
471 }
472}
473
496int radcli_request_done(radcli_request *r)
497{
498 if (r == NULL || !r->async.active)
499 return RADCLI_ERROR;
500 if (!r->async.delivered)
501 return RADCLI_AGAIN;
502
503 r->reply_attrs = r->async.reply_attrs;
504 r->async.reply_attrs = NULL; /* ownership moved to r */
505 r->async.active = 0;
506
507 switch (r->async.result) {
508 case OK_RC:
509 case REJECT_RC:
510 case CHALLENGE_RC:
511 r->reply_code = r->async.reply_code; /* REQ-NET2-RECV-015 */
512 return RADCLI_OK;
513 case TIMEOUT_RC:
514 return RADCLI_TIMEOUT;
515 default:
516 return RADCLI_ERROR;
517 }
518}
519
525radcli_code radcli_request_code(const radcli_request *r)
526{
527 if (r == NULL || !r->performed)
528 return 0;
529 return (radcli_code)r->reply_code;
530}
531
538const radcli_avp_list *radcli_request_attrs(const radcli_request *r)
539{
540 if (r == NULL)
541 return NULL;
542 return r->reply_attrs;
543}
544
549const char *radcli_request_server(const radcli_request *r)
550{
551 static const char empty[] = "";
552
553 if (r == NULL)
554 return empty;
555 return r->server;
556}
557
570void radcli_request_free(radcli_request *r)
571{
572 if (r == NULL)
573 return;
574 radcli_transport_async_abort(&r->async);
575 radcli_avp_list_free(r->send);
576 radcli_avp_list_free(r->reply_attrs);
577 memset(r->secret, 0, sizeof(r->secret));
578 free(r);
579}
void radcli_avp_list_free(radcli_avp_list *list)
Free a list and every attribute it holds.
Definition avp.c:159
radcli_avp_iter radcli_avp_list_iter(const radcli_avp_list *list)
Begin iterating list.
Definition avp.c:666
int radcli_avp_add_bytes(radcli_avp_list *list, const radcli_attr_def *def, const void *value, size_t len)
Append an attribute holding an arbitrary byte string.
Definition avp.c:224
radcli_avp_list * radcli_avp_list_new(void)
Create an empty attribute-value pair list.
Definition avp.c:145
const radcli_attr_def * radcli_avp_def(const radcli_avp *a)
Return the attribute definition of a.
Definition avp.c:703
const radcli_avp * radcli_avp_iter_next(radcli_avp_iter *it)
Return the current attribute and advance.
Definition avp.c:680
int radcli_avp_get_bytes(const radcli_avp *a, const void **out, size_t *len)
Read an attribute's value as raw bytes.
Definition avp.c:853
int radcli_request_perform(radcli_request *r, unsigned flags)
Send a request, optionally waiting for the reply.
Definition request.c:403
const radcli_avp_list * radcli_request_attrs(const radcli_request *r)
Return the reply's decoded attributes.
Definition request.c:538
const char * radcli_request_server(const radcli_request *r)
Return the name of the server a request was (or will be) sent to.
Definition request.c:549
int radcli_request_done(radcli_request *r)
Report r's outcome once radcli_ctx_dispatch() has resolved it – a pure state query,...
Definition request.c:496
int radcli_request_set_timeout(radcli_request *r, int timeout_ms, int retries)
Override the timeout and retry count of one request.
Definition request.c:208
radcli_code
Definition radcli2.h:509
radcli_request * radcli_request_new(radcli_ctx *ctx, radcli_code code, const radcli_avp_list *send)
Create a request to send.
Definition request.c:97
void radcli_request_free(radcli_request *r)
Release a request.
Definition request.c:570
radcli_code radcli_request_code(const radcli_request *r)
Return the reply's RADIUS code.
Definition request.c:525
@ RADCLI_TIMEOUT
No reply from any address the server name resolved to.
Definition radcli2.h:568
@ RADCLI_AGAIN
Definition radcli2.h:573
@ RADCLI_ERROR
Malformed input, a verification failure, or no server configured.
Definition radcli2.h:567
@ RADCLI_OK
A validated reply was received; see radcli_request_code() for which one.
Definition radcli2.h:566
@ RADCLI_REQUEST_SENDONLY_NONBLOCK
Definition radcli2.h:622
@ RADCLI_REQUEST_SENDONLY
Definition radcli2.h:606