author | Mahlon E. Smith <mahlon@laika.com> |
Fri, 15 Mar 2013 10:02:20 -0700 | |
changeset 50 | 21ba5eb5c2fc |
parent 49 | 57df728cdb77 |
child 51 | 27bbe75233a3 |
permissions | -rwxr-xr-x |
0 | 1 |
#!/usr/bin/env perl |
2 |
# vim: set nosta noet ts=4 sw=4: |
|
3 |
# |
|
42
b8c6d4e8f828
Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents:
41
diff
changeset
|
4 |
# Copyright (c) 2006-2012, Mahlon E. Smith <mahlon@martini.nu> |
0 | 5 |
# All rights reserved. |
6 |
# Redistribution and use in source and binary forms, with or without |
|
7 |
# modification, are permitted provided that the following conditions are met: |
|
8 |
# |
|
9 |
# * Redistributions of source code must retain the above copyright |
|
10 |
# notice, this list of conditions and the following disclaimer. |
|
6
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
11 |
# |
0 | 12 |
# * Redistributions in binary form must reproduce the above copyright |
13 |
# notice, this list of conditions and the following disclaimer in the |
|
14 |
# documentation and/or other materials provided with the distribution. |
|
6
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
15 |
# |
0 | 16 |
# * Neither the name of Mahlon E. Smith nor the names of his |
17 |
# contributors may be used to endorse or promote products derived |
|
18 |
# from this software without specific prior written permission. |
|
19 |
# |
|
20 |
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY |
|
21 |
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
22 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
23 |
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY |
|
24 |
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
25 |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
26 |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
27 |
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
28 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
29 |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30 |
||
31 |
=head1 NAME |
|
32 |
||
10
664bbe3dcd44
Follow regular man page conventions. Patch from
Mahlon E. Smith <mahlon@laika.com>
parents:
9
diff
changeset
|
33 |
Shelldap - A program for interacting with an LDAP server via a shell-like interface |
0 | 34 |
|
10
664bbe3dcd44
Follow regular man page conventions. Patch from
Mahlon E. Smith <mahlon@laika.com>
parents:
9
diff
changeset
|
35 |
=head1 DESCRIPTION |
664bbe3dcd44
Follow regular man page conventions. Patch from
Mahlon E. Smith <mahlon@laika.com>
parents:
9
diff
changeset
|
36 |
|
664bbe3dcd44
Follow regular man page conventions. Patch from
Mahlon E. Smith <mahlon@laika.com>
parents:
9
diff
changeset
|
37 |
Shelldap /LDAP::Shell is a program for interacting with an LDAP server via a shell-like |
0 | 38 |
interface. |
39 |
||
40 |
This is not meant to be an exhaustive LDAP editing and browsing |
|
41 |
interface, but rather an intuitive shell for performing basic LDAP |
|
42 |
tasks quickly and with minimal effort. |
|
43 |
||
44 |
=head1 SYNPOSIS |
|
45 |
||
3
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
46 |
shelldap --server example.net [--help] |
0 | 47 |
|
48 |
=head1 FEATURES |
|
49 |
||
50 |
- Upon successful authenticated binding, credential information is |
|
51 |
auto-cached to ~/.shelldap.rc -- future loads require no command line |
|
52 |
flags. |
|
53 |
||
54 |
- Custom 'description maps' for entry listings. (See the 'list' command.) |
|
55 |
||
56 |
- History and autocomplete via readline, if installed. |
|
57 |
||
58 |
- Automatic reconnection attempts if the connection is lost with the |
|
59 |
LDAP server. |
|
60 |
||
61 |
- It feels like a semi-crippled shell, making LDAP browsing and editing |
|
62 |
at least halfway pleasurable. |
|
63 |
||
64 |
=head1 OPTIONS |
|
65 |
||
66 |
All command line options follow getopts long conventions. |
|
67 |
||
68 |
shelldap --server example.net --basedn dc=your,o=company |
|
69 |
||
70 |
You may also optionally create a ~/.shelldap.rc file with command line |
|
71 |
defaults. This file should be valid YAML. (This file is generated |
|
72 |
automatically on a successful bind auth.) |
|
73 |
||
74 |
Example: |
|
75 |
||
76 |
server: ldap.example.net |
|
77 |
binddn: cn=Manager,dc=your,o=company |
|
78 |
bindpass: xxxxxxxxx |
|
79 |
basedn: dc=your,o=company |
|
80 |
tls: yes |
|
3
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
81 |
tls_cacert: /etc/ssl/certs/cacert.pem |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
82 |
tls_cert: ~/.ssl/client.cert.pem |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
83 |
tls_key: ~/.ssl/private/client.key.pem |
0 | 84 |
|
85 |
=over 4 |
|
86 |
||
50 | 87 |
=item B<configfile> |
88 |
||
89 |
Optional. Use an alternate configuration file, instead of the |
|
90 |
default ~/.shelldap.rc. |
|
91 |
||
92 |
--configfile /tmp/alternate-config.yml |
|
93 |
-f /tmp/alternate-config.yml |
|
94 |
||
95 |
=back |
|
96 |
||
97 |
=over 4 |
|
98 |
||
0 | 99 |
=item B<server> |
100 |
||
101 |
Required. The LDAP server to connect to. This can be a hostname, IP |
|
102 |
address, or a URI. |
|
103 |
||
104 |
--server ldaps://ldap.example.net |
|
19
18e71da965ff
Add documentation for the additional short flags.
Mahlon E. Smith <mahlon@martini.nu>
parents:
18
diff
changeset
|
105 |
-H ldaps://ldap.example.net |
0 | 106 |
|
107 |
=back |
|
108 |
||
109 |
=over 4 |
|
110 |
||
111 |
=item B<binddn> |
|
112 |
||
113 |
The full dn of a user to authenticate as. If not specified, defaults to |
|
114 |
an anonymous bind. You will be prompted for a password. |
|
115 |
||
116 |
--binddn cn=Manager,dc=your,o=company |
|
19
18e71da965ff
Add documentation for the additional short flags.
Mahlon E. Smith <mahlon@martini.nu>
parents:
18
diff
changeset
|
117 |
-D cn=Manager,dc=your,o=company |
0 | 118 |
|
119 |
=back |
|
120 |
||
121 |
=over 4 |
|
122 |
||
123 |
=item B<basedn> |
|
124 |
||
125 |
The directory 'root' of your LDAP server. If omitted, shelldap will |
|
126 |
try and ask the server for a sane default. |
|
127 |
||
128 |
--basedn dc=your,o=company |
|
19
18e71da965ff
Add documentation for the additional short flags.
Mahlon E. Smith <mahlon@martini.nu>
parents:
18
diff
changeset
|
129 |
-b dc=your,o=company |
0 | 130 |
|
131 |
=back |
|
132 |
||
133 |
=over 4 |
|
134 |
||
42
b8c6d4e8f828
Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents:
41
diff
changeset
|
135 |
=item B<promptpass> |
b8c6d4e8f828
Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents:
41
diff
changeset
|
136 |
|
b8c6d4e8f828
Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents:
41
diff
changeset
|
137 |
Force password prompting. Useful to temporarily override cached |
b8c6d4e8f828
Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents:
41
diff
changeset
|
138 |
credentials. |
b8c6d4e8f828
Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents:
41
diff
changeset
|
139 |
|
b8c6d4e8f828
Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents:
41
diff
changeset
|
140 |
=back |
b8c6d4e8f828
Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents:
41
diff
changeset
|
141 |
|
b8c6d4e8f828
Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents:
41
diff
changeset
|
142 |
=over 4 |
b8c6d4e8f828
Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents:
41
diff
changeset
|
143 |
|
b8c6d4e8f828
Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents:
41
diff
changeset
|
144 |
=item B<tls> |
0 | 145 |
|
146 |
Enables TLS over what would normally be an insecure connection. |
|
147 |
Requires server side support. |
|
148 |
||
3
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
149 |
=item B<tls_cacert> |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
150 |
|
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
151 |
Specify CA Certificate to trust. |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
152 |
|
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
153 |
--tls_cacert /etc/ssl/certs/cacert.pem |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
154 |
|
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
155 |
=item B<tls_cert> |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
156 |
|
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
157 |
The TLS client certificate. |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
158 |
|
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
159 |
--tls_cert ~/.ssl/client.cert.pem |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
160 |
|
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
161 |
=item B<tls_key> |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
162 |
|
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
163 |
The TLS client key. Not specifying a key will connect via TLS without |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
164 |
key verification. |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
165 |
|
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
166 |
--tls_key ~/.ssl/private/client.key.pem |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
167 |
|
0 | 168 |
=back |
169 |
||
170 |
=over 4 |
|
171 |
||
172 |
=item B<cacheage> |
|
173 |
||
174 |
Set the time to cache directory lookups in seconds. |
|
175 |
||
176 |
By default, directory lookups are cached for 300 seconds, to speed |
|
177 |
autocomplete up when changing between different basedns. |
|
178 |
||
179 |
Modifications to the directory automatically reset the cache. Directory |
|
180 |
listings are not cached. (This is just used for autocomplete.) Set it |
|
181 |
to 0 to disable caching completely. |
|
182 |
||
183 |
=back |
|
184 |
||
185 |
=over 4 |
|
186 |
||
187 |
=item B<timeout> |
|
188 |
||
189 |
Set the maximum time an LDAP operation can take before it is cancelled. |
|
190 |
||
191 |
=back |
|
192 |
||
193 |
=over 4 |
|
194 |
||
195 |
=item B<debug> |
|
196 |
||
197 |
Print extra operational info out, and backtrace on fatal error. |
|
198 |
||
199 |
=back |
|
200 |
||
37 | 201 |
=over 4 |
202 |
||
203 |
=item B<version> |
|
204 |
||
205 |
Display the version number. |
|
206 |
||
207 |
=back |
|
208 |
||
0 | 209 |
=head1 SHELL COMMANDS |
210 |
||
211 |
=over 4 |
|
212 |
||
213 |
=item B< cat> |
|
214 |
||
215 |
Display an LDIF dump of an entry. Globbing is supported. Specify |
|
216 |
either the full dn, or an rdn. For most commands, rdns are local to the |
|
217 |
current search base. ('cwd', as translated to shell speak.) You may additionally |
|
218 |
add a list of attributes to display. Use '+' for server side attributes. |
|
219 |
||
220 |
cat uid=mahlon |
|
221 |
cat ou=* |
|
222 |
cat uid=mahlon,ou=People,dc=example,o=company |
|
223 |
cat uid=mahlon + userPassword |
|
224 |
||
225 |
=item B< cd> |
|
226 |
||
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
227 |
Change directory. Translated to LDAP, this changes the current basedn. |
0 | 228 |
All commands after a 'cd' operate within the new basedn. |
229 |
||
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
230 |
cd change to 'home' basedn |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
231 |
cd ~ change to the binddn, or basedn if anonymously bound |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
232 |
cd - change to previous node |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
233 |
cd ou=People change to explicit path below current node |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
234 |
cd .. change to parent node |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
235 |
cd ../../ou=Groups change to node ou=Groups, which is a sibling |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
236 |
to the current node's grandparent |
0 | 237 |
|
238 |
Since LDAP doesn't actually limit what can be a container object, you |
|
239 |
can actually cd into any entry. Many commands then work on '.', meaning |
|
240 |
"wherever I currently am." |
|
241 |
||
242 |
cd uid=mahlon |
|
243 |
cat . |
|
244 |
||
245 |
=item B<clear> |
|
246 |
||
247 |
Clear the screen. |
|
248 |
||
249 |
=item B<copy> |
|
250 |
||
251 |
Copy an entry to a different dn path. All copies are relative to the |
|
252 |
current basedn, unless a full dn is specified. All attributes are |
|
253 |
copied, then an LDAP moddn() is performed. |
|
254 |
||
255 |
copy uid=mahlon uid=bob |
|
256 |
copy uid=mahlon ou=Others,dc=example,o=company |
|
257 |
copy uid=mahlon,ou=People,dc=example,o=company uid=mahlon,ou=Others,dc=example,o=company |
|
258 |
||
259 |
aliased to: cp |
|
260 |
||
261 |
=item B<create> |
|
262 |
||
263 |
Create an entry from scratch. Arguments are space separated objectClass |
|
264 |
names. Possible objectClasses are derived automatically from the |
|
265 |
server, and will tab-complete. |
|
266 |
||
267 |
After the classes are specified, an editor will launch. Required |
|
268 |
attributes are listed first, then optional attributes. Optionals are |
|
269 |
commented out. After the editor exits, the resulting LDIF is validated |
|
270 |
and added to the LDAP directory. |
|
271 |
||
272 |
create top person organizationalPerson inetOrgPerson posixAccount |
|
273 |
||
274 |
aliased to: touch |
|
275 |
||
276 |
=item B<delete> |
|
277 |
||
278 |
Remove an entry from the directory. Globbing is supported. |
|
279 |
All deletes are sanity-prompted. |
|
280 |
||
281 |
delete uid=mahlon |
|
282 |
delete uid=ma* |
|
283 |
||
284 |
aliased to: rm |
|
285 |
||
286 |
=item B<edit> |
|
287 |
||
288 |
Edit an entry in an external editor. After the editor exits, the |
|
289 |
resulting LDIF is sanity checked, and changes are written to the LDAP |
|
290 |
directory. |
|
291 |
||
292 |
edit uid=mahlon |
|
293 |
||
294 |
aliased to: vi |
|
295 |
||
296 |
=item B< env> |
|
297 |
||
298 |
Show values for various runtime variables. |
|
299 |
||
300 |
=item B<grep> |
|
301 |
||
302 |
Search for arbitrary LDAP filters, and return matching dn results. |
|
303 |
The search string must be a valid LDAP filter. |
|
304 |
||
305 |
grep uid=mahlon |
|
306 |
grep uid=mahlon ou=People |
|
307 |
grep -r (&(uid=mahlon)(objectClass=*)) |
|
308 |
||
309 |
aliased to: search |
|
310 |
||
311 |
=item B<list> |
|
312 |
||
313 |
List entries for the current basedn. Globbing is supported. |
|
314 |
||
315 |
aliased to: ls |
|
316 |
||
317 |
ls -l |
|
318 |
ls -lR uid=mahlon |
|
319 |
list uid=m* |
|
320 |
||
36
2e78218b8045
Small documentation fixes, add better verbosity when saving connection
Mahlon E. Smith <mahlon@martini.nu>
parents:
35
diff
changeset
|
321 |
In 'long' mode, descriptions are listed as well, if they exist. |
2e78218b8045
Small documentation fixes, add better verbosity when saving connection
Mahlon E. Smith <mahlon@martini.nu>
parents:
35
diff
changeset
|
322 |
There are some default 'long listing' mappings for common objectClass |
2e78218b8045
Small documentation fixes, add better verbosity when saving connection
Mahlon E. Smith <mahlon@martini.nu>
parents:
35
diff
changeset
|
323 |
types. You can additionally specify your own mappings in your |
2e78218b8045
Small documentation fixes, add better verbosity when saving connection
Mahlon E. Smith <mahlon@martini.nu>
parents:
35
diff
changeset
|
324 |
.shelldap.rc, like so: |
0 | 325 |
|
326 |
... |
|
327 |
descmaps: |
|
328 |
objectClass: attributename |
|
329 |
posixAccount: gecos |
|
330 |
posixGroup: gidNumber |
|
331 |
ipHost: ipHostNumber |
|
332 |
||
333 |
=item B<mkdir> |
|
334 |
||
38
12f279ef4f9d
Backout the additional objectClasses patch for mkdir: same behavior can
Mahlon E. Smith <mahlon@martini.nu>
parents:
37
diff
changeset
|
335 |
Creates a new 'organizationalUnit' entry. |
0 | 336 |
|
38
12f279ef4f9d
Backout the additional objectClasses patch for mkdir: same behavior can
Mahlon E. Smith <mahlon@martini.nu>
parents:
37
diff
changeset
|
337 |
mkdir containername |
12f279ef4f9d
Backout the additional objectClasses patch for mkdir: same behavior can
Mahlon E. Smith <mahlon@martini.nu>
parents:
37
diff
changeset
|
338 |
mkdir ou=whatever |
0 | 339 |
|
340 |
=item B<move> |
|
341 |
||
342 |
Move an entry to a different dn path. Usage is identical to B<copy>. |
|
343 |
||
344 |
aliased to: mv |
|
345 |
||
346 |
=item B<passwd> |
|
347 |
||
348 |
If supported server side, change the password for a specified entry. |
|
349 |
The entry must have a 'userPassword' attribute. |
|
350 |
||
351 |
passwd uid=mahlon |
|
352 |
||
353 |
=item B< pwd> |
|
354 |
||
355 |
Print the 'working directory' - aka, the current ldap basedn. |
|
356 |
||
357 |
=item B<setenv> |
|
358 |
||
359 |
Modify various runtime variables normally set from the command line. |
|
360 |
||
361 |
setenv debug 1 |
|
362 |
export debug=1 |
|
363 |
||
364 |
=item B<whoami> |
|
365 |
||
366 |
Show current auth credentials. Unless you specified a binddn, this |
|
367 |
will just show an anonymous bind. |
|
368 |
||
369 |
=back |
|
370 |
||
371 |
=head1 TODO |
|
372 |
||
373 |
Referral support. Currently, if you try to write to a replicant slave, |
|
374 |
you'll just get a referral. It would be nice if shelldap automatically |
|
375 |
tried to follow it. |
|
376 |
||
377 |
For now, it only makes sense to connect to a master if you plan on doing |
|
378 |
any writes. |
|
379 |
||
380 |
=head1 BUGS / LIMITATIONS |
|
381 |
||
6
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
382 |
There is no support for editing binary data. If you need to edit base64 |
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
383 |
stuff, just feed it to the regular ldapmodify/ldapadd/etc tools. |
0 | 384 |
|
385 |
=head1 AUTHOR |
|
386 |
||
387 |
Mahlon E. Smith <mahlon@martini.nu> |
|
388 |
||
389 |
=cut |
|
390 |
||
391 |
package LDAP::Shell; |
|
392 |
use strict; |
|
393 |
use warnings; |
|
394 |
use Term::ReadKey; |
|
395 |
use Term::Shell; |
|
396 |
use Digest::MD5; |
|
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
397 |
use Net::LDAP qw/ |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
398 |
LDAP_SUCCESS |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
399 |
LDAP_SERVER_DOWN |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
400 |
LDAP_OPERATIONS_ERROR |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
401 |
LDAP_TIMELIMIT_EXCEEDED |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
402 |
LDAP_BUSY |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
403 |
LDAP_UNAVAILABLE |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
404 |
LDAP_OTHER |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
405 |
LDAP_TIMEOUT |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
406 |
LDAP_NO_MEMORY |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
407 |
LDAP_CONNECT_ERROR /; |
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
408 |
use Net::LDAP::Util qw/ canonical_dn ldap_explode_dn /; |
0 | 409 |
use Net::LDAP::LDIF; |
410 |
use Data::Dumper; |
|
411 |
use File::Temp; |
|
412 |
use Algorithm::Diff; |
|
413 |
use Carp 'confess'; |
|
414 |
use base 'Term::Shell'; |
|
415 |
require Net::LDAP::Extension::SetPassword; |
|
416 |
||
417 |
my $conf = $main::conf; |
|
418 |
||
419 |
# make 'die' backtrace in debug mode |
|
420 |
$SIG{'__DIE__'} = \&Carp::confess if $conf->{'debug'}; |
|
421 |
||
422 |
||
50 | 423 |
######################################################################## |
424 |
### U T I L I T Y F U N C T I O N S |
|
425 |
######################################################################## |
|
426 |
||
427 |
### Initial shell behaviors. |
|
428 |
### |
|
0 | 429 |
sub init |
430 |
{ |
|
431 |
my $self = shift; |
|
432 |
$self->{'API'}->{'match_uniq'} = 0; |
|
433 |
||
50 | 434 |
$self->{'editor'} = $conf->{'editor'} || $ENV{'EDITOR'} || 'vi'; |
0 | 435 |
$self->{'env'} = [ qw/ debug cacheage timeout / ]; |
436 |
||
437 |
# let autocomplete work with the '=' character |
|
438 |
my $term = $self->term(); |
|
439 |
$term->Attribs->{'basic_word_break_characters'} =~ s/=//m; |
|
440 |
$term->Attribs->{'completer_word_break_characters'} =~ s/=//m; |
|
441 |
||
442 |
# read in history |
|
443 |
eval { |
|
444 |
$term->history_truncate_file("$ENV{'HOME'}/.shelldap_history", 50); |
|
445 |
$term->ReadHistory("$ENV{'HOME'}/.shelldap_history"); |
|
446 |
}; |
|
447 |
||
50 | 448 |
# gather metadata from the LDAP server |
0 | 449 |
$self->{'root_dse'} = $self->ldap->root_dse(); |
50 | 450 |
$self->{'schema'} = $self->ldap->schema(); |
451 |
||
452 |
# get an initial list of all objectClasses |
|
453 |
$self->{'objectclasses'} = []; |
|
454 |
foreach my $o ( $self->{'schema'}->all_objectclasses() ) { |
|
455 |
push @{ $self->{'objectclasses'} }, $o->{'name'}; |
|
456 |
} |
|
457 |
||
0 | 458 |
if ( $conf->{'debug'} ) { |
17
669085d93aa3
simplify over-complex call of N:L:E->get_value()
Peter Marschall <peter@adpm.de>
parents:
16
diff
changeset
|
459 |
my @versions = $self->{'root_dse'}->get_value('supportedLDAPVersion'); |
0 | 460 |
print "Connected to $conf->{'server'}\n"; |
461 |
print "Supported LDAP version: ", ( join ', ', @versions ), "\n"; |
|
462 |
print "Cipher in use: ", $self->ldap()->cipher(), "\n"; |
|
463 |
} |
|
464 |
||
50 | 465 |
# try an initial search and bail early if it doesn't work. (bad baseDN?) |
0 | 466 |
my $s = $self->search(); |
467 |
die "LDAP baseDN error: ", $s->{'message'}, "\n" if $s->{'code'}; |
|
468 |
||
50 | 469 |
# okay, now do an initial population of 'cwd' for autocomplete. |
0 | 470 |
$self->update_entries(); |
471 |
||
472 |
# whew, okay. Update prompt, wait for input! |
|
473 |
$self->update_prompt(); |
|
474 |
||
475 |
return; |
|
476 |
} |
|
477 |
||
478 |
||
50 | 479 |
### Return an LDAP connection handle, creating it if necessary. |
480 |
### |
|
0 | 481 |
sub ldap |
482 |
{ |
|
483 |
my $self = shift; |
|
50 | 484 |
my $rv; |
0 | 485 |
|
486 |
# use cached connection object if it exists |
|
487 |
return $self->{'ldap'} if $self->{'ldap'}; |
|
488 |
||
489 |
# fill in potentially missing info |
|
490 |
die "No server specified.\n" unless $conf->{'server'}; |
|
15
f6157d378459
Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents:
14
diff
changeset
|
491 |
|
f6157d378459
Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents:
14
diff
changeset
|
492 |
# Emit a nicer error message if IO::Socket::SSL is |
f6157d378459
Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents:
14
diff
changeset
|
493 |
# not installed and Net::LDAP decides it is required. |
f6157d378459
Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents:
14
diff
changeset
|
494 |
# |
f6157d378459
Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents:
14
diff
changeset
|
495 |
if ( $conf->{'tls'} || $conf->{'server'} =~ m|ldaps://| ) { |
f6157d378459
Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents:
14
diff
changeset
|
496 |
eval 'use IO::Socket::SSL'; |
f6157d378459
Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents:
14
diff
changeset
|
497 |
die qq{IO::Socket::SSL not installed, but is required for SSL or TLS connections. |
f6157d378459
Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents:
14
diff
changeset
|
498 |
You may try connecting insecurely, or install the module and try again.\n} if $@; |
f6157d378459
Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents:
14
diff
changeset
|
499 |
} |
f6157d378459
Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents:
14
diff
changeset
|
500 |
|
50 | 501 |
# Prompt for a password after disabling local echo. |
502 |
# |
|
42
b8c6d4e8f828
Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents:
41
diff
changeset
|
503 |
if ( ($conf->{'binddn'} && ! $conf->{'bindpass'}) || $conf->{'promptpass'} ) { |
0 | 504 |
print "Bind password: "; |
505 |
Term::ReadKey::ReadMode 2; |
|
50 | 506 |
chomp( $conf->{'bindpass'} = <STDIN> ); |
0 | 507 |
Term::ReadKey::ReadMode 0; |
508 |
print "\n"; |
|
509 |
} |
|
510 |
||
50 | 511 |
# make the connection |
0 | 512 |
my $ldap = Net::LDAP->new( $conf->{'server'} ) |
513 |
or die "Unable to connect to LDAP server '$conf->{'server'}': $!\n"; |
|
3
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
514 |
|
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
515 |
# secure connection options |
15
f6157d378459
Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents:
14
diff
changeset
|
516 |
# |
f6157d378459
Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents:
14
diff
changeset
|
517 |
if ( $conf->{'tls'} ) { |
3
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
518 |
if ( $conf->{'tls_key'} ) { |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
519 |
$ldap->start_tls( |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
520 |
verify => 'require', |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
521 |
cafile => $conf->{'tls_cacert'}, |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
522 |
clientcert => $conf->{'tls_cert'}, |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
523 |
clientkey => $conf->{'tls_key'}, |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
524 |
keydecrypt => sub { |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
525 |
print "Key Passphrase: "; |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
526 |
Term::ReadKey::ReadMode 2; |
50 | 527 |
chomp( my $secret = <STDIN> ); |
3
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
528 |
Term::ReadKey::ReadMode 0; |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
529 |
print "\n"; |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
530 |
return $secret; |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
531 |
}); |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
532 |
} |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
533 |
else { |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
534 |
$ldap->start_tls( verify => 'none' ); |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
535 |
} |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
536 |
} |
0 | 537 |
|
50 | 538 |
# bind as an authenicated dn |
0 | 539 |
if ( $conf->{'binddn'} ) { |
540 |
$rv = $ldap->bind( |
|
541 |
$conf->{'binddn'}, |
|
542 |
password => $conf->{'bindpass'} |
|
543 |
); |
|
544 |
} |
|
50 | 545 |
|
546 |
# bind anonymously |
|
0 | 547 |
else { |
548 |
$rv = $ldap->bind(); |
|
549 |
} |
|
550 |
||
551 |
my $err = $rv->error(); |
|
552 |
if ( $rv->code() ) { |
|
50 | 553 |
$err .= " (try the --tls flag?)" if $err =~ /confidentiality required/i; |
0 | 554 |
die "LDAP bind error: $err\n"; |
555 |
} |
|
556 |
||
557 |
# offer to cache authentication info |
|
558 |
# if we enter this conditional, we have successfully |
|
559 |
# authed with the server (non anonymous), and |
|
560 |
# we haven't cached anything in the past. |
|
15
f6157d378459
Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents:
14
diff
changeset
|
561 |
# |
50 | 562 |
if ( $conf->{'binddn'} && ! -e $conf->{'configfile'} ) { |
563 |
print "Would you like to cache your connection information? [Yn]: "; |
|
0 | 564 |
chomp( my $response = <STDIN> ); |
565 |
unless ( $response =~ /^n/i ) { |
|
50 | 566 |
YAML::Syck::DumpFile( $conf->{'configfile'}, $conf ); |
567 |
chmod 0600, $conf->{'configfile'}; |
|
568 |
print "Connection info cached to $conf->{'configfile'}.\n"; |
|
0 | 569 |
} |
570 |
} |
|
571 |
||
572 |
$self->{'ldap'} = $ldap; |
|
573 |
return $ldap; |
|
574 |
} |
|
575 |
||
50 | 576 |
|
577 |
### Return a new LDIF object, suitable for populating with |
|
578 |
### a Net::LDAP::Entry. |
|
579 |
### |
|
0 | 580 |
sub ldif |
581 |
{ |
|
582 |
my $self = shift; |
|
583 |
my $use_temp = shift; |
|
584 |
||
49
57df728cdb77
Alter the default wrap width for LDIF to expand to the terminal size,
Mahlon E. Smith <mahlon@laika.com>
parents:
48
diff
changeset
|
585 |
# find the terminal width |
57df728cdb77
Alter the default wrap width for LDIF to expand to the terminal size,
Mahlon E. Smith <mahlon@laika.com>
parents:
48
diff
changeset
|
586 |
# |
57df728cdb77
Alter the default wrap width for LDIF to expand to the terminal size,
Mahlon E. Smith <mahlon@laika.com>
parents:
48
diff
changeset
|
587 |
my $wrap = $conf->{'wrap'}; |
57df728cdb77
Alter the default wrap width for LDIF to expand to the terminal size,
Mahlon E. Smith <mahlon@laika.com>
parents:
48
diff
changeset
|
588 |
eval { |
57df728cdb77
Alter the default wrap width for LDIF to expand to the terminal size,
Mahlon E. Smith <mahlon@laika.com>
parents:
48
diff
changeset
|
589 |
my $rows; |
57df728cdb77
Alter the default wrap width for LDIF to expand to the terminal size,
Mahlon E. Smith <mahlon@laika.com>
parents:
48
diff
changeset
|
590 |
my $term = Term::ReadLine->new( 1 ); |
57df728cdb77
Alter the default wrap width for LDIF to expand to the terminal size,
Mahlon E. Smith <mahlon@laika.com>
parents:
48
diff
changeset
|
591 |
( $rows, $wrap ) = $term->get_screen_size() unless $wrap; |
57df728cdb77
Alter the default wrap width for LDIF to expand to the terminal size,
Mahlon E. Smith <mahlon@laika.com>
parents:
48
diff
changeset
|
592 |
}; |
57df728cdb77
Alter the default wrap width for LDIF to expand to the terminal size,
Mahlon E. Smith <mahlon@laika.com>
parents:
48
diff
changeset
|
593 |
$wrap ||= 78; |
57df728cdb77
Alter the default wrap width for LDIF to expand to the terminal size,
Mahlon E. Smith <mahlon@laika.com>
parents:
48
diff
changeset
|
594 |
|
0 | 595 |
# create tmpfile and link ldif object with it |
49
57df728cdb77
Alter the default wrap width for LDIF to expand to the terminal size,
Mahlon E. Smith <mahlon@laika.com>
parents:
48
diff
changeset
|
596 |
# |
0 | 597 |
if ( $use_temp ) { |
598 |
my ( undef, $fname ) = |
|
599 |
File::Temp::tempfile( 'shelldap_XXXXXXXX', DIR => '/tmp', UNLINK => 1 ); |
|
49
57df728cdb77
Alter the default wrap width for LDIF to expand to the terminal size,
Mahlon E. Smith <mahlon@laika.com>
parents:
48
diff
changeset
|
600 |
$self->{'ldif'} = Net::LDAP::LDIF->new( $fname, 'w', sort => 1, wrap => $wrap ); |
0 | 601 |
$self->{'ldif_fname'} = $fname; |
602 |
} |
|
603 |
||
604 |
# ldif -> stdout |
|
605 |
else { |
|
49
57df728cdb77
Alter the default wrap width for LDIF to expand to the terminal size,
Mahlon E. Smith <mahlon@laika.com>
parents:
48
diff
changeset
|
606 |
$self->{'ldif'} = Net::LDAP::LDIF->new( \*STDOUT, 'w', sort => 1, wrap => $wrap ); |
0 | 607 |
} |
608 |
||
609 |
return $self->{'ldif'}; |
|
610 |
} |
|
611 |
||
50 | 612 |
|
613 |
### Return an Entry object from an LDIF filename, or undef if there was an error. |
|
614 |
### |
|
0 | 615 |
sub load_ldif |
616 |
{ |
|
617 |
my $self = shift; |
|
50 | 618 |
my $ldif = Net::LDAP::LDIF->new( shift(), 'r' ); |
0 | 619 |
|
620 |
return unless $ldif; |
|
621 |
||
622 |
my $e; |
|
623 |
eval { $e = $ldif->read_entry(); }; |
|
624 |
||
625 |
return if $@; |
|
626 |
return $e; |
|
627 |
} |
|
628 |
||
50 | 629 |
|
630 |
### Given a filename, return an md5 checksum. |
|
631 |
### |
|
0 | 632 |
sub chksum |
633 |
{ |
|
634 |
my $self = shift; |
|
635 |
my $file = shift or return; |
|
636 |
||
637 |
my $md5 = Digest::MD5->new(); |
|
50 | 638 |
open F, $file or die "Unable to read file: $!\n"; |
0 | 639 |
my $hash = $md5->addfile( *F )->hexdigest(); |
640 |
close F; |
|
641 |
||
642 |
return $hash; |
|
643 |
} |
|
644 |
||
50 | 645 |
|
646 |
### Used by Term::Shell to generate the prompt. |
|
647 |
### |
|
0 | 648 |
sub prompt_str |
649 |
{ |
|
650 |
my $self = shift; |
|
651 |
return $self->{'prompt'}; |
|
652 |
} |
|
50 | 653 |
|
654 |
||
655 |
### Display the current working entry as the prompt, |
|
656 |
### truncating if necessary. |
|
657 |
### |
|
0 | 658 |
sub update_prompt |
659 |
{ |
|
660 |
my $self = shift; |
|
661 |
my $base = $self->base(); |
|
662 |
||
663 |
if ( length $base > 50 ) { |
|
664 |
my $cwd_dn = $1 if $base =~ /^(.*?),/; |
|
665 |
$self->{'prompt'} = "... $cwd_dn > "; |
|
666 |
} |
|
667 |
else { |
|
668 |
my $prompt = $base; |
|
669 |
$prompt =~ s/$conf->{'basedn'}/~/; |
|
670 |
$self->{'prompt'} = "$prompt > "; |
|
671 |
} |
|
672 |
return; |
|
673 |
} |
|
674 |
||
50 | 675 |
|
676 |
### Prompt the user to re-edit their LDIF on error. |
|
677 |
### Returns true if the user wants to do so. |
|
678 |
### |
|
679 |
sub prompt_edit_again |
|
680 |
{ |
|
681 |
my $self = shift; |
|
682 |
print "Edit again? [Yn]: "; |
|
683 |
chomp( my $ans = <STDIN> ); |
|
684 |
return $ans !~ /^n/i; |
|
685 |
} |
|
686 |
||
687 |
||
688 |
### Return the basedn of the LDAP connection, being either explicitly |
|
689 |
### configured or determined automatically from server metadata. |
|
690 |
### |
|
0 | 691 |
sub base |
692 |
{ |
|
693 |
my $self = shift; |
|
694 |
$self->{'base'} ||= $conf->{'basedn'}; |
|
695 |
||
696 |
# try and determine base automatically from rootDSE |
|
697 |
# |
|
698 |
unless ( $self->{'base'} ) { |
|
20
d956658803b8
use sane way to get a default basedn: RootDSe's namingContexts
Peter Marschall <peter@adpm.de>
parents:
19
diff
changeset
|
699 |
my @namingContexts = $self->{'root_dse'}->get_value('namingContexts'); |
d956658803b8
use sane way to get a default basedn: RootDSe's namingContexts
Peter Marschall <peter@adpm.de>
parents:
19
diff
changeset
|
700 |
$conf->{'basedn'} = $namingContexts[0]; |
d956658803b8
use sane way to get a default basedn: RootDSe's namingContexts
Peter Marschall <peter@adpm.de>
parents:
19
diff
changeset
|
701 |
$self->{'base'} = $namingContexts[0]; |
0 | 702 |
} |
23 | 703 |
if ( $_[0] ) { |
704 |
my $base = canonical_dn( $_[0], casefold => 'none' ); |
|
705 |
$self->{'base'} = $base if $base; |
|
0 | 706 |
} |
707 |
return $self->{'base'}; |
|
708 |
} |
|
709 |
||
50 | 710 |
|
711 |
### Returns true if the specified dn is valid on this LDAP server. |
|
712 |
### |
|
0 | 713 |
sub is_valid_dn |
714 |
{ |
|
715 |
my $self = shift; |
|
716 |
my $dn = shift or return 0; |
|
717 |
||
718 |
my $r = $self->search({ base => $dn }); |
|
16
a2e3faa3d2fc
use symbolic LDAP error codes instead of numbers
Peter Marschall <peter@adpm.de>
parents:
15
diff
changeset
|
719 |
return $r->{'code'} == LDAP_SUCCESS ? 1 : 0; |
0 | 720 |
} |
721 |
||
50 | 722 |
|
723 |
### Perform an LDAP search. |
|
724 |
### |
|
725 |
### Returns a hashref containing the return code and |
|
726 |
### an arrayref of Net::LDAP::Entry objects. |
|
727 |
### |
|
0 | 728 |
sub search |
729 |
{ |
|
730 |
my $self = shift; |
|
731 |
my $opts = shift || {}; |
|
732 |
||
733 |
$opts->{'base'} ||= $self->base(), |
|
734 |
$opts->{'filter'} ||= '(objectClass=*)'; |
|
735 |
$opts->{'scope'} ||= 'base'; |
|
736 |
||
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
737 |
my $search = sub { |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
738 |
return $self->ldap->search( |
43
b8836c9018fb
Attempt to retry the operation on failure.
Mahlon E. Smith <mahlon@laika.com>
parents:
42
diff
changeset
|
739 |
base => $opts->{'base'}, |
b8836c9018fb
Attempt to retry the operation on failure.
Mahlon E. Smith <mahlon@laika.com>
parents:
42
diff
changeset
|
740 |
filter => $opts->{'filter'}, |
b8836c9018fb
Attempt to retry the operation on failure.
Mahlon E. Smith <mahlon@laika.com>
parents:
42
diff
changeset
|
741 |
scope => $opts->{'scope'}, |
b8836c9018fb
Attempt to retry the operation on failure.
Mahlon E. Smith <mahlon@laika.com>
parents:
42
diff
changeset
|
742 |
timelimit => $conf->{'timeout'}, |
b8836c9018fb
Attempt to retry the operation on failure.
Mahlon E. Smith <mahlon@laika.com>
parents:
42
diff
changeset
|
743 |
typesonly => ! $opts->{'vals'}, |
b8836c9018fb
Attempt to retry the operation on failure.
Mahlon E. Smith <mahlon@laika.com>
parents:
42
diff
changeset
|
744 |
attrs => $opts->{'attrs'} || ['*'] |
b8836c9018fb
Attempt to retry the operation on failure.
Mahlon E. Smith <mahlon@laika.com>
parents:
42
diff
changeset
|
745 |
); |
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
746 |
}; |
43
b8836c9018fb
Attempt to retry the operation on failure.
Mahlon E. Smith <mahlon@laika.com>
parents:
42
diff
changeset
|
747 |
|
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
748 |
my $s = $self->with_retry( $search ); |
0 | 749 |
my $rv = { |
50 | 750 |
code => $s->code(), |
751 |
message => $s->error(), |
|
752 |
entries => [] |
|
0 | 753 |
}; |
754 |
||
755 |
$rv->{'entries'} = |
|
756 |
$opts->{'scope'} eq 'base' ? [ $s->shift_entry() ] : [ $s->entries() ]; |
|
757 |
||
758 |
return $rv; |
|
759 |
} |
|
760 |
||
50 | 761 |
|
762 |
### Maintain the cache of possible autocomplete values for |
|
763 |
### the current DN. |
|
764 |
### |
|
0 | 765 |
sub update_entries |
766 |
{ |
|
767 |
my $self = shift; |
|
768 |
my %opts = @_; |
|
769 |
my $base = lc( $self->base() ); |
|
770 |
||
50 | 771 |
my $s = $opts{'search'} || $self->search({ scope => 'one', base => $base }); |
0 | 772 |
|
773 |
$self->{'cwd_entries'} = []; |
|
774 |
return if $s->{'code'}; |
|
775 |
||
776 |
# setup cache object |
|
777 |
$self->{'cache'} ||= {}; |
|
778 |
$self->{'cache'}->{ $base } ||= {}; |
|
779 |
$self->{'cache'}->{ $base } = {} if $opts{'clearcache'}; |
|
780 |
my $cache = $self->{'cache'}->{ $base }; |
|
781 |
||
782 |
my $now = time(); |
|
783 |
if ( ! exists $cache->{'entries'} |
|
784 |
or $now - $cache->{'timestamp'} > $conf->{'cacheage'} ) |
|
785 |
{ |
|
786 |
$self->debug("Caching entries for $base\n"); |
|
787 |
foreach my $e ( @{ $s->{'entries'} } ) { |
|
788 |
my $dn = $e->dn(); |
|
789 |
my $rdn = $dn; |
|
790 |
$rdn =~ s/,$base//i; # remove base from display |
|
791 |
push @{ $self->{'cwd_entries'} }, $rdn; |
|
792 |
} |
|
793 |
$cache->{'timestamp'} = $now; |
|
794 |
$cache->{'entries'} = $self->{'cwd_entries'}; |
|
795 |
} |
|
796 |
else { |
|
797 |
$self->debug("Using cached lookups for $base\n"); |
|
798 |
} |
|
799 |
||
800 |
$self->{'cwd_entries'} = $cache->{'entries'}; |
|
801 |
return; |
|
802 |
} |
|
803 |
||
50 | 804 |
|
805 |
### Roughly convert a given path to a DN. |
|
806 |
### |
|
807 |
### Additionally support: |
|
808 |
### parent '..' |
|
809 |
### current '.' |
|
810 |
### last '-' |
|
811 |
### home '~' |
|
812 |
### |
|
813 |
### Synopsis: $dn = $self->path_to_dn( $path ); |
|
814 |
### |
|
30
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
815 |
sub path_to_dn |
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
816 |
{ |
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
817 |
my $self = shift; |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
818 |
my $path = shift; |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
819 |
my %flags = @_; |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
820 |
my $curbase = $self->base(); |
30
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
821 |
|
46
f0616455056d
Fix the uninitialized $path value errors I erroneously introduced on
Mahlon E. Smith <mahlon@laika.com>
parents:
44
diff
changeset
|
822 |
# support empty 'cd' or 'cd ~' going to root |
f0616455056d
Fix the uninitialized $path value errors I erroneously introduced on
Mahlon E. Smith <mahlon@laika.com>
parents:
44
diff
changeset
|
823 |
return $conf->{'basedn'} if ! $path || $path eq '~'; |
f0616455056d
Fix the uninitialized $path value errors I erroneously introduced on
Mahlon E. Smith <mahlon@laika.com>
parents:
44
diff
changeset
|
824 |
|
30
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
825 |
# return current base DN |
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
826 |
return $curbase if $path eq '.'; |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
827 |
|
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
828 |
# support 'cd -' |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
829 |
return $self->{'previous_base'} if $path eq '-'; |
30
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
830 |
|
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
831 |
# relative path, upwards |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
832 |
# |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
833 |
if ( $path =~ /^\.\./o ) { |
30
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
834 |
# support '..' (possibly iterated and as prefix to a DN) |
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
835 |
my @base = @{ ldap_explode_dn($curbase, casefold => 'none') }; |
30
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
836 |
|
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
837 |
# deal with leading .., |
50 | 838 |
# |
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
839 |
while ( $path =~ /^\.\./ ) { |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
840 |
shift( @base ) if @base; |
30
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
841 |
$path =~ s/^\.\.//; |
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
842 |
last if $path !~ /[,\/]\s*/; |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
843 |
$path =~ s/[,\/]\s*//; |
30
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
844 |
} |
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
845 |
|
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
846 |
# append the new dn to the node if one was specified: |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
847 |
# cd ../../cn=somewhere vs |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
848 |
# cd ../../ |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
849 |
# |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
850 |
my $newbase_root = canonical_dn( \@base, casefold => 'none' ); |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
851 |
$path = $path ? $path . ',' . $newbase_root : $newbase_root; |
30
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
852 |
} |
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
853 |
|
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
854 |
# attach the base if it isn't already there (this takes care of |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
855 |
# deeper relative nodes and absolutes) |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
856 |
# |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
857 |
else { |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
858 |
$path = "$path," . $curbase unless $path =~ /$curbase/; |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
859 |
} |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
860 |
|
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
861 |
return $path; |
30
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
862 |
} |
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
863 |
|
50 | 864 |
|
865 |
### Given an array ref of shell-like globs, |
|
866 |
### create and return a Net::LDAP::Filter object. |
|
867 |
### |
|
0 | 868 |
sub make_filter |
869 |
{ |
|
870 |
my $self = shift; |
|
871 |
my $globs = shift or return; |
|
872 |
||
873 |
return unless ref $globs eq 'ARRAY'; |
|
874 |
return unless scalar @$globs; |
|
875 |
||
876 |
my $filter; |
|
28
d42bd1b087a1
make_filter: cope with filters that are already parenthesized
Peter Marschall <peter@adpm.de>
parents:
27
diff
changeset
|
877 |
$filter = join('', map { (/^\(.*\)$/o) ? $_ : "($_)" } @$globs); |
d42bd1b087a1
make_filter: cope with filters that are already parenthesized
Peter Marschall <peter@adpm.de>
parents:
27
diff
changeset
|
878 |
$filter = '(|' . $filter . ')' if (scalar(@$globs) > 1); |
50 | 879 |
$filter = Net::LDAP::Filter->new( $filter ); |
0 | 880 |
|
881 |
if ( $filter ) { |
|
50 | 882 |
$self->debug( 'Filter parsed as: ' . $filter->as_string() . "\n" ); |
0 | 883 |
} |
884 |
else { |
|
885 |
print "Error parsing filter.\n"; |
|
886 |
return; |
|
887 |
} |
|
888 |
||
889 |
return $filter; |
|
890 |
} |
|
891 |
||
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
892 |
|
50 | 893 |
### Given an arrayref of objectClasses, pull a complete list of |
894 |
### required and optional attrbutes. Returns two arrayrefs. |
|
895 |
### |
|
896 |
sub fetch_attributes |
|
897 |
{ |
|
898 |
my $self = shift; |
|
899 |
my $ocs = shift or return [], []; |
|
900 |
||
901 |
my ( %seen, @must_attr, @may_attr ); |
|
902 |
foreach my $oc ( sort @{$ocs} ) { |
|
903 |
||
904 |
# required |
|
905 |
my @must = $self->{'schema'}->must( $oc ); |
|
906 |
foreach my $attr ( sort { $a->{'name'} cmp $b->{'name'} } @must ) { |
|
907 |
next if $attr->{'name'} =~ /^objectclass$/i; |
|
908 |
next if $seen{ $attr->{'name'} }; |
|
909 |
push @must_attr, $attr->{'name'}; |
|
910 |
$seen{ $attr->{'name'} }++; |
|
911 |
} |
|
912 |
||
913 |
# optional |
|
914 |
my @may = $self->{'schema'}->may( $oc ); |
|
915 |
foreach my $attr ( sort { $a->{'name'} cmp $b->{'name'} } @may ) { |
|
916 |
next if $attr->{'name'} =~ /^objectclass$/i; |
|
917 |
next if $seen{ $attr->{'name'} }; |
|
918 |
push @may_attr, $attr->{'name'}; |
|
919 |
$seen{ $attr->{'name'} }++; |
|
920 |
} |
|
921 |
} |
|
922 |
||
923 |
return \@must_attr, \@may_attr; |
|
924 |
} |
|
925 |
||
926 |
||
927 |
### Check whether a given string can be used directly as |
|
928 |
### an LDAP search filter. |
|
929 |
### |
|
930 |
### Synopsis: $yesNo = $self->is_valid_filter($string); |
|
931 |
### |
|
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
932 |
sub is_valid_filter |
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
933 |
{ |
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
934 |
my $self = shift; |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
935 |
my $filter = shift or return; |
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
936 |
|
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
937 |
return Net::LDAP::Filter->new( $filter ) ? 1 : 0; |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
938 |
} |
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
939 |
|
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
940 |
|
50 | 941 |
### Call code in subref $action, if there's any connection related errors, |
942 |
### try it one additional time before giving up. This should take care of |
|
943 |
### most server disconnects due to timeout and other generic connection |
|
944 |
### errors, and will attempt to transparently re-establish a connection. |
|
945 |
### |
|
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
946 |
sub with_retry |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
947 |
{ |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
948 |
my $self = shift; |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
949 |
my $action = shift; |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
950 |
|
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
951 |
my $rv = $action->(); |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
952 |
if ( $rv->code() == LDAP_OPERATIONS_ERROR || |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
953 |
$rv->code() == LDAP_TIMELIMIT_EXCEEDED || |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
954 |
$rv->code() == LDAP_BUSY || |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
955 |
$rv->code() == LDAP_UNAVAILABLE || |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
956 |
$rv->code() == LDAP_OTHER || |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
957 |
$rv->code() == LDAP_SERVER_DOWN || |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
958 |
$rv->code() == LDAP_TIMEOUT || |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
959 |
$rv->code() == LDAP_NO_MEMORY || |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
960 |
$rv->code() == LDAP_CONNECT_ERROR ) { |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
961 |
|
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
962 |
$self->debug( "Error ". $rv->code() . ", retrying.\n" ); |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
963 |
$self->{'ldap'} = undef; |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
964 |
$rv = $action->(); |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
965 |
} |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
966 |
|
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
967 |
return $rv; |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
968 |
} |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
969 |
|
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
970 |
|
50 | 971 |
### little. yellow. different. better. |
972 |
### |
|
0 | 973 |
sub debug |
974 |
{ |
|
975 |
my $self = shift; |
|
976 |
return unless $conf->{'debug'}; |
|
977 |
print "\e[33m"; |
|
978 |
print shift(); |
|
979 |
print "\e[0m"; |
|
980 |
return; |
|
981 |
} |
|
982 |
||
50 | 983 |
|
984 |
### Autocomplete values: Returns cached children entries. |
|
985 |
### |
|
0 | 986 |
sub autocomplete_cwd |
987 |
{ |
|
988 |
my $self = shift; |
|
989 |
my $word = $_[0]; |
|
990 |
||
991 |
return sort @{ $self->{'cwd_entries'} }; |
|
992 |
} |
|
993 |
||
50 | 994 |
|
995 |
### Autocomplete values: Returns previously set shelldap environment values. |
|
996 |
### |
|
0 | 997 |
sub comp_setenv |
998 |
{ |
|
999 |
my $self = shift; |
|
1000 |
return @{ $self->{'env'} }; |
|
1001 |
} |
|
1002 |
||
50 | 1003 |
|
1004 |
### Autocomplete values: Returns all objectClasses as defined |
|
1005 |
### by the LDAP server. |
|
1006 |
### |
|
0 | 1007 |
sub comp_create |
1008 |
{ |
|
1009 |
my $self = shift; |
|
50 | 1010 |
return @{ $self->{'objectclasses'} }; |
0 | 1011 |
} |
1012 |
||
50 | 1013 |
|
1014 |
### Inject various autocomplete and alias routines into the symbol table. |
|
1015 |
### |
|
0 | 1016 |
{ |
1017 |
no warnings; |
|
1018 |
no strict 'refs'; |
|
1019 |
||
1020 |
# command, alias |
|
1021 |
my %cmd_map = ( |
|
1022 |
whoami => 'id', |
|
1023 |
list => 'ls', |
|
1024 |
grep => 'search', |
|
1025 |
edit => 'vi', |
|
1026 |
delete => 'rm', |
|
1027 |
copy => 'cp', |
|
3
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
1028 |
cat => 'read', |
0 | 1029 |
move => 'mv', |
3
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
1030 |
cd => undef, |
0 | 1031 |
passwd => undef |
1032 |
); |
|
1033 |
||
1034 |
# setup autocompletes |
|
1035 |
foreach ( %cmd_map ) { |
|
1036 |
next unless $_; |
|
1037 |
my $sub = "comp_$_"; |
|
3
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
1038 |
*$sub = \&autocomplete_cwd; |
0 | 1039 |
} |
1040 |
*comp_touch = \&comp_create; |
|
1041 |
*comp_export = \&comp_setenv; |
|
1042 |
||
1043 |
# setup alias subs |
|
1044 |
# |
|
1045 |
# Term::Shell has an alias_* feature, but |
|
1046 |
# it seems to work about 90% of the time. |
|
1047 |
# that last 10% is something of a mystery. |
|
1048 |
# |
|
1049 |
$cmd_map{'create'} = 'touch'; |
|
1050 |
foreach my $cmd ( keys %cmd_map ) { |
|
1051 |
next unless defined $cmd_map{$cmd}; |
|
1052 |
my $alias_sub = 'run_' . $cmd_map{$cmd}; |
|
1053 |
my $real_sub = 'run_' . $cmd; |
|
1054 |
*$alias_sub = \&$real_sub; |
|
1055 |
} |
|
1056 |
} |
|
1057 |
||
6
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1058 |
|
50 | 1059 |
### Given an $arrayref, remove LDIF continuation wrapping in place, |
1060 |
### effectively making each entry a single line for LCS comparisons. |
|
1061 |
### |
|
1062 |
sub unwrap_line { |
|
1063 |
my $self = shift; |
|
5
78b2a48e07db
Combine multiple lines into a single one before displaying LDIF. Patch
Mahlon E. Smith <mahlon@laika.com>
parents:
4
diff
changeset
|
1064 |
my $array = shift; |
78b2a48e07db
Combine multiple lines into a single one before displaying LDIF. Patch
Mahlon E. Smith <mahlon@laika.com>
parents:
4
diff
changeset
|
1065 |
|
6
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1066 |
my $i = 1; |
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1067 |
while ( $i < scalar(@$array) ) { |
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1068 |
if ( $array->[$i] =~ /^\s/ ) { |
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1069 |
$array->[ $i - 1 ] =~ s/\n$//; |
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1070 |
$array->[ $i ] =~ s/^\s//; |
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1071 |
splice( @$array, $i - 1, 2, $array->[$i - 1] . $array->[$i] ); |
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1072 |
} |
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1073 |
else { |
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1074 |
$i++; |
5
78b2a48e07db
Combine multiple lines into a single one before displaying LDIF. Patch
Mahlon E. Smith <mahlon@laika.com>
parents:
4
diff
changeset
|
1075 |
} |
78b2a48e07db
Combine multiple lines into a single one before displaying LDIF. Patch
Mahlon E. Smith <mahlon@laika.com>
parents:
4
diff
changeset
|
1076 |
} |
78b2a48e07db
Combine multiple lines into a single one before displaying LDIF. Patch
Mahlon E. Smith <mahlon@laika.com>
parents:
4
diff
changeset
|
1077 |
} |
78b2a48e07db
Combine multiple lines into a single one before displaying LDIF. Patch
Mahlon E. Smith <mahlon@laika.com>
parents:
4
diff
changeset
|
1078 |
|
6
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1079 |
|
50 | 1080 |
######################################################################## |
1081 |
### S H E L L M E T H O D S |
|
1082 |
######################################################################## |
|
0 | 1083 |
|
50 | 1084 |
### Don't die on a newline, just no-op. |
1085 |
### |
|
0 | 1086 |
sub run_ { return; } |
1087 |
||
50 | 1088 |
|
1089 |
### Term::Shell hook. |
|
1090 |
### Write history for each command, print shell debug actions. |
|
1091 |
### |
|
0 | 1092 |
sub precmd |
1093 |
{ |
|
1094 |
my $self = shift; |
|
1095 |
my ( $handler, $cmd, $args ) = @_; |
|
1096 |
||
1097 |
my $term = $self->term(); |
|
1098 |
eval { $term->WriteHistory("$ENV{'HOME'}/.shelldap_history"); }; |
|
1099 |
||
1100 |
$self->debug( "$$cmd (" . ( join ' ', @$args ) . "), calling '$$handler'\n" ); |
|
1101 |
return; |
|
1102 |
} |
|
1103 |
||
50 | 1104 |
|
1105 |
### Display an entry as LDIF to the terminal. |
|
1106 |
### |
|
0 | 1107 |
sub run_cat |
1108 |
{ |
|
1109 |
my $self = shift; |
|
1110 |
my $dn = shift; |
|
21
cf8013cbfb58
slight cleanup: make more clear, it's an array
Peter Marschall <peter@adpm.de>
parents:
20
diff
changeset
|
1111 |
my @attrs = (@_) ? @_ : ('*'); |
0 | 1112 |
|
1113 |
unless ( $dn ) { |
|
1114 |
print "No dn provided.\n"; |
|
1115 |
return; |
|
1116 |
} |
|
1117 |
||
1118 |
# support '.' |
|
1119 |
$dn = $self->base() if $dn eq '.'; |
|
1120 |
||
1121 |
# support globbing |
|
50 | 1122 |
# |
0 | 1123 |
my $s; |
1124 |
if ( $dn eq '*' ) { |
|
1125 |
$s = $self->search({ |
|
1126 |
scope => 'one', |
|
1127 |
vals => 1, |
|
21
cf8013cbfb58
slight cleanup: make more clear, it's an array
Peter Marschall <peter@adpm.de>
parents:
20
diff
changeset
|
1128 |
attrs => \@attrs |
0 | 1129 |
}); |
1130 |
} |
|
1131 |
elsif ( $dn =~ /\*/ ) { |
|
1132 |
$s = $self->search({ |
|
1133 |
scope => 'one', |
|
1134 |
vals => 1, |
|
1135 |
filter => $dn, |
|
21
cf8013cbfb58
slight cleanup: make more clear, it's an array
Peter Marschall <peter@adpm.de>
parents:
20
diff
changeset
|
1136 |
attrs => \@attrs |
0 | 1137 |
}); |
1138 |
} |
|
50 | 1139 |
|
1140 |
# absolute/relative dn |
|
1141 |
# |
|
0 | 1142 |
else { |
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
1143 |
$dn = $self->path_to_dn( $dn ); |
0 | 1144 |
$s = $self->search({ |
1145 |
base => $dn, |
|
1146 |
vals => 1, |
|
21
cf8013cbfb58
slight cleanup: make more clear, it's an array
Peter Marschall <peter@adpm.de>
parents:
20
diff
changeset
|
1147 |
attrs => \@attrs |
0 | 1148 |
}); |
1149 |
} |
|
1150 |
||
50 | 1151 |
# emit error, if any |
1152 |
# |
|
0 | 1153 |
if ( $s->{'code'} ) { |
1154 |
print $s->{'message'} . "\n"; |
|
1155 |
return; |
|
1156 |
} |
|
1157 |
||
50 | 1158 |
# display to stdout |
1159 |
# |
|
0 | 1160 |
foreach my $e ( @{ $s->{'entries'} } ) { |
1161 |
$self->ldif->write_entry( $e ); |
|
1162 |
print "\n"; |
|
1163 |
} |
|
50 | 1164 |
|
0 | 1165 |
return; |
1166 |
} |
|
1167 |
||
50 | 1168 |
|
1169 |
### Change shelldap's idea of a current working 'directory', |
|
1170 |
### by adjusting the current default basedn for all searches. |
|
1171 |
### |
|
0 | 1172 |
sub run_cd |
1173 |
{ |
|
1174 |
my $self = shift; |
|
41
3e7c107f8b93
Take only second argument for run_{cd,edit,mkdir}
Salvatore Bonaccorso <carnil@debian.org>
parents:
40
diff
changeset
|
1175 |
my $newbase = shift; |
0 | 1176 |
|
30
e4b4b0968107
add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents:
29
diff
changeset
|
1177 |
# convert given path to a DN |
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
1178 |
$newbase = $self->path_to_dn( $newbase ); |
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
1179 |
|
0 | 1180 |
unless ( $self->is_valid_dn( $newbase ) ) { |
1181 |
print "No such object\n"; |
|
1182 |
return; |
|
1183 |
} |
|
1184 |
||
1185 |
# store old base |
|
1186 |
$self->{'previous_base'} = $self->base(); |
|
1187 |
||
1188 |
# update new base |
|
1189 |
$self->base( $newbase ); |
|
1190 |
||
1191 |
# get new 'cwd' listing |
|
26
68318d115f6c
fix attribute lists for LDAP queries
Peter Marschall <peter@adpm.de>
parents:
25
diff
changeset
|
1192 |
my $s = $self->search({ scope => 'one', attrs => [ '1.1' ] }); |
0 | 1193 |
if ( $s->{'code'} ) { |
1194 |
print "$s->{'message'}\n"; |
|
1195 |
return; |
|
1196 |
} |
|
1197 |
$self->update_entries( search => $s ); |
|
1198 |
||
1199 |
# reflect cwd change in prompt |
|
1200 |
$self->update_prompt(); |
|
1201 |
return; |
|
1202 |
} |
|
1203 |
||
50 | 1204 |
|
1205 |
### Simply clear the screen. |
|
1206 |
### |
|
0 | 1207 |
sub run_clear |
1208 |
{ |
|
1209 |
my $self = shift; |
|
50 | 1210 |
system( 'clear' ); |
0 | 1211 |
return; |
1212 |
} |
|
1213 |
||
50 | 1214 |
|
1215 |
### Fetch the source DN entry, modify it's DN data |
|
1216 |
### and write it back to the directory. |
|
1217 |
### |
|
0 | 1218 |
sub run_copy |
1219 |
{ |
|
1220 |
my $self = shift; |
|
1221 |
my ( $s_dn, $d_dn ) = @_; |
|
1222 |
||
1223 |
unless ( $s_dn ) { |
|
50 | 1224 |
print "No source DN provided.\n"; |
0 | 1225 |
return; |
1226 |
} |
|
1227 |
unless ( $d_dn ) { |
|
50 | 1228 |
print "No destination DN provided.\n"; |
0 | 1229 |
return; |
1230 |
} |
|
1231 |
||
31
a3a710f720dd
run_cat: convert to using path_to_dn()
Peter Marschall <peter@adpm.de>
parents:
30
diff
changeset
|
1232 |
# convert given source path to DN |
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
1233 |
$s_dn = $self->path_to_dn( $s_dn ); |
31
a3a710f720dd
run_cat: convert to using path_to_dn()
Peter Marschall <peter@adpm.de>
parents:
30
diff
changeset
|
1234 |
|
50 | 1235 |
# sanity check source |
1236 |
# |
|
0 | 1237 |
my $s = $self->search({ base => $s_dn, vals => 1 }); |
16
a2e3faa3d2fc
use symbolic LDAP error codes instead of numbers
Peter Marschall <peter@adpm.de>
parents:
15
diff
changeset
|
1238 |
unless ( $s->{'code'} == LDAP_SUCCESS ) { |
0 | 1239 |
print "No such object\n"; |
1240 |
return; |
|
1241 |
} |
|
1242 |
||
50 | 1243 |
# see if we're copying the entry to a nonexistent path |
1244 |
# |
|
0 | 1245 |
my ( $new_dn, $old_dn ); |
1246 |
( $d_dn, $new_dn ) = ( $1, $2 ) if $d_dn =~ /^([\w=]+),(.*)$/; |
|
50 | 1247 |
if ( $new_dn ) { # absolute |
0 | 1248 |
unless ( $self->is_valid_dn( $new_dn ) ) { |
1249 |
print "Invalid destination.\n"; |
|
1250 |
return; |
|
1251 |
} |
|
1252 |
} |
|
50 | 1253 |
else { # relative |
0 | 1254 |
$new_dn = $self->base(); |
1255 |
} |
|
1256 |
$old_dn = $1 if $s_dn =~ /^[\w=]+,(.*)$/; |
|
1257 |
||
50 | 1258 |
# get the source entry object |
0 | 1259 |
my $e = ${ $s->{'entries'} }[0]; |
1260 |
$e->dn( $s_dn ); |
|
1261 |
||
1262 |
# add changes in new entry instead of modifying existing |
|
50 | 1263 |
$e->changetype( 'add' ); |
0 | 1264 |
$e->dn( "$d_dn,$new_dn" ); |
1265 |
||
1266 |
# get the unique attribute from the dn for modification |
|
1267 |
# perhaps there is a better way to do this...? |
|
1268 |
# |
|
1269 |
my ( $uniqkey, $uniqval ) = ( $1, $2 ) |
|
14
b8fae8fb7942
Allow '-' on RDN name when copying
Giacomo Tenaglia <Giacomo.Tenaglia@cern.ch>
parents:
12
diff
changeset
|
1270 |
if $d_dn =~ /^([\.\w\-]+)(?:\s+)?=(?:\s+)?([\.\-\s\w]+),?/; |
0 | 1271 |
unless ( $uniqkey && $uniqval ) { |
50 | 1272 |
print "Unable to parse unique values from RDN.\n"; |
0 | 1273 |
return; |
1274 |
} |
|
1275 |
$e->replace( $uniqkey => $uniqval ); |
|
1276 |
||
50 | 1277 |
# update (which will actually create the new entry) |
1278 |
# |
|
1279 |
my $update = sub { return $e->update($self->ldap()) }; |
|
1280 |
my $rv = $self->with_retry( $update ); |
|
1281 |
print $rv->error(), "\n"; |
|
0 | 1282 |
|
1283 |
# clear caches |
|
50 | 1284 |
# |
0 | 1285 |
$self->{'cache'}->{ $new_dn } = {} if $new_dn; |
1286 |
$self->{'cache'}->{ $old_dn } = {} if $old_dn; |
|
1287 |
$self->update_entries( clearcache => 1 ); |
|
1288 |
return; |
|
1289 |
} |
|
1290 |
||
50 | 1291 |
|
1292 |
### Create a new entry from scratch, using attributes from |
|
1293 |
### what the server's schema says is available from the specified |
|
1294 |
### (optional) objectClass list. Populate a new LDIF file and |
|
1295 |
### present an editor to the user. |
|
1296 |
### |
|
0 | 1297 |
sub run_create |
1298 |
{ |
|
1299 |
my $self = shift; |
|
1300 |
my @ocs = @_; |
|
1301 |
||
50 | 1302 |
# manually generate some boilerplate LDIF. |
0 | 1303 |
# |
50 | 1304 |
unless ( $self->{'create_file'} ) { |
1305 |
my $fh; |
|
1306 |
||
1307 |
( $fh, $self->{'create_file'} ) = |
|
1308 |
File::Temp::tempfile( 'shelldap_XXXXXXXX', DIR => '/tmp', UNLINK => 1 ); |
|
0 | 1309 |
|
50 | 1310 |
# first print out the dn and object classes. |
1311 |
# |
|
1312 |
print $fh 'dn: ???,', $self->base(), "\n"; |
|
1313 |
foreach my $oc ( sort @ocs ) { |
|
1314 |
print $fh "objectClass: $oc\n"; |
|
0 | 1315 |
} |
1316 |
||
50 | 1317 |
# gather and print attributes for requested objectClasses |
1318 |
# |
|
1319 |
my ( $must_attr, $may_attr ) = $self->fetch_attributes( \@ocs ); |
|
1320 |
print $fh "$_: \n" foreach @{ $must_attr }; |
|
1321 |
print $fh "# $_: \n" foreach @{ $may_attr }; |
|
1322 |
close $fh; |
|
0 | 1323 |
} |
1324 |
||
50 | 1325 |
# checksum the file. |
1326 |
# |
|
1327 |
my $hash_orig = $self->chksum( $self->{'create_file'} ); |
|
1328 |
system( $self->{'editor'}, $self->{'create_file'} ) && die "Unable to launch editor: $!\n"; |
|
0 | 1329 |
|
50 | 1330 |
# detect a total lack of change |
1331 |
# |
|
1332 |
if ( $hash_orig eq $self->chksum($self->{'create_file'}) ) { |
|
0 | 1333 |
print "Entry not modified.\n"; |
50 | 1334 |
unlink $self->{'create_file'}; |
1335 |
$self->{'create_file'} = undef; |
|
0 | 1336 |
return; |
1337 |
} |
|
1338 |
||
1339 |
# load in LDIF |
|
50 | 1340 |
# |
1341 |
my $ldif = Net::LDAP::LDIF->new( $self->{'create_file'}, 'r', onerror => 'warn' ); |
|
1342 |
my $e = $ldif->read_entry(); |
|
0 | 1343 |
unless ( $e ) { |
1344 |
print "Unable to parse LDIF.\n"; |
|
50 | 1345 |
unlink $self->{'create_file'}; |
1346 |
$self->{'create_file'} = undef; |
|
0 | 1347 |
return; |
1348 |
} |
|
50 | 1349 |
|
1350 |
# create the new entry. |
|
1351 |
# |
|
0 | 1352 |
$e->changetype('add'); |
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1353 |
my $create = sub { return $e->update($self->ldap()) }; |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1354 |
my $rv = $self->with_retry( $create ); |
0 | 1355 |
print $rv->error(), "\n"; |
1356 |
||
50 | 1357 |
if ( $rv->code() != LDAP_SUCCESS && $self->prompt_edit_again() ) { |
1358 |
return $self->run_create(); |
|
1359 |
} |
|
0 | 1360 |
|
50 | 1361 |
$self->update_entries( clearcache => 1 ); |
1362 |
unlink $self->{'create_file'}; |
|
1363 |
$self->{'create_file'} = undef; |
|
0 | 1364 |
return; |
1365 |
} |
|
1366 |
||
50 | 1367 |
|
1368 |
### Remove an entry (or entries) from the LDAP directory. |
|
1369 |
### |
|
0 | 1370 |
sub run_delete |
1371 |
{ |
|
1372 |
my $self = shift; |
|
1373 |
my @DNs = @_; |
|
1374 |
||
1375 |
unless ( scalar @DNs ) { |
|
1376 |
print "No dn specified.\n"; |
|
1377 |
return; |
|
1378 |
} |
|
1379 |
||
6
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1380 |
my $filter; |
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1381 |
unless ( $DNs[0] eq '*' ) { |
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1382 |
$filter = $self->make_filter( \@DNs ) or return; |
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1383 |
} |
0 | 1384 |
|
1385 |
my $s = $self->search({ scope => 'one', filter => $filter }); |
|
50 | 1386 |
unless ( $s->{'code'} == LDAP_SUCCESS ) { |
0 | 1387 |
print "$s->{'message'}\n"; |
1388 |
return; |
|
1389 |
} |
|
1390 |
||
50 | 1391 |
print "Are you sure? [Ny]: "; |
0 | 1392 |
chomp( my $resp = <STDIN> ); |
1393 |
return unless $resp =~ /^y/i; |
|
1394 |
||
1395 |
foreach my $e ( @{ $s->{'entries'} } ) { |
|
1396 |
my $dn = $e->dn(); |
|
1397 |
my $rv = $self->ldap->delete( $dn ); |
|
1398 |
print "$dn: ", $rv->error(), "\n"; |
|
1399 |
} |
|
1400 |
||
1401 |
$self->update_entries( clearcache => 1 ); |
|
1402 |
return; |
|
1403 |
} |
|
1404 |
||
50 | 1405 |
|
1406 |
### Fetch an entry from the directory, write it out to disk |
|
1407 |
### as LDIF, launch an editor, then compare changes and write |
|
1408 |
### it back to the directory. |
|
1409 |
### |
|
0 | 1410 |
sub run_edit |
1411 |
{ |
|
1412 |
my $self = shift; |
|
41
3e7c107f8b93
Take only second argument for run_{cd,edit,mkdir}
Salvatore Bonaccorso <carnil@debian.org>
parents:
40
diff
changeset
|
1413 |
my $dn = shift; |
0 | 1414 |
|
1415 |
unless ( $dn ) { |
|
1416 |
print "No dn provided.\n"; |
|
1417 |
return; |
|
1418 |
} |
|
1419 |
||
31
a3a710f720dd
run_cat: convert to using path_to_dn()
Peter Marschall <peter@adpm.de>
parents:
30
diff
changeset
|
1420 |
# convert given path to DN |
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
1421 |
$dn = $self->path_to_dn( $dn ); |
0 | 1422 |
|
50 | 1423 |
# sanity check |
1424 |
# |
|
0 | 1425 |
my $s = $self->search({ base => $dn, vals => 1 }); |
50 | 1426 |
unless ( $s->{'code'} == LDAP_SUCCESS ) { |
0 | 1427 |
print $s->{'message'} . "\n"; |
1428 |
return; |
|
1429 |
} |
|
1430 |
||
50 | 1431 |
# fetch entry. |
0 | 1432 |
my $e = ${ $s->{'entries'} }[0]; |
50 | 1433 |
$e->changetype( 'modify' ); |
1434 |
||
1435 |
# write it out to disk. |
|
1436 |
# |
|
1437 |
unless( $self->{'edit_again'} ) { |
|
1438 |
my $ldif = $self->ldif(1); |
|
1439 |
$ldif->write_entry( $e ); |
|
1440 |
$ldif->done(); # force sync |
|
1441 |
} |
|
0 | 1442 |
|
1443 |
# load it into an array for potential comparison |
|
1444 |
open LDIF, "$self->{'ldif_fname'}" or return; |
|
6
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1445 |
my @orig_ldif = <LDIF>; |
0 | 1446 |
close LDIF; |
1447 |
||
50 | 1448 |
# append optional, unused attributes as comments for fast reference. |
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1449 |
# |
50 | 1450 |
unless ( $self->{'edit_again'} ) { |
1451 |
my %current_attrs = map { $_ => 1 } $e->attributes(); |
|
1452 |
my ( $must_attr, $may_attr ) = $self->fetch_attributes( $e->get_value('objectClass', asref => 1) ); |
|
1453 |
||
1454 |
open LDIF, ">> $self->{'ldif_fname'}"; |
|
1455 |
foreach my $opt_attr ( sort { $a cmp $b } @{$may_attr} ) { |
|
1456 |
next if $current_attrs{ $opt_attr }; |
|
1457 |
print LDIF "# " . $opt_attr . ":\n"; |
|
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1458 |
} |
50 | 1459 |
close LDIF; |
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1460 |
} |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1461 |
|
0 | 1462 |
# checksum it, then open it in an editor |
50 | 1463 |
# |
1464 |
my $hash_orig = $self->chksum( $self->{'ldif_fname'} ); |
|
1465 |
system( $self->{'editor'}, $self->{'ldif_fname'} ) && |
|
0 | 1466 |
die "Unable to launch editor: $!\n"; |
1467 |
||
1468 |
# detect a total lack of change |
|
50 | 1469 |
# |
1470 |
if ( $hash_orig eq $self->chksum($self->{'ldif_fname'}) ) { |
|
0 | 1471 |
print "Entry not modified.\n"; |
1472 |
unlink $self->{'ldif_fname'}; |
|
1473 |
return; |
|
1474 |
} |
|
1475 |
||
1476 |
# check changes for basic LDIF validity |
|
50 | 1477 |
# |
1478 |
while( ! $self->load_ldif($self->{'ldif_fname'}) ) { |
|
0 | 1479 |
print "Unable to parse LDIF.\n"; |
50 | 1480 |
if ( $self->prompt_edit_again() ) { |
1481 |
system( $self->{'editor'}, $self->{'ldif_fname'} ); |
|
1482 |
} |
|
1483 |
else { |
|
1484 |
unlink $self->{'ldif_fname'}; |
|
1485 |
return; |
|
1486 |
} |
|
0 | 1487 |
} |
1488 |
||
1489 |
# load changes into a new array for comparison |
|
50 | 1490 |
# |
0 | 1491 |
open LDIF, "$self->{'ldif_fname'}" or return; |
6
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1492 |
my @new_ldif = <LDIF>; |
0 | 1493 |
close LDIF; |
1494 |
||
50 | 1495 |
# parser subref |
1496 |
# |
|
0 | 1497 |
my $parse = sub { |
1498 |
my $line = shift || $_; |
|
1499 |
return if $line =~ /^\#/; # ignore comments |
|
1500 |
my ( $attr, $val ) = ( $1, $2 ) if $line =~ /^(.+?): (.*)$/; |
|
6
46dfe9d6f368
Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents:
5
diff
changeset
|
1501 |
return unless $attr; |
0 | 1502 |
return if index($attr, ':') != -1; # ignore base64 |
1503 |
return ( $attr, $val ); |
|
1504 |
}; |
|
1505 |
||
50 | 1506 |
$self->unwrap_line( \@orig_ldif ); |
1507 |
$self->unwrap_line( \@new_ldif ); |
|
5
78b2a48e07db
Combine multiple lines into a single one before displaying LDIF. Patch
Mahlon E. Smith <mahlon@laika.com>
parents:
4
diff
changeset
|
1508 |
|
0 | 1509 |
my $diff = Algorithm::Diff->new( \@orig_ldif, \@new_ldif ); |
1510 |
HUNK: |
|
1511 |
while ( $diff->Next() ) { |
|
1512 |
next if $diff->Same(); |
|
1513 |
my $diff_bit = $diff->Diff(); |
|
1514 |
my %seen_attr; |
|
1515 |
||
50 | 1516 |
# attr removals |
1517 |
# |
|
0 | 1518 |
if ( $diff_bit == 1 ) { |
1519 |
foreach ( $diff->Items(1) ) { |
|
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1520 |
my ( $attr, $val ) = $parse->( $_ ) or next; |
0 | 1521 |
$self->debug("DELETE: $_"); |
1522 |
$e->delete( $attr => [ $val ] ); |
|
1523 |
} |
|
1524 |
} |
|
1525 |
||
50 | 1526 |
# attr insertions |
1527 |
# |
|
0 | 1528 |
if ( $diff_bit == 2 ) { |
1529 |
foreach ( $diff->Items(2) ) { |
|
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1530 |
my ( $attr, $val ) = $parse->( $_ ) or next; |
0 | 1531 |
$self->debug("INSERT: $_"); |
1532 |
$e->add( $attr => $val ); |
|
1533 |
} |
|
1534 |
} |
|
1535 |
||
50 | 1536 |
# attr change |
1537 |
# |
|
0 | 1538 |
if ( $diff_bit == 3 ) { |
1539 |
foreach ( $diff->Items(2) ) { |
|
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1540 |
my ( $attr, $val ) = $parse->( $_ ) or next; |
0 | 1541 |
$self->debug("MODIFY: $_"); |
1542 |
||
1543 |
my $cur_vals = $e->get_value( $attr, asref => 1 ) || []; |
|
1544 |
my $cur_valcount = scalar @$cur_vals; |
|
1545 |
next if $cur_valcount == 0; # should have been an 'add' |
|
1546 |
||
1547 |
# replace immediately |
|
1548 |
# |
|
1549 |
if ( $cur_valcount == 1 ) { |
|
1550 |
$e->replace( $attr => $val ); |
|
1551 |
} |
|
1552 |
else { |
|
1553 |
||
50 | 1554 |
# retain attributes that allow multiples, so updating |
1555 |
# one attribute doesn't inadvertently remove others with |
|
1556 |
# the same name. |
|
0 | 1557 |
# |
1558 |
next if $seen_attr{ $attr }; |
|
1559 |
my @new_vals; |
|
1560 |
foreach my $line ( @new_ldif ) { |
|
1561 |
my ( $new_attr, $new_val ) = $parse->( $line ) or next; |
|
1562 |
next unless $new_attr eq $attr; |
|
1563 |
$seen_attr{ $attr }++; |
|
1564 |
push @new_vals, $new_val; |
|
1565 |
} |
|
50 | 1566 |
|
0 | 1567 |
$e->replace( $attr => \@new_vals ); |
1568 |
} |
|
1569 |
} |
|
1570 |
} |
|
1571 |
} |
|
1572 |
||
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1573 |
my $update = sub { return $e->update( $self->ldap ); }; |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1574 |
my $rv = $self->with_retry( $update ); |
0 | 1575 |
print $rv->error(), "\n"; |
1576 |
||
50 | 1577 |
if ( $rv->code() != LDAP_SUCCESS && $self->prompt_edit_again() ) { |
1578 |
$self->{'edit_again'} = 1; |
|
1579 |
return $self->run_edit( $dn ); |
|
1580 |
} |
|
1581 |
||
1582 |
unlink $self->{'ldif_fname'}; |
|
1583 |
$self->{'edit_again'} = undef; |
|
0 | 1584 |
return; |
1585 |
} |
|
1586 |
||
50 | 1587 |
|
1588 |
### Display current tunable runtime settings. |
|
1589 |
### |
|
0 | 1590 |
sub run_env |
1591 |
{ |
|
1592 |
my $self = shift; |
|
1593 |
||
1594 |
foreach ( sort @{ $self->{'env'} } ) { |
|
1595 |
print "$_: "; |
|
1596 |
print $conf->{$_} ? $conf->{$_} : 0; |
|
1597 |
print "\n" |
|
1598 |
} |
|
1599 |
} |
|
1600 |
||
50 | 1601 |
|
1602 |
### Alter settings. |
|
1603 |
### |
|
1604 |
sub run_setenv |
|
1605 |
{ |
|
1606 |
my $self = shift; |
|
1607 |
my ( $key, $val ) = @_; |
|
1608 |
||
1609 |
( $key, $val ) = split /=/, $key if $key && ! defined $val; |
|
1610 |
return unless $key && defined $val; |
|
1611 |
$key = lc $key; |
|
1612 |
||
1613 |
$conf->{$key} = $val; |
|
1614 |
return; |
|
1615 |
} |
|
1616 |
||
1617 |
||
1618 |
### Search across the directory and display matching entries. |
|
1619 |
### |
|
0 | 1620 |
sub run_grep |
1621 |
{ |
|
1622 |
my $self = shift; |
|
1623 |
my ( $recurse, $filter, $base ) = @_; |
|
1624 |
||
1625 |
# set 'recursion' |
|
1626 |
unless ( $recurse && $recurse =~ /\-r|recurse/ ) { |
|
1627 |
# shift args to the left |
|
1628 |
( $recurse, $filter, $base ) = ( undef, $recurse, $filter ); |
|
1629 |
} |
|
1630 |
||
1631 |
$filter = Net::LDAP::Filter->new( $filter ); |
|
1632 |
unless ( $filter ) { |
|
1633 |
print "Invalid search filter.\n"; |
|
1634 |
return; |
|
1635 |
} |
|
1636 |
||
1637 |
# support '*' |
|
1638 |
$base = $self->base() if ! $base or $base eq '*'; |
|
1639 |
||
1640 |
unless ( $base ) { |
|
1641 |
print "No search base specified.\n"; |
|
1642 |
return; |
|
1643 |
} |
|
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
1644 |
|
31
a3a710f720dd
run_cat: convert to using path_to_dn()
Peter Marschall <peter@adpm.de>
parents:
30
diff
changeset
|
1645 |
# convert base path to DN |
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
1646 |
$base = $self->path_to_dn( $base ); |
0 | 1647 |
|
1648 |
$self->debug("Filter parsed as: " . $filter->as_string() . "\n"); |
|
1649 |
||
50 | 1650 |
my $s = $self->search({ |
1651 |
scope => $recurse ? 'sub' : 'one', |
|
1652 |
base => $base, |
|
1653 |
filter => $filter |
|
1654 |
}); |
|
0 | 1655 |
|
1656 |
foreach my $e ( @{ $s->{'entries'} } ) { |
|
1657 |
my $dn = $e->dn(); |
|
1658 |
print "$dn\n"; |
|
1659 |
} |
|
1660 |
||
1661 |
return; |
|
1662 |
} |
|
1663 |
||
50 | 1664 |
|
1665 |
### Override internal help function with pod2usage output. |
|
1666 |
### |
|
0 | 1667 |
sub run_help |
1668 |
{ |
|
1669 |
return Pod::Usage::pod2usage( |
|
1670 |
-exitval => 'NOEXIT', |
|
1671 |
-verbose => 99, |
|
1672 |
-sections => 'SHELL COMMANDS' |
|
1673 |
); |
|
1674 |
} |
|
1675 |
||
50 | 1676 |
|
1677 |
### Generate and display a list of LDAP entries, relative to the current |
|
1678 |
### location the command was run from. |
|
1679 |
### |
|
0 | 1680 |
sub run_list |
1681 |
{ |
|
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1682 |
my $self = shift; |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1683 |
my @args = @_; |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1684 |
my @attrs = (); |
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1685 |
my $filter; |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1686 |
|
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1687 |
# flag booleans |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1688 |
my ( $recurse, $long ); |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1689 |
|
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1690 |
# parse arguments: [ <option> ...] [<filter> ...] [<attribute> ...] |
50 | 1691 |
# |
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1692 |
if ( scalar @args ) { |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1693 |
# options: support '-l' or '-R' listings |
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1694 |
if ( $args[0] =~ /^\-(\w+)/o ) { |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1695 |
my $flags = $1; |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1696 |
$recurse = $flags =~ /R/; |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1697 |
$long = $flags =~ /l/; |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1698 |
shift( @args ); |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1699 |
} |
0 | 1700 |
|
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1701 |
my @filters; |
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1702 |
|
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1703 |
# get filter elements from argument list |
50 | 1704 |
# |
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1705 |
while ( @args && $self->is_valid_filter($args[0]) ) { |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1706 |
push( @filters, shift(@args) ); |
0 | 1707 |
} |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1708 |
|
50 | 1709 |
# No filter for display? Default to all entries. |
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1710 |
push( @filters, '(objectClass=*)' ) unless scalar @filters; |
0 | 1711 |
|
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1712 |
# construct OR'ed filter from filter elements |
0 | 1713 |
$filter = $self->make_filter( \@filters ); |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1714 |
|
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1715 |
# remaining arguments must be attributes |
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1716 |
push( @attrs, @args ); |
0 | 1717 |
} |
1718 |
||
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1719 |
# Get all attributes if none are specified, and we're in long-list mode. |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1720 |
push( @attrs, '*' ) if $long && ! scalar @attrs; |
0 | 1721 |
|
4
5a65bc849363
Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents:
3
diff
changeset
|
1722 |
my $s = $self->search({ |
5a65bc849363
Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents:
3
diff
changeset
|
1723 |
scope => $recurse ? 'sub' : 'one', |
5a65bc849363
Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents:
3
diff
changeset
|
1724 |
vals => 1, |
5a65bc849363
Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents:
3
diff
changeset
|
1725 |
filter => $filter, |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1726 |
attrs => [ @attrs, 'hasSubordinates' ] |
4
5a65bc849363
Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents:
3
diff
changeset
|
1727 |
}); |
0 | 1728 |
if ( $s->{'code'} ) { |
1729 |
print "$s->{'message'}\n"; |
|
1730 |
return; |
|
1731 |
} |
|
1732 |
||
1733 |
# if an entry doesn't have a description field, |
|
1734 |
# try and show some nice defaults for ls -l ! |
|
1735 |
# |
|
1736 |
# objectClass -> Attribute to show |
|
1737 |
# |
|
1738 |
my %descs = %{ |
|
1739 |
$conf->{'descmaps'} |
|
1740 |
|| { |
|
1741 |
posixAccount => 'gecos', |
|
1742 |
posixGroup => 'gidNumber', |
|
4
5a65bc849363
Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents:
3
diff
changeset
|
1743 |
ipHost => 'ipHostNumber', |
0 | 1744 |
} |
1745 |
}; |
|
1746 |
||
1747 |
# iterate and print |
|
1748 |
# |
|
1749 |
my $dn_count = 0; |
|
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1750 |
my $base = $self->base(); |
0 | 1751 |
foreach my $e ( sort { $a->dn() cmp $b->dn() } @{ $s->{'entries'} } ) { |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1752 |
my $dn = $e->dn(); |
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1753 |
next if lc( $dn ) eq lc( $base ); |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1754 |
|
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1755 |
if ( ! $long ) { |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1756 |
# strip the current base from the dn, if we're recursing and not in long mode |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1757 |
if ( $recurse ) { |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1758 |
$dn =~ s/,$base//oi; |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1759 |
} |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1760 |
|
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1761 |
# only show RDN unless -l was given |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1762 |
else { |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1763 |
$dn = canonical_dn( [shift(@{ldap_explode_dn($dn, casefold => 'none')})], casefold => 'none' ) |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1764 |
} |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1765 |
} |
4
5a65bc849363
Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents:
3
diff
changeset
|
1766 |
|
5a65bc849363
Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents:
3
diff
changeset
|
1767 |
# if this entry is a container for other entries, append a |
5a65bc849363
Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents:
3
diff
changeset
|
1768 |
# trailing slash. |
35
3e5572aeee55
Make sure the hasSubordinates attribute is defined before checking its
Mahlon E. Smith <mahlon@martini.nu>
parents:
34
diff
changeset
|
1769 |
$dn .= '/' if $e->get_value('hasSubordinates') && |
3e5572aeee55
Make sure the hasSubordinates attribute is defined before checking its
Mahlon E. Smith <mahlon@martini.nu>
parents:
34
diff
changeset
|
1770 |
$e->get_value('hasSubordinates') eq 'TRUE'; |
0 | 1771 |
|
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1772 |
# additional arguments/attributes were given; show their values |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1773 |
# |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1774 |
if ( scalar @args ) { |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1775 |
my @elements = ( $dn ); |
0 | 1776 |
|
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1777 |
foreach my $attr ( @args ) { |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1778 |
my @vals = $e->get_value( $attr ); |
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1779 |
push( @elements, join(',', @vals) ); |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1780 |
} |
0 | 1781 |
|
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1782 |
print join( "\t", @elements )."\n"; |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1783 |
} |
50 | 1784 |
|
1785 |
# show descriptions |
|
1786 |
# |
|
0 | 1787 |
else { |
34
40c3719c87d4
fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents:
33
diff
changeset
|
1788 |
my $desc = $e->get_value( 'description' ); |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1789 |
if ( $desc ) { |
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1790 |
$desc =~ s/\n.*//s; # 1st line only |
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1791 |
$dn .= " ($desc)"; |
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1792 |
} |
0 | 1793 |
|
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1794 |
# no desc? Try and infer something useful |
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1795 |
# to display. |
50 | 1796 |
# |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1797 |
else { |
0 | 1798 |
|
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1799 |
# pull objectClasses, hash for lookup speed |
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1800 |
my @oc = $e->get_value( 'objectClass' ); |
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1801 |
my %ochash; |
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1802 |
map { $ochash{$_} = 1 } @oc; |
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1803 |
|
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1804 |
foreach my $d_listing ( sort keys %descs ) { |
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1805 |
if ( exists $ochash{ $d_listing } ) { |
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1806 |
my $str = $e->get_value( $descs{ $d_listing }, asref => 1 ); |
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1807 |
$dn .= ' (' . (join ', ', @$str) . ')' if $str && scalar @$str; |
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1808 |
} |
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1809 |
next; |
0 | 1810 |
} |
1811 |
} |
|
27
7d170d1bc17b
run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents:
26
diff
changeset
|
1812 |
print "$dn\n"; |
0 | 1813 |
} |
1814 |
$dn_count++; |
|
1815 |
} |
|
1816 |
||
1817 |
print "\n$dn_count " . |
|
1818 |
( $dn_count == 1 ? 'object.' : 'objects.') . |
|
1819 |
"\n" if $long; |
|
1820 |
return; |
|
1821 |
} |
|
1822 |
||
50 | 1823 |
|
1824 |
### Create a new organizationalUnit entry. |
|
1825 |
### |
|
0 | 1826 |
sub run_mkdir |
1827 |
{ |
|
1828 |
my $self = shift; |
|
41
3e7c107f8b93
Take only second argument for run_{cd,edit,mkdir}
Salvatore Bonaccorso <carnil@debian.org>
parents:
40
diff
changeset
|
1829 |
my $dir = shift; |
0 | 1830 |
|
1831 |
unless ( $dir ) { |
|
1832 |
print "No 'directory' provided.\n"; |
|
1833 |
return; |
|
1834 |
} |
|
1835 |
||
38
12f279ef4f9d
Backout the additional objectClasses patch for mkdir: same behavior can
Mahlon E. Smith <mahlon@martini.nu>
parents:
37
diff
changeset
|
1836 |
# normalize name, if it is not yet a legal DN |
12f279ef4f9d
Backout the additional objectClasses patch for mkdir: same behavior can
Mahlon E. Smith <mahlon@martini.nu>
parents:
37
diff
changeset
|
1837 |
$dir = 'ou=' . $dir unless canonical_dn( $dir ); |
0 | 1838 |
|
38
12f279ef4f9d
Backout the additional objectClasses patch for mkdir: same behavior can
Mahlon E. Smith <mahlon@martini.nu>
parents:
37
diff
changeset
|
1839 |
# convert given path to full DN |
12f279ef4f9d
Backout the additional objectClasses patch for mkdir: same behavior can
Mahlon E. Smith <mahlon@martini.nu>
parents:
37
diff
changeset
|
1840 |
$dir = $self->path_to_dn( $dir ); |
29
bd95c3aea253
mkdir: support more objectclasses
Peter Marschall <peter@adpm.de>
parents:
28
diff
changeset
|
1841 |
|
bd95c3aea253
mkdir: support more objectclasses
Peter Marschall <peter@adpm.de>
parents:
28
diff
changeset
|
1842 |
# get RDN: naming attributes (lower-case) and their values |
38
12f279ef4f9d
Backout the additional objectClasses patch for mkdir: same behavior can
Mahlon E. Smith <mahlon@martini.nu>
parents:
37
diff
changeset
|
1843 |
my %rdn = %{ shift(@{ ldap_explode_dn($dir, casefold => 'lower') }) }; |
0 | 1844 |
|
38
12f279ef4f9d
Backout the additional objectClasses patch for mkdir: same behavior can
Mahlon E. Smith <mahlon@martini.nu>
parents:
37
diff
changeset
|
1845 |
# add |
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1846 |
my $mkdir = sub { |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1847 |
return $self->ldap()->add( $dir, attr => [ |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1848 |
objectClass => [ 'top', 'organizationalUnit' ], %rdn |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1849 |
]); |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1850 |
}; |
0 | 1851 |
|
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1852 |
my $rv = $self->with_retry( $mkdir ); |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1853 |
|
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1854 |
print $rv->error(), "\n"; |
0 | 1855 |
$self->update_entries( clearcache => 1 ); |
1856 |
return; |
|
1857 |
} |
|
1858 |
||
50 | 1859 |
|
1860 |
### Alter an entry's DN. |
|
1861 |
### |
|
0 | 1862 |
sub run_move |
1863 |
{ |
|
1864 |
my $self = shift; |
|
1865 |
my ( $s_dn, $d_dn ) = @_; |
|
1866 |
||
1867 |
unless ( $s_dn ) { |
|
1868 |
print "No source dn provided.\n"; |
|
1869 |
return; |
|
1870 |
} |
|
1871 |
unless ( $d_dn ) { |
|
1872 |
print "No destination dn provided.\n"; |
|
1873 |
return; |
|
1874 |
} |
|
1875 |
||
31
a3a710f720dd
run_cat: convert to using path_to_dn()
Peter Marschall <peter@adpm.de>
parents:
30
diff
changeset
|
1876 |
# convert given source path to DN |
33
057fefab56b0
Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents:
32
diff
changeset
|
1877 |
$s_dn = $self->path_to_dn( $s_dn ); |
31
a3a710f720dd
run_cat: convert to using path_to_dn()
Peter Marschall <peter@adpm.de>
parents:
30
diff
changeset
|
1878 |
|
0 | 1879 |
unless ( $self->is_valid_dn( $s_dn ) ) { |
1880 |
print "No such object\n"; |
|
1881 |
return; |
|
1882 |
} |
|
1883 |
||
1884 |
# see if we're moving the entry to a totally new path |
|
1885 |
my ( $new_dn, $old_dn ); |
|
1886 |
( $d_dn, $new_dn ) = ( $1, $2 ) if $d_dn =~ /^([\w=]+),(.*)$/; |
|
1887 |
$old_dn = $1 if $s_dn =~ /^[\w=]+,(.*)$/; |
|
1888 |
||
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1889 |
my $moddn = sub { |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1890 |
return $self->ldap()->moddn( |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1891 |
$s_dn, |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1892 |
newrdn => $d_dn, |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1893 |
deleteoldrdn => 1, |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1894 |
newsuperior => $new_dn |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1895 |
); |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1896 |
}; |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1897 |
my $rv = $self->with_retry( $moddn ); |
0 | 1898 |
print $rv->error(), "\n"; |
1899 |
||
1900 |
# clear caches |
|
1901 |
$self->{'cache'}->{ $new_dn } = {} if $new_dn; |
|
1902 |
$self->{'cache'}->{ $old_dn } = {} if $old_dn; |
|
1903 |
$self->update_entries( clearcache => 1 ); |
|
1904 |
return; |
|
1905 |
} |
|
1906 |
||
50 | 1907 |
|
1908 |
### Change the 'userPassword' attribute of an entry, if |
|
1909 |
### supported by the LDAP server. |
|
1910 |
### |
|
0 | 1911 |
sub run_passwd |
1912 |
{ |
|
1913 |
my $self = shift; |
|
1914 |
my $dn = shift || $self->base(); |
|
1915 |
||
1916 |
$self->{'root_dse'} ||= $self->ldap->root_dse(); |
|
1917 |
||
1918 |
my $pw_extension = '1.3.6.1.4.1.4203.1.11.1'; |
|
1919 |
unless ( $self->{'root_dse'}->supported_extension( $pw_extension ) ) { |
|
1920 |
print "Sorry, password changes not supported by LDAP server.\n"; |
|
1921 |
return; |
|
1922 |
} |
|
1923 |
||
31
a3a710f720dd
run_cat: convert to using path_to_dn()
Peter Marschall <peter@adpm.de>
parents:
30
diff
changeset
|
1924 |
# convert given path to DN |
40
c6a3abc56c74
Fix bug introduced in rev:a3a710f720dd with passwd arguments.
Mahlon E. Smith <mahlon@martini.nu>
parents:
38
diff
changeset
|
1925 |
$dn = $self->path_to_dn( $dn ); |
0 | 1926 |
|
1927 |
my $s = $self->search( { base => $dn, scope => 'base' } ); |
|
1928 |
if ( $s->{'code'} ) { |
|
1929 |
print $s->{'message'}, "\n"; |
|
1930 |
return; |
|
1931 |
} |
|
1932 |
my $e = ${ $s->{'entries'} }[0]; |
|
1933 |
||
1934 |
unless ( $e->exists('userPassword') ) { |
|
1935 |
print "No userPassword attribute for $dn\n"; |
|
1936 |
return; |
|
1937 |
} |
|
1938 |
||
1939 |
print "Changing password for $dn\n"; |
|
1940 |
Term::ReadKey::ReadMode 2; |
|
1941 |
print "Enter new password: "; |
|
1942 |
chomp( my $pw = <STDIN> ); |
|
1943 |
print "\nRetype new password: "; |
|
1944 |
chomp( my $pw2 = <STDIN> ); |
|
1945 |
print "\n"; |
|
1946 |
Term::ReadKey::ReadMode 0; |
|
1947 |
||
1948 |
if ( $pw ne $pw2 ) { |
|
1949 |
print "Sorry, passwords do not match.\n"; |
|
1950 |
return; |
|
1951 |
} |
|
1952 |
||
50 | 1953 |
my $setpw = sub { return $self->ldap->set_password( user => $dn, newpasswd => $pw ); }; |
1954 |
my $rv = $self->with_retry( $setpw ); |
|
0 | 1955 |
|
16
a2e3faa3d2fc
use symbolic LDAP error codes instead of numbers
Peter Marschall <peter@adpm.de>
parents:
15
diff
changeset
|
1956 |
if ( $rv->code() == LDAP_SUCCESS ) { |
0 | 1957 |
print "Password updated successfully.\n"; |
48
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1958 |
} |
fe27dfe5179e
More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents:
46
diff
changeset
|
1959 |
else { |
0 | 1960 |
print "Password error: " . $rv->error() . "\n"; |
1961 |
} |
|
1962 |
||
1963 |
return; |
|
1964 |
} |
|
1965 |
||
50 | 1966 |
|
1967 |
### Display the current working "directory". |
|
1968 |
### |
|
0 | 1969 |
sub run_pwd |
1970 |
{ |
|
1971 |
my $self = shift; |
|
1972 |
print $self->base() . "\n"; |
|
1973 |
return; |
|
1974 |
} |
|
1975 |
||
1976 |
||
50 | 1977 |
### Display the currently bound user. |
1978 |
### |
|
0 | 1979 |
sub run_whoami |
1980 |
{ |
|
1981 |
my $self = shift; |
|
1982 |
print $conf->{'binddn'} || 'anonymous bind'; |
|
1983 |
print "\n"; |
|
1984 |
return; |
|
1985 |
} |
|
1986 |
||
50 | 1987 |
|
1988 |
######################################################################## |
|
1989 |
### M A I N |
|
1990 |
######################################################################## |
|
0 | 1991 |
|
1992 |
package main; |
|
1993 |
use strict; |
|
1994 |
use warnings; |
|
1995 |
||
1996 |
$0 = 'shelldap'; |
|
50 | 1997 |
my $VERSION = '0.9.0'; |
0 | 1998 |
|
1999 |
use Getopt::Long; |
|
2000 |
use YAML::Syck; |
|
2001 |
use Pod::Usage; |
|
2002 |
eval 'use Term::ReadLine::Gnu'; |
|
2003 |
warn qq{Term::ReadLine::Gnu not installed. |
|
2004 |
Continuing, but shelldap is of limited usefulness without it.\n\n} if $@; |
|
2005 |
||
2006 |
# get config - rc file first, command line overrides |
|
2007 |
use vars '$conf'; |
|
2008 |
$conf = load_config() || {}; |
|
2009 |
Getopt::Long::GetOptions( |
|
2010 |
$conf, |
|
18
db47ba64ebda
accept short option names for some options
Peter Marschall <peter@adpm.de>
parents:
17
diff
changeset
|
2011 |
'server|H=s', |
50 | 2012 |
'configfile|f=s', |
18
db47ba64ebda
accept short option names for some options
Peter Marschall <peter@adpm.de>
parents:
17
diff
changeset
|
2013 |
'binddn|D=s', |
db47ba64ebda
accept short option names for some options
Peter Marschall <peter@adpm.de>
parents:
17
diff
changeset
|
2014 |
'basedn|b=s', |
0 | 2015 |
'cacheage=i', |
42
b8c6d4e8f828
Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents:
41
diff
changeset
|
2016 |
'promptpass|W', |
0 | 2017 |
'timeout=i', |
3
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
2018 |
'tls_cacert=s', |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
2019 |
'tls_cert=s', |
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
2020 |
'tls_key=s', |
37 | 2021 |
'tls', 'debug', 'version', |
0 | 2022 |
help => sub { |
2023 |
Pod::Usage::pod2usage( |
|
2024 |
-verbose => 1, |
|
2025 |
-message => "\n$0 command line flags\n" . '-' x 65 |
|
2026 |
); |
|
2027 |
} |
|
2028 |
); |
|
2029 |
||
37 | 2030 |
# show version |
2031 |
if ( $conf->{'version'} ) { |
|
50 | 2032 |
print "$0 $VERSION\n"; |
37 | 2033 |
exit( 0 ); |
2034 |
} |
|
2035 |
||
50 | 2036 |
# additional/different config file? |
2037 |
# |
|
2038 |
if ( $conf->{'configfile'} ) { |
|
2039 |
my $more_conf = load_config( $conf->{'configfile'} ); |
|
2040 |
while ( my ($k, $v) = each %{$conf} ) { $conf->{ $k } = $v } |
|
2041 |
} |
|
2042 |
||
2043 |
||
0 | 2044 |
# defaults |
50 | 2045 |
$conf->{'configfile'} ||= "$ENV{'HOME'}/.shelldap.rc"; |
0 | 2046 |
$conf->{'cacheage'} ||= 300; |
2047 |
$conf->{'timeout'} ||= 10; |
|
2048 |
||
2049 |
# create and enter shell loop |
|
2050 |
my $shell = LDAP::Shell->new(); |
|
2051 |
$shell->cmdloop(); |
|
2052 |
||
50 | 2053 |
### load YAML config into global conf. |
2054 |
### |
|
0 | 2055 |
sub load_config |
2056 |
{ |
|
50 | 2057 |
my $confpath = shift; |
0 | 2058 |
my ( $d, $data ); |
2059 |
||
50 | 2060 |
unless ( $confpath ) { |
2061 |
my @confs = ( |
|
2062 |
"$ENV{'HOME'}/.shelldap.rc", |
|
2063 |
'/usr/local/etc/shelldap.conf', |
|
2064 |
'/etc/shelldap.conf', |
|
2065 |
); |
|
2066 |
foreach ( @confs ) { |
|
2067 |
if ( -e $_ ) { |
|
2068 |
$confpath = $_; |
|
2069 |
last; |
|
2070 |
} |
|
0 | 2071 |
} |
2072 |
} |
|
2073 |
$confpath or return undef; |
|
2074 |
||
2075 |
open YAML, $confpath or return undef; |
|
2076 |
do { |
|
2077 |
local $/ = undef; |
|
2078 |
$data = <YAML>; # slurp! |
|
2079 |
}; |
|
2080 |
close YAML; |
|
2081 |
||
2082 |
eval { $conf = YAML::Syck::Load( $data ) }; |
|
3
0f815f3daaf7
Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
2083 |
die "Invalid YAML in $confpath\n" if $@; |
0 | 2084 |
|
2085 |
return $conf; |
|
2086 |
} |
|
2087 |
||
50 | 2088 |
### EOF |
0 | 2089 |