/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
- <?php
- namespace pinetd;
- use \Exception;
- class SUID {
- private $uid;
- private $gid;
- function __construct($uid, $gid = null) {
- if (is_numeric($uid)) {
- $info = posix_getpwuid($uid);
- } else {
- $info = posix_getpwnam($uid);
- }
- if (!$info) throw new Exception('SUID: uid '.$uid.' not found on system, please check config');
- $this->uid = $info['uid'];
- $this->gid = $info['gid'];
- $username = $info['name'];
- // do we have gid?
- if (!is_null($gid)) {
- if(is_numeric($gid)) {
- $info = posix_getgrgid($gid);
- } else {
- $info = posix_getgrnam($gid);
- }
- if (!$info) throw new Exception('SUID: Group provided, but can\'t find it on system, please check config');
- $this->gid = $info['gid'];
- }
- posix_initgroups($username, $this->gid); // initialize groups NOW as we might become unable to do that later (ie. after chroot)
- }
- function setIt() {
- if (!posix_setgid($this->gid)) return false;
- if (!posix_setuid($this->uid)) return false;
- return true;
- }
- }