#!/bin/sh #save/load users and groups from/to flash (not TFFS) SAVE_DIR=/tmp/flash/users FILES="passwd group shadow gshadow" load() { mkdir -pm 700 $SAVE_DIR # temporary file touch "/tmp/passwd.mod" chmod 644 "/tmp/passwd.mod" cat /tmp/passwd > /tmp/passwd.mod for f in $FILES; do # set permissions: 644 for passwd & group, 600 for shadow & gshadow touch "/tmp/$f" [ "$f" == "${f%shadow}" ] && chmod 644 "/tmp/$f" || chmod 600 "/tmp/$f" # existing settings available? if [ ! -e "$SAVE_DIR/$f" ]; then # save default users&groups from var.tar cat "/tmp/$f" > "$SAVE_DIR/$f" 2>/dev/null else # load saved users&groups from flash grep -vE "^boxusr|^ftpuser" "$SAVE_DIR/$f" > "/tmp/${f/passwd/passwd.mod}" 2>/dev/null fi done # /var/tmp/passwd.tmp is created by ctlmgr, do NOT MoVe due to libctlmgr cp /var/tmp/passwd.tmp /var/tmp/passwd.avm 2>/dev/null && rm -rf /var/tmp/passwd.tmp # load AVM's boxusers (and ftpuser) which were created by ctlmgr [ -e /var/tmp/passwd.avm ] && grep -v "^root:" /var/tmp/passwd.avm >> /tmp/passwd.mod # special case: ftpuser is created and/or used by avm and/or freetz grep -q "^ftpuser" /tmp/passwd.mod || grep "^ftpuser" "$SAVE_DIR/passwd" >> /tmp/passwd.mod # prevent race-condition while creating /etc/passwd mv /tmp/passwd.mod /tmp/passwd touch /tmp/.usersloaded } save() { [ ! -e /tmp/.usersloaded ] && return 1 rm -rf $SAVE_DIR mkdir -pm 700 $SAVE_DIR for f in $FILES; do cat "/tmp/$f" > "$SAVE_DIR/$f" 2>/dev/null done } case $1 in load) load ;; save) save ;; update) save load ;; *) echo "Usage: $0 [load|save|update]" 1>&2 exit 1 ;; esac