Skip to content

Commit ffbdacd

Browse files
committed
Added high level functions to access the value pair struct
1 parent c86e68d commit ffbdacd

7 files changed

Lines changed: 158 additions & 55 deletions

File tree

include/includes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ int sigprocmask (int, sigset_t *, sigset_t *);
159159
# endif
160160
#endif
161161

162+
#include <radcli/radcli.h>
163+
162164
#define AUTH_VECTOR_LEN 16
163165
#define GETSTR_LENGTH 128 //!< must be bigger than AUTH_PASS_LEN.
164166

include/radcli/radcli.h

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656

5757
#define AUTH_PASS_LEN (3 * 16) /* multiple of 16 */
5858
#define AUTH_ID_LEN 64
59-
#define AUTH_STRING_LEN 253 /* maximum of 253 */
6059

6160
#define BUFFER_LEN 8192
6261

@@ -420,16 +419,6 @@ typedef struct dict_vendor
420419
struct dict_vendor *next;
421420
} DICT_VENDOR;
422421

423-
typedef struct value_pair
424-
{
425-
char name[NAME_LENGTH + 1]; //!< attribute name if known.
426-
unsigned attribute; //!< attribute numeric value of type rc_attr_id.
427-
rc_attr_type type; //!< attribute type.
428-
uint32_t lvalue; //!< attribute value if type is PW_TYPE_INTEGER, PW_TYPE_DATE or PW_TYPE_IPADDR.
429-
char strvalue[AUTH_STRING_LEN + 1]; //!< contains attribute value in other cases.
430-
struct value_pair *next;
431-
} VALUE_PAIR;
432-
433422
/* don't change this, as it has to be the same as in the Merit radiusd code */
434423
#define MGMT_POLL_SECRET "Hardlyasecret" //!< Default for Merit radiusd
435424

@@ -444,6 +433,22 @@ typedef enum rc_send_status {
444433
REJECT_RC=2
445434
} rc_send_status;
446435

