summaryrefslogtreecommitdiff
path: root/ext/osx/postflight.erb
blob: 2e36b25ef676b7a2d96838b8aaea0a7a8528b086 (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
#!/bin/bash

#by doing the below, we are ensuring we are modifying the target system the package is being installed on,
#not the OS running the installer
declare -x dest_vol="${3}"
declare -x dscl="${dest_vol}/usr/bin/dscl"
declare -x dspath="${dest_vol}/var/db/dslocal/nodes/Default/"
declare -x scripts_dir="${1}/Contents/Resources/"
declare -x awk="/usr/bin/awk"
declare -x last_user_id='-1'
declare -x last_group_id='-1'

function idFree() {
  declare -a idArray=("${!2}")
  for inc in ${idArray[@]}
  do
    if  [ $inc == $1 ]
    then
      return 1
    fi
  done

  return 0

}

function create_puser () {
  "${dscl}" -f "${dspath}" localonly -create /Local/Target/Users/puppet
  "${dscl}" -f "${dspath}" localonly -create /Local/Target/Users/puppet UniqueID $1
  "${dscl}" -f "${dspath}" localonly -create /Local/Target/Users/puppet PrimaryGroupID $2
}

function create_pgroup () {
  "${dscl}" -f "${dspath}" localonly -create /Local/Target/Groups/puppet
  "${dscl}" -f "${dspath}" localonly -create /Local/Target/Groups/puppet PrimaryGroupID $1
}

function scan_users () {
  UniqueIDS=(`"${dscl}" -f "${dspath}" localonly  list /Local/Target/Users UniqueID | $awk '{print $2}'`);

  #first just check for UID 52
  if idFree '52' UniqueIDS[@]
  then
    last_user_id='52'
  else
    for possibleUID in {450..495}
    do
      if idFree $possibleUID UniqueIDS[@]
      then
        last_user_id=$possibleUID
        #echo $last_good_id
      fi
    done
  fi
}

function scan_groups () {
  GroupIDS=(`"${dscl}" -f "${dspath}" localonly list /Local/Target/Groups PrimaryGroupID | $awk '{print $2}'`);
  #check for 52 for group, if it's free, take it, don't bother doing the big search
  if idFree '52' GroupIDS[@]
  then
    last_group_id='52'
  else
   for groupID in {450..495}
    do
     if idFree $groupID GroupIDS[@]
     then
        last_group_id=$groupID
      fi
    done
  fi
}

echo "looking for Puppet User"
"${dscl}" -f "${dspath}" localonly -read /Local/Target/Users/puppet
puser_exists=$?
echo "Looking for Puppet Group"
"${dscl}" -f "${dspath}" localonly -read /Local/Target/Groups/puppet
pgroup_exists=$?
# exit status 56 indicates user/group not found
# exit status 0 indicates user/group does exist

if [ $pgroup_exists == '0' ] && [ $puser_exists == '0' ]; then
  #Puppet user and group already exist
  echo "Puppet User / Group already exist, not adding anything"
  #storing the existing UID/GID to set permissions for /var/lib/puppet and /etc/puppet/puppet.conf
  last_group_id=`"${dscl}" -f "${dspath}" localonly -read /Local/Target/Groups/puppet PrimaryGroupID | awk '{print $2}'`
  last_user_id=`"${dscl}" -f "${dspath}" localonly -read /Local/Target/Users/puppet UniqueID | awk '{print $2}'`
elif [ $pgroup_exists == '0' ] && [ $puser_exists == '56' ]; then
  #puppet group exists, but user does not
  last_group_id=`"${dscl}" -f "${dspath}" localonly -read /Local/Target/Groups/puppet PrimaryGroupID | awk '{print $2}'`
  scan_users
  echo "Creating Puppet User (uid: $last_user_id) in existing Puppet Group (gid: $last_group_id)"
  create_puser $last_user_id $last_group_id
elif [ $pgroup_exists == '56' ] && [ $puser_exists == '0' ]; then
  #puppet user exists, but group does not
  last_user_id=`"${dscl}" -f "${dspath}" localonly -read /Local/Target/Users/puppet UniqueID | awk '{print $2}'`
  scan_groups
  echo "Creating Puppet Group (gid: $last_group_id), Puppet User exists (uid: $last_user_id)"
  create_pgroup $last_group_id
elif [ $pgroup_exists == '56' ] && [ $puser_exists == '56' ]; then
  scan_users
  scan_groups
  echo "Creating Puppet User (uid: $last_user_id) in new Puppet Group (gid: $last_group_id)"
  create_pgroup $last_group_id
  create_puser $last_user_id $last_group_id
else
  echo "Something went wrong and user creation will need to be done manually"
fi