/**
 * performs authentication
 * @param ptr authentication structure
 * @return integer 1 on success, otherwise 0
 */
int authenticate (struct auth *ptr) {
	char write_buf[GEN_BUF_SIZE];	/** socket fd write buffer */
	char read_buf[GEN_BUF_SIZE];	/** socket fd read buffer */
	FILE *sock = NULL;
	char srv_code[4];
	int result = 0;
	
	/** connect to server */
	if ((sock = srv_connect()) == NULL)
		return 0; 

	/** format authentication string */
	snprintf(
		write_buf,
		sizeof(write_buf),
		"username=%s\npassword=%s\ncommon_name=%s\nhost=%s\nport=%d\n\n",
		ptr->username,
		ptr->password,
		ptr->common_name,
		ptr->untrusted_ip,
		ptr->untrusted_port
	);

	/** send it to server and flush buffers */
	fprintf(sock, "%s", write_buf);
	fflush(sock);

	/* read response from server */
	if (! fgets(read_buf, sizeof(read_buf), sock)) {
		log_msg("No response read from authentication server: %s (errno %d)", strerror(errno), errno);
		goto outta_func;
	}
	else if (strlen(read_buf) < 3) {
		log_msg("Invalid response from server: %s", read_buf);
		goto outta_func;
	}

	/** chop result code and message */
	memset(srv_code, '\0', sizeof(srv_code));
	chomp(read_buf);
	strncpy(srv_code, read_buf, 2);

	if (strcasecmp(srv_code, "OK") != 0)
		log_msg("Authentication FAILED for user '%s': %s", ptr->username, read_buf);
	else {
		log_msg("Authentication SUCCEEDED for user '%s'", ptr->username);
		result = 1;
	}

	outta_func:

	/* close socket */
	srv_disconnect(sock);

	return result;
}
예제 #2
0
/*
 * server_create_handle -- handle a create request message
 */
static void
server_create_handle(struct server *s, const struct rpmem_msg_create_resp *resp)
{
	size_t msg_size = sizeof(struct rpmem_msg_create) +
			strlen(POOL_DESC) + 1;
	struct rpmem_msg_create *msg = MALLOC(msg_size);

	srv_accept(s);
	srv_recv(s, msg, msg_size);
	rpmem_ntoh_msg_create(msg);
	check_create_msg(msg);
	srv_send(s, resp, sizeof(*resp));
	srv_disconnect(s);

	FREE(msg);
}