PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/code/classes/pinetd/SUID.class.php

https://github.com/blekkzor/pinetd2
PHP | 40 lines | 33 code | 6 blank | 1 comment | 9 complexity | 3f672a5873c14ca7ec2300581b587da2 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. namespace pinetd;
  3. use \Exception;
  4. class SUID {
  5. private $uid;
  6. private $gid;
  7. function __construct($uid, $gid = null) {
  8. if (is_numeric($uid)) {
  9. $info = posix_getpwuid($uid);
  10. } else {
  11. $info = posix_getpwnam($uid);
  12. }
  13. if (!$info) throw new Exception('SUID: uid '.$uid.' not found on system, please check config');
  14. $this->uid = $info['uid'];
  15. $this->gid = $info['gid'];
  16. $username = $info['name'];
  17. // do we have gid?
  18. if (!is_null($gid)) {
  19. if(is_numeric($gid)) {
  20. $info = posix_getgrgid($gid);
  21. } else {
  22. $info = posix_getgrnam($gid);
  23. }
  24. if (!$info) throw new Exception('SUID: Group provided, but can\'t find it on system, please check config');
  25. $this->gid = $info['gid'];
  26. }
  27. posix_initgroups($username, $this->gid); // initialize groups NOW as we might become unable to do that later (ie. after chroot)
  28. }
  29. function setIt() {
  30. if (!posix_setgid($this->gid)) return false;
  31. if (!posix_setuid($this->uid)) return false;
  32. return true;
  33. }
  34. }