@@ -46,6 +46,8 @@ static int debug = 0;
4646
4747static time_t eoe_timeout = EOE_TIMEOUT ;
4848
49+ static int au_auparse_next_event (auparse_state_t * au , int * malformed );
50+
4951/* like strchr except string is delimited by length, not null byte */
5052static char * strnchr (const char * s , int c , size_t n )
5153{
@@ -603,19 +605,60 @@ void auparse_add_callback(auparse_state_t *au, auparse_callback_ptr callback,
603605 au -> callback_user_data_destroy = user_destroy_func ;
604606}
605607
606- static void consume_feed (auparse_state_t * au , int flush )
608+ /* validate_feed - check whether @au is ready to consume feed data
609+ * @au: parser state machine to validate
610+ *
611+ * Return: 0 on success, -1 on failure with errno set.
612+ */
613+ static int validate_feed (const auparse_state_t * au )
614+ {
615+ if (au == NULL || au -> source != AUSOURCE_FEED ||
616+ au -> callback == NULL ) {
617+ errno = EINVAL ;
618+ return -1 ;
619+ }
620+
621+ return 0 ;
622+ }
623+
624+ /* consume_feed - process buffered feed data and optionally flush events
625+ * @au: parser state machine
626+ * @flush: non-zero to terminate and deliver outstanding events
627+ *
628+ * Return: 0 on success, -1 on failure with errno set.
629+ */
630+ static int consume_feed (auparse_state_t * au , int flush )
607631{
632+ int rc , malformed , saved_errno = 0 ;
633+
608634 //if (debug) printf("consume feed, flush %d\n", flush);
609- while (auparse_next_event (au ) > 0 ) {
610- if (au -> callback ) {
635+ do {
636+ /*
637+ * This calls the lower-level parser so malformed feed
638+ * records can be reported while preserving auparse_next_event()
639+ * normalizer state handling.
640+ */
641+ malformed = 0 ;
642+ clear_normalizer (& au -> norm_data );
643+ rc = au_auparse_next_event (au , & malformed );
644+ if (malformed && saved_errno == 0 )
645+ saved_errno = EBADMSG ;
646+
647+ if (rc > 0 )
611648 (* au -> callback )(au , AUPARSE_CB_EVENT_READY ,
612649 au -> callback_user_data );
613- }
650+ } while (rc > 0 );
651+
652+ if (rc < 0 ) {
653+ if (errno == 0 )
654+ errno = EIO ;
655+ return -1 ;
614656 }
657+
615658 if (flush ) {
616659 /* Terminate all outstanding events, as we are at end of input
617- * (ie mark BUILDING events as COMPLETE events) then if we
618- * have a callback execute the callback on each event
660+ * (ie mark BUILDING events as COMPLETE events) then execute
661+ * the callback on each event
619662 */
620663 event_list_t * l ;
621664
@@ -630,12 +673,17 @@ static void consume_feed(auparse_state_t *au, int flush)
630673 load_interpretation_list (au , r -> interp );
631674 aup_list_first_field (l );
632675
633- if (au -> callback ) {
634- (* au -> callback )(au , AUPARSE_CB_EVENT_READY ,
676+ (* au -> callback )(au , AUPARSE_CB_EVENT_READY ,
635677 au -> callback_user_data );
636- }
637678 }
638679 }
680+
681+ if (saved_errno ) {
682+ errno = saved_errno ;
683+ return -1 ;
684+ }
685+
686+ return 0 ;
639687}
640688
641689int auparse_new_buffer (auparse_state_t * au , const char * data , size_t data_len )
@@ -651,18 +699,39 @@ int auparse_new_buffer(auparse_state_t *au, const char *data, size_t data_len)
651699 return 0 ;
652700}
653701
702+ /* auparse_feed - append data to the feed parser and consume what is ready
703+ * @au: parser state machine
704+ * @data: feed data to append
705+ * @data_len: length of @data
706+ *
707+ * Return: 0 on success, -1 on failure with errno set.
708+ */
654709int auparse_feed (auparse_state_t * au , const char * data , size_t data_len )
655710{
656- if (databuf_append (& au -> databuf , data , data_len ) < 0 )
711+ if (data == NULL && data_len != 0 ) {
712+ errno = EINVAL ;
657713 return -1 ;
658- consume_feed (au , 0 );
659- return 0 ;
714+ }
715+ if (validate_feed (au ) < 0 )
716+ return -1 ;
717+ if (databuf_append (& au -> databuf , data , data_len ) < 0 ) {
718+ if (errno == 0 )
719+ errno = ENOMEM ;
720+ return -1 ;
721+ }
722+ return consume_feed (au , 0 );
660723}
661724
725+ /* auparse_flush_feed - flush pending feed data through the parser
726+ * @au: parser state machine
727+ *
728+ * Return: 0 on success, -1 on failure with errno set.
729+ */
662730int auparse_flush_feed (auparse_state_t * au )
663731{
664- consume_feed (au , 1 );
665- return 0 ;
732+ if (validate_feed (au ) < 0 )
733+ return -1 ;
734+ return consume_feed (au , 1 );
666735}
667736
668737// If there is any data in the state machine, return 1.
@@ -696,11 +765,21 @@ int auparse_feed_has_ready_event(auparse_state_t *au)
696765 return 0 ;
697766}
698767
768+ /* auparse_feed_age_events - complete feed events aged out by the clock
769+ * @au: parser state machine
770+ *
771+ * Return: none.
772+ */
699773void auparse_feed_age_events (auparse_state_t * au )
700774{
701- time_t t = time (NULL );
775+ time_t t ;
776+
777+ if (validate_feed (au ) < 0 )
778+ return ;
779+
780+ t = time (NULL );
702781 au_check_events (au , t );
703- consume_feed (au , 0 );
782+ ( void ) consume_feed (au , 0 );
704783}
705784
706785void auparse_set_escape_mode (auparse_state_t * au , auparse_esc_t mode )
@@ -1476,16 +1555,14 @@ int ausearch_next_event(auparse_state_t *au)
14761555 return 0 ;
14771556}
14781557
1479- /*
1480- * au_auparse_next_event - Get the next complete event
1481- * Args:
1482- * au - the parser state machine
1483- * Rtns:
1484- * < 0 - error
1485- * == 0 - no data
1486- * > 0 - we have an event and it's set to the 'current event' au->le
1558+ /* au_auparse_next_event - get the next complete event
1559+ * @au: parser state machine
1560+ * @malformed: optional flag set when a malformed record is skipped
1561+ *
1562+ * Return: less than 0 on error, 0 if no data is available, or greater
1563+ * than 0 if an event is available and set as the current event.
14871564 */
1488- static int au_auparse_next_event (auparse_state_t * au )
1565+ static int au_auparse_next_event (auparse_state_t * au , int * malformed )
14891566{
14901567 int rc , i , built ;
14911568 event_list_t * l ;
@@ -1621,6 +1698,8 @@ static int au_auparse_next_event(auparse_state_t *au)
16211698 if (debug )
16221699 printf ("Malformed line:%s\n" , au -> cur_buf );
16231700#endif /* LOL_EVENTS_DEBUG01 */
1701+ if (malformed )
1702+ * malformed = 1 ;
16241703 continue ;
16251704 }
16261705
@@ -1716,7 +1795,7 @@ static int au_auparse_next_event(auparse_state_t *au)
17161795int auparse_next_event (auparse_state_t * au )
17171796{
17181797 clear_normalizer (& au -> norm_data );
1719- return au_auparse_next_event (au );
1798+ return au_auparse_next_event (au , NULL );
17201799}
17211800
17221801/* Accessors to event data */
0 commit comments