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