Date: Sun, 9 Jan 2000 01:25:07 -0800 (PST) From: Kris Kennaway <kris@hub.freebsd.org> To: audit@freebsd.org Subject: Tempfile handling in ctm Message-ID: <Pine.BSF.4.21.0001090123350.79366-100000@hub.freebsd.org>
next in thread | raw e-mail | index | archive | help
This patch fixes the tempfile handling in ctm(1), removing race conditions and replacing tempnam() with mkstemp(). Reviews please! :) Kris Index: ctm.c =================================================================== RCS file: /home/ncvs/src/usr.sbin/ctm/ctm/ctm.c,v retrieving revision 1.18 diff -u -r1.18 ctm.c --- ctm.c 1999/08/28 01:15:59 1.18 +++ ctm.c 2000/01/09 09:17:03 @@ -39,6 +39,7 @@ */ #define EXTERN /* */ +#include <paths.h> #include "ctm.h" #define CTM_STATUS ".ctm_status" @@ -64,6 +65,9 @@ BackupFile = NULL; TarCmd = TARCMD; LastFilter = FilterList = NULL; + TmpDir = getenv("TMPDIR"); + if (TmpDir == NULL) + TmpDir = strdup(_PATH_TMP); setbuf(stderr,0); setbuf(stdout,0); @@ -224,18 +228,27 @@ /* If we cannot seek, we're doomed, so copy to a tmp-file in that case */ if(!p && -1 == fseek(f,0,SEEK_END)) { - char *fn = tempnam(TmpDir,"CTMclient"); - FILE *f2 = fopen(fn,"w+"); - int i; + char *fn; + FILE *f2; + int fd; - if(!f2) { - warn("%s", fn); + if (asprintf(&fn, "%s/CTMclient.XXXXXXXXXX", TmpDir) == -1) { + fprintf(stderr, "Cannot allocate memory\n"); fclose(f); return Exit_Broke; } + if ((fd = mkstemp(fn)) == -1 || (f2 = fdopen(fd, "w+")) == NULL) { + perror(fn); + free(fn); + if (fd != -1) + close(fd); + fclose(f); + return Exit_Broke; + } unlink(fn); if (Verbose > 0) fprintf(stderr,"Writing tmp-file \"%s\"\n",fn); + free(fn); while(EOF != (i=getc(f))) if(EOF == putc(i,f2)) { fclose(f2); Index: ctm_pass2.c =================================================================== RCS file: /home/ncvs/src/usr.sbin/ctm/ctm/ctm_pass2.c,v retrieving revision 1.17 diff -u -r1.17 ctm_pass2.c --- ctm_pass2.c 1999/08/28 01:16:00 1.17 +++ ctm_pass2.c 2000/01/09 09:22:47 @@ -182,7 +182,21 @@ if (!match) break; if(!strcmp(sp->Key,"FN")) { - p = tempnam(TmpDir,"CTMclient"); + if(asprintf((char **)&p, "%s/CTMclient.XXXXXXXXXX", + TmpDir) == -1) { + fprintf(stderr, "Cannot allocate memory\n"); + ret |= Exit_NotOK; + return ret; + } + if((j = mkstemp(p)) == -1) { + fprintf(stderr, + " %s: Could not create tempfile.\n", + sp->Key); + Free(p); + ret |= Exit_NotOK; + return ret; + } + close(j); j = ctm_edit(trash,cnt,name,p); if(j) { fprintf(stderr," %s: %s edit returned %d.\n", @@ -202,7 +216,21 @@ unlink(p); Free(p); } else if (!strcmp(sp->Key,"FE")) { - p = tempnam(TmpDir,"CTMclient"); + if(asprintf((char **)&p, "%s/CTMclient.XXXXXXXXXX", + TmpDir) == -1) { + fprintf(stderr, "Cannot allocate memory\n"); + ret |= Exit_NotOK; + return ret; + } + if((j = mkstemp(p)) == -1) { + fprintf(stderr, + " %s: Could not create tempfile.\n", + sp->Key); + Free(p); + ret |= Exit_NotOK; + return ret; + } + close(j); ed = popen("ed","w"); if (!ed) { WRONG To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-audit" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.21.0001090123350.79366-100000>