<commit_before>/* Copyright (c) 2002-2012 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "hostpid.h"

#include <unistd.h>
#include <netdb.h>

const char *my_hostname = NULL;
const char *my_pid = NULL;

static char *my_domain = NULL;

void hostpid_init(void)
{
	static char hostname[256], pid[MAX_INT_STRLEN];

	if (gethostname(hostname, sizeof(hostname)-1) == -1) {
		if (i_strocpy(hostname, "unknown", sizeof(hostname)) < 0)
			i_unreached();
	}
	hostname[sizeof(hostname)-1] = '\0';
	my_hostname = hostname;

	if (strchr(hostname, '/') != NULL)
		i_fatal("Invalid system hostname: %s", hostname);

	/* allow calling hostpid_init() multiple times to reset hostname */
	i_free_and_null(my_domain);

	i_snprintf(pid, sizeof(pid), "%lld", (long long)getpid());
	my_pid = pid;
}

const char *my_hostdomain(void)
{
	struct hostent *hent;<commit_msg>hostpid_init(): Don't allow gethostname() to fail. Be more strict about what chars it can contain.<commit_after>/* Copyright (c) 2002-2012 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "hostpid.h"

#include <unistd.h>
#include <netdb.h>

#define HOSTNAME_DISALLOWED_CHARS "/\r\n\t"

const char *my_hostname = NULL;
const char *my_pid = NULL;

static char *my_domain = NULL;

void hostpid_init(void)
{
	static char hostname[256], pid[MAX_INT_STRLEN];

	if (gethostname(hostname, sizeof(hostname)-1) == -1)
		i_fatal("gethostname() failed: %m");
	hostname[sizeof(hostname)-1] = '\0';
	my_hostname = hostname;

	if (strcspn(hostname, HOSTNAME_DISALLOWED_CHARS) != strlen(hostname))
		i_fatal("Invalid system hostname: %s", hostname);

	/* allow calling hostpid_init() multiple times to reset hostname */
	i_free_and_null(my_domain);

	i_snprintf(pid, sizeof(pid), "%lld", (long long)getpid());
	my_pid = pid;
}

const char *my_hostdomain(void)
{
	struct hostent *hent;
{'fraction_non_alphanumeric': 0.14804917156600747, 'fraction_numerical': 0.016568679850347406, 'mean_word_length': 5.36734693877551, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, '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': '4180828', 'n_tokens_mistral': 724, 'n_tokens_neox': 711, 'n_words': 176}