Exemplo n.º 1
0
/* Handle syslog TCP connections */
void HandleSyslogTCP()
{
    int client_socket = 0;
    int st_errors = 0;
    int childcount = 0;
    char srcip[IPSIZE + 1];

    /* Initialize some variables */
    memset(srcip, '\0', IPSIZE + 1);

    /* Connecting to the message queue
     * Exit if it fails.
     */
    if ((logr.m_queue = StartMQ(DEFAULTQUEUE, WRITE)) < 0) {
        ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQUEUE);
    }

    while (1) {
        /* Wait for the children */
        while (childcount) {
            int wp;
            wp = waitpid((pid_t) - 1, NULL, WNOHANG);
            if (wp < 0) {
                merror(WAITPID_ERROR, ARGV0, errno, strerror(errno));
            }

            /* if = 0, we still need to wait for the child process */
            else if (wp == 0) {
                break;
            } else {
                childcount--;
            }
        }

        /* Accept new connections */
        client_socket = OS_AcceptTCP(logr.sock, srcip, IPSIZE);
        if (client_socket < 0) {
            st_errors++;
        }

        /* Check if IP is allowed here */
        if (OS_IPNotAllowed(srcip)) {
            merror(DENYIP_WARN, ARGV0, srcip);
            close(client_socket);
        }

        /* Fork to deal with new client */
        if (fork() == 0) {
            HandleClient(client_socket, srcip);
            exit(0);
        } else {
            childcount++;

            /* Close client socket, since the child is handling it */
            close(client_socket);
            continue;
        }
    }
}
Exemplo n.º 2
0
/** void HandleSyslog() v0.2
 * Handle syslog connections
 */
void HandleSyslog()
{
    char buffer[OS_SIZE_1024 +2];
    char srcip[IPSIZE +1];

    char *buffer_pt = NULL;

    int recv_b;

    struct sockaddr_storage peer_info;
    socklen_t peer_size;


    /* setting peer size */
    peer_size = sizeof(peer_info);


    /* Initializing some variables */
    memset(buffer, '\0', OS_SIZE_1024 +2);


    /* Connecting to the message queue
     * Exit if it fails.
     */
    if((logr.m_queue = StartMQ(DEFAULTQUEUE,WRITE)) < 0)
    {
        ErrorExit(QUEUE_FATAL,ARGV0, DEFAULTQUEUE);
    }


    /* Infinite loop in here */
    while(1)
    {
        /* Receiving message  */
        recv_b = recvfrom(logr.sock, buffer, OS_SIZE_1024, 0,
                (struct sockaddr *)&peer_info, &peer_size);

        /* Nothing received */
        if(recv_b <= 0)
            continue;


        /* null terminating the message */
        buffer[recv_b] = '\0';


        /* Removing new line */
        if(buffer[recv_b -1] == '\n')
        {
            buffer[recv_b -1] = '\0';
        }

        /* Setting the source ip */
        satop((struct sockaddr *) &peer_info, srcip, IPSIZE);
        srcip[IPSIZE] = '\0';


        /* Removing syslog header */
        if(buffer[0] == '<')
        {
            buffer_pt = strchr(buffer+1, '>');
            if(buffer_pt)
            {
                buffer_pt++;
            }
            else
            {
                buffer_pt = buffer;
            }
        }
        else
        {
            buffer_pt = buffer;
        }

        /* Checking if IP is allowed here */
        if(OS_IPNotAllowed(srcip))
        {
            merror(DENYIP_WARN,ARGV0,srcip);
        }

        else if(SendMSG(logr.m_queue, buffer_pt, srcip,
                        SYSLOG_MQ) < 0)
        {
            merror(QUEUE_ERROR,ARGV0,DEFAULTQUEUE, strerror(errno));
            if((logr.m_queue = StartMQ(DEFAULTQUEUE,READ)) < 0)
            {
                ErrorExit(QUEUE_FATAL,ARGV0,DEFAULTQUEUE);
            }
        }
    }
}