-
Notifications
You must be signed in to change notification settings - Fork 3
/
nochgUserPWD.sh
135 lines (121 loc) · 4.71 KB
/
nochgUserPWD.sh
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
# ------------------------------------------------------------------------------------
# Name: nochgUserPWD.sh
# ------------------------------------------------------------------------------------
#
# ---------------------
# Description:
# Test if a user account password is changed by other than customized service
# and then logout and reset password to initial password
#
# ---------------------
# Condition:
# 1. Initialization:
# if initial_password_time or initial_password are not set, then save "password
# changed time"" and "password" as initial_password_time and initial_password
#
# 2. Test on login:
# If the account's "password changed time" is different from initial_password_time
# then restore user's initial_password and set initial_password_time
#
# ---------------------
# History:
# 2020-02-20, Initial version 0.2
#
# ---------------------
# License: MIT License
#
# Copyright (c) 2020- Tony Liu
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ------------------------------------------------------------------------------------
sUserID=${1:-}
#loginUser=$(stat -f "%Su" /dev/console)
loginUser="$1"
userPWDlog="/tmp/nochgUserPWD.log"
echo "------- $(date) ($sUserID) -------" >> "$userPWDlog"
sUserHashPWD="ShadowHashData"
sUserShell="UserShell"
sUserInitHashPWD="${sUserHashPWD}_Initial"
sUserInitShell="${sUserShell}_Initial"
#sUserPWDTime="accountPolicyData"
#sUserInitPWDTime="${sUserPWDTime}_Initial"
readUserShell () { dscl . -read /Users/"$sUserID" "$1" | awk '{ print $2}'; }
writeUserShell () { dscl . -create /users/"$sUserID" "$1" "$2"; }
readUserHashPWD () { defaults read /var/db/dslocal/nodes/Default/users/"$sUserID".plist "$1"; }
writeUserPWD () { defaults write /var/db/dslocal/nodes/Default/users/"$sUserID".plist "$1" "$2"; }
initialUserStatus () {
echo " -initialUserStatus:" >> "$userPWDlog"
initPWD=$(readUserHashPWD "$sUserInitHashPWD" 2>/dev/null)
if [[ "$initPWD" = "" ]]; then
echo " -initializing: ($sUserInitHashPWD)" >> "$userPWDlog"
uPWD=$(readUserHashPWD "$sUserHashPWD")
echo " - uPWD=$uPWD;" >> "$userPWDlog"
writeUserPWD "$sUserInitHashPWD" "$uPWD"
# writeUserShell "sUserInitShell" "$userShell"
else
echo " -was initialized:($sUserInitHashPWD)" >> "$userPWDlog"
fi
}
logoutUser () {
echo " -logoutUser:" >> "$userPWDlog"
# shutdown -r +2
killall loginwindow
}
disableUser () {
echo " -disableUser:" >> "$userPWDlog"
# updateUserProperty "$sUserShell" "/usr/bin/false"
# logoutUser
}
restoreStatus () {
echo " -restoreUserStatus" >> "$userPWDlog"
userPWD=$(readUserHashPWD "$sUserInitHashPWD")
writeUserPWD "$sUserHashPWD" "$userPWD"
}
enableUser () {
echo " -enableUser:" >> "$userPWDlog"
restoreStatus
logoutUser
}
testUserStatusChged () {
echo " -testUserStatusChged:" >> "$userPWDlog"
currentPWD=$(readUserHashPWD "$sUserHashPWD")
echo " -currentPWD=$currentPWD" >> "$userPWDlog"
initPWD=$(readUserHashPWD "$sUserInitHashPWD")
echo " -initPWD=$initPWD" >> "$userPWDlog"
[ "$currentPWD" = "$initPWD" ] && return 1 || return 0
}
echo "Start: Init_user=$sUserID" >> "$userPWDlog"
if [[ -n "${sUserID// }" ]]; then
echo " : current user=$(id -un); login user=$loginUser" >> "$userPWDlog"
if [ "$sUserID" = "$loginUser" ]; then
echo " : InitUser is $loginUser continue..." >> "$userPWDlog"
initialUserStatus
if testUserStatusChged; then
echo " : $(id -un) password CHANGED." >> "$userPWDlog"
disableUser
# sleep 20
enableUser
else
echo " : $(id -un) password OK." >> "$userPWDlog"
fi
fi
else
echo " : need a user command line argument." >> "$userPWDlog"
fi