diff options
Diffstat (limited to 'util-src')
-rw-r--r-- | util-src/Makefile | 15 | ||||
-rw-r--r-- | util-src/pposix.c | 92 |
2 files changed, 94 insertions, 13 deletions
diff --git a/util-src/Makefile b/util-src/Makefile index 06e72577..4058b59d 100644 --- a/util-src/Makefile +++ b/util-src/Makefile @@ -6,7 +6,8 @@ LUA_INCDIR?=/usr/include/lua$(LUA_SUFFIX) LUA_LIB?=lua$(LUA_SUFFIX) IDN_LIB?=idn OPENSSL_LIB?=crypto - +CC?=gcc +LD?=gcc all: encodings.so hashes.so pposix.so @@ -21,18 +22,18 @@ clean: rm -f ../util/*.so encodings.o: encodings.c - gcc $(CFLAGS) -I$(LUA_INCDIR) -c -o encodings.o encodings.c + $(CC) $(CFLAGS) -I$(LUA_INCDIR) -c -o encodings.o encodings.c encodings.so: encodings.o - export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc $(LFLAGS) -o encodings.so encodings.o -L/usr/local/lib -llua$(LUA_SUFFIX) -lidn + export MACOSX_DEPLOYMENT_TARGET="10.3"; $(LD) $(LFLAGS) -o encodings.so encodings.o -L$(LUA_LIBDIR) -llua$(LUA_SUFFIX) -lidn hashes.o: hashes.c - gcc $(CFLAGS) -I$(LUA_INCDIR) -c -o hashes.o hashes.c + $(CC) $(CFLAGS) -I$(LUA_INCDIR) -c -o hashes.o hashes.c hashes.so: hashes.o - export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc $(LFLAGS) -o hashes.so hashes.o -L/usr/local/lib -llua$(LUA_SUFFIX) -lcrypto + export MACOSX_DEPLOYMENT_TARGET="10.3"; $(LD) $(LFLAGS) -o hashes.so hashes.o -L$(LUA_LIBDIR) -llua$(LUA_SUFFIX) -lcrypto pposix.o: pposix.c - gcc $(CFLAGS) -I$(LUA_INCDIR) -c -o pposix.o pposix.c + $(CC) $(CFLAGS) -I$(LUA_INCDIR) -c -o pposix.o pposix.c pposix.so: pposix.o - export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc $(LFLAGS) -o pposix.so pposix.o -L/usr/local/lib -llua$(LUA_SUFFIX) + export MACOSX_DEPLOYMENT_TARGET="10.3"; $(LD) $(LFLAGS) -o pposix.so pposix.o -L$(LUA_LIBDIR) -llua$(LUA_SUFFIX) diff --git a/util-src/pposix.c b/util-src/pposix.c index 1257fa8c..075c9c8e 100644 --- a/util-src/pposix.c +++ b/util-src/pposix.c @@ -22,9 +22,10 @@ #include <fcntl.h> #include <syslog.h> +#include <pwd.h> #include <string.h> - +#include <errno.h> #include "lua.h" #include "lauxlib.h" @@ -85,7 +86,8 @@ static int lc_daemonize(lua_State *L) /* Syslog support */ -char *facility_strings[] = { "auth", +const char * const facility_strings[] = { + "auth", "authpriv", "cron", "daemon", @@ -142,7 +144,7 @@ char* syslog_ident = NULL; int lc_syslog_open(lua_State* L) { - int facility = luaL_checkoption(L, 2, "daemon", &facility_strings); + int facility = luaL_checkoption(L, 2, "daemon", facility_strings); facility = facility_constants[facility]; luaL_checkstring(L, 1); @@ -156,7 +158,7 @@ int lc_syslog_open(lua_State* L) return 0; } -char *level_strings[] = { +const char * const level_strings[] = { "debug", "info", "notice", @@ -174,7 +176,7 @@ int level_constants[] = { }; int lc_syslog_log(lua_State* L) { - int level = luaL_checkoption(L, 1, "notice", &level_strings); + int level = luaL_checkoption(L, 1, "notice", level_strings); level = level_constants[level]; luaL_checkstring(L, 2); @@ -196,7 +198,7 @@ int lc_syslog_close(lua_State* L) int lc_syslog_setmask(lua_State* L) { - int level_idx = luaL_checkoption(L, 1, "notice", &level_strings); + int level_idx = luaL_checkoption(L, 1, "notice", level_strings); int mask = 0; do { @@ -215,6 +217,78 @@ int lc_getpid(lua_State* L) return 1; } +/* UID/GID functions */ + +int lc_getuid(lua_State* L) +{ + lua_pushinteger(L, getuid()); + return 1; +} + +int lc_getgid(lua_State* L) +{ + lua_pushinteger(L, getgid()); + return 1; +} + +int lc_setuid(lua_State* L) +{ + int uid = -1; + if(lua_gettop(L) < 1) + return 0; + if(!lua_isnumber(L, 1) && lua_tostring(L, 1)) + { + /* Passed UID is actually a string, so look up the UID */ + struct passwd *p; + p = getpwnam(lua_tostring(L, 1)); + if(!p) + { + lua_pushboolean(L, 0); + lua_pushstring(L, "no-such-user"); + return 2; + } + uid = p->pw_uid; + } + else + { + uid = lua_tonumber(L, 1); + } + + if(uid>-1) + { + /* Ok, attempt setuid */ + errno = 0; + if(setuid(uid)) + { + /* Fail */ + lua_pushboolean(L, 0); + switch(errno) + { + case EINVAL: + lua_pushstring(L, "invalid-uid"); + break; + case EPERM: + lua_pushstring(L, "permission-denied"); + break; + default: + lua_pushstring(L, "unknown-error"); + } + return 2; + } + else + { + /* Success! */ + lua_pushboolean(L, 1); + return 1; + } + } + + /* Seems we couldn't find a valid UID to switch to */ + lua_pushboolean(L, 0); + lua_pushstring(L, "invalid-uid"); + return 2; +} + /* Register functions */ int luaopen_util_pposix(lua_State *L) @@ -239,6 +313,12 @@ int luaopen_util_pposix(lua_State *L) lua_pushcfunction(L, lc_getpid); lua_setfield(L, -2, "getpid"); + lua_pushcfunction(L, lc_getuid); + lua_setfield(L, -2, "getuid"); + + lua_pushcfunction(L, lc_setuid); + lua_setfield(L, -2, "setuid"); + lua_pushliteral(L, "pposix"); lua_setfield(L, -2, "_NAME"); |