open_directory_utils 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,281 +0,0 @@
1
- require "open_directory_utils/dscl"
2
- require "open_directory_utils/clean_check"
3
- require "open_directory_utils/commands_base"
4
-
5
- module OpenDirectoryUtils
6
-
7
- # this is a long list of pre-built dscl commands affecting users to accomplish common actions
8
- # @note - these commands were derived from the following resrouces:
9
- # * https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/dscl.1.html
10
- # * https://superuser.com/questions/592921/mac-osx-users-vs-dscl-command-to-list-user/621055?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
11
- module CommandsUserAttribsLdap
12
-
13
- # include OpenDirectoryUtils::Dscl
14
- include OpenDirectoryUtils::CleanCheck
15
- include OpenDirectoryUtils::CommandsBase
16
-
17
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$USER cn "$NAME"
18
- def user_set_common_name(attribs, dir_info)
19
- attribs = user_record_name_alternatives(attribs)
20
-
21
- attribs[:value] = attribs[:value] || attribs[:cn]
22
- attribs[:value] = attribs[:value] || attribs[:realname]
23
- attribs[:value] = attribs[:value] || attribs[:real_name]
24
- attribs[:value] = attribs[:value] || attribs[:fullname]
25
- attribs[:value] = attribs[:value] || attribs[:full_name]
26
- attribs[:value] = attribs[:value] || "#{attribs[:first_name]} #{attribs[:last_name]}"
27
-
28
- check_critical_attribute( attribs, :record_name )
29
- check_critical_attribute( attribs, :value, :common_name )
30
- attribs = tidy_attribs(attribs)
31
-
32
- command = {action: 'create', scope: 'Users', attribute: 'cn'}
33
- user_attrs = attribs.merge(command)
34
-
35
- dscl( user_attrs, dir_info )
36
- end
37
- alias_method :user_set_cn, :user_set_common_name
38
-
39
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME givenName "$VALUE"
40
- def user_set_given_name(attribs, dir_info)
41
- attribs = user_record_name_alternatives(attribs)
42
-
43
- attribs[:value] = attribs[:value] || attribs[:given_name]
44
- attribs[:value] = attribs[:value] || attribs[:first_name]
45
-
46
- check_critical_attribute( attribs, :record_name )
47
- check_critical_attribute( attribs, :value, :given_name )
48
- attribs = tidy_attribs(attribs)
49
-
50
- command = {action: 'create', scope: 'Users', attribute: 'givenName'}
51
- user_attrs = attribs.merge(command)
52
-
53
- dscl( user_attrs, dir_info )
54
- end
55
-
56
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME sn "$VALUE"
57
- def user_set_surname(attribs, dir_info)
58
- attribs = user_record_name_alternatives(attribs)
59
-
60
- attribs[:value] = attribs[:value] || attribs[:sn]
61
- attribs[:value] = attribs[:value] || attribs[:surname]
62
- attribs[:value] = attribs[:value] || attribs[:last_name]
63
-
64
- check_critical_attribute( attribs, :record_name )
65
- check_critical_attribute( attribs, :value, :surname )
66
- attribs = tidy_attribs(attribs)
67
-
68
- command = {action: 'create', scope: 'Users', attribute: 'sn'}
69
- user_attrs = attribs.merge(command)
70
-
71
- dscl( user_attrs, dir_info )
72
- end
73
- alias_method :user_set_sn, :user_set_surname
74
-
75
- # # sudo dscl . -create /Users/someuser uidnumber "1010"
76
- def user_set_uidnumber(attribs, dir_info)
77
- attribs = user_record_name_alternatives(attribs)
78
-
79
- attribs[:value] = attribs[:value] || attribs[:uniqueid]
80
- attribs[:value] = attribs[:value] || attribs[:unique_id]
81
- attribs[:value] = attribs[:value] || attribs[:uidnumber]
82
-
83
- check_critical_attribute( attribs, :record_name )
84
- check_critical_attribute( attribs, :value, :unique_id )
85
- attribs = tidy_attribs(attribs)
86
-
87
- command = {action: 'create', scope: 'Users', attribute: 'uidnumber'}
88
- user_attrs = attribs.merge(command)
89
-
90
- dscl( user_attrs, dir_info )
91
- end
92
-
93
- # sudo dscl . -create /Users/someuser PrimaryGroupID 80
94
- def user_set_gidnumber(attribs, dir_info)
95
- attribs = user_record_name_alternatives(attribs)
96
-
97
- attribs[:value] = attribs[:value] || attribs[:group_id]
98
- attribs[:value] = attribs[:value] || attribs[:gidnumber]
99
- attribs[:value] = attribs[:value] || attribs[:group_number]
100
- attribs[:value] = attribs[:value] || attribs[:primary_group_id]
101
-
102
- check_critical_attribute( attribs, :record_name )
103
- check_critical_attribute( attribs, :value, :group_id )
104
- attribs = tidy_attribs(attribs)
105
-
106
- command = {action: 'create', scope: 'Users', attribute: 'gidnumber'}
107
- user_attrs = attribs.merge(command)
108
-
109
- dscl( user_attrs, dir_info )
110
- end
111
-
112
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME homedirectory "$VALUE"
113
- def user_set_home_directory(attribs, dir_info)
114
- attribs = user_record_name_alternatives(attribs)
115
-
116
- attribs[:value] = attribs[:value] || attribs[:home_directory]
117
- attribs[:value] = attribs[:value] || attribs[:nfs_home_directory]
118
- attribs[:value] = attribs[:value] || '/Volumes/Macintosh HD/Users/someone'
119
-
120
- command = {action: 'create', scope: 'Users', attribute: 'homedirectory'}
121
- attribs = attribs.merge(command)
122
-
123
- check_critical_attribute( attribs, :record_name )
124
- check_critical_attribute( attribs, :value, :home_directory )
125
- user_attrs = tidy_attribs(attribs)
126
-
127
- dscl( user_attrs, dir_info )
128
- end
129
-
130
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME loginShell "$VALUE"
131
- def user_set_login_shell(attribs, dir_info)
132
- attribs = user_record_name_alternatives(attribs)
133
-
134
- attribs[:value] = attribs[:value] || attribs[:user_shell]
135
- attribs[:value] = attribs[:value] || attribs[:shell]
136
- attribs[:value] = attribs[:value] || '/bin/bash'
137
-
138
- check_critical_attribute( attribs, :record_name )
139
- check_critical_attribute( attribs, :value, :shell )
140
- attribs = tidy_attribs(attribs)
141
-
142
- command = {action: 'create', scope: 'Users', attribute: 'loginShell'}
143
- user_attrs = attribs.merge(command)
144
-
145
- dscl( user_attrs, dir_info )
146
- end
147
-
148
-
149
- # OTHER FIELDS
150
- #####################
151
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME mail "$VALUE"
152
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME email "$VALUE"
153
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME apple-user-mailattribute "$VALUE"
154
- def user_set_first_email(attribs, dir_info)
155
- attribs = user_record_name_alternatives(attribs)
156
-
157
- attribs[:value] = attribs[:value] || attribs['apple-user-mailattribute']
158
- attribs[:value] = attribs[:value] || attribs[:apple_user_mailattribute]
159
- attribs[:value] = attribs[:value] || attribs[:email]
160
- attribs[:value] = attribs[:value] || attribs[:mail]
161
-
162
- check_critical_attribute( attribs, :record_name )
163
- check_critical_attribute( attribs, :value, :email )
164
- attribs = tidy_attribs(attribs)
165
-
166
- answer = []
167
-
168
- command = {action: 'create', scope: 'Users', attribute: 'mail'}
169
- user_attrs = attribs.merge(command)
170
- answer << dscl( user_attrs, dir_info )
171
-
172
- command = {action: 'create', scope: 'Users', attribute: 'email'}
173
- user_attrs = attribs.merge(command)
174
- answer << dscl( user_attrs, dir_info )
175
-
176
- command = {action: 'create', scope: 'Users', attribute: 'apple-user-mailattribute'}
177
- user_attrs = attribs.merge(command)
178
- answer << dscl( user_attrs, dir_info )
179
-
180
- return answer
181
- end
182
- alias_method :user_set_email, :user_set_first_email
183
-
184
- def user_append_email(attribs, dir_info)
185
- attribs = user_record_name_alternatives(attribs)
186
-
187
- attribs[:value] = attribs[:value] || attribs['apple-user-mailattribute']
188
- attribs[:value] = attribs[:value] || attribs[:apple_user_mailattribute]
189
- attribs[:value] = attribs[:value] || attribs[:email]
190
- attribs[:value] = attribs[:value] || attribs[:mail]
191
-
192
- check_critical_attribute( attribs, :record_name )
193
- check_critical_attribute( attribs, :value, :email )
194
- attribs = tidy_attribs(attribs)
195
-
196
- answer = []
197
-
198
- command = {action: 'append', scope: 'Users', attribute: 'mail'}
199
- user_attrs = attribs.merge(command)
200
- answer << dscl( user_attrs, dir_info )
201
-
202
- command = {action: 'append', scope: 'Users', attribute: 'email'}
203
- user_attrs = attribs.merge(command)
204
- answer << dscl( user_attrs, dir_info )
205
-
206
- return answer
207
- end
208
-
209
- # dscl . -delete /Users/yourUserName
210
- # https://tutorialforlinux.com/2011/09/15/delete-users-and-groups-from-terminal/
211
- def user_delete(attribs, dir_info)
212
- attribs = user_record_name_alternatives(attribs)
213
-
214
- check_critical_attribute( attribs, :record_name )
215
- attribs = tidy_attribs(attribs)
216
-
217
- command = {action: 'delete', scope: 'Users', value: nil, attribute: nil}
218
- user_attrs = attribs.merge(command)
219
-
220
- dscl( user_attrs, dir_info )
221
- end
222
-
223
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME mobile "$VALUE"
224
- def user_set_mobile_phone
225
- end
226
-
227
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME telephoneNumber "$VALUE"
228
- def user_set_work_phone
229
- end
230
-
231
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME homePhone "$VALUE"
232
- def user_set_home_phone
233
- end
234
-
235
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME title "$VALUE"
236
- def user_set_title
237
- end
238
-
239
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME departmentNumber "$VALUE"
240
- def user_set_department
241
- end
242
-
243
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME street "$VALUE"
244
- def user_set_street
245
- end
246
- alias_method :las_set_dorm, :user_set_street
247
- alias_method :las_set_housing, :user_set_street
248
-
249
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname l "$VALUE"
250
- def user_set_city
251
- end
252
- alias_method :las_, :user_set_city
253
-
254
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME st "$VALUE"
255
- def user_set_state
256
- end
257
- alias_method :las_cultural_trip, :user_set_state
258
-
259
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME postalCode "$VALUE"
260
- def user_set_postcode
261
- end
262
- alias_method :las_faculty_family, :user_set_postcode
263
-
264
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$USER c "$VALUE"
265
- def user_set_country
266
- end
267
-
268
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$shortname_USERNAME labeledURI "$VALUE"
269
- def user_set_homepage
270
- end
271
- alias_method :user_set_webpage, :user_set_homepage
272
- alias_method :las_enrollment_date, :user_set_homepage
273
- alias_method :las_begin_date, :user_set_homepage
274
-
275
- # /usr/bin/dscl -u diradmin -P A-B1g-S3cret /LDAPv3/127.0.0.1 -create /Users/$USER description "$NAME"
276
- def user_set_comments
277
- end
278
- alias_method :user_set_description, :user_set_comments
279
-
280
- end
281
- end
@@ -1,53 +0,0 @@
1
- require "open_directory_utils/clean_check"
2
-
3
- module OpenDirectoryUtils
4
-
5
- # https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/dscl.1.html
6
- # https://superuser.com/questions/592921/mac-osx-users-vs-dscl-command-to-list-user/621055?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
7
- module Dscl
8
-
9
- include OpenDirectoryUtils::CleanCheck
10
-
11
- # builds the dscl command (with complete flexibility)
12
- # attribs [Hash] - required - :record_name (the resource to affect), :action (create, append, delete, passwd, etc), attribute: (resource attribute to change), value: (value to add to attribute)
13
- # dir_info [Hash] - usually configured in the connection initializer and then passed to dscl to build command correctly
14
- def dscl(attribs, dir_info)
15
- check_critical_attribute( attribs, :record_name )
16
- check_critical_attribute( attribs, :action )
17
- check_critical_attribute( attribs, :scope )
18
- tidy_attribs = tidy_attribs(attribs)
19
- build_dscl_command( tidy_attribs, dir_info )
20
- end
21
-
22
- # TODO: switch to template pattern
23
- def build_dscl_command(attribs, dir_info)
24
- # allow :recordname to be passed-in if using dscl directly
25
- attribs[:record_name] = attribs[:record_name] || attribs[:recordname]
26
- # /usr/bin/dscl -u diradmin -P "BigSecret" /LDAPv3/127.0.0.1 -append /Users/$UID_USERNAME apple-keyword "$VALUE"
27
- # "/usr/bin/dscl -plist -u #{od_username} -P #{od_password} #{od_dsclpath} -#{command} #{resource} #{params}"
28
- ans = "#{dir_info[:dscl]}"
29
- unless attribs[:format].nil?
30
- ans += ' -plist' if attribs[:format].eql? 'plist' or
31
- attribs[:format].eql? 'xml'
32
- end
33
- ans += " -u #{dir_info[:username]}" unless dir_info[:username].nil? or
34
- dir_info[:username].empty? or
35
- attribs[:action].eql? 'auth'
36
- ans += %Q[ -P "#{dir_info[:password]}"] unless dir_info[:password].nil? or
37
- dir_info[:password].empty? or
38
- attribs[:action].eql? 'auth'
39
- ans += " #{dir_info[:data_path]}"
40
-
41
- ans += %Q[ -#{attribs[:action]}]
42
- ans += %Q[ #{attribs[:record_name]}] if attribs[:action].eql? 'auth'
43
- ans += %Q[ /#{attribs[:scope]}/#{attribs[:record_name]}] unless
44
- attribs[:action].eql? 'auth'
45
- ans += %Q[ #{attribs[:attribute]}] unless attribs[:attribute].nil? or
46
- attribs[:attribute].empty?
47
- ans += %Q[ "#{attribs[:value]}"] unless attribs[:value].nil? or
48
- attribs[:value].empty?
49
- return ans
50
- end
51
-
52
- end
53
- end
@@ -1,48 +0,0 @@
1
- require "open_directory_utils/clean_check"
2
-
3
- module OpenDirectoryUtils
4
-
5
- # https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/dscl.1.html
6
- # https://superuser.com/questions/592921/mac-osx-users-vs-dscl-command-to-list-user/621055?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
7
- module Pwpolicy
8
-
9
- include OpenDirectoryUtils::CleanCheck
10
-
11
- def build_pwpolicy_command(params, dir_info)
12
- # /usr/bin/pwpolicy -a diradmin -p "BigSecret" -u username -setpolicy "isDisabled=0"
13
- ans = "#{dir_info[:pwpol]}"
14
- ans += " -a #{dir_info[:diradmin]}" unless dir_info[:diradmin].nil? or
15
- dir_info[:diradmin].empty?
16
- ans += %Q[ -p "#{dir_info[:password]}"] unless dir_info[:password].nil? or
17
- dir_info[:password].empty?
18
- ans += %Q[ -u #{params[:record_name]}]
19
- ans += %Q[ -#{params[:attribute]}]
20
- ans += %Q[ "#{params[:value]}"] unless params[:value].nil? or
21
- params[:value].empty?
22
- return ans
23
- end
24
-
25
- def pwpolicy(params, dir_info)
26
- check_critical_attribute( params, :record_name )
27
- cmd_params = tidy_attribs(params)
28
-
29
- build_pwpolicy_command( cmd_params, dir_info )
30
- end
31
-
32
- ## PRE-BUILT commands
33
- #####################
34
- # /usr/bin/pwpolicy -a diradmin -p A-B1g-S3cret -u $shortname_USERNAME -setpolicy "isDisabled=0"
35
- def user_enable_login(params, dir_info)
36
- command = {attribute: 'enableuser'}
37
- params = command.merge(params)
38
- pwpolicy(params, dir_info)
39
- end
40
- # /usr/bin/pwpolicy -a diradmin -p A-B1g-S3cret -u $shortname_USERNAME -setpolicy "isDisabled=1"
41
- def user_disable_login(params, dir_info)
42
- command = {attribute: 'disableuser'}
43
- params = command.merge(params)
44
- pwpolicy(params, dir_info)
45
- end
46
-
47
- end
48
- end