2 * nixio - Linux I/O library for lua
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <sys/types.h>
23 #include <sys/param.h>
35 int nixio__check_group(lua_State
*L
, int idx
) {
36 if (lua_isnumber(L
, idx
)) {
37 return lua_tointeger(L
, idx
);
38 } else if (lua_isstring(L
, idx
)) {
39 struct group
*g
= getgrnam(lua_tostring(L
, idx
));
40 return (!g
) ? -1 : g
->gr_gid
;
42 return luaL_argerror(L
, idx
, "supported values: <groupname>, <gid>");
46 int nixio__check_user(lua_State
*L
, int idx
) {
47 if (lua_isnumber(L
, idx
)) {
48 return lua_tointeger(L
, idx
);
49 } else if (lua_isstring(L
, idx
)) {
50 struct passwd
*p
= getpwnam(lua_tostring(L
, idx
));
51 return (!p
) ? -1 : p
->pw_uid
;
53 return luaL_argerror(L
, idx
, "supported values: <username>, <uid>");
58 static int nixio__push_group(lua_State
*L
, struct group
*gr
) {
59 lua_createtable(L
, 0, 4);
60 lua_pushstring(L
, gr
->gr_name
);
61 lua_setfield(L
, -2, "name");
62 lua_pushstring(L
, gr
->gr_passwd
);
63 lua_setfield(L
, -2, "passwd");
64 lua_pushinteger(L
, gr
->gr_gid
);
65 lua_setfield(L
, -2, "gid");
68 for (int i
=0; gr
->gr_mem
[i
]; i
++) {
69 lua_pushstring(L
, gr
->gr_mem
[i
]);
70 lua_rawseti(L
, -2, i
+1);
73 lua_setfield(L
, -2, "mem");
77 static int nixio_getgr(lua_State
*L
) {
80 if (lua_isnumber(L
, 1)) {
81 gr
= getgrgid(lua_tointeger(L
, 1));
82 } else if (lua_isstring(L
, 1)) {
83 gr
= getgrnam(lua_tostring(L
, 1));
84 } else if (lua_isnoneornil(L
, 1)) {
89 while ((gr
= getgrent())) {
90 nixio__push_group(L
, gr
);
91 lua_rawseti(L
, -2, ++i
);
95 return nixio__perror(L
);
101 return luaL_argerror(L
, 1, "supported values: <groupname>, <gid>");
105 return nixio__perror(L
);
107 return nixio__push_group(L
, gr
);
111 static int nixio__push_passwd(lua_State
*L
, struct passwd
*pw
) {
112 lua_createtable(L
, 0, 7);
113 lua_pushstring(L
, pw
->pw_name
);
114 lua_setfield(L
, -2, "name");
115 lua_pushstring(L
, pw
->pw_passwd
);
116 lua_setfield(L
, -2, "passwd");
117 lua_pushinteger(L
, pw
->pw_gid
);
118 lua_setfield(L
, -2, "gid");
119 lua_pushinteger(L
, pw
->pw_uid
);
120 lua_setfield(L
, -2, "uid");
121 lua_pushstring(L
, pw
->pw_dir
);
122 lua_setfield(L
, -2, "dir");
123 lua_pushstring(L
, pw
->pw_gecos
);
124 lua_setfield(L
, -2, "gecos");
125 lua_pushstring(L
, pw
->pw_shell
);
126 lua_setfield(L
, -2, "shell");
130 static int nixio_getpw(lua_State
*L
) {
133 if (lua_isnumber(L
, 1)) {
134 pw
= getpwuid(lua_tointeger(L
, 1));
135 } else if (lua_isstring(L
, 1)) {
136 pw
= getpwnam(lua_tostring(L
, 1));
137 } else if (lua_isnoneornil(L
, 1)) {
142 while ((pw
= getpwent())) {
143 nixio__push_passwd(L
, pw
);
144 lua_rawseti(L
, -2, ++i
);
148 return nixio__perror(L
);
154 return luaL_argerror(L
, 1, "supported values: <username>, <uid>");
158 return nixio__perror(L
);
160 return nixio__push_passwd(L
, pw
);
165 static int nixio__push_spwd(lua_State
*L
, struct spwd
*sp
) {
166 lua_createtable(L
, 0, 9);
167 lua_pushstring(L
, sp
->sp_namp
);
168 lua_setfield(L
, -2, "namp");
169 lua_pushinteger(L
, sp
->sp_expire
);
170 lua_setfield(L
, -2, "expire");
171 lua_pushinteger(L
, sp
->sp_flag
);
172 lua_setfield(L
, -2, "flag");
173 lua_pushinteger(L
, sp
->sp_inact
);
174 lua_setfield(L
, -2, "inact");
175 lua_pushinteger(L
, sp
->sp_lstchg
);
176 lua_setfield(L
, -2, "lstchg");
177 lua_pushinteger(L
, sp
->sp_max
);
178 lua_setfield(L
, -2, "max");
179 lua_pushinteger(L
, sp
->sp_min
);
180 lua_setfield(L
, -2, "min");
181 lua_pushinteger(L
, sp
->sp_warn
);
182 lua_setfield(L
, -2, "warn");
183 lua_pushstring(L
, sp
->sp_pwdp
);
184 lua_setfield(L
, -2, "pwdp");
188 static int nixio_getsp(lua_State
*L
) {
191 if (lua_isstring(L
, 1)) {
192 sp
= getspnam(lua_tostring(L
, 1));
193 } else if (lua_isnoneornil(L
, 1)) {
198 while ((sp
= getspent())) {
199 nixio__push_spwd(L
, sp
);
200 lua_rawseti(L
, -2, ++i
);
204 return nixio__perror(L
);
210 return luaL_argerror(L
, 1, "supported values: <username>");
214 return nixio__perror(L
);
216 return nixio__push_spwd(L
, sp
);
221 static int nixio_crypt(lua_State
*L
) {
222 const char *key
= luaL_checkstring(L
, 1);
223 const char *salt
= luaL_checkstring(L
, 2);
224 const char *hash
= crypt(key
, salt
);
227 lua_pushstring(L
, hash
);
237 static const luaL_reg R
[] = {
238 {"crypt", nixio_crypt
},
239 {"getgr", nixio_getgr
},
240 {"getpw", nixio_getpw
},
242 {"getsp", nixio_getsp
},
247 #else /* __WINNT__ */
249 static const luaL_reg R
[] = {
255 void nixio_open_user(lua_State
*L
) {
256 luaL_register(L
, NULL
, R
);