X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=util-src%2Fpposix.c;h=ffd2128835ddb9124cc8fb858a23be23e8e2734a;hb=adc0dd24c3553ba366b45ca474b9cb8ccb778fcf;hp=8c1dbcc6f9a754c5626ecebc2a3cd5eb4e506597;hpb=332ebda71d7fa4771a8222664b3b8bf341109432;p=prosody.git diff --git a/util-src/pposix.c b/util-src/pposix.c index 8c1dbcc6..ffd21288 100644 --- a/util-src/pposix.c +++ b/util-src/pposix.c @@ -1,6 +1,6 @@ -/* Prosody IM v0.4 --- Copyright (C) 2008-2009 Matthew Wild --- Copyright (C) 2008-2009 Waqas Hussain +/* Prosody IM +-- Copyright (C) 2008-2010 Matthew Wild +-- Copyright (C) 2008-2010 Waqas Hussain -- Copyright (C) 2009 Tobias Markmann -- -- This project is MIT/X11 licensed. Please see the @@ -13,7 +13,7 @@ * POSIX support functions for Lua */ -#define MODULE_VERSION "0.3.3" +#define MODULE_VERSION "0.3.5" #include #include @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -359,6 +360,62 @@ int lc_setgid(lua_State* L) return 2; } +int lc_initgroups(lua_State* L) +{ + int ret; + gid_t gid; + struct passwd *p; + + if(!lua_isstring(L, 1)) + { + lua_pushnil(L); + lua_pushstring(L, "invalid-username"); + return 2; + } + p = getpwnam(lua_tostring(L, 1)); + if(!p) + { + lua_pushnil(L); + lua_pushstring(L, "no-such-user"); + return 2; + } + if(lua_gettop(L) < 2) + lua_pushnil(L); + switch(lua_type(L, 2)) + { + case LUA_TNIL: + gid = p->pw_gid; + break; + case LUA_TNUMBER: + gid = lua_tointeger(L, 2); + break; + default: + lua_pushnil(L); + lua_pushstring(L, "invalid-gid"); + return 2; + } + ret = initgroups(lua_tostring(L, 1), gid); + switch(errno) + { + case 0: + lua_pushboolean(L, 1); + lua_pushnil(L); + break; + case ENOMEM: + lua_pushnil(L); + lua_pushstring(L, "no-memory"); + break; + case EPERM: + lua_pushnil(L); + lua_pushstring(L, "permission-denied"); + break; + default: + lua_pushnil(L); + lua_pushstring(L, "unknown-error"); + } + return 2; +} + int lc_umask(lua_State* L) { char old_mode_string[7]; @@ -497,6 +554,29 @@ int lc_abort(lua_State* L) return 0; } +int lc_uname(lua_State* L) +{ + struct utsname uname_info; + if(uname(&uname_info) != 0) + { + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); + return 2; + } + lua_newtable(L); + lua_pushstring(L, uname_info.sysname); + lua_setfield(L, -2, "sysname"); + lua_pushstring(L, uname_info.nodename); + lua_setfield(L, -2, "nodename"); + lua_pushstring(L, uname_info.release); + lua_setfield(L, -2, "release"); + lua_pushstring(L, uname_info.version); + lua_setfield(L, -2, "version"); + lua_pushstring(L, uname_info.machine); + lua_setfield(L, -2, "machine"); + return 1; +} + /* Register functions */ int luaopen_util_pposix(lua_State *L) @@ -517,6 +597,7 @@ int luaopen_util_pposix(lua_State *L) { "setuid", lc_setuid }, { "setgid", lc_setgid }, + { "initgroups", lc_initgroups }, { "umask", lc_umask }, @@ -525,6 +606,8 @@ int luaopen_util_pposix(lua_State *L) { "setrlimit", lc_setrlimit }, { "getrlimit", lc_getrlimit }, + { "uname", lc_uname }, + { NULL, NULL } }; @@ -537,4 +620,4 @@ int luaopen_util_pposix(lua_State *L) lua_setfield(L, -2, "_VERSION"); return 1; -}; +}