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