From d428f25cb05d2def10d08cd0e34fe5fe4d4b45fc Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Wed, 4 Dec 2013 12:55:17 +0400 Subject: [PATCH] create new file and rename --- authfile.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/authfile.c b/authfile.c index abf31fb..8c023d1 100644 --- a/authfile.c +++ b/authfile.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -109,7 +110,7 @@ struct _auth_obj authfile(const char *tokenid, struct _auth_obj ret = {0}; mode_t oldmask; FILE *fp = NULL; - char *fn; + char *fn, *nfn; int fnl; char *buf = NULL; struct { @@ -130,6 +131,8 @@ struct _auth_obj authfile(const char *tokenid, } fn = alloca(fnl); make_path(fn, tokenid, userid); + nfn = alloca(fnl+32); + snprintf(nfn, fnl+32, "%s.%d.%ld", fn, (int)getpid(), (long)time(NULL)); fp = fopen(fn, "r"); if (fp) { struct stat st; @@ -187,7 +190,7 @@ struct _auth_obj authfile(const char *tokenid, } oldmask = umask(077); - if ((fp = fopen(fn, "w"))) { + if ((fp = fopen(nfn, "w"))) { int i; if (fprintf(fp, "%s:%s:%s:", tokenid?tokenid:w.tokenid, @@ -205,6 +208,11 @@ struct _auth_obj authfile(const char *tokenid, ret.err = strerror(errno); } (void)umask(oldmask); + if (ret.err) { + unlink(nfn); /* may not exist but no matter */ + } else if (rename(nfn, fn)) { + ret.err = strerror(errno); + } if (!ret.err) { int bufsize = (w.userid?strlen(w.userid)+1:0) + ao.paylsize; -- 2.39.2