aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-02-02 23:42:42 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-02-02 23:42:42 +0000
commit63f8b66c21b2731408f34d7ab4a2d09d2e41cc2f (patch)
treec921bf01006b5a0b4ee8122bcde547d973ce4d28
parent29f54d263c352640cfcf386a062f5ff37b401f7e (diff)
downloadrspamd-63f8b66c21b2731408f34d7ab4a2d09d2e41cc2f.tar.gz
rspamd-63f8b66c21b2731408f34d7ab4a2d09d2e41cc2f.zip
Add plain open file API method
-rw-r--r--CMakeLists.txt1
-rw-r--r--config.h.in1
-rw-r--r--src/libutil/util.c31
-rw-r--r--src/libutil/util.h9
4 files changed, 42 insertions, 0 deletions
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