| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | passwd_file=`mktemp` |
| 4 | |
| 5 | # getpwent returns duplicate entries for root and nobody users |
| 6 | # due to a bug in systemd-userdb.service that occurs |
| 7 | # in systemd 245 |
| 8 | # https://github.com/systemd/systemd/issues/15160 |
| 9 | if which systemctl &>/dev/null && \ |
| 10 | [[ `systemctl --version | grep "systemd 245"` =~ "245" ]] ; then |
| 11 | grep -Ev '^(root|nobody)' /etc/passwd > "$passwd_file" |
| 12 | else |
| 13 | case `uname` in |
| 14 | # The first two lines of /etc/passwd on FreeBSD are not account entries |
| 15 | FreeBSD) |
| 16 | sed 1,2d /etc/passwd > $passwd_file |
| 17 | ;; |
| 18 | *) |
| 19 | cp /etc/passwd "$passwd_file" |
| 20 | ;; |
| 21 | esac |
| 22 | fi |
| 23 | |
| 24 | LINES_COUNT=`cat "$passwd_file" | wc -l` |
| 25 | |
| 26 | function getField { |
| 27 | LINE=`sed -n "${I}p" "$passwd_file"` |
| 28 | case $1 in |
| 29 | 'username' ) |
| 30 | echo $LINE | awk -F':' '{print $1}' |
| 31 | ;; |
| 32 | 'password' ) |
| 33 | echo $LINE | awk -F':' '{print $2}' |
| 34 | ;; |
| 35 | 'user_id' ) |
| 36 | echo $LINE | awk -F':' '{print $3}' |
| 37 | ;; |
| 38 | 'group_id' ) |
| 39 | echo $LINE | awk -F':' '{print $4}' |
| 40 | ;; |
| 41 | 'gcos' ) |
| 42 | echo $LINE | awk -F':' '{gsub(/&/,"&",$5); print $5}' |
| 43 | ;; |
| 44 | 'home_dir' ) |
| 45 | echo $LINE | awk -F':' '{print $6}' |
| 46 | ;; |
| 47 | 'login_shell' ) |
| 48 | echo $LINE | awk -F':' '{print $7}' |
| 49 | ;; |
| 50 | esac |
| 51 | } |
| 52 | |
| 53 | cat <<EOF |
| 54 | <?xml version="1.0"?> |
| 55 | <oval_definitions xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5" xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ind-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent" xmlns:unix-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#unix" xmlns:lin-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#linux" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5" xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-definitions-5#unix unix-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#independent independent-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#linux linux-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5 oval-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd"> |
| 56 | |
| 57 | <generator> |
| 58 | <oval:product_name>password</oval:product_name> |
| 59 | <oval:product_version>1.0</oval:product_version> |
| 60 | <oval:schema_version>5.4</oval:schema_version> |
| 61 | <oval:timestamp>2008-03-31T00:00:00-00:00</oval:timestamp> |
| 62 | </generator> |
| 63 | |
| 64 | <definitions> |
| 65 | |
| 66 | <definition class="compliance" version="1" id="oval:1:def:1"> <!-- comment="true" --> |
| 67 | <metadata> |
| 68 | <title></title> |
| 69 | <description></description> |
| 70 | </metadata> |
| 71 | <criteria> |
| 72 | <criteria operator="AND"> |
| 73 | EOF |
| 74 | I=1 |
| 75 | while [ $I -le $LINES_COUNT ]; do |
| 76 | echo "<criterion test_ref=\"oval:1:tst:${I}\"/>" |
| 77 | I=$[$I+1] |
| 78 | done |
| 79 | cat <<EOF |
| 80 | </criteria> |
| 81 | </criteria> |
| 82 | </definition> |
| 83 | |
| 84 | </definitions> |
| 85 | |
| 86 | <tests> |
| 87 | |
| 88 | EOF |
| 89 | |
| 90 | I=1 |
| 91 | while [ $I -le $LINES_COUNT ]; do |
| 92 | cat <<EOF |
| 93 | <password_test version="1" id="oval:1:tst:${I}" check="all" comment="true" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#unix"> |
| 94 | <object object_ref="oval:1:obj:${I}"/> |
| 95 | <state state_ref="oval:1:ste:${I}"/> |
| 96 | </password_test> |
| 97 | EOF |
| 98 | I=$[$I+1] |
| 99 | done |
| 100 | |
| 101 | cat <<EOF |
| 102 | </tests> |
| 103 | |
| 104 | <objects> |
| 105 | EOF |
| 106 | |
| 107 | I=1 |
| 108 | while [ $I -le $LINES_COUNT ]; do |
| 109 | cat <<EOF |
| 110 | <password_object version="1" id="oval:1:obj:${I}" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#unix"> |
| 111 | <username>`getField "username" ${I}`</username> |
| 112 | </password_object> |
| 113 | EOF |
| 114 | I=$[$I+1] |
| 115 | done |
| 116 | |
| 117 | cat <<EOF |
| 118 | </objects> |
| 119 | |
| 120 | <states> |
| 121 | EOF |
| 122 | |
| 123 | I=1 |
| 124 | while [ $I -le $LINES_COUNT ]; do |
| 125 | cat <<EOF |
| 126 | <password_state version="1" id="oval:1:ste:${I}" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#unix"> |
| 127 | <username>`getField 'username' $I`</username> |
| 128 | <password>`getField 'password' $I`</password> |
| 129 | <user_id datatype="int">`getField 'user_id' $I`</user_id> |
| 130 | <group_id datatype="int">`getField 'group_id' $I`</group_id> |
| 131 | <gcos>`getField 'gcos' $I`</gcos> |
| 132 | <home_dir>`getField 'home_dir' $I`</home_dir> |
| 133 | <login_shell>`getField 'login_shell' $I`</login_shell> |
| 134 | </password_state> |
| 135 | EOF |
| 136 | I=$[$I+1] |
| 137 | done |
| 138 | |
| 139 | cat <<EOF |
| 140 | </states> |
| 141 | |
| 142 | </oval_definitions> |
| 143 | EOF |