shelldap
author Uwe Kleine-König <uwe@kleine-koenig.org>
Sun, 13 Oct 2019 10:19:40 -0700
changeset 130 af3c3c0a1f3b
parent 127 60516e84a813
child 131 3b6cb1117ffb
permissions -rwxr-xr-x
Don't provide a password for sasl authentication. When a sasl parameter is given (and used) the password parameter is not used by Net::LDAP. If indeed a password is required it has to be passed in the Authen::SASL object, not as parameter to bind. So drop the password parameter which stops trying (and failing) to use password authentication sometimes if in Net/LDAP.pm the iteration over %ptype hits its 'password' member before the 'sasl' member.
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
#
99
2a7a3072d76c Add LICENSE file for packaging, bump copyright.
Mahlon E. Smith <mahlon@martini.nu>
parents: 95
diff changeset
     4
# Copyright (c) 2006-2019, Mahlon E. Smith <mahlon@martini.nu>
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
     5
# All rights reserved.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
     6
# Redistribution and use in source and binary forms, with or without
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
     7
# modification, are permitted provided that the following conditions are met:
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
     8
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
     9
#     * Redistributions of source code must retain the above copyright
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    10
#       notice, this list of conditions and the following disclaimer.
6
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
    11
#
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    12
#     * Redistributions in binary form must reproduce the above copyright
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    13
#       notice, this list of conditions and the following disclaimer in the
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    14
#       documentation and/or other materials provided with the distribution.
6
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
    15
#
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    16
#     * Neither the name of Mahlon E. Smith nor the names of his
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    17
#       contributors may be used to endorse or promote products derived
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    18
#       from this software without specific prior written permission.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    19
#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    20
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    21
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    22
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    23
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    24
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    25
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    26
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    27
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    28
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    29
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    30
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    31
=head1 NAME
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    32
10
664bbe3dcd44 Follow regular man page conventions. Patch from
Mahlon E. Smith <mahlon@laika.com>
parents: 9
diff changeset
    33
Shelldap - A program for interacting with an LDAP server via a shell-like interface
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    34
10
664bbe3dcd44 Follow regular man page conventions. Patch from
Mahlon E. Smith <mahlon@laika.com>
parents: 9
diff changeset
    35
=head1 DESCRIPTION
664bbe3dcd44 Follow regular man page conventions. Patch from
Mahlon E. Smith <mahlon@laika.com>
parents: 9
diff changeset
    36
664bbe3dcd44 Follow regular man page conventions. Patch from
Mahlon E. Smith <mahlon@laika.com>
parents: 9
diff changeset
    37
Shelldap /LDAP::Shell is a program for interacting with an LDAP server via a shell-like
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    38
interface.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    39
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    40
This is not meant to be an exhaustive LDAP editing and browsing
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    41
interface, but rather an intuitive shell for performing basic LDAP
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    42
tasks quickly and with minimal effort.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    43
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    44
=head1 SYNPOSIS
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    45
3
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    46
 shelldap --server example.net [--help]
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    47
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    48
=head1 FEATURES
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    49
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    50
 - Upon successful authenticated binding, credential information is
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    51
   auto-cached to ~/.shelldap.rc -- future loads require no command line
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    52
   flags.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    53
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    54
 - Custom 'description maps' for entry listings.  (See the 'list' command.)
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    55
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    56
 - History and autocomplete via readline, if installed.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    57
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    58
 - Automatic reconnection attempts if the connection is lost with the
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    59
   LDAP server.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    60
51
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
    61
 - Basic schema introspection for quick reference.
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
    62
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    63
 - It feels like a semi-crippled shell, making LDAP browsing and editing
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    64
   at least halfway pleasurable.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    65
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    66
=head1 OPTIONS
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    67
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    68
All command line options follow getopts long conventions.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    69
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    70
    shelldap --server example.net --basedn dc=your,o=company
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    71
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    72
You may also optionally create a ~/.shelldap.rc file with command line
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    73
defaults.  This file should be valid YAML.  (This file is generated
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    74
automatically on a successful bind auth.)
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    75
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    76
Example:
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    77
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    78
    server: ldap.example.net
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    79
    binddn: cn=Manager,dc=your,o=company
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    80
    bindpass: xxxxxxxxx
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    81
    basedn: dc=your,o=company
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    82
    tls: yes
3
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    83
    tls_cacert: /etc/ssl/certs/cacert.pem
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    84
    tls_cert:   ~/.ssl/client.cert.pem 
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    85
    tls_key:    ~/.ssl/private/client.key.pem
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    86
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    87
=over 4
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
    88
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
    89
=item B<configfile>
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
    90
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
    91
Optional.  Use an alternate configuration file, instead of the
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
    92
default ~/.shelldap.rc.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
    93
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
    94
    --configfile /tmp/alternate-config.yml
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
    95
    -f /tmp/alternate-config.yml
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
    96
51
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
    97
This config file overrides values found in the default config, so
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
    98
you can easily have separate config files for connecting to your
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
    99
cn=monitor or cn=log overlays (for example.)
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   100
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   101
=back
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   102
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   103
=over 4
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   104
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   105
=item B<server>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   106
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   107
Required. The LDAP server to connect to.  This can be a hostname, IP
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   108
address, or a URI.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   109
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   110
    --server ldaps://ldap.example.net
19
18e71da965ff Add documentation for the additional short flags.
Mahlon E. Smith <mahlon@martini.nu>
parents: 18
diff changeset
   111
    -H ldaps://ldap.example.net
117
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   112
    -h hostname_or_IP
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   113
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   114
=back
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   115
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   116
=over 4
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   117
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   118
=item B<binddn>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   119
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   120
The full dn of a user to authenticate as.  If not specified, defaults to
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   121
an anonymous bind.  You will be prompted for a password.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   122
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   123
    --binddn cn=Manager,dc=your,o=company
19
18e71da965ff Add documentation for the additional short flags.
Mahlon E. Smith <mahlon@martini.nu>
parents: 18
diff changeset
   124
    -D cn=Manager,dc=your,o=company
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   125
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   126
=back
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   127
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   128
=over 4
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   129
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   130
=item B<basedn>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   131
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   132
The directory 'root' of your LDAP server.  If omitted, shelldap will
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   133
try and ask the server for a sane default.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   134
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   135
    --basedn dc=your,o=company
19
18e71da965ff Add documentation for the additional short flags.
Mahlon E. Smith <mahlon@martini.nu>
parents: 18
diff changeset
   136
    -b dc=your,o=company
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   137
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   138
=back
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   139
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   140
=over 4
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   141
92
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   142
=item B<paginate>
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   143
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   144
Integer.  If enabled, shelldap will attempt to use server side
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   145
pagination to build listings.  Note: if you're using this to avoid
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   146
sizelimit errors, you'll likely need server configuration to raise the
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   147
limits for paginated results.
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   148
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   149
  --paginate 100
92
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   150
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   151
=back
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   152
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   153
=over 4
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   154
42
b8c6d4e8f828 Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents: 41
diff changeset
   155
=item B<promptpass>
b8c6d4e8f828 Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents: 41
diff changeset
   156
b8c6d4e8f828 Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents: 41
diff changeset
   157
Force password prompting.  Useful to temporarily override cached
b8c6d4e8f828 Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents: 41
diff changeset
   158
credentials.
b8c6d4e8f828 Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents: 41
diff changeset
   159
b8c6d4e8f828 Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents: 41
diff changeset
   160
=back
b8c6d4e8f828 Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents: 41
diff changeset
   161
66
5a0c99ca0c0d Fix the pod so it can build without complaint under perl 5.20, bump to
Mahlon E. Smith <mahlon@martini.nu>
parents: 64
diff changeset
   162
=over 4
5a0c99ca0c0d Fix the pod so it can build without complaint under perl 5.20, bump to
Mahlon E. Smith <mahlon@martini.nu>
parents: 64
diff changeset
   163
62
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   164
=item B<sasl>
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   165
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   166
A space separated list of SASL mechanisms.  Requires the Authen::SASL
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   167
module.
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   168
117
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   169
    --sasl 'PLAIN CRAM-MD5 GSSAPI'
62
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   170
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   171
=back
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   172
42
b8c6d4e8f828 Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents: 41
diff changeset
   173
=over 4
b8c6d4e8f828 Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents: 41
diff changeset
   174
b8c6d4e8f828 Add a flag to force a password prompt, so you can override credentials
Mahlon E. Smith <mahlon@laika.com>
parents: 41
diff changeset
   175
=item B<tls>
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   176
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   177
Enables TLS over what would normally be an insecure connection.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   178
Requires server side support.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   179
3
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   180
=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
   181
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   182
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
   183
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   184
    --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
   185
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   186
=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
   187
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   188
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
   189
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   190
    --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
   191
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   192
=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
   193
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   194
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
   195
key verification.
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   196
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   197
    --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
   198
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   199
=back
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   200
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   201
=over 4
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   202
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   203
=item B<cacheage>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   204
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   205
Set the time to cache directory lookups in seconds.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   206
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   207
By default, directory lookups are cached for 300 seconds, to speed
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   208
autocomplete up when changing between different basedns.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   209
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   210
Modifications to the directory automatically reset the cache.  Directory
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   211
listings are not cached.  (This is just used for autocomplete.)  Set it
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   212
to 0 to disable caching completely.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   213
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   214
=back
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   215
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   216
=over 4
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   217
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   218
=item B<timeout>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   219
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   220
Set the maximum time an LDAP operation can take before it is cancelled.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   221
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   222
=back
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   223
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   224
=over 4
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   225
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   226
=item B<debug>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   227
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   228
Print extra operational info out, and backtrace on fatal error.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   229
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   230
=back
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   231
37
7a8855e7cfb8 Add a --version flag.
Mahlon E. Smith <mahlon@martini.nu>
parents: 36
diff changeset
   232
=over 4
7a8855e7cfb8 Add a --version flag.
Mahlon E. Smith <mahlon@martini.nu>
parents: 36
diff changeset
   233
7a8855e7cfb8 Add a --version flag.
Mahlon E. Smith <mahlon@martini.nu>
parents: 36
diff changeset
   234
=item B<version>
7a8855e7cfb8 Add a --version flag.
Mahlon E. Smith <mahlon@martini.nu>
parents: 36
diff changeset
   235
7a8855e7cfb8 Add a --version flag.
Mahlon E. Smith <mahlon@martini.nu>
parents: 36
diff changeset
   236
Display the version number.
7a8855e7cfb8 Add a --version flag.
Mahlon E. Smith <mahlon@martini.nu>
parents: 36
diff changeset
   237
7a8855e7cfb8 Add a --version flag.
Mahlon E. Smith <mahlon@martini.nu>
parents: 36
diff changeset
   238
=back
7a8855e7cfb8 Add a --version flag.
Mahlon E. Smith <mahlon@martini.nu>
parents: 36
diff changeset
   239
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   240
=head1 SHELL COMMANDS
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   241
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   242
=head2 alias
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   243
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   244
Define or display aliases.
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   245
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   246
Without arguments, `alias' prints the list of aliases in the reusable
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   247
form `alias NAME=VALUE' on standard output.
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   248
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   249
Otherwise, an alias is defined for each NAME whose VALUE is given.
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   250
A trailing space in VALUE causes the next word to be checked for
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   251
alias substitution when the alias is expanded.
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   252
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   253
    alias
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   254
    alias ll=ls -al
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   255
    alias ll
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   256
    alias show=cat
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   257
    alias cmd1=command 'arg with spaces'
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   258
    alias cmd2='command '
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   259
    alias cmd2=command 'with_arg '
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   260
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   261
=head2 cat
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   262
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   263
Print contents of LDAP entry to STDOUT in LDIF format.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   264
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   265
Globbing is supported. Specify either full DN, or a RDN.
105
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   266
RDNs are local to the current search base ('cwd' in shell terms). If RDN is '.' or missing,
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   267
it defaults to the current search base.
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   268
You may additionally add a list of attributes to display (e.g. use '+' for operational
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   269
attributes or provide a specific space-separated list). Default list of attributes is ['*']
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   270
and this default list can be changed using 'attributes' config key or --attributes cmdline
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   271
option.
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   272
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   273
    cat uid=mahlon
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   274
    cat ou=*
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   275
    cat uid=mahlon,ou=People,dc=example,o=company
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   276
    cat uid=mahlon + userPassword
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   277
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   278
=head2 configfile
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   279
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   280
Load or save config file.
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   281
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   282
If no config file is specified as argument to 'load', the default search list is:
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   283
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   284
    $HOME/.shelldap.rc
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   285
    /usr/local/etc/shelldap.conf
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   286
    /etc/shelldap.conf
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   287
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   288
If no config file is specified as argument to 'save', the default path is
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   289
$HOME/.shelldap.rc.
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   290
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   291
    configfile load
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   292
    configfile load /path/to/config
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   293
    configfile save
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   294
    configfile save /path/to/config
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   295
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   296
=head2 less
72
39e9f802eb40 Add a 'less' command, that does the same as cat, but uses a pager.
Dennis Kaarsemaker <dennis.kaarsemaker@booking.com>
parents: 71
diff changeset
   297
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   298
Like 'cat', but use configured pager to paginate output.
72
39e9f802eb40 Add a 'less' command, that does the same as cat, but uses a pager.
Dennis Kaarsemaker <dennis.kaarsemaker@booking.com>
parents: 71
diff changeset
   299
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   300
=head2 cd
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   301
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   302
Change the working directory (LDAP search base).
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   303
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   304
Translated to LDAP, this changes the current basedn.
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   305
All commands after a 'cd' operate within the new basedn.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   306
105
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   307
    cd                  change to 'home' (binddn if any, or basedn)
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   308
    cd ~                change to 'home' (binddn if any, or basedn)
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
   309
    cd -                change to previous node
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
   310
    cd ou=People        change to explicit path below current node
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
   311
    cd ..               change to parent node
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
   312
    cd ../../ou=Groups  change to node ou=Groups, which is a sibling
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
   313
                        to the current node's grandparent
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   314
105
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   315
Since LDAP doesn't limit what can be a container object, you can 'cd' into
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   316
any entry. Many commands then work on '.' or default to '.', meaning
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   317
"wherever I currently am."
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   318
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   319
    cd uid=mahlon
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   320
    cat .
105
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   321
    cat
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   322
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   323
=head2 clear
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   324
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   325
Clear the terminal screen.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   326
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   327
Clears screen similar to 'clear' or Ctrl+l on the shell command line.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   328
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   329
Ctrl+l alias is also supported.
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   330
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   331
=head2 copy
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   332
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   333
Copy an entry.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   334
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   335
All copies are relative to the
105
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   336
current basedn unless a full DN is specified.  All attributes are
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   337
copied and then an LDAP moddn() is performed.
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   338
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   339
    copy uid=mahlon uid=bob
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   340
    copy uid=mahlon ou=Others,dc=example,o=company
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   341
    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
   342
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   343
=head2 create
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   344
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   345
Create an entry.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   346
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   347
Arguments are space separated objectClass
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   348
names.  Possible objectClasses are derived automatically from the
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   349
server, and will tab-complete.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   350
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   351
After the classes are specified, an editor will launch.  Required
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   352
attributes are listed first, then optional attributes.  Optionals are
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   353
commented out.  After the editor exits, the resulting LDIF is validated
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   354
and added to the LDAP directory.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   355
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   356
    create top person organizationalPerson inetOrgPerson posixAccount
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   357
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   358
=head2 delete
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   359
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   360
Remove an entry.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   361
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   362
Globbing is supported.
67
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
   363
All deletes are sanity-prompted.  The -v flag prints the entries out
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
   364
for review before delete.
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   365
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   366
    delete uid=mahlon
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   367
    delete uid=ma*
67
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
   368
    rm -v uid=mahlon,ou=People,dc=example,o=company l=office
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   369
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   370
=head2 edit
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   371
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   372
Edit an entry in an external editor.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   373
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   374
After the editor exits, the
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   375
resulting LDIF is sanity checked, and changes are written to the LDAP
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   376
directory.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   377
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   378
    edit uid=mahlon
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   379
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   380
=head2 env
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   381
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   382
Print values of configurable shelldap variables.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   383
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   384
This is a subset of all variables configurable via shelldap config
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   385
file and/or its command line options.
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   386
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   387
=head2 grep
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   388
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   389
Search using LDAP filters and return matching DN results.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   390
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   391
The search string must be a valid LDAP filter.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   392
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   393
    grep uid=mahlon
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   394
    grep uid=mahlon ou=People
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   395
    grep -r (&(uid=mahlon)(objectClass=*))
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   396
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   397
=head2 inspect
51
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   398
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   399
View schema and flags for an entry or objectClass.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   400
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   401
It also includes the most common flags for the objectClass
51
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   402
attributes.
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   403
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   404
    inspect uid=mahlon
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   405
    inspect posixAccount organizationalUnit
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   406
    inspect _schema
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   407
91
80ec00959fbd Fix spelling errors
Salvatore Bonaccorso <carnil@debian.org>
parents: 87
diff changeset
   408
