#!/bin/sh #------------------------------------------------------------------------------ # /usr/sbin/partimaged-passwd - passwd tool for partimaged # # Copyright (C) 2006 Michael Bieb # Copyright (c) 2010-2012 The Eisfair Team, team(at)eisfair(dot)org # # Creation: 2010-10-04 jed # Last Update: $Id: $ # # 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.. #------------------------------------------------------------------------------ # read eislib etc. . /var/install/include/eislib # variables db_load_prog='' db_dump_prog='' passwd_file='/etc/partimaged/passwd.db' if [ "$(id -ru)" -ne 0 ] then mecho -warn "This program has to be run as root!" exit 1 fi #------------------------------------------------------------------------------ # check for a version of db_load/db_dump #------------------------------------------------------------------------------ check_db_utils () { for idx in 8 7 6 5 4 3 2 do if [ -x /usr/bin/db4.${idx}_load ] then db_load_prog=/usr/bin/db4.${idx}_load db_dump_prog=/usr/bin/db4.${idx}_dump return 0 fi done return 1 } #------------------------------------------------------------------------------ # show help #------------------------------------------------------------------------------ show_help () { mecho "Manage partimaged user database." mecho mecho "Usage:" mecho " $0 [-Dhl] username password" mecho " $0 [-Dhl] username" mecho mecho " -D Delete user." mecho " -h Display this help message." mecho " -l List existing users." mecho } #------------------------------------------------------------------------------ # delete user # $1 - username #------------------------------------------------------------------------------ delete_user () { user="$1" mecho "Deleting user ${user} ..." ${db_dump_prog} -p ${passwd_file} | sed -e '1,/HEADER=END/d' \ -e '/DATA=END/,$d' -e 's/^[ \t]*//' -e "/^$1$/{n;d}" -e "/^$1$/d" | \ ${db_load_prog} -T -t hash ${passwd_file}.new mv ${passwd_file}.new ${passwd_file} set_rights } #------------------------------------------------------------------------------ # add user # $1 - username # $2 - password #------------------------------------------------------------------------------ add_user () { user="$1" pass="$2" mecho "Adding user ${user} ..." printf "${user}\n${pass}\n" | ${db_load_prog} -T -t hash ${passwd_file} set_rights } #------------------------------------------------------------------------------ # list users #------------------------------------------------------------------------------ list_users () { if [ -f ${passwd_file} ] then mecho "Users:" ${db_dump_prog} -p ${passwd_file} | sed -e '1,/HEADER=END/d' \ -e '/DATA=END/,$d' -e 's/^[ \t]*//' | sed -n '1~2p' else exit 0 fi } #------------------------------------------------------------------------------ # set ownership and access rights #------------------------------------------------------------------------------ set_rights () { chown partimag ${passwd_file} chgrp partimag ${passwd_file} chmod 640 ${passwd_file} } #============================================================================== # main #============================================================================== username="$1" password="$2" if ! check_db_utils then mecho -error "Could not find db_load/db_dump programs!" mecho -error "Please install the appropriate util-db4 package." exit 1 fi while getopts D:hl opt do case "${opt}" in D) # delete user delete_user ${OPTARG} exit 0 ;; h) # show help show_help exit 0 ;; l) # list users list_users exit 0 ;; [?]) # show help show_help exit 1 ;; esac done shift $((${OPTIND}-1)) case $# in 1 ) echo -n "New password: " read -s passwd1 echo echo -n "Re-type new password: " read -s passwd2 echo if [ "${passwd1}" != "${passwd2}" ] then mecho -error "Passwords do not match!" exit 1 elif [ -z ${passwd1} ] then echo "Please enter a valid password!" exit 1 else add_user ${username} ${passwd1} fi ;; 2 ) add_user ${username} ${password} ;; * ) show_help exit 1 ;; esac exit 0 #============================================================================== # end #==============================================================================