summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2013-02-25 15:51:03 +0100
committerSimon Rettberg2013-02-25 15:51:03 +0100
commit365ceb07608b583ed7f0db6b420f1d1221f90839 (patch)
treec7c51ebf001df128c8ed501a1e778e320d25cc3f
parentreally fix password generation/replacement (diff)
downloadtm-scripts-365ceb07608b583ed7f0db6b420f1d1221f90839.tar.gz
tm-scripts-365ceb07608b583ed7f0db6b420f1d1221f90839.tar.xz
tm-scripts-365ceb07608b583ed7f0db6b420f1d1221f90839.zip
[helper] make useradd independent of tools build order, [helper] simplify logger
-rw-r--r--helper/logging.inc37
-rw-r--r--helper/useradd.inc31
-rw-r--r--remote/tools/base/base.build10
-rw-r--r--remote/tools/base/base.conf4
4 files changed, 52 insertions, 30 deletions
diff --git a/helper/logging.inc b/helper/logging.inc
index 7c8bc78c..4daf1e12 100644
--- a/helper/logging.inc
+++ b/helper/logging.inc
@@ -1,14 +1,19 @@
+if [ "x$MLTK_QUIET" == "x" ]; then #ifndef _MLTK_QUIET
+#define _MLTK_QUIET
+
MLTK_QUIET=0
+exec 6>&2
+
LOG_DIR=${ROOT_DIR}/logs
-mkdir -p $LOG_DIR
+mkdir -p "$LOG_DIR"
set_quiet () {
if [ "x$DEBUG" != "x1" -a "x$MLTK_QUIET" != "x1" ]; then
- exec 6>&1 > $LOG_DIR/stdout.log
- exec 7>&2 2> $LOG_DIR/stderr.log
+ exec 6>&1 > "$LOG_DIR/stdout.log"
+ exec 7>&2 2> "$LOG_DIR/stderr.log"
MLTK_QUIET="1"
fi
}
@@ -23,35 +28,25 @@ unset_quiet () {
pinfo () {
- if [ "x$MLTK_QUIET" = "x1" ]; then
- echo -e "\033[38;5;10m[info]\033[0m $TOOL_STR $@" >&6
- else
- echo -e "\033[38;5;10m[info]\033[0m $TOOL_STR $@" >&2
- fi
+ echo -e "\033[38;5;10m[info]\033[0m $TOOL_STR $@" >&6
}
+
perror () {
- if [ "x$MLTK_QUIET" = "x1" ]; then
- echo -e "\033[38;5;9m[error]\033[0m $TOOL_STR $@" >&6
- else
- echo -e "\033[38;5;9m[error]\033[0m $TOOL_STR $@" >&2
- fi
+ echo -e "\033[38;5;9m[error]\033[0m $TOOL_STR $@" >&6
qnd_exit
}
+
pwarning () {
- if [ "x$MLTK_QUIET" = "x1" ]; then
- echo -e "\033[38;5;11m[warning]\033[0m $TOOL_STR $@" >&6
- else
- echo -e "\033[38;5;11m[warning]\033[0m $TOOL_STR $@" >&2
- fi
+ echo -e "\033[38;5;11m[warning]\033[0m $TOOL_STR $@" >&6
}
pdebug () {
if [ "x$DEBUG" != "x1" ]; then
echo -e "[DEBUG] $TOOL_STR $@" >&2
- elif [ "x$MLTK_QUIET" = "x1" ]; then
- echo -e "\033[38;5;6m[debug]\033[0m $TOOL_STR $@" >&6
else
- echo -e "\033[38;5;6m[debug]\033[0m $TOOL_STR $@" >&2
+ echo -e "\033[38;5;6m[debug]\033[0m $TOOL_STR $@" >&6
fi
}
+#endif
+fi
diff --git a/helper/useradd.inc b/helper/useradd.inc
index 8b1315c6..d1191aed 100644
--- a/helper/useradd.inc
+++ b/helper/useradd.inc
@@ -70,6 +70,7 @@ add_user() {
local _PASSWD=${INIT_DIR}/etc/passwd
local _GROUP=${INIT_DIR}/etc/group
local _SHADOW=${INIT_DIR}/etc/shadow
+ init_users_and_groups
[ ! -f "${_PASSWD}" ] && perror "add_user: password file does not exist in target system. (build base first)"
[ ! -f "${_GROUP}" ] && perror "add_user: group file does not exist in target system. (build base first)"
[ ! -f "${_SHADOW}" ] && perror "add_user: shadow file does not exist in target system. (build base first)"
@@ -156,6 +157,7 @@ add_group () {
local _PASSWD=${INIT_DIR}/etc/passwd
local _GROUP=${INIT_DIR}/etc/group
local _SHADOW=${INIT_DIR}/etc/shadow
+ init_users_and_groups
[ ! -f "${_GROUP}" ] && perror "add_user: group file does not exist in target system. (build base first)"
local GROUP=$1
local GROUPID="-"
@@ -178,3 +180,32 @@ add_group () {
pinfo "Created group $GROUP"
}
+init_users_and_groups() {
+ [ -z "${INIT_DIR}" ] && perror "Almost wrecked your local passwd, group and shadow file. phew."
+ local PASSWD=${INIT_DIR}/etc/passwd
+ local GROUP=${INIT_DIR}/etc/group
+ local SHADOW=${INIT_DIR}/etc/shadow
+ [ -s "${PASSWD}" -a -s "${GROUP}" -a -s "${SHADOW}" ] && return
+ pinfo "Creating users and groups based on local system...."
+ cp "/etc/passwd" "$PASSWD"
+ cp "/etc/group" "$GROUP"
+ cp "/etc/shadow" "$SHADOW"
+ # remove local users from group file (TODO: currently assumes users have ids 1000-1999)
+ local LOCALUSERS=$(grep -E '^[^:]+:x?:1[0-9]{3}:' "${PASSWD}" | awk -F ':' '{print $1}')
+ for USER in $LOCALUSERS; do
+ sed -r -i "s/([:,])${USER}/\1/g" "${GROUP}"
+ done
+ # fix syntax: remove trailing ',' in group file
+ sed -r -i 's/,+$//g' "${GROUP}"
+ sed -r -i 's/,+/,/g' "${GROUP}"
+ sed -i 's/:,/:/g' "${GROUP}"
+ # remove all non-system groups (also assumes users have 1000-1999, so nogroup will be kept)
+ grep -v -E '^[^:]+:x?:1[0-9]{3}:' "${GROUP}" > "${GROUP}.tmp"
+ mv "${GROUP}.tmp" "${GROUP}"
+ # same for users...
+ grep -v -E '^[^:]+:x?:1[0-9]{3}:' "${PASSWD}" > "${PASSWD}.tmp"
+ mv "${PASSWD}.tmp" "${PASSWD}"
+ # generate fresh shadow file
+ awk -F ':' '{print $1":*:15555:0:99999:7:::"}' "${PASSWD}" > "${SHADOW}"
+}
+
diff --git a/remote/tools/base/base.build b/remote/tools/base/base.build
index aee237a0..8a327f83 100644
--- a/remote/tools/base/base.build
+++ b/remote/tools/base/base.build
@@ -107,16 +107,16 @@ post_copy() {
echo "# no configuration" >> "${INIT_DIR}/etc/fstab"
# link /etc/mtab, needed for systemd
- [ ! -e ${INIT_DIR}/etc/mtab ] && ln -s /proc/self/mounts ${INIT_DIR}/etc/mtab
+ [ ! -e ${INIT_DIR}/etc/mtab ] && ln -s /proc/self/mounts ${INIT_DIR}/etc/mtab
+
+ # passwd, group, shadow
+ init_users_and_groups
# quick fix for missing group in /etc/group
add_group "lock"
- # clean passwd/group files (do BEFORE setting root pw)
- clean_users_and_groups "${INIT_DIR}"
-
# setup root accoun
- USER=root PASSWORD="!r00t" add_user
+ USER=root PASSWORD='!r00t' add_user
[ ! -d ${INIT_DIR}/root ] && mkdir ${INIT_DIR}/root
diff --git a/remote/tools/base/base.conf b/remote/tools/base/base.conf
index c03579b0..85b638e6 100644
--- a/remote/tools/base/base.conf
+++ b/remote/tools/base/base.conf
@@ -42,10 +42,6 @@ REQUIRED_DIRECTORIES=" /etc/pam.d
/etc/security"
REQUIRED_FILES=" /sbin/mount.nfs4
/sbin/umount.nfs4
- /etc/passwd
- /etc/shadow
- /etc/group
- /etc/hostname
/etc/environment
/etc/pam.conf
/etc/issue