From: Ben Collins Date: Mon, 24 Apr 2000 16:42:16 +0000 (+0000) Subject: when opening files in /tmp use O_CREAT|O_EXCL to overcome race conditions X-Git-Tag: LDBM_PRE_GIANT_RWLOCK~3091 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=50a41f3ee28042ba09ee2781492da0386a2492a3;p=openldap when opening files in /tmp use O_CREAT|O_EXCL to overcome race conditions --- diff --git a/clients/ud/edit.c b/clients/ud/edit.c index a6367d7909..30a2fb76a7 100644 --- a/clients/ud/edit.c +++ b/clients/ud/edit.c @@ -18,6 +18,7 @@ #include "portable.h" #include +#include #include @@ -28,6 +29,12 @@ #include #include +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif #ifdef HAVE_SYS_RESOURCE_H #include #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); diff --git a/servers/slapd/tools/ldbmtest.c b/servers/slapd/tools/ldbmtest.c index 35b265a291..35daa1f474 100644 --- a/servers/slapd/tools/ldbmtest.c +++ b/servers/slapd/tools/ldbmtest.c @@ -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; } diff --git a/servers/slurpd/rq.c b/servers/slurpd/rq.c index 0dd599ba6d..e146047edc 100644 --- a/servers/slurpd/rq.c +++ b/servers/slurpd/rq.c @@ -35,11 +35,19 @@ #include "portable.h" #include +#include #include #include #include /* get ftruncate() */ +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#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 )) {