<commit_before>/* Copyright (C) 2001-2003 Tresys Technology, LLC
 * see file 'COPYING' for use and warranty information */

/*
 * Author: <EMAIL>
 */

/* policy-io.c
 *
 * Policy I/O functions 
 */
 
#include "policy.h"
#include "util.h"
#include "stdio.h"
#include "queue.h"
#include "binpol/binpol.h"
#include "policy-io.h"
#include <unistd.h>
#include <assert.h>
#ifdef APOL_PERFORM_TEST
#include <time.h>
#endif

#ifndef LIBAPOL_POLICY_INSTALL_DIR
	#define LIBAPOL_POLICY_INSTALL_DIR "/etc/security/selinux"
#endif

#ifndef LIBAPOL_SELINUX_DIR
	#define LIBAPOL_SELINUX_DIR "/selinux"
#endif

/* Defines for find_default_policy_file() function. */
#define GENERAL_ERROR	 		-1
#define POLICY_FILE_DOES_NOT_EXIST 	-2
#define POLICY_VER_FILE_DOES_NOT_EXIST	-3
#define NOT_SELINUX_AWARE		-4
#define READ_POLICY_VER_FILE_ERROR	-5

/* Error TEXT definitions for decoding the above error definitions. */
#define TEXT_POLICY_FILE_DOES_NOT_EXIST		"Policy file(s) does not exist.\n"
#define TEXT_POLICY_VER_FILE_DOES_NOT_EXIST	"Selinux policy version file does not exist.\n"
#define TEXT_NOT_SELINUX_AWARE			"This is not an selinux system.\n"
#define TEXT_READ_POLICY_VER_FILE_ERROR		"Cannot read selinux policy version file.\n"
#define TEXT_GENERAL_ERROR_TEXT			"Error in find_default_policy_file().\n"

/* externs mostly with yacc parser */
extern policy_t *parse_policy; /* parser using a global policy which we must set here */
extern unsigned int policydb_lineno;
extern queue_t id_queue;
extern FILE *yyin;
extern int yyparse(void);
extern void yyrestart(FILE *);
extern unsigned int pass;
extern int yydebug;

/* returns an error string based on a return error from seuser_label_home_dir() */
const char* decode_find_default_policy_file_err(int err)
{
	switch(err) {
	case POLICY_FILE_DOES_NOT_EXIST:
		return TEXT_POLICY_FILE_DOES_NOT_EXIST;
	case POLICY_VER_FILE_DOES_NOT_EXIST:
		return TEXT_POLICY_VER_FILE_DOES_NOT_EXIST;
	case NOT_SELINUX_AWARE:
		return TEXT_NOT_SELINUX_AWARE;
	case READ_POLICY_VER_FILE_ERROR:
		return TEXT_READ_POLICY_VER_FILE_ERROR;
	default:
		return TEXT_GENERAL_ERROR_TEXT;
	}
}

static int search_binary_policy_file(char *policy_file_path)
{
	int rt, len;
	char *version = NULL;
	char policy_version_file[BUF_SZ], buf[BUF_SZ];
	
     	/* a. Check /selinux/policyvers for the currently loaded policy version */ 
     	snprintf(policy_version_file, BUF_SZ-1, "%s/policyvers", LIBAPOL_SELINUX_DIR);
	rt = access(policy_version_file, F_OK);
	if (rt != 0) {
		return POLICY_VER_FILE_DOES_NOT_EXIST;
     	}
     	
     	/* b. Read in the loaded policy version number. */
	rt = read_file_to_buffer(policy_version_file, &version, &len);
	if (rt != 0) {
		if (version)
			free(version);
		return READ_POLICY_VER_FILE_ERROR;
	}
	/* c. See if policy.VERSION exists in the policy install directory. */
	snprintf(buf, sizeof(buf)-1, "%s/policy.%s", LIBAPOL_POLICY_INSTALL_DIR, version);
	rt = access(buf, R_OK);
	if (rt != 0) {
		return POLICY_FILE_DOES_NOT_EXIST;
     	}
	free(version);
	snprintf(policy_file_path, BUF_SZ-1, "%s", buf);
	return 0;
}

