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;
        }
    );
    
    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)