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>
37 int nixio__check_group(lua_State
*L
, int idx
) {
38 if (lua_isnumber(L
, idx
)) {
39 return lua_tointeger(L
, idx
);
40 } else if (lua_isstring(L
, idx
)) {
41 struct group
*g
= getgrnam(lua_tostring(L
, idx
));
42 return (!g
) ? -1 : g
->gr_gid
;
44 return luaL_argerror(L
, idx
, "supported values: <groupname>, <gid>");
48 int nixio__check_user(lua_State
*L
, int idx
) {
49 if (lua_isnumber(L
, idx
)) {
50 return lua_tointeger(L
, idx
);
51 } else if (lua_isstring(L
, idx
)) {
52 struct passwd
*p
= getpwnam(lua_tostring(L
, idx
));
53 return (!p
) ? -1 : p
->pw_uid
;
55 return luaL_argerror(L
, idx
, "supported values: <username>, <uid>");
60 static int nixio__push_group(lua_State
*L
, struct group
*gr
) {
61 lua_createtable(L
, 0, 4);
62 lua_pushstring(L
, gr
->gr_name
);
63 lua_setfield(L
, -2, "name");
64 lua_pushstring(L
, gr
->gr_passwd
);
65 lua_setfield(L
, -2, "passwd");
66 lua_pushinteger(L
, gr
->gr_gid
);
67 lua_setfield(L
, -2, "gid");
70 for (int i
=0; gr
->gr_mem
[i
]; i
++) {
71 lua_pushstring(L
, gr
->gr_mem
[i
]);
72 lua_rawseti(L
, -2, i
+1);
75 lua_setfield(L
, -2, "mem");
79 static int nixio_getgr(lua_State
*L
) {
82 if (lua_isnumber(L
, 1)) {
83 gr
= getgrgid(lua_tointeger(L
, 1));
84 } else if (lua_isstring(L
, 1)) {
85 gr
= getgrnam(lua_tostring(L
, 1));
86 } else if (lua_isnoneornil(L
, 1)) {
91 while ((gr
= getgrent())) {
92 nixio__push_group(L
, gr
);
93 lua_rawseti(L
, -2, ++i
);
97 return nixio__perror(L
);
103 return luaL_argerror(L
, 1, "supported values: <groupname>, <gid>");
107 return nixio__perror(L
);
109 return nixio__push_group(L
, gr
);
113 static int nixio__push_passwd(lua_State
*L
, struct passwd
*pw
) {
114 lua_createtable(L
, 0, 7);
115 lua_pushstring(L
, pw
->pw_name
);
116 lua_setfield(L
, -2, "name");
117 lua_pushstring(L
, pw
->pw_passwd
);
118 lua_setfield(L
, -2, "passwd");
119 lua_pushinteger(L
, pw
->pw_gid
);
120 lua_setfield(L
, -2, "gid");
121 lua_pushinteger(L
, pw
->pw_uid
);
122 lua_setfield(L
, -2, "uid");
123 lua_pushstring(L
, pw
->pw_dir
);
124 lua_setfield(L
, -2, "dir");
125 lua_pushstring(L
, pw
->pw_gecos
);
126 lua_setfield(L
, -2, "gecos");
127 lua_pushstring(L
, pw
->pw_shell
);
128 lua_setfield(L
, -2, "shell");
132 static int nixio_getpw(lua_State
*L
) {
135 if (lua_isnumber(L
, 1)) {
136 pw
= getpwuid(lua_tointeger(L
, 1));
137 } else if (lua_isstring(L
, 1)) {
138 pw
= getpwnam(lua_tostring(L
, 1));
139 } else if (lua_isnoneornil(L
, 1)) {
144 while ((pw
= getpwent())) {
145 nixio__push_passwd(L
, pw
);
146 lua_rawseti(L
, -2, ++i
);
150 return nixio__perror(L
);
156 return luaL_argerror(L
, 1, "supported values: <username>, <uid>");
160 return nixio__perror(L
);
162 return nixio__push_passwd(L
, pw
);
168 static int nixio__push_spwd(lua_State
*L
, struct spwd
*sp
) {
169 lua_createtable(L
, 0, 9);
170 lua_pushstring(L
, sp
->sp_namp
);
171 lua_setfield(L
, -2, "namp");
172 lua_pushinteger(L
, sp
->sp_expire
);
173 lua_setfield(L
, -2, "expire");
174 lua_pushinteger(L
, sp
->sp_flag
);
175 lua_setfield(L
, -2, "flag");
176 lua_pushinteger(L
, sp
->sp_inact
);
177 lua_setfield(L
, -2, "inact");
178 lua_pushinteger(L
, sp
->sp_lstchg
);
179 lua_setfield(L
, -2, "lstchg");
180 lua_pushinteger(L
, sp
->sp_max
);
181 lua_setfield(L
, -2, "max");
182 lua_pushinteger(L
, sp
->sp_min
);
183 lua_setfield(L
, -2, "min");
184 lua_pushinteger(L
, sp
->sp_warn
);
185 lua_setfield(L
, -2, "warn");
186 lua_pushstring(L
, sp
->sp_pwdp
);
187 lua_setfield(L
, -2, "pwdp");
191 static int nixio_getsp(lua_State
*L
) {
194 if (lua_isstring(L
, 1)) {
195 sp
= getspnam(lua_tostring(L
, 1));
196 } else if (lua_isnoneornil(L
, 1)) {
201 while ((sp
= getspent())) {
202 nixio__push_spwd(L
, sp
);
203 lua_rawseti(L
, -2, ++i
);
207 return nixio__perror(L
);
213 return luaL_argerror(L
, 1, "supported values: <username>");
217 return nixio__perror(L
);
219 return nixio__push_spwd(L
, sp
);
222 #endif /* !NO_SHADOW */
225 static int nixio_crypt(lua_State
*L
) {
226 const char *key
= luaL_checkstring(L
, 1);
227 const char *salt
= luaL_checkstring(L
, 2);
228 const char *hash
= crypt(key
, salt
);
231 lua_pushstring(L
, hash
);
241 static const luaL_Reg R
[] = {
242 {"crypt", nixio_crypt
},
243 {"getgr", nixio_getgr
},
244 {"getpw", nixio_getpw
},
247 {"getsp", nixio_getsp
},
253 #else /* __WINNT__ */
255 static const luaL_Reg R
[] = {
261 void nixio_open_user(lua_State
*L
) {
262 luaL_register(L
, NULL
, R
);