Example #1
0
void acl_netdb_add_addr(ACL_DNS_DB *dns_db, const char *ip, int hport)
{
	const char *myname = "acl_netdb_add_addr";
	ACL_HOSTNAME *phost;
	char  buf[256];

	if (dns_db == NULL || dns_db->h_db == NULL || ip == NULL) {
		acl_msg_error("%s(%d): input invalid", myname, __LINE__);
		return;
	}

	phost = acl_mycalloc(1, sizeof(ACL_HOSTNAME));
	if (phost == NULL) {
		acl_msg_error("%s(%d): calloc error(%s)",
			myname, __LINE__, acl_last_strerror(buf, sizeof(buf)));
		return;
	}

	memset(&phost->saddr, 0, sizeof(phost->saddr));
	ACL_SAFE_STRNCPY(phost->ip, ip, sizeof(phost->ip));
	phost->saddr.sin_addr.s_addr = (unsigned long) inet_addr(ip);
	phost->hport = hport;

	if (acl_array_append(dns_db->h_db, phost) < 0) {
		acl_msg_error("%s(%d): array append error(%s)",
			myname, __LINE__, acl_last_strerror(buf, sizeof(buf)));
		return;
	}

	dns_db->size++;
}
Example #2
0
int acl_cfg_parser_append(ACL_CFG_PARSER *parser, ACL_CFG_LINE *cfg_line)
{
	char  myname[] = "acl_cfg_parser_append";
	char  tbuf[256];

	if (parser == NULL || cfg_line == NULL) {
		printf("%s: input error\n", myname);
		return (-1);
	}

	if (parser->_cfg_array == NULL) {
		parser->_cfg_array = acl_array_create(10);
		if (parser->_cfg_array == NULL) {
			printf("%s: can't create array, errmsg=%s",
					myname, acl_last_strerror(tbuf, sizeof(tbuf)));
			return (-1);
		}
		parser->total_line = 0;
		parser->valid_line = 0;
	}

	if (acl_array_append(parser->_cfg_array, (void *) cfg_line) < 0) {
		printf("%s: can't add ACL_CFG_LINE to array, errmsg=%s",
			myname, acl_last_strerror(tbuf, sizeof(tbuf)));
		return (-1);
	}
	parser->total_line++;
	if (cfg_line->pdata == NULL && cfg_line->value != NULL)
		parser->valid_line++;
	cfg_line->line_number = parser->total_line;

	return (0);
}
Example #3
0
ACL_DNS_DB *acl_netdb_clone(const ACL_DNS_DB *db)
{
	ACL_DNS_DB *dbp;
	ACL_HOSTNAME *phost, *h_host;
	int  i, n;

	if (db == NULL || db->h_db == NULL)
		return NULL;

	n = acl_array_size(db->h_db);
	if (n <= 0) {
		acl_msg_error("%s, %s(%d): h_db's size(%d) <= 0",
			__FILE__, __FUNCTION__, __LINE__, n);
		return NULL;
	}

	dbp = acl_netdb_new(db->name);

	for (i = 0; i < n; i++) {
		phost = (ACL_HOSTNAME *) acl_array_index(db->h_db, i);
		acl_assert(phost);

		h_host = acl_mycalloc(1, sizeof(ACL_HOSTNAME));
		memcpy(&h_host->saddr, &phost->saddr, sizeof(h_host->saddr));
		ACL_SAFE_STRNCPY(h_host->ip, phost->ip, sizeof(h_host->ip));
		h_host->hport = phost->hport;

		(void) acl_array_append(dbp->h_db, h_host);
		dbp->size++;
	}

	return dbp;
}
Example #4
0
void acl_netdb_add_addr(ACL_DNS_DB *db, const char *ip, int hport)
{
	ACL_HOSTNAME *phost;
	ACL_SOCKADDR  saddr;

	if (db == NULL || db->h_db == NULL || ip == NULL) {
		acl_msg_error("%s(%d): input invalid", __FUNCTION__, __LINE__);
		return;
	}

	memset(&saddr, 0, sizeof(saddr));

	if (ip2addr(ip, 0, &saddr) == 0) {
		acl_msg_error("%s(%d): invalid ip=%s",
			__FUNCTION__, __LINE__, ip);
		return;
	}

	phost = acl_mycalloc(1, sizeof(ACL_HOSTNAME));
	memcpy(&phost->saddr, &saddr, sizeof(phost->saddr));
	ACL_SAFE_STRNCPY(phost->ip, ip, sizeof(phost->ip));
	phost->hport = hport;

	(void) acl_array_append(db->h_db, phost);
	db->size++;
}
Example #5
0
ACL_DNS_DB *acl_netdb_clone(const ACL_DNS_DB *h_dns_db)
{
	const char *myname = "acl_netdb_clone";
	char  buf[256];
	ACL_DNS_DB *dns_db;
	ACL_HOSTNAME *phost, *h_host;
	int  i, n;

	if (h_dns_db == NULL || h_dns_db->h_db == NULL)
		return (NULL);

	n = acl_array_size(h_dns_db->h_db);
	if (n <= 0) {
		acl_msg_error("%s, %s(%d): h_db's size(%d) <= 0",
				__FILE__, myname, __LINE__, n);
		return (NULL);
	}

	dns_db = acl_netdb_new(h_dns_db->name);
	if (dns_db == NULL) {
		acl_msg_error("%s, %s(%d): acl_netdb_new error(%s)",
				__FILE__, myname, __LINE__,
				acl_last_strerror(buf, sizeof(buf)));
		return (NULL);
	}

	for (i = 0; i < n; i++) {
		phost = (ACL_HOSTNAME *) acl_array_index(h_dns_db->h_db, i);
		acl_assert(phost);

		h_host = acl_mycalloc(1, sizeof(ACL_HOSTNAME));
		if (h_host == NULL) {
			acl_msg_error("%s, %s(%d): calloc error(%s)",
					__FILE__, myname, __LINE__,
					acl_last_strerror(buf, sizeof(buf)));
			acl_netdb_free(dns_db);
			return (NULL);
		}

		h_host->saddr.sin_addr.s_addr = phost->saddr.sin_addr.s_addr;
		ACL_SAFE_STRNCPY(h_host->ip, phost->ip, sizeof(h_host->ip));
		h_host->hport = phost->hport;

		if (acl_array_append(dns_db->h_db, h_host) < 0) {
			acl_msg_error("%s, %s(%d): array append error(%s)",
					__FILE__, myname, __LINE__,
					acl_last_strerror(buf, sizeof(buf)));
			acl_netdb_free(dns_db);
			return (NULL);
		}       

		dns_db->size++;
	}

	return (dns_db);
}
Example #6
0
static void proctl_service_add(PROCTL_SERVICE *service)
{
	const char *myname = "proctl_service_add";

	/* 向服务对象数据组中添加新的服务对象 */

	LOCK_RUNNING_SERVICE;
	if (acl_array_append(__services, service) < 0)
		acl_msg_fatal("%s(%d): add service to array error", myname, __LINE__);
	UNLOCK_RUNNING_SERVICE;
}
Example #7
0
ACL_ARRAY *acl_xml_getElementsByTagName(ACL_XML *xml, const char *tag)
{
	ACL_ITER iter;
	ACL_ARRAY *a = acl_array_create(10);

	acl_foreach(iter, xml) {
		ACL_XML_NODE *node = (ACL_XML_NODE*) iter.data;
		if (strcasecmp(tag, STR(node->ltag)) == 0) {
			acl_array_append(a, node);
		}
	}
Example #8
0
AUT_LINE *aut_add_outer_cmd(const ACL_CFG_LINE *cfg_line)
{
	const char *myname = "aut_add_outer_cmd";
	AUT_LINE *test_line;

	if (cfg_line->ncount < 3)
		aut_log_fatal("%s: cmd_name=%s, ncount=%d, input error, "
				"please check configure file",
				myname, cfg_line->value[0], cfg_line->ncount);

	test_line = (AUT_LINE *) acl_mycalloc(1, sizeof(*test_line));

	if (test_line == NULL) {
		char tbuf[256];
		aut_log_fatal("%s: can't malloc AUT_LINE, err_msg=%s",
			myname, acl_last_strerror(tbuf, sizeof(tbuf)));
	}

	snprintf(test_line->cmd_name, sizeof(test_line->cmd_name),
			"%s", cfg_line->value[0]);
	test_line->line_number = cfg_line->line_number;
	test_line->result      = atoi(cfg_line->value[1]);
	test_line->argc        = atoi(cfg_line->value[2]);

	if (cfg_line->ncount >= 4) {
		test_line->args_str = acl_mystrdup(cfg_line->value[3]);
		if (test_line->args_str == NULL) {
			char tbuf[256];
			aut_log_fatal("%s: cmd_name=%s, strdup for "
					"args_str, err_msg=%s",
					myname,
					cfg_line->value[0],
					acl_last_strerror(tbuf, sizeof(tbuf)));
		}
		test_line->argv = aut_parse_args_list(cfg_line->value[3]);
		if (test_line->argv == NULL)
			aut_log_fatal("%s: cmd_name=%s, aut_parse_args_list error",
					myname, cfg_line->value[0]);
	} else {
		test_line->args_str = NULL;
		test_line->argv = NULL;
	}

	if (acl_array_append(var_aut_line_array, (void *) test_line) < 0) {
		char tbuf[256];
		aut_log_fatal("%s: cmd_name=%s, acl_array_append error, err_msg=%s",
				myname, acl_last_strerror(tbuf, sizeof(tbuf)));
	}

	test_line->valid_line_idx = var_aut_valid_line_idx++;

	return (test_line);
}
Example #9
0
AUT_LINE *aut_add_inner_cmd(const ACL_CFG_LINE *line)
{
	char  myname[] = "aut_add_inner_cmd";
	AUT_LINE *test_line = NULL;
	__MATCH_CMD *pmatch_cmd;
	AUT_CMD_TOKEN *inner_token;
	int   i;

	if (line->ncount < 1) {
		aut_log_error("%s: ncount=%d", myname, line->ncount);
		return (NULL);
	}

	for (i = 0; __inner_cmd_tab[i].cmd_name != NULL; i++) {
		pmatch_cmd = &__inner_cmd_tab[i];
		if (strcasecmp(line->value[0], pmatch_cmd->cmd_name) == 0) {
			/* 由内部命令保留函数动态分配一个相对应的
			 * AUT_LINE 对象
			 */
			test_line = pmatch_cmd->match_fn(line);
			break;
		}
	}

	if (test_line == NULL)
		return (NULL);

	if (acl_array_append(var_aut_line_array, (void *) test_line) < 0) {
		char  tbuf[256];
		aut_log_fatal("%s: cmd_name=%s, "
			"acl_array_append error, err_msg=%s",
			myname, test_line->cmd_name, acl_last_strerror(tbuf, sizeof(tbuf)));
	}

	/* 设置有效行号 */
	inner_token = (AUT_CMD_TOKEN *) test_line->arg_inner;
	if (inner_token == NULL)
		return (test_line);

	test_line->valid_line_idx = var_aut_valid_line_idx++;

	/* 调整有效行号 */
	inner_token->valid_line_idx = test_line->valid_line_idx;

	/* 只对循环命令起作用, 设置相对命令位移 */
	inner_token->offset_valid_line_idx = inner_token->valid_line_idx;

	return (test_line);
}
Example #10
0
static void __add_fn_item(ACL_ARRAY *fn_tab, const AUT_FN_ITEM *fn_item, int inner)
{
	char  myname[] = "__add_fn_item";
	AUT_FN_ITEM *item;

	item = (AUT_FN_ITEM *) acl_mycalloc(1, sizeof(AUT_FN_ITEM));
	item->cmd_name    = fn_item->cmd_name;
	item->fn_name     = fn_item->fn_name;
	item->fn_callback = fn_item->fn_callback;
	item->arg         = fn_item->arg;
	item->inner       = inner;

	if (acl_array_append(fn_tab, item) < 0) {
		char tbuf[256];
		aut_log_fatal("%s(%d): array_append error(%s)",
			myname, __LINE__, acl_last_strerror(tbuf, sizeof(tbuf)));
	}
}
Example #11
0
ACL_XML_ATTR *acl_xml_attr_alloc(ACL_XML_NODE *node)
{
    ACL_XML_ATTR *attr;

    if (node->xml->slice)
        attr = (ACL_XML_ATTR*) acl_slice_pool_calloc(__FILE__, __LINE__,
                node->xml->slice, 1, sizeof(ACL_XML_ATTR));
    else
        attr = (ACL_XML_ATTR*) acl_mycalloc(1, sizeof(ACL_XML_ATTR));
    attr->node = node;
    attr->name = acl_vstring_slice_alloc(node->xml->slice, 16);
    attr->value = acl_vstring_slice_alloc(node->xml->slice, 16);
    attr->quote = 0;
    attr->backslash = 0;
    attr->part_word = 0;

    acl_array_append(node->attr_list, attr);
    return (attr);
}
Example #12
0
File: zdb.c Project: lunlun1992/acl
static ZDB_DISK *zdb_disks_load(const char *dbname, const char *dbpath)
{
    const char *myname = "zdb_disks_load";
    ACL_VSTRING *buf = acl_vstring_alloc(256);
    ACL_FILE *fp = NULL;
    char  disk_info[INFO_LEN + 1];
    ZDB_DISK *disk, *disks;
    ACL_ARRAY *a = NULL;
    ACL_ITER iter;
    int   n, i;

#undef	RETURN
#define	RETURN(x) do {  \
	if (fp)  \
		acl_fclose(fp);  \
	acl_vstring_free(buf);  \
	if (a)  \
		acl_array_destroy(a, free_disk);  \
	return (x);  \
} while (0)

    acl_vstring_sprintf(buf, "%s/.%s.disk", dbpath, dbname);
    fp = acl_fopen(STR(buf), "r");
    if (fp == NULL) {
        acl_msg_error("%s(%d): fopen(%s) error(%s)",
                      myname, __LINE__, STR(buf), acl_last_serror());
        RETURN (NULL);
    }

    a = acl_array_create(10);
    while (1) {
        ACL_ARGV *argv;

        if (acl_fgets_nonl(disk_info, sizeof(disk_info), fp) == NULL)
            break;
        argv = acl_argv_split(disk_info, "|");
        if (argv->argc != ITEM_CNT) {
            acl_msg_error("%s(%d): invalid line(%s)",
                          myname, __LINE__, disk_info);
            acl_argv_free(argv);
            continue;
        }
        disk = (ZDB_DISK*) acl_mycalloc(1, sizeof(ZDB_DISK));
        disk->path = acl_mystrdup(argv->argv[0]);
        disk->idisk = atoi(argv->argv[1]);
        disk->priority = atoi(argv->argv[2]);
        disk->limit = acl_atoui64(argv->argv[3]);
        disk->count = acl_atoui64(argv->argv[4]);
        if (acl_array_append(a, disk) < 0)
            acl_msg_fatal("%s(%d): add disk error(%s)",
                          myname, __LINE__, acl_last_serror());
        acl_argv_free(argv);
    }

    n = acl_array_size(a);
    if (n <= 0) {
        acl_msg_error("%s(%d): empty array of ZDB_DISK", myname, __LINE__);
        RETURN (NULL);
    }

    disks = (ZDB_DISK*) acl_mycalloc(n + 1, sizeof(ZDB_DISK));
    i = 0;
    acl_foreach(iter, a) {
        disk = (ZDB_DISK*) iter.data;
        disks[i].limit = disk->limit;
        disks[i].count = disk->count;
        disks[i].path = acl_mystrdup(disk->path);
        disks[i].idisk = disk->idisk;
        disks[i].priority = disk->priority;
        disks[i].dat_ifiles = NULL;
        disks[i].dat_ifiles_size = 0;
        if (disks[i].idisk != i) {
            acl_msg_error("%s(%d): idisk(%d) != %d invalid for %s",
                          myname, __LINE__, disks[i].idisk, i, disks[i].path);
            acl_myfree(disks);
            RETURN (NULL);
        }
        i++;
    }
Example #13
0
static ACL_DB_HANDLE_MYSQL *__open_mysql_handle(ACL_DB_POOL_MYSQL *mysql_pool,
	ACL_DB_HANDLE_MYSQL *mysql_handle, ACL_DB_INFO *db_info)
{
	char  myname[] = "__open_mysql_handle";
	int   reuse_flag = 0;
	const char *ptr;
	char *db_host, *db_unix;
	int   db_port;
	char  tmpbuf[256];
	int   n, len, i;
	my_bool reconnect = 1;

	ptr = strchr(db_info->db_addr, '/');
	if (ptr == NULL) {
		ptr = strchr(db_info->db_addr, ':');
		if (ptr == NULL)
			acl_msg_fatal("%s, %s(%d): invalid db_addr=%s",
				__FILE__, myname, __LINE__, db_info->db_addr);
		len = ptr - db_info->db_addr;
		if (len == 0)
			acl_msg_fatal("%s, %s(%d): invalid db_addr=%s",
				__FILE__, myname, __LINE__, db_info->db_addr);

		len++;	/* 1 for '\0' */
		i = sizeof(tmpbuf) - 1;
		n = i > len ? len : i - 1;
		ACL_SAFE_STRNCPY(tmpbuf, db_info->db_addr, n);
		db_host = tmpbuf;

		ptr++;  /* skip ':' */
		db_port = atoi(ptr);
		if (db_port <= 0)
			acl_msg_fatal("%s, %s(%d): invalid port=%d",
				__FILE__, myname, __LINE__, db_port);

		db_unix = NULL;
	} else {
		db_unix = db_info->db_addr;
		db_host = NULL;
		db_port = 0;
	}

	if (mysql_handle == NULL) {
		mysql_handle = __new_mysql_handle();
		mysql_handle->handle.parent = &mysql_pool->db_pool;
	} else
		reuse_flag = 1;

	mysql_handle->connection = mysql_init(NULL);
	if (mysql_handle->connection == NULL) {
		acl_msg_error("%s, %s(%d): mysql init error",
			__FILE__, myname, __LINE__);
		if (!reuse_flag)
			acl_myfree(mysql_handle);
		return (NULL);
	}

	if (db_info->conn_timeout > 0)
		mysql_options(mysql_handle->connection,
			MYSQL_OPT_CONNECT_TIMEOUT,
			(const void*) &db_info->conn_timeout);
	if (db_info->rw_timeout > 0) {
		mysql_options(mysql_handle->connection,
			MYSQL_OPT_READ_TIMEOUT,
			(const void*) &db_info->rw_timeout);
		mysql_options(mysql_handle->connection,
			MYSQL_OPT_WRITE_TIMEOUT,
			(const void*) &db_info->rw_timeout);
	}
	mysql_options(mysql_handle->connection,
		MYSQL_OPT_RECONNECT, (const void*) &reconnect);

	if (db_info->db_before_connect
		&& db_info->db_before_connect((ACL_DB_HANDLE*) mysql_handle,
			db_info->ctx) < 0)
	{
		acl_msg_error("%s, %s(%d): db_before_connect return < 0",
			__FILE__, myname, __LINE__);
		mysql_close(mysql_handle->connection);
		mysql_handle->connection = NULL;
		if (!reuse_flag)
			acl_myfree(mysql_handle);
		return (NULL);
	}

	if (mysql_real_connect(mysql_handle->connection,
				db_host,
				db_info->db_user,
				db_info->db_pass,
				db_info->db_name,
				db_port,
				db_unix,
				db_info->db_flags) == NULL) {
		acl_msg_error("%s, %s(%d): connect mysql error(%s), db_host=%s",
				__FILE__, myname, __LINE__,
				mysql_error(mysql_handle->connection), db_info->db_addr);

		mysql_close(mysql_handle->connection);
		mysql_handle->connection = NULL;
		if (!reuse_flag)
			acl_myfree(mysql_handle);
		return (NULL);
	}

#if MYSQL_VERSION_ID >= 50000
	if (mysql_autocommit(mysql_handle->connection, db_info->auto_commit) != 0) {
		acl_msg_error("%s, %s(%d): mysql_autocommit error",
				__FILE__, myname, __LINE__);
		mysql_close(mysql_handle->connection);
		mysql_handle->connection = NULL;
		if (!reuse_flag)
			acl_myfree(mysql_handle);
		return (NULL);
	}
#else
	db_info->auto_commit = 0;
#endif

	if (db_info->db_after_connect
		&& db_info->db_after_connect((ACL_DB_HANDLE*) mysql_handle,
			db_info->ctx) < 0)
	{
		acl_msg_error("%s, %s(%d): db_after_connect return < 0",
			__FILE__, myname, __LINE__);
		mysql_handle->connection = NULL;
		if (!reuse_flag)
			acl_myfree(mysql_handle);
		return (NULL);
	}

	if (acl_msg_verbose)
		acl_msg_info("OK, database connected, db_host: %s"
			", db_name: %s, db_user: %s, autocommit %s",
			db_info->db_addr, db_info->db_name, db_info->db_user,
			db_info->auto_commit ? "true" : "false");
#if 0
	acl_msg_info("db_pass: %s\n", db_info->db_pass);
#endif

	mysql_handle->handle.status  = ACL_DBH_STATUS_READY;
	mysql_handle->handle.timeout = time(NULL) + db_info->timeout_inter;
	mysql_handle->handle.ping    = time(NULL) + db_info->ping_inter;

	if (!reuse_flag) {
		if (acl_array_append(mysql_pool->handles, mysql_handle) < 0) {
			acl_msg_fatal("%s, %s(%d): append to handles error(%s)",
				__FILE__, myname, __LINE__, acl_last_serror());
		}
	}

	return (mysql_handle);
}
Example #14
0
static void array_push_back(struct ACL_ARRAY *a, void *obj)
{
	acl_array_append(a, obj);
}
Example #15
0
ACL_DNS_DB *acl_gethostbyname(const char *name, int *h_error)
{
	ACL_DNS_DB *db;
	ACL_SOCKADDR saddr;
	struct addrinfo *res0, *res;

	if (h_error)
		*h_error = 0;
	
	/* lookup the local dns cache first */
	db = acl_netdb_cache_lookup(name);
	if (db)
		return db;

	db = acl_netdb_new(name);

	if (ip2addr(name, 0, &saddr)) {
		ACL_HOSTNAME *h_host = acl_mycalloc(1, sizeof(ACL_HOSTNAME));

		memcpy(&h_host->saddr, &saddr, sizeof(h_host->saddr));
		ACL_SAFE_STRNCPY(h_host->ip, name, sizeof(h_host->ip));
		(void) acl_array_append(db->h_db, h_host);
		db->size++;
		return db;
	}

	res0 = acl_host_addrinfo(name, SOCK_DGRAM);
	if (res0 == NULL)
		return NULL;

	for (res = res0; res != NULL; res = res->ai_next) {
		ACL_SOCKADDR *sa = (ACL_SOCKADDR *) res->ai_addr;
		ACL_HOSTNAME *h_host;
		char ip[64];

		memset(&saddr, 0, sizeof(saddr));
		saddr.sa.sa_family = res->ai_family;
		if (res->ai_family == AF_INET) {
			if (inet_ntop(res->ai_family, &sa->in.sin_addr,
				ip, sizeof(ip)) == NULL) {

				continue;
			}
			memcpy(&saddr.in.sin_addr, &sa->in.sin_addr,
				sizeof(saddr.in.sin_addr));
#ifdef AF_INET6
		} else if (res->ai_family == AF_INET6) {
			if (inet_ntop(res->ai_family, &sa->in6.sin6_addr,
				ip, sizeof(ip)) == NULL) {

				continue;
			}
			memcpy(&saddr.in6.sin6_addr, &sa->in6.sin6_addr,
				sizeof(saddr.in6.sin6_addr));
#endif
		} else {
			continue;
		}

		h_host = (ACL_HOSTNAME*) acl_mycalloc(1, sizeof(ACL_HOSTNAME));
		memcpy(&h_host->saddr, &saddr, sizeof(h_host->saddr));
		ACL_SAFE_STRNCPY(h_host->ip, ip, sizeof(h_host->ip));
		h_host->hport = 0;

		(void) acl_array_append(db->h_db, h_host);
		db->size++;
	}

	freeaddrinfo(res0);

	if (acl_netdb_size(db) > 0)
		return db;

	acl_netdb_free(db);
	return NULL;
}
Example #16
0
ACL_CFG_PARSER *acl_cfg_parser_load(const char *pathname, const char *delimiter)
{
	char  myname[] = "acl_cfg_parse_load";
	ACL_CFG_PARSER *parser = NULL;
	ACL_CFG_LINE *cfg_line;
	struct stat stat_buf;
	int   buf_size;
	char *content_buf = NULL, *ptr;
	char *pline_begin;
	ACL_FILE_HANDLE   filefd = ACL_FILE_INVALID;
	char  tbuf[256];
	
#undef	ERETURN
#define	ERETURN(x) do { \
	if (content_buf != NULL) \
		acl_myfree(content_buf); \
	if (filefd != ACL_FILE_INVALID) \
		acl_file_close(filefd); \
	if (parser != NULL) { \
		acl_array_destroy(parser->_cfg_array, NULL); \
		acl_myfree(parser); \
	} \
	return (x); \
} while (0);

#undef	RETURN
#define	RETURN(x) do { \
	if (content_buf != NULL) \
		acl_myfree(content_buf); \
	if (filefd != ACL_FILE_INVALID) \
		acl_file_close(filefd); \
	return (x); \
} while (0);

	if (pathname == NULL || *pathname == 0) {
		printf("%s: invalid pathname\n", myname);
		return (NULL);
	}

	if (stat(pathname, &stat_buf) < 0) {
		printf("%s: can't stat, pathname=%s, errmsg=%s\n",
			myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf)));
		ERETURN (NULL);
	}

	parser = (ACL_CFG_PARSER *) acl_mycalloc(1, sizeof(*parser));
	if (parser == NULL) {
		printf("%s: can't calloc ACL_CFG_PARSER, pathname=%s, errmsg=%s",
			myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf)));
		ERETURN (NULL);
	}

	parser->_cfg_array = acl_array_create(10);
	if (parser->_cfg_array == NULL) {
		printf("%s: can't create array, pathname=%s, errmsg=%s",
			myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf)));
		ERETURN (NULL);
	}
	parser->total_line = 0;
	parser->valid_line = 0;

	buf_size = (int) stat_buf.st_size + 256;
	content_buf = (char *) acl_mycalloc(1, buf_size);
	if (content_buf == NULL) {
		printf("%s: can't calloc, pathname=%s, errmsg=%s\n",
			myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf)));
		ERETURN (NULL);
	}
	
