X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Futils.c;h=d0d106279268a3efb183fd2eeee386b2bf0b554f;hb=7208397398f7e08d741bfa83594a88e5d01b6220;hp=f14094f6f2460a38a461d99b7bd0af1cce833a23;hpb=4214794ce68b9456146a5bf35db9e58da836b728;p=tinc diff --git a/src/utils.c b/src/utils.c index f14094f6..d0d10627 100644 --- a/src/utils.c +++ b/src/utils.c @@ -284,3 +284,30 @@ char *replace_name(const char *name) { return ret_name; } + +/* Open a file with the desired permissions, minus the umask. + Also, if we want to create an executable file, we call fchmod() + to set the executable bits. */ + +FILE *fopenmask(const char *filename, const char *mode, mode_t perms) { + mode_t mask = umask(0); + perms &= ~mask; + umask(~perms & 0777); + FILE *f = fopen(filename, mode); + + if(!f) { + fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno)); + return NULL; + } + +#ifdef HAVE_FCHMOD + + if((perms & 0444) && f) { + fchmod(fileno(f), perms); + } + +#endif + umask(mask); + return f; +} +