Radcli library 2.0.0
A simple radius library -- legacy API reference
Loading...
Searching...
No Matches
compat.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
42
43#include <config.h>
44#include <includes.h>
45#include <radcli/radcli.h>
46#include "util.h"
47
48/* Legacy-only debug-verbosity knob (freeradius-client/radiusclient-ng
49 * source compatibility). Unlike a handle's own `debug` field
50 * (lib/includes.h), rc_setdebug() has no rc_handle argument, so it can
51 * only pre-seed handles constructed afterward -- see rc_new()/
52 * rc_read_config() below. Deliberately confined to this legacy shim: see
53 * REQ-GEN-SEC-005. */
54static unsigned int radcli_legacy_debug = 0;
55
56int rc_add_config(rc_handle *rh, char const *option_name, char const *option_val,
57 char const *source, int line)
58{
59 return radcli2_priv_add_config(rh, option_name, option_val, source, line);
60}
61
62rc_handle *rc_config_init(rc_handle *rh)
63{
64 return radcli2_priv_config_init(rh);
65}
66
124rc_handle *rc_read_config(char const *filename)
125{
126 rc_handle *rh = radcli2_priv_read_config(filename, 0);
127
128 if (rh != NULL && radcli_legacy_debug)
129 rh->debug = radcli_legacy_debug;
130
131 return rh;
132}
133
134char *rc_conf_str(rc_handle const *rh, char const *optname)
135{
136 return radcli2_priv_conf_str(rh, optname);
137}
138
139int rc_conf_int(rc_handle const *rh, char const *optname)
140{
141 return radcli2_priv_conf_int(rh, optname);
142}
143
144SERVER *rc_conf_srv(rc_handle const *rh, char const *optname)
145{
146 return radcli2_priv_conf_srv(rh, optname);
147}
148
149int rc_test_config(rc_handle *rh, char const *filename)
150{
151 return radcli2_priv_test_config(rh, filename);
152}
153
154int rc_apply_config(rc_handle *rh)
155{
156 return radcli2_priv_apply_config(rh);
157}
158
159int rc_find_server_addr(rc_handle const *rh, char const *server_name,
160 struct addrinfo **info, char *secret, rc_type type)
161{
162 return radcli2_priv_find_server_addr(rh, server_name, info, secret, type);
163}
164
165void rc_config_free(rc_handle *rh)
166{
167 radcli2_priv_config_free(rh);
168}
169
170rc_handle *rc_new(void)
171{
172 rc_handle *rh = radcli2_priv_new();
173
174 if (rh != NULL && radcli_legacy_debug)
175 rh->debug = radcli_legacy_debug;
176
177 return rh;
178}
179
180void rc_destroy(rc_handle *rh)
181{
182 radcli2_priv_destroy(rh);
183}
184
185rc_socket_type rc_get_socket_type(rc_handle *rh)
186{
187 return rh->so_type;
188}
189
190int rc_read_dictionary(rc_handle *rh, char const *filename)
191{
192 return radcli2_priv_read_dictionary(rh, filename);
193}
194
195int rc_read_dictionary_from_buffer(rc_handle *rh, char const *buf, size_t size)
196{
197 return radcli2_priv_read_dictionary_from_buffer(rh, buf, size);
198}
199
200void rc_dict_free(rc_handle *rh)
201{
202 radcli2_priv_dict_free(rh);
203}
204
205int rc_tls_fd(rc_handle *rh)
206{
207 return radcli2_priv_tls_fd(rh);
208}
209
210int rc_check_tls(rc_handle *rh)
211{
212 return radcli2_priv_check_tls(rh);
213}
214
215/* rc_getport()/rc_own_hostname()/rc_mksid()/rc_setdebug()/rc_openlog(),
216 * unlike the wrappers above, are the real implementations, moved here
217 * verbatim rather than renamed-and-wrapped: nothing else in libradcli2
218 * ever called them (confirmed by grepping every libradcli2-bound source
219 * file for a real call site, not just each function's own definition),
220 * so there was no reason for them to live there at all -- unlike
221 * lib/tls.c's rc_tls_fd()/rc_check_tls() just above, which stay
222 * genuinely private-wrapped because rc_check_tls() calls tls.c's static
223 * restart_session() and touches its session-internal tls_st fields:
224 * pulling it out would mean exporting more private surface (that static
225 * helper plus tls_st's layout), not less. */
226
227unsigned short rc_getport(int type)
228{
229 struct servent *svp;
230
231 if ((svp = getservbyname((type == AUTH) ? "radius" : "radacct", "udp")) == NULL) {
232 return (type == AUTH) ? PW_AUTH_UDP_PORT : PW_ACCT_UDP_PORT;
233 } else {
234 return ntohs((unsigned short)svp->s_port);
235 }
236}
237
238int rc_own_hostname(char *hostname, int len)
239{
240#ifdef HAVE_UNAME
241 struct utsname uts;
242#endif
243
244#if defined(HAVE_UNAME)
245 if (uname(&uts) < 0) {
246 rc_log(LOG_ERR, "rc_own_hostname: couldn't get own hostname");
247 return -1;
248 }
249 strlcpy(hostname, uts.nodename, len);
250#elif defined(HAVE_GETHOSTNAME)
251 if (gethostname(hostname, len) < 0) {
252 rc_log(LOG_ERR, "rc_own_hostname: couldn't get own hostname");
253 return -1;
254 }
255#elif defined(HAVE_SYSINFO)
256 if (sysinfo(SI_HOSTNAME, hostname, len) < 0) {
257 rc_log(LOG_ERR, "rc_own_hostname: couldn't get own hostname");
258 return -1;
259 }
260#else
261 return -1;
262#endif
263
264 return 0;
265}
266
267int rc_get_srcaddr(struct sockaddr *lia, const struct sockaddr *ria)
268{
269 return radcli2_priv_get_srcaddr(lia, ria);
270}
271
272void rc_setdebug(int debug)
273{
274 radcli_legacy_debug = debug;
275}
276
277void rc_openlog(char const *ident)
278{
279#ifndef _MSC_VER /* TODO: Fix me */
280 openlog(ident, LOG_PID, RC_LOG_FACILITY);
281#endif
282}
283
293char *rc_mksid(void)
294{
295 static char buf[15];
296 static unsigned short int cnt = 0;
297
298 snprintf(buf, sizeof(buf), "%08lX%04X%02hX",
299 (unsigned long int)time(NULL),
300 (unsigned int)getpid(),
301 cnt & 0xFF);
302 cnt++;
303 return buf;
304}
rc_type
Definition radcli.h:81
rc_socket_type
Definition radcli.h:112
@ AUTH
Request for authentication server.
Definition radcli.h:82