mod_storage_sql: Fix commit c806a599224a for compatibility with non-MySQL databases...
[prosody.git] / util-src / pposix.c
index b47faaafe414aedc57f8669b479fa57753fac44b..ffd2128835ddb9124cc8fb858a23be23e8e2734a 100644 (file)
@@ -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.2"
+#define MODULE_VERSION "0.3.5"
 
 #include <stdlib.h>
 #include <math.h>
@@ -22,6 +22,7 @@
 #include <sys/resource.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/utsname.h>
 #include <fcntl.h>
 
 #include <syslog.h>
@@ -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];
@@ -371,6 +428,21 @@ int lc_umask(lua_State* L)
        return 1;
 }
 
+int lc_mkdir(lua_State* L)
+{
+       int ret = mkdir(luaL_checkstring(L, 1), S_IRUSR | S_IWUSR | S_IXUSR
+               | S_IRGRP | S_IWGRP | S_IXGRP
+               | S_IROTH | S_IXOTH); /* mode 775 */
+
+       lua_pushboolean(L, ret==0);
+       if(ret)
+       {
+               lua_pushstring(L, strerror(errno));
+               return 2;
+       }
+       return 1;
+}
+
 /*     Like POSIX's setrlimit()/getrlimit() API functions.
  *
  *     Syntax:
@@ -482,51 +554,64 @@ int lc_abort(lua_State* L)
        return 0;
 }
 
-/* Register functions */
-
-int luaopen_util_pposix(lua_State *L)
+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;
+}
 
-       lua_pushcfunction(L, lc_abort);
-       lua_setfield(L, -2, "abort");
+/* Register functions */
 
-       lua_pushcfunction(L, lc_daemonize);
-       lua_setfield(L, -2, "daemonize");
+int luaopen_util_pposix(lua_State *L)
+{
+       luaL_Reg exports[] = {
+               { "abort", lc_abort },
 
-       lua_pushcfunction(L, lc_syslog_open);
-       lua_setfield(L, -2, "syslog_open");
+               { "daemonize", lc_daemonize },
 
-       lua_pushcfunction(L, lc_syslog_close);
-       lua_setfield(L, -2, "syslog_close");
+               { "syslog_open", lc_syslog_open },
+               { "syslog_close", lc_syslog_close },
+               { "syslog_log", lc_syslog_log },
+               { "syslog_setminlevel", lc_syslog_setmask },
 
-       lua_pushcfunction(L, lc_syslog_log);
-       lua_setfield(L, -2, "syslog_log");
+               { "getpid", lc_getpid },
+               { "getuid", lc_getuid },
+               { "getgid", lc_getgid },
 
-       lua_pushcfunction(L, lc_syslog_setmask);
-       lua_setfield(L, -2, "syslog_setminlevel");
+               { "setuid", lc_setuid },
+               { "setgid", lc_setgid },
+               { "initgroups", lc_initgroups },
 
-       lua_pushcfunction(L, lc_getpid);
-       lua_setfield(L, -2, "getpid");
+               { "umask", lc_umask },
 
-       lua_pushcfunction(L, lc_getuid);
-       lua_setfield(L, -2, "getuid");
-       lua_pushcfunction(L, lc_getgid);
-       lua_setfield(L, -2, "getgid");
+               { "mkdir", lc_mkdir },
 
-       lua_pushcfunction(L, lc_setuid);
-       lua_setfield(L, -2, "setuid");
-       lua_pushcfunction(L, lc_setgid);
-       lua_setfield(L, -2, "setgid");
+               { "setrlimit", lc_setrlimit },
+               { "getrlimit", lc_getrlimit },
 
-       lua_pushcfunction(L, lc_umask);
-       lua_setfield(L, -2, "umask");
+               { "uname", lc_uname },
 
-       lua_pushcfunction(L, lc_setrlimit);
-       lua_setfield(L, -2, "setrlimit");
+               { NULL, NULL }
+       };
 
-       lua_pushcfunction(L, lc_getrlimit);
-       lua_setfield(L, -2, "getrlimit");
+       luaL_register(L, "pposix",  exports);
 
        lua_pushliteral(L, "pposix");
        lua_setfield(L, -2, "_NAME");
@@ -535,4 +620,4 @@ int luaopen_util_pposix(lua_State *L)
        lua_setfield(L, -2, "_VERSION");
 
        return 1;
-};
+}