diff --git a/Makefile.in b/Makefile.in index 6342acc..30be5fb 100644 --- a/Makefile.in +++ b/Makefile.in @@ -23,6 +23,7 @@ MAINOBJ= \ cpucount.c.o \ base32_to.c.o \ base32_from.c.o \ + ioutil.c.o \ $(ED25519OBJ) \ keccak.c.o diff --git a/ioutil.c b/ioutil.c new file mode 100644 index 0000000..26c6254 --- /dev/null +++ b/ioutil.c @@ -0,0 +1,67 @@ +#include +#include +#include "types.h" +#include "ioutil.h" +#include +#include +#include +#include + +int writeall(FH fd,const u8 *data,size_t len) +{ + ssize_t wrote; + while (len) { + wrote = write(fd,data,len); + if (wrote == -1) { + if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) + continue; + return -1; + } + len -= wrote; + data += wrote; + } + return 0; +} + +FH createfile(const char *path,int secret) +{ + int fd; + do { + fd = open(path,O_WRONLY | O_CREAT | O_TRUNC,secret ? 0600 : 0666); + if (fd == -1) { + if (errno == EINTR) + continue; + return -1; + } + } while (0); + return fd; +} + +int closefile(FH fd) +{ + int cret; + do { + cret = close(fd); + if (cret == -1) { + if (errno == EINTR) + continue; + return -1; + } + } while (0); + return 0; +} + +int writetofile(const char *path,const u8 *data,size_t len,int secret) +{ + FH fd = createfile(path,secret); + int wret = writeall(fd,data,len); + int cret = closefile(fd); + if (cret == -1) + return -1; + return wret; +} + +int createdir(const char *path,int secret) +{ + return mkdir(path,secret ? 0700 : 0777); +} diff --git a/ioutil.h b/ioutil.h new file mode 100644 index 0000000..7255a2e --- /dev/null +++ b/ioutil.h @@ -0,0 +1,8 @@ +typedef int FH; +#define FH_invalid -1 + +FH createfile(const char *path,int secret); +int closefile(FH fd); +int writeall(FH,const u8 *data,size_t len); +int writetofile(const char *path,const u8 *data,size_t len,int secret); +int createdir(const char *path,int secret); diff --git a/main.c b/main.c index a0c69f8..6773af6 100644 --- a/main.c +++ b/main.c @@ -9,7 +9,6 @@ #include #include #include -#include #include #include "types.h" @@ -19,6 +18,7 @@ #include "cpucount.h" #include "keccak.h" #include "ed25519/ed25519.h" +#include "ioutil.h" // additional leading zero is added by C static const char * const pkprefix = "== ed25519v1-public: type0 ==\0\0"; @@ -646,8 +646,6 @@ VEC_STRUCT(tstatsvec,struct tstatstruct); static void onionready(char *sname, const u8 *secret, const u8 *pubonion) { - FILE *fh; - if (endwork) return; @@ -659,7 +657,7 @@ static void onionready(char *sname, const u8 *secret, const u8 *pubonion) } } - if (mkdir(sname,0700) != 0) { + if (createdir(sname,1) != 0) { if (numneedgenerate) pthread_mutex_unlock(&keysgenerated_mutex); return; @@ -673,26 +671,18 @@ static void onionready(char *sname, const u8 *secret, const u8 *pubonion) } strcpy(&sname[onionendpos], "/hs_ed25519_secret_key"); - fh = fopen(sname, "wb"); - if (fh) { - fwrite(secret, skprefixlen + SECRET_LEN, 1, fh); - fclose(fh); - } + writetofile(sname,secret,skprefixlen + SECRET_LEN,1); strcpy(&sname[onionendpos], "/hostname"); - fh = fopen(sname, "w"); - if (fh) { + FILE *hfile = fopen(sname,"w"); + if (hfile) { sname[onionendpos] = '\n'; - fwrite(&sname[direndpos], ONIONLEN+1, 1, fh); - fclose(fh); + fwrite(&sname[direndpos],ONIONLEN + 1,1,hfile); + fclose(hfile); } strcpy(&sname[onionendpos], "/hs_ed25519_public_key"); - fh = fopen(sname, "wb"); - if (fh) { - fwrite(pubonion, pkprefixlen + PUBLIC_LEN, 1, fh); - fclose(fh); - } + writetofile(sname,pubonion,pkprefixlen + PUBLIC_LEN,0); if (fout) { sname[onionendpos] = '\n'; @@ -1164,7 +1154,7 @@ int main(int argc,char **argv) fout = fopen(outfile, "w"); if (workdir) - mkdir(workdir, 0700); + createdir(workdir,1); direndpos = workdirlen; onionendpos = workdirlen + ONIONLEN;