www.pudn.com > RadiusSrv.rar > valuepair.cpp, change:2003-10-22,size:6626b


#include "libRadius.h" 
#include "radius.h" 
 
/* 
 *	Delete the pair(s) with the matching attribute 
 */ 
void pairdelete(VALUE_PAIR **first, int attr) 
{ 
	VALUE_PAIR *i, *next; 
	VALUE_PAIR **last = first; 
 
	for(i = *first; i; i = next) { 
		next = i->next; 
		if (i->attribute == attr) { 
			*last = next; 
			free(i); 
		} else { 
			last = &i->next; 
		} 
	} 
} 
 
/* 
 *	Find the pair with the matching attribute 
 */ 
VALUE_PAIR * pairfind(VALUE_PAIR *first, int attr) 
{ 
	while(first && first->attribute != attr) 
		first = first->next; 
	return first; 
} 
 
/* 
 *	Release the memory used by a list of attribute-value 
 *	pairs, and sets the pair pointer to NULL. 
 */ 
void pairfree(VALUE_PAIR **pair_ptr) 
{ 
	VALUE_PAIR	*next, *pair; 
 
	if (!pair_ptr) return; 
	pair = *pair_ptr; 
 
	while (pair != NULL) { 
		next = pair->next; 
		free(pair); 
		pair = next; 
	} 
 
	*pair_ptr = NULL; 
} 
/* 
 *	Add a pair at the end of a VALUE_PAIR list. 
 */ 
void pairadd(VALUE_PAIR **first, VALUE_PAIR *add) 
{ 
	VALUE_PAIR *i; 
 
	if (*first == NULL) { 
		*first = add; 
		return; 
	} 
	for(i = *first; i->next; i = i->next) 
		; 
	i->next = add; 
} 
 
/* 
 *	Copy just a certain type of pairs. 
 */ 
VALUE_PAIR *paircopy2(VALUE_PAIR *vp, int attr) 
{ 
	VALUE_PAIR	*first, *n, **last; 
 
	first = NULL; 
	last = &first; 
 
	while (vp) { 
		if (attr >= 0 && vp->attribute != attr) { 
			vp = vp->next; 
			continue; 
		} 
		if ((n = (VALUE_PAIR *)malloc(sizeof(VALUE_PAIR))) == NULL) { 
			printf("out of memory"); 
			return first; 
		} 
		memcpy(n, vp, sizeof(VALUE_PAIR)); 
		n->next = NULL; 
		*last = n; 
		last = &n->next; 
		vp = vp->next; 
	} 
	return first; 
} 
 
/* 
 *	Move one kind of attributes from one list to the other 
 */ 
void pairmove2(VALUE_PAIR **to, VALUE_PAIR **from, int attr) 
{ 
	VALUE_PAIR *to_tail, *i, *next; 
	VALUE_PAIR *iprev = NULL; 
 
	/* 
	 *	Find the last pair in the "to" list and put it in "to_tail". 
	 */ 
	if (*to != NULL) { 
		to_tail = *to; 
		for(i = *to; i; i = i->next) 
			to_tail = i; 
	} else 
		to_tail = NULL; 
 
	for(i = *from; i; i = next) { 
		next = i->next; 
 
 
		/* 
		 *	If the attribute to move is NOT a VSA, then it 
		 *	ignores any attributes which do not match exactly. 
		 */ 
		if ((attr != PW_VENDOR_SPECIFIC) && 
		    (i->attribute != attr)) { 
			iprev = i; 
			continue; 
		} 
 
		/* 
		 *	If the attribute to move IS a VSA, then it ignores 
		 *	any non-VSA attribute. 
		 */ 
		if ((attr == PW_VENDOR_SPECIFIC) && 
		    (VENDOR(i->attribute) == 0)) { 
			iprev = i; 
			continue; 
		} 
 
		/* 
		 *	Remove the attribute from the "from" list. 
		 */ 
		if (iprev) 
			iprev->next = next; 
		else 
			*from = next; 
 
		/* 
		 *	Add the attribute to the "to" list. 
		 */ 
		if (to_tail) 
			to_tail->next = i; 
		else 
			*to = i; 
		to_tail = i; 
		i->next = NULL; 
	} 
} 
 
/* 
 *	Create a VALUE_PAIR from an ASCII attribute and value. 
 */ 
