shelldap
author Mahlon E. Smith <mahlon@martini.nu>
Thu, 15 Jul 2010 09:01:05 -0700
changeset 4 5a65bc849363
parent 3 0f815f3daaf7
child 5 78b2a48e07db
permissions -rwxr-xr-x
Append a trailing slash to entries that contain other entries. Thanks to Jonathan Rozes <jonathan@laika.com> for the idea, and Michael Granger <ged@faeriemud.org> for telling me about the hasSubordinates attribute (that he was already using to do exactly this in the Treequel-based ruby shell, heh!)
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
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
     4
# Copyright (c) 2006, Mahlon E. Smith <mahlon@martini.nu>
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.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    11
#     * Redistributions in binary form must reproduce the above copyright
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    12
#       notice, this list of conditions and the following disclaimer in the
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    13
#       documentation and/or other materials provided with the distribution.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    14
#     * Neither the name of Mahlon E. Smith nor the names of his
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    15
#       contributors may be used to endorse or promote products derived
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    16
#       from this software without specific prior written permission.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    17
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    18
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    19
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    20
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    21
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    22
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    23
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    24
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    25
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    26
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    27
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    28
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    29
=head1 NAME
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    30
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    31
Shelldap / LDAP::Shell
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    32
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    33
A program for interacting with an LDAP server via a shell-like
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    34
interface.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    35
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    36
This is not meant to be an exhaustive LDAP editing and browsing
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    37
interface, but rather an intuitive shell for performing basic LDAP
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    38
tasks quickly and with minimal effort.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    39
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    40
=head1 SYNPOSIS
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    41
3
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    42
 shelldap --server example.net [--help]
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    43
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    44
=head1 FEATURES
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    45
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    46
 - Upon successful authenticated binding, credential information is
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    47
   auto-cached to ~/.shelldap.rc -- future loads require no command line
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    48
   flags.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    49
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    50
 - Custom 'description maps' for entry listings.  (See the 'list' command.)
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    51
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    52
 - History and autocomplete via readline, if installed.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    53
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    54
 - Automatic reconnection attempts if the connection is lost with the
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    55
   LDAP server.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    56
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    57
 - It feels like a semi-crippled shell, making LDAP browsing and editing
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    58
   at least halfway pleasurable.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    59
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    60
=head1 OPTIONS
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    61
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    62
All command line options follow getopts long conventions.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    63
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    64
    shelldap --server example.net --basedn dc=your,o=company
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    65
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    66
You may also optionally create a ~/.shelldap.rc file with command line
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    67
defaults.  This file should be valid YAML.  (This file is generated
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    68
automatically on a successful bind auth.)
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    69
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    70
Example:
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    71
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    72
    server: ldap.example.net
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    73
    binddn: cn=Manager,dc=your,o=company
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    74
    bindpass: xxxxxxxxx
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    75
    basedn: dc=your,o=company
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    76
    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
    77
    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
    78
    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
    79
    tls_key:    ~/.ssl/private/client.key.pem
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    80
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    81
=over 4
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    82
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    83
=item B<server>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    84
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    85
Required. The LDAP server to connect to.  This can be a hostname, IP
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    86
address, or a URI.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    87
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    88
    --server ldaps://ldap.example.net
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    89
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    90
=back
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    91
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    92
=over 4
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    93
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    94
=item B<binddn>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    95
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    96
The full dn of a user to authenticate as.  If not specified, defaults to
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    97
an anonymous bind.  You will be prompted for a password.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    98
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    99
    --binddn cn=Manager,dc=your,o=company
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   100
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   101
=back
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   102
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   103
=over 4
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   104
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   105
=item B<basedn>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   106
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   107
The directory 'root' of your LDAP server.  If omitted, shelldap will
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   108
try and ask the server for a sane default.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   109
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   110
    --basedn dc=your,o=company
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   111
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   112
=back
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   113
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   114
=over 4
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   115
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   116
=item B< tls>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   117
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   118
Enables TLS over what would normally be an insecure connection.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   119
Requires server side support.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   120
3
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   121
=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
   122
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   123
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
   124
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   125
    --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
   126
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   127
=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
   128
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   129
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
   130
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   131
    --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
   132
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   133
=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
   134
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   135
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
   136
key verification.
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   137
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   138
    --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
   139
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   140
=back
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   141
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   142
=over 4
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   143
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   144
=item B<cacheage>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   145
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   146
Set the time to cache directory lookups in seconds.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   147
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   148
By default, directory lookups are cached for 300 seconds, to speed
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   149
autocomplete up when changing between different basedns.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   150
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   151
Modifications to the directory automatically reset the cache.  Directory
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   152
listings are not cached.  (This is just used for autocomplete.)  Set it
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   153
to 0 to disable caching completely.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   154
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   155
=back
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   156
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   157
=over 4
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   158
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   159
=item B<timeout>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   160
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   161
Set the maximum time an LDAP operation can take before it is cancelled.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   162
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   163
=back
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   164
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   165
=over 4
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   166
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   167
=item B<debug>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   168
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   169
Print extra operational info out, and backtrace on fatal error.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   170
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   171
=back
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   172
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   173
=head1 SHELL COMMANDS
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   174
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   175
=over 4
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   176
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   177
=item B< cat>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   178
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   179
Display an LDIF dump of an entry.  Globbing is supported.  Specify
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   180
either the full dn, or an rdn.  For most commands, rdns are local to the
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   181
current search base. ('cwd', as translated to shell speak.)  You may additionally
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   182
add a list of attributes to display.  Use '+' for server side attributes.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   183
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   184
    cat uid=mahlon
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   185
    cat ou=*
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   186
    cat uid=mahlon,ou=People,dc=example,o=company
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   187
    cat uid=mahlon + userPassword
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   188
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   189
=item B<  cd>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   190
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   191
Change directory.  Translated to LDAP, this changes the current basedn.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   192
All commands after a 'cd' operate within the new basedn.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   193
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   194
    cd                cd to 'home' basedn
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   195
    cd ~              same thing
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   196
    cd -              cd to previous directory
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   197
    cd ou=People      cd to explicit path
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   198
    cd ..             cd to parent node
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   199
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   200
Since LDAP doesn't actually limit what can be a container object, you
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   201
can actually cd into any entry. Many commands then work on '.', meaning
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   202
"wherever I currently am."
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   203
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   204
    cd uid=mahlon
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   205
    cat .
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   206
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   207
=item B<clear>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   208
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   209
Clear the screen.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   210
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   211
=item B<copy>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   212
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   213
Copy an entry to a different dn path.  All copies are relative to the
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   214
current basedn, unless a full dn is specified.  All attributes are
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   215
copied, then an LDAP moddn() is performed.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   216
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   217
    copy uid=mahlon uid=bob
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   218
    copy uid=mahlon ou=Others,dc=example,o=company
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   219
    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
   220
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   221
aliased to: cp
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   222
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   223
=item B<create>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   224
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   225
Create an entry from scratch.  Arguments are space separated objectClass
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   226
names.  Possible objectClasses are derived automatically from the
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   227
server, and will tab-complete.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   228
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   229
After the classes are specified, an editor will launch.  Required
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   230
attributes are listed first, then optional attributes.  Optionals are
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   231
commented out.  After the editor exits, the resulting LDIF is validated
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   232
and added to the LDAP directory.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   233
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   234
    create top person organizationalPerson inetOrgPerson posixAccount
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   235
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   236
aliased to: touch
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   237
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   238
=item B<delete>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   239
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   240
Remove an entry from the directory.  Globbing is supported.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   241
All deletes are sanity-prompted.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   242
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   243
    delete uid=mahlon
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   244
    delete uid=ma*
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   245
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   246
aliased to: rm
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   247
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   248
=item B<edit>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   249
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   250
Edit an entry in an external editor.  After the editor exits, the
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   251
resulting LDIF is sanity checked, and changes are written to the LDAP
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   252
directory.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   253
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   254
    edit uid=mahlon
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   255
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   256
aliased to: vi
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   257
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   258
=item B< env>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   259
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   260
 Show values for various runtime variables.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   261
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   262
=item B<grep>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   263
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   264
Search for arbitrary LDAP filters, and return matching dn results.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   265
The search string must be a valid LDAP filter.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   266
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   267
    grep uid=mahlon
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   268
    grep uid=mahlon ou=People
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   269
    grep -r (&(uid=mahlon)(objectClass=*))
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   270
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   271
 aliased to: search
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   272
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   273
=item B<list>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   274
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   275
List entries for the current basedn.  Globbing is supported.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   276
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   277
aliased to: ls
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   278
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   279
    ls -l
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   280
    ls -lR uid=mahlon
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   281
    list uid=m*
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   282
    list verbose
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   283
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   284
In 'verbose' mode, descriptions are listed as well, if they exist.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   285
There are also some 'sane' long listings for common objectClass types.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   286
You can actually specify your own in your .shelldap.rc, like so:
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   287
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   288
    ...
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   289
    descmaps:
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   290
        objectClass: attributename
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   291
        posixAccount: gecos
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   292
        posixGroup: gidNumber
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   293
        ipHost: ipHostNumber
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   294
        puppetClient: puppetclass
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   295
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   296
=item B<mkdir>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   297
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   298
Creates a new 'organizationalUnit' entry.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   299
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   300
    mkdir containername
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   301
    mkdir ou=whatever
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   302
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   303
=item B<move>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   304
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   305
Move an entry to a different dn path.  Usage is identical to B<copy>.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   306
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   307
aliased to: mv
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   308
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   309
=item B<passwd>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   310
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   311
If supported server side, change the password for a specified entry.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   312
The entry must have a 'userPassword' attribute.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   313
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   314
    passwd uid=mahlon
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   315
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   316
=item B< pwd>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   317
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   318
Print the 'working directory' - aka, the current ldap basedn.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   319
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   320
=item B<setenv>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   321
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   322
Modify various runtime variables normally set from the command line.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   323
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   324
    setenv debug 1
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   325
    export debug=1
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   326
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   327
=item B<whoami>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   328
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   329
Show current auth credentials.  Unless you specified a binddn, this
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   330
will just show an anonymous bind.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   331
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   332
=back
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   333
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   334
=head1 TODO
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   335
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   336
Referral support.  Currently, if you try to write to a replicant slave,
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   337
you'll just get a referral.  It would be nice if shelldap automatically
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   338
tried to follow it.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   339
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   340
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
   341
any writes.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   342
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   343
"cd ../ou=SomewhereElse" doesn't work, but "cd ../../" does.  This is
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   344
weird, as both should probably work.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   345
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   346
=head1 BUGS / LIMITATIONS
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   347
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   348
There is currently no attribute multiline support - attribute values
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   349
that span over one line will be ignored if modified.  (Thankfully, they
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   350
are generally rare.)
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   351
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   352
There is no support for editing binary data.  This is actually related
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   353
to the lack of multiline support - if you just base64 encode data and
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   354
paste it in, it will be ignored for the same reasons.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   355
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   356
=head1 AUTHOR
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   357
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   358
Mahlon E. Smith <mahlon@martini.nu>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   359
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   360
=cut
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   361
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   362
package LDAP::Shell;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   363
use strict;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   364
use warnings;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   365
use Term::ReadKey;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   366
use Term::Shell;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   367
use Digest::MD5;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   368
use Net::LDAP;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   369
use Net::LDAP::LDIF;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   370
use Data::Dumper;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   371
use File::Temp;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   372
use Algorithm::Diff;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   373
use Carp 'confess';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   374
use base 'Term::Shell';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   375
require Net::LDAP::Extension::SetPassword;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   376
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   377
my $conf = $main::conf;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   378
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   379
# make 'die' backtrace in debug mode
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   380
$SIG{'__DIE__'} = \&Carp::confess if $conf->{'debug'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   381
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   382
###############################################################
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   383
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   384
# UTILITY FUNCTIONS
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   385
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   386
###############################################################
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   387
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   388
# initial shell behaviors
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   389
# 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   390
sub init
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   391
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   392
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   393
	$self->{'API'}->{'match_uniq'} = 0;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   394
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   395
	$self->{'editor'} = $ENV{'EDITOR'} || 'vi';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   396
	$self->{'env'}	= [ qw/ debug cacheage timeout / ];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   397
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   398
	# let autocomplete work with the '=' character
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   399
	my $term = $self->term();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   400
	$term->Attribs->{'basic_word_break_characters'}	 =~ s/=//m;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   401
	$term->Attribs->{'completer_word_break_characters'} =~ s/=//m;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   402
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   403
	# read in history
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   404
	eval {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   405
		$term->history_truncate_file("$ENV{'HOME'}/.shelldap_history", 50);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   406
		$term->ReadHistory("$ENV{'HOME'}/.shelldap_history");
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   407
	};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   408
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   409
	$self->{'root_dse'} = $self->ldap->root_dse();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   410
	if ( $conf->{'debug'} ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   411
		$self->{'schema'}   = $self->ldap->schema();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   412
		my @versions = 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   413
			@{ $self->{'root_dse'}->get_value('supportedLDAPVersion', asref => 1) };
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   414
		print "Connected to $conf->{'server'}\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   415
		print "Supported LDAP version: ", ( join ', ', @versions ), "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   416
		print "Cipher in use: ", $self->ldap()->cipher(), "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   417
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   418
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   419
	# try an initial search and die if it doesn't work
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   420
	# (bad baseDN)
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   421
	my $s = $self->search();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   422
	die "LDAP baseDN error: ", $s->{'message'}, "\n" if $s->{'code'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   423
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   424
	$self->{'schema'} = $self->ldap->schema();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   425
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   426
	# okay, now do an initial population of 'cwd'
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   427
	# for autocomplete.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   428
	$self->update_entries();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   429
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   430
	# whew, okay.  Update prompt, wait for input!
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   431
	$self->update_prompt();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   432
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   433
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   434
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   435
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   436
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   437
# get an ldap connection handle
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   438
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   439
sub ldap
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   440
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   441
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   442
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   443
	# use cached connection object if it exists
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   444
	return $self->{'ldap'} if $self->{'ldap'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   445
	
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   446
	# fill in potentially missing info
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   447
	die "No server specified.\n" unless $conf->{'server'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   448
	if ( $conf->{'binddn'} && ! $conf->{'bindpass'} ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   449
		print "Bind password: ";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   450
		Term::ReadKey::ReadMode 2;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   451
		chomp($conf->{'bindpass'} = <STDIN>);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   452
		Term::ReadKey::ReadMode 0;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   453
		print "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   454
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   455
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   456
	# make connection
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   457
	my $ldap = Net::LDAP->new( $conf->{'server'} )
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   458
		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
   459
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   460
	# secure connection options
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   461
	if ( $conf->{'tls'} ) {
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   462
		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
   463
			$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
   464
				verify     => 'require',
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   465
				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
   466
				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
   467
				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
   468
				keydecrypt => sub {
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   469
					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
   470
					Term::ReadKey::ReadMode 2;
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   471
					chomp(my $secret = <STDIN>);
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   472
					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
   473
					print "\n";
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   474
					return $secret;
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   475
				});
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   476
		}
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   477
		else {
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   478
			$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
   479
		}
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   480
	}
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   481
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   482
	# bind
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   483
	my $rv;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   484
	if ( $conf->{'binddn'} ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   485
		# authed
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   486
		$rv = $ldap->bind(
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   487
			$conf->{'binddn'},
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   488
			password => $conf->{'bindpass'}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   489
		);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   490
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   491
	else {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   492
		# anon
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   493
		$rv = $ldap->bind();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   494
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   495
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   496
	my $err = $rv->error();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   497
	if ( $rv->code() ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   498
		$err .= " (forgot the --tls flag?)"
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   499
			if $err =~ /confidentiality required/i;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   500
		die "LDAP bind error: $err\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   501
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   502
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   503
	# offer to cache authentication info
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   504
	# if we enter this conditional, we have successfully 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   505
	# authed with the server (non anonymous), and 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   506
	# we haven't cached anything in the past.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   507
	if ( $conf->{'binddn'} && ! -e $conf->{'confpath'} ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   508
		print "Would you like to cache your connection information? [Y/n]: ";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   509
		chomp( my $response = <STDIN> );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   510
		unless ( $response =~ /^n/i ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   511
			YAML::Syck::DumpFile( $conf->{'confpath'}, $conf );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   512
			chmod 0600, $conf->{'confpath'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   513
			print "Connection info cached.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   514
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   515
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   516
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   517
	$self->{'ldap'} = $ldap;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   518
	return $ldap;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   519
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   520
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   521
# just return an LDIF object
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   522
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   523
sub ldif 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   524
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   525
	my $self	 = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   526
	my $use_temp = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   527
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   528
	# create tmpfile and link ldif object with it
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   529
	if ( $use_temp ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   530
		my ( undef, $fname ) =
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   531
		  File::Temp::tempfile( 'shelldap_XXXXXXXX', DIR => '/tmp', UNLINK => 1 );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   532
		$self->{'ldif'}	   = Net::LDAP::LDIF->new( $fname, 'w', sort => 1 );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   533
		$self->{'ldif_fname'} = $fname;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   534
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   535
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   536
	# ldif -> stdout
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   537
	else {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   538
		$self->{'ldif'} = Net::LDAP::LDIF->new( \*STDOUT, 'w', sort => 1 );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   539
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   540
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   541
	return $self->{'ldif'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   542
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   543
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   544
# load and return an Entry object from LDIF
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   545
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   546
sub load_ldif
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   547
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   548
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   549
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   550
	my $ldif =  Net::LDAP::LDIF->new( shift(), 'r' );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   551
	return unless $ldif;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   552
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   553
	my $e;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   554
	eval { $e = $ldif->read_entry(); };
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   555
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   556
	return if $@;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   557
	return $e;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   558
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   559
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   560
# given a filename, return an md5 checksum
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   561
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   562
sub chksum 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   563
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   564
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   565
	my $file = shift or return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   566
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   567
	my $md5 = Digest::MD5->new();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   568
	open F, $file or die "Unable to read temporary ldif: $!\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   569
	my $hash = $md5->addfile( *F )->hexdigest();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   570
	close F;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   571
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   572
	return $hash;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   573
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   574
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   575
# prompt functions
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   576
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   577
sub prompt_str
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   578
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   579
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   580
	return $self->{'prompt'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   581
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   582
sub update_prompt 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   583
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   584
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   585
	my $base = $self->base();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   586
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   587
	if ( length $base > 50 ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   588
		my $cwd_dn = $1 if $base =~ /^(.*?),/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   589
		$self->{'prompt'} = "... $cwd_dn > ";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   590
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   591
	else {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   592
		my $prompt = $base;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   593
		$prompt =~ s/$conf->{'basedn'}/~/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   594
		$self->{'prompt'} = "$prompt > ";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   595
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   596
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   597
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   598
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   599
# search base accessor
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   600
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   601
sub base 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   602
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   603
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   604
	$self->{'base'} ||= $conf->{'basedn'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   605
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   606
	# try and determine base automatically from rootDSE
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   607
	#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   608
	unless ( $self->{'base'} ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   609
		my $base = $self->{'root_dse'}->{'asn'} || {};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   610
		$base = $base->{'attributes'}			|| [];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   611
		$base = $base->[0]						|| {};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   612
		$base = $base->{'vals'}					|| [];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   613
		$conf->{'basedn'} = $base->[0];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   614
		$self->{'base'}   = $base->[0];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   615
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   616
	if ( $_[0] ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   617
		$self->{'base'} = $_[0];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   618
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   619
	return $self->{'base'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   620
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   621
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   622
# make sure a given rdn includes the current
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   623
# base, making it a dn.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   624
# accepts a string reference.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   625
# 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   626
sub rdn_to_dn 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   627
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   628
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   629
	my $rdn  = shift or return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   630
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   631
	return unless ref $rdn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   632
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   633
	# allow cd to 'basedn' and cd to directories 'higher' in the tree
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   634
	return if $$rdn =~ /$conf->{'basedn'}$/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   635
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   636
	# auto fill in current base for deeper DNs
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   637
	my ( $dn, $curbase ) = ( $$rdn, $self->base() );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   638
	$dn = "$$rdn," . $curbase unless $$rdn =~ /$curbase/i;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   639
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   640
	$$rdn = $dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   641
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   642
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   643
# do a search on a dn to determine if it is valid.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   644
# returns a bool.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   645
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   646
sub is_valid_dn 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   647
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   648
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   649
	my $dn   = shift or return 0;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   650
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   651
	my $r = $self->search({ base => $dn });
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   652
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   653
	return $r->{'code'} == 0 ? 1 : 0;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   654
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   655
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   656
# perform an ldap search
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   657
# return an hashref containing return code and
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   658
# arrayref of Net::LDAP::Entry objects
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   659
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   660
sub search 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   661
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   662
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   663
	my $opts = shift || {};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   664
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   665
	$opts->{'base'}   ||= $self->base(),
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   666
	$opts->{'filter'} ||= '(objectClass=*)';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   667
	$opts->{'scope'}  ||= 'base';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   668
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   669
	my $s = $self->ldap->search(
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   670
		base	  => $opts->{'base'},
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   671
		filter	  => $opts->{'filter'},
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   672
		scope	  => $opts->{'scope'},
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   673
		timelimit => $conf->{'timeout'},
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   674
		typesonly => ! $opts->{'vals'},
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   675
		attrs	  => $opts->{'attrs'} || ['*']
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   676
	);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   677
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   678
	my $rv = {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   679
		code	  => $s->code(),
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   680
		message   => $s->error(),
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   681
		entries   => []
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   682
	};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   683
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   684
	# since search is used just about everywhere, this seems like 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   685
	# a pretty good place to check for connection errors.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   686
	#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   687
	# check for a lost connection, kill cached object so we
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   688
	# try to reconnect on the next search.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   689
	#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   690
	$self->{'ldap'} = undef if $s->code() == 81;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   691
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   692
	$rv->{'entries'} =
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   693
	  $opts->{'scope'} eq 'base' ? [ $s->shift_entry() ] : [ $s->entries() ];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   694
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   695
	return $rv;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   696
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   697
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   698
# update the autocomplete for entries
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   699
# in the current base tree, respecting or creating cache.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   700
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   701
sub update_entries 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   702
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   703
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   704
	my %opts = @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   705
	my $base = lc( $self->base() );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   706
	
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   707
	my $s = $opts{'search'} || $self->search({ scope => 'one' });
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   708
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   709
	$self->{'cwd_entries'} = [];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   710
	return if $s->{'code'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   711
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   712
	# setup cache object
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   713
	$self->{'cache'} ||= {};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   714
	$self->{'cache'}->{ $base } ||= {};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   715
	$self->{'cache'}->{ $base } = {} if $opts{'clearcache'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   716
	my $cache = $self->{'cache'}->{ $base };
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   717
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   718
	my $now = time();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   719
	if ( ! exists $cache->{'entries'}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   720
		or $now - $cache->{'timestamp'} > $conf->{'cacheage'} )
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   721
	{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   722
		$self->debug("Caching entries for $base\n");
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   723
		foreach my $e ( @{ $s->{'entries'} } ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   724
			my $dn  = $e->dn();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   725
			my $rdn = $dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   726
			$rdn =~ s/,$base//i;  # remove base from display
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   727
			push @{ $self->{'cwd_entries'} }, $rdn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   728
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   729
		$cache->{'timestamp'} = $now;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   730
		$cache->{'entries'} = $self->{'cwd_entries'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   731
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   732
	else {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   733
		$self->debug("Using cached lookups for $base\n");
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   734
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   735
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   736
	$self->{'cwd_entries'} = $cache->{'entries'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   737
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   738
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   739
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   740
# parse parent ('..') cn requests
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   741
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   742
sub parent_dn 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   743
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   744
	my $self = shift;	
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   745
	my $rdn  = shift or return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   746
	return unless ref $rdn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   747
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   748
	# FIXME: 'cd ../ou=somewhere' should work
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   749
	my $dn = $self->base();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   750
	my $dotcount = $$rdn =~ s/\.\./\.\./g;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   751
	$dn =~ s/^.*?,// for 1 .. $dotcount;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   752
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   753
	$$rdn = $dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   754
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   755
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   756
# given an array ref of shell-like globs, 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   757
# make and return an LDAP filter object.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   758
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   759
sub make_filter 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   760
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   761
	my $self  = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   762
	my $globs = shift or return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   763
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   764
	return unless ref $globs eq 'ARRAY';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   765
	return unless scalar @$globs;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   766
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   767
	my $filter;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   768
	$filter = join '', map { "($_)" } @$globs;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   769
	$filter = '(|' . $filter . ')' if scalar @$globs > 1;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   770
	$filter = Net::LDAP::Filter->new( $filter );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   771
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   772
	if ( $filter ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   773
		$self->debug('Filter parsed as: ' . $filter->as_string() . "\n");
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   774
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   775
	else {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   776
		print "Error parsing filter.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   777
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   778
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   779
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   780
	return $filter;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   781
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   782
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   783
# little. yellow. different. better.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   784
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   785
sub debug 
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
	return unless $conf->{'debug'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   789
	print "\e[33m";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   790
	print shift();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   791
	print "\e[0m";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   792
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   793
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   794
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   795
# setup command autocompletes for
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   796
# all commands that have the same possible values
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   797
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   798
sub autocomplete_cwd
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   799
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   800
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   801
	my $word = $_[0];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   802
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   803
	return sort @{ $self->{'cwd_entries'} };
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   804
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   805
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   806
sub comp_setenv
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   807
{ 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   808
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   809
	return @{ $self->{'env'} };
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   810
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   811
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   812
sub comp_create
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   813
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   814
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   815
	return @{ $self->{'objectclasses'} } if $self->{'objectclasses'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   816
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   817
	my @oc_data = $self->{'schema'}->all_objectclasses();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   818
	my @oc;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   819
	foreach my $o ( @oc_data ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   820
		push @oc, $o->{'name'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   821
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   822
	@oc = sort @oc;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   823
	$self->{'objectclasses'} = \@oc;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   824
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   825
	return @oc;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   826
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   827
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   828
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   829
	no warnings;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   830
	no strict 'refs';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   831
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   832
	# command, alias
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   833
	my %cmd_map = (
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   834
		whoami => 'id',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   835
		list   => 'ls',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   836
		grep   => 'search',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   837
		edit   => 'vi',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   838
		delete => 'rm',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   839
		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
   840
		cat    => 'read',
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   841
		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
   842
		cd     => undef,
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   843
		passwd => undef
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   844
	);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   845
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   846
	# setup autocompletes
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   847
	foreach ( %cmd_map ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   848
		next unless $_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   849
		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
   850
		*$sub   = \&autocomplete_cwd;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   851
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   852
	*comp_touch  = \&comp_create;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   853
	*comp_export = \&comp_setenv;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   854
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   855
	# setup alias subs
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   856
	#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   857
	# Term::Shell has an alias_* feature, but
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   858
	# it seems to work about 90% of the time.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   859
	# that last 10% is something of a mystery.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   860
	#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   861
	$cmd_map{'create'} = 'touch';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   862
	foreach my $cmd ( keys %cmd_map ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   863
		next unless defined $cmd_map{$cmd};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   864
		my $alias_sub = 'run_' . $cmd_map{$cmd};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   865
		my $real_sub  = 'run_' . $cmd;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   866
		*$alias_sub = \&$real_sub;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   867
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   868
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   869
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
# SHELL METHODS
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   873
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   874
###############################################################
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   875
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   876
# don't die on a newline
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   877
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   878
sub run_ { return; }
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   879
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   880
# print shell debug actions
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   881
# 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   882
sub precmd
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   883
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   884
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   885
	my ( $handler, $cmd, $args ) = @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   886
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   887
	my $term = $self->term();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   888
	eval { $term->WriteHistory("$ENV{'HOME'}/.shelldap_history"); };
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   889
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   890
	return unless $conf->{'debug'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   891
	$self->debug( "$$cmd (" . ( join ' ', @$args ) . "), calling '$$handler'\n" );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   892
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   893
} 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   894
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   895
sub run_cat 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   896
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   897
	my $self  = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   898
	my $dn    = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   899
	my $attrs = \@_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   900
	$attrs->[0] = '*' unless scalar @$attrs;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   901
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   902
	unless ( $dn ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   903
		print "No dn provided.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   904
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   905
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   906
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   907
	# support '.'
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   908
	$dn = $self->base() if $dn eq '.';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   909
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   910
	# support globbing
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   911
	my $s;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   912
	if ( $dn eq '*' ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   913
		$s = $self->search({
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   914
			scope  => 'one',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   915
			vals   => 1,
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   916
			attrs  => $attrs
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   917
		});
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   918
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   919
	elsif ( $dn =~ /\*/ ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   920
		$s = $self->search({
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   921
			scope  => 'one',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   922
			vals   => 1,
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   923
			filter => $dn,
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   924
			attrs  => $attrs
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   925
		});
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   926
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   927
	else {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   928
		$self->rdn_to_dn( \$dn );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   929
		$s = $self->search({
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   930
			base   => $dn,
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   931
			vals   => 1,
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   932
			attrs  => $attrs
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   933
		});
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   934
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   935
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   936
	if ( $s->{'code'} ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   937
		print $s->{'message'} . "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   938
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   939
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   940
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   941
	foreach my $e ( @{ $s->{'entries'} } ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   942
		$self->ldif->write_entry( $e );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   943
		print "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   944
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   945
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   946
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   947
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   948
sub run_cd 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   949
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   950
	my $self	= shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   951
	my $newbase = join ' ', @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   952
	
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   953
	# support 'cd' going to root
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   954
	$newbase ||= $conf->{'basedn'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   955
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   956
	# support 'cd -'
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   957
	if ( $newbase eq '-' ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   958
		$newbase = $self->{'previous_base'} || 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
	# support '..'
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   962
	if ( $newbase =~ /\.\./ ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   963
		$self->parent_dn( \$newbase );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   964
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   965
	else {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   966
		$self->rdn_to_dn( \$newbase );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   967
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   968
	
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   969
	unless ( $self->is_valid_dn( $newbase ) ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   970
		print "No such object\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   971
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   972
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   973
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   974
	# store old base
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   975
	$self->{'previous_base'} = $self->base();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   976
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   977
	# update new base
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   978
	$self->base( $newbase );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   979
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   980
	# get new 'cwd' listing
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   981
	my $s = $self->search({ scope => 'one' });
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   982
	if ( $s->{'code'} ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   983
		print "$s->{'message'}\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   984
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   985
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   986
	$self->update_entries( search => $s );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   987
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   988
	# reflect cwd change in prompt
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   989
	$self->update_prompt();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   990
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   991
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   992
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   993
sub run_clear
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   994
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   995
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   996
	system('clear');
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   997
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   998
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   999
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1000
sub run_copy
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1001
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1002
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1003
	my ( $s_dn, $d_dn ) = @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1004
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1005
	unless ( $s_dn ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1006
		print "No source dn provided.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1007
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1008
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1009
	unless ( $d_dn ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1010
		print "No destination dn provided.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1011
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1012
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1013
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1014
	my $s_rdn = $s_dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1015
	$self->rdn_to_dn( \$s_dn );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1016
	my $s = $self->search({ base => $s_dn, vals => 1 });
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1017
	unless ( $s->{'code'} == 0 ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1018
		print "No such object\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1019
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1020
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1021
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1022
	# see if we're copying the entry to a totally new path
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1023
	my ( $new_dn, $old_dn );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1024
	( $d_dn, $new_dn ) = ( $1, $2 ) if $d_dn =~ /^([\w=]+),(.*)$/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1025
	if ( $new_dn ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1026
		unless ( $self->is_valid_dn( $new_dn ) ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1027
			print "Invalid destination.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1028
			return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1029
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1030
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1031
	else {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1032
		$new_dn = $self->base();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1033
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1034
	$old_dn = $1 if $s_dn =~ /^[\w=]+,(.*)$/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1035
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1036
	# get the source object
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1037
	my $e = ${ $s->{'entries'} }[0];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1038
	$e->dn( $s_dn );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1039
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1040
	# add changes in new entry instead of modifying existing
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1041
	$e->changetype('add'); 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1042
	$e->dn( "$d_dn,$new_dn" );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1043
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1044
	# get the unique attribute from the dn for modification
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1045
	# perhaps there is a better way to do this...?
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1046
	#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1047
	my ( $uniqkey, $uniqval ) = ( $1, $2 )
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1048
	  if $d_dn =~ /^([\.\w]+)(?:\s+)?=(?:\s+)?([\.\-\s\w]+),?/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1049
	unless ( $uniqkey && $uniqval ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1050
		print "Unable to parse unique values from rdn.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1051
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1052
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1053
	$e->replace( $uniqkey => $uniqval );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1054
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1055
	# update
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1056
	my $rv = $e->update( $self->ldap() );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1057
	print $rv->error , "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1058
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1059
	# clear caches
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1060
	$self->{'cache'}->{ $new_dn } = {} if $new_dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1061
	$self->{'cache'}->{ $old_dn } = {} if $old_dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1062
	$self->update_entries( clearcache => 1 );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1063
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1064
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1065
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1066
sub run_create
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1067
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1068
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1069
	my @ocs  = @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1070
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1071
	my ( $fh, $fname ) =
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1072
		File::Temp::tempfile( 'shelldap_XXXXXXXX', DIR => '/tmp', UNLINK => 1 );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1073
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1074
	# first print out the dn and object classes.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1075
	print $fh 'dn: ???,', $self->base(), "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1076
	foreach my $oc ( sort @ocs ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1077
		print $fh "objectClass: $oc\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1078
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1079
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1080
	# now gather attributes for requested objectClasses
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1081
	#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1082
	my ( %seen, @must_attr, @may_attr );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1083
	foreach my $oc ( sort @ocs ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1084
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1085
		# required
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1086
		my @must = $self->{'schema'}->must( $oc );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1087
		foreach my $attr ( sort { $a->{'name'} cmp $b->{'name'} } @must ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1088
			next if $attr->{'name'} =~ /^objectclass$/i;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1089
			next if $seen{ $attr->{'name'} };
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1090
			push @must_attr, $attr->{'name'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1091
			$seen{ $attr->{'name'} }++;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1092
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1093
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1094
		# optional
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1095
		my @may  = $self->{'schema'}->may( $oc );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1096
		foreach my $attr ( sort { $a->{'name'} cmp $b->{'name'} } @may ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1097
			next if $attr->{'name'} =~ /^objectclass$/i;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1098
			next if $seen{ $attr->{'name'} };
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1099
			push @may_attr, $attr->{'name'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1100
			$seen{ $attr->{'name'} }++;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1101
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1102
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1103
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1104
	# print attributes
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1105
	print $fh "$_: \n"   foreach @must_attr;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1106
	print $fh "# $_: \n" foreach @may_attr;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1107
	close $fh;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1108
	my $hash_a = $self->chksum( $fname );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1109
	system( $self->{'editor'}, $fname ) && die "Unable to launch editor: $!\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1110
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1111
	# hash compare
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1112
	my $hash_b = $self->chksum( $fname );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1113
	if ( $hash_a eq $hash_b ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1114
		print "Entry not modified.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1115
		unlink $fname;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1116
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1117
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1118
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1119
	# load in LDIF
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1120
	my $ldif = Net::LDAP::LDIF->new( $fname, 'r', onerror => 'warn' );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1121
	my $e	= $ldif->read_entry();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1122
	unless ( $e ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1123
		print "Unable to parse LDIF.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1124
		unlink $fname;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1125
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1126
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1127
	$e->changetype('add');
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1128
	my $rv = $e->update( $self->ldap() );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1129
	print $rv->error(), "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1130
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1131
	$self->update_entries( clearcache => 1 ) unless $rv->code();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1132
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1133
	unlink $fname;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1134
	return;
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
sub run_delete
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1138
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1139
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1140
	my @DNs  = @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1141
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1142
	unless ( scalar @DNs ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1143
		print "No dn specified.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1144
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1145
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1146
        my $filter;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1147
        unless ( $DNs[0] eq '*' ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1148
            $filter = $self->make_filter( \@DNs ) or return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1149
        }
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1150
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1151
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1152
	my $s = $self->search({ scope => 'one', filter => $filter });
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1153
	if ( $s->{'code'} ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1154
		print "$s->{'message'}\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1155
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1156
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1157
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1158
	print "Are you sure? [N/y]: ";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1159
	chomp( my $resp = <STDIN> );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1160
	return unless $resp =~ /^y/i;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1161
   
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1162
	foreach my $e ( @{ $s->{'entries'} } ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1163
		my $dn = $e->dn();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1164
		my $rv = $self->ldap->delete( $dn );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1165
		print "$dn: ", $rv->error(), "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1166
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1167
   
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1168
	$self->update_entries( clearcache => 1 );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1169
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1170
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1171
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1172
sub run_edit
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 $dn   = join ' ', @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1176
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1177
	unless ( $dn ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1178
		print "No dn provided.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1179
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1180
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1181
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1182
	# support '.'
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1183
	$dn = $self->base() if $dn eq '.';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1184
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1185
	$self->rdn_to_dn( \$dn );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1186
	my $s = $self->search({ base => $dn, vals => 1 });
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1187
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1188
	if ( $s->{'code'} ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1189
		print $s->{'message'} . "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1190
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1191
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1192
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1193
	# fetch entry and write it out to disk
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1194
	my $e = ${ $s->{'entries'} }[0];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1195
	my $ldif = $self->ldif(1);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1196
	$ldif->write_entry( $e );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1197
	$ldif->done();  # force sync
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1198
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1199
	# load it into an array for potential comparison
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1200
	my @orig_ldif;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1201
	open LDIF, "$self->{'ldif_fname'}" or return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1202
	@orig_ldif = <LDIF>;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1203
	close LDIF;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1204
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1205
	# checksum it, then open it in an editor
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1206
	my $hash_a = $self->chksum( $self->{'ldif_fname'} );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1207
	system( "$self->{'editor'} $self->{'ldif_fname'}" ) &&
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1208
		die "Unable to launch editor: $!\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1209
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1210
	# detect a total lack of change
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1211
	my $hash_b = $self->chksum( $self->{'ldif_fname'} );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1212
	if ( $hash_a eq $hash_b ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1213
		print "Entry not modified.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1214
		unlink $self->{'ldif_fname'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1215
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1216
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1217
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1218
	# check changes for basic LDIF validity
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1219
	my $new_e = $self->load_ldif( $self->{'ldif_fname'} );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1220
	unless ( $new_e ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1221
		print "Unable to parse LDIF.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1222
		unlink $self->{'ldif_fname'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1223
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1224
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1225
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1226
	# load changes into a new array for comparison
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1227
	my @new_ldif;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1228
	open LDIF, "$self->{'ldif_fname'}" or return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1229
	@new_ldif = <LDIF>;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1230
	close LDIF;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1231
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1232
	$e->changetype('modify');
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1233
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1234
	my $parse = sub {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1235
		my $line = shift || $_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1236
		return unless $line =~ /^\w/; # ignore multiline
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1237
		return if $line	 =~ /^\#/; # ignore comments
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1238
		my ( $attr, $val ) = ( $1, $2 ) if $line =~ /^(.+?): (.*)$/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1239
		return if index($attr, ':') != -1;  # ignore base64
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1240
		return ( $attr, $val );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1241
	};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1242
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1243
	my $diff = Algorithm::Diff->new( \@orig_ldif, \@new_ldif );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1244
	HUNK:
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1245
	while ( $diff->Next() ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1246
		next if $diff->Same();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1247
		my $diff_bit = $diff->Diff();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1248
		my %seen_attr;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1249
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1250
		# total deletions
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1251
		if ( $diff_bit == 1 ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1252
			foreach ( $diff->Items(1) ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1253
				next unless /\w+/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1254
				$self->debug("DELETE: $_");
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1255
				my ( $attr, $val ) = $parse->( $_ ) or next;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1256
				$e->delete( $attr => [ $val ] );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1257
			}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1258
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1259
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1260
		# new insertions
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1261
		if ( $diff_bit == 2 ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1262
			foreach ( $diff->Items(2) ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1263
				next unless /\w+/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1264
				$self->debug("INSERT: $_");
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1265
				my ( $attr, $val ) = $parse->( $_ ) or next;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1266
				$e->add( $attr => $val );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1267
			}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1268
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1269
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1270
		# replacements
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1271
		# these are trickier with multivalue lines
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1272
		if ( $diff_bit == 3 ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1273
			foreach ( $diff->Items(2) ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1274
				next unless /\w+/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1275
				$self->debug("MODIFY: $_");
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1276
				my ( $attr, $val ) = $parse->( $_ ) or next;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1277
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1278
				my $cur_vals = $e->get_value( $attr, asref => 1 ) || [];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1279
				my $cur_valcount = scalar @$cur_vals;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1280
				next if $cur_valcount == 0; # should have been an 'add'
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1281
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1282
				# replace immediately 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1283
				#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1284
				if ( $cur_valcount == 1 ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1285
					$e->replace( $attr => $val );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1286
				}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1287
				else {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1288
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1289
					# make sure the replace doesn't squash
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1290
					# other attributes listed with the same name
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1291
					#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1292
					next if $seen_attr{ $attr };
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1293
					my @new_vals;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1294
					foreach my $line ( @new_ldif ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1295
						my ( $new_attr, $new_val ) = $parse->( $line ) or next;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1296
						next unless $new_attr eq $attr;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1297
						$seen_attr{ $attr }++;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1298
						push @new_vals, $new_val;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1299
					}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1300
					$e->replace( $attr => \@new_vals );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1301
				}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1302
			}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1303
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1304
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1305
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1306
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1307
	unlink $self->{'ldif_fname'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1308
	my $rv = $e->update( $self->ldap );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1309
	print $rv->error(), "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1310
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1311
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1312
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1313
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1314
sub run_env
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1315
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1316
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1317
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1318
	foreach ( sort @{ $self->{'env'} } ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1319
		print "$_: ";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1320
		print $conf->{$_} ? $conf->{$_} : 0; 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1321
		print "\n"
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1322
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1323
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1324
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1325
sub run_grep
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1326
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1327
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1328
	my ( $recurse, $filter, $base ) = @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1329
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1330
	# set 'recursion'
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1331
	unless ( $recurse && $recurse =~ /\-r|recurse/ ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1332
		# shift args to the left
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1333
		( $recurse, $filter, $base ) = ( undef, $recurse, $filter );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1334
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1335
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1336
	$filter = Net::LDAP::Filter->new( $filter );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1337
	unless ( $filter ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1338
		print "Invalid search filter.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1339
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1340
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1341
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1342
	# support '*'
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1343
	$base = $self->base() if ! $base or $base eq '*';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1344
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1345
	unless ( $base ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1346
		print "No search base specified.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1347
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1348
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1349
	$self->rdn_to_dn( \$base );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1350
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1351
	$self->debug("Filter parsed as: " . $filter->as_string() . "\n");
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1352
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1353
	my $s = $self->search(
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1354
		{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1355
			scope  => $recurse ? 'sub' : 'one',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1356
			base   => $base,
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1357
			filter => $filter
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1358
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1359
	);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1360
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1361
	foreach my $e ( @{ $s->{'entries'} } ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1362
		my $dn = $e->dn();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1363
		print "$dn\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1364
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1365
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1366
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1367
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1368
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1369
# override internal help functions
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1370
# with pod2usage
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1371
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1372
sub run_help 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1373
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1374
	return Pod::Usage::pod2usage(
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1375
		-exitval  => 'NOEXIT',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1376
		-verbose  => 99,
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1377
		-sections => 'SHELL COMMANDS'
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1378
	);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1379
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1380
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1381
sub run_list
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1382
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1383
	my $self	= shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1384
	my @filters = @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1385
	my $base	= $self->base();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1386
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1387
	# setup filters
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1388
	my ( $flags, $filter );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1389
	if ( scalar @filters ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1390
		# support '-l' or '-R' listings
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1391
		if ( $filters[0] =~ /\-[lR]|verbose/ ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1392
			$flags = shift @filters;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1393
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1394
		
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1395
		$filter = $self->make_filter( \@filters );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1396
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1397
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1398
	# flag booleans
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1399
	my ( $recurse, $long );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1400
	if ( $flags ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1401
		$recurse = $flags =~ /R/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1402
		$long	= $flags =~ /l/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1403
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1404
4
5a65bc849363 Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
  1405
	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
  1406
		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
  1407
		vals   => 1,
5a65bc849363 Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
  1408
		filter => $filter,
5a65bc849363 Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
  1409
		attrs  => [ '*', 'hasSubordinates' ]
5a65bc849363 Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
  1410
	});
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1411
	if ( $s->{'code'} ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1412
		print "$s->{'message'}\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1413
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1414
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1415
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1416
	# if an entry doesn't have a description field,
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1417
	# try and show some nice defaults for ls -l !
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1418
	# 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1419
	# objectClass -> Attribute to show
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1420
	#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1421
	my %descs = %{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1422
		$conf->{'descmaps'}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1423
		  || {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1424
			posixAccount => 'gecos',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1425
			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
  1426
			ipHost	     => 'ipHostNumber',
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1427
		  }
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1428
	  };
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1429
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1430
	# iterate and print
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1431
	#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1432
	my $dn_count = 0;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1433
	my $dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1434
	foreach my $e ( sort { $a->dn() cmp $b->dn() } @{ $s->{'entries'} } ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1435
		$dn = $e->dn();
4
5a65bc849363 Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
  1436
5a65bc849363 Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
  1437
		# 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
  1438
		# trailing slash.
5a65bc849363 Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
  1439
		if ( $e->get_value('hasSubordinates') eq 'TRUE' ) {
5a65bc849363 Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
  1440
			$dn .= '/';
5a65bc849363 Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
  1441
		}
5a65bc849363 Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
  1442
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1443
		my $rdn = $dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1444
		$rdn =~ s/,$base//i;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1445
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1446
		unless ( $long ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1447
			$dn = $rdn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1448
			next;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1449
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1450
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1451
		# show descriptions
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1452
		my $desc = $e->get_value('description');
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1453
		if ( $desc ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1454
			$desc =~ s/\n.*//s; # 1st line only
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1455
			$dn .= " ($desc)";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1456
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1457
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1458
		# no desc?  Try and infer something useful
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1459
		# to display.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1460
		else {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1461
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1462
			# pull objectClasses, hash for lookup speed
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1463
			my @oc   = @{ $e->get_value( 'objectClass', asref => 1 ) || [] };
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1464
			my %ochash;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1465
			map { $ochash{$_} = 1 } @oc;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1466
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1467
			foreach my $d_listing ( sort keys %descs ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1468
				if ( exists $ochash{ $d_listing } ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1469
					my $str = $e->get_value( $descs{ $d_listing }, asref => 1 );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1470
					$dn .= ' (' . (join ', ', @$str) . ')' if $str && scalar @$str;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1471
				}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1472
				next;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1473
			}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1474
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1475
	} 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1476
	continue {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1477
		print "$dn\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1478
		$dn_count++;
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
	print "\n$dn_count " .
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1482
		( $dn_count == 1 ? 'object.' : 'objects.') .
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1483
		"\n" if $long;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1484
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1485
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1486
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1487
sub run_mkdir
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1488
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1489
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1490
	my $dir  = join ' ', @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1491
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1492
	unless ( $dir ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1493
		print "No 'directory' provided.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1494
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1495
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1496
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1497
	# normalize ou name, then pull uniq val back out.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1498
	$dir = "ou=$dir" unless $dir =~ /^ou=/i;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1499
	$self->rdn_to_dn( \$dir );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1500
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1501
	my $ou = $1
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1502
	  if $dir =~ /^[\.\w]+(?:\s+)?=(?:\s+)?([\.\-\s\w]+),?/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1503
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1504
	# add
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1505
	my $r = $self->ldap()->add( $dir, attr => [
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1506
		objectClass => [ 'top', 'organizationalUnit' ],
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1507
		ou		  => $ou,
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1508
	]);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1509
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1510
	print $r->error(), "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1511
	$self->update_entries( clearcache => 1 );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1512
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1513
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1514
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1515
sub run_move
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1516
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1517
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1518
	my ( $s_dn, $d_dn ) = @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1519
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1520
	unless ( $s_dn ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1521
		print "No source dn provided.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1522
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1523
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1524
	unless ( $d_dn ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1525
		print "No destination dn provided.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1526
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1527
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1528
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1529
	my $s_rdn = $s_dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1530
	$self->rdn_to_dn( \$s_dn );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1531
	unless ( $self->is_valid_dn( $s_dn ) ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1532
		print "No such object\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1533
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1534
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1535
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1536
	# see if we're moving the entry to a totally new path
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1537
	my ( $new_dn, $old_dn );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1538
	( $d_dn, $new_dn ) = ( $1, $2 ) if $d_dn =~ /^([\w=]+),(.*)$/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1539
	$old_dn = $1 if $s_dn =~ /^[\w=]+,(.*)$/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1540
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1541
	my $rv = $self->ldap()->moddn(
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1542
		$s_dn,
3
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
  1543
		newrdn	     => $d_dn,
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1544
		deleteoldrdn => 1,
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1545
		newsuperior  => $new_dn
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1546
	);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1547
	print $rv->error(), "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1548
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1549
	# clear caches
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1550
	$self->{'cache'}->{ $new_dn } = {} if $new_dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1551
	$self->{'cache'}->{ $old_dn } = {} if $old_dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1552
	$self->update_entries( clearcache => 1 );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1553
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1554
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1555
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1556
sub run_passwd 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1557
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1558
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1559
	my $dn   = shift || $self->base();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1560
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1561
	$self->{'root_dse'} ||= $self->ldap->root_dse();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1562
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1563
	my $pw_extension = '1.3.6.1.4.1.4203.1.11.1';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1564
	unless ( $self->{'root_dse'}->supported_extension( $pw_extension ) ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1565
		print "Sorry, password changes not supported by LDAP server.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1566
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1567
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1568
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1569
	# support '.'
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1570
	$dn = $self->base() if $dn eq '.';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1571
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1572
	$self->rdn_to_dn( \$dn );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1573
	my $s = $self->search( { base => $dn, scope => 'base' } );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1574
	if ( $s->{'code'} ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1575
		print $s->{'message'}, "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1576
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1577
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1578
	my $e = ${ $s->{'entries'} }[0];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1579
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1580
	unless ( $e->exists('userPassword') ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1581
		print "No userPassword attribute for $dn\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1582
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1583
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1584
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1585
	print "Changing password for $dn\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1586
	Term::ReadKey::ReadMode 2;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1587
	print "Enter new password: ";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1588
	chomp( my $pw  = <STDIN> );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1589
	print "\nRetype new password: ";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1590
	chomp( my $pw2 = <STDIN> );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1591
	print "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1592
	Term::ReadKey::ReadMode 0;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1593
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1594
	if ( $pw ne $pw2 ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1595
		print "Sorry, passwords do not match.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1596
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1597
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1598
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1599
	my $rv = $self->ldap->set_password(
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1600
		user	  => $dn,
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1601
		newpasswd => $pw
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1602
	);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1603
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1604
	if ( $rv->code() == 0 ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1605
		print "Password updated successfully.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1606
	} else {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1607
		print "Password error: " . $rv->error() . "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1608
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1609
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1610
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1611
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1612
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1613
sub run_pwd 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1614
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1615
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1616
	print $self->base() . "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1617
	return;   
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1618
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1619
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1620
sub run_setenv
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1621
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1622
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1623
	my ( $key, $val ) = @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1624
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1625
	( $key, $val ) = split /=/, $key if $key && ! defined $val;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1626
	return unless $key && defined $val;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1627
	$key = lc $key;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1628
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1629
	$conf->{$key} = $val;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1630
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1631
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1632
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1633
sub run_whoami
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1634
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1635
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1636
	print $conf->{'binddn'} || 'anonymous bind';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1637
	print "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1638
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1639
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1640
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1641
###############################################################
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1642
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1643
# MAIN
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1644
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1645
###############################################################
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1646
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1647
package main;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1648
use strict;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1649
use warnings;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1650
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1651
$0 = 'shelldap';
1
66ab8df0b6c8 Bumping to 0.2 release.
mahlon
parents: 0
diff changeset
  1652
my $VERSION = '0.2';
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1653
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1654
use Getopt::Long;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1655
use YAML::Syck;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1656
use Pod::Usage;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1657
eval 'use Term::ReadLine::Gnu';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1658
warn qq{Term::ReadLine::Gnu not installed.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1659
Continuing, but shelldap is of limited usefulness without it.\n\n} if $@;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1660
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1661
# get config - rc file first, command line overrides
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1662
use vars '$conf';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1663
$conf = load_config() || {};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1664
Getopt::Long::GetOptions(
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1665
	$conf, 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1666
	'server=s',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1667
	'binddn=s',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1668
	'basedn=s',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1669
	'cacheage=i',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1670
	'timeout=i',
3
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
  1671
	'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
  1672
	'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
  1673
	'tls_key=s',
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1674
	'tls', 'debug',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1675
	 help => sub {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1676
		Pod::Usage::pod2usage(
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1677
			-verbose => 1,
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1678
			-message => "\n$0 command line flags\n" . '-' x 65
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1679
		);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1680
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1681
);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1682
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1683
# defaults
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1684
$conf->{'confpath'} = "$ENV{'HOME'}/.shelldap.rc";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1685
$conf->{'cacheage'} ||= 300;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1686
$conf->{'timeout'}  ||= 10;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1687
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1688
# create and enter shell loop
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1689
my $shell = LDAP::Shell->new();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1690
$shell->cmdloop();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1691
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1692
# load YAML config into global conf.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1693
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1694
sub load_config
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1695
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1696
	my ( $d, $data );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1697
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1698
	my $confpath;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1699
	my @confs = (
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1700
		"$ENV{'HOME'}/.shelldap.rc",
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1701
		'/usr/local/etc/shelldap.conf',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1702
		'/etc/shelldap.conf',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1703
	);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1704
	foreach ( @confs ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1705
		if ( -e $_ ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1706
			$confpath = $_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1707
			last;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1708
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1709
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1710
	$confpath or return undef;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1711
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1712
	open YAML, $confpath or return undef;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1713
	do {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1714
		local $/ = undef;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1715
		$data = <YAML>;  # slurp!
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1716
	};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1717
	close YAML;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1718
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1719
	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
  1720
	die "Invalid YAML in $confpath\n" if $@;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1721
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1722
	return $conf;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1723
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1724
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1725
## EOF
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1726