436+
437+
# define AUTH_STRING_LEN 253 /* maximum of 253 */
438+
439+
/** \struct rc_value_pair Avoid using this structure directly. Use the rc_avpair_get_ functions.
440+
*/
441+
typedef struct rc_value_pair
442+
{
443+
char name[NAME_LENGTH + 1]; //!< attribute name if known.
444+
unsigned attribute; //!< attribute numeric value of type rc_attr_id.
445+
rc_attr_type type; //!< attribute type.
446+
uint32_t lvalue; //!< attribute value if type is PW_TYPE_INTEGER, PW_TYPE_DATE or PW_TYPE_IPADDR.
447+
char strvalue[AUTH_STRING_LEN + 1]; //!< contains attribute value in other cases.
448+
struct rc_value_pair *next;
449+
char pad[32]; //!< unused pad
450+
} VALUE_PAIR;
451+
447452
typedef struct send_data /* Used to pass information to sendserver() function */
448453
{
449454
uint8_t code; //!< RADIUS packet code.
@@ -522,7 +527,6 @@ __BEGIN_DECLS
522527
* This is an example servers configuration file with TLS PSK.
523528
*/
524529

525-
526530
/* avpair.c */
527531

528532
VALUE_PAIR *rc_avpair_add (rc_handle const *rh, VALUE_PAIR **list, int attrid, void const *pval, int len, int vendorpec);
@@ -536,7 +540,12 @@ void rc_avpair_free (VALUE_PAIR *pair);
536540
int rc_avpair_parse (rc_handle const *rh, char const *buffer, VALUE_PAIR **first_pair);
537541
int rc_avpair_tostr (rc_handle const *rh, VALUE_PAIR *pair, char *name, int ln, char *value, int lv);
538542
char *rc_avpair_log(rc_handle const *rh, VALUE_PAIR *pair, char *buf, size_t buf_len);
539-
VALUE_PAIR *rc_avpair_readin(rc_handle const *rh, FILE *input);
543+
VALUE_PAIR *rc_avpair_next(VALUE_PAIR *t);
544+
545+
int rc_avpair_get_uint32 (VALUE_PAIR *vp, uint32_t *res);
546+
int rc_avpair_get_in6 (VALUE_PAIR *vp, struct in6_addr *res, unsigned *prefix);
547+
int rc_avpair_get_raw (VALUE_PAIR *vp, char **res, unsigned *res_size);
548+
void rc_avpair_get_attr (VALUE_PAIR *vp, unsigned *type, unsigned *id);
540549

541550
/* buildreq.c */
542551

lib/avpair.c

Lines changed: 94 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (C) 2015 Nikos Mavrogiannopoulos
23
* Copyright (C) 1995 Lars Fenneberg
34
*
45
* Copyright 1992 Livingston Enterprises, Inc.
@@ -50,6 +51,20 @@ VALUE_PAIR *rc_avpair_add (rc_handle const *rh, VALUE_PAIR **list, int attrid, v
5051

5152
}
5253

54+
/** Iterates through the attribute-value pairs
55+
*
56+
* The attribute-value are organized in a linked-list, and this
57+
* function provides a way to iterate them given the first element
58+
* initially.
59+
*
60+
* @param t the current pair.
61+
* @return pointer to the next pair, or NULL when finished.
62+
*/
63+
VALUE_PAIR *rc_avpair_next (VALUE_PAIR *t)
64+
{
65+
return t->next;
66+
}
67+
5368
/** Assigns the given value to an attribute-value pair
5469
*
5570
* @param vp a pointer to a VALUE_PAIR structure.
@@ -854,34 +869,94 @@ char *rc_avpair_log(rc_handle const *rh, VALUE_PAIR *pair, char *buf, size_t buf
854869
return buf;
855870
}
856871

857-
/** Get a sequence of attribute value pairs from the file input and make them into a list of value_pairs
872+
/** Get the integer value of the given attribute value-pair
858873
*
859-
* @param rh a handle to parsed configuration.
860-
* @param input a %FILE handle.
861-
* @return the array of value pairs.
874+
* This function is valid for PW_TYPE_INTEGER, PW_TYPE_IPADDR.
875+
* PW_TYPE_DATE. In PW_TYPE_IPADDR this value will contain the
876+
* IPv4 address in host by order.
877+
*
878+
* @param vp a pointer to a VALUE_PAIR structure.
879+
* @param res The integer value returned.
880+
* @return zero on success or -1 on failure.
862881
*/
863-
VALUE_PAIR *rc_avpair_readin(rc_handle const *rh, FILE *input)
882+
int rc_avpair_get_uint32 (VALUE_PAIR *vp, uint32_t *res)
864883
{
865-
VALUE_PAIR *vp = NULL;
866-
char buffer[1024], *q;
884+
if (vp->type == PW_TYPE_DATE || vp->type == PW_TYPE_IPADDR ||
885+
vp->type == PW_TYPE_INTEGER) {
886+
if (res)
887+
*res = vp->lvalue;
888+
return 0;
889+
} else {
890+
return -1;
891+
}
892+
}
867893

868-
while (fgets(buffer, sizeof(buffer), input) != NULL)
869-
{
870-
q = buffer;
894+
/** Get the IPv6 address and prefix value of the given attribute value-pair
895+
*
896+
* This function is valid for PW_TYPE_IPV6ADDR, PW_TYPE_IPV6PREFIX.
897+
*
898+
* @param vp a pointer to a VALUE_PAIR structure.
899+
* @param res An in6_addr structure for result to be copied in.
900+
* @param prefix If of type PW_TYPE_IPV6PREFIX the prefix will be copied (may be NULL).
901+
* @return zero on success or -1 on failure.
902+
*/
903+
int rc_avpair_get_in6 (VALUE_PAIR *vp, struct in6_addr *res, unsigned *prefix)
904+
{
905+
if (vp->type == PW_TYPE_IPV6ADDR) {
906+
memcpy(res, vp->strvalue, 16);
907+
return 0;
908+
} else if (vp->type == PW_TYPE_IPV6PREFIX) {
909+
if (vp->lvalue < 2 || vp->lvalue > 18)
910+
return -1;
871911

872-
while(*q && isspace(*q)) q++;
912+
if (res) {
913+
memset(res, 0, 16);
914+
memcpy(res, vp->strvalue+2, vp->lvalue-2);
915+
}
873916

874-
if ((*q == '\n') || (*q == '#') || (*q == '\0'))
875-
continue;
917+
if (prefix)
918+
*prefix = (unsigned char)vp->strvalue[1];
919+
return 0;
920+
}
876921

877-
if (rc_avpair_parse(rh, q, &vp) < 0) {
878-
rc_log(LOG_ERR, "rc_avpair_readin: malformed attribute: %s", buffer);
879-
rc_avpair_free(vp);
880-
return NULL;
881-
}
922+
return -1;
923+
}
924+
925+
/** Get the raw value of the given attribute value-pair
926+
*
927+
* This function is valid for PW_TYPE_STRING, PW_TYPE_IPV6ADDR,
928+
* PW_TYPE_IPV6PREFIX.
929+
*
930+
* @param vp a pointer to a VALUE_PAIR structure.
931+
* @param res The integer value returned.
932+
* @return zero on success or -1 on failure.
933+
*/
934+
int rc_avpair_get_raw (VALUE_PAIR *vp, char **res, unsigned *res_size)
935+
{
936+
if (vp->type == PW_TYPE_STRING || vp->type == PW_TYPE_IPV6ADDR ||
937+
vp->type == PW_TYPE_IPV6PREFIX) {
938+
if (res)
939+
*res = vp->strvalue;
940+
if (res_size)
941+
*res_size = vp->lvalue;
942+
return 0;
943+
} else {
944+
return -1;
882945
}
946+
}
883947

884-
return vp;
948+
/** Get the attribute ID and type of the given attribute value-pair
949+
*
950+
* @param vp a pointer to a VALUE_PAIR structure.
951+
* @param type The attribute type, of type rc_attr_type
952+
* @param id The attribute identifier, of type rc_attr_id
953+
*/
954+
void rc_avpair_get_attr (VALUE_PAIR *vp, unsigned *type, unsigned *id)
955+
{
956+
if (type)
957+
*type = vp->type;
958+
if (id)
959+
*id = vp->attribute;
885960
}
886961

887962
/** @} */

src/radacct.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
*
88
*/
99

10-
static char rcsid[] =
11-
"$Id: radacct.c,v 1.6 2007/07/11 17:29:30 cparker Exp $";
12-
1310
#include <config.h>
14-
#include <includes.h>
15-
#include <freeradius-client.h>
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <syslog.h>
14+
#include <string.h>
15+
#include <radcli/radcli.h>
16+
#include <radcli/version.h>
1617
#include <messages.h>
1718
#include <pathnames.h>
1819

@@ -29,10 +30,35 @@ void usage(void)
2930

3031
void version(void)
3132
{
32-
fprintf(stderr,"%s: %s\n", pname ,rcsid);
33+
fprintf(stderr,"%s: %s\n", pname, RADCLI_VERSION);
3334
exit(ERROR_RC);
3435
}
3536

37+
static
38+
VALUE_PAIR *rc_avpair_readin(rc_handle const *rh, FILE *input)
39+
{
40+
VALUE_PAIR *vp = NULL;
41+
char buffer[1024], *q;
42+
43+
while (fgets(buffer, sizeof(buffer), input) != NULL)
44+
{
45+
q = buffer;
46+
47+
while(*q && isspace(*q)) q++;
48+
49+
if ((*q == '\n') || (*q == '#') || (*q == '\0'))
50+
continue;
51+
52+
if (rc_avpair_parse(rh, q, &vp) < 0) {
53+
rc_log(LOG_ERR, "rc_avpair_readin: malformed attribute: %s", buffer);
54+
rc_avpair_free(vp);
55+
return NULL;
56+
}
57+
}
58+
59+
return vp;
60+
}
61+
3662
int
3763
main (int argc, char **argv)
3864
{

src/radiusclient.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ main(int argc, char **argv)
5252
int i, nas_port, ch, acct, server, ecount, firstline, theend;
5353
void *rh;
5454
size_t len;
55-
VALUE_PAIR *send, **vp;
55+
VALUE_PAIR *send;
5656
char *rc_conf, *cp;
5757
char lbuf[4096];
5858

@@ -101,19 +101,16 @@ main(int argc, char **argv)
101101

102102
if (server == 0) {
103103
send = NULL;
104-
vp = &send;
105104
for (i = 0; i < argc; i++) {
106-
if (rc_avpair_parse(rh, argv[i], vp) < 0) {
105+
if (rc_avpair_parse(rh, argv[i], &send) < 0) {
107106
fprintf(stderr, "%s: can't parse AV pair\n", argv[i]);
108107
exit(3);
109108
}
110-
vp = &send->next;
111109
}
112110
exit(process(rh, send, acct, nas_port));
113111
}
114112
while (1 == 1) {
115113
send = NULL;
116-
vp = &send;
117114
ecount = 0;
118115
firstline = 1;
119116
acct = 0;
@@ -138,11 +135,9 @@ main(int argc, char **argv)
138135
if (theend == 0) {
139136
memcpy(lbuf, cp, len);
140137
lbuf[len] = '\0';
141-
if (rc_avpair_parse(rh, lbuf, vp) < 0) {
138+
if (rc_avpair_parse(rh, lbuf, &send) < 0) {
142139
fprintf(stderr, "%s: can't parse AV pair\n", lbuf);
143140
ecount++;
144-
} else {
145-
vp = &send->next;
146141
}
147142
}
148143
}

tests/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ EXTRA_DIST = radiusclient-ipv6.conf servers-ipv6 \
99
dtls/clikey.pem dtls/radiusclient-tls.conf \
1010
dtls/radsecproxy.conf
1111

12-
AM_CPPFLAGS = -I$(srcdir) -I$(top_srcdir)/include -I$(top_builddir)
12+
AM_CPPFLAGS = -I$(srcdir) -I$(top_srcdir)/include -I$(top_srcdir)/src -I$(top_builddir)
1313
LDADD = ../lib/libfreeradius-client.la
1414

1515
nodist_check_SCRIPTS = basic-tests.sh ipv6-tests.sh tls-tests.sh

tests/tls-restart.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <unistd.h>
3434

3535
#include <radcli/radcli.h>
36+
#include <common.h>
3637

3738
#define BUF_LEN 4096
3839

@@ -43,7 +44,7 @@ int main(int argc, char **argv)
4344
int i, nas_port, ch, acct, server, ecount, firstline, theend;
4445
void *rh;
4546
size_t len;
46-
VALUE_PAIR *send, **vp;
47+
VALUE_PAIR *send;
4748
char *rc_conf, *cp;
4849
char lbuf[4096];
4950

@@ -89,20 +90,17 @@ int main(int argc, char **argv)
8990

9091
if (server == 0) {
9192
send = NULL;
92-
vp = &send;
9393
for (i = 0; i < argc; i++) {
94-
if (rc_avpair_parse(rh, argv[i], vp) < 0) {
94+
if (rc_avpair_parse(rh, argv[i], &send) < 0) {
9595
fprintf(stderr, "%s: can't parse AV pair\n",
9696
argv[i]);
9797
exit(3);
9898
}
99-
vp = &send->next;
10099
}
101100
exit(process(rh, send, acct, nas_port));
102101
}
103102
while (1 == 1) {
104103
send = NULL;
105-
vp = &send;
106104
ecount = 0;
107105
firstline = 1;
108106
acct = 0;
@@ -128,13 +126,11 @@ int main(int argc, char **argv)
128126
if (theend == 0) {
129127
memcpy(lbuf, cp, len);
130128
lbuf[len] = '\0';
131-
if (rc_avpair_parse(rh, lbuf, vp) < 0) {
129+
if (rc_avpair_parse(rh, lbuf, &send) < 0) {
132130
fprintf(stderr,
133131
"%s: can't parse AV pair\n",
134132
lbuf);
135133
ecount++;
136-
} else {
137-
vp = &send->next;
138134
}
139135
}
140136
}

0 commit comments

Comments
 (0)