⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rlm_counter.c

📁 radius服务器
💻 C
📖 第 1 页 / 共 2 页
字号:
		return -1;	}	data->gdbm = gdbm_open(data->filename, sizeof(int),			GDBM_WRCREAT | GDBM_COUNTER_OPTS, 0600, NULL);	if (data->gdbm == NULL) {		radlog(L_ERR, "rlm_counter: Failed to open file %s: %s",				data->filename, strerror(errno));		counter_detach(data);		return -1;	}	if (gdbm_setopt(data->gdbm, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1)		radlog(L_ERR, "rlm_counter: Failed to set cache size");	/*	 * Look for the DEFAULT1 entry. This entry if it exists contains the	 * time of the next database reset. This time is set each time we reset	 * the database. If next_reset < now then we reset the database.	 * That way we can overcome the problem where radiusd is down during a database	 * reset time. If we did not keep state information in the database then the reset	 * would be extended and that would create problems.	 *	 * We also store the time of the last reset in the DEFAULT2 entry.	 *	 * If DEFAULT1 and DEFAULT2 do not exist (new database) we add them to the database	 */	key_datum.dptr = (char *)default1;	key_datum.dsize = strlen(default1);	time_datum = gdbm_fetch(data->gdbm, key_datum);	if (time_datum.dptr != NULL){		time_t next_reset = 0;		memcpy(&next_reset, time_datum.dptr, sizeof(time_t));		free(time_datum.dptr);		if (next_reset && next_reset <= now){			data->last_reset = now;			ret = reset_db(data);			if (ret != RLM_MODULE_OK){				radlog(L_ERR, "rlm_counter: reset_db() failed");				counter_detach(data);				return -1;			}		}		else			data->reset_time = next_reset;		key_datum.dptr = (char *)default2;		key_datum.dsize = strlen(default2);		time_datum = gdbm_fetch(data->gdbm, key_datum);		if (time_datum.dptr != NULL){			memcpy(&data->last_reset, time_datum.dptr, sizeof(time_t));			free(time_datum.dptr);		}	}	else{		ret = add_defaults(data);		if (ret != RLM_MODULE_OK){			radlog(L_ERR, "rlm_counter: add_defaults() failed");			counter_detach(data);			return -1;		}	}	/*	 *	Register the counter comparison operation.	 */	paircompare_register(data->dict_attr, 0, counter_cmp, data);	/*	 * Init the mutex	 */	pthread_mutex_init(&data->mutex, NULL);	*instance = data;	return 0;}/* *	Write accounting information to this modules database. */static int counter_accounting(void *instance, REQUEST *request){	rlm_counter_t *data = (rlm_counter_t *)instance;	datum key_datum;	datum count_datum;	VALUE_PAIR *key_vp, *count_vp, *proto_vp, *uniqueid_vp;	rad_counter counter;	int rcode;	int acctstatustype = 0;	time_t diff;	if ((key_vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) != NULL)		acctstatustype = key_vp->lvalue;	else {		DEBUG("rlm_counter: Could not find account status type in packet.");		return RLM_MODULE_NOOP;	}	if (acctstatustype != PW_STATUS_STOP){		DEBUG("rlm_counter: We only run on Accounting-Stop packets.");		return RLM_MODULE_NOOP;	}	uniqueid_vp = pairfind(request->packet->vps, PW_ACCT_UNIQUE_SESSION_ID);	if (uniqueid_vp != NULL)		DEBUG("rlm_counter: Packet Unique ID = '%s'",uniqueid_vp->strvalue);	/*	 *	Before doing anything else, see if we have to reset	 *	the counters.	 */	if (data->reset_time && (data->reset_time <= request->timestamp)) {		int ret;		DEBUG("rlm_counter: Time to reset the database.");		data->last_reset = data->reset_time;		find_next_reset(data,request->timestamp);		pthread_mutex_lock(&data->mutex);		ret = reset_db(data);		pthread_mutex_unlock(&data->mutex);		if (ret != RLM_MODULE_OK)			return ret;	}	/*	 * Check if we need to watch out for a specific service-type. If yes then check it	 */	if (data->service_type != NULL) {		if ((proto_vp = pairfind(request->packet->vps, PW_SERVICE_TYPE)) == NULL){			DEBUG("rlm_counter: Could not find Service-Type attribute in the request. Returning NOOP.");			return RLM_MODULE_NOOP;		}		if ((unsigned)proto_vp->lvalue != data->service_val){			DEBUG("rlm_counter: This Service-Type is not allowed. Returning NOOP.");			return RLM_MODULE_NOOP;		}	}	/*	 * Check if request->timestamp - {Acct-Delay-Time} < last_reset	 * If yes reject the packet since it is very old	 */	key_vp = pairfind(request->packet->vps, PW_ACCT_DELAY_TIME);	if (key_vp != NULL){		if (key_vp->lvalue != 0 &&		    (request->timestamp - key_vp->lvalue) < data->last_reset){			DEBUG("rlm_counter: This packet is too old. Returning NOOP.");			return RLM_MODULE_NOOP;		}	}	/*	 *	Look for the key.  User-Name is special.  It means	 *	The REAL username, after stripping.	 */	key_vp = (data->key_attr == PW_USER_NAME) ? request->username : pairfind(request->packet->vps, data->key_attr);	if (key_vp == NULL){		DEBUG("rlm_counter: Could not find the key-attribute in the request. Returning NOOP.");		return RLM_MODULE_NOOP;	}	/*	 *	Look for the attribute to use as a counter.	 */	count_vp = pairfind(request->packet->vps, data->count_attr);	if (count_vp == NULL){		DEBUG("rlm_counter: Could not find the count-attribute in the request.");		return RLM_MODULE_NOOP;	}	key_datum.dptr = key_vp->strvalue;	key_datum.dsize = key_vp->length;	DEBUG("rlm_counter: Searching the database for key '%s'",key_vp->strvalue);	pthread_mutex_lock(&data->mutex);	count_datum = gdbm_fetch(data->gdbm, key_datum);	pthread_mutex_unlock(&data->mutex);	if (count_datum.dptr == NULL){		DEBUG("rlm_counter: Could not find the requested key in the database.");		counter.user_counter = 0;		if (uniqueid_vp != NULL)			strncpy(counter.uniqueid,uniqueid_vp->strvalue,UNIQUEID_MAX_LEN - 1);		else			memset((char *)counter.uniqueid,0,UNIQUEID_MAX_LEN);	}	else{		DEBUG("rlm_counter: Key found.");		memcpy(&counter, count_datum.dptr, sizeof(rad_counter));		free(count_datum.dptr);		if (counter.uniqueid)			DEBUG("rlm_counter: Counter Unique ID = '%s'",counter.uniqueid);		if (uniqueid_vp != NULL){			if (counter.uniqueid != NULL &&				strncmp(uniqueid_vp->strvalue,counter.uniqueid, UNIQUEID_MAX_LEN - 1) == 0){				DEBUG("rlm_counter: Unique IDs for user match. Droping the request.");				return RLM_MODULE_NOOP;			}			strncpy(counter.uniqueid,uniqueid_vp->strvalue,UNIQUEID_MAX_LEN - 1);		}		DEBUG("rlm_counter: User=%s, Counter=%d.",request->username->strvalue,counter.user_counter);	}	if (data->count_attr == PW_ACCT_SESSION_TIME) {		/*		 *	If session time < diff then the user got in after the		 *	last reset. So add his session time, otherwise add the		 *	diff.		 *		 *	That way if he logged in at 23:00 and we reset the		 *	daily counter at 24:00 and he logged out at 01:00		 *	then we will only count one hour (the one in the new		 *	day). That is the right thing		 */		diff = request->timestamp - data->last_reset;		counter.user_counter += (count_vp->lvalue < diff) ? count_vp->lvalue : diff;	} else if (count_vp->type == PW_TYPE_INTEGER) {		/*		 *	Integers get counted, without worrying about		 *	reset dates.		 */		counter.user_counter += count_vp->lvalue;	} else {		/*		 *	The attribute is NOT an integer, just count once		 *	more that we've seen it.		 */		counter.user_counter++;	}	DEBUG("rlm_counter: User=%s, New Counter=%d.",request->username->strvalue,counter.user_counter);	count_datum.dptr = (char *) &counter;	count_datum.dsize = sizeof(rad_counter);	DEBUG("rlm_counter: Storing new value in database.");	pthread_mutex_lock(&data->mutex);	rcode = gdbm_store(data->gdbm, key_datum, count_datum, GDBM_REPLACE);	pthread_mutex_unlock(&data->mutex);	if (rcode < 0) {		radlog(L_ERR, "rlm_counter: Failed storing data to %s: %s",				data->filename, gdbm_strerror(gdbm_errno));		return RLM_MODULE_FAIL;	}	DEBUG("rlm_counter: New value stored successfully.");	return RLM_MODULE_OK;}/* *	Find the named user in this modules database.  Create the set *	of attribute-value pairs to check and reply with for this user *	from the database. The authentication code only needs to check *	the password, the rest is done here. */static int counter_authorize(void *instance, REQUEST *request){	rlm_counter_t *data = (rlm_counter_t *) instance;	int ret=RLM_MODULE_NOOP;	datum key_datum;	datum count_datum;	rad_counter counter;	int res=0;	VALUE_PAIR *key_vp, *check_vp;	VALUE_PAIR *reply_item;	char msg[128];	/* quiet the compiler */	instance = instance;	request = request;	/*	 *	Before doing anything else, see if we have to reset	 *	the counters.	 */	if (data->reset_time && (data->reset_time <= request->timestamp)) {		int ret2;		data->last_reset = data->reset_time;		find_next_reset(data,request->timestamp);		pthread_mutex_lock(&data->mutex);		ret2 = reset_db(data);		pthread_mutex_unlock(&data->mutex);		if (ret2 != RLM_MODULE_OK)			return ret2;	}	/*	 *      Look for the key.  User-Name is special.  It means	 *      The REAL username, after stripping.	 */	DEBUG2("rlm_counter: Entering module authorize code");	key_vp = (data->key_attr == PW_USER_NAME) ? request->username : pairfind(request->packet->vps, data->key_attr);	if (key_vp == NULL) {		DEBUG2("rlm_counter: Could not find Key value pair");		return ret;	}	/*	 *      Look for the check item	 */	if ((check_vp= pairfind(request->config_items, data->check_attr)) == NULL) {		DEBUG2("rlm_counter: Could not find Check item value pair");		return ret;	}	key_datum.dptr = key_vp->strvalue;	key_datum.dsize = key_vp->length;	/*	 * Init to be sure	 */	counter.user_counter = 0;	DEBUG("rlm_counter: Searching the database for key '%s'",key_vp->strvalue);	pthread_mutex_lock(&data->mutex);	count_datum = gdbm_fetch(data->gdbm, key_datum);	pthread_mutex_unlock(&data->mutex);	if (count_datum.dptr != NULL){		DEBUG("rlm_counter: Key Found.");		memcpy(&counter, count_datum.dptr, sizeof(rad_counter));		free(count_datum.dptr);	}	else		DEBUG("rlm_counter: Could not find the requested key in the database.");	/*	 * Check if check item > counter	 */	DEBUG("rlm_counter: Check item = %d, Count = %d",check_vp->lvalue,counter.user_counter);	res=check_vp->lvalue - counter.user_counter;	if (res > 0) {		DEBUG("rlm_counter: res is greater than zero");		if (data->count_attr == PW_ACCT_SESSION_TIME) {			/*			 * Do the following only if the count attribute is			 * AcctSessionTime			 */			/*		 	*	We are assuming that simultaneous-use=1. But		 	*	even if that does not happen then our user		 	*	could login at max for 2*max-usage-time Is		 	*	that acceptable?		 	*/			/*		 	*	User is allowed, but set Session-Timeout.		 	*	Stolen from main/auth.c		 	*/			/*		 	*	If we are near a reset then add the next		 	*	limit, so that the user will not need to		 	*	login again		 	*/			if (data->reset_time && (				res >= (data->reset_time - request->timestamp))) {				res += check_vp->lvalue;			}			if ((reply_item = pairfind(request->reply->vps, PW_SESSION_TIMEOUT)) != NULL) {				if (reply_item->lvalue > res)					reply_item->lvalue = res;			} else {				if ((reply_item = paircreate(PW_SESSION_TIMEOUT, PW_TYPE_INTEGER)) == NULL) {					radlog(L_ERR|L_CONS, "no memory");					return RLM_MODULE_NOOP;				}				reply_item->lvalue = res;				pairadd(&request->reply->vps, reply_item);			}		}		ret=RLM_MODULE_OK;		DEBUG2("rlm_counter: (Check item - counter) is greater than zero");		DEBUG2("rlm_counter: Authorized user %s, check_item=%d, counter=%d",				key_vp->strvalue,check_vp->lvalue,counter.user_counter);		DEBUG2("rlm_counter: Sent Reply-Item for user %s, Type=Session-Timeout, value=%d",				key_vp->strvalue,res);	}	else{		char module_fmsg[MAX_STRING_LEN];		VALUE_PAIR *module_fmsg_vp;		/*		 * User is denied access, send back a reply message		*/		sprintf(msg, "Your maximum %s usage time has been reached", data->reset);		reply_item=pairmake("Reply-Message", msg, T_OP_EQ);		pairadd(&request->reply->vps, reply_item);		snprintf(module_fmsg,sizeof(module_fmsg), "rlm_counter: Maximum %s usage time reached", data->reset);		module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);		pairadd(&request->packet->vps, module_fmsg_vp);		ret=RLM_MODULE_REJECT;		DEBUG2("rlm_counter: Rejected user %s, check_item=%d, counter=%d",				key_vp->strvalue,check_vp->lvalue,counter.user_counter);	}	return ret;}static int counter_detach(void *instance){	rlm_counter_t *data = (rlm_counter_t *) instance;	paircompare_unregister(data->dict_attr, counter_cmp);	if (data->gdbm)		gdbm_close(data->gdbm);	free(data->filename);	free(data->reset);	free(data->key_name);	free(data->count_attribute);	free(data->counter_name);	free(data->check_name);	free(data->service_type);	pthread_mutex_destroy(&data->mutex);	free(instance);	return 0;}/* *	The module name should be the only globally exported symbol. *	That is, everything else should be 'static'. * *	If the module needs to temporarily modify it's instantiation *	data, the type should be changed to RLM_TYPE_THREAD_UNSAFE. *	The server will then take care of ensuring that the module *	is single-threaded. */module_t rlm_counter = {	"Counter",	RLM_TYPE_THREAD_SAFE,		/* type */	NULL,				/* initialization */	counter_instantiate,		/* instantiation */	{		NULL,			/* authentication */		counter_authorize, 	/* authorization */		NULL,			/* preaccounting */		counter_accounting,	/* accounting */		NULL,			/* checksimul */		NULL,			/* pre-proxy */		NULL,			/* post-proxy */		NULL			/* post-auth */	},	counter_detach,			/* detach */	NULL,				/* destroy */};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -