summaryrefslogtreecommitdiff
path: root/lib/avp.c
blob: bdc6216bd6e6607057e4f9518b4f2149abc07d64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/* Copyright 2011 PADL Software Pty Ltd. All rights reserved.
   See the file COPYING for licensing information.  */

#if defined HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>

#include <radsec/radsec.h>
#include <radius/client.h>

#define RS_ERR(err) ((err) < 0 ? -err : RSE_OK)

void
rs_avp_free (rs_avp **vps)
{
  nr_vp_free (vps);
}

size_t
rs_avp_length (rs_const_avp *vp)
{
  if (vp == NULL)
    return 0;

  return vp->length;
}

rs_attr_type_t
rs_avp_typeof (rs_const_avp *vp)
{
  if (vp == NULL)
    return RS_TYPE_INVALID;

  return vp->da->type;
}

void
rs_avp_attrid (rs_const_avp *vp,
	       unsigned int *attr,
	       unsigned int *vendor)
{
  assert (vp != NULL);

  *attr = vp->da->attr;
  *vendor = vp->da->vendor;
}

const char *
rs_avp_name (rs_const_avp *vp)
{
  return (vp != NULL) ? vp->da->name : NULL;
}

void
rs_avp_append (rs_avp **head, rs_avp *tail)
{
  nr_vps_append (head, tail);
}

rs_avp *
rs_avp_find (rs_avp *vp, unsigned int attr, unsigned int vendor)
{
  if (vp == NULL)
    return NULL;

  return nr_vps_find (vp, attr, vendor);
}

rs_const_avp *
rs_avp_find_const (rs_const_avp *vp,
                   unsigned int attr, unsigned int vendor)
{
  if (vp == NULL)
    return NULL;

  return nr_vps_find ((rs_avp *)vp, attr, vendor);
}

rs_avp *
rs_avp_alloc (unsigned int attr, unsigned int vendor)
{
  const DICT_ATTR *da;
  VALUE_PAIR *vp;

  da = nr_dict_attr_byvalue (attr, vendor);
  if (da == NULL) {
    vp = nr_vp_alloc_raw (attr, vendor);
  } else {
    vp = nr_vp_alloc (da);
  }

  if (vp == NULL)
    return NULL;

  return vp;
}

rs_avp *
rs_avp_dup (rs_const_avp *vp)
{
  rs_avp *vp2;

  if (vp->da->flags.unknown)
    vp2 = nr_vp_alloc_raw (vp->da->attr, vp->da->vendor);
  else
    vp2 = nr_vp_alloc (vp->da);
  if (vp2 == NULL)
    return NULL;

  vp2->length = vp->length;
  vp2->tag = vp->tag;
  vp2->next = NULL;

#ifdef RS_TYPE_TLV
  if (rs_avp_is_tlv (vp)) {
    vp2->vp_tlv = malloc (vp->length);
    if (vp2->vp_tlv == NULL) {
      rs_avp_free (vp2);
      return NULL;
    }
    memcpy (vp2->vp_tlv, vp->vp_tlv, vp->length);
    return vp2;
  }
#endif

  memcpy (vp2->vp_strvalue, vp->vp_strvalue, vp->length);
  if (rs_avp_is_string (vp))
    vp2->vp_strvalue[vp->length] = '\0';

  return vp2;
}

rs_avp *
rs_avp_next (rs_avp *vp)
{
  return (vp != NULL) ? vp->next : NULL;
}

rs_const_avp *
rs_avp_next_const (rs_const_avp *vp)
{
  return (vp != NULL) ? vp->next : NULL;
}

int
rs_avp_delete (rs_avp **first,
               unsigned int attr, unsigned int vendor)
{
  int found = 0;
  rs_avp **p;

  for (p = first; *p != NULL; p++) {
    if ((*p)->da->attr == attr &&
        (*p)->da->vendor == vendor) {
      rs_avp *next = (*p)->next;

      (*p)->next = NULL;
      rs_avp_free (p);

      *p = next;
      found++;
    }
  }

  return found ? RSE_OK : RSE_ATTR_UNKNOWN;
}

const char *
rs_avp_string_value (rs_const_avp *vp)
{
  if (!rs_avp_is_string (vp))
    return NULL;

  return vp->vp_strvalue;
}

