【问题标题】:How to Assign User-Account Name and Directory to Array in Bash/sh?如何在 Bash/sh 中将用户帐户名称和目录分配给数组?
【发布时间】:2019-11-12 04:59:02
【问题描述】:

我正在尝试在 Red Hat Enterprise Linux (RHEL) 系统上为 STIG 测试创建一个漏洞 ID V-72017 的 bash 脚本。我的任务是确保所有用户权限的八进制值不超过 0750

我有能力通过使用来收集用户的权限八进制值

    stat -c "%a" /home/$username

我正在尝试使用命令创建一个 $username(或目录)数组(输出系统上每个用户的名称):

    eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)} | cut -d: -f1

我计划将此输出映射到一个数组,可能是一个 while 循环。这是一个可能的解决方案吗?

来自以下的语法错误:

    (eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)} | cut -d: -f1) | while read -r line
    do
      myarray+=line
      stat -c "%a" /home/$line
    done

所需的输出案例 1:

    Users:
    rob
    bob
    Exit Fail: bob has permission octal value 0755.

所需的输出案例 2:

    Users:
    rob
    bob
    Exit Pass: All users have permission octal value of 0750 or less.

【问题讨论】:

    标签: bash command-line sh rhel user-accounts


    【解决方案1】:

    您已找到所有登录用户。正则表达式可用于检查主目录的权限。

    echo "Users: "                                                                   
    (eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)} | cut -d: -f1) | while read -r line
    do                                                                               
      echo $line                                                                     
      perm=$(stat -c "%a" /home/$line)                                               
      [[ "$perm" =~ [0-7][0,1,4,5][0] ]] || echo "Exit fail: $line has permission octal value $perm"                                                                            
    done
    

    也许你想调整输出形式。

    【讨论】:

      【解决方案2】:

      建议尽量避免使用eval。更何况 如果您正在调查系统安全状态。请尝试 改为:

      #!/bin/bash
      
      perm=0750       # system policy
      uid_min=$(sed -n '/^UID_MIN/ s/[^0-9]*\([0-9]\+\).*/\1/p' "/etc/login.defs")
      uid_max=$(sed -n '/^UID_MAX/ s/[^0-9]*\([0-9]\+\).*/\1/p' "/etc/login.defs")
      
      # read /etc/passwd and process line by line
      while IFS=: read -ra a; do
          # now ${a[0]} holds username and ${a[2]} holds uid
          if (( ${a[2]} >= uid_min && ${a[2]} <= uid_max )); then
              # narrow down the users whose uid is within the range
              users+=("${a[0]}")
              # check the user's permission
              userperm="0$(stat -c "%a" "/home/${a[0]}")"
              if (( (~ perm) & userperm )); then
                  # the user's permission exceeds the limitation $perm
                  fail+=("$(printf "%s has permission octal value 0%o." "${a[0]}" "$userperm")")
              fi
          fi
      done < "/etc/passwd"
      
      echo "Users:"
      for i in "${users[@]}"; do
          echo "$i"
      done
      
      if (( ${#fail[@]} == 0 )); then
          printf "Exit Pass: All users have permission octal value of 0%o or less.\n" "$perm"
      else
          for i in "${fail[@]}"; do
              printf "Exit Fail: %s\n" "$i"
          done
      fi
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-08-03
        • 1970-01-01
        • 2016-10-06
        • 2014-01-04
        • 2019-10-24
        • 2019-09-19
        • 1970-01-01
        相关资源
        最近更新 更多