/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "str.h"
#include "hex-binary.h"
#include "randgen.h"
#include "hostpid.h"
#include "eacces-error.h"
#include "safe-mkstemp.h"

#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

static int
safe_mkstemp_create(string_t *prefix, mode_t mode,
		    int (*create_func)(const char *path, mode_t mode))
{
	size_t prefix_len;
	struct stat st;
	unsigned char randbuf[8];
	int ret;

	prefix_len = str_len(prefix);
	for (;;) {
		do {
			random_fill(randbuf, sizeof(randbuf));
			str_truncate(prefix, prefix_len);
			str_append(prefix,
				   binary_to_hex(randbuf, sizeof(randbuf)));
		} while (lstat(str_c(prefix), &st) == 0);

		if (errno != ENOENT) {
			i_error("stat(%s) failed: %m", str_c(prefix));
			str_truncate(prefix, prefix_len);
			return -1;
		}

		ret = create_func(str_c(prefix), mode);
		if (ret != -1)
			break;

		if (errno != EEXIST) {
			str_truncate(prefix, prefix_len);
			return -1;
		}
	}
	return ret;
}

static int
safe_mkstemp_create_file(const char *path, mode_t mode)
{
	mode_t old_umask;
	int fd;

	old_umask = umask(0666 ^ mode);
	fd = open(path, O_RDWR | O_EXCL | O_CREAT | O_NOFOLLOW, 0666);
	umask(old_umask);

	if (fd != -1)
		return fd;
	if (errno != EEXIST && errno != ENOENT && errno != EACCES)
		i_error("open(%s) failed: %m", path);
	return -1;
}

static int
safe_mkstemp_full(string_t *prefix, mode_t mode, uid_t uid, gid_t gid,
		  const char *gid_origin)
{
	size_t prefix_len;
	int fd;

	prefix_len = str_len(prefix);
	fd = safe_mkstemp_create(prefix, mode, safe_mkstemp_create_file);
	if (fd == -1)
		return fd;
	if (uid == (uid_t)-1 && gid == (gid_t)-1)
		return fd;

	if (fchown(fd, uid, gid) < 0) {
		if (errno == EPERM) {
			i_error("%s", eperm_error_get_chgrp("fchown",
					str_c(prefix), gid, gid_origin));
		} else {
			i_error("fchown(%s, %ld, %ld) failed: %m",
				str_c(prefix),
				uid == (uid_t)-1 ? -1L : (long)uid,
				gid == (gid_t)-1 ? -1L : (long)gid);
		}
		i_close_fd(&fd);
		i_unlink(str_c(prefix));
		str_truncate(prefix, prefix_len);
		return -1;
	}
	return fd;
}

int safe_mkstemp(string_t *prefix, mode_t mode, uid_t uid, gid_t gid)
{
	return safe_mkstemp_full(prefix, mode, uid, gid, NULL);
}

int safe_mkstemp_group(string_t *prefix, mode_t mode,
		       gid_t gid, const char *gid_origin)
{
	return safe_mkstemp_full(prefix, mode, (uid_t)-1, gid, gid_origin);
}

int safe_mkstemp_hostpid(string_t *prefix, mode_t mode, uid_t uid, gid_t gid)
{
	size_t orig_prefix_len = str_len(prefix);
	int fd;

	str_printfa(prefix, "%s.%s.", my_hostname, my_pid);
	if ((fd = safe_mkstemp(prefix, mode, uid, gid)) == -1)
		str_truncate(prefix, orig_prefix_len);
	return fd;
}

int safe_mkstemp_hostpid_group(string_t *prefix, mode_t mode,
			       gid_t gid, const char *gid_origin)
{
	size_t orig_prefix_len = str_len(prefix);
	int fd;

	str_printfa(prefix, "%s.%s.", my_hostname, my_pid);
	if ((fd = safe_mkstemp_group(prefix, mode, gid, gid_origin)) == -1)
		str_truncate(prefix, orig_prefix_len);
	return fd;
}

static int
safe_mkstemp_create_dir(const char *path, mode_t mode)
{
	mode_t old_umask;
	int ret;

	old_umask = umask(0777 ^ mode);
	ret = mkdir(path, 0777);
	umask(old_umask);

	if (ret != -1)
		return ret;
	if (errno != EEXIST && errno != ENOENT && errno != EACCES)
		i_error("mkdir(%s) failed: %m", path);
	return -1;
}

int safe_mkstemp_dir(string_t *prefix, mode_t mode)
{
	return safe_mkstemp_create(prefix, mode, safe_mkstemp_create_dir);
}

int safe_mkstemp_dir_pid(string_t *prefix, mode_t mode)
{
	size_t orig_prefix_len = str_len(prefix);

	str_printfa(prefix, "%s.", my_pid);
	if (safe_mkstemp_dir(prefix, mode) == -1) {
		str_truncate(prefix, orig_prefix_len);
		return -1;
	}
	return 0;
}
