2 * $Id: avpair.c,v 1.1 2004/11/14 07:26:26 paulus Exp $
4 * Copyright (C) 1995 Lars Fenneberg
6 * Copyright 1992 Livingston Enterprises, Inc.
8 * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
9 * and Merit Network, Inc. All Rights Reserved
11 * See the file COPYRIGHT for the respective terms and conditions.
12 * If the file is missing contact me at lf@elemental.net
13 * and I'll send you a copy.
18 #include <radiusclient.h>
20 static void rc_extract_vendor_specific_attributes(int attrlen,
24 * Function: rc_avpair_add
26 * Purpose: add an attribute-value pair to the given list.
28 * Returns: pointer to added a/v pair upon success, NULL pointer upon failure.
30 * Remarks: Always appends the new pair to the end of the list.
34 VALUE_PAIR *rc_avpair_add (VALUE_PAIR **list, int attrid, void *pval, int len,
39 vp = rc_avpair_new (attrid, pval, len, vendorcode);
41 if (vp != (VALUE_PAIR *) NULL)
43 rc_avpair_insert (list, (VALUE_PAIR *) NULL, vp);
51 * Function: rc_avpair_assign
53 * Purpose: assign the given value to an attribute-value pair.
55 * Returns: 0 on success,
60 int rc_avpair_assign (VALUE_PAIR *vp, void *pval, int len)
68 if (((len == 0) && (strlen ((char *) pval)) > AUTH_STRING_LEN)
69 || (len > AUTH_STRING_LEN)) {
70 error("rc_avpair_assign: bad attribute length");
75 memcpy(vp->strvalue, (char *)pval, len);
76 vp->strvalue[len] = '\0';
79 strncpy (vp->strvalue, (char *) pval, AUTH_STRING_LEN);
80 vp->lvalue = strlen((char *) pval);
90 vp->lvalue = * (UINT4 *) pval;
96 error("rc_avpair_assign: unknown attribute %d", vp->type);
102 * Function: rc_avpair_new
104 * Purpose: make a new attribute-value pair with given parameters.
106 * Returns: pointer to generated a/v pair when successful, NULL when failure.
110 VALUE_PAIR *rc_avpair_new (int attrid, void *pval, int len, int vendorcode)
112 VALUE_PAIR *vp = (VALUE_PAIR *) NULL;
115 if ((pda = rc_dict_getattr (attrid, vendorcode)) == (DICT_ATTR *) NULL)
117 error("rc_avpair_new: unknown attribute %d", attrid);
121 if ((vp = (VALUE_PAIR *) malloc (sizeof (VALUE_PAIR)))
122 != (VALUE_PAIR *) NULL)
124 strncpy (vp->name, pda->name, sizeof (vp->name));
125 vp->attribute = attrid;
126 vp->vendorcode = vendorcode;
127 vp->next = (VALUE_PAIR *) NULL;
128 vp->type = pda->type;
129 if (rc_avpair_assign (vp, pval, len) == 0)
134 vp = (VALUE_PAIR *) NULL;
137 novm("rc_avpair_new");
144 * Function: rc_avpair_gen
146 * Purpose: takes attribute/value pairs from buffer and builds a
147 * value_pair list using allocated memory.
149 * Returns: value_pair list or NULL on failure
152 VALUE_PAIR *rc_avpair_gen (AUTH_HDR *auth)
159 unsigned char *x_ptr;
164 unsigned char hex[3]; /* For hex string conversion. */
168 * Extract attribute-value pairs
171 length = ntohs ((unsigned short) auth->length) - AUTH_HDR_LEN;
172 vp = (VALUE_PAIR *) NULL;
181 error("rc_avpair_gen: received attribute with invalid length");
185 /* Handle vendor-specific specially */
186 if (attribute == PW_VENDOR_SPECIFIC) {
187 rc_extract_vendor_specific_attributes(attrlen, ptr, &vp);
189 length -= (attrlen + 2);
192 if ((attr = rc_dict_getattr (attribute, VENDOR_NONE)) == (DICT_ATTR *) NULL)
194 *buffer= '\0'; /* Initial length. */
195 for (x_ptr = ptr, x_len = attrlen ;
199 sprintf (hex, "%2.2X", *x_ptr);
200 strcat (buffer, hex);
202 warn("rc_avpair_gen: received unknown attribute %d of length %d: 0x%s",
203 attribute, attrlen, buffer);
208 (VALUE_PAIR *) malloc (sizeof (VALUE_PAIR))) ==
211 novm("rc_avpair_gen");
215 strcpy (pair->name, attr->name);
216 pair->attribute = attr->value;
217 pair->vendorcode = VENDOR_NONE;
218 pair->type = attr->type;
219 pair->next = (VALUE_PAIR *) NULL;
225 memcpy (pair->strvalue, (char *) ptr, (size_t) attrlen);
226 pair->strvalue[attrlen] = '\0';
227 pair->lvalue = attrlen;
228 rc_avpair_insert (&vp, (VALUE_PAIR *) NULL, pair);
231 case PW_TYPE_INTEGER:
233 memcpy ((char *) &lvalue, (char *) ptr,
235 pair->lvalue = ntohl (lvalue);
236 rc_avpair_insert (&vp, (VALUE_PAIR *) NULL, pair);
240 warn("rc_avpair_gen: %s has unknown type", attr->name);
247 length -= attrlen + 2;
253 * Function: rc_extract_vendor_specific_attributes
255 * Purpose: Extracts vendor-specific attributes, assuming they are in
256 * the "SHOULD" format recommended by RCF 2138.
258 * Returns: found value_pair
261 static void rc_extract_vendor_specific_attributes(int attrlen,
272 /* ptr is sitting at vendor-ID */
274 /* Nothing to see here... */
278 /* High-order octet of Vendor-Id must be zero (RFC2138) */
283 /* Extract vendor_id */
285 ((unsigned int) ptr[1]) * 256 * 256 +
286 ((unsigned int) ptr[2]) * 256 +
287 ((unsigned int) ptr[3]));
288 /* Bump ptr up to contents */
291 /* Set attrlen to length of data */
293 for (; attrlen; attrlen -= vlen+2, ptr += vlen) {
297 if (vlen < 0 || vlen > attrlen - 2) {
298 /* Do not log an error. We are supposed to be able to cope with
299 arbitrary vendor-specific gunk */
302 /* Looks plausible... */
303 if ((attr = rc_dict_getattr(vtype, vendor_id)) == NULL) {
307 /* TODO: Check that length matches data size!!!!! */
308 pair = (VALUE_PAIR *) malloc(sizeof(VALUE_PAIR));
310 novm("rc_avpair_gen");
313 strcpy(pair->name, attr->name);
314 pair->attribute = attr->value;
315 pair->vendorcode = vendor_id;
316 pair->type = attr->type;
318 switch (attr->type) {
320 memcpy (pair->strvalue, (char *) ptr, (size_t) vlen);
321 pair->strvalue[vlen] = '\0';
323 rc_avpair_insert (vp, (VALUE_PAIR *) NULL, pair);
326 case PW_TYPE_INTEGER:
328 memcpy ((char *) &lvalue, (char *) ptr,
330 pair->lvalue = ntohl (lvalue);
331 rc_avpair_insert (vp, (VALUE_PAIR *) NULL, pair);
335 warn("rc_avpair_gen: %s has unknown type", attr->name);
343 * Function: rc_avpair_get
345 * Purpose: Find the first attribute value-pair (which matches the given
346 * attribute) from the specified value-pair list.
348 * Returns: found value_pair
352 VALUE_PAIR *rc_avpair_get (VALUE_PAIR *vp, UINT4 attr)
354 for (; vp != (VALUE_PAIR *) NULL && vp->attribute != attr; vp = vp->next)
362 * Function: rc_avpair_copy
364 * Purpose: Return a copy of the existing list "p" ala strdup().
367 VALUE_PAIR *rc_avpair_copy(VALUE_PAIR *p)
369 VALUE_PAIR *vp, *fp = NULL, *lp = NULL;
372 vp = malloc(sizeof(VALUE_PAIR));
374 novm("rc_avpair_copy");
375 return NULL; /* leaks a little but so what */
390 * Function: rc_avpair_insert
392 * Purpose: Given the address of an existing list "a" and a pointer
393 * to an entry "p" in that list, add the list "b" to
394 * the "a" list after the "p" entry. If "p" is NULL, add
395 * the list "b" to the end of "a".
399 void rc_avpair_insert (VALUE_PAIR **a, VALUE_PAIR *p, VALUE_PAIR *b)
401 VALUE_PAIR *this_node = NULL;
404 if (*a == (VALUE_PAIR *) NULL)
415 if ( p == (VALUE_PAIR *) NULL) /* run to end of "a" list */
417 while (vp != (VALUE_PAIR *) NULL)
423 else /* look for the "p" entry in the "a" list (or run to end) */
426 while (this_node != (VALUE_PAIR *) NULL)
432 this_node = this_node->next;
436 /* add "b" at this_node */
437 vp = this_node->next;
440 /* run to end of "b" and connect the rest of "a" */
449 * Function: rc_avpair_free
451 * Purpose: frees all value_pairs in the list
455 void rc_avpair_free (VALUE_PAIR *pair)
459 while (pair != (VALUE_PAIR *) NULL)
468 * Function: rc_fieldcpy
470 * Purpose: Copy a data field from the buffer. Advance the buffer
471 * past the data field.
475 static void rc_fieldcpy (char *string, char **uptr)
483 while (*ptr != '"' && *ptr != '\0' && *ptr != '\n')
496 while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0' && *ptr != '\n' &&
497 *ptr != '=' && *ptr != ',')
508 * Function: rc_avpair_parse
510 * Purpose: parses the buffer to extract the attribute-value pairs.
512 * Returns: 0 = successful parse of attribute-value pair,
513 * -1 = syntax (or other) error detected.
517 #define PARSE_MODE_NAME 0
518 #define PARSE_MODE_EQUAL 1
519 #define PARSE_MODE_VALUE 2
520 #define PARSE_MODE_INVALID 3
522 int rc_avpair_parse (char *buffer, VALUE_PAIR **first_pair)
525 char attrstr[AUTH_ID_LEN];
526 char valstr[AUTH_ID_LEN];
527 DICT_ATTR *attr = NULL;
534 mode = PARSE_MODE_NAME;
535 while (*buffer != '\n' && *buffer != '\0')
537 if (*buffer == ' ' || *buffer == '\t')
545 case PARSE_MODE_NAME: /* Attribute Name */
546 rc_fieldcpy (attrstr, &buffer);
548 rc_dict_findattr (attrstr)) == (DICT_ATTR *) NULL)
550 error("rc_avpair_parse: unknown attribute");
552 rc_avpair_free(*first_pair);
553 *first_pair = (VALUE_PAIR *) NULL;
557 mode = PARSE_MODE_EQUAL;
560 case PARSE_MODE_EQUAL: /* Equal sign */
563 mode = PARSE_MODE_VALUE;
568 error("rc_avpair_parse: missing or misplaced equal sign");
570 rc_avpair_free(*first_pair);
571 *first_pair = (VALUE_PAIR *) NULL;
577 case PARSE_MODE_VALUE: /* Value */
578 rc_fieldcpy (valstr, &buffer);
581 (VALUE_PAIR *) malloc (sizeof (VALUE_PAIR)))
582 == (VALUE_PAIR *) NULL)
584 novm("rc_avpair_parse");
586 rc_avpair_free(*first_pair);
587 *first_pair = (VALUE_PAIR *) NULL;
591 strcpy (pair->name, attr->name);
592 pair->attribute = attr->value;
593 pair->type = attr->type;
594 pair->vendorcode = attr->vendorcode;
600 strcpy (pair->strvalue, valstr);
601 pair->lvalue = strlen(valstr);
604 case PW_TYPE_INTEGER:
605 if (isdigit (*valstr))
607 pair->lvalue = atoi (valstr);
611 if ((dval = rc_dict_findval (valstr))
612 == (DICT_VALUE *) NULL)
614 error("rc_avpair_parse: unknown attribute value: %s", valstr);
616 rc_avpair_free(*first_pair);
617 *first_pair = (VALUE_PAIR *) NULL;
624 pair->lvalue = dval->value;
630 pair->lvalue = rc_get_ipaddr(valstr);
635 tm = localtime (&timeval);
639 rc_str2tm (valstr, tm);
641 pair->lvalue = (UINT4) timelocal (tm);
642 #else /* TIMELOCAL */
643 pair->lvalue = (UINT4) mktime (tm);
644 #endif /* TIMELOCAL */
648 error("rc_avpair_parse: unknown attribute type %d", pair->type);
650 rc_avpair_free(*first_pair);
651 *first_pair = (VALUE_PAIR *) NULL;
656 pair->next = (VALUE_PAIR *) NULL;
658 if (*first_pair == (VALUE_PAIR *) NULL)
665 while (link->next != (VALUE_PAIR *) NULL)
672 mode = PARSE_MODE_NAME;
676 mode = PARSE_MODE_NAME;
684 * Function: rc_avpair_tostr
686 * Purpose: Translate an av_pair into two strings
688 * Returns: 0 on success, -1 on failure
692 int rc_avpair_tostr (VALUE_PAIR *pair, char *name, int ln, char *value, int lv)
699 *name = *value = '\0';
701 if (!pair || pair->name[0] == '\0') {
702 error("rc_avpair_tostr: pair is NULL or empty");
706 strncpy(name, pair->name, (size_t) ln);
712 ptr = (unsigned char *) pair->strvalue;
715 if (!(isprint (*ptr)))
717 sprintf (buffer, "\\%03o", *ptr);
718 strncat(value, buffer, (size_t) lv);
724 strncat(value, ptr, 1);
732 case PW_TYPE_INTEGER:
733 dval = rc_dict_getval (pair->lvalue, pair->name);
734 if (dval != (DICT_VALUE *) NULL)
736 strncpy(value, dval->name, (size_t) lv-1);
740 sprintf (buffer, "%ld", pair->lvalue);
741 strncpy(value, buffer, (size_t) lv);
746 inad.s_addr = htonl(pair->lvalue);
747 strncpy (value, inet_ntoa (inad), (size_t) lv-1);
751 strftime (buffer, sizeof (buffer), "%m/%d/%y %H:%M:%S",
752 gmtime ((time_t *) & pair->lvalue));
753 strncpy(value, buffer, lv-1);
757 error("rc_avpair_tostr: unknown attribute type %d", pair->type);
766 * Function: rc_avpair_readin
768 * Purpose: get a sequence of attribute value pairs from the file input
769 * and make them into a list of value_pairs
773 VALUE_PAIR *rc_avpair_readin(FILE *input)
775 VALUE_PAIR *vp = NULL;
776 char buffer[1024], *q;
778 while (fgets(buffer, sizeof(buffer), input) != NULL)
782 while(*q && isspace(*q)) q++;
784 if ((*q == '\n') || (*q == '#') || (*q == '\0'))
787 if (rc_avpair_parse(q, &vp) < 0) {
788 error("rc_avpair_readin: malformed attribute: %s", buffer);