Radcli library 2.0.0
A simple radius library -- new API reference
Loading...
Searching...
No Matches
radexample.c
1/*
2 * Copyright (C) 1995,1996,1997 Lars Fenneberg
3 * Copyright (C) 2015,2026 Nikos Mavrogiannopoulos
4 *
5 * See the file COPYRIGHT for the respective terms and conditions.
6 *
7 */
8
12
13#include <config.h>
14#include <stdio.h>
15#include <syslog.h>
16#include <arpa/inet.h>
17#include <radcli/radcli2.h>
18
19/* As in a real client (e.g. ocserv's src/auth/radius.c), the attributes a
20 * reply may usefully carry are known ahead of time -- each looked up
21 * directly by its PW_* ID and read back with the typed accessor matching
22 * its radcli_attr_def_type(), rather than dumping the whole reply generically. */
23static void
24print_reply(radcli_ctx *ctx, const radcli_avp_list *recvd)
25{
26 uint32_t timeout, service_type;
27 struct in6_addr framed_ipv6;
28 char buf[INET6_ADDRSTRLEN];
29
30 if (radcli_avp_get_uint32_by_num(recvd, ctx, PW_SESSION_TIMEOUT, 0, &timeout) == 0)
31 fprintf(stderr, "Session-Timeout: %u seconds\n", (unsigned)timeout);
32
33 if (radcli_avp_get_ip6_by_num(recvd, ctx, PW_FRAMED_IPV6_ADDRESS, 0, &framed_ipv6, NULL) == 0 &&
34 inet_ntop(AF_INET6, &framed_ipv6, buf, sizeof(buf)) != NULL)
35 fprintf(stderr, "Framed-IPv6-Address: %s\n", buf);
36
37 if (radcli_avp_get_uint32_by_num(recvd, ctx, PW_SERVICE_TYPE, 0, &service_type) == 0)
38 fprintf(stderr, "Service-Type: %u\n", (unsigned)service_type);
39}
40
41int
42main (int argc, char **argv)
43{
44 int result = 1;
45 radcli_ctx *ctx;
46 radcli_avp_list *send = NULL;
47 radcli_request *r;
48
49 /* openlog() sets the syslog identity used by radcli's internal messages */
50 openlog("my-prog-name", LOG_PID, LOG_DAEMON);
51
52 if ((ctx = radcli_ctx_read_config(RC_CONFIG_FILE, 0)) == NULL)
53 return 1;
54
55 send = radcli_avp_list_new();
56 if (send == NULL) {
57 radcli_ctx_free(ctx);
58 return 1;
59 }
60
61 /*
62 * Fill in User-Name, User-Password, Service-Type. Each add is
63 * independent -- radcli_avp_list_error() below is one aggregate
64 * check for all of them, instead of chaining "!= 0 || ... != 0"
65 * across every call or repeating a check-and-bail block after each.
66 */
67 radcli_avp_add_str_by_num(send, ctx, PW_USER_NAME, 0, "my-username");
68 radcli_avp_add_str_by_num(send, ctx, PW_USER_PASSWORD, 0, "my-password");
69 radcli_avp_add_uint32_by_num(send, ctx, PW_SERVICE_TYPE, 0, PW_AUTHENTICATE_ONLY);
70
71 if (radcli_avp_list_error(send)) {
72 fprintf(stderr, "error constructing the Access-Request\n");
74 radcli_ctx_free(ctx);
75 return 1;
76 }
77
78 r = radcli_request_new(ctx, RADCLI_CODE_ACCESS_REQUEST, send);
80 if (r == NULL) {
81 radcli_ctx_free(ctx);
82 return 1;
83 }
84
85 if (radcli_request_perform(r, 0) == RADCLI_OK &&
86 radcli_request_code(r) == RADCLI_CODE_ACCESS_ACCEPT) {
87 fprintf(stderr, "\"my-username\" RADIUS Authentication OK\n");
88 print_reply(ctx, radcli_request_attrs(r));
89 result = 0;
90 } else {
91 fprintf(stderr, "\"my-username\" RADIUS Authentication failure\n");
92 }
93
95 radcli_ctx_free(ctx);
96
97 return result;
98}
int radcli_avp_list_error(const radcli_avp_list *list)
Check whether any radcli_avp_add_*()/_by_num() call on list has ever failed.
Definition avp.c:1230
void radcli_avp_list_free(radcli_avp_list *list)
Free a list and every attribute it holds.
Definition avp.c:159
int radcli_avp_add_uint32_by_num(radcli_avp_list *list, const radcli_ctx *ctx, uint32_t attrid, uint32_t vendor, uint32_t value)
Look up an integer/IPv4-address/date-typed attribute by legacy numeric ID and append it.
Definition avp.c:503
int radcli_avp_add_str_by_num(radcli_avp_list *list, const radcli_ctx *ctx, uint32_t attrid, uint32_t vendor, const char *value)
Look up a string-typed attribute by legacy numeric ID and append it.
Definition avp.c:485
int radcli_avp_get_uint32_by_num(const radcli_avp_list *list, const radcli_ctx *ctx, uint32_t attrid, uint32_t vendor, uint32_t *out)
Look up an integer/IPv4-address/date-typed attribute by legacy numeric ID and read its first occurren...
Definition avp.c:970
radcli_avp_list * radcli_avp_list_new(void)
Create an empty attribute-value pair list.
Definition avp.c:145
int radcli_avp_get_ip6_by_num(const radcli_avp_list *list, const radcli_ctx *ctx, uint32_t attrid, uint32_t vendor, struct in6_addr *out, unsigned *prefix)
Look up an IPv6-address or IPv6-prefix-typed attribute by legacy numeric ID and read its first occurr...
Definition avp.c:1007
radcli_ctx * radcli_ctx_read_config(const char *filename, unsigned flags)
Create a context by parsing a config file – the main, recommended way to configure radcli.
Definition config2.c:108
void radcli_ctx_free(radcli_ctx *ctx)
Release a context.
Definition config2.c:173
int radcli_request_perform(radcli_request *r, unsigned flags)
Send a request, optionally waiting for the reply.
Definition request.c:364
const radcli_avp_list * radcli_request_attrs(const radcli_request *r)
Return the reply's decoded attributes.
Definition request.c:486
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:517
radcli_code radcli_request_code(const radcli_request *r)
Return the reply's RADIUS code.
Definition request.c:473
@ RADCLI_OK
A validated reply was received; see radcli_request_code() for which one.
Definition radcli2.h:563