1#!/bin/sh
2####################################################################################################
3#
4# ABOUT
5#
6# Convert Mobile Account to Local Account
7# Based on: https://jamfnation.jamfsoftware.com/discussion.html?id=12462#responseChild73117
8#
9# This script is designed to remove a mobile user account and re-create
10# a local account with the same username and the password from user-input.
11# It will also give read/write permissions to the user's home folder.
12#
13####################################################################################################
14#
15# HISTORY
16#
17# Version 1.0, 28-Apr-2016, Dan K. Snelson
18# Version 1.1, 02-May-2016, Dan K. Snelson
19# Removed code and verbiage about the user's keychain
20# Version 1.2, 03-May-2016, Dan K. Snelson
21# Fixed error when no users with 5nn UID existed
22# Version 1.2 Brewster Edition 27-July-2018, Chris Hafner
23# Modified to allow account conversion from personal local, to EC User local account.
24#
25
26####################################################################################################
27# Import general functions
28source /Users/Shared/Client-Side-Functions.sh
29####################################################################################################
30
31ScriptLog "###############################################"
32ScriptLog "### Convert Personal Local Account to Brewsterized Local Account ###"
33ScriptLog "###############################################"
34
35### Variables
36loggedInUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
37");'`
38UserUID=`/usr/bin/dscl . read /Users/"${loggedInUser}" UniqueID | grep UniqueID: | cut -c 11-`
39userRealName=`/usr/bin/dscl . -read /Users/"${loggedInUser}" | /usr/bin/grep RealName: | cut -c11-`
40user_home_location=`/usr/bin/dscl . -read /Users/"${loggedInUser}" NFSHomeDirectory 2>/dev/null | /usr/bin/sed 's/^[^/]*//g'`
41
42# Echo variables
43ScriptLog "Variables ..."
44ScriptLog "* loggedInUser=${loggedInUser}"
45ScriptLog "* UserUID=${UserUID}"
46ScriptLog "* userRealName=${userRealName}"
47ScriptLog "* adminStatus=${userIsAdmin}"
48ScriptLog "* ec_user=${ec_user}"
49
50#This will set the "admiStatus" to nothing in all circumstances. I've just left the line here in case I change my mind later. Proper adminStatus would be -admin for a non-standard user.
51if [[ $(/usr/bin/dsmemberutil checkmembership -U "${loggedInUser}" -G admin) != *not* ]]; then
52 adminStatus=""
53 userIsAdmin="Yes"
54else
55 adminStatus=""
56 userIsAdmin="No"
57fi
58
59if [[ -d "/Applications/Enterprise Connect.app" ]]; then
60 /usr/bin/security find-generic-password -l "Enterprise Connect" "${user_home_location}"/Library/Keychains/login.keychain > /dev/null 2>&1
61 if [[ $? -eq 0 ]]; then
62 ec_user=`/usr/bin/security find-generic-password -l "Enterprise Connect" | grep "acct" | awk -F "=" '{print $2}' | tr -d """`
63 ec_userPW=`/usr/bin/security find-generic-password -l "Enterprise Connect" -w`
64 fi
65fi
66
67#Rename home directory
68ScriptLog "* Moving Home Directory ..."
69mv $user_home_location /Users/$ec_user
70
71# Delete the currently logged-in user account
72ScriptLog "* Deleting ${loggedInUser} account from client-side directory ..."
73sysadminctl -deleteUser "$loggedInUser" -keepHome
74
75Gets the current highest user UID
76ScriptLog "* Discovering the highest available UID ..."
77maxid=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)
78
79 if [ -z ${maxid} ]; then
80 newid=501
81 else
82 newid=$((maxid+1))
83 fi
84
85# Create local user account ...
86ScriptLog "* Create ${loggedInUser} local account in client-side directory ..."
87/usr/sbin/sysadminctl -addUser "${ec_user}" -fullName "${ec_user}" -UID "${newid}" -password "${ec_userPW}" -home "/Users/${ec_user}" "${adminStatus}"
88
89# Reset ownership on home directory and append location
90ScriptLog "* Correct permissions for ${loggedInUser} ..."
91/usr/sbin/chown -R "${ec_user}":staff /Users/"${ec_user}"
92
93#This would delete the user's keychain folder if uncommented
94#ScriptLog "* Delete ${loggedInUser} keychain ..."
95#/bin/rm -Rf /Users/"${ec_user}"/Library/Keychains/*
96
97#Sleep for five seconds ..."
98ScriptLog "* Sleep for five seconds ..."
99/bin/sleep 5
100
101# Force logout
102ScriptLog "* Force logout ..."
103/bin/ps -Ajc | /usr/bin/grep loginwindow | /usr/bin/awk '{print $2}' | /usr/bin/xargs /bin/kill -9
104
105ScriptLog "---"
106ScriptLog "- $loggedInUser account converted from personal to stu###"
107ScriptLog "---"
108
109
110exit 0