int
rs_avp_string_set (rs_avp *vp, const char *str)
{
  int err;

  if (vp == NULL)
    return RSE_INVAL;
  if (!rs_avp_is_string (vp))
    return RSE_ATTR_INVALID;

  err = nr_vp_set_data (vp, str, strlen (str));
  return RS_ERR(err);
}

uint32_t
rs_avp_integer_value (rs_const_avp *vp)
{
  if (!rs_avp_is_integer (vp))
    return 0;
  return vp->vp_integer;
}

int
rs_avp_integer_set (rs_avp *vp, uint32_t val)
{
  int err;

  if (vp == NULL)
    return RSE_INVAL;
  if (!rs_avp_is_integer (vp))
    return RSE_ATTR_INVALID;

  err = nr_vp_set_data (vp, &val, sizeof (val));
  return RS_ERR(err);
}

uint32_t
rs_avp_ipaddr_value (rs_const_avp *vp)
{
  if (!rs_avp_is_ipaddr (vp))
    return 0;
  return vp->vp_ipaddr;
}

int
rs_avp_ipaddr_set (rs_avp *vp, struct in_addr in)
{
  int err;

  if (vp == NULL)
    return RSE_INVAL;
  if (!rs_avp_is_ipaddr (vp))
    return RSE_ATTR_INVALID;

  err = nr_vp_set_data (vp, &in, sizeof (in));
  return RS_ERR(err);
}

time_t
rs_avp_date_value (rs_const_avp *vp)
{
  if (!rs_avp_is_date (vp))
    return 0;
  return vp->vp_date;
}

int
rs_avp_date_set (rs_avp *vp, time_t date)
{
  uint32_t date32;
  int err;

  if (vp == NULL)
    return RSE_INVAL;
  if (!rs_avp_is_date (vp))
    return RSE_ATTR_INVALID;
  if (date > 0xFFFFFFFF)
    return RSE_ATTR_INVALID;

  date32 = (uint32_t)date;
  err = nr_vp_set_data (vp, &date32, sizeof (date32));

  return RS_ERR(err);
}

const unsigned char *
rs_avp_octets_value_const_ptr (rs_const_avp *vp)
{
  return rs_avp_octets_value_ptr ((rs_avp *)vp);
}

unsigned char *
rs_avp_octets_value_ptr (rs_avp *vp)
{
  if (vp == NULL)
    return NULL;

#ifdef RS_TYPE_TLV
  if (rs_avp_is_tlv (vp))
    return vp->vp_tlv;
#endif

  return vp->vp_octets;
}

int
rs_avp_octets_value_byref (rs_avp *vp,
			   unsigned char **p,
			   size_t *len)
{
  if (vp == NULL)
    return RSE_INVAL;

  *len = vp->length;
  *p = (unsigned char *)rs_avp_octets_value_ptr (vp);

  return RSE_OK;
}

int
rs_avp_octets_value (rs_const_avp *vp,
		     unsigned char *buf,
		     size_t *len)
{
  if (vp == NULL)
    return RSE_INVAL;

  if (vp->length > *len) {
    *len = vp->length;
    return RSE_ATTR_TOO_SMALL;
  }

  *len = vp->length;

#ifdef RS_TYPE_TLV
  if (rs_avp_is_tlv (vp))
    memcpy (buf, vp->vp_tlv, vp->length);
  else
#endif
    memcpy (buf, vp->vp_octets, vp->length);

  return RSE_OK;
}

int
rs_avp_fragmented_value (rs_const_avp *vps,
		         unsigned char *buf,
		         size_t *len)
{
  size_t total_len = 0;
  unsigned char *p;
  rs_const_avp *vp;

  if (vps == NULL)
    return RSE_INVAL;

  if (!rs_avp_is_octets (vps) &&
      !rs_avp_is_string (vps))
    return RSE_ATTR_INVALID;

  for (vp = vps;
       vp != NULL;
       vp = rs_avp_find_const (vp->next, vp->da->attr, vp->da->vendor))
    total_len += vp->length;

  if (*len < total_len) {
    *len = total_len;
    return RSE_ATTR_TOO_SMALL;
  }

  for (vp = vps, p = buf;
       vp != NULL;
       vp = rs_avp_find_const (vp->next, vp->da->attr, vp->da->vendor)) {
    memcpy (p, vp->vp_octets, vp->length);
    p += vp->length;
  }

  *len = total_len;

  return RSE_OK;
}

