2019-08-09 11:42:19 +00:00
|
|
|
|
#include "../include/parsefile.h"
|
|
|
|
|
#include "../include/configm.h"
|
|
|
|
|
#include "../../../netlink_uapi/libnetlinku.h"
|
|
|
|
|
#include "authfree.h"
|
|
|
|
|
#include <cjson/cJSON.h>
|
2019-08-15 06:24:41 +00:00
|
|
|
|
#include "s2j/s2j.h"
|
|
|
|
|
#include "commuapinl.h"
|
2019-08-09 11:42:19 +00:00
|
|
|
|
#include "auth_parameters.h"
|
2019-08-15 06:24:41 +00:00
|
|
|
|
#include "rpc.h"
|
|
|
|
|
#include "../Platform/common/database/database.h"
|
|
|
|
|
#include "include/user_authpara.h"
|
|
|
|
|
#include "config_manager.h"
|
2019-08-19 08:18:47 +00:00
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include "stdlib.h"
|
|
|
|
|
#include "redisMq.h"
|
2019-08-15 06:24:41 +00:00
|
|
|
|
|
2019-08-09 11:42:19 +00:00
|
|
|
|
|
|
|
|
|
/*定义结构体 存认证参数*/
|
|
|
|
|
auth_parameters_t *auth_para;
|
|
|
|
|
|
2019-08-19 08:18:47 +00:00
|
|
|
|
#define LOCAL_PORTALSERVER_PORT "local_portalserver_port"
|
|
|
|
|
|
|
|
|
|
|
2019-08-09 11:42:19 +00:00
|
|
|
|
#ifdef AGINGTIME_ACK_COOKIES
|
|
|
|
|
#define CFG_AGINGTIME_ACK_COOKIES
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*全局变量初始化 失败为1 成功为0*/
|
|
|
|
|
int authparInit()
|
|
|
|
|
{
|
|
|
|
|
auth_para = (auth_parameters_t *)malloc(sizeof(auth_parameters_t));
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
if(NULL == auth_para) {
|
2019-08-09 11:42:19 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 01:23:42 +00:00
|
|
|
|
/*下发用户老化时间配置到内核态 */
|
2019-08-09 11:42:19 +00:00
|
|
|
|
int set_agingtimecfg_waitack(int *agingtime)
|
|
|
|
|
{
|
|
|
|
|
int agingtime_len = 0;
|
|
|
|
|
struct nlmsghdr *ack = NULL;
|
|
|
|
|
struct nlmsghdr **answer = &ack;
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
struct {
|
2019-08-09 11:42:19 +00:00
|
|
|
|
struct nlmsghdr n;
|
|
|
|
|
char buf[1024];
|
2019-08-20 01:23:42 +00:00
|
|
|
|
} req = {
|
2019-08-09 11:42:19 +00:00
|
|
|
|
.n.nlmsg_len = NLMSG_LENGTH(0),
|
|
|
|
|
#ifdef CFG_AGINGTIME_ACK_COOKIES
|
|
|
|
|
.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,/*set NLM_F_ACK:use kernel auto ack*/
|
|
|
|
|
#else
|
|
|
|
|
.n.nlmsg_flags = NLM_F_REQUEST, /*not use kernel auto ack */
|
|
|
|
|
#endif
|
|
|
|
|
.n.nlmsg_type = AGINGTIME_CFG, /*用户态发送给内核态的用户老化时间消息 */
|
|
|
|
|
.n.nlmsg_pid = getpid(),
|
2019-08-20 01:23:42 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*判断要发送的数据是否为NULL,不为NULL,打印出来 */
|
|
|
|
|
if(agingtime == NULL) {
|
|
|
|
|
printf("set_agingtimecfg_waitack is error: input struct_agingtime is NULL.\r\n");
|
|
|
|
|
return -1;
|
|
|
|
|
} else {
|
|
|
|
|
printf("set_freeauthcfg_waitack :agingtime %d\n", *agingtime);
|
|
|
|
|
}
|
2019-08-09 11:42:19 +00:00
|
|
|
|
|
|
|
|
|
/*计算需要发送的数据的长度 */
|
|
|
|
|
agingtime_len = sizeof(int);
|
|
|
|
|
printf("%d\n", agingtime_len);
|
|
|
|
|
|
|
|
|
|
/*可选属性 */
|
|
|
|
|
commnl_addattr_l(&req.n, sizeof(req), 1, agingtime, agingtime_len);
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
2019-08-09 11:42:19 +00:00
|
|
|
|
/*发送组装好的netlink消息 */
|
2019-08-20 01:23:42 +00:00
|
|
|
|
if(pdeliv_talk(1, &req.n, answer) < 0) {
|
2019-08-09 11:42:19 +00:00
|
|
|
|
printf("set_user_agingtime_waitack rcv ack msg faild.\r\n");
|
|
|
|
|
return -2;
|
2019-08-20 01:23:42 +00:00
|
|
|
|
} else {
|
2019-08-09 11:42:19 +00:00
|
|
|
|
printf("set_user_agingtime_waitack rcv ack msg success.\r\n");
|
|
|
|
|
}
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
if(*answer != NULL) {
|
|
|
|
|
printf("set_user_agingtime_waitack rcv answer.\r\n");
|
|
|
|
|
} else {
|
|
|
|
|
printf("set_user_agingtime_waitack rcv answer error.\r\n");
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
2019-08-09 11:42:19 +00:00
|
|
|
|
|
|
|
|
|
#ifdef CFG_AGINGTIME_ACK_COOKIES
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
/*recv answer*/
|
|
|
|
|
if((*answer)->nlmsg_type == NLMSG_ERROR) {
|
|
|
|
|
nl_debugfs_extack(*answer);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-09 11:42:19 +00:00
|
|
|
|
#else
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
/*recv answer*/
|
|
|
|
|
if((*answer)->nlmsg_type == AGINGTIME_CFG) {
|
|
|
|
|
nl_debugfs(*answer);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-09 11:42:19 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2019-08-20 01:23:42 +00:00
|
|
|
|
return 0;
|
2019-08-09 11:42:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*检查IP地址是否有效,端口号是否被占用 */
|
2019-08-15 06:24:41 +00:00
|
|
|
|
int _valid_port(int port)
|
2019-08-09 11:42:19 +00:00
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
int fd;
|
|
|
|
|
int i;
|
|
|
|
|
volatile int local_errno;
|
|
|
|
|
struct sockaddr_in addr;
|
2019-08-20 01:23:42 +00:00
|
|
|
|
fd = socket(AF_INET, SOCK_STREAM, 0); /*初始化*/
|
2019-08-09 11:42:19 +00:00
|
|
|
|
|
2019-08-20 01:23:42 +00:00
|
|
|
|
if(fd == -1) { /*检查是否正常初始化socket */
|
2019-08-09 11:42:19 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 06:24:41 +00:00
|
|
|
|
addr.sin_family = AF_INET; /*地址结构的协议簇 */
|
2019-08-20 01:23:42 +00:00
|
|
|
|
addr.sin_port = htons(port); /*地址结构的端口地址,网络字节序 */
|
|
|
|
|
|
|
|
|
|
i = (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)));
|
2019-08-15 06:24:41 +00:00
|
|
|
|
printf("the value of i:%d\n", i);
|
2019-08-09 11:42:19 +00:00
|
|
|
|
|
2019-08-20 01:23:42 +00:00
|
|
|
|
if(i < 0) {
|
2019-08-15 06:24:41 +00:00
|
|
|
|
printf("port %d has been used. \n", port);
|
2019-08-09 11:42:19 +00:00
|
|
|
|
close(fd);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2019-08-15 06:24:41 +00:00
|
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
return 0;
|
|
|
|
|
|
2019-08-09 11:42:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 06:24:41 +00:00
|
|
|
|
/* iuput格式:{"type": 0, "data": {"port": 1010,"timehorizon": 10,"failcount": 20,"locktime":30, "aging_time":10}}*/
|
2019-08-09 11:42:19 +00:00
|
|
|
|
ret_code authpara_config_json_parse(pointer input, uint *conf_type, auth_parameters_t *authpara_buff)
|
|
|
|
|
{
|
2019-08-20 01:23:42 +00:00
|
|
|
|
char *pString = (char *)input;
|
2019-08-09 11:42:19 +00:00
|
|
|
|
ret_code ret = RET_OK;
|
|
|
|
|
cJSON *cjson, *type, *data;
|
|
|
|
|
|
2019-08-15 06:24:41 +00:00
|
|
|
|
printf("json:[%s]\n", pString);
|
2019-08-09 11:42:19 +00:00
|
|
|
|
/*JSON字符串到JSON格式 */
|
2019-08-15 06:24:41 +00:00
|
|
|
|
cjson = cJSON_Parse(pString);
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
if(!cjson) {
|
2019-08-09 11:42:19 +00:00
|
|
|
|
ret = RET_INPUTERR;
|
|
|
|
|
ASSERT_RET(ret);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
2019-08-09 11:42:19 +00:00
|
|
|
|
/*获取操作类型 add、mod、del */
|
|
|
|
|
type = cJSON_GetObjectItem(cjson, "type");
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
if(!type) {
|
2019-08-09 11:42:19 +00:00
|
|
|
|
ret = RET_INPUTERR;
|
|
|
|
|
cJSON_Delete(cjson);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*conf_type = type->valueint;
|
|
|
|
|
|
|
|
|
|
/*获取免认证规则的data部分 */
|
|
|
|
|
data = cJSON_GetObjectItem(cjson, "data");
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
if(!data) {
|
2019-08-09 11:42:19 +00:00
|
|
|
|
ret = RET_INPUTERR;
|
|
|
|
|
cJSON_Delete(cjson);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*创建freeauth_configure_t结构体对象 */
|
|
|
|
|
s2j_create_struct_obj(auth_parameters, auth_parameters_t);
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
if(auth_parameters == NULL) {
|
2019-08-09 11:42:19 +00:00
|
|
|
|
cJSON_Delete(cjson);
|
|
|
|
|
return RET_NOMEM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*反序列化数据到freeauth_configure_t结构体对象 */
|
|
|
|
|
s2j_struct_get_basic_element(auth_parameters, data, int, port);
|
|
|
|
|
s2j_struct_get_basic_element(auth_parameters, data, int, timehorizon);
|
|
|
|
|
s2j_struct_get_basic_element(auth_parameters, data, int, failcount);
|
|
|
|
|
s2j_struct_get_basic_element(auth_parameters, data, int, locktime);
|
|
|
|
|
s2j_struct_get_basic_element(auth_parameters, data, int, aging_time);
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
2019-08-09 11:42:19 +00:00
|
|
|
|
authpara_buff->port = auth_parameters->port;
|
|
|
|
|
authpara_buff->timehorizon = auth_parameters->timehorizon;
|
|
|
|
|
authpara_buff->failcount = auth_parameters->failcount;
|
|
|
|
|
authpara_buff->locktime = auth_parameters->locktime;
|
|
|
|
|
authpara_buff->aging_time = auth_parameters->aging_time;
|
|
|
|
|
|
2019-08-15 06:24:41 +00:00
|
|
|
|
//s2j_delete_struct_obj(auth_parameters);
|
2019-08-09 11:42:19 +00:00
|
|
|
|
cJSON_Delete(cjson);
|
|
|
|
|
|
|
|
|
|
return RET_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-19 08:18:47 +00:00
|
|
|
|
/* 发布配置的本地Portal server 的port*/
|
|
|
|
|
void local_portal_port(char *port)
|
|
|
|
|
{
|
2019-08-20 01:23:42 +00:00
|
|
|
|
printf("port of local portal server:%s\n", port);
|
|
|
|
|
bool ret = redisPubInit();
|
|
|
|
|
|
|
|
|
|
if(!ret) {
|
2019-08-19 08:18:47 +00:00
|
|
|
|
printf("Init failed.\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
2019-08-19 08:18:47 +00:00
|
|
|
|
ret = redisPubConnect();
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
if(!ret) {
|
2019-08-19 08:18:47 +00:00
|
|
|
|
printf("connect failed.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 01:23:42 +00:00
|
|
|
|
redisPublish(LOCAL_PORTALSERVER_PORT, port);
|
2019-08-19 08:18:47 +00:00
|
|
|
|
|
2019-08-20 01:23:42 +00:00
|
|
|
|
redisPubDisconnect();
|
|
|
|
|
redisPubUninit();
|
|
|
|
|
return;
|
2019-08-19 08:18:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-15 06:24:41 +00:00
|
|
|
|
ret_code authpara_config_chk(uint source, uint *config_type,
|
2019-08-20 01:23:42 +00:00
|
|
|
|
pointer input, int *input_len,
|
|
|
|
|
pointer output, int *output_len)
|
|
|
|
|
{
|
2019-08-09 11:42:19 +00:00
|
|
|
|
ret_code ret = RET_OK;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
2019-08-15 06:24:41 +00:00
|
|
|
|
ret_code authpara_config_proc(uint source, uint config_type,
|
2019-08-20 01:23:42 +00:00
|
|
|
|
pointer input, int input_len,
|
|
|
|
|
pointer output, int *output_len)
|
2019-08-09 11:42:19 +00:00
|
|
|
|
{
|
|
|
|
|
ret_code ret = RET_OK;
|
2019-08-15 06:24:41 +00:00
|
|
|
|
auth_parameters_t auth_parameters = {0};
|
|
|
|
|
int config_len = sizeof(auth_parameters_t);
|
|
|
|
|
uint conf_type = AUTHPARA_CONFIG_MOD;
|
|
|
|
|
int code = 0;
|
2019-08-09 11:42:19 +00:00
|
|
|
|
cJSON *res;
|
2019-08-20 01:23:42 +00:00
|
|
|
|
char *ret_char = NULL;
|
2019-08-09 11:42:19 +00:00
|
|
|
|
unsigned int ret_int = 0;
|
2019-08-15 06:24:41 +00:00
|
|
|
|
configure_result_t *configure_result;
|
2019-08-09 11:42:19 +00:00
|
|
|
|
int r = -1;
|
2019-08-15 06:24:41 +00:00
|
|
|
|
int portresult = 0;
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
2019-08-15 06:24:41 +00:00
|
|
|
|
authpara_config_json_parse(input, &conf_type, &auth_parameters);
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
if((input_len < sizeof(auth_parameters_t)) || (input_len > sizeof(auth_parameters_t))) {
|
2019-08-15 06:24:41 +00:00
|
|
|
|
ret = RET_INPUTERR;
|
2019-08-09 11:42:19 +00:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
2019-08-19 08:18:47 +00:00
|
|
|
|
|
2019-08-15 06:24:41 +00:00
|
|
|
|
portresult = _valid_port(auth_parameters.port);
|
2019-08-19 08:18:47 +00:00
|
|
|
|
printf("portresult:%d\n", portresult);
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
if(portresult == 1) {
|
2019-08-15 06:24:41 +00:00
|
|
|
|
return RET_CHKERR;
|
2019-08-09 11:42:19 +00:00
|
|
|
|
}
|
2019-08-15 06:24:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*数据库修改 存入全局变量*/
|
|
|
|
|
configure_result = (configure_result_t *)malloc(sizeof(configure_result_t));
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
if(NULL == configure_result) {
|
2019-08-09 11:42:19 +00:00
|
|
|
|
return RET_NOMEM;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 06:24:41 +00:00
|
|
|
|
mod_authpara(auth_parameters.port, auth_parameters.timehorizon, auth_parameters.failcount,
|
2019-08-20 01:23:42 +00:00
|
|
|
|
auth_parameters.locktime, auth_parameters.aging_time, configure_result);
|
2019-08-09 11:42:19 +00:00
|
|
|
|
|
2019-08-15 06:24:41 +00:00
|
|
|
|
/*共享内存 传送用户态和内核态之间的配置信息*/
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
|
2019-08-15 06:24:41 +00:00
|
|
|
|
/*存数据库成功,则下发到内核态auth_hook*/
|
2019-08-20 01:23:42 +00:00
|
|
|
|
if(0 == configure_result->resultcode) {
|
2019-08-15 06:24:41 +00:00
|
|
|
|
/*用户态下发到内核态auth_hook */
|
|
|
|
|
printf("cfgchannel main begin:\r\n");
|
2019-08-09 11:42:19 +00:00
|
|
|
|
|
2019-08-15 06:24:41 +00:00
|
|
|
|
/*创建通道 */
|
|
|
|
|
r = commcfgnl_open();
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
if(r < 0) {
|
2019-08-15 06:24:41 +00:00
|
|
|
|
printf(" pdlivnl_open fail, exit.\r\n");
|
|
|
|
|
return RET_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*下发配置到内核态 */
|
|
|
|
|
r = set_agingtimecfg_waitack(&(auth_parameters.aging_time));
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
if(r < 0) {
|
2019-08-15 06:24:41 +00:00
|
|
|
|
printf("set_cfg_debug_waitack failed.\r\n");
|
|
|
|
|
return RET_ERR;
|
|
|
|
|
}
|
2019-08-09 11:42:19 +00:00
|
|
|
|
|
2019-08-20 01:23:42 +00:00
|
|
|
|
/*关闭netlink通道 */
|
2019-08-15 06:24:41 +00:00
|
|
|
|
commcfgnl_close();
|
|
|
|
|
printf("cfgchannel main exit!\r\n");
|
2019-08-20 01:23:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
2019-08-09 11:42:19 +00:00
|
|
|
|
|
2019-08-19 08:18:47 +00:00
|
|
|
|
/*Portal server的port通过redis消息队列接口发布给web server*/
|
|
|
|
|
char auth_port[20];
|
2019-08-20 01:23:42 +00:00
|
|
|
|
sprintf(auth_port, "%d ", auth_parameters.port);
|
|
|
|
|
printf("The number 'port' is %d and the string 'port' is %s. \n", auth_parameters.port, auth_port);
|
2019-08-19 08:18:47 +00:00
|
|
|
|
local_portal_port(auth_port);
|
|
|
|
|
|
2019-08-09 11:42:19 +00:00
|
|
|
|
/*创建json对象 */
|
|
|
|
|
res = cJSON_CreateObject();
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
if(!res) {
|
2019-08-09 11:42:19 +00:00
|
|
|
|
ret = RET_ERR;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*将json对象转换成json字符串 返回处理结果*/
|
2019-08-20 01:23:42 +00:00
|
|
|
|
cJSON_AddNumberToObject(res, "resultcode", configure_result->resultcode);
|
2019-08-15 06:24:41 +00:00
|
|
|
|
cJSON_AddStringToObject(res, "message", configure_result->message);
|
2019-08-09 11:42:19 +00:00
|
|
|
|
ret_char = cJSON_PrintUnformatted(res);
|
|
|
|
|
ret_int = strlen(ret_char);
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
if(output_len) {
|
2019-08-09 11:42:19 +00:00
|
|
|
|
*output_len = ret_int;
|
|
|
|
|
}
|
2019-08-20 01:23:42 +00:00
|
|
|
|
|
2019-08-09 11:42:19 +00:00
|
|
|
|
/*超出2k的内存,报错 */
|
2019-08-20 01:23:42 +00:00
|
|
|
|
if(ret_int >= 1024 * 2) {
|
2019-08-09 11:42:19 +00:00
|
|
|
|
free(ret_char);
|
|
|
|
|
cJSON_Delete(res);
|
|
|
|
|
return RET_NOMEM;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 06:24:41 +00:00
|
|
|
|
memcpy(output, ret_char, ret_int + 1);
|
2019-08-09 11:42:19 +00:00
|
|
|
|
|
|
|
|
|
free(ret_char);
|
|
|
|
|
cJSON_Delete(res);
|
2019-08-20 01:23:42 +00:00
|
|
|
|
return RET_OK;
|
2019-08-09 11:42:19 +00:00
|
|
|
|
}
|