-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-ldap-user.sh
executable file
·199 lines (170 loc) · 5.65 KB
/
create-ldap-user.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash
: <<'DOC'
Script for creating or updating an LDAP group on all instances.
Requires a template XML file
DOC
# source the get-token.sh file
# TIP for Visual Studio Code - Add Custom Arg '-x' to the Shellcheck extension settings
source "get-token.sh"
# set instance list type
instance_list_type="ios"
usage() {
cat <<'USAGE'
Usage:
NOTE: LDAP server is obtained automatically from the instance (must be configured already)
./set_credentials.sh - set the Keychain credentials
[no arguments] - interactive mode
--template /path/to/Template.xml - template to use (must be a .xml file)
--ldap-server EXAMPLE.COM - provide the LDAP server
--user LDAP_USER - set username
--il FILENAME (without .txt) - provide an instance list filename
(must exist in the instance-lists folder)
--i JSS_URL - perform action on a single instance
(must exist in the relevant instance list)
--all - perform action on ALL instances in the instance list
-v - add verbose curl output
USAGE
}
parse_template() {
parsed_template=$(sed "s|%LDAP_USER_NAME%|${chosen_user}|g" "$template_file")
parsed_template="${parsed_template/\%LDAP_SERVER\%/$ldap_server}"
parsed_template="${parsed_template/\%LDAP_SERVER_ID\%/$ldap_server_id}"
}
upload_data() {
# determine jss_url
set_credentials "$jss_instance"
jss_url="${jss_instance}"
# Check this LDAP server actually exists
# send request
curl_url="$jss_url/JSSResource/ldapservers"
curl_args=("--header")
curl_args+=("Accept: application/xml")
send_curl_request
# get id from output
ldap_server_id=$(xmllint --xpath "//ldap_servers/ldap_server[name = '$ldap_server']/id/text()" "$curl_output_file" 2>/dev/null)
if [[ ! $ldap_server_id ]]; then
echo " [upload_data] No existing LDAP server found. Please run set-ldap-server.sh before using this script on this instance"
return
fi
# if we got this far it is ok to write the account
# parse the template
parse_template
# show output if verbose mode set
if [[ $verbose == 1 ]]; then
echo "$parsed_template"
fi
# Check this LDAP user actually exists
# send request
curl_url="$jss_url/JSSResource/accounts"
curl_args=("--header")
curl_args+=("Accept: application/xml")
send_curl_request
# get id from output
existing_id=$(xmllint --xpath "//users/user[name = '$chosen_user']/id/text()" "$curl_output_file" 2>/dev/null)
# send request
curl_args=("--header")
curl_args+=("Content-Type: application/xml")
curl_args+=("--data")
curl_args+=("$parsed_template")
if [[ $existing_id ]]; then
echo " [upload_data] Existing LDAP user '$chosen_user' found on '$jss_url' (ID $existing_id). Updating..."
curl_url="$jss_url/JSSResource/accounts/userid/$existing_id"
curl_args+=("--request")
curl_args+=("PUT")
else
echo " [upload_data] LDAP user '$chosen_user' not found on '$jss_url'. Creating..."
curl_url="$jss_url/JSSResource/accounts/userid/0"
curl_args+=("--request")
curl_args+=("POST")
fi
send_curl_request
}
if [[ ! -d "${this_script_dir}" ]]; then
echo "ERROR: path to repo ambiguous. Aborting."
exit 1
fi
## MAIN BODY
# -------------------------------------------------------------------------
# Command line options (presets to avoid interaction)
# -------------------------------------------------------------------------
# Command line override for the above settings
while [[ "$#" -gt 0 ]]; do
key="$1"
case $key in
-u|--user)
shift
chosen_user="$1"
;;
-s|--ldap-server)
shift
ldap_server="$1"
;;
-t|--template)
shift
template="$1"
;;
-il|--instance-list)
shift
instance_list_file="$1"
;;
-i|--instance)
shift
chosen_instance="$1"
;;
-a|--all)
all_instances=1
;;
-v|--verbose)
verbose=1
;;
-h|--help)
usage
exit
;;
esac
# Shift after checking all the cases to get the next option
shift
done
echo
# Set default instance list
default_instance_list="prd"
# get template
choose_template_file
# select the instances that will be changed
choose_destination_instances
# set ldap_server
if [[ ! $ldap_server ]]; then
read -r "Enter LDAP Server Name : " ldap_server
fi
# set ldap user
if [[ ! $chosen_user ]]; then
read -r "Enter LDAP User : " chosen_user
fi
# get specific instance if entered
item=0
for instance in "${instance_choice_array[@]}"; do
jss_instance="$instance"
echo " [main] Updating the LDAP user(s) on $jss_instance..."
# restrict to the chosen instance if present, otherwise do all instances
if [[ $chosen_instance ]]; then
if [[ "$instance" == "$chosen_instance" ]]; then
# skip if set to "None"
if [[ ! $chosen_user || $chosen_user == "None" ]]; then
echo " [main] $instance has no LDAP user assigned, skipping..."
else
upload_data
fi
fi
else
# skip if set to "None"
if [[ ! $chosen_user || $chosen_user == "None" ]]; then
echo " [main] $instance has no LDAP user assigned, skipping..."
else
upload_data
fi
fi
((item++))
done
echo
echo "Finished"
echo