[dovecot-cvs] dovecot/src/lib Makefile.am, 1.73, 1.74 safe-mkstemp.c, NONE, 1.1 safe-mkstemp.h, NONE, 1.1

tss at dovecot.org tss at dovecot.org
Wed Apr 11 14:24:36 EEST 2007


Update of /var/lib/cvs/dovecot/src/lib
In directory talvi:/tmp/cvs-serv26212

Modified Files:
	Makefile.am 
Added Files:
	safe-mkstemp.c safe-mkstemp.h 
Log Message:
Added safe_mkstemp().



Index: Makefile.am
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/Makefile.am,v
retrieving revision 1.73
retrieving revision 1.74
diff -u -d -r1.73 -r1.74
--- Makefile.am	4 Apr 2007 08:17:03 -0000	1.73
+++ Makefile.am	11 Apr 2007 11:24:34 -0000	1.74
@@ -74,6 +74,7 @@
 	restrict-process-size.c \
 	safe-memset.c \
 	safe-mkdir.c \
+	safe-mkstemp.c \
 	sendfile-util.c \
 	seq-range-array.c \
 	sha1.c \
@@ -153,6 +154,7 @@
 	restrict-process-size.h \
 	safe-memset.h \
 	safe-mkdir.h \
+	safe-mkstemp.h \
 	sendfile-util.h \
 	seq-range-array.h \
 	sha1.h \

--- NEW FILE: safe-mkstemp.c ---
/* Copyright (c) 2007 Timo Sirainen */

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

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

int safe_mkstemp(string_t *prefix, mode_t mode, uid_t uid, gid_t gid)
{
	size_t prefix_len;
	struct stat st;
	unsigned char randbuf[8];
	int fd;

	prefix_len = str_len(prefix);
	for (;;) {
		do {
			random_fill_weak(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));
			return -1;
		}

		fd = open(str_c(prefix), O_RDWR | O_EXCL | O_CREAT, mode);
		if (fd != -1)
			break;

		if (errno != EEXIST) {
			i_error("open(%s) failed: %m", str_c(prefix));
			return -1;
		}
	}
	if (uid != (uid_t)-1 || gid != (gid_t)-1) {
		if (fchown(fd, uid, gid) < 0) {
			i_error("fchown(%s) failed: %m", str_c(prefix));
			(void)close(fd);
			(void)unlink(str_c(prefix));
			return -1;
		}
	}
	return fd;
}

int safe_mkstemp_hostpid(string_t *prefix, mode_t mode, uid_t uid, gid_t gid)
{
	str_printfa(prefix, "%s.%s.", my_hostname, my_pid);
	return safe_mkstemp(prefix, mode, uid, gid);
}

--- NEW FILE: safe-mkstemp.h ---
#ifndef __SAFE_MKSTEMP_H
#define __SAFE_MKSTEMP_H

/* Create a new file with a given prefix. The string is updated to contain the
   created filename. uid and gid can be (uid_t)-1 and (gid_t)-1 to use the
   defaults. */
int safe_mkstemp(string_t *prefix, mode_t mode, uid_t uid, gid_t gid);
/* Append host and PID to the prefix. */
int safe_mkstemp_hostpid(string_t *prefix, mode_t mode, uid_t uid, gid_t gid);

#endif



More information about the dovecot-cvs mailing list