From fcecad2b77f54c1ce356190168db8ac2c216f30d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 6 May 2014 15:32:47 +0200 Subject: add strequals and startswith macros + add drop_privileges macro that drops the group before the user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libmdsserver/macros.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/libmdsserver') diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h index 0c2a2e2..bb4e1c3 100644 --- a/src/libmdsserver/macros.h +++ b/src/libmdsserver/macros.h @@ -23,6 +23,7 @@ #include #include #include +#include */ @@ -180,5 +181,39 @@ buf_next(buffer, type, 1) +/** + * Check whether two strings are equal + * + * @param a:char* One of the strings + * @param b:char* The other of the strings + * @return :int Whether the strings are equal + */ +#define strequals(a, b) \ + (strcmp(a, b) == 0) + + +/** + * Check whether a string starts with another string + * + * @param haystack:char* The string to inspect + * @param needle:char* The string `haystack` should start with + * @return :int Whether `haystack` starts with `needle` + */ +#define startswith(haystack, needle) \ + (strstr(haystack, needle) == haystack) + + +/** + * Set effective user and the effective group to the + * real user and the real group, respectively. If the + * group cannot be set, the user till not be set either. + * + * @return :int Non-zero on error + */ +#define drop_privileges() \ + ((getegid() == getgid() ? 0 : setegid(getgid())) || \ + (geteuid() == getuid() ? 0 : seteuid(getuid()))) + + #endif -- cgit v1.2.3-70-g09d2