-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify-user.sh
56 lines (42 loc) · 1.52 KB
/
verify-user.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
# This script verifies the identity of the user running it by checking
# both the User ID (UID) and username.
# It performs the following actions:
# 1. Displays the UID of the current user.
# 2. Checks if the user's UID matches a predefined value (configurable).
# - If there's a mismatch, it exits with an error message.
# 3. Retrieves the username using the `id` command.
# - If the `id` command fails, it exits with an error message.
# 4. Optionally checks if the username matches another predefined value.
# - If there's a username mismatch (and username check is enabled),
# it exits with an error message.
# This script can be used to restrict access to the script based on
# specific user credentials (UID and/or username).
# Edit the variables `UID_TO_TEST_FOR` and `USER_NAME_TO_TEST` to customise
# the expected UID and username for allowed execution.
echo "Your UID is ${UID}"
UID_TO_TEST_FOR='1000'
if [[ "${UID_TO_TEST_FOR}" -ne "${UID}" ]]
then
echo "Your UID does not match ${UID_TO_TEST_FOR}"
exit 1
fi
USER_NAME=$(id -un)
if [[ "${?}" -ne 0 ]]
then
echo "id command did not execute succesfully"
exit 1
fi
echo "Your username is ${USER_NAME}"
# Display if the user is 'user1'
USER_NAME_TO_TEST="user1"
if [[ "${USER_NAME_TO_TEST}" = "${USER_NAME}" ]]
then
echo "Your username matches ${USER_NAME_TO_TEST}"
fi
if [[ "${USER_NAME_TO_TEST}" != "${USER_NAME}" ]]
then
echo "Your username doesn't match ${USER_NAME_TO_TEST}"
exit 1
fi
exit 0