Skip to content

Commit

Permalink
Merge pull request #2942 from reubenmiller/fix-user-group-lookup-busybox
Browse files Browse the repository at this point in the history
fix: improve compatibility of user/group existence check in package maintainer scripts
  • Loading branch information
reubenmiller authored Jun 17, 2024
2 parents 82732a9 + 9b4a8bb commit 54b736d
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 16 deletions.
28 changes: 26 additions & 2 deletions configuration/package_scripts/_generated/tedge/apk/postrm
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,32 @@ command_exists() {
command -V "$1" >/dev/null 2>&1
}

group_exists() {
name="$1"
if command_exists id; then
id -g "$name" >/dev/null 2>&1
elif command_exists getent; then
getent group "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/group
fi
}

user_exists() {
name="$1"
if command_exists id; then
id -u "$name" >/dev/null 2>&1
elif command_exists getent; then
getent passwd "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/passwd
fi
}

remove_user_tedge() {
if getent passwd tedge > /dev/null; then
if user_exists tedge; then
if command_exists userdel; then
userdel tedge
elif command_exists deluser; then
Expand All @@ -18,7 +42,7 @@ remove_user_tedge() {
}

remove_tedge_group() {
if getent group tedge > /dev/null; then
if group_exists tedge; then
if command_exists groupdel; then
groupdel tedge
elif command_exists delgroup; then
Expand Down
28 changes: 26 additions & 2 deletions configuration/package_scripts/_generated/tedge/apk/preinst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,32 @@ command_exists() {
command -V "$1" >/dev/null 2>&1
}

group_exists() {
name="$1"
if command_exists id; then
id -g "$name" >/dev/null 2>&1
elif command_exists getent; then
getent group "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/group
fi
}

user_exists() {
name="$1"
if command_exists id; then
id -u "$name" >/dev/null 2>&1
elif command_exists getent; then
getent passwd "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/passwd
fi
}

### Create groups
if ! getent group tedge >/dev/null; then
if ! group_exists tedge; then
if command_exists groupadd; then
groupadd --system tedge
elif command_exists addgroup; then
Expand All @@ -18,7 +42,7 @@ fi

### Create users
# Create user tedge with no home(--no-create-home), no login(--shell) and in group tedge(--gid)
if ! getent passwd tedge >/dev/null; then
if ! user_exists tedge; then
if command_exists useradd; then
useradd --system --no-create-home --shell /sbin/nologin --gid tedge tedge
elif command_exists adduser; then
Expand Down
28 changes: 26 additions & 2 deletions configuration/package_scripts/_generated/tedge/deb/postrm
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,32 @@ command_exists() {
command -V "$1" >/dev/null 2>&1
}

group_exists() {
name="$1"
if command_exists id; then
id -g "$name" >/dev/null 2>&1
elif command_exists getent; then
getent group "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/group
fi
}

user_exists() {
name="$1"
if command_exists id; then
id -u "$name" >/dev/null 2>&1
elif command_exists getent; then
getent passwd "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/passwd
fi
}

remove_user_tedge() {
if getent passwd tedge > /dev/null; then
if user_exists tedge; then
if command_exists userdel; then
userdel tedge
elif command_exists deluser; then
Expand All @@ -18,7 +42,7 @@ remove_user_tedge() {
}

remove_tedge_group() {
if getent group tedge > /dev/null; then
if group_exists tedge; then
if command_exists groupdel; then
groupdel tedge
elif command_exists delgroup; then
Expand Down
28 changes: 26 additions & 2 deletions configuration/package_scripts/_generated/tedge/deb/preinst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,32 @@ command_exists() {
command -V "$1" >/dev/null 2>&1
}

group_exists() {
name="$1"
if command_exists id; then
id -g "$name" >/dev/null 2>&1
elif command_exists getent; then
getent group "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/group
fi
}

user_exists() {
name="$1"
if command_exists id; then
id -u "$name" >/dev/null 2>&1
elif command_exists getent; then
getent passwd "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/passwd
fi
}

### Create groups
if ! getent group tedge >/dev/null; then
if ! group_exists tedge; then
if command_exists groupadd; then
groupadd --system tedge
elif command_exists addgroup; then
Expand All @@ -18,7 +42,7 @@ fi

### Create users
# Create user tedge with no home(--no-create-home), no login(--shell) and in group tedge(--gid)
if ! getent passwd tedge >/dev/null; then
if ! user_exists tedge; then
if command_exists useradd; then
useradd --system --no-create-home --shell /sbin/nologin --gid tedge tedge
elif command_exists adduser; then
Expand Down
28 changes: 26 additions & 2 deletions configuration/package_scripts/_generated/tedge/rpm/postrm
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,32 @@ command_exists() {
command -V "$1" >/dev/null 2>&1
}

group_exists() {
name="$1"
if command_exists id; then
id -g "$name" >/dev/null 2>&1
elif command_exists getent; then
getent group "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/group
fi
}

user_exists() {
name="$1"
if command_exists id; then
id -u "$name" >/dev/null 2>&1
elif command_exists getent; then
getent passwd "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/passwd
fi
}

remove_user_tedge() {
if getent passwd tedge > /dev/null; then
if user_exists tedge; then
if command_exists userdel; then
userdel tedge
elif command_exists deluser; then
Expand All @@ -18,7 +42,7 @@ remove_user_tedge() {
}

remove_tedge_group() {
if getent group tedge > /dev/null; then
if group_exists tedge; then
if command_exists groupdel; then
groupdel tedge
elif command_exists delgroup; then
Expand Down
28 changes: 26 additions & 2 deletions configuration/package_scripts/_generated/tedge/rpm/preinst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,32 @@ command_exists() {
command -V "$1" >/dev/null 2>&1
}

group_exists() {
name="$1"
if command_exists id; then
id -g "$name" >/dev/null 2>&1
elif command_exists getent; then
getent group "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/group
fi
}

user_exists() {
name="$1"
if command_exists id; then
id -u "$name" >/dev/null 2>&1
elif command_exists getent; then
getent passwd "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/passwd
fi
}

### Create groups
if ! getent group tedge >/dev/null; then
if ! group_exists tedge; then
if command_exists groupadd; then
groupadd --system tedge
elif command_exists addgroup; then
Expand All @@ -18,7 +42,7 @@ fi

### Create users
# Create user tedge with no home(--no-create-home), no login(--shell) and in group tedge(--gid)
if ! getent passwd tedge >/dev/null; then
if ! user_exists tedge; then
if command_exists useradd; then
useradd --system --no-create-home --shell /sbin/nologin --gid tedge tedge
elif command_exists adduser; then
Expand Down
28 changes: 26 additions & 2 deletions configuration/package_scripts/tedge/postrm
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,32 @@ command_exists() {
command -V "$1" >/dev/null 2>&1
}

group_exists() {
name="$1"
if command_exists id; then
id -g "$name" >/dev/null 2>&1
elif command_exists getent; then
getent group "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/group
fi
}

user_exists() {
name="$1"
if command_exists id; then
id -u "$name" >/dev/null 2>&1
elif command_exists getent; then
getent passwd "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/passwd
fi
}

remove_user_tedge() {
if getent passwd tedge > /dev/null; then
if user_exists tedge; then
if command_exists userdel; then
userdel tedge
elif command_exists deluser; then
Expand All @@ -18,7 +42,7 @@ remove_user_tedge() {
}

remove_tedge_group() {
if getent group tedge > /dev/null; then
if group_exists tedge; then
if command_exists groupdel; then
groupdel tedge
elif command_exists delgroup; then
Expand Down
28 changes: 26 additions & 2 deletions configuration/package_scripts/tedge/preinst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,32 @@ command_exists() {
command -V "$1" >/dev/null 2>&1
}

group_exists() {
name="$1"
if command_exists id; then
id -g "$name" >/dev/null 2>&1
elif command_exists getent; then
getent group "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/group
fi
}

user_exists() {
name="$1"
if command_exists id; then
id -u "$name" >/dev/null 2>&1
elif command_exists getent; then
getent passwd "$name" >/dev/null 2>&1
else
# Fallback to plain grep, as busybox does not have getent
grep -q "^${name}:" /etc/passwd
fi
}

### Create groups
if ! getent group tedge >/dev/null; then
if ! group_exists tedge; then
if command_exists groupadd; then
groupadd --system tedge
elif command_exists addgroup; then
Expand All @@ -18,7 +42,7 @@ fi

### Create users
# Create user tedge with no home(--no-create-home), no login(--shell) and in group tedge(--gid)
if ! getent passwd tedge >/dev/null; then
if ! user_exists tedge; then
if command_exists useradd; then
useradd --system --no-create-home --shell /sbin/nologin --gid tedge tedge
elif command_exists adduser; then
Expand Down

0 comments on commit 54b736d

Please sign in to comment.