static int search_policy_src_file(char *policy_file_path)
{	
	int rt;
	
	/* Check if the default policy source file exists. */
	rt = access(LIBAPOL_DEFAULT_POLICY, F_OK);<commit_msg>fix to potential memory leak.<commit_after>/* Copyright (C) 2001-2003 Tresys Technology, LLC
 * see file 'COPYING' for use and warranty information */

/*
 * Author: <EMAIL>
 */

/* policy-io.c
 *
 * Policy I/O functions 
 */
 
#include "policy.h"
#include "util.h"
#include "stdio.h"
#include "queue.h"
#include "binpol/binpol.h"
#include "policy-io.h"
#include <unistd.h>
#include <assert.h>
#ifdef APOL_PERFORM_TEST
#include <time.h>
#endif

#ifndef LIBAPOL_POLICY_INSTALL_DIR
	#define LIBAPOL_POLICY_INSTALL_DIR "/etc/security/selinux"
#endif

#ifndef LIBAPOL_SELINUX_DIR
	#define LIBAPOL_SELINUX_DIR "/selinux"
#endif

/* Defines for find_default_policy_file() function. */
#define GENERAL_ERROR	 		-1
#define POLICY_FILE_DOES_NOT_EXIST 	-2
#define POLICY_VER_FILE_DOES_NOT_EXIST	-3
#define NOT_SELINUX_AWARE		-4
#define READ_POLICY_VER_FILE_ERROR	-5

/* Error TEXT definitions for decoding the above error definitions. */
#define TEXT_POLICY_FILE_DOES_NOT_EXIST		"Policy file(s) does not exist.\n"
#define TEXT_POLICY_VER_FILE_DOES_NOT_EXIST	"Selinux policy version file does not exist.\n"
#define TEXT_NOT_SELINUX_AWARE			"This is not an selinux system.\n"
#define TEXT_READ_POLICY_VER_FILE_ERROR		"Cannot read selinux policy version file.\n"
#define TEXT_GENERAL_ERROR_TEXT			"Error in find_default_policy_file().\n"

/* externs mostly with yacc parser */
extern policy_t *parse_policy; /* parser using a global policy which we must set here */
extern unsigned int policydb_lineno;
extern queue_t id_queue;
extern FILE *yyin;
extern int yyparse(void);
extern void yyrestart(FILE *);
extern unsigned int pass;
extern int yydebug;

/* returns an error string based on a return error from seuser_label_home_dir() */
const char* decode_find_default_policy_file_err(int err)
{
	switch(err) {
	case POLICY_FILE_DOES_NOT_EXIST:
		return TEXT_POLICY_FILE_DOES_NOT_EXIST;
	case POLICY_VER_FILE_DOES_NOT_EXIST:
		return TEXT_POLICY_VER_FILE_DOES_NOT_EXIST;
	case NOT_SELINUX_AWARE:
		return TEXT_NOT_SELINUX_AWARE;
	case READ_POLICY_VER_FILE_ERROR:
		return TEXT_READ_POLICY_VER_FILE_ERROR;
	default:
		return TEXT_GENERAL_ERROR_TEXT;
	}
}

static int search_binary_policy_file(char *policy_file_path)
{
	int rt, len;
	char *version = NULL;
	char policy_version_file[BUF_SZ], buf[BUF_SZ];
	
     	/* a. Check /selinux/policyvers for the currently loaded policy version */ 
     	snprintf(policy_version_file, BUF_SZ-1, "%s/policyvers", LIBAPOL_SELINUX_DIR);
	rt = access(policy_version_file, F_OK);
	if (rt != 0) {
		return POLICY_VER_FILE_DOES_NOT_EXIST;
     	}
     	
     	/* b. Read in the loaded policy version number. */
	rt = read_file_to_buffer(policy_version_file, &version, &len);
	if (rt != 0) {
		if (version)
			free(version);
		return READ_POLICY_VER_FILE_ERROR;
	}
	/* c. See if policy.VERSION exists in the policy install directory. */
	snprintf(buf, sizeof(buf)-1, "%s/policy.%s", LIBAPOL_POLICY_INSTALL_DIR, version);
	rt = access(buf, R_OK);
	if (rt != 0) {
		if (version)
			free(version);
		return POLICY_FILE_DOES_NOT_EXIST;
     	}
	free(version);
	snprintf(policy_file_path, BUF_SZ-1, "%s", buf);
	return 0;
}

static int search_policy_src_file(char *policy_file_path)
{	
	int rt;
	
	/* Check if the default policy source file exists. */
	rt = access(LIBAPOL_DEFAULT_POLICY, F_OK);
{'fraction_non_alphanumeric': 0.09849062357066626, 'fraction_numerical': 0.006098490623570666, 'mean_word_length': 4.996343692870201, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1047698', 'n_tokens_mistral': 2610, 'n_tokens_neox': 2470, 'n_words': 600}