/** Adds an attribute-value pair to the given list * * @note Always appends the new pair to the end of the list. * * @param rh a handle to parsed configuration. * @param list a #VALUE_PAIR array of values; initially must be %NULL. * @param attrid The attribute of the pair to add (e.g., %PW_USER_NAME). * @param pval the value (e.g., the actual username). * @param len the length of @pval, or -1 if to calculate (in case of strings). * @param vendorpec The vendor ID in case of a vendor specific value - 0 otherwise. * @return pointer to added a/v pair upon success, NULL pointer upon failure. */ VALUE_PAIR *rc_avpair_add (rc_handle const *rh, VALUE_PAIR **list, int attrid, void const *pval, int len, int vendorpec) { VALUE_PAIR *vp; vp = rc_avpair_new (rh, attrid, pval, len, vendorpec); if (vp != NULL) { rc_avpair_insert (list, NULL, vp); } return vp; }
VALUE_PAIR *rc_avpair_add (VALUE_PAIR **list, int attrid, void *pval, int len, int vendorcode) { VALUE_PAIR *vp; vp = rc_avpair_new (attrid, pval, len, vendorcode); if (vp != (VALUE_PAIR *) NULL) { rc_avpair_insert (list, (VALUE_PAIR *) NULL, vp); } return vp; }
/* * Function: rc_extract_vendor_specific_attributes * * Purpose: Extracts vendor-specific attributes, assuming they are in * the "SHOULD" format recommended by RCF 2138. * * Returns: found value_pair * */ static void rc_extract_vendor_specific_attributes(int attrlen, unsigned char *ptr, VALUE_PAIR **vp) { int vendor_id; int vtype; int vlen; UINT4 lvalue; DICT_ATTR *attr; VALUE_PAIR *pair; /* ptr is sitting at vendor-ID */ if (attrlen < 8) { /* Nothing to see here... */ return; } /* High-order octet of Vendor-Id must be zero (RFC2138) */ if (*ptr) { return; } /* Extract vendor_id */ vendor_id = (int) ( ((unsigned int) ptr[1]) * 256 * 256 + ((unsigned int) ptr[2]) * 256 + ((unsigned int) ptr[3])); /* Bump ptr up to contents */ ptr += 4; /* Set attrlen to length of data */ attrlen -= 4; for (; attrlen; attrlen -= vlen+2, ptr += vlen) { vtype = *ptr++; vlen = *ptr++; vlen -= 2; if (vlen < 0 || vlen > attrlen - 2) { /* Do not log an error. We are supposed to be able to cope with arbitrary vendor-specific gunk */ return; } /* Looks plausible... */ if ((attr = rc_dict_getattr(vtype, vendor_id)) == NULL) { continue; } /* TODO: Check that length matches data size!!!!! */ pair = (VALUE_PAIR *) malloc(sizeof(VALUE_PAIR)); if (!pair) { novm("rc_avpair_gen"); return; } strcpy(pair->name, attr->name); pair->attribute = attr->value; pair->vendorcode = vendor_id; pair->type = attr->type; pair->next = NULL; switch (attr->type) { case PW_TYPE_STRING: memcpy (pair->strvalue, (char *) ptr, (size_t) vlen); pair->strvalue[vlen] = '\0'; pair->lvalue = vlen; rc_avpair_insert (vp, (VALUE_PAIR *) NULL, pair); break; case PW_TYPE_INTEGER: case PW_TYPE_IPADDR: memcpy ((char *) &lvalue, (char *) ptr, sizeof (UINT4)); pair->lvalue = ntohl (lvalue); rc_avpair_insert (vp, (VALUE_PAIR *) NULL, pair); break; default: warn("rc_avpair_gen: %s has unknown type", attr->name); free (pair); break; } } }
VALUE_PAIR *rc_avpair_gen (AUTH_HDR *auth) { int length; int x_len; int attribute; int attrlen; UINT4 lvalue; unsigned char *x_ptr; unsigned char *ptr; DICT_ATTR *attr; VALUE_PAIR *vp; VALUE_PAIR *pair; unsigned char hex[3]; /* For hex string conversion. */ char buffer[512]; /* * Extract attribute-value pairs */ ptr = auth->data; length = ntohs ((unsigned short) auth->length) - AUTH_HDR_LEN; vp = (VALUE_PAIR *) NULL; while (length > 0) { attribute = *ptr++; attrlen = *ptr++; attrlen -= 2; if (attrlen < 0) { error("rc_avpair_gen: received attribute with invalid length"); break; } /* Handle vendor-specific specially */ if (attribute == PW_VENDOR_SPECIFIC) { rc_extract_vendor_specific_attributes(attrlen, ptr, &vp); ptr += attrlen; length -= (attrlen + 2); continue; } if ((attr = rc_dict_getattr (attribute, VENDOR_NONE)) == (DICT_ATTR *) NULL) { *buffer= '\0'; /* Initial length. */ for (x_ptr = ptr, x_len = attrlen ; x_len > 0 ; x_len--, x_ptr++) { sprintf ((char *)hex, "%2.2X", *x_ptr); strcat (buffer, (char *)hex); } warn("rc_avpair_gen: received unknown attribute %d of length %d: 0x%s", attribute, attrlen, buffer); } else { if ((pair = (VALUE_PAIR *) malloc (sizeof (VALUE_PAIR))) == (VALUE_PAIR *) NULL) { novm("rc_avpair_gen"); rc_avpair_free(vp); return NULL; } strcpy (pair->name, attr->name); pair->attribute = attr->value; pair->vendorcode = VENDOR_NONE; pair->type = attr->type; pair->next = (VALUE_PAIR *) NULL; switch (attr->type) { case PW_TYPE_STRING: memcpy (pair->strvalue, (char *) ptr, (size_t) attrlen); pair->strvalue[attrlen] = '\0'; pair->lvalue = attrlen; rc_avpair_insert (&vp, (VALUE_PAIR *) NULL, pair); break; case PW_TYPE_INTEGER: case PW_TYPE_IPADDR: memcpy ((char *) &lvalue, (char *) ptr, sizeof (UINT4)); pair->lvalue = ntohl (lvalue); rc_avpair_insert (&vp, (VALUE_PAIR *) NULL, pair); break; default: warn("rc_avpair_gen: %s has unknown type", attr->name); free (pair); break; } } ptr += attrlen; length -= attrlen + 2; } return (vp); }