FreeBSD/contrib/openbsm/bin/auditdistd/receiver.c
/*
 * Function that opens trail file requested by the sender.
 * If the file already exist, it has to be the most recent file and it can
 * only be open for append.
 * If the file doesn't already exist, it has to be "older" than all existing
 * files.
 */
Function that opens trail file requested by the sender.
If the file already exist, it has to be the most recent file and it can
only be open for append.
If the file doesn't already exist, it has to be "older" than all existing
files.
Function that opens trail file requested by the sender . If the file already exist it has to be the most recent file and it can only be open for append . If the file doesn ' t already exist it has to be " older " than all existing files .
static int
receiver_open(const char *filename)
{
	int fd;
	if (adhost->adh_trail_fd != -1) {
		pjdlog_error("Sender requested opening file \"%s\" without first closing \"%s\".",
		    filename, adhost->adh_trail_name);
		return (ADIST_ERROR_WRONG_ORDER);
	}
	if (!trail_validate_name(filename, NULL)) {
		pjdlog_error("Sender wants to open file \"%s\", which has invalid name.",
		    filename);
		return (ADIST_ERROR_INVALID_NAME);
	}
	switch (trail_name_compare(filename, adhost->adh_trail_name)) {
	case TRAIL_RENAMED:
		if (!trail_is_not_terminated(adhost->adh_trail_name)) {
			pjdlog_error("Terminated trail \"%s/%s\" was unterminated on the sender as \"%s/%s\"?",
			    adhost->adh_directory, adhost->adh_trail_name,
			    adhost->adh_directory, filename);
			return (ADIST_ERROR_INVALID_NAME);
		}
		if (renameat(adhost->adh_trail_dirfd, adhost->adh_trail_name,
		    adhost->adh_trail_dirfd, filename) == -1) {
			pjdlog_errno(LOG_ERR,
			    "Unable to rename file \"%s/%s\" to \"%s/%s\"",
			    adhost->adh_directory, adhost->adh_trail_name,
			    adhost->adh_directory, filename);
			PJDLOG_ASSERT(errno > 0);
			return (ADIST_ERROR_RENAME);
		}
		pjdlog_debug(1, "Renamed file \"%s/%s\" to \"%s/%s\".",
		    adhost->adh_directory, adhost->adh_trail_name,
		    adhost->adh_directory, filename);
	case TRAIL_IDENTICAL:
		fd = openat(adhost->adh_trail_dirfd, filename,
		    O_WRONLY | O_APPEND | O_NOFOLLOW);
		if (fd == -1) {
			pjdlog_errno(LOG_ERR,
			    "Unable to open file \"%s/%s\" for append",
			    adhost->adh_directory, filename);
			PJDLOG_ASSERT(errno > 0);
			return (ADIST_ERROR_OPEN);
		}
		pjdlog_debug(1, "Opened file \"%s/%s\".",
		    adhost->adh_directory, filename);
		break;
	case TRAIL_NEWER:
		fd = openat(adhost->adh_trail_dirfd, filename,
		    O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
		if (fd == -1) {
			pjdlog_errno(LOG_ERR,
			    "Unable to create file \"%s/%s\"",
			    adhost->adh_directory, filename);
			PJDLOG_ASSERT(errno > 0);
			return (ADIST_ERROR_CREATE);
		}
		pjdlog_debug(1, "Created file \"%s/%s\".",
		    adhost->adh_directory, filename);
		break;
	case TRAIL_OLDER:
		pjdlog_error("Sender wants to open an old file \"%s\".", filename);
		return (ADIST_ERROR_OPEN_OLD);
	default:
		PJDLOG_ABORT("Unknown return value from trail_name_compare().");
	}
	PJDLOG_VERIFY(strlcpy(adhost->adh_trail_name, filename,
	    sizeof(adhost->adh_trail_name)) < sizeof(adhost->adh_trail_name));
	adhost->adh_trail_fd = fd;
	return (0);
}
static int receiver_open ( const char * filename ) { int fd ; if ( adhost -> adh_trail_fd != -1 ) { pjdlog_error ( " \" \" \" \" " , filename , adhost -> adh_trail_name ) ; return ( ADIST_ERROR_WRONG_ORDER ) ; } if ( ! trail_validate_name ( filename , NULL ) ) { pjdlog_error ( " \" \" " , filename ) ; return ( ADIST_ERROR_INVALID_NAME ) ; } switch ( trail_name_compare ( filename , adhost -> adh_trail_name ) ) { case TRAIL_RENAMED : if ( ! trail_is_not_terminated ( adhost -> adh_trail_name ) ) { pjdlog_error ( " \" \" \" \" " , adhost -> adh_directory , adhost -> adh_trail_name , adhost -> adh_directory , filename ) ; return ( ADIST_ERROR_INVALID_NAME ) ; } if ( renameat ( adhost -> adh_trail_dirfd , adhost -> adh_trail_name , adhost -> adh_trail_dirfd , filename ) == -1 ) { pjdlog_errno ( LOG_ERR , " \" \" \" \" " , adhost -> adh_directory , adhost -> adh_trail_name , adhost -> adh_directory , filename ) ; PJDLOG_ASSERT ( errno > 0 ) ; return ( ADIST_ERROR_RENAME ) ; } pjdlog_debug ( 1 , " \" \" \" \" " , adhost -> adh_directory , adhost -> adh_trail_name , adhost -> adh_directory , filename ) ; case TRAIL_IDENTICAL : fd = openat ( adhost -> adh_trail_dirfd , filename , O_WRONLY | O_APPEND | O_NOFOLLOW ) ; if ( fd == -1 ) { pjdlog_errno ( LOG_ERR , " \" \" " , adhost -> adh_directory , filename ) ; PJDLOG_ASSERT ( errno > 0 ) ; return ( ADIST_ERROR_OPEN ) ; } pjdlog_debug ( 1 , " \" \" " , adhost -> adh_directory , filename ) ; break ; case TRAIL_NEWER : fd = openat ( adhost -> adh_trail_dirfd , filename , O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW , 0600 ) ; if ( fd == -1 ) { pjdlog_errno ( LOG_ERR , " \" \" " , adhost -> adh_directory , filename ) ; PJDLOG_ASSERT ( errno > 0 ) ; return ( ADIST_ERROR_CREATE ) ; } pjdlog_debug ( 1 , " \" \" " , adhost -> adh_directory , filename ) ; break ; case TRAIL_OLDER : pjdlog_error ( " \" \" " , filename ) ; return ( ADIST_ERROR_OPEN_OLD ) ; default : PJDLOG_ABORT ( " " ) ; } PJDLOG_VERIFY ( strlcpy ( adhost -> adh_trail_name , filename , sizeof ( adhost -> adh_trail_name ) ) < sizeof ( adhost -> adh_trail_name ) ) ; adhost -> adh_trail_fd = fd ; return ( 0 ) ; }
Function that opens trail file requested by the sender.
Function that opens trail file requested by the sender .
/*
	 * Previous file should be closed by now. Sending OPEN request without
	 * sending CLOSE for the previous file is a sender bug.
	 */ /* FALLTHROUGH */ /* Opening existing file. */ /* Opening new file. */ /* Trying to open old file. */