Software /
code /
prosody
Comparison
util-src/pposix.c @ 1578:5bd8b3bdbfbc
pposix: Add setgid() function
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 23 Jul 2009 03:40:01 +0100 |
parent | 1565:f1eeb049a0a5 |
child | 1579:95698f021c5d |
comparison
equal
deleted
inserted
replaced
1577:c0e6d11e35ce | 1578:5bd8b3bdbfbc |
---|---|
23 #include <sys/stat.h> | 23 #include <sys/stat.h> |
24 #include <fcntl.h> | 24 #include <fcntl.h> |
25 | 25 |
26 #include <syslog.h> | 26 #include <syslog.h> |
27 #include <pwd.h> | 27 #include <pwd.h> |
28 #include <grp.h> | |
28 | 29 |
29 #include <string.h> | 30 #include <string.h> |
30 #include <errno.h> | 31 #include <errno.h> |
31 #include "lua.h" | 32 #include "lua.h" |
32 #include "lauxlib.h" | 33 #include "lauxlib.h" |
289 lua_pushboolean(L, 0); | 290 lua_pushboolean(L, 0); |
290 lua_pushstring(L, "invalid-uid"); | 291 lua_pushstring(L, "invalid-uid"); |
291 return 2; | 292 return 2; |
292 } | 293 } |
293 | 294 |
295 int lc_setgid(lua_State* L) | |
296 { | |
297 int gid = -1; | |
298 if(lua_gettop(L) < 1) | |
299 return 0; | |
300 if(!lua_isnumber(L, 1) && lua_tostring(L, 1)) | |
301 { | |
302 /* Passed GID is actually a string, so look up the GID */ | |
303 struct group *g; | |
304 g = getgrnam(lua_tostring(L, 1)); | |
305 if(!g) | |
306 { | |
307 lua_pushboolean(L, 0); | |
308 lua_pushstring(L, "no-such-group"); | |
309 return 2; | |
310 } | |
311 gid = g->gr_gid; | |
312 } | |
313 else | |
314 { | |
315 gid = lua_tonumber(L, 1); | |
316 } | |
317 | |
318 if(gid>-1) | |
319 { | |
320 /* Ok, attempt setgid */ | |
321 errno = 0; | |
322 if(setgid(gid)) | |
323 { | |
324 /* Fail */ | |
325 lua_pushboolean(L, 0); | |
326 switch(errno) | |
327 { | |
328 case EINVAL: | |
329 lua_pushstring(L, "invalid-gid"); | |
330 break; | |
331 case EPERM: | |
332 lua_pushstring(L, "permission-denied"); | |
333 break; | |
334 default: | |
335 lua_pushstring(L, "unknown-error"); | |
336 } | |
337 return 2; | |
338 } | |
339 else | |
340 { | |
341 /* Success! */ | |
342 lua_pushboolean(L, 1); | |
343 return 1; | |
344 } | |
345 } | |
346 | |
347 /* Seems we couldn't find a valid GID to switch to */ | |
348 lua_pushboolean(L, 0); | |
349 lua_pushstring(L, "invalid-gid"); | |
350 return 2; | |
351 } | |
352 | |
294 /* Like POSIX's setrlimit()/getrlimit() API functions. | 353 /* Like POSIX's setrlimit()/getrlimit() API functions. |
295 * | 354 * |
296 * Syntax: | 355 * Syntax: |
297 * pposix.setrlimit( resource, soft limit, hard limit) | 356 * pposix.setrlimit( resource, soft limit, hard limit) |
298 * | 357 * |
418 lua_pushcfunction(L, lc_getpid); | 477 lua_pushcfunction(L, lc_getpid); |
419 lua_setfield(L, -2, "getpid"); | 478 lua_setfield(L, -2, "getpid"); |
420 | 479 |
421 lua_pushcfunction(L, lc_getuid); | 480 lua_pushcfunction(L, lc_getuid); |
422 lua_setfield(L, -2, "getuid"); | 481 lua_setfield(L, -2, "getuid"); |
482 lua_pushcfunction(L, lc_getgid); | |
483 lua_setfield(L, -2, "getgid"); | |
423 | 484 |
424 lua_pushcfunction(L, lc_setuid); | 485 lua_pushcfunction(L, lc_setuid); |
425 lua_setfield(L, -2, "setuid"); | 486 lua_setfield(L, -2, "setuid"); |
487 lua_pushcfunction(L, lc_setgid); | |
488 lua_setfield(L, -2, "setgid"); | |
426 | 489 |
427 lua_pushcfunction(L, lc_setrlimit); | 490 lua_pushcfunction(L, lc_setrlimit); |
428 lua_setfield(L, -2, "setrlimit"); | 491 lua_setfield(L, -2, "setrlimit"); |
429 | 492 |
430 lua_pushcfunction(L, lc_getrlimit); | 493 lua_pushcfunction(L, lc_getrlimit); |