---
luxio.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 106 insertions(+), 1 deletion(-)
diff --git a/luxio.c b/luxio.c
index e744746..49dbb67 100644
--- a/luxio.c
+++ b/luxio.c
@@ -62,6 +62,7 @@ Not all systems will provide all the functions described here.
#include <syslog.h>
#include <iconv.h>
#include <grp.h>
+#include <pwd.h>
#if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 24)
/* readdir is deprecated as of glibc 2.24, use readdir instead */
@@ -2562,7 +2563,108 @@ luxio_getgrnam(lua_State *L)
return 2;
}
-/* TODO: getpwuid(), getpwuid_r(), getpwnam(), getpwnam_r() 9.2.2 */
+static void
+luxio__push_passwd_table(lua_State *L, const struct passwd *p)
+{
+ lua_newtable(L);
+ lua_pushstring(L, "pw_name");
+ lua_pushstring(L, p->pw_name);
+ lua_settable(L, -3);
+ lua_pushstring(L, "pw_passwd");
+ lua_pushstring(L, p->pw_passwd);
+ lua_settable(L, -3);
+ lua_pushstring(L, "pw_uid");
+ lua_pushinteger(L, p->pw_uid);
+ lua_settable(L, -3);
+ lua_pushstring(L, "pw_gid");
+ lua_pushinteger(L, p->pw_gid);
+ lua_settable(L, -3);
+ lua_pushstring(L, "pw_gecos");
+ lua_pushstring(L, p->pw_gecos);
+ lua_settable(L, -3);
+ lua_pushstring(L, "pw_dir");
+ lua_pushstring(L, p->pw_dir);
+ lua_settable(L, -3);
+ lua_pushstring(L, "pw_shell");
+ lua_pushstring(L, p->pw_shell);
+ lua_settable(L, -3);
+}
+
+/*** passwd() information table.
+Returned by @{getpwuid} and @{getpwnam}.
+@field pw_name Username
+@field pw_passwd User password - if this field is empty, no password is needed.
+@field pw_uid User ID
+@field pw_gid Group ID
+@field pw_gecos User information
+@field pw_dir Home directory
+@field pw_shell Shell program
+@table passwd-table
+*/
+
+/*** Get password file entry.
+
+Returns 0 on success and non-zero on failure.
+
+On success the second return value is a table containing password data
+from the password database (e.g. /etc/passwd, LDAP).
+On failure the second return value contains errno.
+
+@tparam number uid
+@treturn number return value
+@treturn passwd-table|errno result table, or errno.
+@function getpwuid
+*/
+static int
+luxio_getpwuid(lua_State *L)
+{
+ uid_t uid = luaL_checkinteger(L, 1);
+ struct passwd *p = getpwuid(uid);
+
+ if (p == NULL) {
+ lua_pushinteger(L, -1);
+ lua_pushinteger(L, errno);
+
+ return 2;
+ }
+
+ lua_pushinteger(L, 0);
+ luxio__push_passwd_table(L, p);
+
+ return 2;
+}
+
+/*** Get password file entry.
+
+Returns 0 on success and non-zero on failure.
+
+On success the second return value is a table containing password data
+from the password database (e.g. /etc/passwd, LDAP).
+On failure the second return value contains errno.
+
+@tparam string username
+@treturn number return value
+@treturn passwd-table|errno result table, or errno.
+@function getpwnam
+*/
+static int
+luxio_getpwnam(lua_State *L)
+{
+ const char *name = luaL_checkstring(L, 1);
+ struct passwd *p = getpwnam(name);
+
+ if (p == NULL) {
+ lua_pushinteger(L, -1);
+ lua_pushinteger(L, errno);
+
+ return 2;
+ }
+
+ lua_pushinteger(L, 0);
+ luxio__push_passwd_table(L, p);
+
+ return 2;
+}
/* 10 Data interchange format ************************************************/
@@ -4642,6 +4744,9 @@ luxio_functions[] = {
{ "getgrgid", luxio_getgrgid },
{ "getgrnam", luxio_getgrnam },
+ { "getpwuid", luxio_getpwuid },
+ { "getpwnam", luxio_getpwnam },
+
{ "getuid", luxio_getuid },
{ "geteuid", luxio_geteuid },
{ "getgid", luxio_getgid },
--
2.11.0