FreeBSD/contrib/openbsm/libbsm/bsm_wrappers.c
/*
 * General purpose audit submission mechanism for userspace.
 */
General purpose audit submission mechanism for userspace.
General purpose audit submission mechanism for userspace .
int
audit_submit(short au_event, au_id_t auid, char status,
    int reterr, const char *fmt, ...)
{
	char text[MAX_AUDITSTRING_LEN];
	token_t *token;
	int acond;
	va_list ap;
	pid_t pid;
	int error, afd, subj_ex;
	struct auditinfo ai;
	struct auditinfo_addr aia;
	au_tid_t atid;
	if (audit_get_cond(&acond) != 0) {
		if (errno == ENOSYS)
			return (0);
		error = errno;
		syslog(LOG_AUTH | LOG_ERR, "audit: auditon failed: %s",
		    strerror(errno));
		errno = error;
		return (-1);
	}
	if (acond == AUC_NOAUDIT)
		return (0);
	afd = au_open();
	if (afd < 0) {
		error = errno;
		syslog(LOG_AUTH | LOG_ERR, "audit: au_open failed: %s",
		    strerror(errno));
		errno = error;
		return (-1);
	}
	subj_ex = 0;
	error = getaudit_addr(&aia, sizeof(aia));
	if (error < 0 && errno == ENOSYS) {
		error = getaudit(&ai);
		if (error < 0) {
			error = errno;
			syslog(LOG_AUTH | LOG_ERR, "audit: getaudit failed: %s",
			    strerror(errno));
			errno = error;
			return (-1);
		}
		aia.ai_auid = ai.ai_auid;
		aia.ai_mask = ai.ai_mask;
		aia.ai_asid = ai.ai_asid;
		aia.ai_termid.at_type = AU_IPv4;
		aia.ai_termid.at_addr[0] = ai.ai_termid.machine;
		aia.ai_termid.at_port = ai.ai_termid.port;
	} else if (error < 0) {
		error = errno;
		syslog(LOG_AUTH | LOG_ERR, "audit: getaudit_addr failed: %s",
		    strerror(errno));
		errno = error;
		return (-1);
	}
	if (aia.ai_termid.at_type == AU_IPv6)
		subj_ex = 1;
	pid = getpid();
	if (subj_ex == 0) {
		atid.port = aia.ai_termid.at_port;
		atid.machine = aia.ai_termid.at_addr[0];
		token = au_to_subject32(auid, geteuid(), getegid(),
		    getuid(), getgid(), pid, pid, &atid);
	} else
		token = au_to_subject_ex(auid, geteuid(), getegid(),
		    getuid(), getgid(), pid, pid, &aia.ai_termid);
	if (token == NULL) {
		syslog(LOG_AUTH | LOG_ERR,
		    "audit: unable to build subject token");
		(void) au_close(afd, AU_TO_NO_WRITE, au_event);
		errno = EPERM;
		return (-1);
	}
	if (au_write(afd, token) < 0) {
		error = errno;
		syslog(LOG_AUTH | LOG_ERR,
		    "audit: au_write failed: %s", strerror(errno));
		(void) au_close(afd, AU_TO_NO_WRITE, au_event);
		errno = error;
		return (-1);
	}
	if (fmt != NULL) {
		va_start(ap, fmt);
		(void) vsnprintf(text, MAX_AUDITSTRING_LEN, fmt, ap);
		va_end(ap);
		token = au_to_text(text);
		if (token == NULL) {
			syslog(LOG_AUTH | LOG_ERR,
			    "audit: failed to generate text token");
			(void) au_close(afd, AU_TO_NO_WRITE, au_event);
			errno = EPERM;
			return (-1);
		}
		if (au_write(afd, token) < 0) {
			error = errno;
			syslog(LOG_AUTH | LOG_ERR,
			    "audit: au_write failed: %s", strerror(errno));
			(void) au_close(afd, AU_TO_NO_WRITE, au_event);
			errno = error;
			return (-1);
		}
	}
	token = au_to_return32(au_errno_to_bsm(status), reterr);
	if (token == NULL) {
		syslog(LOG_AUTH | LOG_ERR,
		    "audit: unable to build return token");
		(void) au_close(afd, AU_TO_NO_WRITE, au_event);
		errno = EPERM;
		return (-1);
	}
	if (au_write(afd, token) < 0) {
		error = errno;
		syslog(LOG_AUTH | LOG_ERR,
		    "audit: au_write failed: %s", strerror(errno));
		(void) au_close(afd, AU_TO_NO_WRITE, au_event);
		errno = error;
		return (-1);
	}
	if (au_close(afd, AU_TO_WRITE, au_event) < 0) {
		error = errno;
		syslog(LOG_AUTH | LOG_ERR, "audit: record not committed");
		errno = error;
		return (-1);
	}
	return (0);
}
int audit_submit ( short au_event , au_id_t auid , char status , int reterr , const char * fmt , ... ) { char text [ MAX_AUDITSTRING_LEN ] ; token_t * token ; int acond ; va_list ap ; pid_t pid ; int error , afd , subj_ex ; struct auditinfo ai ; struct auditinfo_addr aia ; au_tid_t atid ; if ( audit_get_cond ( & acond ) != 0 ) { if ( errno == ENOSYS ) return ( 0 ) ; error = errno ; syslog ( LOG_AUTH | LOG_ERR , " " , strerror ( errno ) ) ; errno = error ; return ( -1 ) ; } if ( acond == AUC_NOAUDIT ) return ( 0 ) ; afd = au_open ( ) ; if ( afd < 0 ) { error = errno ; syslog ( LOG_AUTH | LOG_ERR , " " , strerror ( errno ) ) ; errno = error ; return ( -1 ) ; } subj_ex = 0 ; error = getaudit_addr ( & aia , sizeof ( aia ) ) ; if ( error < 0 && errno == ENOSYS ) { error = getaudit ( & ai ) ; if ( error < 0 ) { error = errno ; syslog ( LOG_AUTH | LOG_ERR , " " , strerror ( errno ) ) ; errno = error ; return ( -1 ) ; } aia . ai_auid = ai . ai_auid ; aia . ai_mask = ai . ai_mask ; aia . ai_asid = ai . ai_asid ; aia . ai_termid . at_type = AU_IPv4 ; aia . ai_termid . at_addr [ 0 ] = ai . ai_termid . machine ; aia . ai_termid . at_port = ai . ai_termid . port ; } else if ( error < 0 ) { error = errno ; syslog ( LOG_AUTH | LOG_ERR , " " , strerror ( errno ) ) ; errno = error ; return ( -1 ) ; } if ( aia . ai_termid . at_type == AU_IPv6 ) subj_ex = 1 ; pid = getpid ( ) ; if ( subj_ex == 0 ) { atid . port = aia . ai_termid . at_port ; atid . machine = aia . ai_termid . at_addr [ 0 ] ; token = au_to_subject32 ( auid , geteuid ( ) , getegid ( ) , getuid ( ) , getgid ( ) , pid , pid , & atid ) ; } else token = au_to_subject_ex ( auid , geteuid ( ) , getegid ( ) , getuid ( ) , getgid ( ) , pid , pid , & aia . ai_termid ) ; if ( token == NULL ) { syslog ( LOG_AUTH | LOG_ERR , " " ) ; ( void ) au_close ( afd , AU_TO_NO_WRITE , au_event ) ; errno = EPERM ; return ( -1 ) ; } if ( au_write ( afd , token ) < 0 ) { error = errno ; syslog ( LOG_AUTH | LOG_ERR , " " , strerror ( errno ) ) ; ( void ) au_close ( afd , AU_TO_NO_WRITE , au_event ) ; errno = error ; return ( -1 ) ; } if ( fmt != NULL ) { va_start ( ap , fmt ) ; ( void ) vsnprintf ( text , MAX_AUDITSTRING_LEN , fmt , ap ) ; va_end ( ap ) ; token = au_to_text ( text ) ; if ( token == NULL ) { syslog ( LOG_AUTH | LOG_ERR , " " ) ; ( void ) au_close ( afd , AU_TO_NO_WRITE , au_event ) ; errno = EPERM ; return ( -1 ) ; } if ( au_write ( afd , token ) < 0 ) { error = errno ; syslog ( LOG_AUTH | LOG_ERR , " " , strerror ( errno ) ) ; ( void ) au_close ( afd , AU_TO_NO_WRITE , au_event ) ; errno = error ; return ( -1 ) ; } } token = au_to_return32 ( au_errno_to_bsm ( status ) , reterr ) ; if ( token == NULL ) { syslog ( LOG_AUTH | LOG_ERR , " " ) ; ( void ) au_close ( afd , AU_TO_NO_WRITE , au_event ) ; errno = EPERM ; return ( -1 ) ; } if ( au_write ( afd , token ) < 0 ) { error = errno ; syslog ( LOG_AUTH | LOG_ERR , " " , strerror ( errno ) ) ; ( void ) au_close ( afd , AU_TO_NO_WRITE , au_event ) ; errno = error ; return ( -1 ) ; } if ( au_close ( afd , AU_TO_WRITE , au_event ) < 0 ) { error = errno ; syslog ( LOG_AUTH | LOG_ERR , " " ) ; errno = error ; return ( -1 ) ; } return ( 0 ) ; }
General purpose audit submission mechanism for userspace.
General purpose audit submission mechanism for userspace .
/*
		 * If auditon(2) returns ENOSYS, then audit has not been
		 * compiled into the kernel, so just return.
		 */ /*
	 * Try to use getaudit_addr(2) first.  If this kernel does not support
	 * it, then fall back on to getaudit(2).
	 */ /*
		 * Convert this auditinfo_t to an auditinfo_addr_t to make the
		 * following code less complicated wrt to preselection and
		 * subject token generation.
		 */ /*
	 * NB: We should be performing pre-selection here now that we have the
	 * masks for this process.
	 */