summaryrefslogtreecommitdiff
path: root/system/partimage/partimaged-passwd
blob: 1033a027083aa477233af54e86fe5fa869823205 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
####################################################
# partimaged-passwd
# Copyright (C) 2006 Michael Bieb <biebl@debian.org>
####################################################

DB_LOAD=""
DB_DUMP=""
PASSWD_FILE="/etc/partimaged/passwd.db"

if [ $UID -ne 0 ] ; then
	:
	echo "This program has to be run as root!"
	exit 1
fi

# Check for a version of db_load/db_dump
check_db_utils() {
	for i in 5.1 4.8 4.7; do
		if [ -x /usr/bin/db${i}_load ] ; then
			DB_LOAD=/usr/bin/db${i}_load
			DB_DUMP=/usr/bin/db${i}_dump
			return 0
		fi
	done
	return 1
}

help() {
	echo "Manage partimaged user database."
	echo 
	echo "Usage:"
	echo "  $0 [-Dhl] username password"
	echo "  $0 [-Dhl] username"
	echo
	echo "  -D Delete user."
	echo "  -h Display this help message."
	echo "  -l List existing users."
	echo
}

delete_user() {
	echo "Deleting user $1..."
	$DB_DUMP -p $PASSWD_FILE | sed -e '1,/HEADER=END/d' -e '/DATA=END/,$d' | sed 's/^[ \t]*//' | sed "/^$1$/{n;d}" | sed "/^$1$/d" | $DB_LOAD -T -t hash ${PASSWD_FILE}.new
	mv ${PASSWD_FILE}.new $PASSWD_FILE
	fix_rights
}

add_user() {
	echo "Adding user $1..."
	echo -e "$1\n$2" | $DB_LOAD -T -t hash $PASSWD_FILE
	fix_rights
}

list_users() {
	[ -f $PASSWD_FILE ] || exit 0
	echo "Users:"
	$DB_DUMP -p $PASSWD_FILE | sed -e '1,/HEADER=END/d' -e '/DATA=END/,$d' | sed 's/^[ \t]*//' | sed -n '1~2p'
}

fix_rights() {
	chmod 640 $PASSWD_FILE
	chown partimag:partimag $PASSWD_FILE
}

if ! check_db_utils ; then
	echo "Could not find db_load/db_dump!"
	echo "Please install the db5.1-util package."
	exit 1
fi



while getopts D:hl opt 
do
	case "$opt" in
		D) 
			delete_user $OPTARG
			exit 0
			;;
		h) 
			help 
			exit 0
			;;
		l)	
			list_users
			exit 0
			;;
		[?])
			help
			exit 1
			;;
	esac
done

shift $((${OPTIND}-1))

if [ $# -eq 1 ] ; then
	echo -n "New password: "
	read -s passwd1
	echo
	echo -n "Re-type new password: "
	read -s passwd2
	echo
	if [ "$passwd1" != "$passwd2" ] ; then
		echo "Passwords do not match!"
		exit 1
	elif [ -z $passwd1 ] ; then
		echo "Please supply a valid password!"
		exit 1
	else
		add_user $1 $passwd1
	fi
elif [ $# -eq 2 ] ; then
	add_user $1 $2
else
	help
	exit 1
fi

exit 0