5a3797e
diff -up dhcp-4.2.0/client/clparse.c.cloexec dhcp-4.2.0/client/clparse.c
5a3797e
--- dhcp-4.2.0/client/clparse.c.cloexec	2010-07-21 13:55:42.000000000 +0200
5a3797e
+++ dhcp-4.2.0/client/clparse.c	2010-07-21 14:31:13.000000000 +0200
5a3797e
@@ -211,7 +211,7 @@ int read_client_conf_file (const char *n
e2381ff
 	int token;
e2381ff
 	isc_result_t status;
e2381ff
 	
e2381ff
-	if ((file = open (name, O_RDONLY)) < 0)
e2381ff
+	if ((file = open (name, O_RDONLY | O_CLOEXEC)) < 0)
e2381ff
 		return uerr2isc (errno);
e2381ff
 
4bf24c8
 	cfile = NULL;
5a3797e
@@ -248,7 +248,7 @@ void read_client_leases ()
e2381ff
 
e2381ff
 	/* Open the lease file.   If we can't open it, just return -
e2381ff
 	   we can safely trust the server to remember our state. */
e2381ff
-	if ((file = open (path_dhclient_db, O_RDONLY)) < 0)
e2381ff
+	if ((file = open (path_dhclient_db, O_RDONLY | O_CLOEXEC)) < 0)
e2381ff
 		return;
4bf24c8
 
4bf24c8
 	cfile = NULL;
5a3797e
diff -up dhcp-4.2.0/client/dhclient.c.cloexec dhcp-4.2.0/client/dhclient.c
5a3797e
--- dhcp-4.2.0/client/dhclient.c.cloexec	2010-07-21 13:55:42.000000000 +0200
5a3797e
+++ dhcp-4.2.0/client/dhclient.c	2010-07-21 14:33:44.000000000 +0200
9538db2
@@ -137,11 +137,11 @@ main(int argc, char **argv) {
4bf24c8
 	/* Make sure that file descriptors 0 (stdin), 1, (stdout), and
4bf24c8
 	   2 (stderr) are open. To do this, we assume that when we
4bf24c8
 	   open a file the lowest available file descriptor is used. */
4bf24c8
-	fd = open("/dev/null", O_RDWR);
4bf24c8
+	fd = open("/dev/null", O_RDWR | O_CLOEXEC);
4bf24c8
 	if (fd == 0)
4bf24c8
-		fd = open("/dev/null", O_RDWR);
4bf24c8
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
4bf24c8
 	if (fd == 1)
4bf24c8
-		fd = open("/dev/null", O_RDWR);
4bf24c8
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
4bf24c8
 	if (fd == 2)
4bf24c8
 		log_perror = 0; /* No sense logging to /dev/null. */
4bf24c8
 	else if (fd != -1)
5a3797e
@@ -488,7 +488,7 @@ main(int argc, char **argv) {
e2381ff
 		int e;
e2381ff
 
e2381ff
 		oldpid = 0;
e2381ff
-		if ((pidfd = fopen(path_dhclient_pid, "r")) != NULL) {
e2381ff
+		if ((pidfd = fopen(path_dhclient_pid, "re")) != NULL) {
e2381ff
 			e = fscanf(pidfd, "%ld\n", &temp);
e2381ff
 			oldpid = (pid_t)temp;
e2381ff
 
5a3797e
@@ -530,7 +530,7 @@ main(int argc, char **argv) {
e2381ff
 					strncpy(new_path_dhclient_pid, path_dhclient_pid, pfx);
e2381ff
 					sprintf(new_path_dhclient_pid + pfx, "-%s.pid", ip->name);
e2381ff
 
e2381ff
-					if ((pidfd = fopen(new_path_dhclient_pid, "r")) != NULL) {
e2381ff
+					if ((pidfd = fopen(new_path_dhclient_pid, "re")) != NULL) {
e2381ff
 						e = fscanf(pidfd, "%ld\n", &temp);
e2381ff
 						oldpid = (pid_t)temp;
e2381ff
 
5a3797e
@@ -555,7 +555,7 @@ main(int argc, char **argv) {
e2381ff
 		int dhc_running = 0;
e2381ff
 		char procfn[256] = "";
e2381ff
 
e2381ff
-		if ((pidfp = fopen(path_dhclient_pid, "r")) != NULL) {
e2381ff
+		if ((pidfp = fopen(path_dhclient_pid, "re")) != NULL) {
e2381ff
 			if ((fscanf(pidfp, "%ld", &temp)==1) && ((dhcpid=(pid_t)temp) > 0)) {
e2381ff
 				snprintf(procfn,256,"/proc/%u",dhcpid);
4bf24c8
 				dhc_running = (access(procfn, F_OK) == 0);
5a3797e
@@ -2931,7 +2931,7 @@ void rewrite_client_leases ()
e2381ff
 
e2381ff
 	if (leaseFile != NULL)
e2381ff
 		fclose (leaseFile);
e2381ff
-	leaseFile = fopen (path_dhclient_db, "w");
e2381ff
+	leaseFile = fopen (path_dhclient_db, "we");
e2381ff
 	if (leaseFile == NULL) {
e2381ff
 		log_error ("can't create %s: %m", path_dhclient_db);
e2381ff
 		return;
5a3797e
@@ -3035,7 +3035,7 @@ write_duid(struct data_string *duid)
5a3797e
 		return DHCP_R_INVALIDARG;
e2381ff
 
e2381ff
 	if (leaseFile == NULL) {	/* XXX? */
e2381ff
-		leaseFile = fopen(path_dhclient_db, "w");
e2381ff
+		leaseFile = fopen(path_dhclient_db, "we");
e2381ff
 		if (leaseFile == NULL) {
e2381ff
 			log_error("can't create %s: %m", path_dhclient_db);
e2381ff
 			return ISC_R_IOERROR;
5a3797e
@@ -3215,7 +3215,7 @@ int write_client_lease (client, lease, r
e2381ff
 		return 1;
e2381ff
 
e2381ff
 	if (leaseFile == NULL) {	/* XXX */
e2381ff
-		leaseFile = fopen (path_dhclient_db, "w");
e2381ff
+		leaseFile = fopen (path_dhclient_db, "we");
e2381ff
 		if (leaseFile == NULL) {
e2381ff
 			log_error ("can't create %s: %m", path_dhclient_db);
e2381ff
 			return 0;
5a3797e
@@ -3668,9 +3668,9 @@ void go_daemon ()
4bf24c8
 	close(2);
e2381ff
 
e2381ff
 	/* Reopen them on /dev/null. */
e2381ff
-	open("/dev/null", O_RDWR);
e2381ff
-	open("/dev/null", O_RDWR);
e2381ff
-	open("/dev/null", O_RDWR);
e2381ff
+	open("/dev/null", O_RDWR | O_CLOEXEC);
e2381ff
+	open("/dev/null", O_RDWR | O_CLOEXEC);
e2381ff
+	open("/dev/null", O_RDWR | O_CLOEXEC);
e2381ff
 
e2381ff
 	write_client_pid_file ();
e2381ff
 
5a3797e
@@ -3682,14 +3682,14 @@ void write_client_pid_file ()
e2381ff
 	FILE *pf;
e2381ff
 	int pfdesc;
e2381ff
 
e2381ff
-	pfdesc = open (path_dhclient_pid, O_CREAT | O_TRUNC | O_WRONLY, 0644);
e2381ff
+	pfdesc = open (path_dhclient_pid, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
e2381ff
 
e2381ff
 	if (pfdesc < 0) {
e2381ff
 		log_error ("Can't create %s: %m", path_dhclient_pid);
e2381ff
 		return;
e2381ff
 	}
e2381ff
 
e2381ff
-	pf = fdopen (pfdesc, "w");
e2381ff
+	pf = fdopen (pfdesc, "we");
e2381ff
 	if (!pf)
e2381ff
 		log_error ("Can't fdopen %s: %m", path_dhclient_pid);
e2381ff
 	else {
5a3797e
diff -up dhcp-4.2.0/common/bpf.c.cloexec dhcp-4.2.0/common/bpf.c
5a3797e
--- dhcp-4.2.0/common/bpf.c.cloexec	2009-11-20 02:48:59.000000000 +0100
5a3797e
+++ dhcp-4.2.0/common/bpf.c	2010-07-21 14:31:13.000000000 +0200
e2381ff
@@ -94,7 +94,7 @@ int if_register_bpf (info)
e2381ff
 	for (b = 0; 1; b++) {
e2381ff
 		/* %Audit% 31 bytes max. %2004.06.17,Safe% */
e2381ff
 		sprintf(filename, BPF_FORMAT, b);
e2381ff
-		sock = open (filename, O_RDWR, 0);
e2381ff
+		sock = open (filename, O_RDWR | O_CLOEXEC, 0);
e2381ff
 		if (sock < 0) {
e2381ff
 			if (errno == EBUSY) {
e2381ff
 				continue;
5a3797e
diff -up dhcp-4.2.0/common/discover.c.cloexec dhcp-4.2.0/common/discover.c
5a3797e
--- dhcp-4.2.0/common/discover.c.cloexec	2009-10-28 05:12:29.000000000 +0100
5a3797e
+++ dhcp-4.2.0/common/discover.c	2010-07-21 14:31:13.000000000 +0200
4bf24c8
@@ -409,7 +409,7 @@ begin_iface_scan(struct iface_conf_list 
e2381ff
 	int len;
e2381ff
 	int i;
e2381ff
 
e2381ff
-	ifaces->fp = fopen("/proc/net/dev", "r");
e2381ff
+	ifaces->fp = fopen("/proc/net/dev", "re");
e2381ff
 	if (ifaces->fp == NULL) {
e2381ff
 		log_error("Error opening '/proc/net/dev' to list interfaces");
e2381ff
 		return 0;
9538db2
@@ -444,7 +444,7 @@ begin_iface_scan(struct iface_conf_list 
e2381ff
 
e2381ff
 #ifdef DHCPv6
9538db2
 	if (local_family == AF_INET6) {
9538db2
-		ifaces->fp6 = fopen("/proc/net/if_inet6", "r");
9538db2
+		ifaces->fp6 = fopen("/proc/net/if_inet6", "re");
9538db2
 		if (ifaces->fp6 == NULL) {
9538db2
 			log_error("Error opening '/proc/net/if_inet6' to "
9538db2
 				  "list IPv6 interfaces; %m");
5a3797e
diff -up dhcp-4.2.0/common/dlpi.c.cloexec dhcp-4.2.0/common/dlpi.c
5a3797e
--- dhcp-4.2.0/common/dlpi.c.cloexec	2009-11-20 02:49:00.000000000 +0100
5a3797e
+++ dhcp-4.2.0/common/dlpi.c	2010-07-21 14:31:13.000000000 +0200
5a3797e
@@ -807,7 +807,7 @@ dlpiopen(const char *ifname) {
e2381ff
 	}
e2381ff
 	*dp = '\0';
e2381ff
 	
e2381ff
-	return open (devname, O_RDWR, 0);
e2381ff
+	return open (devname, O_RDWR | O_CLOEXEC, 0);
e2381ff
 }
e2381ff
 
e2381ff
 /*
5a3797e
diff -up dhcp-4.2.0/common/nit.c.cloexec dhcp-4.2.0/common/nit.c
5a3797e
--- dhcp-4.2.0/common/nit.c.cloexec	2009-11-20 02:49:01.000000000 +0100
5a3797e
+++ dhcp-4.2.0/common/nit.c	2010-07-21 14:31:13.000000000 +0200
e2381ff
@@ -81,7 +81,7 @@ int if_register_nit (info)
e2381ff
 	struct strioctl sio;
e2381ff
 
e2381ff
 	/* Open a NIT device */
e2381ff
-	sock = open ("/dev/nit", O_RDWR);
e2381ff
+	sock = open ("/dev/nit", O_RDWR | O_CLOEXEC);
e2381ff
 	if (sock < 0)
e2381ff
 		log_fatal ("Can't open NIT device for %s: %m", info -> name);
e2381ff
 
5a3797e
diff -up dhcp-4.2.0/common/resolv.c.cloexec dhcp-4.2.0/common/resolv.c
5a3797e
--- dhcp-4.2.0/common/resolv.c.cloexec	2009-11-20 02:49:01.000000000 +0100
5a3797e
+++ dhcp-4.2.0/common/resolv.c	2010-07-21 14:31:13.000000000 +0200
4bf24c8
@@ -49,7 +49,7 @@ void read_resolv_conf (parse_time)
e2381ff
 	struct domain_search_list *dp, *dl, *nd;
4bf24c8
 	isc_result_t status;
e2381ff
 
e2381ff
-	if ((file = open (path_resolv_conf, O_RDONLY)) < 0) {
e2381ff
+	if ((file = open (path_resolv_conf, O_RDONLY | O_CLOEXEC)) < 0) {
e2381ff
 		log_error ("Can't open %s: %m", path_resolv_conf);
e2381ff
 		return;
e2381ff
 	}
5a3797e
diff -up dhcp-4.2.0/common/upf.c.cloexec dhcp-4.2.0/common/upf.c
5a3797e
--- dhcp-4.2.0/common/upf.c.cloexec	2009-11-20 02:49:01.000000000 +0100
5a3797e
+++ dhcp-4.2.0/common/upf.c	2010-07-21 14:31:13.000000000 +0200
e2381ff
@@ -77,7 +77,7 @@ int if_register_upf (info)
e2381ff
 		/* %Audit% Cannot exceed 36 bytes. %2004.06.17,Safe% */
e2381ff
 		sprintf(filename, "/dev/pf/pfilt%d", b);
e2381ff
 
e2381ff
-		sock = open (filename, O_RDWR, 0);
e2381ff
+		sock = open (filename, O_RDWR | O_CLOEXEC, 0);
e2381ff
 		if (sock < 0) {
e2381ff
 			if (errno == EBUSY) {
e2381ff
 				continue;
5a3797e
diff -up dhcp-4.2.0/dst/dst_api.c.cloexec dhcp-4.2.0/dst/dst_api.c
5a3797e
--- dhcp-4.2.0/dst/dst_api.c.cloexec	2009-10-29 01:46:48.000000000 +0100
5a3797e
+++ dhcp-4.2.0/dst/dst_api.c	2010-07-21 14:31:13.000000000 +0200
5a3797e
@@ -437,7 +437,7 @@ dst_s_write_private_key(const DST_KEY *k
e2381ff
 			     PRIVATE_KEY, PATH_MAX);
e2381ff
 
e2381ff
 	/* Do not overwrite an existing file */
e2381ff
-	if ((fp = dst_s_fopen(file, "w", 0600)) != NULL) {
e2381ff
+	if ((fp = dst_s_fopen(file, "we", 0600)) != NULL) {
e2381ff
 		int nn;
e2381ff
 		if ((nn = fwrite(encoded_block, 1, len, fp)) != len) {
e2381ff
 			EREPORT(("dst_write_private_key(): Write failure on %s %d != %d errno=%d\n",
5a3797e
@@ -494,7 +494,7 @@ dst_s_read_public_key(const char *in_nam
e2381ff
 	 * flags, proto, alg stored as decimal (or hex numbers FIXME).
e2381ff
 	 * (FIXME: handle parentheses for line continuation.)
e2381ff
 	 */
e2381ff
-	if ((fp = dst_s_fopen(name, "r", 0)) == NULL) {
e2381ff
+	if ((fp = dst_s_fopen(name, "re", 0)) == NULL) {
e2381ff
 		EREPORT(("dst_read_public_key(): Public Key not found %s\n",
e2381ff
 			 name));
e2381ff
 		return (NULL);
5a3797e
@@ -620,7 +620,7 @@ dst_s_write_public_key(const DST_KEY *ke
e2381ff
 		return (0);
e2381ff
 	}
e2381ff
 	/* create public key file */
e2381ff
-	if ((fp = dst_s_fopen(filename, "w+", 0644)) == NULL) {
e2381ff
+	if ((fp = dst_s_fopen(filename, "w+e", 0644)) == NULL) {
e2381ff
 		EREPORT(("DST_write_public_key: open of file:%s failed (errno=%d)\n",
e2381ff
 			 filename, errno));
e2381ff
 		return (0);
5a3797e
@@ -854,7 +854,7 @@ dst_s_read_private_key_file(char *name, 
e2381ff
 		return (0);
e2381ff
 	}
e2381ff
 	/* first check if we can find the key file */
e2381ff
-	if ((fp = dst_s_fopen(filename, "r", 0)) == NULL) {
e2381ff
+	if ((fp = dst_s_fopen(filename, "re", 0)) == NULL) {
e2381ff
 		EREPORT(("dst_s_read_private_key_file: Could not open file %s in directory %s\n",
e2381ff
 			 filename, dst_path[0] ? dst_path :
e2381ff
 			 (char *) getcwd(NULL, PATH_MAX - 1)));
5a3797e
diff -up dhcp-4.2.0/dst/prandom.c.cloexec dhcp-4.2.0/dst/prandom.c
5a3797e
--- dhcp-4.2.0/dst/prandom.c.cloexec	2009-11-20 02:49:01.000000000 +0100
5a3797e
+++ dhcp-4.2.0/dst/prandom.c	2010-07-21 14:31:13.000000000 +0200
5a3797e
@@ -269,7 +269,7 @@ get_dev_random(u_char *output, unsigned 
e2381ff
 
e2381ff
 	s = stat("/dev/random", &st);
e2381ff
 	if (s == 0 && S_ISCHR(st.st_mode)) {
e2381ff
-		if ((fd = open("/dev/random", O_RDONLY | O_NONBLOCK)) != -1) {
e2381ff
+		if ((fd = open("/dev/random", O_RDONLY | O_NONBLOCK | O_CLOEXEC)) != -1) {
e2381ff
 			if ((n = read(fd, output, size)) < 0)
e2381ff
 				n = 0;
e2381ff
 			close(fd);
5a3797e
@@ -480,7 +480,7 @@ digest_file(dst_work *work) 
e2381ff
 		work->file_digest = dst_free_key(work->file_digest);
e2381ff
 		return (0);
e2381ff
 	}
e2381ff
-	if ((fp = fopen(name, "r")) == NULL) 
e2381ff
+	if ((fp = fopen(name, "re")) == NULL) 
e2381ff
 		return (0);
e2381ff
 	for (no = 0; (i = fread(buf, sizeof(*buf), sizeof(buf), fp)) > 0; 
e2381ff
 	     no += i) 
5a3797e
diff -up dhcp-4.2.0/omapip/trace.c.cloexec dhcp-4.2.0/omapip/trace.c
5a3797e
--- dhcp-4.2.0/omapip/trace.c.cloexec	2010-06-01 19:29:59.000000000 +0200
5a3797e
+++ dhcp-4.2.0/omapip/trace.c	2010-07-21 14:36:48.000000000 +0200
5a3797e
@@ -141,10 +141,10 @@ isc_result_t trace_begin (const char *fi
5a3797e
 		return DHCP_R_INVALIDARG;
e2381ff
 	}
e2381ff
 
e2381ff
-	traceoutfile = open (filename, O_CREAT | O_WRONLY | O_EXCL, 0600);
e2381ff
+	traceoutfile = open (filename, O_CREAT | O_WRONLY | O_EXCL | O_CLOEXEC, 0600);
e2381ff
 	if (traceoutfile < 0 && errno == EEXIST) {
e2381ff
 		log_error ("WARNING: Overwriting trace file \"%s\"", filename);
5a3797e
-		traceoutfile = open (filename, O_WRONLY | O_EXCL | O_TRUNC,
5a3797e
+		traceoutfile = open (filename, O_WRONLY | O_EXCL | O_TRUNC | O_CLOEXEC,
5a3797e
 				     0600);
e2381ff
 	}
e2381ff
 
5a3797e
@@ -431,7 +431,7 @@ void trace_file_replay (const char *file
e2381ff
 	isc_result_t result;
e2381ff
 	int len;
e2381ff
 
e2381ff
-	traceinfile = fopen (filename, "r");
e2381ff
+	traceinfile = fopen (filename, "re");
e2381ff
 	if (!traceinfile) {
5a3797e
 		log_error("Can't open tracefile %s: %m", filename);
e2381ff
 		return;
5a3797e
diff -up dhcp-4.2.0/relay/dhcrelay.c.cloexec dhcp-4.2.0/relay/dhcrelay.c
5a3797e
--- dhcp-4.2.0/relay/dhcrelay.c.cloexec	2010-02-17 21:33:55.000000000 +0100
5a3797e
+++ dhcp-4.2.0/relay/dhcrelay.c	2010-07-21 14:31:39.000000000 +0200
9538db2
@@ -177,11 +177,11 @@ main(int argc, char **argv) {
4bf24c8
 	/* Make sure that file descriptors 0(stdin), 1,(stdout), and
4bf24c8
 	   2(stderr) are open. To do this, we assume that when we
4bf24c8
 	   open a file the lowest available file descriptor is used. */
4bf24c8
-	fd = open("/dev/null", O_RDWR);
4bf24c8
+	fd = open("/dev/null", O_RDWR | O_CLOEXEC);
4bf24c8
 	if (fd == 0)
4bf24c8
-		fd = open("/dev/null", O_RDWR);
4bf24c8
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
4bf24c8
 	if (fd == 1)
4bf24c8
-		fd = open("/dev/null", O_RDWR);
4bf24c8
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
4bf24c8
 	if (fd == 2)
4bf24c8
 		log_perror = 0; /* No sense logging to /dev/null. */
4bf24c8
 	else if (fd != -1)
5a3797e
@@ -520,12 +520,12 @@ main(int argc, char **argv) {
4bf24c8
 			exit(0);
4bf24c8
 
4bf24c8
 		pfdesc = open(path_dhcrelay_pid,
e2381ff
-			       O_CREAT | O_TRUNC | O_WRONLY, 0644);
e2381ff
+			       O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
e2381ff
 
e2381ff
 		if (pfdesc < 0) {
4bf24c8
 			log_error("Can't create %s: %m", path_dhcrelay_pid);
e2381ff
 		} else {
4bf24c8
-			pf = fdopen(pfdesc, "w");
4bf24c8
+			pf = fdopen(pfdesc, "we");
e2381ff
 			if (!pf)
4bf24c8
 				log_error("Can't fdopen %s: %m",
e2381ff
 				      path_dhcrelay_pid);
5a3797e
diff -up dhcp-4.2.0/server/confpars.c.cloexec dhcp-4.2.0/server/confpars.c
5a3797e
--- dhcp-4.2.0/server/confpars.c.cloexec	2010-03-24 22:49:47.000000000 +0100
5a3797e
+++ dhcp-4.2.0/server/confpars.c	2010-07-21 14:31:39.000000000 +0200
4bf24c8
@@ -116,7 +116,7 @@ isc_result_t read_conf_file (const char 
e2381ff
 	}
e2381ff
 #endif
e2381ff
 
e2381ff
-	if ((file = open (filename, O_RDONLY)) < 0) {
e2381ff
+	if ((file = open (filename, O_RDONLY | O_CLOEXEC)) < 0) {
e2381ff
 		if (leasep) {
e2381ff
 			log_error ("Can't open lease database %s: %m --",
e2381ff
 				   path_dhcpd_db);
5a3797e
diff -up dhcp-4.2.0/server/db.c.cloexec dhcp-4.2.0/server/db.c
5a3797e
--- dhcp-4.2.0/server/db.c.cloexec	2010-02-17 21:33:55.000000000 +0100
5a3797e
+++ dhcp-4.2.0/server/db.c	2010-07-21 14:31:39.000000000 +0200
5a3797e
@@ -1035,7 +1035,7 @@ void db_startup (testp)
e2381ff
 	}
e2381ff
 #endif
e2381ff
 	if (!testp) {
e2381ff
-		db_file = fopen (path_dhcpd_db, "a");
e2381ff
+		db_file = fopen (path_dhcpd_db, "ae");
e2381ff
 		if (!db_file)
e2381ff
 			log_fatal ("Can't open %s for append.", path_dhcpd_db);
e2381ff
 		expire_all_pools ();
5a3797e
@@ -1083,12 +1083,12 @@ int new_lease_file ()
e2381ff
 		     path_dhcpd_db, (int)t) >= sizeof newfname)
e2381ff
 		log_fatal("new_lease_file: lease file path too long");
e2381ff
 
e2381ff
-	db_fd = open (newfname, O_WRONLY | O_TRUNC | O_CREAT, 0664);
e2381ff
+	db_fd = open (newfname, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0664);
e2381ff
 	if (db_fd < 0) {
e2381ff
 		log_error ("Can't create new lease file: %m");
e2381ff
 		return 0;
e2381ff
 	}
e2381ff
-	if ((new_db_file = fdopen(db_fd, "w")) == NULL) {
e2381ff
+	if ((new_db_file = fdopen(db_fd, "we")) == NULL) {
e2381ff
 		log_error("Can't fdopen new lease file: %m");
e2381ff
 		close(db_fd);
e2381ff
 		goto fdfail;
5a3797e
diff -up dhcp-4.2.0/server/dhcpd.c.cloexec dhcp-4.2.0/server/dhcpd.c
5a3797e
--- dhcp-4.2.0/server/dhcpd.c.cloexec	2010-06-01 19:29:59.000000000 +0200
5a3797e
+++ dhcp-4.2.0/server/dhcpd.c	2010-07-21 14:31:39.000000000 +0200
5a3797e
@@ -268,11 +268,11 @@ main(int argc, char **argv) {
e2381ff
         /* Make sure that file descriptors 0 (stdin), 1, (stdout), and
e2381ff
            2 (stderr) are open. To do this, we assume that when we
e2381ff
            open a file the lowest available file descriptor is used. */
e2381ff
-        fd = open("/dev/null", O_RDWR);
e2381ff
+        fd = open("/dev/null", O_RDWR | O_CLOEXEC);
e2381ff
         if (fd == 0)
e2381ff
-                fd = open("/dev/null", O_RDWR);
e2381ff
+                fd = open("/dev/null", O_RDWR | O_CLOEXEC);
e2381ff
         if (fd == 1)
e2381ff
-                fd = open("/dev/null", O_RDWR);
e2381ff
+                fd = open("/dev/null", O_RDWR | O_CLOEXEC);
e2381ff
         if (fd == 2)
e2381ff
                 log_perror = 0; /* No sense logging to /dev/null. */
e2381ff
         else if (fd != -1)
5a3797e
@@ -780,7 +780,7 @@ main(int argc, char **argv) {
4bf24c8
 #endif /* PARANOIA */
e2381ff
 
e2381ff
 	/* Read previous pid file. */
e2381ff
-	if ((i = open (path_dhcpd_pid, O_RDONLY)) >= 0) {
e2381ff
+	if ((i = open (path_dhcpd_pid, O_RDONLY | O_CLOEXEC)) >= 0) {
e2381ff
 		status = read(i, pbuf, (sizeof pbuf) - 1);
e2381ff
 		close (i);
e2381ff
 		if (status > 0) {
5a3797e
@@ -798,7 +798,7 @@ main(int argc, char **argv) {
e2381ff
 	}
e2381ff
 
e2381ff
         /* Write new pid file. */
e2381ff
-        if ((i = open(path_dhcpd_pid, O_WRONLY|O_CREAT|O_TRUNC, 0644)) >= 0) {
e2381ff
+        if ((i = open(path_dhcpd_pid, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) >= 0) {
e2381ff
                 sprintf(pbuf, "%d\n", (int) getpid());
9538db2
                 IGNORE_RET (write(i, pbuf, strlen(pbuf)));
e2381ff
                 close(i);
5a3797e
@@ -824,9 +824,9 @@ main(int argc, char **argv) {
e2381ff
                 close(2);
e2381ff
 
e2381ff
                 /* Reopen them on /dev/null. */
e2381ff
-                open("/dev/null", O_RDWR);
e2381ff
-                open("/dev/null", O_RDWR);
e2381ff
-                open("/dev/null", O_RDWR);
e2381ff
+                open("/dev/null", O_RDWR | O_CLOEXEC);
e2381ff
+                open("/dev/null", O_RDWR | O_CLOEXEC);
e2381ff
+                open("/dev/null", O_RDWR | O_CLOEXEC);
e2381ff
                 log_perror = 0; /* No sense logging to /dev/null. */
e2381ff
 
9538db2
        		IGNORE_RET (chdir("/"));
5a3797e
diff -up dhcp-4.2.0/server/ldap.c.cloexec dhcp-4.2.0/server/ldap.c
5a3797e
--- dhcp-4.2.0/server/ldap.c.cloexec	2010-03-25 16:26:58.000000000 +0100
5a3797e
+++ dhcp-4.2.0/server/ldap.c	2010-07-21 14:31:39.000000000 +0200
e2381ff
@@ -685,7 +685,7 @@ ldap_start (void)
e2381ff
 
e2381ff
   if (ldap_debug_file != NULL && ldap_debug_fd == -1)
e2381ff
     {
e2381ff
-      if ((ldap_debug_fd = open (ldap_debug_file, O_CREAT | O_TRUNC | O_WRONLY,
e2381ff
+      if ((ldap_debug_fd = open (ldap_debug_file, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC,
e2381ff
                                  S_IRUSR | S_IWUSR)) < 0)
e2381ff
         log_error ("Error opening debug LDAP log file %s: %s", ldap_debug_file,
e2381ff
                    strerror (errno));