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