The output is a list of found objectClasses, their schema hierarchy
51
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   409
(up to 'top'), whether or not they are a structural class, and then
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   410
a merged list of all valid attributes for the given objectClasses.
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   411
Attributes are marked as either required or optional, and whether
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   412
they allow multiple values or not.
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   413
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   414
If you ask for the special "_schema" object, the raw server schema
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   415
is dumped to screen.
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   416
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   417
=head2 list
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   418
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   419
List directory contents.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   420
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   421
Globbing is supported.
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   422
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   423
    ls -l
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   424
    ls -lR uid=mahlon
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   425
    list uid=m*
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   426
36
2e78218b8045 Small documentation fixes, add better verbosity when saving connection
Mahlon E. Smith <mahlon@martini.nu>
parents: 35
diff changeset
   427
In 'long' mode, descriptions are listed as well, if they exist.
2e78218b8045 Small documentation fixes, add better verbosity when saving connection
Mahlon E. Smith <mahlon@martini.nu>
parents: 35
diff changeset
   428
There are some default 'long listing' mappings for common objectClass
2e78218b8045 Small documentation fixes, add better verbosity when saving connection
Mahlon E. Smith <mahlon@martini.nu>
parents: 35
diff changeset
   429
types.  You can additionally specify your own mappings in your
2e78218b8045 Small documentation fixes, add better verbosity when saving connection
Mahlon E. Smith <mahlon@martini.nu>
parents: 35
diff changeset
   430
.shelldap.rc, like so:
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   431
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   432
    ...
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   433
    descmaps:
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   434
        objectClass: attributename
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   435
        posixAccount: gecos
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   436
        posixGroup: gidNumber
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   437
        ipHost: ipHostNumber
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   438
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   439
=head2 mkdir
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   440
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   441
Create a new 'organizationalUnit' LDAP entry.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   442
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   443
  mkdir containername
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   444
  mkdir ou=whatever
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   445
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   446
=head2 move
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   447
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   448
Move (rename) entry.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   449
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   450
Usage is identical to B<copy>.
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   451
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   452
=head2 passwd
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   453
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   454
Change user password.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   455
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   456
If supported server side, change the password for a specified entry.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   457
The entry must have a 'userPassword' attribute.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   458
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   459
    passwd uid=mahlon
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   460
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   461
=head2 pwd
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   462
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   463
Print name of current/working LDAP search base.
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   464
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   465
=head2 setenv
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   466
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   467
Change or define shelldap variable.
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   468
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   469
    setenv debug 1
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   470
    export debug=1
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   471
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   472
=head2 unalias
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   473
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   474
Remove each NAME from the list of defined aliases.
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   475
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   476
    alias ll=ls -al
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   477
    alias
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   478
    unalias ll
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   479
    unalias ll ls
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   480
    alias
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
   481
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
   482
=head2 unsetenv
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
   483
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
   484
Remove each NAME from the list of defined shelldap variables.
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
   485
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
   486
    unset debug
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
   487
    unset configfile
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
   488
    unset myvar1 myvar2 myvar3
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
   489
103
20752fcf201c Improve 'help' command
Davor Ocelic <docelic@crystallabs.io>
parents: 102
diff changeset
   490
=head2 whoami
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   491
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   492
Print current bind DN.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   493
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   494
Show current auth credentials.  Unless you specified a binddn, this
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   495
will just show an anonymous bind.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   496
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   497
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   498
=head1 TODO
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   499
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   500
Referral support.  Currently, if you try to write to a replicant slave,
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   501
you'll just get a referral.  It would be nice if shelldap automatically
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   502
tried to follow it.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   503
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   504
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
   505
any writes.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   506
105
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   507
Add ability for command definitions in cmd_map to contain default
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   508
arguments passed to functions.
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   509
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   510
Then add ability to define custom commands/aliases in config file.
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   511
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   512
Split 'inspect' into separate commands, one working on files/entries,
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   513
and one working on objectclasses. This way, autocompleter for both commands
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   514
will be reasonable, unlike now.
e7e850a1540c Improve command descriptions
Davor Ocelic <docelic@crystallabs.io>
parents: 104
diff changeset
   515
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   516
=head1 BUGS / LIMITATIONS
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   517
6
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
   518
There is no support for editing binary data.  If you need to edit base64
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
   519
stuff, just feed it to the regular ldapmodify/ldapadd/etc tools.
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   520
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   521
=head1 AUTHOR
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   522
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   523
Mahlon E. Smith <mahlon@martini.nu>
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   524
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   525
=cut
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   526
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   527
package LDAP::Shell;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   528
use strict;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   529
use warnings;
108
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
   530
use Term::ReadKey qw//;
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
   531
use Term::Shell qw//;
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
   532
use Digest::MD5 qw//;
48
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
   533
use Net::LDAP qw/
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
   534
	LDAP_SUCCESS
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
   535
	LDAP_SERVER_DOWN
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
   536
	LDAP_OPERATIONS_ERROR
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
   537
	LDAP_TIMELIMIT_EXCEEDED
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
   538
	LDAP_BUSY
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
   539
	LDAP_UNAVAILABLE
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
   540
	LDAP_OTHER
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
   541
	LDAP_TIMEOUT
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
   542
	LDAP_NO_MEMORY
93
4c7843b9a047 Make password extension usage consistent with pager control.
Mahlon E. Smith <mahlon@martini.nu>
parents: 92
diff changeset
   543
	LDAP_EXTENSION_PASSWORD_MODIFY
92
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   544
	LDAP_CONNECT_ERROR
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   545
	LDAP_CONTROL_PAGED /;
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
   546
use Net::LDAP::Util qw/ canonical_dn ldap_explode_dn /;
108
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
   547
use Net::LDAP::LDIF qw//;
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
   548
use Net::LDAP::Extension::SetPassword qw//;
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
   549
use Net::LDAP::Control::Paged qw//;
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
   550
use Data::Dumper qw//;
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
   551
use File::Temp qw//;
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
   552
use Algorithm::Diff qw//;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   553
use Carp 'confess';
108
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
   554
use POSIX qw//;
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   555
use Tie::IxHash qw//;
118
2d7f16eff198 Use local implementation of slurp()
Davor Ocelic <docelic@crystallabs.io>
parents: 117
diff changeset
   556
