Exemple #1
0
boolean nx_syslog_parse_rfc3164(nx_logdata_t *logdata,
				const char *string,
				size_t stringlen)
{
    boolean retval = TRUE;
    const char *ptr, *hoststart = NULL, *hostend = NULL;
    const char *appstart = NULL, *append = NULL;
    const char *msgstart = NULL, *msgend = NULL;
    const char *pidstart = NULL, *pidend = NULL;
    apr_time_t date;
    boolean got_date = FALSE;
    boolean got_pri = FALSE;

    ASSERT(logdata != NULL);
    ASSERT(string != NULL);

    if ( stringlen <= 0 )
    {
	stringlen = strlen(string);
    }

    msgend = string + stringlen;
    ptr = parse_syslog_priority(string, logdata, &retval);
    got_pri = retval;
    msgstart = ptr;
    
    if ( nx_date_parse(&date, ptr, &ptr) != APR_SUCCESS )
    {
	nx_logdata_set_datetime(logdata, "EventTime", apr_time_now());
	for ( appstart = ptr; IS_TAGCHAR(*ptr); ptr++ );
	append = ptr;
    }
    else
    { // date ok
	got_date = TRUE;
	for ( ; *ptr == ' '; ptr++ ); // skip space
	msgstart = ptr;
	for ( hoststart = ptr; IS_HOSTCHAR(*ptr); ptr++ );

	if ( hoststart == ptr )
	{ //no host
	    hoststart = NULL;
	}
	else if ( *ptr == '\0' )
	{ // line ends after host
	    msgstart = NULL;
	    hostend = ptr;
	}
	else if ( *ptr == '[' )
	{ //app instead of host
	    hoststart = NULL;
	    appstart = msgstart;
	}
	else if ( (*ptr != ' ') && (*ptr != ':') )
	{
	    msgstart = hoststart;
	    hoststart = NULL;
	}
	else
	{ // got host
	    hostend = ptr;
	    msgstart = ptr;
	    
	    if ( *ptr == ':' )
	    { // no host
		appstart = hoststart;
		hoststart = NULL;
		append = hostend;
		hostend = NULL;
	    }
	    else
	    {
		for ( ; *ptr == ' '; ptr++ ); // skip space
		for ( appstart = ptr; IS_TAGCHAR(*ptr); ptr++ );
		msgstart = ptr;
	    }
	}
    }

    if ( (got_date == TRUE) || (got_pri = TRUE) )
    {
	if ( (appstart != NULL) && (*appstart == '[') )
	{
	    appstart = NULL;
	}
	else if ( appstart == NULL )
	{
	    // ignore pid
	}
	else if ( *ptr == '[' )
	{ // pid
	    append = ptr;
	    
	    ptr++;
	    pidstart = ptr;
	    for ( ; *ptr != '\0'; ptr++ )
	    {
		if ( (*ptr == ']') || (*ptr == ' ') )
		{
		    break;
		}
	    }
	    
	    msgstart = appstart;
	    
	    if ( *ptr == ']' )
	    {
		pidend = ptr;
		ptr++;
	    }
	    else
	    {
		pidend = NULL;
	    }

	    if ( *ptr == ':' )
	    {
		ptr++;
	    }
	    else
	    {
		pidend = NULL;
	    }
	    if ( *ptr == ' ' )
	    {
		ptr++;
	    }
	    if ( pidend == NULL )
	    {
		appstart = NULL;
		append = NULL;
	    }
	    if ( appstart != NULL )
	    {
		msgstart = ptr;
	    }
	}
	else if ( *ptr == ':')
	{
	    append = ptr;
	    ptr++;
	    if ( *ptr == ' ' )
	    {
		ptr++;
	    }
	    if ( appstart != NULL )
	    {
		msgstart = ptr;
	    }
	}
	else
	{
	    msgstart = appstart;
	    appstart = NULL;
	}
    }
    else
    {
	appstart = NULL;
    }

    set_syslog_hostname(logdata, hoststart, hostend);
    if ( got_date == TRUE )
    {
	nx_date_fix_year(&date);
	set_syslog_timestamp(logdata, date);
    }
    set_syslog_appname(logdata, appstart, append);
    set_syslog_procid(logdata, pidstart, pidend);
    set_syslog_message(logdata, msgstart, msgend);

    return ( retval );
}
Exemple #2
0
// PRI VERSION SP TIMESTAMP SP HOSTNAME SP APP-NAME SP PROCID SP MSGID SP STRUCTURED-DATA [SP MSG]
boolean nx_syslog_parse_rfc5424(nx_logdata_t *logdata,
				const char *string,
				size_t stringlen)
{
    boolean retval = TRUE;
    const char *ptr, *hoststart = NULL, *hostend = NULL;
    const char *appstart = NULL, *append = NULL;
    const char *msgstart = NULL, *msgend = NULL;
    const char *procidstart = NULL, *procidend = NULL;
    const char *msgidstart = NULL, *msgidend = NULL;
    apr_time_t date = 0;

    ASSERT(logdata != NULL);
    ASSERT(string != NULL);

    if ( stringlen <= 0 )
    {
	stringlen = strlen(string);
    }

    msgend = string + stringlen;
    // PRIORITY
    ptr = parse_syslog_priority(string, logdata, &retval);
    if ( (ptr[0] == '1') && (ptr[1] == ' ') )
    { // VERSION
	ptr += 2;
    }
    else
    { // fall back to bsd syslog
	return ( nx_syslog_parse_rfc3164(logdata, string, stringlen) );
    }
    msgstart = ptr;

    // TIMESTAMP
    if ( IS_NILVALUE(ptr) )
    {
	nx_logdata_set_datetime(logdata, "EventTime", apr_time_now());
	ptr++;
    }
    else
    {
	if ( nx_date_parse_iso(&date, ptr, &ptr) != APR_SUCCESS )
	{
	    nx_logdata_set_datetime(logdata, "EventTime", apr_time_now());
	    set_syslog_hostname(logdata, NULL, NULL);
	    set_syslog_message(logdata, msgstart, msgend);
	    return ( FALSE );
	}
	set_syslog_timestamp(logdata, date);
    }
    for ( ; *ptr == ' '; ptr++ ); // skip space

    // HOSTNAME
    if ( IS_NILVALUE(ptr) )
    {
	ptr++;
	set_syslog_hostname(logdata, NULL, NULL);
    }
    else
    {
	hoststart = ptr;
	for ( ; (*ptr != ' ') && (*ptr != '\0'); ptr++ );
	hostend = ptr;
	set_syslog_hostname(logdata, hoststart, hostend);
    }
    for ( ; *ptr == ' '; ptr++ ); // skip space

    // APP-NAME
    if ( IS_NILVALUE(ptr) )
    {
	ptr++;
    }
    else
    {
	appstart = ptr;
	for ( ; (*ptr != ' ') && (*ptr != '\0'); ptr++ );
	append = ptr;
	set_syslog_appname(logdata, appstart, append);
    }
    for ( ; *ptr == ' '; ptr++ ); // skip space

    // PROCID
    if ( IS_NILVALUE(ptr) )
    {
	ptr++;
    }
    else
    {
	procidstart = ptr;
	for ( ; (*ptr != ' ') && (*ptr != '\0'); ptr++ );
	procidend = ptr;
	set_syslog_procid(logdata, procidstart, procidend);
    }
    for ( ; *ptr == ' '; ptr++ ); // skip space

    // MSGID
    if ( IS_NILVALUE(ptr) )
    {
	ptr++;
    }
    else
    {
	msgidstart = ptr;
	for ( ; (*ptr != ' ') && (*ptr != '\0'); ptr++ );
	msgidend = ptr;
	set_syslog_msgid(logdata, msgidstart, msgidend);
    }
    for ( ; *ptr == ' '; ptr++ ); // skip space

    // STRUCTURED-DATA
    if ( IS_NILVALUE(ptr) )
    {
	ptr++;
    }
    else
    {
	ptr = syslog_parse_structured_data(logdata, ptr);
    }
    if ( *ptr == ' ' ) ptr++; // skip space

    // MESSAGE
    if ( (ptr[0] == 0xEF) && (ptr[0] == 0xBB) && (ptr[0] == 0xBF) )
    { //Skip UTF8 BOM
	ptr += 3;
    }
    msgstart = ptr;
    set_syslog_message(logdata, msgstart, msgend);

    return ( retval );
}
Exemple #3
0
static int write_message(Server *s, const char *buf, struct ucred *ucred) {
        ssize_t k;
        char priority[6], pid[16];
        struct iovec iovec[5];
        unsigned i = 0;
        char *process = NULL;
        int r = 0;
        int prio = LOG_USER | LOG_INFO;

        assert(s);
        assert(buf);

        parse_syslog_priority((char**) &buf, &prio);

        if (*buf == 0)
                return 0;

        if ((prio & LOG_FACMASK) == 0)
                prio = LOG_USER | LOG_PRI(prio);

        /* First, set priority field */
        snprintf(priority, sizeof(priority), "<%i>", prio);
        char_array_0(priority);
        IOVEC_SET_STRING(iovec[i++], priority);

        /* Second, skip date */
        skip_date(&buf);

        /* Then, add process if set */
        if (read_process(&buf, &iovec[i]) > 0)
                i++;
        else if (ucred &&
                 ucred->pid > 0 &&
                 get_process_name(ucred->pid, &process) >= 0)
                IOVEC_SET_STRING(iovec[i++], process);

        /* Skip the stored PID if we have a better one */
        if (ucred) {
                snprintf(pid, sizeof(pid), "[%lu]: ", (unsigned long) ucred->pid);
                char_array_0(pid);
                IOVEC_SET_STRING(iovec[i++], pid);

                skip_pid(&buf);

                if (*buf == ':')
                        buf++;

                buf += strspn(buf, WHITESPACE);
        }

        /* Is the remaining message empty? */
        if (*buf) {

                /* And the rest is the message */
                IOVEC_SET_STRING(iovec[i++], buf);
                IOVEC_SET_STRING(iovec[i++], "\n");

                if ((k = writev(s->kmsg_fd, iovec, i)) <= 0) {
                        log_error("Failed to write log message to kmsg: %s", k < 0 ? strerror(errno) : "short write");
                        r = k < 0 ? -errno : -EIO;
                }
        }

        free(process);

        return r;
}