Beispiel #1
0
static int test_service(int live, const char *svc)
{
    /* Set environment to force testing of NAMERD (unless it's already set). */
    if ( ! is_env_set("CONN_LBSMD_DISABLE"))
        CORE_REG_SET("", "CONN_LBSMD_DISABLE", "1", eREG_Transient);

    if ( ! is_env_set("CONN_DISPD_DISABLE"))
        CORE_REG_SET("", "CONN_DISPD_DISABLE", "1", eREG_Transient);

    if ( ! is_env_set("CONN_NAMERD_ENABLE"))
        CORE_REG_SET("", "CONN_NAMERD_ENABLE", "1", eREG_Transient);

    /* Run the test */
    return run_a_test(0, 1 ,svc, s_user_header, 0, 0, NULL, 0, 0) ? 0 : 1;
}
int main(int argc, char *argv[]) {
	int ret = -EINVAL;
	int (*cmd)(char*) = NULL;
	char *url, *env;
	int c;

	umask(0077);
	int inval = 0;
	if (!is_env_set())
	{
		print_env(ANSPASS_ENV);
		inval = 1;
	}

	if (!is_token_set())
	{
		print_env(ANSPASS_TOKEN);
		inval = 1;
	}

	if (argc != 2 && argc != 3)
	{
		print_help(1);
		inval = 1;
	}
	if (inval)
		goto missing_options;


	ret = -ENOMEM;
	url = (char*)calloc(1, sizeof(char)*MAX_MSG_LENGTH);
	if (!url)
		goto no_url_mem;

	ret = -EINVAL;
	static struct option long_options[] =
	{
		{"add",     required_argument, 0, 'a'},
		{"delete",  required_argument, 0, 'd'},
		{"update",  required_argument, 0, 'u'},
		{"reset",   no_argument,       0, 'r'},
		{"quit",    no_argument,       0, 'q'},
		{"help",    no_argument,       0, 'h'},
		{0, 0, 0, 0}
	};
	int option_index = 0;
	while((c = getopt_long (argc, argv, "a:d:u:rqh", long_options,
					&option_index)) != -1)
	{
		if (optarg && strlen(optarg) > MAX_MSG_LENGTH - 1)
		{
			printf("Error: option too long: %s\n", optarg);
			goto too_long;
		}

		switch (c)
		{
		case 'a': /* Add */
			cmd = &handle_add;
			memcpy(url, optarg, strlen(optarg)+ 1);
			break;
		case 'd': /* Delete*/
			cmd = &handle_del;
			memcpy(url, optarg, strlen(optarg) + 1);
			break;
		case 'u': /* Update */
			cmd = &handle_update;
			memcpy(url, optarg, strlen(optarg) + 1);
			break;
		case 'r': /* Reset */
			cmd = &handle_reset;
			break;
		case 'q': /* Reset */
			cmd = &handle_quit;
			break;
		case 'h': /* Help */
			ret = 0;
			/* fall-through expected */
		default:
			print_help(ret);
			goto help;
		}
	}


	ret = -ENOMEM;
	env = getenv(ANSPASS_ENV);
	info.env_path= (char*)calloc(1, sizeof(char)*strlen(env)+1);
	if (!info.env_path)
		goto no_env_path;
	strcpy(info.env_path, env);

	info.old_path = getcwd(NULL, 0);
	if (!info.old_path)
		goto no_old_path;

	if (chdir((const char *)info.env_path) < 0)
	{
		printf("Change dir %s failed: %s\n", info.env_path, strerror(errno));
		goto chdir_fail;
	}

	env = getenv(ANSPASS_TOKEN);
	info.token = (char*)calloc(1, sizeof(char)*TOKEN_LEN+1);
	if (!info.token)
		goto no_token_mem;
	strncpy(info.token, env, TOKEN_LEN);

	ret = setup_socket(&info);
	if (ret) {
		printf("Error: Socket failed: %d\n", ret);
		goto socket_fail;
	}

	ret = -ENOTCONN;
	if (connect(info.socket, (struct sockaddr *)info.s_name, sizeof(struct
					sockaddr_un)) == -1)
	{
		ret = errno;
		printf("Error: Connection refused: %d\n", ret);
		goto not_connected;
	}

	ret = cmd(url);

	close(info.socket);
not_connected:
	free(info.s_name);
socket_fail:
	free(info.token);
no_token_mem:
	if(chdir((const char *)info.old_path))
		perror("chdir");
chdir_fail:
	free(info.old_path);
no_old_path:
	free(info.env_path);
no_env_path:
too_long:
help:
	free(url);
no_url_mem:
missing_options:
	return ret;
}
int main(int argc, char *argv[]) {
	int ret = -EINVAL;
	int i = 0;
	int size = 0;
	char *msg;

	umask(0077);
	if (argc == 1)
		goto no_entry;

	if (!is_env_set())
	{
		print_env(ANSPASS_ENV);
		goto no_env;
	}

	if (!is_token_set())
	{
		print_env(ANSPASS_TOKEN);
		goto no_token;
	}

	for (i = 1; i < argc; i++)
	{
		size = strlen(argv[i]) + 1;
	}

	if (size > MAX_MSG_LENGTH)
		goto too_big;

	ret = -ENOMEM;
	msg = (char*)calloc(1, sizeof(char)*MAX_MSG_LENGTH);
	if (!msg)
		goto no_q_mem;


	size = 0;
	for (i = 1; i < argc; i++)
	{
		strcat(msg+size, argv[i]);
		size += strlen(argv[i]) + 1;
		*(msg+size-1) = ' ';
	}
	*(msg+size-1) = '\0';


	char *env = getenv(ANSPASS_ENV);
	ret = -ENOMEM;
	info.env_path= (char*)calloc(1, sizeof(char)*strlen(env)+1);
	if (!info.env_path)
		goto no_env_path;
	strcpy(info.env_path, env);

	ret = info_check_env_path(&info, 0);
	if (ret)
	{
		printf("Error: Failed to open %s: %d\n", info.env_path, ret);
		goto env_path_dne;
	}

	info.old_path = getcwd(NULL, 0);
	if (!info.old_path)
		goto no_old_path;

	if (chdir((const char *)info.env_path) < 0)
	{
		printf("Change dir %s failed: %s\n", info.env_path, strerror(errno));
		goto chdir_fail;
	}

	env = getenv(ANSPASS_TOKEN);

	if (strlen(env) != TOKEN_LEN)
	{
		printf("Error: Token is not the expected length.\n");
		goto token_len_mismatch;
	}

	info.token = (char*)calloc(1, sizeof(char)*TOKEN_LEN+1);
	if (!info.token)
		goto no_token_mem;
	strncpy(info.token, env, TOKEN_LEN);


	ret = setup_socket(&info);
	if (ret) {
		printf("Error: Socket failed: %d\n", ret);
		goto socket_fail;
	}

	ret = -ENOTCONN;
	if (connect(info.socket, (struct sockaddr *)info.s_name, sizeof(struct
					sockaddr_un)) == -1)
	{
		ret = errno;
		printf("Error: Connection refused: %d\n", ret);
		goto not_connected;
	}

	ret = send_request(&info, QUERY, msg);
	if (ret) {
		printf("Error: Sending packet failed: %d\n", ret);
		goto send_fail;
	}

	memset(msg, 0, MAX_MSG_LENGTH);
	ret = wait_for_reply(msg);
	if (ret < 0)
	{
		printf("Error: Cannot get reply: %d\n", ret);
		goto reply_error;
	}

	printf("%s\n", msg);
	ret = 0;


reply_error:
send_fail:
	close(info.socket);
not_connected:
	free(info.s_name);
socket_fail:
	free(info.token);
no_token_mem:
token_len_mismatch:
env_path_dne:
	if(chdir((const char *)info.old_path))
		perror("chdir");
chdir_fail:
	free(info.old_path);
no_old_path:
	free(info.env_path);
no_env_path:
	free(msg);
no_q_mem:
too_big:
no_token:
no_env:
no_entry:
	return ret;
}