155 lines
5.2 KiB
Bash
155 lines
5.2 KiB
Bash
#!/bin/bash
|
|
##########################################################################
|
|
# Shellscript : User can get admin rights for a limited time
|
|
# Autor : Andreas Vogel, macenterprise gmbh, 2020
|
|
##########################################################################
|
|
#Description
|
|
|
|
# User kann über den Self Service Admin-Rechte einholen. Dabei ist die Begründung pflicht.
|
|
# Die Begründung wird über das Echo im Jamf Log eingetragen und ist danach einsehbar.
|
|
# Wird keine Begründung eingetragen, so werden auch keine Admin Rechte erteilt.
|
|
# Sofern alles eingetragen ist, wird ein Daemon geschrieben. Dieses Startet exakt nach einer festgelegten Zeit.
|
|
# Die Maximale Zeit kann über die Variable 4 gesetzt werden. Ist keine Zeit gesetzt, so wird die Zeit genommen,
|
|
# die im Script festgeschrieben ist. Der Daemon startet ein Script, dass die Admin Rechte entfernt und den Daemon beendet.
|
|
|
|
# User can obtain admin rights via the Self Service. The justification is mandatory. The reason is entered via the echo in
|
|
# the Jamf Log and can then be viewed. If no reason is entered, no admin rights are granted.
|
|
# If everything is entered, a daemon is written. This starts exactly after a set time.
|
|
# The maximum time can be set via variable 4. If no time is set, the time that is set in the script is used.
|
|
# The daemon starts a script that removes the admin rights and ends the daemon.
|
|
|
|
################################### Variablen ###################################
|
|
currentUser=$(stat -f '%u %Su' /dev/console | cut -d ' ' -f 2)
|
|
|
|
################################### Function ###################################
|
|
ask () {
|
|
osascript <<EOF - 2>/dev/null
|
|
tell application "SystemUIServer"
|
|
activate
|
|
text returned of (display dialog "$1" default answer "")
|
|
end tell
|
|
EOF
|
|
}
|
|
|
|
MessageBox() {
|
|
osascript <<EOT
|
|
tell app "System Events"
|
|
with timeout of 300 seconds
|
|
button returned of (display dialog "A justification is imperative.
|
|
Please run it again and provide a reason." buttons {"OK"} default button 1 with title "Error")
|
|
end timeout
|
|
end tell
|
|
EOT
|
|
}
|
|
|
|
Substantiation() {
|
|
answer=$(ask 'Please enter a reason why you need admin rights.') || exit
|
|
echo $answer
|
|
}
|
|
|
|
CreateAdmin() {
|
|
/usr/sbin/dseditgroup -o edit -a $currentUser -t user admin
|
|
}
|
|
################################### Determine time ###################################
|
|
MaxTime="${4}"
|
|
if [[ -z "$MaxTime" ]]
|
|
then
|
|
MaxTime="120"
|
|
fi
|
|
|
|
time=$(/bin/date +%s)
|
|
delayint=$(echo "$MaxTime")
|
|
defercal=$(($(/bin/date +%s) + delayint))
|
|
hour=$(/bin/date -j -f "%s" "$defercal" "+%H")
|
|
minute=$(/bin/date -j -f "%s" "$defercal" "+%M")
|
|
|
|
################################### Daemon ###################################
|
|
RemoveAdminDaemon()
|
|
{
|
|
/bin/cat <<EOB > /Library/LaunchDaemons/de.mac.remove.admin.plist
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
|
|
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
<plist version="1.0">
|
|
<dict>
|
|
<key>Label</key>
|
|
<string>de.mac.admintoremove.sh</string>
|
|
<key>ProgramArguments</key>
|
|
<array>
|
|
<string>/bin/bash</string>
|
|
<string>/Library/Application Support/JAMF/de.mac.admintoremove.sh</string>
|
|
</array>
|
|
<key>StartCalendarInterval</key>
|
|
<dict>
|
|
<key>Hour</key>
|
|
<integer>$hour</integer>
|
|
<key>Minute</key>
|
|
<integer>$minute</integer>
|
|
</dict>
|
|
</dict>
|
|
</plist>
|
|
EOB
|
|
}
|
|
|
|
################################### Script ###################################
|
|
RemoveAdminScript()
|
|
{
|
|
read -d '' RemoveAdmin<<"EOF"
|
|
#!/bin/bash
|
|
#####################################################################################
|
|
# Shellscript :
|
|
# Autor : Andreas Vogel, macenterprise gmbh,
|
|
#####################################################################################
|
|
|
|
currentUser=$(stat -f '%u %Su' /dev/console | cut -d ' ' -f 2)
|
|
if [[ $(dscl . read /Groups/admin GroupMembership | grep -o ${currentUser}) ]]
|
|
then
|
|
echo "${currentUser} is an admin"
|
|
/usr/sbin/dseditgroup -o edit -d $currentUser -t user admin
|
|
launchctl unload /Library/LaunchDaemons/de.mac.remove.admin.plist
|
|
rm /Library/LaunchDaemons/de.mac.remove.admin.plist
|
|
rm /Library/Application Support/JAMF/de.mac.admintoremove.sh
|
|
fi
|
|
EOF
|
|
}
|
|
|
|
################################### Start and Load ###################################
|
|
StartandLoad()
|
|
{
|
|
echo "$RemoveAdmin" > /Library/Application\ Support/JAMF/de.mac.admintoremove.sh
|
|
# set ownership on LastWarningDaemon launch daemon
|
|
/usr/sbin/chown root:wheel /Library/LaunchDaemons/de.mac.remove.admin.plist
|
|
/bin/chmod 644 /Library/LaunchDaemons/de.mac.remove.admin.plist
|
|
|
|
#load launchd
|
|
launchctl load /Library/LaunchDaemons/de.mac.remove.admin.plist
|
|
|
|
#set ownership for Script
|
|
/usr/sbin/chown root:admin "/Library/Application Support/JAMF/de.mac.admintoremove.sh"
|
|
/bin/chmod 755 "/Library/Application Support/JAMF/de.mac.admintoremove.sh"
|
|
|
|
}
|
|
|
|
################################### Ausführung ###################################
|
|
Button1=$(osascript -e 'display dialog "Do you really need admin rights?." with title "Do you need admin rights" buttons {"Yes", "No"} default button 2 ')
|
|
Selection=$(echo $Button1 | cut -d : -f 2)
|
|
|
|
if [[ "$Selection" = "Yes" ]]
|
|
then
|
|
Substantiation
|
|
|
|
if [[ -z "$answer" ]]
|
|
then
|
|
MessageBox
|
|
else
|
|
RemoveAdminDaemon
|
|
RemoveAdminScript
|
|
StartandLoad
|
|
CreateAdmin
|
|
|
|
fi
|
|
|
|
else
|
|
echo "möchte nicht"
|
|
|
|
fi |