Software /
code /
prosody
Comparison
util-src/pposix.c @ 804:9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 15 Feb 2009 15:56:04 +0000 |
parent | 796:63f56696c66c |
child | 859:43f7e342135d |
comparison
equal
deleted
inserted
replaced
803:5a64649f4b94 | 804:9bc1544c99b7 |
---|---|
20 #include <sys/types.h> | 20 #include <sys/types.h> |
21 #include <sys/stat.h> | 21 #include <sys/stat.h> |
22 #include <fcntl.h> | 22 #include <fcntl.h> |
23 | 23 |
24 #include <syslog.h> | 24 #include <syslog.h> |
25 #include <pwd.h> | |
25 | 26 |
26 #include <string.h> | 27 #include <string.h> |
27 | 28 #include <errno.h> |
28 #include "lua.h" | 29 #include "lua.h" |
29 #include "lauxlib.h" | 30 #include "lauxlib.h" |
30 | 31 |
31 /* Daemonization support */ | 32 /* Daemonization support */ |
32 | 33 |
214 { | 215 { |
215 lua_pushinteger(L, getpid()); | 216 lua_pushinteger(L, getpid()); |
216 return 1; | 217 return 1; |
217 } | 218 } |
218 | 219 |
220 /* UID/GID functions */ | |
221 | |
222 int lc_getuid(lua_State* L) | |
223 { | |
224 lua_pushinteger(L, getuid()); | |
225 return 1; | |
226 } | |
227 | |
228 int lc_getgid(lua_State* L) | |
229 { | |
230 lua_pushinteger(L, getgid()); | |
231 return 1; | |
232 } | |
233 | |
234 int lc_setuid(lua_State* L) | |
235 { | |
236 int uid = -1; | |
237 if(lua_gettop(L) < 1) | |
238 return 0; | |
239 if(!lua_isnumber(L, 1) && lua_tostring(L, 1)) | |
240 { | |
241 /* Passed UID is actually a string, so look up the UID */ | |
242 struct passwd *p; | |
243 p = getpwnam(lua_tostring(L, 1)); | |
244 if(!p) | |
245 { | |
246 lua_pushboolean(L, 0); | |
247 lua_pushstring(L, "no-such-user"); | |
248 return 2; | |
249 } | |
250 uid = p->pw_uid; | |
251 } | |
252 else | |
253 { | |
254 uid = lua_tonumber(L, 1); | |
255 } | |
256 | |
257 if(uid>-1) | |
258 { | |
259 /* Ok, attempt setuid */ | |
260 errno = 0; | |
261 if(setuid(uid)) | |
262 { | |
263 /* Fail */ | |
264 lua_pushboolean(L, 0); | |
265 switch(errno) | |
266 { | |
267 case EINVAL: | |
268 lua_pushstring(L, "invalid-uid"); | |
269 break; | |
270 case EPERM: | |
271 lua_pushstring(L, "permission-denied"); | |
272 break; | |
273 default: | |
274 lua_pushstring(L, "unknown-error"); | |
275 } | |
276 return 2; | |
277 } | |
278 else | |
279 { | |
280 /* Success! */ | |
281 lua_pushboolean(L, 1); | |
282 return 1; | |
283 } | |
284 } | |
285 | |
286 /* Seems we couldn't find a valid UID to switch to */ | |
287 lua_pushboolean(L, 0); | |
288 lua_pushstring(L, "invalid-uid"); | |
289 return 2; | |
290 } | |
291 | |
219 /* Register functions */ | 292 /* Register functions */ |
220 | 293 |
221 int luaopen_util_pposix(lua_State *L) | 294 int luaopen_util_pposix(lua_State *L) |
222 { | 295 { |
223 lua_newtable(L); | 296 lua_newtable(L); |
238 lua_setfield(L, -2, "syslog_setminlevel"); | 311 lua_setfield(L, -2, "syslog_setminlevel"); |
239 | 312 |
240 lua_pushcfunction(L, lc_getpid); | 313 lua_pushcfunction(L, lc_getpid); |
241 lua_setfield(L, -2, "getpid"); | 314 lua_setfield(L, -2, "getpid"); |
242 | 315 |
316 lua_pushcfunction(L, lc_getuid); | |
317 lua_setfield(L, -2, "getuid"); | |
318 | |
319 lua_pushcfunction(L, lc_setuid); | |
320 lua_setfield(L, -2, "setuid"); | |
321 | |
243 lua_pushliteral(L, "pposix"); | 322 lua_pushliteral(L, "pposix"); |
244 lua_setfield(L, -2, "_NAME"); | 323 lua_setfield(L, -2, "_NAME"); |
245 | 324 |
246 lua_pushliteral(L, MODULE_VERSION); | 325 lua_pushliteral(L, MODULE_VERSION); |
247 lua_setfield(L, -2, "_VERSION"); | 326 lua_setfield(L, -2, "_VERSION"); |