#ifdef ACL_UNIX
# ifdef ACL_ANDROID
	filefd = acl_file_open(pathname, O_RDWR, 0644);
# else
	filefd = acl_file_open(pathname, O_RDWR, S_IREAD | S_IWRITE | S_IRGRP);
# endif
#elif defined(ACL_WINDOWS)
	filefd = acl_file_open(pathname, O_RDWR, S_IREAD | S_IWRITE);
#else
# error "unknown OS"
#endif

	if (filefd == ACL_FILE_INVALID) {
		printf("%s: can't open, pathname=%s, errmsg=%s\n",
			myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf)));

		ERETURN (NULL);
	}

	if (_cfg_file_load(filefd, content_buf, buf_size) < 0) {
		printf("%s: can't read, pathname=%s, errmsg=%s\n",
			myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf)));
		ERETURN (NULL);
	}

#undef	SKIP
#define SKIP(var, cond) \
        for (; *var && (cond); var++) {}

	ptr = content_buf;
	while (*ptr) {
		pline_begin = ptr;  /* keep the line header */
		/* first, skip all ' ' and '\t' */
		SKIP(ptr, (*ptr == ' ' || *ptr == '\t'));

		if  (*ptr == '#') {  /* the comment line */
			SKIP(ptr, *ptr != '\n'); /* find the line's end */
			if (*ptr) {  /* this must be '\n' */
				*ptr++ = 0;  /* set '\0' and skip one byte */
			}

			cfg_line = _backup_junk_line(pline_begin);
			if (cfg_line == NULL)
				ERETURN (NULL);
			if (acl_array_append(parser->_cfg_array,
					(void *) cfg_line) < 0) {
				printf("%s: can't add ACL_CFG_LINE to array, "
					"errmsg=%s", myname, acl_last_strerror(tbuf, sizeof(tbuf)));
				ERETURN (NULL);
			}
			parser->total_line++;
			cfg_line->line_number = parser->total_line;
			continue;
		} else if (*ptr == '\r' || *ptr == '\n') {
			/* SKIP(ptr, (*ptr == '\r' || *ptr == '\n')); */
			if (*ptr == '\r' && *(ptr + 1) == '\n') {
				*ptr = 0; /* set '\0' first and go on */
				ptr += 2;
			} else if (*ptr == '\n') {
				*ptr = 0; /* set '\0' first and go on */
				ptr++;
			}

			cfg_line = _backup_junk_line(pline_begin);
			if (cfg_line == NULL)
				ERETURN (NULL);
			if (acl_array_append(parser->_cfg_array,
					(void *) cfg_line) < 0) {
				printf("%s: can't add ACL_CFG_LINE to array, "
					"errmsg=%s", myname, acl_last_strerror(tbuf, sizeof(tbuf)));
				ERETURN (NULL);
			}
			parser->total_line++;
			cfg_line->line_number = parser->total_line;
			continue;
		}

		pline_begin = ptr;  /* reset the line header */

		/* find the line's end */
		SKIP(ptr, (*ptr != '\n' && *ptr != '\r'));
		if (*ptr) {  /* this must be '\r' or '\n' */
			if (*ptr == '\r' && *(ptr + 1) == '\n') {
				*ptr = 0; /* set '\0' first and go on */
				ptr += 2;
			} else if (*ptr == '\n') {
				*ptr = 0; /* set '\0' first and go on */
				ptr++;
			}
		}

		/* make ptr to the next line's beginning */
		/* SKIP(ptr, (*ptr == '\r' || *ptr == '\n')); */

		cfg_line = _create_cfg_line(pline_begin, delimiter);
		if (cfg_line == NULL)
			ERETURN (NULL);
		if (acl_array_append(parser->_cfg_array, (void *) cfg_line) < 0) {
			printf("%s: can't add ACL_CFG_LINE to array, errmsg=%s",
				myname, acl_last_strerror(tbuf, sizeof(tbuf)));
			ERETURN (NULL);
		}
		parser->total_line++;
		parser->valid_line++;
		cfg_line->line_number = parser->total_line;
	}

	if (parser->total_line != acl_array_size(parser->_cfg_array)) {
		printf("%s: total_line=%d, acl_array_size=%d, errmsg=not equal\n",
			myname, parser->total_line,
			acl_array_size(parser->_cfg_array));
	}

	RETURN (parser);
