when opening files in /tmp use O_CREAT|O_EXCL to overcome race conditions

This commit is contained in:
Ben Collins 2000-04-24 16:42:16 +00:00
parent 97493c3404
commit 50a41f3ee2
3 changed files with 39 additions and 10 deletions

View file

@ -18,6 +18,7 @@
#include "portable.h"
#include <stdio.h>
#include <sys/stat.h>
#include <ac/stdlib.h>
@ -28,6 +29,12 @@
#include <ac/wait.h>
#include <ac/unistd.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
@ -51,7 +58,7 @@ static int print_attrs_and_values( FILE *fp, struct attribute *attrs, short fla
static int ovalues( char *attr );
static void write_entry( void );
static char *entry_temp_file;
static char entry_temp_file[L_tmpnam];
void
@ -124,7 +131,7 @@ load_editor( void )
{
FILE *fp;
char *cp, *editor = UD_DEFAULT_EDITOR;
static char template[MED_BUF_SIZE];
int tmpfd;
#ifndef HAVE_SPAWNLP
int pid;
int status;
@ -137,13 +144,16 @@ load_editor( void )
#endif
/* write the entry into a temp file */
(void) strcpy(template, "/tmp/udEdit.XXXXXX");
if ((entry_temp_file = mktemp(template)) == NULL) {
perror("mktemp");
return(-1);
if (tmpnam(entry_temp_file) == NULL) {
perror("tmpnam");
return -1;
}
if ((fp = fopen(entry_temp_file, "w")) == NULL) {
perror("fopen");
if ((tmpfd = open(entry_temp_file, O_WRONLY|O_CREAT|O_EXCL, 0600)) == -1) {
perror(entry_temp_file);
return -1;
}
if ((fp = fdopen(tmpfd, "w")) == NULL) {
perror("fdopen");
return(-1);
}
fprintf(fp, "## Directory entry of %s\n", Entry.name);

View file

@ -508,7 +508,7 @@ edit_entry( char c, Datum *data )
strcpy( tmpname, "/tmp/dbtestXXXXXX" );
#ifndef HAVE_MKSTEMP
if ( (fd = open( mktemp( tmpname ), O_RDWR, 0600 )) == -1 ) {
if ( (fd = open( mktemp( tmpname ), O_RDWR|O_CREAT|O_EXCL, 0600 )) == -1 ) {
perror( tmpname );
return;
}

View file

@ -35,11 +35,19 @@
#include "portable.h"
#include <stdio.h>
#include <sys/stat.h>
#include <ac/stdlib.h>
#include <ac/string.h>
#include <ac/unistd.h> /* get ftruncate() */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include "slurp.h"
#include "globals.h"
@ -230,17 +238,28 @@ Rq_dump(
{
Re *re;
FILE *fp;
int tmpfd;
if ( rq == NULL ) {
Debug( LDAP_DEBUG_ANY, "Rq_dump: rq is NULL!\n", 0, 0, 0 );
return;
}
if (( fp = fopen( SLURPD_DUMPFILE, "w" )) == NULL ) {
if (unlink(SLURPD_DUMPFILE) == -1 && errno != ENOENT) {
Debug( LDAP_DEBUG_ANY, "Rq_dump: \"%s\" exists, and cannot unlink\n",
SLURPD_DUMPFILE, 0, 0 );
return;
}
if (( tmpfd = open(SLURPD_DUMPFILE, O_CREAT|O_RDWR|O_EXCL, 0600)) == -1) {
Debug( LDAP_DEBUG_ANY, "Rq_dump: cannot open \"%s\" for write\n",
SLURPD_DUMPFILE, 0, 0 );
return;
}
if (( fp = fdopen( tmpfd, "w" )) == NULL ) {
Debug( LDAP_DEBUG_ANY, "Rq_dump: cannot fdopen \"%s\" for write\n",
SLURPD_DUMPFILE, 0, 0 );
return;
}
rq->rq_lock( rq );
for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {