dongqiang1226 2019-03-18 19:04
浏览 268

解析/ etc / passwd文件以获取范围内的用户ID并获取用户名和路径

I am looking to parse an /etc/passwd file and take out only the users within a range of UIDs. I am not very good with functions it seems.

I found the following function, but can not seem to make it work. from how to make PHP lists all Linux Users?

function getUsers() {
    $result = [];
    /** @see http://php.net/manual/en/function.posix-getpwnam.php */
    $keys = ['name', 'passwd', 'uid', 'gid', 'gecos', 'dir', 'shell'];
    $handle = fopen('/etc/passwd', 'r');
    if(!$handle){
    throw new \RuntimeException("failed to open /etc/passwd for reading! ".print_r(error_get_last(),true));
    }
    while ( ($values = fgetcsv($handle, 1000, ':')) !== false ) {
        $result[] = array_combine($keys, $values);
    }
    fclose($handle);
    return $result;
}

Now I believe I need only put the function in the page then later call it with

getUsers()

This is still not a complete solution however, only a part as I then need to get the UIDs that are within a $min and $max range but if I can not get past the above step it is hard to more on to getting the users in the range mentioned.

Perhaps there is a better way to do this?

UPDATE---------------------------

I got this part,my bad

Now i need to parse the /etc/passwd file

(naysayers go away I do not need your security lecture, as there are NO PASSWORDS in /etc/passwrd. if you knew anything about security you would know that!)

The file looks like this

[24] => Array
    (
        [name] => lightdm
        [passwd] => x
        [uid] => 107
        [gid] => 115
        [gecos] => Light Display Manager
        [dir] => /var/lib/lightdm
        [shell] => /bin/false
    )

[25] => Array
    (
        [name] => kodi
        [passwd] => x
        [uid] => 1000
        [gid] => 1000
        [gecos] => ,,,
        [dir] => /home/kodi
        [shell] => /bin/bash
    )

[26] => Array
    (
        [name] => saned
        [passwd] => x
        [uid] => 108
        [gid] => 119
        [gecos] => 
        [dir] => /var/lib/saned
        [shell] => /bin/false
    )

[27] => Array
    (
        [name] => statd
        [passwd] => x
        [uid] => 109
        [gid] => 65534
        [gecos] => 
        [dir] => /var/lib/nfs
        [shell] => /bin/false
    )

[28] => Array
    (
        [name] => sshd
        [passwd] => x
        [uid] => 110
        [gid] => 65534
        [gecos] => 
        [dir] => /var/run/sshd
        [shell] => /usr/sbin/nologin
    )

[29] => Array
    (
        [name] => pulse
        [passwd] => x
        [uid] => 111
        [gid] => 121
        [gecos] => PulseAudio daemon,,,
        [dir] => /var/run/pulse
        [shell] => /bin/false
    )

[30] => Array
    (
        [name] => rtkit
        [passwd] => x
        [uid] => 112
        [gid] => 123
        [gecos] => RealtimeKit,,,
        [dir] => /proc
        [shell] => /bin/false
    )

[31] => Array
    (
        [name] => avahi
        [passwd] => x
        [uid] => 113
        [gid] => 124
        [gecos] => Avahi mDNS daemon,,,
        [dir] => /var/run/avahi-daemon
        [shell] => /bin/false
    )

[32] => Array
    (
        [name] => hplip
        [passwd] => x
        [uid] => 114
        [gid] => 7
        [gecos] => HPLIP system user,,,
        [dir] => /var/run/hplip
        [shell] => /bin/false
    )

[33] => Array
    (
        [name] => spanish
        [passwd] => x
        [uid] => 1001
        [gid] => 1001
        [gecos] => spanish,,,,
        [dir] => /home/spanish
        [shell] => /bin/bash
    )

I think for greatest effeciency is to parse the file once creating a new array . I have set a $minuid and $maxuid that selects the users I want based on the 'uid' key.

WHat I need to get back is a smaller array with ONLY those users with a UID that falls between $maxuid and $minuid then easily be able to get 'name' and 'dir' fields for those relevant users.

I seem kind of lost at how to do this.

  • 写回答

1条回答 默认 最新

  • douyun1546 2019-03-18 23:07
    关注

    First, anyone who wants to complain about security needs to consider the following:

    /etc/passwd is world readable. This is the required state of the file.

    If this is new and/or surprising to you, then you need to brush up on basic security and Linux administration.


    OP, you're going to want to use array_filter(), eg:

    $min = 500;
    $max = 1000;
    $filtered_users = array_filter(
        $input,
        function($a) use ($min, $max) {
            return $a['uid'] >= $min && $a['uid'] <= $max;
        }
    );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值