#ifdef ACL_BCB_COMPILER
	return (NULL);
#endif
}
Example #17
0
ACL_DNS_DB *acl_gethostbyname(const char *name, int *h_error)
{
	char  myname[] = "acl_gethostbyname";
	ACL_DNS_DB *h_dns_db = NULL;
	ACL_HOSTNAME *h_host;
/* #ifndef	SUNOS5 */
	struct hostent *h_addrp = NULL;
/* #endif */

#ifdef	ACL_UNIX
# ifndef ACL_MACOSX
	struct hostent  h_buf;
	int   errnum = 0;
# endif
#endif
	char **pptr, buf[4096];
	int   n;

#undef	ERETURN
#define	ERETURN(_x_) do {  \
	if (h_dns_db)  \
		acl_netdb_free(h_dns_db);  \
	return (_x_);  \
} while (0)

	if (name == NULL) {
		acl_msg_error("%s, %s(%d): input error",
				__FILE__, myname, __LINE__);
		ERETURN (NULL);
	}

	if (h_error)
		*h_error = 0;
	
	/* lookup the local dns cache first */
	h_dns_db = acl_netdb_cache_lookup(name);
	if (h_dns_db)
		return (h_dns_db);

	h_dns_db = acl_netdb_new(name);
	if (h_dns_db == NULL) {
		acl_msg_error("%s, %s(%d): acl_netdb_new error(%s)",
			__FILE__, myname, __LINE__,
			acl_last_strerror(buf, sizeof(buf)));
		return (NULL);
	}

	if (acl_is_ip(name) == 0) {
		h_host = acl_mycalloc(1, sizeof(ACL_HOSTNAME));
		if (h_host == NULL) {
			acl_msg_error("%s, %s(%d): calloc error(%s)",
				__FILE__, myname, __LINE__,
				acl_last_strerror(buf, sizeof(buf)));
			ERETURN (NULL);
		}
		h_host->saddr.sin_addr.s_addr = inet_addr(name);
		ACL_SAFE_STRNCPY(h_host->ip, name, sizeof(h_host->ip));

		if (acl_array_append(h_dns_db->h_db, h_host) < 0) {
			acl_msg_error("%s, %s(%d): array append error(%s)",
				__FILE__, myname, __LINE__,
				acl_last_strerror(buf, sizeof(buf)));
			ERETURN (NULL);
		}

		h_dns_db->size++;

		return (h_dns_db);
	}

	memset(buf, 0, sizeof(buf));

	h_addrp = NULL;

