#! /bin/bash # Copyright (C) 2011 GRNET S.A. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301, USA. ### BEGIN TASK INFO # Provides: ChangePassword # RunBefore: EnforcePersonality # RunAfter: InstallUnattend # Short-Description: Changes Password for specified users ### END TASK INFO set -e . "@commondir@/common.sh" trap task_cleanup EXIT report_task_start # Check if the task should be prevented from running. check_if_excluded windows_password() { local target="$1" local password="$2" echo "@echo off" > "$target/Windows/SnfScripts/ChangeAdminPassword.cmd" if [ -z "$SNF_IMAGE_PROPERTY_USERS" ]; then SNF_IMAGE_PROPERTY_USERS="Administrator" fi for usr in $SNF_IMAGE_PROPERTY_USERS; do echo -n "Installing new password for user \`$usr'..." echo "net user $usr $password" >> \ "$target/Windows/SnfScripts/ChangeAdminPassword.cmd" echo done done } linux_password() { local target="$1" local password="$2" local hash=$("@scriptsdir@/snf-passtohash.py" "$password") if [ ! -e "$target/etc/shadow" ]; then log_error "No /etc/shadow found!" fi declare -a users if [ -n "$SNF_IMAGE_PROPERTY_USERS" ]; then for usr in $SNF_IMAGE_PROPERTY_USERS; do users+=("$usr") done else users+=("root") fi for i in $(seq 0 1 $((${#users[@]}-1))); do local tmp_shadow="$(mktemp)" add_cleanup rm "$tmp_shadow" echo -n "Setting ${users[$i]} password..." echo "${users[$i]}:$hash:15103:0:99999:7:::" > "$tmp_shadow" grep -v "${users[$i]}" "$target/etc/shadow" >> "$tmp_shadow" cat "$tmp_shadow" > "$target/etc/shadow" echo "done" done } if [ ! -d "$SNF_IMAGE_TARGET" ]; then log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing" fi if [ -z "$SNF_IMAGE_PASSWORD" ]; then log_error "Password is missing" fi #trim users var SNF_IMAGE_PROPERTY_USERS=$(echo $SNF_IMAGE_PROPERTY_USERS) if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then windows_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD" elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then linux_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD" fi exit 0 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :