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