int
rs_avp_octets_set (rs_avp *vp,
		   const unsigned char *buf,
		   size_t len)
{
  int err;

  if (!rs_avp_is_octets (vp))
    return RSE_ATTR_INVALID;

  err = nr_vp_set_data (vp, buf, len);

  return RS_ERR(err);
}

int
rs_avp_ifid_value (rs_const_avp *vp, uint8_t val[8])
{
  if (!rs_avp_is_ifid (vp))
    return RSE_ATTR_INVALID;

  memcpy (val, vp->vp_ifid, 8);

  return RSE_OK;
}

int
rs_avp_ifid_set (rs_avp *vp, const uint8_t val[8])
{
  int err;

  if (!rs_avp_is_ifid (vp))
    return RSE_ATTR_INVALID;

  err = nr_vp_set_data (vp, val, 8);
  return RS_ERR(err);
}

uint8_t
rs_avp_byte_value (rs_const_avp *vp)
{
  if (!rs_avp_is_byte (vp))
    return 0;
  return vp->vp_integer;
}

int
rs_avp_byte_set (rs_avp *vp, uint8_t val)
{
  int err;

  if (!rs_avp_is_byte (vp))
    return RSE_ATTR_INVALID;

  err = nr_vp_set_data (vp, &val, sizeof (val));
  return RS_ERR(err);
}

uint16_t
rs_avp_short_value (rs_const_avp *vp)
{
  if (!rs_avp_is_short (vp))
    return 0;
  return vp->vp_integer;
}

int
rs_avp_short_set (rs_avp *vp, uint16_t val)
{
  int err;

  if (!rs_avp_is_short (vp))
    return RSE_ATTR_INVALID;

  err = nr_vp_set_data (vp, &val, sizeof (val));
  return RS_ERR(err);
}

int
rs_attr_find (const char *name,
              unsigned int *attr,
              unsigned int *vendor)
{
  const DICT_ATTR *da;

  da = nr_dict_attr_byname (name);
  if (da == NULL)
    return RSE_ATTR_UNKNOWN;

  *attr = da->attr;
  *vendor = da->vendor;

  return RSE_OK;
}

int
rs_attr_display_name (unsigned int attr,
                      unsigned int vendor,
                      char *buffer,
                      size_t bufsize,
                      int canonical)
{
  const DICT_ATTR *da = NULL;
  DICT_ATTR da2;
  int err;

  if (!canonical) {
    da = nr_dict_attr_byvalue (attr, vendor);
  }
  if (da == NULL) {
    err = nr_dict_attr_2struct(&da2, attr, vendor,
                               buffer, bufsize);
    if (err < 0)
      return -err;
  } else {
    snprintf(buffer, bufsize, "%s", da->name);
  }

  return RSE_OK;
}

int
rs_attr_parse_name (const char *name,
		    unsigned int *attr,
		    unsigned int *vendor)
{
  const DICT_ATTR *da;

  if (strncmp(name, "Attr-", 5) == 0) {
    char *s = (char *)&name[5];
    unsigned int tmp;

    tmp = strtoul(s, &s, 10);
    if (*s == '.') {
      s++;

      switch (tmp) {
      case PW_VENDOR_SPECIFIC:
	*vendor = strtoul(s, &s, 10);
	if (*s != '.')
	  return RSE_ATTR_BAD_NAME;

	s++;

	*attr = strtoul(s, &s, 10);
	if (*s != '\0')
	  return RSE_ATTR_BAD_NAME;

	break;
      default:
	return RSE_ATTR_BAD_NAME;
      }
    } else {
      *attr = tmp;
      *vendor = 0;
    }
  } else {
    da = nr_dict_attr_byname (name);
    if (da == NULL)
      return RSE_ATTR_UNKNOWN;

    *attr = da->attr;
    *vendor = da->vendor;
  }

  return RSE_OK;
}

size_t
rs_avp_display_value (rs_const_avp *vp,
                      char *buffer,
                      size_t buflen)
{
  return nr_vp_snprintf_value (buffer, buflen, vp);
}