From: Vsevolod Stakhov Date: Tue, 2 Feb 2016 23:42:42 +0000 (+0000) Subject: Add plain open file API method X-Git-Tag: 1.1.3~11 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=63f8b66c21b2731408f34d7ab4a2d09d2e41cc2f;p=rspamd.git Add plain open file API method --- diff --git a/CMakeLists.txt b/CMakeLists.txt index c4332e3f1..2f3a01660 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -876,6 +876,7 @@ CHECK_SYMBOL_EXISTS(PCRE_CONFIG_JIT "pcre.h" HAVE_PCRE_JIT) CHECK_SYMBOL_EXISTS(SOCK_SEQPACKET "sys/types.h;sys/socket.h" HAVE_SOCK_SEQPACKET) CHECK_SYMBOL_EXISTS(I_SETSIG "sys/types.h;sys/ioctl.h" HAVE_SETSIG) CHECK_SYMBOL_EXISTS(O_ASYNC "sys/types.h;sys/fcntl.h" HAVE_OASYNC) +CHECK_SYMBOL_EXISTS(O_NOFOLLOW "sys/types.h;sys/fcntl.h" HAVE_ONOFOLLOW) # Some PCRE implementations are lacking of pcre_jit_exec fast path SET(_PCRE_FAST_TEST " diff --git a/config.h.in b/config.h.in index b976b701c..fbe3bb37b 100644 --- a/config.h.in +++ b/config.h.in @@ -55,6 +55,7 @@ #cmakedefine HAVE_NETINET_IN_H 1 #cmakedefine HAVE_NFTW 1 #cmakedefine HAVE_OASYNC 1 +#cmakedefine HAVE_ONOFOLLOW 1 #cmakedefine HAVE_OPENSSL 1 #cmakedefine HAVE_O_DIRECT 1 #cmakedefine HAVE_PATH_MAX 1 diff --git a/src/libutil/util.c b/src/libutil/util.c index c4f9ef242..6893172c9 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -2096,3 +2096,34 @@ event_get_base (struct event *ev) return ev->ev_base; } #endif + +int +rspamd_file_xopen (const char *fname, int oflags, guint mode) +{ + struct stat sb; + int fd; + char *rp, rp_buf[PATH_MAX]; + + rp = realpath (fname, rp_buf); + + if (rp == NULL) { + return -1; + } + +#ifdef HAVE_ONOFOLLOW + fd = open (fname, oflags | O_NOFOLLOW, mode); +#else + fd = open (fname, oflags, mode); +#endif + + if (fd == -1) { + return (-1); + } + + if (fstat (fd, &sb) == -1 || !S_ISREG (sb.st_mode)) { + close (fd); + return (-1); + } + + return (fd); +} diff --git a/src/libutil/util.h b/src/libutil/util.h index b4de2331a..06b6f7d42 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -432,4 +432,13 @@ struct event_base * event_get_base (struct event *ev); event_set((ev), (x), EV_SIGNAL|EV_PERSIST, (cb), (arg)) #endif +/** + * Open file without following symlinks or special stuff + * @param fname filename + * @param oflags open flags + * @param mode mode to open + * @return fd or -1 in case of error + */ +int rspamd_file_xopen (const char *fname, int oflags, guint mode); + #endif