use Fatal qw/open/;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   557
use base 'Term::Shell';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   558
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   559
my $conf = $main::conf;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   560
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   561
# make 'die' backtrace in debug mode
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   562
$SIG{'__DIE__'} = \&Carp::confess if $conf->{'debug'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   563
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   564
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   565
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   566
########################################################################
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   567
### Term::Shell Fixes
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   568
########################################################################
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   569
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   570
# Term::Shell function add_handlers() is implemented in an incorrect way.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   571
# We reimplement the method here to fix its problems.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   572
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   573
# In add_handlers, we split searching for aliases in a separate loop,
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   574
# because otherwise not all aliases are registered before we look them
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   575
# up.
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   576
sub add_handlers
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   577
{
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   578
    my $o = shift;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   579
    for my $hnd (@_)
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   580
    {
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   581
        next unless $hnd =~ /^(run|help|smry|comp|catch|alias)_/o;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   582
        my $t = $1;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   583
        my $a = substr( $hnd, length($t) + 1 );
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   584
 
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   585
        # Add on the prefix and suffix if the command is defined
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   586
        if ( length $a )
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   587
        {
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   588
            substr( $a, 0, 0 ) = $o->cmd_prefix;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   589
            $a .= $o->cmd_suffix;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   590
        }
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   591
        $o->{handlers}{$a}{$t} = $hnd;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   592
    }
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   593
    for my $hnd (@_)
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   594
    {
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   595
        next unless $hnd =~ /^(run|help|smry|comp|catch|alias)_/o;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   596
        my $t = $1;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   597
        my $a = substr( $hnd, length($t) + 1 );
112
32f396697fb3 Add missing block to Term::Shell fix
Davor Ocelic <docelic@crystallabs.io>
parents: 111
diff changeset
   598
 
32f396697fb3 Add missing block to Term::Shell fix
Davor Ocelic <docelic@crystallabs.io>
parents: 111
diff changeset
   599
        # Add on the prefix and suffix if the command is defined
32f396697fb3 Add missing block to Term::Shell fix
Davor Ocelic <docelic@crystallabs.io>
parents: 111
diff changeset
   600
        if ( length $a )
32f396697fb3 Add missing block to Term::Shell fix
Davor Ocelic <docelic@crystallabs.io>
parents: 111
diff changeset
   601
        {
32f396697fb3 Add missing block to Term::Shell fix
Davor Ocelic <docelic@crystallabs.io>
parents: 111
diff changeset
   602
            substr( $a, 0, 0 ) = $o->cmd_prefix;
32f396697fb3 Add missing block to Term::Shell fix
Davor Ocelic <docelic@crystallabs.io>
parents: 111
diff changeset
   603
            $a .= $o->cmd_suffix;
32f396697fb3 Add missing block to Term::Shell fix
Davor Ocelic <docelic@crystallabs.io>
parents: 111
diff changeset
   604
        }
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   605
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   606
        if ( $o->has_aliases($a) )
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   607
        {
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   608
            my @a = $o->get_aliases($a);
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   609
            for my $alias (@a)
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   610
            {
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   611
                substr( $alias, 0, 0 ) = $o->cmd_prefix;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   612
                $alias .= $o->cmd_suffix;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   613
                $o->{handlers}{$alias}{$t} = $hnd;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   614
            }
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   615
        }
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   616
    }
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   617
}
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   618
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   619
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
   620
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   621
########################################################################
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   622
### U T I L I T Y   F U N C T I O N S
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   623
########################################################################
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   624
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   625
### Initial shell behaviors.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   626
### 
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   627
sub init
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   628
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   629
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   630
	$self->{'API'}->{'match_uniq'} = 0;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   631
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   632
	$self->{'editor'} = $conf->{'editor'} || $ENV{'EDITOR'} || 'vi';
73
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   633
	$self->{'pager'}  = $conf->{'pager'}  || $ENV{'PAGER'}  || 'less';
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
   634
	$self->{'env'}	  = [ qw/ debug cacheage timeout attributes configfile / ];
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   635
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   636
	# let autocomplete work with the '=' character
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   637
	my $term = $self->term();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   638
	$term->Attribs->{'basic_word_break_characters'}	 =~ s/=//m;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   639
	$term->Attribs->{'completer_word_break_characters'} =~ s/=//m;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   640
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   641
	# read in history
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   642
	eval {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   643
		$term->history_truncate_file("$ENV{'HOME'}/.shelldap_history", 50);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   644
		$term->ReadHistory("$ENV{'HOME'}/.shelldap_history");
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   645
	};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   646
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   647
	# gather metadata from the LDAP server
57
85cc85d0c1b1 Catch a case where the LDAP object is defined, but in a state that
Mahlon E. Smith <mahlon@laika.com>
parents: 56
diff changeset
   648
	$self->{'root_dse'} = $self->ldap->root_dse() or
85cc85d0c1b1 Catch a case where the LDAP object is defined, but in a state that
Mahlon E. Smith <mahlon@laika.com>
parents: 56
diff changeset
   649
		die "Unable to retrieve LDAP server information.  (Doublecheck connection arguments.)\n";
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   650
	$self->{'schema'} = $self->ldap->schema();
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   651
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   652
	# get an initial list of all objectClasses
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   653
	$self->{'objectclasses'} = [];
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   654
	foreach my $o ( $self->{'schema'}->all_objectclasses() ) {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   655
		push @{ $self->{'objectclasses'} }, $o->{'name'};
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   656
	}
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   657
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   658
	if ( $conf->{'debug'} ) {
17
669085d93aa3 simplify over-complex call of N:L:E->get_value()
Peter Marschall <peter@adpm.de>
parents: 16
diff changeset
   659
		my @versions = $self->{'root_dse'}->get_value('supportedLDAPVersion');
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   660
		print "Connected to $conf->{'server'}\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   661
		print "Supported LDAP version: ", ( join ', ', @versions ), "\n";
117
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   662
		print "Cipher in use: ", $self->ldap()->cipher() || '', "\n";
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   663
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   664
92
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   665
	# check for the pagination extension on the server early, and bail
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   666
	# if necessary.
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   667
	if ( $conf->{'paginate'} && $conf->{'paginate'} =~ /^\d+$/ && $conf->{'paginate'} > 0 ) {
93
4c7843b9a047 Make password extension usage consistent with pager control.
Mahlon E. Smith <mahlon@martini.nu>
parents: 92
diff changeset
   668
		unless ( $self->{'root_dse'}->supported_control(LDAP_CONTROL_PAGED) ) {
4c7843b9a047 Make password extension usage consistent with pager control.
Mahlon E. Smith <mahlon@martini.nu>
parents: 92
diff changeset
   669
			die "Server pagination is enabled, but the server doesn't seem to support it.\n";
4c7843b9a047 Make password extension usage consistent with pager control.
Mahlon E. Smith <mahlon@martini.nu>
parents: 92
diff changeset
   670
		}
92
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   671
	}
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   672
	else {
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   673
		$conf->{'paginate'} = undef;
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   674
	}
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
   675
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   676
	# try an initial search and bail early if it doesn't work. (bad baseDN?)
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   677
	my $s = $self->search();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   678
	die "LDAP baseDN error: ", $s->{'message'}, "\n" if $s->{'code'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   679
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   680
	# okay, now do an initial population of 'cwd' for autocomplete.
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   681
	$self->update_entries();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   682
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   683
	# whew, okay.  Update prompt, wait for input!
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   684
	$self->update_prompt();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   685
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   686
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   687
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   688
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   689
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   690
### Return an LDAP connection handle, creating it if necessary.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   691
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   692
sub ldap
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   693
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   694
	my $self = shift;
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   695
	my $rv;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   696
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   697
	# use cached connection object if it exists
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   698
	return $self->{'ldap'} if $self->{'ldap'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   699
	
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   700
	# fill in potentially missing info
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   701
	die "No server specified.\n" unless $conf->{'server'};
15
f6157d378459 Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   702
f6157d378459 Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   703
	# Emit a nicer error message if IO::Socket::SSL is
f6157d378459 Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   704
	# not installed and Net::LDAP decides it is required.
f6157d378459 Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   705
	#
f6157d378459 Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   706
	if ( $conf->{'tls'} || $conf->{'server'} =~ m|ldaps://| ) {
87
475aff91675b eval optional modules in blocks instead of strings.
Mahlon E. Smith <mahlon@martini.nu>
parents: 84
diff changeset
   707
		eval { require IO::Socket::SSL; };
15
f6157d378459 Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   708
		die qq{IO::Socket::SSL not installed, but is required for SSL or TLS connections.
f6157d378459 Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   709
You may try connecting insecurely, or install the module and try again.\n} if $@;
f6157d378459 Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   710
	}
f6157d378459 Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   711
117
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   712
	if ($conf->{'binddn'}) {
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   713
		if($conf->{'promptpass'}) {
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   714
			# Prompt for a password after disabling local echo.
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   715
			#
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   716
			print "Bind password: ";
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   717
			Term::ReadKey::ReadMode 2;
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   718
			chomp( $conf->{'bindpass'} = <STDIN> );
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   719
			Term::ReadKey::ReadMode 0;
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   720
			print "\n";
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   721
		} elsif($conf->{'pass'}) {
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   722
			$conf->{'bindpass'} = $conf->{'pass'}
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   723
		} elsif($conf->{'passfile'}) {
118
2d7f16eff198 Use local implementation of slurp()
Davor Ocelic <docelic@crystallabs.io>
parents: 117
diff changeset
   724
			chomp( $conf->{'bindpass'} = slurp($conf->{'passfile'}));
117
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   725
		}
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   726
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   727
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   728
	# make the connection
117
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   729
	my $ldap = Net::LDAP->new( $conf->{'server'}, $conf->{port} ? ('port' => $conf->{port}) : ())
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
   730
		or die "Unable to connect to LDAP server '$conf->{'server'}' port ${\( $conf->{port} || 'default' )}: $!\n";
3
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   731
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   732
	# secure connection options
15
f6157d378459 Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   733
	#
f6157d378459 Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   734
	if ( $conf->{'tls'} )  {
3
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   735
		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
   736
			$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
   737
				verify     => 'require',
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   738
				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
   739
				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
   740
				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
   741
				keydecrypt => sub {
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   742
					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
   743
					Term::ReadKey::ReadMode 2;
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   744
					chomp( my $secret = <STDIN> );
3
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   745
					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
   746
					print "\n";
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   747
					return $secret;
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   748
				});
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   749
		}
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   750
		else {
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   751
			$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
   752
		}
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   753
	}
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   754
87
475aff91675b eval optional modules in blocks instead of strings.
Mahlon E. Smith <mahlon@martini.nu>
parents: 84
diff changeset
   755
	undef $@; eval { require Authen::SASL; };
62
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   756
	my ( $sasl, $sasl_conn );
84
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   757
	my $has_sasl = ! $@;
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   758
	my $use_sasl = $has_sasl && $conf->{'sasl'};
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   759
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   760
	die "SASL requested, but library is not installed.  Please install Authen::SASL and try again.\n" if $conf->{'sasl'} && ! $has_sasl;
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   761
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   762
	if ( $use_sasl ) {
61
e3bd30b95695 Add simple SASL support. Patch from Michael Raitza <spacefrogg-devel@meterriblecrew.net>.
Mahlon E. Smith <mahlon@laika.com>
parents: 60
diff changeset
   763
		my $serv = $conf->{'server'};
e3bd30b95695 Add simple SASL support. Patch from Michael Raitza <spacefrogg-devel@meterriblecrew.net>.
Mahlon E. Smith <mahlon@laika.com>
parents: 60
diff changeset
   764
		$serv =~ s!^ldap[si]?://!!;
62
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   765
		$sasl = Authen::SASL->new( mechanism => $conf->{'sasl'} );
84
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   766
		$sasl_conn = $sasl->client_new( 'ldap', $serv );
61
e3bd30b95695 Add simple SASL support. Patch from Michael Raitza <spacefrogg-devel@meterriblecrew.net>.
Mahlon E. Smith <mahlon@laika.com>
parents: 60
diff changeset
   767
	}
84
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   768
62
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   769
	# bind with sasl
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   770
	#
84
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   771
	if ( $sasl_conn ) {
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   772
		$rv = $ldap->bind( $conf->{'binddn'},
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   773
			sasl     => $sasl_conn
62
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   774
		);
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   775
	}
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   776
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   777
	# simple bind as an authenticated dn
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   778
	#
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   779
	elsif ( $conf->{'binddn'} ) {
84
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   780
		$rv = $ldap->bind( $conf->{'binddn'},
62
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   781
			password => $conf->{'bindpass'}
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   782
		);
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   783
	}
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   784
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   785
	# bind anonymously
62
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   786
	#
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
   787
	else {
84
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   788
		$rv = $sasl_conn ? $ldap->bind( sasl => $sasl_conn ) : $ldap->bind();
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   789
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   790
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   791
	my $err = $rv->error();
57
85cc85d0c1b1 Catch a case where the LDAP object is defined, but in a state that
Mahlon E. Smith <mahlon@laika.com>
parents: 56
diff changeset
   792
	$self->debug(
85cc85d0c1b1 Catch a case where the LDAP object is defined, but in a state that
Mahlon E. Smith <mahlon@laika.com>
parents: 56
diff changeset
   793
		"Bind as " .
84
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   794
			( $conf->{'binddn'} ? $conf->{'binddn'} : 'anonymous' ) .
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   795
			" to " . $conf->{'server'} . ": $err\n"
57
85cc85d0c1b1 Catch a case where the LDAP object is defined, but in a state that
Mahlon E. Smith <mahlon@laika.com>
parents: 56
diff changeset
   796
	);
85cc85d0c1b1 Catch a case where the LDAP object is defined, but in a state that
Mahlon E. Smith <mahlon@laika.com>
parents: 56
diff changeset
   797
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   798
	if ( $rv->code() ) {
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   799
		$err .= " (try the --tls flag?)" if $err =~ /confidentiality required/i;
84
ef002a0b0867 Fix anonymous binds when SASL is not used.
Mahlon E. Smith <mahlon@martini.nu>
parents: 82
diff changeset
   800
		$err .= "\n" . $sasl->error if $sasl_conn && defined( $sasl->error );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   801
		die "LDAP bind error: $err\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   802
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   803
57
85cc85d0c1b1 Catch a case where the LDAP object is defined, but in a state that
Mahlon E. Smith <mahlon@laika.com>
parents: 56
diff changeset
   804
	# Offer to cache authentication info.
85cc85d0c1b1 Catch a case where the LDAP object is defined, but in a state that
Mahlon E. Smith <mahlon@laika.com>
parents: 56
diff changeset
   805
	# If we enter this conditional, we have successfully authed with the server
85cc85d0c1b1 Catch a case where the LDAP object is defined, but in a state that
Mahlon E. Smith <mahlon@laika.com>
parents: 56
diff changeset
   806
	# (non anonymous), and we haven't cached anything in the past.
15
f6157d378459 Exit with a nicer error message if IO::Socket::SSL isn't installed,
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   807
	#
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   808
	if ( $conf->{'binddn'} && ! -e $conf->{'configfile'} ) {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   809
		print "Would you like to cache your connection information? [Yn]: ";
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   810
		chomp( my $response = <STDIN> );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   811
		unless ( $response =~ /^n/i ) {
102
e92cb378144d Fix precedence of command line arguments
Davor Ocelic <docelic@crystallabs.io>
parents: 101
diff changeset
   812
			main::save_config($conf->{configfile});
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   813
			print "Connection info cached to $conf->{'configfile'}.\n";
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   814
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   815
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   816
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   817
	$self->{'ldap'} = $ldap;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   818
	return $ldap;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   819
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   820
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   821
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   822
### Return a new LDIF object, suitable for populating with
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   823
### a Net::LDAP::Entry.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   824
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   825
sub ldif 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   826
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   827
	my $self	 = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   828
	my $use_temp = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   829
99
2a7a3072d76c Add LICENSE file for packaging, bump copyright.
Mahlon E. Smith <mahlon@martini.nu>
parents: 95
diff changeset
   830
	my $raw = qr/(^jpegPhoto|;binary)/;
2a7a3072d76c Add LICENSE file for packaging, bump copyright.
Mahlon E. Smith <mahlon@martini.nu>
parents: 95
diff changeset
   831
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   832
	# create tmpfile and link ldif object with it
49
57df728cdb77 Alter the default wrap width for LDIF to expand to the terminal size,
Mahlon E. Smith <mahlon@laika.com>
parents: 48
diff changeset
   833
	#
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   834
	if ( $use_temp ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   835
		my ( undef, $fname ) =
76
589332cac30b Use the system tempdir instead of hardcoding /tmp.
Mahlon E. Smith <mahlon@laika.com>
parents: 73
diff changeset
   836
		  File::Temp::tempfile( 'shelldap_XXXXXXXX', SUFFIX => '.ldif', TMPDIR => 1, UNLINK => 1 );
99
2a7a3072d76c Add LICENSE file for packaging, bump copyright.
Mahlon E. Smith <mahlon@martini.nu>
parents: 95
diff changeset
   837
		$self->{'ldif'}	      = Net::LDAP::LDIF->new( $fname, 'w', sort => 1, wrap => 0, raw => $raw );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   838
		$self->{'ldif_fname'} = $fname;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   839
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   840
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   841
	# ldif -> stdout
73
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   842
	#
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   843
	else {
99
2a7a3072d76c Add LICENSE file for packaging, bump copyright.
Mahlon E. Smith <mahlon@martini.nu>
parents: 95
diff changeset
   844
		$self->{'ldif'} = Net::LDAP::LDIF->new( \*STDOUT, 'w', sort => 1, wrap => $self->wrapsize, raw => $raw );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   845
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   846
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   847
	return $self->{'ldif'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   848
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   849
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   850
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   851
### Return an Entry object from an LDIF filename, or undef if there was an error.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   852
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   853
sub load_ldif
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   854
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   855
	my $self = shift;
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   856
	my $ldif = Net::LDAP::LDIF->new( shift(), 'r' );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   857
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   858
	return unless $ldif;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   859
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   860
	my $e;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   861
	eval { $e = $ldif->read_entry(); };
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   862
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   863
	return if $@;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   864
	return $e;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   865
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   866
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   867
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   868
### Given a filename, return an md5 checksum.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   869
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   870
sub chksum 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   871
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   872
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   873
	my $file = shift or return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   874
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   875
	my $md5 = Digest::MD5->new();
118
2d7f16eff198 Use local implementation of slurp()
Davor Ocelic <docelic@crystallabs.io>
parents: 117
diff changeset
   876
	open F, $file;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   877
	my $hash = $md5->addfile( *F )->hexdigest();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   878
	close F;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   879
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   880
	return $hash;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   881
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   882
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   883
51
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   884
### Find and return the current terminal width.
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   885
###
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   886
sub wrapsize
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   887
{
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   888
	my $self = shift;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   889
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   890
	my $wrap = $conf->{'wrap'};
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   891
	eval {
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   892
		my $rows;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   893
		my $term = Term::ReadLine->new( 1 );
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   894
		( $rows, $wrap ) = $term->get_screen_size() unless $wrap;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   895
	};
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   896
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   897
	$wrap ||= 78;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   898
	return $wrap;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   899
}
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   900
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
   901
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   902
### Used by Term::Shell to generate the prompt.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   903
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   904
sub prompt_str
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   905
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   906
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   907
	return $self->{'prompt'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   908
}
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   909
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   910
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   911
### Display the current working entry as the prompt,
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   912
### truncating if necessary.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   913
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   914
sub update_prompt 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   915
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   916
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   917
	my $base = $self->base();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   918
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   919
	if ( length $base > 50 ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   920
		my $cwd_dn = $1 if $base =~ /^(.*?),/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   921
		$self->{'prompt'} = "... $cwd_dn > ";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   922
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   923
	else {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   924
		my $prompt = $base;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   925
		$prompt =~ s/$conf->{'basedn'}/~/;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   926
		$self->{'prompt'} = "$prompt > ";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   927
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   928
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   929
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   930
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   931
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   932
### Prompt the user to re-edit their LDIF on error.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   933
### Returns true if the user wants to do so.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   934
###
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   935
sub prompt_edit_again
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   936
{
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   937
	my $self = shift;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   938
	print "Edit again? [Yn]: ";
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   939
	chomp( my $ans = <STDIN> );
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   940
	return $ans !~ /^n/i;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   941
}
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   942
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   943
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   944
### Return the basedn of the LDAP connection, being either explicitly
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   945
### configured or determined automatically from server metadata.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   946
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   947
sub base 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   948
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   949
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   950
	$self->{'base'} ||= $conf->{'basedn'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   951
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   952
	# try and determine base automatically from rootDSE
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   953
	#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   954
	unless ( $self->{'base'} ) {
20
d956658803b8 use sane way to get a default basedn: RootDSe's namingContexts
Peter Marschall <peter@adpm.de>
parents: 19
diff changeset
   955
		my @namingContexts = $self->{'root_dse'}->get_value('namingContexts');
d956658803b8 use sane way to get a default basedn: RootDSe's namingContexts
Peter Marschall <peter@adpm.de>
parents: 19
diff changeset
   956
		$conf->{'basedn'} = $namingContexts[0];
d956658803b8 use sane way to get a default basedn: RootDSe's namingContexts
Peter Marschall <peter@adpm.de>
parents: 19
diff changeset
   957
		$self->{'base'}   = $namingContexts[0];
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   958
	}
23
2ab2df609cc7 small style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 22
diff changeset
   959
	if ( $_[0] ) {
2ab2df609cc7 small style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 22
diff changeset
   960
		my $base = canonical_dn( $_[0], casefold => 'none' );
2ab2df609cc7 small style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 22
diff changeset
   961
		$self->{'base'} = $base if $base;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   962
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   963
	return $self->{'base'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   964
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   965
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   966
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   967
### Returns true if the specified dn is valid on this LDAP server.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   968
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   969
sub is_valid_dn 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   970
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   971
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   972
	my $dn   = shift or return 0;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   973
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   974
	my $r = $self->search({ base => $dn });
16
a2e3faa3d2fc use symbolic LDAP error codes instead of numbers
Peter Marschall <peter@adpm.de>
parents: 15
diff changeset
   975
	return $r->{'code'} == LDAP_SUCCESS ? 1 : 0;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   976
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
   977
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
   978
73
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   979
### Emit LDIF to the terminal.
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   980
###
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   981
sub display
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   982
{
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   983
	my $self  = shift;
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   984
	my $dn    = shift;
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   985
	my @attrs = @{;shift};
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   986
	my $use_pager = shift;
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   987
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   988
	unless ( $dn ) {
106
94d941f13a5a Make 'cat' default to '.' (cwd) instead of requiring argument
Davor Ocelic <docelic@crystallabs.io>
parents: 105
diff changeset
   989
		$dn = '.'
73
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   990
	}
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   991
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   992
	# support '.'
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   993
	$dn = $self->base() if $dn eq '.';
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   994
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   995
	# support globbing
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   996
	#
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   997
	my $s;
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   998
	if ( $dn eq '*' ) {
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
   999
		$s = $self->search({
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1000
			scope  => 'one',
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1001
			vals   => 1,
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1002
			attrs  => \@attrs
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1003
		});
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1004
	}
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1005
	elsif ( $dn =~ /\*/ ) {
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1006
		$s = $self->search({
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1007
			scope  => 'one',
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1008
			vals   => 1,
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1009
			filter => $dn,
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1010
			attrs  => \@attrs
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1011
		});
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1012
	}
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1013
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1014
	# absolute/relative dn
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1015
	#
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1016
	else {
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1017
		$dn = $self->path_to_dn( $dn );
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1018
		$s = $self->search({
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1019
			base   => $dn,
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1020
			vals   => 1,
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1021
			attrs  => \@attrs
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1022
		});
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1023
	}
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1024
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1025
	# emit error, if any
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1026
	#
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1027
	if ( $s->{'code'} ) {
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1028
		print $s->{'message'} . "\n";
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1029
		return;
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1030
	}
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1031
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1032
	# display to stdout or pager
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1033
	#
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1034
	my $ldif = $self->ldif( $use_pager );
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1035
	foreach my $e ( @{ $s->{'entries'} } ) {
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1036
		$ldif->write_entry( $e );
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1037
	}
82
57a1335691e5 Allow the period character when moving an entry by full DN.
Mahlon E. Smith <mahlon@martini.nu>
parents: 81
diff changeset
  1038
	if ( $use_pager ) {
73
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1039
		system( $self->{'pager'}, $self->{'ldif_fname'} );
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1040
		unlink $self->{'ldif_fname'};
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1041
	}	
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1042
	return;
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1043
}
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1044
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1045
92
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1046
### Perform an LDAP search, optionally with the server side pager
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1047
### control.
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1048
###
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1049
### Returns a hashref containing the return code and
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1050
### an arrayref of Net::LDAP::Entry objects.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1051
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1052
sub search 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1053
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1054
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1055
	my $opts = shift || {};
92
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1056
	my $controls = [];
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1057
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1058
	$opts->{'base'}   ||= $self->base(),
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1059
	$opts->{'filter'} ||= '(objectClass=*)';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1060
	$opts->{'scope'}  ||= 'base';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1061
92
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1062
	my $pager;
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1063
	if ( $conf->{'paginate'} ) {
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1064
		$pager = Net::LDAP::Control::Paged->new( size => $conf->{'paginate'} );
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1065
		push( @$controls, $pager );
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1066
	}
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1067
48
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1068
	my $search = sub { 
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1069
		return $self->ldap->search(
43
b8836c9018fb Attempt to retry the operation on failure.
Mahlon E. Smith <mahlon@laika.com>
parents: 42
diff changeset
  1070
			base	  => $opts->{'base'},
b8836c9018fb Attempt to retry the operation on failure.
Mahlon E. Smith <mahlon@laika.com>
parents: 42
diff changeset
  1071
			filter	  => $opts->{'filter'},
b8836c9018fb Attempt to retry the operation on failure.
Mahlon E. Smith <mahlon@laika.com>
parents: 42
diff changeset
  1072
			scope	  => $opts->{'scope'},
b8836c9018fb Attempt to retry the operation on failure.
Mahlon E. Smith <mahlon@laika.com>
parents: 42
diff changeset
  1073
			timelimit => $conf->{'timeout'},
b8836c9018fb Attempt to retry the operation on failure.
Mahlon E. Smith <mahlon@laika.com>
parents: 42
diff changeset
  1074
			typesonly => ! $opts->{'vals'},
92
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1075
			attrs	  => $opts->{'attrs'} || ['*'],
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1076
			control   => $controls
43
b8836c9018fb Attempt to retry the operation on failure.
Mahlon E. Smith <mahlon@laika.com>
parents: 42
diff changeset
  1077
		);
48
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1078
	};
43
b8836c9018fb Attempt to retry the operation on failure.
Mahlon E. Smith <mahlon@laika.com>
parents: 42
diff changeset
  1079
92
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1080
	my $s;
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1081
	my $entries = [];
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1082
   	my $token  = '-';
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1083
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1084
	if ( $conf->{'paginate'} ) {
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1085
		while( $token ) {
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1086
			$s = $self->with_retry( $search );
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1087
			push( @$entries, $s->entries() );
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1088
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1089
			my $page_response = $s->control( LDAP_CONTROL_PAGED ) or last;
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1090
			$token = $page_response->cookie;
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1091
			$pager->cookie( $token );
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1092
		}
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1093
	}
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1094
	else {
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1095
		$s = $self->with_retry( $search );
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1096
		$entries = [ $s->entries() ];
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1097
	}
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1098
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1099
	my $rv = {
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1100
		code	=> $s->code(),
92
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1101
		message => $s->error()
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1102
	};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1103
92
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1104
	if ( $opts->{'scope'} eq 'base' ) {
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1105
		$rv->{'entries'} = [ $s->shift_entry() ]
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1106
	}
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1107
	else {
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1108
		$rv->{'entries'} = $entries;
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  1109
	}
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1110
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1111
	return $rv;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1112
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1113
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1114
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1115
### Maintain the cache of possible autocomplete values for
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1116
### the current DN.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1117
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1118
sub update_entries 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1119
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1120
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1121
	my %opts = @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1122
	my $base = lc( $self->base() );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1123
	
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1124
	my $s = $opts{'search'} || $self->search({ scope => 'one', base => $base });
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1125
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1126
	$self->{'cwd_entries'} = [];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1127
	return if $s->{'code'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1128
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1129
	# setup cache object
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1130
	$self->{'cache'} ||= {};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1131
	$self->{'cache'}->{ $base } ||= {};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1132
	$self->{'cache'}->{ $base } = {} if $opts{'clearcache'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1133
	my $cache = $self->{'cache'}->{ $base };
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1134
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1135
	my $now = time();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1136
	if ( ! exists $cache->{'entries'}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1137
		or $now - $cache->{'timestamp'} > $conf->{'cacheage'} )
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1138
	{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1139
		$self->debug("Caching entries for $base\n");
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1140
		foreach my $e ( @{ $s->{'entries'} } ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1141
			my $dn  = $e->dn();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1142
			my $rdn = $dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1143
			$rdn =~ s/,$base//i;  # remove base from display
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1144
			push @{ $self->{'cwd_entries'} }, $rdn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1145
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1146
		$cache->{'timestamp'} = $now;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1147
		$cache->{'entries'} = $self->{'cwd_entries'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1148
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1149
	else {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1150
		$self->debug("Using cached lookups for $base\n");
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1151
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1152
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1153
	$self->{'cwd_entries'} = $cache->{'entries'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1154
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1155
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1156
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1157
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1158
### Roughly convert a given path to a DN.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1159
###
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1160
### Additionally support:
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1161
###    parent  '..'
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1162
###    current '.'
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1163
###    last    '-'
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1164
###    home    '~'
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1165
###
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1166
### Synopsis: $dn = $self->path_to_dn( $path );
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1167
###
30
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1168
sub path_to_dn
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1169
{
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1170
	my $self    = shift;
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1171
	my $path    = shift;
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1172
	my %flags   = @_;
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1173
	my $curbase = $self->base();
30
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1174
46
f0616455056d Fix the uninitialized $path value errors I erroneously introduced on
Mahlon E. Smith <mahlon@laika.com>
parents: 44
diff changeset
  1175
	# support empty 'cd' or 'cd ~' going to root
f0616455056d Fix the uninitialized $path value errors I erroneously introduced on
Mahlon E. Smith <mahlon@laika.com>
parents: 44
diff changeset
  1176
	return $conf->{'basedn'} if ! $path || $path eq '~';
f0616455056d Fix the uninitialized $path value errors I erroneously introduced on
Mahlon E. Smith <mahlon@laika.com>
parents: 44
diff changeset
  1177
30
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1178
	# return current base DN
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1179
	return $curbase if $path eq '.';
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1180
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1181
	# support 'cd -'
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1182
	return $self->{'previous_base'} if $path eq '-';
30
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1183
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1184
	# relative path, upwards
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1185
	#
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1186
	if ( $path =~ /^\.\./o ) {
30
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1187
		# support '..' (possibly iterated and as prefix to a DN)
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1188
		my @base = @{ ldap_explode_dn($curbase, casefold => 'none') };
30
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1189
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1190
		# deal with leading ..,
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1191
		#
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1192
		while ( $path =~ /^\.\./ ) {
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1193
			shift( @base ) if @base;
30
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1194
			$path =~ s/^\.\.//;
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1195
			last if $path !~ /[,\/]\s*/;
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1196
			$path =~ s/[,\/]\s*//;
30
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1197
		}
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1198
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1199
		# append the new dn to the node if one was specified:
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1200
		#    cd ../../cn=somewhere  vs
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1201
		#    cd ../../
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1202
		#
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1203
		my $newbase_root = canonical_dn( \@base, casefold => 'none' );
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1204
		$path = $path ? $path . ',' . $newbase_root : $newbase_root;
30
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1205
	}
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1206
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1207
	# attach the base if it isn't already there (this takes care of
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1208
	# deeper relative nodes and absolutes)
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1209
	#
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1210
	else {
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1211
		$path = "$path," . $curbase unless $path =~ /$curbase/;
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1212
	}
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1213
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1214
	return $path;
30
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1215
}
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1216
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1217
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1218
### Given an array ref of shell-like globs, 
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1219
### create and return a Net::LDAP::Filter object.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1220
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1221
sub make_filter 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1222
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1223
	my $self  = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1224
	my $globs = shift or return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1225
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1226
	return unless ref $globs eq 'ARRAY';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1227
	return unless scalar @$globs;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1228
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1229
	my $filter;
28
d42bd1b087a1 make_filter: cope with filters that are already parenthesized
Peter Marschall <peter@adpm.de>
parents: 27
diff changeset
  1230
	$filter = join('', map { (/^\(.*\)$/o) ? $_ : "($_)" } @$globs);
d42bd1b087a1 make_filter: cope with filters that are already parenthesized
Peter Marschall <peter@adpm.de>
parents: 27
diff changeset
  1231
	$filter = '(|' . $filter . ')'  if (scalar(@$globs) > 1);
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1232
	$filter = Net::LDAP::Filter->new( $filter );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1233
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1234
	if ( $filter ) {
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1235
		$self->debug( 'Filter parsed as: ' . $filter->as_string() . "\n" );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1236
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1237
	else {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1238
		print "Error parsing filter.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1239
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1240
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1241
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1242
	return $filter;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1243
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1244
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  1245
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1246
### Given an arrayref of objectClasses, pull a complete list of 
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1247
### required and optional attrbutes.  Returns two arrayrefs.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1248
###
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1249
sub fetch_attributes
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1250
{
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1251
	my $self = shift;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1252
	my $ocs  = shift or return [], [];
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1253
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1254
	my ( %seen, @must_attr, @may_attr );
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1255
	foreach my $oc ( sort @{$ocs} ) {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1256
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1257
		# required
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1258
		my @must = $self->{'schema'}->must( $oc );
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1259
		foreach my $attr ( sort { $a->{'name'} cmp $b->{'name'} } @must ) {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1260
			next if $attr->{'name'} =~ /^objectclass$/i;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1261
			next if $seen{ $attr->{'name'} };
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1262
			push @must_attr, $attr->{'name'};
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1263
			$seen{ $attr->{'name'} }++;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1264
		}
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1265
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1266
		# optional
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1267
		my @may  = $self->{'schema'}->may( $oc );
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1268
		foreach my $attr ( sort { $a->{'name'} cmp $b->{'name'} } @may ) {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1269
			next if $attr->{'name'} =~ /^objectclass$/i;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1270
			next if $seen{ $attr->{'name'} };
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1271
			push @may_attr, $attr->{'name'};
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1272
			$seen{ $attr->{'name'} }++;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1273
		}
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1274
	}
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1275
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1276
	return \@must_attr, \@may_attr;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1277
}
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1278
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1279
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1280
### Check whether a given string can be used directly as
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1281
### an LDAP search filter.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1282
###
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1283
### Synopsis: $yesNo = $self->is_valid_filter($string);
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1284
###
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  1285
sub is_valid_filter
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  1286
{
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  1287
	my $self   = shift;
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  1288
	my $filter = shift or return;
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  1289
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  1290
	return Net::LDAP::Filter->new( $filter ) ? 1 : 0;
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  1291
}
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  1292
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  1293
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1294
### Call code in subref $action, if there's any connection related errors,
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1295
### try it one additional time before giving up.  This should take care of
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1296
### most server disconnects due to timeout and other generic connection
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1297
### errors, and will attempt to transparently re-establish a connection.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1298
###
48
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1299
sub with_retry
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1300
{
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1301
	my $self = shift;
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1302
	my $action = shift;
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1303
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1304
	my $rv = $action->();
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1305
	if ( $rv->code() == LDAP_OPERATIONS_ERROR   ||
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1306
		 $rv->code() == LDAP_TIMELIMIT_EXCEEDED ||
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1307
		 $rv->code() == LDAP_BUSY               ||
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1308
		 $rv->code() == LDAP_UNAVAILABLE        ||
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1309
		 $rv->code() == LDAP_OTHER              ||
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1310
		 $rv->code() == LDAP_SERVER_DOWN        ||
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1311
		 $rv->code() == LDAP_TIMEOUT            ||
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1312
		 $rv->code() == LDAP_NO_MEMORY          ||
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1313
		 $rv->code() == LDAP_CONNECT_ERROR ) {
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1314
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1315
		$self->debug( "Error ". $rv->code() . ", retrying.\n" );
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1316
		$self->{'ldap'} = undef;
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1317
		$rv = $action->();
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1318
	}
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1319
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1320
	return $rv;
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1321
}
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1322
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1323
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1324
### little. yellow. different. better.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1325
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1326
sub debug 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1327
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1328
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1329
	return unless $conf->{'debug'};
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1330
	print "\e[33m";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1331
	print shift();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1332
	print "\e[0m";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1333
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1334
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1335
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1336
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1337
### Autocomplete values: Returns cached children entries.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1338
###
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1339
sub autocomplete_from_cwd
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1340
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1341
	my $self = shift;
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1342
	my $word = quotemeta shift;
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1343
	return grep {/^$word/} @{ $self->{'cwd_entries'} };
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1344
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1345
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1346
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1347
### Autocomplete values: Returns previously set shelldap environment values.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1348
###
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1349
sub autocomplete_from_env
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1350
{ 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1351
	my $self = shift;
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1352
	my $word = quotemeta shift;
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1353
	return grep {/^$word/} @{ $self->{'env'} };
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1354
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1355
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1356
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1357
### Autocomplete values: Returns all objectClasses as defined
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1358
### by the LDAP server.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1359
###
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1360
sub autocomplete_from_objectclasses
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1361
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1362
	my $self = shift;
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1363
	my $word = quotemeta shift;
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1364
	return grep {/^$word/} @{ $self->{'objectclasses'} };
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1365
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1366
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1367
51
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  1368
### Autocomplete values: Returns all objectClasses as defined
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  1369
### by the LDAP server, along with current children DNs.
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  1370
###
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1371
sub autocomplete_from_objectclasses_and_cwd
51
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  1372
{
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  1373
	my $self = shift;
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1374
	my $word = quotemeta shift;
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1375
	return grep {/^$word/} ('_schema', @{ $self->{'objectclasses'} }, @{ $self->{'cwd_entries'} });
51
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  1376
}
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  1377
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  1378
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1379
### Given an $arrayref, remove LDIF continuation wrapping in place,
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1380
### effectively making each entry a single line for LCS comparisons.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1381
### 
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1382
sub unwrap_line {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1383
	my $self  = shift;
5
78b2a48e07db Combine multiple lines into a single one before displaying LDIF. Patch
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
  1384
	my $array = shift;
78b2a48e07db Combine multiple lines into a single one before displaying LDIF. Patch
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
  1385
6
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
  1386
	my $i = 1;
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
  1387
	while ( $i < scalar(@$array) ) {
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
  1388
		if ( $array->[$i] =~ /^\s/ ) {
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
  1389
			$array->[ $i - 1 ] =~ s/\n$//;
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
  1390
			$array->[ $i ] =~ s/^\s//;
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
  1391
			splice( @$array, $i - 1, 2, $array->[$i - 1] . $array->[$i] );
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
  1392
		}
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
  1393
		else {
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
  1394
			$i++;
5
78b2a48e07db Combine multiple lines into a single one before displaying LDIF. Patch
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
  1395
		}
78b2a48e07db Combine multiple lines into a single one before displaying LDIF. Patch
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
  1396
	}
78b2a48e07db Combine multiple lines into a single one before displaying LDIF. Patch
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
  1397
}
78b2a48e07db Combine multiple lines into a single one before displaying LDIF. Patch
Mahlon E. Smith <mahlon@laika.com>
parents: 4
diff changeset
  1398
6
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
  1399
81
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1400
### Given an LDAP Entry object $e, an array reference to it's LDIF original
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1401
### content, and another array reference to updated LDIF content, run an LCS
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1402
### comparison, modifying the Entry object in place.
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1403
### 
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1404
sub diff {
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1405
	my $self = shift;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1406
	my $e    = shift;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1407
	my $orig = shift;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1408
	my $new  = shift;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1409
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1410
	$self->unwrap_line( $orig );
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1411
	$self->unwrap_line( $new );
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1412
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1413
	# parser subref
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1414
	#
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1415
	my $parse = sub {
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1416
		my $line = shift || $_;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1417
		return if $line	 =~ /^\#/; # ignore comments
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1418
		my ( $attr, $val ) = ( $1, $2 ) if $line =~ /^(.+?): (.*)$/;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1419
		return unless $attr;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1420
		return if index($attr, ':') != -1;  # ignore base64
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1421
		return ( $attr, $val );
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1422
	};
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1423
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1424
	my $diff = Algorithm::Diff->new( $orig, $new );
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1425
	HUNK:
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1426
	while ( $diff->Next() ) {
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1427
		next if $diff->Same();
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1428
		my $diff_bit = $diff->Diff();
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1429
		my %seen_attr;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1430
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1431
		# attr removal hunk
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1432
		#
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1433
		if ( $diff_bit == 1 ) {
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1434
			foreach ( $diff->Items(1) ) {
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1435
				my ( $attr, $val ) = $parse->( $_ ) or next;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1436
				$self->debug("DELETE: $_");
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1437
				$e->delete( $attr => [ $val ] );
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1438
			}
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1439
		}
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1440
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1441
		# attr insertion hunk
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1442
		#
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1443
		if ( $diff_bit == 2 ) {
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1444
			foreach ( $diff->Items(2) ) {
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1445
				my ( $attr, $val ) = $parse->( $_ ) or next;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1446
				$self->debug("INSERT: $_");
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1447
				$e->add( $attr => $val );
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1448
			}
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1449
		}
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1450
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1451
		# attr change hunk
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1452
		#
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1453
		if ( $diff_bit == 3 ) {
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1454
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1455
			# modification to existing line
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1456
			#
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1457
			foreach ( $diff->Items(2) ) {
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1458
				my ( $attr, $val ) = $parse->( $_ ) or next;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1459
				$self->debug("MODIFY: $_");
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1460
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1461
				my $cur_vals = $e->get_value( $attr, asref => 1 ) || [];
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1462
				my $cur_valcount = scalar @$cur_vals;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1463
				next if $cur_valcount == 0; # should have been an 'add'
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1464
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1465
				# replace immediately 
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1466
				#
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1467
				if ( $cur_valcount == 1 ) {
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1468
					$e->replace( $attr => $val );
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1469
				}
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1470
				else {
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1471
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1472
					# retain attributes that allow multiples, so updating
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1473
					# one attribute doesn't inadvertently remove others with
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1474
					# the same name.
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1475
					#
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1476
					next if $seen_attr{ $attr };
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1477
					my @new_vals;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1478
					foreach my $line ( @$new ) {
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1479
						my ( $new_attr, $new_val ) = $parse->( $line ) or next;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1480
						next unless $new_attr eq $attr;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1481
						$seen_attr{ $attr }++;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1482
						push @new_vals, $new_val;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1483
					}
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1484
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1485
					$e->replace( $attr => \@new_vals );
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1486
				}
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1487
			}
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1488
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1489
			# deletion within the same hunk
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1490
			#
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1491
			foreach ( $diff->Items(1) ) {
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1492
				my ( $attr, $val ) = $parse->( $_ ) or next;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1493
				my $cur_vals = $e->get_value( $attr, asref => 1 ) || [];
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1494
				my $cur_valcount = scalar @$cur_vals;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1495
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1496
				next if $cur_valcount == 1;
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1497
				next if $seen_attr{ $attr };
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1498
				$self->debug("DELETE: $_");
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1499
				$e->delete( $attr => [ $val ] );
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1500
			}
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1501
		}
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1502
	}
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1503
}
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1504
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1505
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  1506
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1507
########################################################################
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1508
### S H E L L   M E T H O D S
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1509
########################################################################
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1510
104
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  1511
# alias_or_command => [ real_command_name, completion_function ]
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  1512
#
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  1513
tie my %cmd_map, 'Tie::IxHash';
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  1514
%cmd_map = (
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1515
	# Real commands:
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1516
	'alias'   => [ undef ],
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1517
	'configfile'=> [ undef ],
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  1518
	'whoami'  => [ undef ],
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  1519
	'pwd'     => [ undef ],
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1520
	'list'    => [ undef, 'autocomplete_from_cwd' ],
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1521
	'grep'    => [ undef, 'autocomplete_from_cwd' ],
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1522
	'edit'    => [ undef, 'autocomplete_from_cwd' ],
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1523
	'delete'  => [ undef, 'autocomplete_from_cwd' ],
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1524
	'copy'    => [ undef, 'autocomplete_from_cwd' ],
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1525
	'cat'     => [ undef, 'autocomplete_from_cwd' ],
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1526
	'move'    => [ undef, 'autocomplete_from_cwd' ],
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1527
	'less'    => [ undef, 'autocomplete_from_cwd' ],
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1528
	'cd'      => [ undef, 'autocomplete_from_cwd' ],
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1529
	'create'  => [ undef, 'autocomplete_from_objectclasses' ],
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1530
	'setenv'  => [ undef, 'autocomplete_from_env' ],
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  1531
	'passwd'  => [ undef ],
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  1532
	'clear'   => [ undef ],
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1533
	'env'     => [ undef, 'autocomplete_from_env' ],
104
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  1534
	#'help'    => [ undef ],
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  1535
	'mkdir'   => [ undef ],
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1536
	'inspect' => [ undef, 'autocomplete_from_objectclasses_and_cwd' ],
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1537
	'unalias' => [ undef ],
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1538
	'unsetenv'=> [ undef ],
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1539
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  1540
	# Aliases:
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1541
	'id'      => [ 'whoami'  ],
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1542
	'ls'      => [ 'list'    ],
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1543
	'search'  => [ 'grep'    ],
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1544
	'vi'      => [ 'edit'    ],
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1545
	'rm'      => [ 'delete'  ],
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1546
	'cp'      => [ 'copy'    ],
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1547
	'read'    => [ 'read'    ],
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1548
	'mv'      => [ 'move'    ],
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1549
	'touch'   => [ 'create'  ],
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1550
	'export'  => [ 'setenv'  ],
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1551
	'set'     => [ 'setenv'  ],
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1552
	'?'       => [ 'help'    ],
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1553
	'man'     => [ 'help'    ],
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1554
	'unset'   => [ 'unsetenv'],
104
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  1555
);
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  1556
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  1557
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1558
### Term::Shell hook.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1559
### Write history for each command, print shell debug actions.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1560
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1561
sub precmd
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1562
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1563
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1564
	my ( $handler, $cmd, $args ) = @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1565
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1566
	my $term = $self->term();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1567
	eval { $term->WriteHistory("$ENV{'HOME'}/.shelldap_history"); };
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1568
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1569
	$self->debug( "$$cmd (" . ( join ' ', @$args ) . "), calling '$$handler'\n" );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1570
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1571
} 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1572
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1573
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1574
### Display or define aliases.
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1575
###
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1576
sub run_alias
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1577
{
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1578
	my $self  = shift;
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1579
	my $cmd_alias = shift;
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1580
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1581
	# If $cmd_alias is empty, user requested printing of known aliases
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1582
	unless($cmd_alias) {
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1583
		while(my($alias,$cmd_args) = each %{$conf->{alias}}) {
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1584
			print "alias $alias=${\( join ' ', map { $_=~ /\s/ ? \"'$_'\" : $_} @{$cmd_args})}\n";
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1585
		}
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1586
		return
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1587
119
a6c211de24f0 Support separating aliases by space too
Davor Ocelic <docelic@crystallabs.io>
parents: 118
diff changeset
  1588
	# If there is argument but without = or space, user wanted to print specific alias
a6c211de24f0 Support separating aliases by space too
Davor Ocelic <docelic@crystallabs.io>
parents: 118
diff changeset
  1589
	} elsif($cmd_alias !~ /[=\s]/ and !@_) {
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1590
		my $alias = $cmd_alias;
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1591
		my $cmd_args = $conf->{alias}{$alias};
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1592
		unless( $cmd_args) {
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1593
			print "alias: $alias: not found\n";
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1594
		} else {
121
6c14c49fe429 Improve defining aliases with = or ' '
Davor Ocelic <docelic@crystallabs.io>
parents: 120
diff changeset
  1595
			print "alias $alias=${\( join ' ', map { $_=~ /\s/ ? \"'$_'\" : $_} @{$cmd_args})}\n";
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1596
		}
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1597
		return
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1598
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1599
	# There is argument with =, so the line is a new alias definition
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1600
	} else {
121
6c14c49fe429 Improve defining aliases with = or ' '
Davor Ocelic <docelic@crystallabs.io>
parents: 120
diff changeset
  1601
		my($alias, $alias2, $command) = ($cmd_alias =~ m/^([a-zA-Z0-9_-]+)$|^(\S+?)[\=\s]+(.+)$/);
6c14c49fe429 Improve defining aliases with = or ' '
Davor Ocelic <docelic@crystallabs.io>
parents: 120
diff changeset
  1602
		$alias = $alias2 if $alias2;
119
a6c211de24f0 Support separating aliases by space too
Davor Ocelic <docelic@crystallabs.io>
parents: 118
diff changeset
  1603
		unless( $alias) {
a6c211de24f0 Support separating aliases by space too
Davor Ocelic <docelic@crystallabs.io>
parents: 118
diff changeset
  1604
			print "Invalid syntax.\n";
a6c211de24f0 Support separating aliases by space too
Davor Ocelic <docelic@crystallabs.io>
parents: 118
diff changeset
  1605
			return
a6c211de24f0 Support separating aliases by space too
Davor Ocelic <docelic@crystallabs.io>
parents: 118
diff changeset
  1606
		}
121
6c14c49fe429 Improve defining aliases with = or ' '
Davor Ocelic <docelic@crystallabs.io>
parents: 120
diff changeset
  1607
		$command = $cmd_map{$command}[0] if $command and $cmd_map{$command} and $cmd_map{$command}[0];
119
a6c211de24f0 Support separating aliases by space too
Davor Ocelic <docelic@crystallabs.io>
parents: 118
diff changeset
  1608
		$conf->{alias}{$alias} = [ $command ? $command : (), @_ ];
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1609
	}
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1610
}
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1611
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1612
# Remove alias
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1613
sub run_unalias
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1614
{
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1615
	my $self = shift;
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1616
	for my $alias(@_) {
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1617
		unless( $conf->{alias}{$alias}) {
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1618
			print "alias: $alias: not found\n";
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1619
		} else {
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1620
		delete $conf->{alias}{$alias};
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1621
		}
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1622
	}
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1623
	return
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1624
}
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1625
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1626
# Run aliased command when alias is entered
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1627
sub catch_run {
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1628
	my $self = shift;
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1629
	my @cmdline;
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1630
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1631
	unless( $conf->{alias}{$_[0]}) {
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1632
		print $self->msg_unknown_cmd($_[0]);
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1633
		return
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1634
	}
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1635
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1636
	my $done = 0;
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1637
	while(my $arg = $_[0]) {
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1638
		my @alias = @{$conf->{alias}{$arg} or last};
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1639
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1640
		if($alias[-1] !~ s/\s+$//) {
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1641
			$done++
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1642
		}
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1643
		push @cmdline, @alias;
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1644
		shift;
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1645
		last if $done;
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1646
	}
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1647
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1648
	push @cmdline, @_;
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1649
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1650
	$self->run(@cmdline);
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1651
}
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1652
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1653
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1654
### Display an entry as LDIF to the terminal.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1655
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1656
sub run_cat 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1657
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1658
	my $self  = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1659
	my $dn    = shift;
101
cb4de2ecc7ab Allow default attributes to be set via config and cmdline
Davor Ocelic <docelic@crystallabs.io>
parents: 99
diff changeset
  1660
	my @attrs = (@_) ? @_ : @{$conf->{'attributes'}};
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1661
73
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1662
	$self->display( $dn, \@attrs, 0 );
72
39e9f802eb40 Add a 'less' command, that does the same as cat, but uses a pager.
Dennis Kaarsemaker <dennis.kaarsemaker@booking.com>
parents: 71
diff changeset
  1663
}
39e9f802eb40 Add a 'less' command, that does the same as cat, but uses a pager.
Dennis Kaarsemaker <dennis.kaarsemaker@booking.com>
parents: 71
diff changeset
  1664
73
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1665
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1666
### Load or save config
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1667
###
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1668
sub run_configfile
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1669
{
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1670
	my $self     = shift;
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1671
	my $action   = shift;
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1672
	my $filepath = shift;
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1673
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1674
	unless ( $action) {
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1675
		if( $conf->{configfile} ) {
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1676
			print qq{Current config file is '$conf->{configfile}'.\n}
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1677
		} else {
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1678
			print qq{Current config file is unset.\nDefault search locations:\n  ${\( join "\n  ", main::default_configfiles() )}\n}
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1679
		}
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1680
		return
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1681
	}
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1682
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1683
	unless( $action =~ /^(?:load|save)$/) {
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1684
		print "Invalid action specified; use 'load' or 'save'.\n";
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1685
		return;
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1686
	}
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1687
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1688
	# This too can result in $filepath being undef. In that case the defaults
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1689
	# from load_config() / save_config() will apply.
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1690
	$filepath ||= $conf->{configfile};
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1691
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1692
	if( $action eq 'load') {
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1693
		my($filepath, $more_conf) = main::load_config($filepath);
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1694
		if( $more_conf) {
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1695
			while ( my ($k, $v) = each %{$more_conf} ) { $conf->{ $k } = $v }
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1696
		}
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1697
		print "Config file '$filepath' loaded.\n";
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1698
	} elsif( $action eq 'save') {
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1699
		$filepath = main::save_config($filepath);
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  1700
		print "Config file '$filepath' saved.\n";
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1701
	}
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1702
}
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1703
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  1704
73
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1705
### Display an entry as LDIF to the terminal with external pagination.
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1706
###
72
39e9f802eb40 Add a 'less' command, that does the same as cat, but uses a pager.
Dennis Kaarsemaker <dennis.kaarsemaker@booking.com>
parents: 71
diff changeset
  1707
sub run_less
39e9f802eb40 Add a 'less' command, that does the same as cat, but uses a pager.
Dennis Kaarsemaker <dennis.kaarsemaker@booking.com>
parents: 71
diff changeset
  1708
{
39e9f802eb40 Add a 'less' command, that does the same as cat, but uses a pager.
Dennis Kaarsemaker <dennis.kaarsemaker@booking.com>
parents: 71
diff changeset
  1709
	my $self  = shift;
39e9f802eb40 Add a 'less' command, that does the same as cat, but uses a pager.
Dennis Kaarsemaker <dennis.kaarsemaker@booking.com>
parents: 71
diff changeset
  1710
	my $dn    = shift;
39e9f802eb40 Add a 'less' command, that does the same as cat, but uses a pager.
Dennis Kaarsemaker <dennis.kaarsemaker@booking.com>
parents: 71
diff changeset
  1711
	my @attrs = (@_) ? @_ : ('*');
39e9f802eb40 Add a 'less' command, that does the same as cat, but uses a pager.
Dennis Kaarsemaker <dennis.kaarsemaker@booking.com>
parents: 71
diff changeset
  1712
73
b3b840a4b56c Minor style cleanups, version bump.
Mahlon E. Smith <mahlon@laika.com>
parents: 72
diff changeset
  1713
	$self->display( $dn, \@attrs, 1 );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1714
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1715
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1716
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1717
### Change shelldap's idea of a current working 'directory',
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1718
### by adjusting the current default basedn for all searches.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1719
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1720
sub run_cd 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1721
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1722
	my $self	= shift;
41
3e7c107f8b93 Take only second argument for run_{cd,edit,mkdir}
Salvatore Bonaccorso <carnil@debian.org>
parents: 40
diff changeset
  1723
	my $newbase = shift;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1724
30
e4b4b0968107 add method path_to_dn() to convert a given "path" to a DN
Peter Marschall <peter@adpm.de>
parents: 29
diff changeset
  1725
	# convert given path to a DN
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1726
	$newbase = $self->path_to_dn( $newbase );
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1727
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1728
	unless ( $self->is_valid_dn( $newbase ) ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1729
		print "No such object\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1730
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1731
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1732
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1733
	# store old base
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1734
	$self->{'previous_base'} = $self->base();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1735
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1736
	# update new base
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1737
	$self->base( $newbase );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1738
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1739
	# get new 'cwd' listing
26
68318d115f6c fix attribute lists for LDAP queries
Peter Marschall <peter@adpm.de>
parents: 25
diff changeset
  1740
	my $s = $self->search({ scope => 'one', attrs => [ '1.1' ] });
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1741
	if ( $s->{'code'} ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1742
		print "$s->{'message'}\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1743
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1744
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1745
	$self->update_entries( search => $s );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1746
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1747
	# reflect cwd change in prompt
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1748
	$self->update_prompt();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1749
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1750
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1751
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1752
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1753
### Simply clear the screen.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1754
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1755
sub run_clear
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1756
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1757
	my $self = shift;
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1758
	system( 'clear' );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1759
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1760
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1761
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1762
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1763
### Fetch the source DN entry, modify it's DN data
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1764
### and write it back to the directory.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1765
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1766
sub run_copy
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1767
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1768
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1769
	my ( $s_dn, $d_dn ) = @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1770
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1771
	unless ( $s_dn ) {
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1772
		print "No source DN provided.\n";
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1773
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1774
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1775
	unless ( $d_dn ) {
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1776
		print "No destination DN provided.\n";
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1777
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1778
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1779
31
a3a710f720dd run_cat: convert to using path_to_dn()
Peter Marschall <peter@adpm.de>
parents: 30
diff changeset
  1780
	# convert given source path to DN
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  1781
	$s_dn = $self->path_to_dn( $s_dn );
31
a3a710f720dd run_cat: convert to using path_to_dn()
Peter Marschall <peter@adpm.de>
parents: 30
diff changeset
  1782
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1783
	# sanity check source
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1784
	#
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1785
	my $s = $self->search({ base => $s_dn, vals => 1 });
16
a2e3faa3d2fc use symbolic LDAP error codes instead of numbers
Peter Marschall <peter@adpm.de>
parents: 15
diff changeset
  1786
	unless ( $s->{'code'} == LDAP_SUCCESS ) {
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1787
		print "No such object\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1788
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1789
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1790
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1791
	# see if we're copying the entry to a nonexistent path
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1792
	#
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1793
	my ( $new_dn, $old_dn );
60
f90f7ff0b146 Fix the DN regexp to include dashes. Patch from Mike Hix <m@hix.io>.
Mahlon E. Smith <mahlon@laika.com>
parents: 58
diff changeset
  1794
	( $d_dn, $new_dn ) = ( $1, $2 ) if $d_dn =~ /^([\-\w=]+),(.*)$/;
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1795
	if ( $new_dn ) { # absolute
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1796
		unless ( $self->is_valid_dn( $new_dn ) ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1797
			print "Invalid destination.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1798
			return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1799
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1800
	}
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1801
	else { # relative
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1802
		$new_dn = $self->base();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1803
	}
60
f90f7ff0b146 Fix the DN regexp to include dashes. Patch from Mike Hix <m@hix.io>.
Mahlon E. Smith <mahlon@laika.com>
parents: 58
diff changeset
  1804
	$old_dn = $1 if $s_dn =~ /^[\-\w=]+,(.*)$/;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1805
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1806
	# get the source entry object
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1807
	my $e = ${ $s->{'entries'} }[0];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1808
	$e->dn( $s_dn );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1809
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1810
	# add changes in new entry instead of modifying existing
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1811
	$e->changetype( 'add' ); 
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1812
	$e->dn( "$d_dn,$new_dn" );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1813
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1814
	# get the unique attribute from the dn for modification
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1815
	# perhaps there is a better way to do this...?
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1816
	#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1817
	my ( $uniqkey, $uniqval ) = ( $1, $2 )
60
f90f7ff0b146 Fix the DN regexp to include dashes. Patch from Mike Hix <m@hix.io>.
Mahlon E. Smith <mahlon@laika.com>
parents: 58
diff changeset
  1818
	  if $d_dn =~ /^([\-\.\w]+)(?:\s+)?=(?:\s+)?([\-\.\s\w]+),?/;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1819
	unless ( $uniqkey && $uniqval ) {
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1820
		print "Unable to parse unique values from RDN.\n";
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1821
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1822
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1823
	$e->replace( $uniqkey => $uniqval );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1824
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1825
	# update (which will actually create the new entry)
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1826
	#
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1827
	my $update = sub { return $e->update($self->ldap()) };
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1828
	my $rv = $self->with_retry( $update );
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1829
	print $rv->error(), "\n";
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1830
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1831
	# clear caches
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1832
	#
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1833
	$self->{'cache'}->{ $new_dn } = {} if $new_dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1834
	$self->{'cache'}->{ $old_dn } = {} if $old_dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1835
	$self->update_entries( clearcache => 1 );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1836
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1837
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1838
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1839
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1840
### Create a new entry from scratch, using attributes from
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1841
### what the server's schema says is available from the specified
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1842
### (optional) objectClass list.  Populate a new LDIF file and
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1843
### present an editor to the user.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1844
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1845
sub run_create
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1846
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1847
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1848
	my @ocs  = @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1849
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1850
	# manually generate some boilerplate LDIF.
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1851
	#
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1852
	unless ( $self->{'create_file'} ) {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1853
		my $fh;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1854
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1855
		( $fh, $self->{'create_file'} ) =
58
94b64bbf93cf Automatically use ldif syntax highlighting for editors that understand
Mahlon E. Smith <mahlon@laika.com>
parents: 57
diff changeset
  1856
			File::Temp::tempfile( 'shelldap_XXXXXXXX', SUFFIX => '.ldif', DIR => '/tmp', UNLINK => 1 );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1857
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1858
		# first print out the dn and object classes.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1859
		#
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1860
		print $fh 'dn: ???,', $self->base(), "\n";
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1861
		foreach my $oc ( sort @ocs ) {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1862
			print $fh "objectClass: $oc\n";
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1863
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1864
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1865
		# gather and print attributes for requested objectClasses
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1866
		#
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1867
		my ( $must_attr, $may_attr ) = $self->fetch_attributes( \@ocs );
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1868
		print $fh "$_: \n"   foreach @{ $must_attr };
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1869
		print $fh "# $_: \n" foreach @{ $may_attr };
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1870
		close $fh;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1871
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1872
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1873
	# checksum the file.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1874
	#
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1875
	my $hash_orig = $self->chksum( $self->{'create_file'} );
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1876
	system( $self->{'editor'}, $self->{'create_file'} ) && die "Unable to launch editor: $!\n";
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1877
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1878
	# detect a total lack of change
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1879
	#
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1880
	if ( $hash_orig eq $self->chksum($self->{'create_file'}) ) {
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1881
		print "Entry not modified.\n";
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1882
		unlink $self->{'create_file'};
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1883
		$self->{'create_file'} = undef;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1884
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1885
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1886
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1887
	# load in LDIF
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1888
	#
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1889
	my $ldif = Net::LDAP::LDIF->new( $self->{'create_file'}, 'r', onerror => 'warn' );
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1890
	my $e	 = $ldif->read_entry();
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1891
	unless ( $e ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1892
		print "Unable to parse LDIF.\n";
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1893
		unlink $self->{'create_file'};
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1894
		$self->{'create_file'} = undef;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1895
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1896
	}
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1897
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1898
	# create the new entry.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1899
	#
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1900
	$e->changetype('add');
48
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1901
	my $create = sub { return $e->update($self->ldap()) };
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  1902
	my $rv = $self->with_retry( $create );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1903
	print $rv->error(), "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1904
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1905
	if ( $rv->code() != LDAP_SUCCESS && $self->prompt_edit_again() ) {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1906
		return $self->run_create();
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1907
	}
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1908
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1909
	$self->update_entries( clearcache => 1 );
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1910
	unlink $self->{'create_file'};
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1911
	$self->{'create_file'} = undef;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1912
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1913
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1914
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1915
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1916
### Remove an entry (or entries) from the LDAP directory.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1917
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1918
sub run_delete
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1919
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1920
	my $self = shift;
67
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1921
	my @args  = @_;
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1922
	my @matches;
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1923
	my $s;
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1924
	my $verbose;
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1925
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1926
	unless ( scalar @args ) {
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1927
		print "No dn specified.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1928
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1929
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1930
67
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1931
	# Flags.
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1932
	#
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1933
	if ( $args[0] =~ /^\-v/ ) {
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1934
		$verbose = 1;
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1935
		shift @args;
6
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
  1936
	}
67
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1937
	
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1938
	# Separate real args from filter arguments.
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1939
	#
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1940
	foreach my $dn ( @args ) {
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1941
		if ( $dn eq '*' ) {
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1942
			$s = $self->search({ scope => 'one' });
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1943
			map { push @matches, $_ } @{ $s->{'entries'} } if $s->{'code'} == LDAP_SUCCESS;
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1944
		}
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1945
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1946
		# Search by filter
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1947
		#
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1948
		else {
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1949
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1950
			my $filter = $self->make_filter( [$dn] ) or next;
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1951
			$s = $self->search({ scope => 'one', filter => $filter });
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1952
			if ( scalar @{$s->{'entries'}} != 0 ) {
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1953
				map { push @matches, $_ } @{ $s->{'entries'} } if $s->{'code'} == LDAP_SUCCESS;
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1954
			}
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1955
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1956
			# Search by exact DN.
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1957
			#
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1958
			else {
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1959
				$dn = $self->path_to_dn( $dn );
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1960
				$s = $self->search({ base => $dn, vals => 0 });
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1961
				my $e = ${ $s->{'entries'} }[0];
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1962
				push @matches, $e if $s->{'code'} == LDAP_SUCCESS;
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1963
			}
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1964
		}
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1965
	}
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1966
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1967
	# Unique the matchset for a consistent count, keyed by DN.
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1968
	#
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1969
	my @uniq_matches = keys %{{ map { $_->dn => 1 } @matches }};
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1970
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1971
	my $mcount = scalar @uniq_matches;
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1972
	if ( $mcount == 0 ) {
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1973
		print "Nothing matched.\n";
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1974
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1975
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1976
67
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1977
	if ( $verbose ) {
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1978
		print "* $_\n" foreach @uniq_matches;
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1979
	}
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1980
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1981
	print "About to remove $mcount item(s).  Are you sure? [Ny]: ";
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1982
	chomp( my $resp = <STDIN> );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1983
	return unless $resp =~ /^y/i;
67
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1984
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1985
	my %seen;
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1986
	foreach my $e ( @matches ) {
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1987
		my $dn = $e->dn();
67
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1988
		next if $seen{ $dn };
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1989
		my $rv = $self->ldap->delete( $dn );
67
86e3374a40a3 Add 'rm' for a fully qualified DN, instead of only working with RDN.
Mahlon E. Smith <mahlon@laika.com>
parents: 62
diff changeset
  1990
		$seen{ $dn }++;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1991
		print "$dn: ", $rv->error(), "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1992
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1993
   
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1994
	$self->update_entries( clearcache => 1 );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1995
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1996
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  1997
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1998
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  1999
### Fetch an entry from the directory, write it out to disk
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2000
### as LDIF, launch an editor, then compare changes and write
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2001
### it back to the directory.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2002
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2003
sub run_edit
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2004
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2005
	my $self = shift;
41
3e7c107f8b93 Take only second argument for run_{cd,edit,mkdir}
Salvatore Bonaccorso <carnil@debian.org>
parents: 40
diff changeset
  2006
	my $dn   = shift;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2007
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2008
	unless ( $dn ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2009
		print "No dn provided.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2010
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2011
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2012
31
a3a710f720dd run_cat: convert to using path_to_dn()
Peter Marschall <peter@adpm.de>
parents: 30
diff changeset
  2013
	# convert given path to DN
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  2014
	$dn = $self->path_to_dn( $dn );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2015
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2016
	# sanity check
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2017
	#
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2018
	my $s = $self->search({ base => $dn, vals => 1 });
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2019
	unless ( $s->{'code'} == LDAP_SUCCESS ) {
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2020
		print $s->{'message'} . "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2021
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2022
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2023
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2024
	# fetch entry.
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2025
	my $e = ${ $s->{'entries'} }[0];
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2026
	$e->changetype( 'modify' );
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2027
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2028
	# write it out to disk.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2029
	#
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2030
	unless( $self->{'edit_again'} )  {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2031
		my $ldif = $self->ldif(1);
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2032
		$ldif->write_entry( $e );
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2033
		$ldif->done();  # force sync
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2034
	}
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2035
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2036
	# load it into an array for potential comparison
118
2d7f16eff198 Use local implementation of slurp()
Davor Ocelic <docelic@crystallabs.io>
parents: 117
diff changeset
  2037
	open LDIF, "$self->{'ldif_fname'}";
6
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
  2038
	my @orig_ldif = <LDIF>;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2039
	close LDIF;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2040
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2041
	# append optional, unused attributes as comments for fast reference.
48
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2042
	#
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2043
	unless ( $self->{'edit_again'} )  {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2044
		my %current_attrs = map { $_ => 1 } $e->attributes();
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2045
		my ( $must_attr, $may_attr ) = $self->fetch_attributes( $e->get_value('objectClass', asref => 1) );
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2046
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2047
		open LDIF, ">> $self->{'ldif_fname'}";
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2048
		foreach my $opt_attr ( sort { $a cmp $b } @{$may_attr} ) {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2049
			next if $current_attrs{ $opt_attr };
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2050
			print LDIF "# " . $opt_attr . ":\n";
48
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2051
		}
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2052
		close LDIF;
48
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2053
	}
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2054
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2055
	# checksum it, then open it in an editor
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2056
	#
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2057
	my $hash_orig = $self->chksum( $self->{'ldif_fname'} );
81
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  2058
	my @edit_args = split /\s+/, $self->{'editor'};
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  2059
	push @edit_args, $self->{'ldif_fname'};
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  2060
	system( @edit_args ) && die "Unable to launch editor: $!\n";
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2061
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2062
	# detect a total lack of change
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2063
	#
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2064
	if ( $hash_orig eq $self->chksum($self->{'ldif_fname'}) ) {
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2065
		print "Entry not modified.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2066
		unlink $self->{'ldif_fname'};
54
0cc20d93ff50 Fix for edge case Diff::LCS traversals. Also ensure re-edit state is
Mahlon E. Smith <mahlon@laika.com>
parents: 51
diff changeset
  2067
		$self->{'edit_again'} = undef;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2068
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2069
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2070
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2071
	# check changes for basic LDIF validity
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2072
	#
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2073
	while( ! $self->load_ldif($self->{'ldif_fname'}) ) {
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2074
		print "Unable to parse LDIF.\n";
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2075
		if ( $self->prompt_edit_again() ) {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2076
			system( $self->{'editor'}, $self->{'ldif_fname'} );
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2077
		}
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2078
		else {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2079
			unlink $self->{'ldif_fname'};
54
0cc20d93ff50 Fix for edge case Diff::LCS traversals. Also ensure re-edit state is
Mahlon E. Smith <mahlon@laika.com>
parents: 51
diff changeset
  2080
			$self->{'edit_again'} = undef;
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2081
			return;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2082
		}
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2083
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2084
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2085
	# load changes into a new array for comparison
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2086
	#
118
2d7f16eff198 Use local implementation of slurp()
Davor Ocelic <docelic@crystallabs.io>
parents: 117
diff changeset
  2087
	open LDIF, "$self->{'ldif_fname'}";
6
46dfe9d6f368 Update documentation, now that multiline edits work. Minor other
Mahlon E. Smith <mahlon@laika.com>
parents: 5
diff changeset
  2088
	my @new_ldif = <LDIF>;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2089
	close LDIF;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2090
81
c39a13f112b6 Break LCS diffing to its own function. Minor style tweaks.
Mahlon E. Smith <mahlon@martini.nu>
parents: 80
diff changeset
  2091
	$self->diff( $e, \@orig_ldif, \@new_ldif );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2092
48
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2093
	my $update = sub { return $e->update( $self->ldap ); };
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2094
	my $rv = $self->with_retry( $update );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2095
	print $rv->error(), "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2096
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2097
	if ( $rv->code() != LDAP_SUCCESS && $self->prompt_edit_again() ) {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2098
		$self->{'edit_again'} = 1;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2099
		return $self->run_edit( $dn );
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2100
	}
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2101
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2102
	unlink $self->{'ldif_fname'};
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2103
	$self->{'edit_again'} = undef;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2104
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2105
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2106
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2107
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2108
### Display current tunable runtime settings.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2109
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2110
sub run_env
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2111
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2112
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2113
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2114
	print YAML::Syck::Dump( { map { $conf->{$_} ? ($_, $conf->{$_}) : ()} sort @{ $self->{'env'}} } );
116
6a0b1209dafa Support arrays/hashes in 'env' (WIP)
Davor Ocelic <docelic@crystallabs.io>
parents: 115
diff changeset
  2115
	print "\n"
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2116
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2117
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2118
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2119
### Alter settings.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2120
###
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2121
sub run_setenv
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2122
{
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2123
	my $self = shift;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2124
	my ( $key, $val ) = @_;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2125
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2126
	( $key, $val ) = split /=/, $key if $key && ! defined $val;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2127
	return unless $key && defined $val;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2128
	$key = lc $key;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2129
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2130
	$conf->{$key} = $val;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2131
	return;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2132
}
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2133
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2134
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2135
### Alter settings.
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2136
###
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2137
sub run_unsetenv
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2138
{
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2139
	my $self = shift;
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2140
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2141
	for(@_) {
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2142
		delete $conf->{$_}
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2143
	}
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2144
	return;
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2145
}
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2146
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2147
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2148
### Search across the directory and display matching entries.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2149
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2150
sub run_grep
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2151
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2152
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2153
	my ( $recurse, $filter, $base ) = @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2154
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2155
	# set 'recursion'
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2156
	unless ( $recurse && $recurse =~ /\-r|recurse/ ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2157
		# shift args to the left
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2158
		( $recurse, $filter, $base ) = ( undef, $recurse, $filter );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2159
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2160
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2161
	$filter = Net::LDAP::Filter->new( $filter );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2162
	unless ( $filter ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2163
		print "Invalid search filter.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2164
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2165
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2166
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2167
	# support '*'
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2168
	$base = $self->base() if ! $base or $base eq '*';
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2169
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2170
	unless ( $base ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2171
		print "No search base specified.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2172
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2173
	}
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  2174
31
a3a710f720dd run_cat: convert to using path_to_dn()
Peter Marschall <peter@adpm.de>
parents: 30
diff changeset
  2175
	# convert base path to DN
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  2176
	$base = $self->path_to_dn( $base );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2177
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2178
	$self->debug("Filter parsed as: " . $filter->as_string() . "\n");
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2179
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2180
	my $s = $self->search({
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2181
		scope  => $recurse ? 'sub' : 'one',
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2182
		base   => $base,
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2183
		filter => $filter
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2184
	});
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2185
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2186
	foreach my $e ( @{ $s->{'entries'} } ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2187
		my $dn = $e->dn();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2188
		print "$dn\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2189
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2190
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2191
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2192
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2193
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2194
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2195
### Generate and display a list of LDAP entries, relative to the current
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2196
### location the command was run from.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2197
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2198
sub run_list
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2199
{
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2200
	my $self  = shift;
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2201
	my @args  = @_;
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2202
	my @attrs = ();
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2203
	my $filter;
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2204
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2205
	# flag booleans
114
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2206
	my ( $recurse, $long, $all );
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2207
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2208
	# parse arguments: [ <option> ...] [<filter> ...] [<attribute> ...]
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2209
	#
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2210
	if ( scalar @args ) {
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2211
		# options: support '-l' or '-R' listings
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2212
		if ( $args[0] =~ /^\-(\w+)/o ) {
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2213
			my $flags = $1;
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2214
			$recurse  = $flags =~ /R/;
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2215
			$long	  = $flags =~ /l/;
114
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2216
			$all	  = $flags =~ /a/;
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2217
			shift( @args );
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2218
		}
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2219
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2220
		my @filters;
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2221
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2222
		# get filter elements from argument list
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2223
		#
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2224
		while ( @args && $self->is_valid_filter($args[0]) ) {
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2225
			push( @filters, shift(@args) );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2226
		}
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2227
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2228
		# No filter for display?  Default to all entries.
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2229
		push( @filters, '(objectClass=*)' ) unless scalar @filters;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2230
		
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2231
		# construct OR'ed filter from filter elements
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2232
		$filter = $self->make_filter( \@filters );
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2233
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2234
		# remaining arguments must be attributes
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2235
		push( @attrs, @args );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2236
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2237
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2238
	# Get all attributes if none are specified, and we're in long-list mode.
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2239
	push( @attrs, '*' )  if $long && ! scalar @attrs;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2240
4
5a65bc849363 Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
  2241
	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
  2242
		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
  2243
		vals   => 1,
5a65bc849363 Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
  2244
		filter => $filter,
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2245
		attrs  => [ @attrs, 'hasSubordinates' ]
4
5a65bc849363 Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
  2246
	});
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2247
	if ( $s->{'code'} ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2248
		print "$s->{'message'}\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2249
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2250
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2251
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2252
	# if an entry doesn't have a description field,
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2253
	# try and show some nice defaults for ls -l !
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2254
	# 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2255
	# objectClass -> Attribute to show
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2256
	#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2257
	my %descs = %{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2258
		$conf->{'descmaps'}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2259
		  || {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2260
			posixAccount => 'gecos',
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2261
			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
  2262
			ipHost	     => 'ipHostNumber',
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2263
		  }
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2264
	  };
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2265
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2266
	# iterate and print
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2267
	#
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2268
	my $dn_count = 0;
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2269
	my $base = $self->base();
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2270
	foreach my $e ( sort { $a->dn() cmp $b->dn() } @{ $s->{'entries'} } ) {
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2271
		my $dn = $e->dn();
114
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2272
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2273
		# Later, turn this into '.' and '..' display
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2274
		unless($all) {
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2275
			next if lc( $dn ) eq lc( $base );
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2276
		}
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2277
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2278
		if ( ! $long ) {
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2279
			# strip the current base from the dn, if we're recursing and not in long mode
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2280
			if ( $recurse ) {
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2281
				$dn =~ s/,$base//oi;
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2282
			# only show RDN unless -l was given
115
5ace587c3ea3 Add alias, unalias, configfile
Davor Ocelic <docelic@crystallabs.io>
parents: 114
diff changeset
  2283
			} else {
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2284
				$dn = canonical_dn( [shift(@{ldap_explode_dn($dn, casefold => 'none')})], casefold => 'none' )
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2285
			}
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2286
		}
4
5a65bc849363 Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
  2287
114
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2288
		my $type = '-'; # Assume the entry is a leaf
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2289
4
5a65bc849363 Append a trailing slash to entries that contain other entries. Thanks
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
  2290
		# 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
  2291
		# trailing slash.
114
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2292
		if( $e->get_value('hasSubordinates') && $e->get_value('hasSubordinates') eq 'TRUE') {
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2293
			$dn .= '/';
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2294
			$type = 'd'
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2295
		}
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2296
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2297
		# additional arguments/attributes were given; show their values
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2298
		#
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2299
		if ( scalar @args ) {
114
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2300
			my @line = ( $type, $dn );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2301
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2302
			foreach my $attr ( @args ) {
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2303
				my @vals = $e->get_value( $attr );
114
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2304
				push( @line, join(',', @vals) );
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2305
			}
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2306
114
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2307
			print join( "\t", @line )."\n";
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2308
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2309
		}
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2310
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2311
		# show descriptions
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2312
		#
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2313
		else {
114
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2314
			my $line = "$type $dn";
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2315
34
40c3719c87d4 fix 'ls -R' output, minor style cleanup
Mahlon E. Smith <mahlon@martini.nu>
parents: 33
diff changeset
  2316
			my $desc = $e->get_value( 'description' );
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2317
			if ( $desc ) {
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2318
				$desc =~ s/\n.*//s; # 1st line only
114
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2319
				$line .= " ($desc)";
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2320
			} else {
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2321
				# no desc?  Try and infer something useful
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2322
				# to display.
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2323
				#
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2324
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2325
				# pull objectClasses, hash for lookup speed
48
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2326
				my @oc = $e->get_value( 'objectClass' );
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2327
				my %ochash;
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2328
				map { $ochash{$_} = 1 } @oc;
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2329
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2330
				foreach my $d_listing ( sort keys %descs ) {
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2331
					if ( exists $ochash{ $d_listing } ) {
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2332
						my $str = $e->get_value( $descs{ $d_listing }, asref => 1 );
114
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2333
						$line .= ' (' . (join ', ', @$str) . ')' if $str && scalar @$str;
27
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2334
					}
7d170d1bc17b run_list: new argument syntax: [<options>] [<filter>] [<attributes>]
Peter Marschall <peter@adpm.de>
parents: 26
diff changeset
  2335
					next;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2336
				}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2337
			}
114
35b7143830e8 With ls -l, print file type ('-' or 'd') as first field of output
Davor Ocelic <docelic@crystallabs.io>
parents: 113
diff changeset
  2338
			print "$line\n";
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2339
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2340
		$dn_count++;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2341
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2342
	
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2343
	print "\n$dn_count " .
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2344
		( $dn_count == 1 ? 'object.' : 'objects.') .
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2345
		"\n" if $long;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2346
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2347
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2348
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2349
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2350
### Create a new organizationalUnit entry.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2351
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2352
sub run_mkdir
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2353
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2354
	my $self = shift;
41
3e7c107f8b93 Take only second argument for run_{cd,edit,mkdir}
Salvatore Bonaccorso <carnil@debian.org>
parents: 40
diff changeset
  2355
	my $dir  = shift;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2356
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2357
	unless ( $dir ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2358
		print "No 'directory' provided.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2359
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2360
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2361
38
12f279ef4f9d Backout the additional objectClasses patch for mkdir: same behavior can
Mahlon E. Smith <mahlon@martini.nu>
parents: 37
diff changeset
  2362
	# normalize name, if it is not yet a legal DN
12f279ef4f9d Backout the additional objectClasses patch for mkdir: same behavior can
Mahlon E. Smith <mahlon@martini.nu>
parents: 37
diff changeset
  2363
	$dir = 'ou=' . $dir unless canonical_dn( $dir );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2364
38
12f279ef4f9d Backout the additional objectClasses patch for mkdir: same behavior can
Mahlon E. Smith <mahlon@martini.nu>
parents: 37
diff changeset
  2365
	# convert given path to full DN
12f279ef4f9d Backout the additional objectClasses patch for mkdir: same behavior can
Mahlon E. Smith <mahlon@martini.nu>
parents: 37
diff changeset
  2366
	$dir = $self->path_to_dn( $dir );
29
bd95c3aea253 mkdir: support more objectclasses
Peter Marschall <peter@adpm.de>
parents: 28
diff changeset
  2367
bd95c3aea253 mkdir: support more objectclasses
Peter Marschall <peter@adpm.de>
parents: 28
diff changeset
  2368
	# get RDN: naming attributes (lower-case) and their values
38
12f279ef4f9d Backout the additional objectClasses patch for mkdir: same behavior can
Mahlon E. Smith <mahlon@martini.nu>
parents: 37
diff changeset
  2369
	my %rdn = %{ shift(@{ ldap_explode_dn($dir, casefold => 'lower') }) };
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2370
38
12f279ef4f9d Backout the additional objectClasses patch for mkdir: same behavior can
Mahlon E. Smith <mahlon@martini.nu>
parents: 37
diff changeset
  2371
	# add
48
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2372
	my $mkdir = sub {
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2373
		return $self->ldap()->add( $dir, attr => [
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2374
			objectClass => [ 'top', 'organizationalUnit' ], %rdn
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2375
		]);
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2376
	};
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2377
48
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2378
	my $rv = $self->with_retry( $mkdir );
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2379
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2380
	print $rv->error(), "\n";
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2381
	$self->update_entries( clearcache => 1 );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2382
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2383
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2384
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2385
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2386
### Alter an entry's DN.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2387
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2388
sub run_move
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2389
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2390
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2391
	my ( $s_dn, $d_dn ) = @_;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2392
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2393
	unless ( $s_dn ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2394
		print "No source dn provided.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2395
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2396
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2397
	unless ( $d_dn ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2398
		print "No destination dn provided.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2399
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2400
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2401
31
a3a710f720dd run_cat: convert to using path_to_dn()
Peter Marschall <peter@adpm.de>
parents: 30
diff changeset
  2402
	# convert given source path to DN
33
057fefab56b0 Repair broken path behavior, remove unneeded #path_to_dn 'relative'
Mahlon E. Smith <mahlon@martini.nu>
parents: 32
diff changeset
  2403
	$s_dn = $self->path_to_dn( $s_dn );
31
a3a710f720dd run_cat: convert to using path_to_dn()
Peter Marschall <peter@adpm.de>
parents: 30
diff changeset
  2404
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2405
	unless ( $self->is_valid_dn( $s_dn ) ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2406
		print "No such object\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2407
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2408
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2409
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2410
	# see if we're moving the entry to a totally new path
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2411
	my ( $new_dn, $old_dn );
82
57a1335691e5 Allow the period character when moving an entry by full DN.
Mahlon E. Smith <mahlon@martini.nu>
parents: 81
diff changeset
  2412
	( $d_dn, $new_dn ) = ( $1, $2 ) if $d_dn =~ /^([\.\-\w=]+),(.*)$/;
57a1335691e5 Allow the period character when moving an entry by full DN.
Mahlon E. Smith <mahlon@martini.nu>
parents: 81
diff changeset
  2413
	$old_dn = $1 if $s_dn =~ /^[\.\-\w=]+,(.*)$/;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2414
48
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2415
	my $moddn = sub {
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2416
		return $self->ldap()->moddn(
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2417
			$s_dn,
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2418
			newrdn	     => $d_dn,
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2419
			deleteoldrdn => 1,
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2420
			newsuperior  => $new_dn
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2421
		);
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2422
	};
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2423
	my $rv = $self->with_retry( $moddn );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2424
	print $rv->error(), "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2425
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2426
	# clear caches
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2427
	$self->{'cache'}->{ $new_dn } = {} if $new_dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2428
	$self->{'cache'}->{ $old_dn } = {} if $old_dn;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2429
	$self->update_entries( clearcache => 1 );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2430
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2431
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2432
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2433
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2434
### Change the 'userPassword' attribute of an entry, if
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2435
### supported by the LDAP server.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2436
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2437
sub run_passwd 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2438
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2439
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2440
	my $dn   = shift || $self->base();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2441
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2442
	$self->{'root_dse'} ||= $self->ldap->root_dse();
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2443
93
4c7843b9a047 Make password extension usage consistent with pager control.
Mahlon E. Smith <mahlon@martini.nu>
parents: 92
diff changeset
  2444
	unless ( $self->{'root_dse'}->supported_extension(LDAP_EXTENSION_PASSWORD_MODIFY) ) {
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2445
		print "Sorry, password changes not supported by LDAP server.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2446
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2447
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2448
31
a3a710f720dd run_cat: convert to using path_to_dn()
Peter Marschall <peter@adpm.de>
parents: 30
diff changeset
  2449
	# convert given path to DN
40
c6a3abc56c74 Fix bug introduced in rev:a3a710f720dd with passwd arguments.
Mahlon E. Smith <mahlon@martini.nu>
parents: 38
diff changeset
  2450
	$dn = $self->path_to_dn( $dn );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2451
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2452
	my $s = $self->search( { base => $dn, scope => 'base' } );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2453
	if ( $s->{'code'} ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2454
		print $s->{'message'}, "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2455
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2456
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2457
	my $e = ${ $s->{'entries'} }[0];
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2458
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2459
	unless ( $e->exists('userPassword') ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2460
		print "No userPassword attribute for $dn\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2461
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2462
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2463
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2464
	print "Changing password for $dn\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2465
	Term::ReadKey::ReadMode 2;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2466
	print "Enter new password: ";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2467
	chomp( my $pw  = <STDIN> );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2468
	print "\nRetype new password: ";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2469
	chomp( my $pw2 = <STDIN> );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2470
	print "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2471
	Term::ReadKey::ReadMode 0;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2472
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2473
	if ( $pw ne $pw2 ) {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2474
		print "Sorry, passwords do not match.\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2475
		return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2476
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2477
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2478
	my $setpw = sub { return $self->ldap->set_password( user => $dn, newpasswd => $pw ); };
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2479
	my $rv = $self->with_retry( $setpw );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2480
16
a2e3faa3d2fc use symbolic LDAP error codes instead of numbers
Peter Marschall <peter@adpm.de>
parents: 15
diff changeset
  2481
	if ( $rv->code() == LDAP_SUCCESS ) {
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2482
		print "Password updated successfully.\n";
48
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2483
	}
fe27dfe5179e More robust path for connection retries. Show optional, unused
Mahlon E. Smith <mahlon@laika.com>
parents: 46
diff changeset
  2484
	else {
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2485
		print "Password error: " . $rv->error() . "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2486
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2487
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2488
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2489
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2490
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2491
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2492
### Display the current working "directory".
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2493
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2494
sub run_pwd 
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2495
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2496
	my $self = shift;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2497
	print $self->base() . "\n";
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2498
	return;   
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2499
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2500
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2501
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2502
### Display the currently bound user.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2503
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2504
sub run_whoami
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2505
{
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2506
	my $self = shift;
57
85cc85d0c1b1 Catch a case where the LDAP object is defined, but in a state that
Mahlon E. Smith <mahlon@laika.com>
parents: 56
diff changeset
  2507
	my $msg = ( $conf->{'binddn'} || 'anonymous bind' ) . ' (' .  $conf->{'server'} . ')';
85cc85d0c1b1 Catch a case where the LDAP object is defined, but in a state that
Mahlon E. Smith <mahlon@laika.com>
parents: 56
diff changeset
  2508
	print "$msg\n";
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2509
	return;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2510
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2511
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2512
51
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2513
### Show basic information for an entry (DN) or list of objectClasses.
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2514
###
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2515
###   structural/auxillary classes
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2516
###   required attributes
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2517
###   optional attributes
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2518
###
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2519
sub run_inspect
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2520
{
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2521
	my $self = shift;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2522
	my @ocs  = @_;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2523
	my $dn   = $ocs[0];
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2524
	my ( $must_attr, $may_attr );
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2525
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2526
	unless ( $dn ) {
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2527
		print "No DN or objectClass(es) provided.\n";
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2528
		return;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2529
	}
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2530
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2531
	# "Magic" argument that dumps all raw schema information.
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2532
	#
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2533
	if ( $dn eq '_schema' ) {
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2534
		$self->{'schema'}->dump();
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2535
		return;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2536
	}
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2537
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2538
	# one argument -- if it successfully resolves to a valid DN, fetch
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2539
	# the objectClass list from it.
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2540
	#
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2541
	if ( scalar @ocs == 1 ) {
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2542
		$dn = $self->base() if $dn eq '.';
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2543
		$dn = $self->path_to_dn( $dn );
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2544
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2545
		my $s = $self->search({ base => $dn, vals => 1, attrs => ['objectClass'] });
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2546
		if ( $s->{'code'} == LDAP_SUCCESS ) {
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2547
			my $e = ${ $s->{'entries'} }[0];
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2548
			@ocs = $e->get_value('objectClass');
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2549
		}
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2550
	}
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2551
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2552
	# get the complete attributes list.
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2553
	#
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2554
	( $must_attr, $may_attr ) = $self->fetch_attributes( \@ocs );
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2555
	my %must = map { $_ => 1 } @{$must_attr};
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2556
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2557
	# Output objectClass chains and flags.
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2558
	#
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2559
	print "ObjectClasses:\n";
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2560
	foreach my $oc ( sort @ocs ) {
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2561
		my @sups = $self->findall_supers( $oc );
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2562
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2563
		my @oc_chain = ( $oc, @sups );
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2564
		my @oc_out;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2565
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2566
		foreach my $oc ( @oc_chain ) {
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2567
			my $oc_obj = $self->{'schema'}->objectclass( $oc );
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2568
			next unless $oc_obj;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2569
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2570
			$oc = $oc . ' (' . 'structural' . ')' if $oc_obj->{'structural'};
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2571
			push( @oc_out, $oc );
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2572
		}
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2573
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2574
		print "    " . join( ' --> ', @oc_out ) . "\n" if scalar @oc_out;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2575
	}
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2576
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2577
	# Output attributes and flags.
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2578
	#
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2579
	print "\nAttributes:\n";
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2580
	foreach my $attr ( sort (@{$must_attr}, @{$may_attr}) ) {
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2581
		my @flaglist;
107
3e31beb6c845 Do not sort flaglist twice
Davor Ocelic <docelic@crystallabs.io>
parents: 106
diff changeset
  2582
3e31beb6c845 Do not sort flaglist twice
Davor Ocelic <docelic@crystallabs.io>
parents: 106
diff changeset
  2583
		push ( @flaglist, $must{$attr} ? 'required' : 'optional' );
3e31beb6c845 Do not sort flaglist twice
Davor Ocelic <docelic@crystallabs.io>
parents: 106
diff changeset
  2584
51
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2585
		if ( $self->{'schema'}->attribute( $attr )->{'single-value'} ) {
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2586
			push ( @flaglist, 'single-value' );
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2587
		}
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2588
		else {
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2589
			push ( @flaglist, 'multivalue' );
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2590
		}
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2591
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2592
		my $flags = '';
107
3e31beb6c845 Do not sort flaglist twice
Davor Ocelic <docelic@crystallabs.io>
parents: 106
diff changeset
  2593
		$flags = (' (' . join( ', ', @flaglist ) . ')') if scalar @flaglist > 0;
51
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2594
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2595
		printf( "    %s%s\n", $attr, $flags );
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2596
	}
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2597
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2598
	print "\n";
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2599
	return;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2600
}
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2601
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2602
104
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  2603
### Inject various autocomplete and alias routines into the symbol table.
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  2604
###
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  2605
{ no strict 'refs';
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2606
	local $| = 1;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2607
	my %aliases;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2608
111
252611c8d283 Make aliases be auto-detected and inserted into help texts.
Davor Ocelic <docelic@crystallabs.io>
parents: 110
diff changeset
  2609
	# In first pass, only identify aliases.
104
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  2610
	while(my($cmd, $data) = each %cmd_map ) {
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  2611
		# If command is an alias, it is enough to mark it as such.
104
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  2612
		if( $$data[0]) {
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2613
			$aliases{$$data[0]} ||= [];
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2614
			push @{$aliases{$$data[0]}}, $cmd;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2615
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  2616
		# If it is a real command, let's do more work.
111
252611c8d283 Make aliases be auto-detected and inserted into help texts.
Davor Ocelic <docelic@crystallabs.io>
parents: 110
diff changeset
  2617
		}
252611c8d283 Make aliases be auto-detected and inserted into help texts.
Davor Ocelic <docelic@crystallabs.io>
parents: 110
diff changeset
  2618
	}
252611c8d283 Make aliases be auto-detected and inserted into help texts.
Davor Ocelic <docelic@crystallabs.io>
parents: 110
diff changeset
  2619
252611c8d283 Make aliases be auto-detected and inserted into help texts.
Davor Ocelic <docelic@crystallabs.io>
parents: 110
diff changeset
  2620
	# In second pass, deal with non-aliases.
252611c8d283 Make aliases be auto-detected and inserted into help texts.
Davor Ocelic <docelic@crystallabs.io>
parents: 110
diff changeset
  2621
	while(my($cmd, $data) = each %cmd_map ) {
252611c8d283 Make aliases be auto-detected and inserted into help texts.
Davor Ocelic <docelic@crystallabs.io>
parents: 110
diff changeset
  2622
		if( !$$data[0]) {
110
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  2623
			# If completer is defined, set it.
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  2624
			if( $$data[1]) {
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  2625
				my $comp_sub = "comp_$cmd";
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  2626
				*$comp_sub   = \&{$$data[1]}
dbceec51da98 Fix autocompleter for all commands.
Davor Ocelic <docelic@crystallabs.io>
parents: 109
diff changeset
  2627
			}
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2628
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2629
			# Define help and summary functions for the command:
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2630
			my $pod = ''; open my $io, '>', \$pod;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2631
			Pod::Usage::pod2usage( -exitval  => 'NOEXIT', -verbose  => 99, -sections => "SHELL COMMANDS/${\( quotemeta $cmd )}", -output => \*$io );
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2632
			my @pod = split /\n/, $pod;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2633
			my $summary = $pod[1];
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2634
			if($summary) {
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2635
				$summary =~ s/^\s+//s;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2636
				$summary =~ s/\s+$//s;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2637
				$summary =~ s/\s+/ /s;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2638
			}
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2639
			my $help = join "\n", @pod;
111
252611c8d283 Make aliases be auto-detected and inserted into help texts.
Davor Ocelic <docelic@crystallabs.io>
parents: 110
diff changeset
  2640
			if( $aliases{$cmd}) {
252611c8d283 Make aliases be auto-detected and inserted into help texts.
Davor Ocelic <docelic@crystallabs.io>
parents: 110
diff changeset
  2641
				local $" = ', ';
252611c8d283 Make aliases be auto-detected and inserted into help texts.
Davor Ocelic <docelic@crystallabs.io>
parents: 110
diff changeset
  2642
				$help .= "\n\n    Aliases: @{$aliases{$cmd}}\n"
252611c8d283 Make aliases be auto-detected and inserted into help texts.
Davor Ocelic <docelic@crystallabs.io>
parents: 110
diff changeset
  2643
			}
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2644
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2645
			my $helpfunc = sub { "$help\n" };
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2646
			*{"help_$cmd"} = \&$helpfunc;
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2647
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2648
			my $summfunc = sub { $summary };
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2649
			*{"smry_$cmd"} = \&$summfunc;
104
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  2650
		}
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2651
	}
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2652
111
252611c8d283 Make aliases be auto-detected and inserted into help texts.
Davor Ocelic <docelic@crystallabs.io>
parents: 110
diff changeset
  2653
	# In third pass, actually register found aliases.
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2654
	while(my($cmd,$aliases) = each %aliases) {
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2655
		my $aliasfunc = sub { @$aliases };
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2656
		*{"alias_$cmd"} = \&$aliasfunc;
104
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  2657
	}
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  2658
}
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  2659
b60189929c4b Streamline %cmd_map
Davor Ocelic <docelic@crystallabs.io>
parents: 103
diff changeset
  2660
91
80ec00959fbd Fix spelling errors
Salvatore Bonaccorso <carnil@debian.org>
parents: 87
diff changeset
  2661
### Recursively walk an objectClass hierarchy, returning an array
51
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2662
### of inheritence.
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2663
###
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2664
sub findall_supers
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2665
{
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2666
	my $self = shift;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2667
	my $oc   = shift or return;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2668
	my @found;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2669
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2670
	foreach my $sup ( $self->{'schema'}->superclass($oc) ) {
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2671
		push( @found, $sup );
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2672
		push( @found, $self->findall_supers( $sup ) );
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2673
	}
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2674
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2675
	return @found;
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2676
}
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2677
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2678
27bbe75233a3 Add the "inspect" command, which provides some quick reference for
Mahlon E. Smith <mahlon@laika.com>
parents: 50
diff changeset
  2679
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2680
########################################################################
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2681
### M A I N
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2682
########################################################################
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2683
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2684
package main;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2685
use strict;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2686
use warnings;
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2687
use Fatal qw/open/;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2688
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2689
$0 = 'shelldap';
122
d6a75d14db12 Update CONTRIBUTORS and bump version.
Mahlon E. Smith <mahlon@martini.nu>
parents: 121
diff changeset
  2690
my $VERSION = '1.5.0';
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2691
117
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
  2692
use Getopt::Long qw(:config no_ignore_case);
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
  2693
use YAML::Syck qw//;
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
  2694
use Pod::Usage qw//;
87
475aff91675b eval optional modules in blocks instead of strings.
Mahlon E. Smith <mahlon@martini.nu>
parents: 84
diff changeset
  2695
eval { require Term::ReadLine::Gnu; };
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2696
warn qq{Term::ReadLine::Gnu not installed.
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2697
Continuing, but shelldap is of limited usefulness without it.\n\n} if $@;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2698
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2699
# get config - rc file first, command line overrides
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2700
use vars '$conf';
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2701
$conf = (load_config())[1];
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2702
$conf ||= {};
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2703
Getopt::Long::GetOptions(
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2704
	$conf, 
117
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
  2705
	'server|h|H=s',
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
  2706
	'port|p=s',
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2707
	'configfile|f=s',
18
db47ba64ebda accept short option names for some options
Peter Marschall <peter@adpm.de>
parents: 17
diff changeset
  2708
	'binddn|D=s',
db47ba64ebda accept short option names for some options
Peter Marschall <peter@adpm.de>
parents: 17
diff changeset
  2709
	'basedn|b=s',
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2710
	'cacheage=i',
116
6a0b1209dafa Support arrays/hashes in 'env' (WIP)
Davor Ocelic <docelic@crystallabs.io>
parents: 115
diff changeset
  2711
	'cmdline_attributes|attributes=s@',
92
a1aa55019077 Optionally use the server side pager control for search results.
Mahlon E. Smith <mahlon@martini.nu>
parents: 91
diff changeset
  2712
	'paginate=i',
117
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
  2713
	'promptpass|W!',
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
  2714
	'pass|w=s',
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
  2715
	'passfile|y=s',
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2716
	'timeout=i',
62
ed8253b3105a Add quick documentation blurb for SASL mechanisms. Make SASL
Mahlon E. Smith <mahlon@laika.com>
parents: 61
diff changeset
  2717
	'sasl|Y=s',
117
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
  2718
	'simple|x!' => sub {
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
  2719
		my($opt,$arg) = @_;
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
  2720
		$conf->{sasl} = $arg ? undef : 'PLAIN CRAM-MD5 GSSAPI'
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
  2721
	},
3
0f815f3daaf7 Add options to support ssl key verification when connecting with TLS.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
  2722
	'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
  2723
	'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
  2724
	'tls_key=s',
117
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
  2725
	'tls|Z|ZZ!',
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
  2726
	'debug|v',
eb1bcbaf9763 Improve accepted command line options
Davor Ocelic <docelic@crystallabs.io>
parents: 116
diff changeset
  2727
	'version',
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2728
	 help => sub {
109
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2729
	  Pod::Usage::pod2usage(
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2730
	  	-verbose => 1,
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2731
	  	-message => "\n$0 command line flags\n" . '-' x 65
d37c6197818f Multiple help-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 108
diff changeset
  2732
	  );
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2733
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2734
);
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2735
37
7a8855e7cfb8 Add a --version flag.
Mahlon E. Smith <mahlon@martini.nu>
parents: 36
diff changeset
  2736
# show version
7a8855e7cfb8 Add a --version flag.
Mahlon E. Smith <mahlon@martini.nu>
parents: 36
diff changeset
  2737
if ( $conf->{'version'} ) {
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2738
	print "$0 $VERSION\n";
37
7a8855e7cfb8 Add a --version flag.
Mahlon E. Smith <mahlon@martini.nu>
parents: 36
diff changeset
  2739
	exit( 0 );
7a8855e7cfb8 Add a --version flag.
Mahlon E. Smith <mahlon@martini.nu>
parents: 36
diff changeset
  2740
}
7a8855e7cfb8 Add a --version flag.
Mahlon E. Smith <mahlon@martini.nu>
parents: 36
diff changeset
  2741
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2742
# additional/different config file?
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2743
#
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2744
if ( $conf->{'configfile'} ) {
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2745
	my( $filepath, $more_conf) = load_config( $conf->{'configfile'} );
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2746
	while ( my ($k, $v) = each %{$more_conf} ) { $conf->{ $k } = $v }
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2747
}
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2748
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2749
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2750
# defaults
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2751
$conf->{'configfile'} ||= "$ENV{'HOME'}/.shelldap.rc";
101
cb4de2ecc7ab Allow default attributes to be set via config and cmdline
Davor Ocelic <docelic@crystallabs.io>
parents: 99
diff changeset
  2752
$conf->{'cacheage'}   ||= 300;
cb4de2ecc7ab Allow default attributes to be set via config and cmdline
Davor Ocelic <docelic@crystallabs.io>
parents: 99
diff changeset
  2753
$conf->{'timeout'}    ||= 10;
cb4de2ecc7ab Allow default attributes to be set via config and cmdline
Davor Ocelic <docelic@crystallabs.io>
parents: 99
diff changeset
  2754
$conf->{'attributes'} ||= ['*'];
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2755
116
6a0b1209dafa Support arrays/hashes in 'env' (WIP)
Davor Ocelic <docelic@crystallabs.io>
parents: 115
diff changeset
  2756
# Allow command line option --attributes to override settings from
6a0b1209dafa Support arrays/hashes in 'env' (WIP)
Davor Ocelic <docelic@crystallabs.io>
parents: 115
diff changeset
  2757
# config file.
6a0b1209dafa Support arrays/hashes in 'env' (WIP)
Davor Ocelic <docelic@crystallabs.io>
parents: 115
diff changeset
  2758
if($conf->{'cmdline_attributes'}) {
6a0b1209dafa Support arrays/hashes in 'env' (WIP)
Davor Ocelic <docelic@crystallabs.io>
parents: 115
diff changeset
  2759
	$conf->{'attributes'} = $conf->{'cmdline_attributes'}
6a0b1209dafa Support arrays/hashes in 'env' (WIP)
Davor Ocelic <docelic@crystallabs.io>
parents: 115
diff changeset
  2760
}
108
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
  2761
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
  2762
# create and enter shell loop while also handling Ctrl+C correctly.
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
  2763
my $shell = LDAP::Shell->new;
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
  2764
my $sigset = POSIX::SigSet->new();
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
  2765
sub ctrl_c_handler {
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
  2766
	print "\n";
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
  2767
	$shell->term->on_new_line;
124
c1d8da18e65d Reset isearch state on ctrl+c when using Readline 7
Davor Ocelic <docelic@crystallabs.io>
parents: 121
diff changeset
  2768
	$shell->term->kill_text;
c1d8da18e65d Reset isearch state on ctrl+c when using Readline 7
Davor Ocelic <docelic@crystallabs.io>
parents: 121
diff changeset
  2769
	$shell->term->callback_sigcleanup;
c1d8da18e65d Reset isearch state on ctrl+c when using Readline 7
Davor Ocelic <docelic@crystallabs.io>
parents: 121
diff changeset
  2770
	$shell->term->free_line_state;
c1d8da18e65d Reset isearch state on ctrl+c when using Readline 7
Davor Ocelic <docelic@crystallabs.io>
parents: 121
diff changeset
  2771
	$shell->term->cleanup_after_signal;
c1d8da18e65d Reset isearch state on ctrl+c when using Readline 7
Davor Ocelic <docelic@crystallabs.io>
parents: 121
diff changeset
  2772
	$shell->term->callback_handler_remove;
108
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
  2773
	$shell->term->redisplay;
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
  2774
}
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
  2775
my $sigaction = POSIX::SigAction->new( \&ctrl_c_handler, $sigset, 0);
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
  2776
my $old_action = POSIX::SigAction->new;
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
  2777
POSIX::sigaction(&POSIX::SIGINT, $sigaction, $old_action); # save default one
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2778
$shell->cmdloop();
108
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
  2779
POSIX::sigaction(&POSIX::SIGINT, $old_action); # restore default one
24340fdef276 Properly handle Ctrl+C; add '?' and 'man' aliases; don't import symbols
Davor Ocelic <docelic@crystallabs.io>
parents: 107
diff changeset
  2780
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2781
### List of default config files
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2782
###
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2783
sub default_configfiles
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2784
{
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2785
	( "$ENV{'HOME'}/.shelldap.rc", '/usr/local/etc/shelldap.conf', '/etc/shelldap.conf' )
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2786
}
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2787
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2788
### load YAML config into global conf.
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2789
###
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2790
sub load_config
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2791
{
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2792
	my $confpath = shift;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2793
	my ( $d, $data );
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2794
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2795
	unless ( $confpath ) {
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2796
		my @confs = default_configfiles();
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2797
		foreach ( @confs ) {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2798
			if ( -e $_ ) {
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2799
				$confpath = $_;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2800
				last;
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2801
			}
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2802
		}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2803
	}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2804
	$confpath or return undef;
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2805
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2806
	open(my($yaml) , "< $confpath");
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2807
	do {
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2808
		local $/ = undef;
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2809
		$data = <$yaml>;  # slurp!
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2810
	};
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2811
	close $yaml;
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2812
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2813
	my $conf2 = eval { 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
  2814
	die "Invalid YAML in $confpath\n" if $@;
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2815
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2816
	if( $conf2->{'configfile'} and ($confpath eq $conf2->{'configfile'})) {
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2817
		delete $conf2->{'configfile'}
102
e92cb378144d Fix precedence of command line arguments
Davor Ocelic <docelic@crystallabs.io>
parents: 101
diff changeset
  2818
	}
e92cb378144d Fix precedence of command line arguments
Davor Ocelic <docelic@crystallabs.io>
parents: 101
diff changeset
  2819
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2820
	$conf2->{alias} ||= {};
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2821
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2822
	return($confpath, $conf2);
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2823
}
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2824
102
e92cb378144d Fix precedence of command line arguments
Davor Ocelic <docelic@crystallabs.io>
parents: 101
diff changeset
  2825
### dump YAML config into conf file while making sure that
e92cb378144d Fix precedence of command line arguments
Davor Ocelic <docelic@crystallabs.io>
parents: 101
diff changeset
  2826
### name of configfile itself is not dumped if it is equal
e92cb378144d Fix precedence of command line arguments
Davor Ocelic <docelic@crystallabs.io>
parents: 101
diff changeset
  2827
### to the file being written.
e92cb378144d Fix precedence of command line arguments
Davor Ocelic <docelic@crystallabs.io>
parents: 101
diff changeset
  2828
###
e92cb378144d Fix precedence of command line arguments
Davor Ocelic <docelic@crystallabs.io>
parents: 101
diff changeset
  2829
sub save_config
e92cb378144d Fix precedence of command line arguments
Davor Ocelic <docelic@crystallabs.io>
parents: 101
diff changeset
  2830
{
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2831
	my $confpath = shift || $conf->{'configfile'} || (default_configfiles)[0];
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2832
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2833
	my %conf2 = %$conf;
113
ce001854d4b8 Improve save_config()
Davor Ocelic <docelic@crystallabs.io>
parents: 112
diff changeset
  2834
	# This check is currently unnecessary because the comparison will always
ce001854d4b8 Improve save_config()
Davor Ocelic <docelic@crystallabs.io>
parents: 112
diff changeset
  2835
	# be true, but is left here for effect of least surprise in the future.
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2836
	if( $conf->{configfile} and ($confpath eq $conf->{configfile})) {
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2837
	 			 delete $conf2{'configfile'}
102
e92cb378144d Fix precedence of command line arguments
Davor Ocelic <docelic@crystallabs.io>
parents: 101
diff changeset
  2838
	}
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2839
	YAML::Syck::DumpFile( $confpath, \%conf2 );
113
ce001854d4b8 Improve save_config()
Davor Ocelic <docelic@crystallabs.io>
parents: 112
diff changeset
  2840
	chmod 0600, $confpath;
102
e92cb378144d Fix precedence of command line arguments
Davor Ocelic <docelic@crystallabs.io>
parents: 101
diff changeset
  2841
120
7f804e1f903c Multiple configfile-related changes
Davor Ocelic <docelic@crystallabs.io>
parents: 119
diff changeset
  2842
	return $confpath;
102
e92cb378144d Fix precedence of command line arguments
Davor Ocelic <docelic@crystallabs.io>
parents: 101
diff changeset
  2843
}
e92cb378144d Fix precedence of command line arguments
Davor Ocelic <docelic@crystallabs.io>
parents: 101
diff changeset
  2844
118
2d7f16eff198 Use local implementation of slurp()
Davor Ocelic <docelic@crystallabs.io>
parents: 117
diff changeset
  2845
sub slurp
2d7f16eff198 Use local implementation of slurp()
Davor Ocelic <docelic@crystallabs.io>
parents: 117
diff changeset
  2846
{
2d7f16eff198 Use local implementation of slurp()
Davor Ocelic <docelic@crystallabs.io>
parents: 117
diff changeset
  2847
	open my $fh, '<:encoding(UTF-8)', $_[0];
2d7f16eff198 Use local implementation of slurp()
Davor Ocelic <docelic@crystallabs.io>
parents: 117
diff changeset
  2848
	local $/;
2d7f16eff198 Use local implementation of slurp()
Davor Ocelic <docelic@crystallabs.io>
parents: 117
diff changeset
  2849
	my $ret = <$fh>;
2d7f16eff198 Use local implementation of slurp()
Davor Ocelic <docelic@crystallabs.io>
parents: 117
diff changeset
  2850
	close $fh;
2d7f16eff198 Use local implementation of slurp()
Davor Ocelic <docelic@crystallabs.io>
parents: 117
diff changeset
  2851
	return $ret;
2d7f16eff198 Use local implementation of slurp()
Davor Ocelic <docelic@crystallabs.io>
parents: 117
diff changeset
  2852
};
2d7f16eff198 Use local implementation of slurp()
Davor Ocelic <docelic@crystallabs.io>
parents: 117
diff changeset
  2853
50
21ba5eb5c2fc Numerous changes:
Mahlon E. Smith <mahlon@laika.com>
parents: 49
diff changeset
  2854
### EOF
0
f7990a76e217 Restructure for tags/branches.
mahlon
parents:
diff changeset
  2855