src/communication/security-server-comm.c
/* Create a socket and connect to Security Server */
Create a socket and connect to Security Server
Create a socket and connect to Security Server
int connect_to_server(int *fd)
{
	struct sockaddr_un clientaddr;
	int client_len = 0, localsockfd, ret, flags;
	*fd = -1;
	localsockfd = socket(AF_UNIX, SOCK_STREAM, 0);
	if(localsockfd < 0)
	{
		SEC_SVR_DBG("%s", "Error on socket()");
		return SECURITY_SERVER_ERROR_SOCKET;
	}
	if((flags = fcntl(localsockfd, F_GETFL, 0)) < 0 ||
			fcntl(localsockfd, F_SETFL, flags | O_NONBLOCK) < 0)
	{
		close(localsockfd);
		SEC_SVR_DBG("%s", "Cannot go to nonblocking mode");
		return SECURITY_SERVER_ERROR_SOCKET;
	}
	bzero(&clientaddr, sizeof(clientaddr));
	clientaddr.sun_family = AF_UNIX;
	strncpy(clientaddr.sun_path, SECURITY_SERVER_SOCK_PATH, strlen(SECURITY_SERVER_SOCK_PATH));
	clientaddr.sun_path[strlen(SECURITY_SERVER_SOCK_PATH)] = 0;
	client_len = sizeof(clientaddr);
	ret = connect(localsockfd, (struct sockaddr*)&clientaddr, client_len);
	if( ret < 0)
	{
		if(errno == EINPROGRESS)
		{
			SEC_SVR_DBG("%s", "Connection is in progress");
			ret = check_socket_poll(localsockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
			if(ret == SECURITY_SERVER_ERROR_POLL)
			{
				SEC_SVR_DBG("%s", "poll() error");
				close(localsockfd);
				return SECURITY_SERVER_ERROR_SOCKET;
			}
			if(ret == SECURITY_SERVER_ERROR_TIMEOUT)
			{
				SEC_SVR_DBG("%s", "poll() timeout");
				close(localsockfd);
				return SECURITY_SERVER_ERROR_SOCKET;
			}
			ret = connect(localsockfd, (struct sockaddr*)&clientaddr, client_len);
			if(ret < 0)
			{
				SEC_SVR_DBG("%s", "connection failed");
				close(localsockfd);
				return SECURITY_SERVER_ERROR_SOCKET;
			}
		}
		else
		{
			SEC_SVR_DBG("%s", "Connection failed");
			close(localsockfd);
			return SECURITY_SERVER_ERROR_SOCKET;
		}
	}
	ret = authenticate_server(localsockfd);
	if(ret  != SECURITY_SERVER_SUCCESS)
	{
		close(localsockfd);
		SEC_SVR_DBG("Authentication failed. %d", ret);
		return ret;
	}
	*fd = localsockfd;
	return SECURITY_SERVER_SUCCESS;
}
int connect_to_server ( int * fd ) { struct sockaddr_un clientaddr ; int client_len = 0 , localsockfd , ret , flags ; * fd = -1 ; localsockfd = socket ( AF_UNIX , SOCK_STREAM , 0 ) ; if ( localsockfd < 0 ) { SEC_SVR_DBG ( " " , " " ) ; return SECURITY_SERVER_ERROR_SOCKET ; } if ( ( flags = fcntl ( localsockfd , F_GETFL , 0 ) ) < 0 || fcntl ( localsockfd , F_SETFL , flags | O_NONBLOCK ) < 0 ) { close ( localsockfd ) ; SEC_SVR_DBG ( " " , " " ) ; return SECURITY_SERVER_ERROR_SOCKET ; } bzero ( & clientaddr , sizeof ( clientaddr ) ) ; clientaddr . sun_family = AF_UNIX ; strncpy ( clientaddr . sun_path , SECURITY_SERVER_SOCK_PATH , strlen ( SECURITY_SERVER_SOCK_PATH ) ) ; clientaddr . sun_path [ strlen ( SECURITY_SERVER_SOCK_PATH ) ] = 0 ; client_len = sizeof ( clientaddr ) ; ret = connect ( localsockfd , ( struct sockaddr * ) & clientaddr , client_len ) ; if ( ret < 0 ) { if ( errno == EINPROGRESS ) { SEC_SVR_DBG ( " " , " " ) ; ret = check_socket_poll ( localsockfd , POLLOUT , SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND ) ; if ( ret == SECURITY_SERVER_ERROR_POLL ) { SEC_SVR_DBG ( " " , " " ) ; close ( localsockfd ) ; return SECURITY_SERVER_ERROR_SOCKET ; } if ( ret == SECURITY_SERVER_ERROR_TIMEOUT ) { SEC_SVR_DBG ( " " , " " ) ; close ( localsockfd ) ; return SECURITY_SERVER_ERROR_SOCKET ; } ret = connect ( localsockfd , ( struct sockaddr * ) & clientaddr , client_len ) ; if ( ret < 0 ) { SEC_SVR_DBG ( " " , " " ) ; close ( localsockfd ) ; return SECURITY_SERVER_ERROR_SOCKET ; } } else { SEC_SVR_DBG ( " " , " " ) ; close ( localsockfd ) ; return SECURITY_SERVER_ERROR_SOCKET ; } } ret = authenticate_server ( localsockfd ) ; if ( ret != SECURITY_SERVER_SUCCESS ) { close ( localsockfd ) ; SEC_SVR_DBG ( " " , ret ) ; return ret ; } * fd = localsockfd ; return SECURITY_SERVER_SUCCESS ; }
Create a socket and connect to Security Server
Create a socket and connect to Security Server
/* Create a socket */ /* Make socket as non blocking */ /* Authenticate the peer is actually security server */