From f12ef3fd3851bb69aa2fbadd713ccff3298b7fab Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 14 Oct 2019 10:35:45 -0600 Subject: Create script for detecting duplicate login names --- README.md | 19 +++++++++++++++++-- bin/duplicate-login-names | 13 +++++++++++++ test/duplicate-login-names_test.bats | 15 +++++++++++++++ test/fixtures/passwd_with_duplicate_login_names | 3 +++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100755 bin/duplicate-login-names create mode 100644 test/duplicate-login-names_test.bats create mode 100644 test/fixtures/passwd_with_duplicate_login_names diff --git a/README.md b/README.md index bbc7e94..1adf5c9 100644 --- a/README.md +++ b/README.md @@ -1171,6 +1171,7 @@ Rootly Powers and Processes ```bash #!/bin/bash + cd "$(dirname "$0")/.." FILE=${1:-'/etc/passwd'} cat "$FILE" | cut -d: -f1,3 | grep ':0' @@ -1181,9 +1182,10 @@ Rootly Powers and Processes ```bash #!/bin/bash + cd "$(dirname "$0")/.." FILE=${1:-'/etc/passwd'} - grep -E '^\w+::.*' $FILE + grep -E '^\w+::.*' "$FILE" ``` c. Find any sets of entries that have duplicate UIDs. @@ -1191,7 +1193,20 @@ Rootly Powers and Processes ```bash #!/bin/bash - cat /etc/passwd | grep -v -E '^#' | awk -F: '{ print $3 }' | sort -n | uniq -d + cd "$(dirname "$0")/.." + FILE=${1:-'/etc/passwd'} + + ids=$(grep -v -E '^#' "$FILE" | awk -F: '{ print $3 }' | sort -n | uniq -d) + + for id in $ids; do + grep -v -E '^#' "$FILE" | awk -F: '{ print $3 " " $1 }' | grep -E "^$id " + done + + if [ -z "$ids" ]; then + exit 0 + else + exit 1 + fi ``` d. Find entries that have duplicate login names. diff --git a/bin/duplicate-login-names b/bin/duplicate-login-names new file mode 100755 index 0000000..af575f3 --- /dev/null +++ b/bin/duplicate-login-names @@ -0,0 +1,13 @@ +#!/bin/bash + +cd "$(dirname "$0")/.." +FILE=${1:-'/etc/passwd'} + +DUPLICATES=$(grep -v -E '^#' "$FILE" | awk -F: '{ print $1 }' | sort | uniq -d) +echo $DUPLICATES + +if [ -z "$DUPLICATES" ]; then + exit 0 +else + exit 1 +fi diff --git a/test/duplicate-login-names_test.bats b/test/duplicate-login-names_test.bats new file mode 100644 index 0000000..f91d0a2 --- /dev/null +++ b/test/duplicate-login-names_test.bats @@ -0,0 +1,15 @@ +#!/usr/bin/env bats + +load test_helper + +@test "it finds users with duplicate login names" { + run ./bin/duplicate-login-names test/fixtures/passwd_with_duplicate_login_names + + assert_failure $'root' +} + +@test "it does not find any duplicate login names" { + run ./bin/duplicate-login-names test/fixtures/passwd + + assert_success +} diff --git a/test/fixtures/passwd_with_duplicate_login_names b/test/fixtures/passwd_with_duplicate_login_names new file mode 100644 index 0000000..e943ab4 --- /dev/null +++ b/test/fixtures/passwd_with_duplicate_login_names @@ -0,0 +1,3 @@ +root:x:0:0:root:/root:/bin/bash +bin:x:1:1:bin:/bin:/sbin/nologin +root:x:2:2:daemon:/sbin:/sbin/nologin -- cgit v1.2.3