Skip to content

Commit 753fbf5

Browse files
Merge pull request #16 from RandomCoderOrg/7-add-useradd-option
7 add useradd option
2 parents 73a7ab1 + 114caf8 commit 753fbf5

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
__pycache__
2+
logictest.py
3+
.vscode

bash/adduser.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/bash
2+
3+
# #############################################################################
4+
# (C) @RandomCoderOrg @SaicharanKandukuri 2022
5+
# A useradd wrapper to add a new non-root user for proot linux container
6+
# #############################################################################
7+
8+
REQUIRED_DEPS="openssl"
9+
NOPASSWD=false
10+
11+
# sudo check
12+
[[ $EUID -ne 0 ]] && die "You need to be root to run this script"
13+
14+
# check for dependencies
15+
for debs in $REQUIRED_DEPS; do
16+
if ! dpkg -s "$debs" >/dev/null 2>&1; then
17+
echo "Missing dependency: $debs"
18+
fi
19+
done
20+
21+
# check for arguments
22+
if [[ $# -le 1 ]]; then
23+
echo "Usage: $0 [ -u | --user | --username ] <username> [ -p | --passwd | --password ] <password> [options]"
24+
echo "options:"
25+
echo "--nopasswd -> do not set a password"
26+
exit 1
27+
fi
28+
29+
while [[ $# -gt 0 ]]; do
30+
case $1 in
31+
-u | --user | --username)
32+
USERNAME="$2"
33+
shift 2
34+
;;
35+
-p | --passwd | --password)
36+
PASSWORD="$2"
37+
shift 2
38+
;;
39+
--nopasswd) NOPASSWD=true ;;
40+
esac
41+
done
42+
43+
if [ -d /home/$USERNAME ]; then
44+
echo "User $USERNAME already exists"
45+
exit 1
46+
fi
47+
48+
echo "creating user $USERNAME"
49+
if $NOPASSWD; then
50+
useradd -m -G sudo -p "$(openssl passwd -1 "$PASSWORD")" -d /home/"$USERNAME" -k /etc/skel -s "$SHELL" "$USERNAME"
51+
echo "Adding user $USERNAME to sudoers "
52+
echo "$USERNAME" ALL=\(root\) ALL >/etc/sudoers.d/"$USERNAME"
53+
chmod 0440 /etc/sudoers.d/"$USERNAME"
54+
else
55+
useradd -m -G sudo -p "$(openssl passwd -1 "$PASSWORD")" -d /home/"$USERNAME" -k /etc/skel -s "$SHELL" "$USERNAME"
56+
echo "Adding user $USERNAME to sudoers"
57+
echo "$USERNAME" ALL=\(root\) NOPASSWD: ALL >/etc/sudoers.d/"$USERNAME"
58+
chmod 0440 /etc/sudoers.d/"$USERNAME"
59+
fi
60+
61+
# TODO: copy user layout from root to new user
62+
echo "Done"

install.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ if [[ -f /usr/bin/udroid-upgrade ]]; then
6666
rm -rvf /usr/bin/udroid-upgrade
6767
fi
6868

69+
if [[ -f /usr/bin/udroid-adduser ]]; then
70+
rm -rvf /usr/bin/udroid-adduser
71+
fi
72+
6973
ln -sv /usr/share/udroid/main.sh /usr/bin/startvnc || {
7074
die "Failed to create symlink"
7175
}
@@ -78,5 +82,8 @@ ln -sv /usr/share/udroid/main.sh /usr/bin/udroid-upgrade-check || {
7882
ln -sv /usr/share/udroid/main.sh /usr/bin/udroid-upgrade || {
7983
die "Failed to create symlink"
8084
}
85+
ln -sv /usr/share/udroid/bash/adduser.sh /usr/bin/udroid-adduser || {
86+
die "Failed to create symlink"
87+
}
8188

8289
shout "Installation Complete."

utils/pkgresolver.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
import logging
3+
4+
5+
def resolvdep(deps: list):
6+
"""Function to check & install debian dependencies
7+
8+
Args:
9+
deps (list): dependencies in standard python list format
10+
11+
ENV:
12+
FORCE_RESOLV_DEPS : set this os environment varibale to force install missing dependencies
13+
14+
Returns:
15+
1: when missing dependencie
16+
2: when installation failed
17+
0: normal successfull exit
18+
"""
19+
missing_deps = None
20+
installdeps = None
21+
22+
for dep in deps:
23+
if os.WEXITSTATUS(os.system(f"which {dep} >> /dev/null")):
24+
logging.info(f"{dep} found in path.")
25+
26+
if os.getenv('FORCE_RESOLV_DEPS') is not None:
27+
installdeps += dep
28+
else:
29+
missing_deps += dep
30+
logging.warning(f"Missing dependencie {dep}.")
31+
32+
# check if any missing dependencies
33+
if missing_deps is not None:
34+
return 1
35+
36+
# finally try to install dependencies
37+
if installdeps is not None:
38+
logging.info(f"Installing dependencies: [{installdeps}]")
39+
if os.WEXITSTATUS(os.system(f"apt install -y {installdeps}")) != 0:
40+
return 2
41+
else:
42+
return 0

0 commit comments

Comments
 (0)