VALUE_PAIR *pairmake(const char *attribute, const char *value, int oper) 
{ 
	DICT_ATTR	*da; 
	VALUE_PAIR	*vp; 
	char            *tc, *ts; 
	signed char     tag; 
	int             found_tag; 
#ifdef HAVE_REGEX_H 
	int		res; 
	regex_t		cre; 
#endif 
 
	/* 
	 *    Check for tags in 'Attribute:Tag' format. 
	 */ 
	found_tag = 0; 
	tag = 0; 
 
	ts = strrchr( attribute, ':' ); 
	if (ts && ts[1]) { 
	         /* Colon found with something behind it */ 
	         if (ts[1] == '*' && ts[2] == 0) { 
		         /* Wildcard tag for check items */ 
		         tag = TAG_ANY; 
			 *ts = 0; 
		 } else if ((ts[1] >= '0') && (ts[1] <= '9')) { 
		         /* It's not a wild card tag */ 
		         tag = strtol(ts + 1, &tc, 0); 
			 if (tc && !*tc && TAG_VALID_ZERO(tag)) 
				 *ts = 0; 
			 else tag = 0; 
		 } else { 
			 printf("Invalid tag for attribute %s", attribute); 
			 return NULL; 
		 } 
		 found_tag = 1; 
	} 
 
	if ((da = dict_attrbyname(attribute)) == NULL) { 
		printf("Unknown attribute %s", attribute); 
		return NULL; 
	} 
 
	if ((vp = (VALUE_PAIR *)malloc(sizeof(VALUE_PAIR))) == NULL) { 
		printf("out of memory"); 
		return NULL; 
	} 
 
	memset(vp, 0, sizeof(VALUE_PAIR)); 
	vp->attribute = da->attr; 
	vp->type = da->type; 
	//vp->oper = (oper == 0) ? T_OP_EQ : oper; 
	strcpy(vp->name, da->name); 
	vp->flags = da->flags; 
	vp->next = NULL; 
 
	/*      Check for a tag in the 'Merit' format of: 
	 *      :Tag:Value.  Print an error if we already found 
	 *      a tag in the Attribute. 
	 */ 
 
	if (value && (*value == ':' && da->flags.has_tag)) { 
	        /* If we already found a tag, this is invalid */ 
	        if(found_tag) { 
		        free(vp); 
			printf("Duplicate tag %s for attribute %s", 
				   value, vp->name); 
			//("Duplicate tag %s for attribute %s\n", 
			///	   value, vp->name); 
			return NULL; 
 
		} 
	        /* Colon found and attribute allows a tag */ 
	        if (value[1] == '*' && value[2] == ':') { 
		       /* Wildcard tag for check items */ 
		       tag = TAG_ANY; 
		       value += 3; 
		} else { 
	               /* Real tag */ 
		       tag = strtol(value + 1, &tc, 0); 
		       if (tc && *tc==':' && TAG_VALID_ZERO(tag)) 
			    value = tc + 1; 
		       else tag = 0; 
		} 
		found_tag = 1; 
	}	 
	 
	if (found_tag) { 
	  vp->flags.tag = tag; 
	} 
 
	switch (vp->oper) { 
	default: 
		break; 
 
		/* 
		 *      For =* and !* opers, the value is irrelevant 
		 *      so we return now. 
		 */ 
	case T_OP_CMP_TRUE: 
	case T_OP_CMP_FALSE: 
		vp->strvalue[0] = '\0'; 
		vp->length = 0; 
	        return vp; 
		break; 
 
		/* 
		 *	Regular expression comparison of integer attributes 
		 *	does a STRING comparison of the names of their 
		 *	integer attributes. 
		 */ 
	case T_OP_REG_EQ:	/* =~ */ 
	case T_OP_REG_NE:	/* !~ */ 
		if (vp->type == PW_TYPE_INTEGER) { 
			return vp; 
		} 
#ifdef HAVE_REGEX_H 
		/* 
		 *	Regular expression match with no regular 
		 *	expression is wrong. 
		 */ 
		if (!value) { 
			pairfree(&vp); 
			return NULL; 
		} 
 
		res = regcomp(&cre, value, REG_EXTENDED|REG_NOSUB); 
		if (res != 0) { 
			char	msg[128]; 
 
			regerror(res, &cre, msg, sizeof(msg));                
			printf("Illegal regular expression in attribute: %s: %s", 
				vp->name, msg); 
			free(vp); 
			return NULL; 
		} 
		regfree(&cre); 
#else 
		printf("Regelar expressions not enabled in this build, error in attribute %s", 
				vp->name); 
		free(vp); 
		return NULL; 
#endif 
	} 
 
	if (value && (pairparsevalue(vp, value) == NULL)) { 
		free(vp); 
		return NULL; 
	} 
 
	return vp; 
} 
 
/* 
 *  Parse a string value into a given VALUE_PAIR 
 */ 
VALUE_PAIR *pairparsevalue(VALUE_PAIR *vp, const char *value) 
{ 
 
	return vp; 
}