#if	defined(ACL_MS_WINDOWS) || defined(ACL_MACOSX)
	h_addrp = gethostbyname(name);
	if (h_addrp == NULL) {
		acl_msg_error("%s, %s(%d): gethostbyname error(%s), addr=%s",
				__FILE__, myname, __LINE__,
				acl_last_strerror(buf, sizeof(buf)), name);
		ERETURN (NULL);
	}

#elif	defined(ACL_UNIX)
	memset(&h_buf, 0, sizeof(h_buf));
# if	defined(LINUX2) || defined(ACL_FREEBSD)
	n = gethostbyname_r(name, &h_buf, buf, sizeof(buf), &h_addrp, &errnum);
	if (n) {
		if (h_error)
			*h_error = errnum;
		ERETURN (NULL);
	}
# elif	defined(SUNOS5)
	h_addrp = gethostbyname_r(name, &h_buf, buf, sizeof(buf), &errnum);
	if (h_addrp == NULL) {
		if (h_error)
			*h_error = errnum;
		ERETURN (NULL);
	}
# else
#  error "unknown OS type"
# endif
#else
# error "unknown OS type"
#endif

	if (h_addrp == NULL || h_addrp->h_addr_list == NULL) {
		acl_msg_error("%s, %s(%d): null result return(%s)",
				__FILE__, myname, __LINE__,
				acl_last_strerror(buf, sizeof(buf)));
		ERETURN (NULL);
	}

	for (pptr = h_addrp->h_addr_list; *pptr != NULL; pptr++) {
		h_host = acl_mycalloc(1, sizeof(ACL_HOSTNAME));
		if (h_host == NULL) {
			acl_msg_error("%s, %s(%d): calloc error(%s)",
				__FILE__, myname, __LINE__,
				acl_last_strerror(buf, sizeof(buf)));
			ERETURN (NULL);
		}

		memset(&h_host->saddr, 0, sizeof(h_host->saddr));
		n = (int) sizeof(h_host->saddr.sin_addr) > h_addrp->h_length
			? h_addrp->h_length : (int) sizeof(h_host->saddr.sin_addr);
		memcpy(&h_host->saddr.sin_addr, *pptr, n);
		/* bugifx: 2009.12.8
		 * this is not thread safe
		 * ACL_SAFE_STRNCPY(h_host->ip, inet_ntoa(h_host->saddr.sin_addr), sizeof(h_host->ip));
		 */
		acl_inet_ntoa(h_host->saddr.sin_addr, h_host->ip, sizeof(h_host->ip));

		if (acl_array_append(h_dns_db->h_db, h_host) < 0) {
			acl_msg_error("%s, %s(%d): array append error(%s)",
				__FILE__, myname, __LINE__,
				acl_last_strerror(buf, sizeof(buf)));
			ERETURN (NULL);
		}

		h_dns_db->size++;
	}

	acl_netdb_cache_push(h_dns_db, 0);

	return (h_dns_db);
}
Example #18
0
static ACL_CFG_LINE *_create_cfg_line(char *data, const char *delimiter)
{
	ACL_CFG_LINE *cfg_line = NULL;
	ACL_ARRAY *a = NULL;
	int   i, n;
	char *ptr, *pdata, *pitem;

#undef	ERETURN
#define	ERETURN(x) do {                                                      \
	if (a)                                                               \
		acl_array_destroy(a, acl_myfree_fn);                         \
	if (cfg_line) {                                                      \
		if (cfg_line->value)                                         \
			acl_myfree(cfg_line);                                    \
		acl_myfree(cfg_line);                                            \
	}                                                                    \
	return (x);                                                          \
} while (0);

	if (data == NULL)
		return (NULL);

	pdata = data;

	cfg_line = (ACL_CFG_LINE *) acl_mycalloc(1, sizeof(ACL_CFG_LINE));
	if (cfg_line == NULL)
		return (NULL);

	a = acl_array_create(10);
	while (1) {
		ptr = acl_mystrtok(&pdata, delimiter);
		if (ptr == NULL)
			break;
		pitem = acl_mystrdup(ptr);
		if (pitem == NULL) {
			ERETURN (NULL);
		}
		if (acl_array_append(a, (void *) pitem) < 0) {
			ERETURN (NULL);
		}
	}

	cfg_line->ncount = 0;
	cfg_line->pdata = NULL;
	n = acl_array_size(a);
	if (n > 0) {
		cfg_line->value = (char **) acl_mycalloc(1 + n, sizeof(char *));
		if (cfg_line->value == NULL) {
			ERETURN (NULL);
		}
		for (i = 0; i < n; i++) {
			pitem = (char *) acl_array_index(a, i);
			if (pitem == NULL)
				break;
			cfg_line->value[i] = pitem;
			if (cfg_line->value[i] == NULL)
				ERETURN (NULL);
			cfg_line->ncount++;
		}
	}

	/* NOTICE: in acl_array_destroy, please don't input acl_myfree, but
	 * NULL as the second parameter, because the mystrup's result
	 * set are stored in cfg_line->value now:)
	 */
	acl_array_destroy(a, NULL);

	return (cfg_line);
}
Example #19
0
/* 分析配置文件中的第四个参数, 将其进行分解并存入动态数组之中 */
ACL_ARRAY *aut_parse_args_list(const char *str_in)
{
	const char *myname = "aut_parse_args_list";
	ACL_ARRAY *argvs_array = NULL;
	AUT_ARG_ITEM *arg_item = NULL;
	char *ptr_item, *pstr, *pstr_saved, *pname, *pvalue;
	char *ptr;
	int   len;
	char  tbuf[256];

	argvs_array = acl_array_create(10);
	pstr = acl_mystrdup(str_in);
	pstr_saved = pstr;

#define	SKIP_WHILE(_cond, _ptr) { while (*_ptr && (_cond)) _ptr++; }
#define	SKIP_WHILE_DEC(_cond, _ptr) { while (*_ptr && (_cond)) _ptr--; }

	len = strlen("=");
	while (1) {
		/* 找到每一参数项, 分隔符为逗号 */
		ptr_item = acl_mystrtok(&pstr, ",");
		if (ptr_item == NULL)
			break;

		/* 删除变量名前的空格和 tab */
		SKIP_WHILE((*ptr_item == ' ' || *ptr_item == '\t'), ptr_item);
		pname = ptr_item;

		/* 先找到等于号分隔符 */
		pvalue = strstr(ptr_item, "=");
		if (pvalue == NULL) /* not found '=' */
			continue;

		ptr = pvalue;

		/* 删除等号左边的空格或 tab */
		SKIP_WHILE_DEC((*ptr == ' ' || *ptr == '\t'), ptr);
		if (ptr < pvalue)
			*(++ptr) = 0;

		*pvalue = 0;
		pvalue += len; /* skip '=' */

		/* 删除等号右边的空格和ab */
		SKIP_WHILE((*pvalue == ' ' || *pvalue == '\t'), pvalue);
		if (*pvalue == 0)
			continue;

		/* 分配一个参数项 */
		arg_item = (AUT_ARG_ITEM *) acl_mycalloc(1, sizeof(AUT_ARG_ITEM));
		arg_item->name = acl_mystrdup(pname);
		arg_item->value = acl_mystrdup(pvalue);

		/* 把该参数项加入到动态数组之中 */
		if (acl_array_append(argvs_array, (void *) arg_item) < 0)
			aut_log_fatal("%s(%d): append to array error(%s)",
				myname, __LINE__, acl_last_strerror(tbuf, sizeof(tbuf)));
	}

	acl_myfree(pstr_saved);

	return (argvs_array);
}