summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2019-10-14 10:35:45 -0600
committermo khan <mo.khan@gmail.com>2019-10-14 10:35:45 -0600
commitf12ef3fd3851bb69aa2fbadd713ccff3298b7fab (patch)
treed70ad5378717ba288aea01a71fe1d32084eeecae
parentd32e87b86f6ddad2c2e427718bc9f480e0905055 (diff)
Create script for detecting duplicate login names
-rw-r--r--README.md19
-rwxr-xr-xbin/duplicate-login-names13
-rw-r--r--test/duplicate-login-names_test.bats15
-rw-r--r--test/fixtures/passwd_with_duplicate_login_names3
4 files changed, 48 insertions, 2 deletions
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