From 95ab68a2723390b531de76c5a39e88692d41dceb Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Tue, 1 Oct 2019 08:26:59 +0200 Subject: [PATCH] [__ssh_authorized_keys] Fall back to /etc files if getent(1) is not available Some (embedded) systems don't provide getent(1). The workaround parses /etc/passwd and /etc/group under the assumption that these sysems only use local users and groups. --- .../type/__ssh_authorized_keys/explorer/file | 26 +++++++++++++++-- .../type/__ssh_authorized_keys/explorer/group | 29 +++++++++++++++++-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/cdist/conf/type/__ssh_authorized_keys/explorer/file b/cdist/conf/type/__ssh_authorized_keys/explorer/file index 5a02721a..09d55f6f 100755 --- a/cdist/conf/type/__ssh_authorized_keys/explorer/file +++ b/cdist/conf/type/__ssh_authorized_keys/explorer/file @@ -1,6 +1,7 @@ #!/bin/sh # # 2014 Steven Armstrong (steven-cdist at armstrong.cc) +# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch) # # This file is part of cdist. # @@ -21,7 +22,28 @@ if [ -f "$__object/parameter/file" ]; then cat "$__object/parameter/file" else - owner="$(cat "$__object/parameter/owner" 2>/dev/null || echo "$__object_id")" - home=$(getent passwd "$owner" | cut -d':' -f 6) + if [ -s "$__object/parameter/owner" ] + then + owner=$(cat "$__object/parameter/owner") + else + owner="$__object_id" + fi + + if command -v getent >/dev/null + then + owner_line=$(getent passwd "$owner") + else + case $owner + in + [0-9][0-9]*) + owner_line=$(awk -F: "\$3 == \"${owner}\" { print }" /etc/passwd) + ;; + *) + owner_line=$(awk -F: "\$1 == \"${owner}\" { print }" /etc/passwd) + ;; + esac + fi + + home=$(echo "$owner_line" | cut -d':' -f6) echo "$home/.ssh/authorized_keys" fi diff --git a/cdist/conf/type/__ssh_authorized_keys/explorer/group b/cdist/conf/type/__ssh_authorized_keys/explorer/group index 72a4e314..1bd14840 100755 --- a/cdist/conf/type/__ssh_authorized_keys/explorer/group +++ b/cdist/conf/type/__ssh_authorized_keys/explorer/group @@ -1,6 +1,7 @@ #!/bin/sh # # 2014 Steven Armstrong (steven-cdist at armstrong.cc) +# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch) # # This file is part of cdist. # @@ -18,6 +19,28 @@ # along with cdist. If not, see . # -owner="$(cat "$__object/parameter/owner" 2>/dev/null || echo "$__object_id")" -gid="$(getent passwd "$owner" | cut -d':' -f 4)" -getent group "$gid" || true +if [ -s "$__object/parameter/owner" ] +then + owner=$(cat "$__object/parameter/owner") +else + owner="$__object_id" +fi + +if command -v getent >/dev/null +then + gid=$(getent passwd "$owner" | cut -d':' -f4) + getent group "$gid" || true +else + # Fallback to local file scanning + case $owner + in + [0-9][0-9]*) + gid=$(awk -F: "\$3 == \"${owner}\" { print $4 }" /etc/passwd) + ;; + *) + gid=$(awk -F: "\$1 == \"${owner}\" { print $4 }" /etc/passwd) + ;; + esac + + awk -F: "\$3 == \"$gid\" { print }" /etc/group +fi