2 * $Id: avpair.c,v 1.1 2002/01/22 16:03:02 dfs 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.
19 #include <radiusclient.h>
21 static void rc_extract_vendor_specific_attributes(int attrlen,
25 * Function: rc_avpair_add
27 * Purpose: add an attribute-value pair to the given list.
29 * Returns: pointer to added a/v pair upon success, NULL pointer upon failure.
31 * Remarks: Always appends the new pair to the end of the list.
35 VALUE_PAIR *rc_avpair_add (VALUE_PAIR **list, int attrid, void *pval, int len,
40 vp = rc_avpair_new (attrid, pval, len, vendorcode);
42 if (vp != (VALUE_PAIR *) NULL)
44 rc_avpair_insert (list, (VALUE_PAIR *) NULL, vp);
52 * Function: rc_avpair_assign
54 * Purpose: assign the given value to an attribute-value pair.
56 * Returns: 0 on success,
61 int rc_avpair_assign (VALUE_PAIR *vp, void *pval, int len)
69 if (((len == 0) && (strlen ((char *) pval)) > AUTH_STRING_LEN)
70 || (len > AUTH_STRING_LEN)) {
71 rc_log(LOG_ERR, "rc_avpair_assign: bad attribute length");
76 memcpy(vp->strvalue, (char *)pval, len);
77 vp->strvalue[len] = '\0';
80 strncpy (vp->strvalue, (char *) pval, AUTH_STRING_LEN);
81 vp->lvalue = strlen((char *) pval);
91 vp->lvalue = * (UINT4 *) pval;
97 rc_log(LOG_ERR, "rc_avpair_assign: unknown attribute %d", vp->type);
103 * Function: rc_avpair_new
105 * Purpose: make a new attribute-value pair with given parameters.
107 * Returns: pointer to generated a/v pair when successful, NULL when failure.
111 VALUE_PAIR *rc_avpair_new (int attrid, void *pval, int len, int vendorcode)
113 VALUE_PAIR *vp = (VALUE_PAIR *) NULL;
116 if ((pda = rc_dict_getattr (attrid, vendorcode)) == (DICT_ATTR *) NULL)
118 rc_log(LOG_ERR,"rc_avpair_new: unknown attribute %d", attrid);
122 if ((vp = (VALUE_PAIR *) malloc (sizeof (VALUE_PAIR)))
123 != (VALUE_PAIR *) NULL)
125 strncpy (vp->name, pda->name, sizeof (vp->name));
126 vp->attribute = attrid;
127 vp->vendorcode = vendorcode;
128 vp->next = (VALUE_PAIR *) NULL;
129 vp->type = pda->type;
130 if (rc_avpair_assign (vp, pval, len) == 0)
135 vp = (VALUE_PAIR *) NULL;
139 rc_log(LOG_CRIT,"rc_avpair_new: out of memory");
147 * Function: rc_avpair_gen
149 * Purpose: takes attribute/value pairs from buffer and builds a
150 * value_pair list using allocated memory.
152 * Returns: value_pair list or NULL on failure
155 VALUE_PAIR *rc_avpair_gen (AUTH_HDR *auth)
162 unsigned char *x_ptr;
167 unsigned char hex[3]; /* For hex string conversion. */
171 * Extract attribute-value pairs
174 length = ntohs ((unsigned short) auth->length) - AUTH_HDR_LEN;
175 vp = (VALUE_PAIR *) NULL;
184 rc_log(LOG_ERR, "rc_avpair_gen: received attribute with invalid length");
188 /* Handle vendor-specific specially */
189 if (attribute == PW_VENDOR_SPECIFIC) {
190 rc_extract_vendor_specific_attributes(attrlen, ptr, &vp);
192 length -= (attrlen + 2);
195 if ((attr = rc_dict_getattr (attribute, VENDOR_NONE)) == (DICT_ATTR *) NULL)
197 *buffer= '\0'; /* Initial length. */
198 for (x_ptr = ptr, x_len = attrlen ;
202 sprintf (hex, "%2.2X", *x_ptr);
203 strcat (buffer, hex);
205 rc_log(LOG_WARNING, "rc_avpair_gen: received unknown attribute %d of length %d: 0x%s",
206 attribute, attrlen, buffer);
211 (VALUE_PAIR *) malloc (sizeof (VALUE_PAIR))) ==
214 rc_log(LOG_CRIT, "rc_avpair_gen: out of memory");
218 strcpy (pair->name, attr->name);
219 pair->attribute = attr->value;
220 pair->vendorcode = VENDOR_NONE;
221 pair->type = attr->type;
222 pair->next = (VALUE_PAIR *) NULL;
228 memcpy (pair->strvalue, (char *) ptr, (size_t) attrlen);
229 pair->strvalue[attrlen] = '\0';
230 pair->lvalue = attrlen;
231 rc_avpair_insert (&vp, (VALUE_PAIR *) NULL, pair);
234 case PW_TYPE_INTEGER:
236 memcpy ((char *) &lvalue, (char *) ptr,
238 pair->lvalue = ntohl (lvalue);
239 rc_avpair_insert (&vp, (VALUE_PAIR *) NULL, pair);
243 rc_log(LOG_WARNING, "rc_avpair_gen: %s has unknown type", attr->name);
250 length -= attrlen + 2;
256 * Function: rc_extract_vendor_specific_attributes
258 * Purpose: Extracts vendor-specific attributes, assuming they are in
259 * the "SHOULD" format recommended by RCF 2138.
261 * Returns: found value_pair
264 static void rc_extract_vendor_specific_attributes(int attrlen,
275 /* ptr is sitting at vendor-ID */
277 /* Nothing to see here... */
281 /* High-order octet of Vendor-Id must be zero (RFC2138) */
286 /* Extract vendor_id */
288 ((unsigned int) ptr[1]) * 256 * 256 +
289 ((unsigned int) ptr[2]) * 256 +
290 ((unsigned int) ptr[3]));
291 /* Bump ptr up to contents */
294 /* Set attrlen to length of data */
296 for (; attrlen; attrlen -= vlen+2, ptr += vlen) {
300 if (vlen < 0 || vlen > attrlen - 2) {
301 /* Do not log an error. We are supposed to be able to cope with
302 arbitrary vendor-specific gunk */
305 /* Looks plausible... */
306 if ((attr = rc_dict_getattr(vtype, vendor_id)) == NULL) {
310 /* TODO: Check that length matches data size!!!!! */
311 pair = (VALUE_PAIR *) malloc(sizeof(VALUE_PAIR));
313 rc_log(LOG_CRIT, "rc_avpair_gen: out of memory");
316 strcpy(pair->name, attr->name);
317 pair->attribute = attr->value;
318 pair->vendorcode = vendor_id;
319 pair->type = attr->type;
321 switch (attr->type) {
323 memcpy (pair->strvalue, (char *) ptr, (size_t) vlen);
324 pair->strvalue[vlen] = '\0';
326 rc_avpair_insert (vp, (VALUE_PAIR *) NULL, pair);
329 case PW_TYPE_INTEGER:
331 memcpy ((char *) &lvalue, (char *) ptr,
333 pair->lvalue = ntohl (lvalue);
334 rc_avpair_insert (vp, (VALUE_PAIR *) NULL, pair);
338 rc_log(LOG_WARNING, "rc_avpair_gen: %s has unknown type", attr->name);
346 * Function: rc_avpair_get
348 * Purpose: Find the first attribute value-pair (which matches the given
349 * attribute) from the specified value-pair list.
351 * Returns: found value_pair
355 VALUE_PAIR *rc_avpair_get (VALUE_PAIR *vp, UINT4 attr)
357 for (; vp != (VALUE_PAIR *) NULL && vp->attribute != attr; vp = vp->next)
365 * Function: rc_avpair_insert
367 * Purpose: Given the address of an existing list "a" and a pointer
368 * to an entry "p" in that list, add the value pair "b" to
369 * the "a" list after the "p" entry. If "p" is NULL, add
370 * the value pair "b" to the end of "a".
374 void rc_avpair_insert (VALUE_PAIR **a, VALUE_PAIR *p, VALUE_PAIR *b)
376 VALUE_PAIR *this_node = NULL;
379 if (b->next != (VALUE_PAIR *) NULL)
381 rc_log(LOG_CRIT, "rc_avpair_insert: value pair (0x%p) next ptr. (0x%p) not NULL", b, b->next);
385 if (*a == (VALUE_PAIR *) NULL)
393 if ( p == (VALUE_PAIR *) NULL) /* run to end of "a" list */
395 while (vp != (VALUE_PAIR *) NULL)
401 else /* look for the "p" entry in the "a" list */
404 while (this_node != (VALUE_PAIR *) NULL)
410 this_node = this_node->next;
414 b->next = this_node->next;
421 * Function: rc_avpair_free
423 * Purpose: frees all value_pairs in the list
427 void rc_avpair_free (VALUE_PAIR *pair)
431 while (pair != (VALUE_PAIR *) NULL)
440 * Function: rc_fieldcpy
442 * Purpose: Copy a data field from the buffer. Advance the buffer
443 * past the data field.
447 static void rc_fieldcpy (char *string, char **uptr)
455 while (*ptr != '"' && *ptr != '\0' && *ptr != '\n')
468 while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0' && *ptr != '\n' &&
469 *ptr != '=' && *ptr != ',')
480 * Function: rc_avpair_parse
482 * Purpose: parses the buffer to extract the attribute-value pairs.
484 * Returns: 0 = successful parse of attribute-value pair,
485 * -1 = syntax (or other) error detected.
489 #define PARSE_MODE_NAME 0
490 #define PARSE_MODE_EQUAL 1
491 #define PARSE_MODE_VALUE 2
492 #define PARSE_MODE_INVALID 3
494 int rc_avpair_parse (char *buffer, VALUE_PAIR **first_pair)
497 char attrstr[AUTH_ID_LEN];
498 char valstr[AUTH_ID_LEN];
499 DICT_ATTR *attr = NULL;
506 mode = PARSE_MODE_NAME;
507 while (*buffer != '\n' && *buffer != '\0')
509 if (*buffer == ' ' || *buffer == '\t')
517 case PARSE_MODE_NAME: /* Attribute Name */
518 rc_fieldcpy (attrstr, &buffer);
520 rc_dict_findattr (attrstr)) == (DICT_ATTR *) NULL)
522 rc_log(LOG_ERR, "rc_avpair_parse: unknown attribute");
524 rc_avpair_free(*first_pair);
525 *first_pair = (VALUE_PAIR *) NULL;
529 mode = PARSE_MODE_EQUAL;
532 case PARSE_MODE_EQUAL: /* Equal sign */
535 mode = PARSE_MODE_VALUE;
540 rc_log(LOG_ERR, "rc_avpair_parse: missing or misplaced equal sign");
542 rc_avpair_free(*first_pair);
543 *first_pair = (VALUE_PAIR *) NULL;
549 case PARSE_MODE_VALUE: /* Value */
550 rc_fieldcpy (valstr, &buffer);
553 (VALUE_PAIR *) malloc (sizeof (VALUE_PAIR)))
554 == (VALUE_PAIR *) NULL)
556 rc_log(LOG_CRIT, "rc_avpair_parse: out of memory");
558 rc_avpair_free(*first_pair);
559 *first_pair = (VALUE_PAIR *) NULL;
563 strcpy (pair->name, attr->name);
564 pair->attribute = attr->value;
565 pair->type = attr->type;
571 strcpy (pair->strvalue, valstr);
572 pair->lvalue = strlen(valstr);
575 case PW_TYPE_INTEGER:
576 if (isdigit (*valstr))
578 pair->lvalue = atoi (valstr);
582 if ((dval = rc_dict_findval (valstr))
583 == (DICT_VALUE *) NULL)
585 rc_log(LOG_ERR, "rc_avpair_parse: unknown attribute value: %s", valstr);
587 rc_avpair_free(*first_pair);
588 *first_pair = (VALUE_PAIR *) NULL;
595 pair->lvalue = dval->value;
601 pair->lvalue = rc_get_ipaddr(valstr);
606 tm = localtime (&timeval);
610 rc_str2tm (valstr, tm);
612 pair->lvalue = (UINT4) timelocal (tm);
613 #else /* TIMELOCAL */
614 pair->lvalue = (UINT4) mktime (tm);
615 #endif /* TIMELOCAL */
619 rc_log(LOG_ERR, "rc_avpair_parse: unknown attribute type %d", pair->type);
621 rc_avpair_free(*first_pair);
622 *first_pair = (VALUE_PAIR *) NULL;
627 pair->next = (VALUE_PAIR *) NULL;
629 if (*first_pair == (VALUE_PAIR *) NULL)
636 while (link->next != (VALUE_PAIR *) NULL)
643 mode = PARSE_MODE_NAME;
647 mode = PARSE_MODE_NAME;
655 * Function: rc_avpair_tostr
657 * Purpose: Translate an av_pair into two strings
659 * Returns: 0 on success, -1 on failure
663 int rc_avpair_tostr (VALUE_PAIR *pair, char *name, int ln, char *value, int lv)
670 *name = *value = '\0';
672 if (!pair || pair->name[0] == '\0') {
673 rc_log(LOG_ERR, "rc_avpair_tostr: pair is NULL or empty");
677 strncpy(name, pair->name, (size_t) ln);
683 ptr = (unsigned char *) pair->strvalue;
686 if (!(isprint (*ptr)))
688 sprintf (buffer, "\\%03o", *ptr);
689 strncat(value, buffer, (size_t) lv);
695 strncat(value, ptr, 1);
703 case PW_TYPE_INTEGER:
704 dval = rc_dict_getval (pair->lvalue, pair->name);
705 if (dval != (DICT_VALUE *) NULL)
707 strncpy(value, dval->name, (size_t) lv-1);
711 sprintf (buffer, "%ld", pair->lvalue);
712 strncpy(value, buffer, (size_t) lv);
717 inad.s_addr = htonl(pair->lvalue);
718 strncpy (value, inet_ntoa (inad), (size_t) lv-1);
722 strftime (buffer, sizeof (buffer), "%m/%d/%y %H:%M:%S",
723 gmtime ((time_t *) & pair->lvalue));
724 strncpy(value, buffer, lv-1);
728 rc_log(LOG_ERR, "rc_avpair_tostr: unknown attribute type %d", pair->type);
737 * Function: rc_avpair_readin
739 * Purpose: get a sequence of attribute value pairs from the file input
740 * and make them into a list of value_pairs
744 VALUE_PAIR *rc_avpair_readin(FILE *input)
746 VALUE_PAIR *vp = NULL;
747 char buffer[1024], *q;
749 while (fgets(buffer, sizeof(buffer), input) != NULL)
753 while(*q && isspace(*q)) q++;
755 if ((*q == '\n') || (*q == '#') || (*q == '\0'))
758 if (rc_avpair_parse(q, &vp) < 0) {
759 rc_log(LOG_ERR, "rc_avpair_readin: malformed attribute: %s", buffer);