2003-11-26 07:22:12 +08:00
|
|
|
/* common.c - common routines for the ldap client tools */
|
2002-12-13 00:32:26 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 07:22:12 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2005-01-02 04:49:32 +08:00
|
|
|
* Copyright 1998-2005 The OpenLDAP Foundation.
|
2003-11-26 07:22:12 +08:00
|
|
|
* Portions Copyright 2003 Kurt D. Zeilenga.
|
|
|
|
* Portions Copyright 2003 IBM Corporation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
2003-11-26 10:58:56 +08:00
|
|
|
/* ACKNOWLEDGEMENTS:
|
|
|
|
* This file was initially created by Hallvard B. Furuseth based (in
|
2003-11-26 07:22:12 +08:00
|
|
|
* part) upon argument parsing code for individual tools located in
|
|
|
|
* this directory. Additional contributors include:
|
|
|
|
* Kurt D. Zeilenga (additional common argument and control support)
|
2002-12-13 00:32:26 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
|
|
|
#include <ac/signal.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/unistd.h>
|
|
|
|
#include <ac/errno.h>
|
|
|
|
|
|
|
|
#include <ldap.h>
|
|
|
|
|
|
|
|
#include "lutil_ldap.h"
|
2003-06-05 19:49:07 +08:00
|
|
|
#include "ldap_defaults.h"
|
2003-09-17 02:56:04 +08:00
|
|
|
#include "ldap_pvt.h"
|
2003-10-14 02:09:15 +08:00
|
|
|
#include "lber_pvt.h"
|
2002-12-13 00:32:26 +08:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
|
|
|
|
int authmethod = -1;
|
|
|
|
char *binddn = NULL;
|
|
|
|
int contoper = 0;
|
|
|
|
int debug = 0;
|
|
|
|
char *infile = NULL;
|
|
|
|
char *ldapuri = NULL;
|
|
|
|
char *ldaphost = NULL;
|
|
|
|
int ldapport = 0;
|
|
|
|
#ifdef HAVE_CYRUS_SASL
|
|
|
|
unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
|
|
|
|
char *sasl_realm = NULL;
|
|
|
|
char *sasl_authc_id = NULL;
|
|
|
|
char *sasl_authz_id = NULL;
|
|
|
|
char *sasl_mech = NULL;
|
|
|
|
char *sasl_secprops = NULL;
|
|
|
|
#endif
|
|
|
|
int use_tls = 0;
|
|
|
|
|
2003-09-17 02:56:04 +08:00
|
|
|
int assertctl;
|
2003-06-10 10:19:30 +08:00
|
|
|
char *assertion = NULL;
|
2002-12-13 00:32:26 +08:00
|
|
|
char *authzid = NULL;
|
|
|
|
int manageDSAit = 0;
|
|
|
|
int noop = 0;
|
2004-03-16 07:54:38 +08:00
|
|
|
int ppolicy = 0;
|
2003-09-17 02:56:04 +08:00
|
|
|
int preread = 0;
|
|
|
|
char *preread_attrs = NULL;
|
|
|
|
int postread = 0;
|
|
|
|
char *postread_attrs = NULL;
|
2002-12-13 00:32:26 +08:00
|
|
|
|
|
|
|
int not = 0;
|
|
|
|
int want_bindpw = 0;
|
|
|
|
struct berval passwd = { 0, NULL };
|
|
|
|
char *pw_file = NULL;
|
|
|
|
int referrals = 0;
|
2003-01-21 04:11:57 +08:00
|
|
|
int protocol = -1;
|
2002-12-13 00:32:26 +08:00
|
|
|
int verbose = 0;
|
2003-01-21 04:11:57 +08:00
|
|
|
int version = 0;
|
2002-12-13 00:32:26 +08:00
|
|
|
|
|
|
|
/* Set in main() */
|
2003-01-21 04:11:57 +08:00
|
|
|
char *prog = NULL;
|
2002-12-13 00:32:26 +08:00
|
|
|
|
2003-04-06 10:59:15 +08:00
|
|
|
void
|
|
|
|
tool_init( void )
|
|
|
|
{
|
2003-06-05 19:49:07 +08:00
|
|
|
ldap_pvt_setlocale(LC_MESSAGES, "");
|
|
|
|
ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
|
|
|
|
ldap_pvt_textdomain(OPENLDAP_PACKAGE);
|
2003-04-06 10:59:15 +08:00
|
|
|
}
|
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
void
|
|
|
|
tool_common_usage( void )
|
|
|
|
{
|
|
|
|
static const char *const descriptions[] = {
|
2003-04-06 10:59:15 +08:00
|
|
|
N_(" -c continuous operation mode (do not stop on errors)\n"),
|
2003-12-03 02:02:24 +08:00
|
|
|
N_(" -C chase referrals (anonymously)\n"),
|
2003-04-06 10:59:15 +08:00
|
|
|
N_(" -d level set LDAP debugging level to `level'\n"),
|
|
|
|
N_(" -D binddn bind DN\n"),
|
2004-03-13 06:34:07 +08:00
|
|
|
N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
|
2003-09-17 02:56:04 +08:00
|
|
|
N_(" [!]assert=<filter> (an RFC 2254 Filter)\n")
|
|
|
|
N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
|
|
|
|
N_(" [!]manageDSAit\n")
|
2004-03-18 04:50:35 +08:00
|
|
|
N_(" [!]noop\n")
|
2004-03-16 09:47:16 +08:00
|
|
|
#ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
|
2004-03-18 04:50:35 +08:00
|
|
|
N_(" ppolicy\n")
|
2004-03-16 09:47:16 +08:00
|
|
|
#endif
|
2004-03-18 04:50:35 +08:00
|
|
|
N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
|
2003-09-17 02:56:04 +08:00
|
|
|
N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n"),
|
2003-04-06 10:59:15 +08:00
|
|
|
N_(" -f file read operations from `file'\n"),
|
|
|
|
N_(" -h host LDAP server\n"),
|
|
|
|
N_(" -H URI LDAP Uniform Resource Indentifier(s)\n"),
|
|
|
|
N_(" -I use SASL Interactive mode\n"),
|
|
|
|
N_(" -k use Kerberos authentication\n"),
|
|
|
|
N_(" -K like -k, but do only step 1 of the Kerberos bind\n"),
|
|
|
|
N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
|
|
|
|
N_(" -n show what would be done but don't actually do it\n"),
|
|
|
|
N_(" -O props SASL security properties\n"),
|
|
|
|
N_(" -p port port on LDAP server\n"),
|
|
|
|
N_(" -P version procotol version (default: 3)\n"),
|
|
|
|
N_(" -Q use SASL Quiet mode\n"),
|
|
|
|
N_(" -R realm SASL realm\n"),
|
|
|
|
N_(" -U authcid SASL authentication identity\n"),
|
|
|
|
N_(" -v run in verbose mode (diagnostics to standard output)\n"),
|
|
|
|
N_(" -V print version info (-VV only)\n"),
|
|
|
|
N_(" -w passwd bind password (for simple authentication)\n"),
|
|
|
|
N_(" -W prompt for bind password\n"),
|
|
|
|
N_(" -x Simple authentication\n"),
|
|
|
|
N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
|
|
|
|
N_(" -y file Read password from file\n"),
|
|
|
|
N_(" -Y mech SASL mechanism\n"),
|
|
|
|
N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
|
2002-12-13 00:32:26 +08:00
|
|
|
NULL
|
|
|
|
};
|
2003-01-21 04:11:57 +08:00
|
|
|
const char *const *cpp;
|
2002-12-13 00:32:26 +08:00
|
|
|
|
2003-04-06 10:59:15 +08:00
|
|
|
fputs( _("Common options:\n"), stderr );
|
2003-01-21 04:11:57 +08:00
|
|
|
for( cpp = descriptions; *cpp != NULL; cpp++ ) {
|
2004-03-16 09:47:16 +08:00
|
|
|
if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
|
2003-04-06 10:59:15 +08:00
|
|
|
fputs( _(*cpp), stderr );
|
2003-01-21 04:11:57 +08:00
|
|
|
}
|
|
|
|
}
|
2002-12-13 00:32:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
tool_args( int argc, char **argv )
|
|
|
|
{
|
|
|
|
int i;
|
2003-05-17 09:37:01 +08:00
|
|
|
|
|
|
|
while (( i = getopt( argc, argv, options )) != EOF ) {
|
2004-06-18 16:30:46 +08:00
|
|
|
int crit, ival;
|
|
|
|
char *control, *cvalue, *next;
|
2002-12-13 00:32:26 +08:00
|
|
|
switch( i ) {
|
|
|
|
case 'c': /* continuous operation mode */
|
2003-03-28 02:11:18 +08:00
|
|
|
contoper++;
|
2002-12-13 00:32:26 +08:00
|
|
|
break;
|
|
|
|
case 'C':
|
2003-03-28 02:11:18 +08:00
|
|
|
referrals++;
|
2002-12-13 00:32:26 +08:00
|
|
|
break;
|
|
|
|
case 'd':
|
2004-06-18 16:30:46 +08:00
|
|
|
ival = strtol( optarg, &next, 10 );
|
|
|
|
if (next == NULL || next[0] != '\0') {
|
|
|
|
fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
debug |= ival;
|
2002-12-13 00:32:26 +08:00
|
|
|
break;
|
|
|
|
case 'D': /* bind DN */
|
|
|
|
if( binddn != NULL ) {
|
|
|
|
fprintf( stderr, "%s: -D previously specified\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
binddn = ber_strdup( optarg );
|
|
|
|
break;
|
2004-03-13 06:34:07 +08:00
|
|
|
case 'e': /* general extensions (controls and such) */
|
2002-12-13 00:32:26 +08:00
|
|
|
/* should be extended to support comma separated list of
|
|
|
|
* [!]key[=value] parameters, e.g. -e !foo,bar=567
|
|
|
|
*/
|
|
|
|
|
|
|
|
crit = 0;
|
|
|
|
cvalue = NULL;
|
|
|
|
if( optarg[0] == '!' ) {
|
|
|
|
crit = 1;
|
|
|
|
optarg++;
|
|
|
|
}
|
|
|
|
|
|
|
|
control = ber_strdup( optarg );
|
|
|
|
if ( (cvalue = strchr( control, '=' )) != NULL ) {
|
|
|
|
*cvalue++ = '\0';
|
|
|
|
}
|
|
|
|
|
2003-06-10 10:19:30 +08:00
|
|
|
if ( strcasecmp( control, "assert" ) == 0 ) {
|
2003-09-17 02:56:04 +08:00
|
|
|
if( assertctl ) {
|
2003-06-10 10:19:30 +08:00
|
|
|
fprintf( stderr, "assert control previously specified\n");
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
if( cvalue == NULL ) {
|
|
|
|
fprintf( stderr, "assert: control value expected\n" );
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2003-09-17 02:56:04 +08:00
|
|
|
assertctl = 1 + crit;
|
|
|
|
|
2003-06-10 10:19:30 +08:00
|
|
|
assert( assertion == NULL );
|
|
|
|
assertion = cvalue;
|
|
|
|
|
|
|
|
} else if ( strcasecmp( control, "authzid" ) == 0 ) {
|
2002-12-13 00:32:26 +08:00
|
|
|
if( authzid != NULL ) {
|
|
|
|
fprintf( stderr, "authzid control previously specified\n");
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
if( cvalue == NULL ) {
|
|
|
|
fprintf( stderr, "authzid: control value expected\n" );
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if( !crit ) {
|
|
|
|
fprintf( stderr, "authzid: must be marked critical\n" );
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert( authzid == NULL );
|
|
|
|
authzid = cvalue;
|
|
|
|
|
|
|
|
} else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
|
|
|
|
if( manageDSAit ) {
|
|
|
|
fprintf( stderr,
|
2003-05-17 09:37:01 +08:00
|
|
|
"manageDSAit control previously specified\n");
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
if( cvalue != NULL ) {
|
|
|
|
fprintf( stderr,
|
2003-05-17 09:37:01 +08:00
|
|
|
"manageDSAit: no control value expected\n" );
|
2002-12-13 00:32:26 +08:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
manageDSAit = 1 + crit;
|
|
|
|
|
|
|
|
} else if ( strcasecmp( control, "noop" ) == 0 ) {
|
|
|
|
if( noop ) {
|
|
|
|
fprintf( stderr, "noop control previously specified\n");
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
if( cvalue != NULL ) {
|
|
|
|
fprintf( stderr, "noop: no control value expected\n" );
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
noop = 1 + crit;
|
|
|
|
|
2004-03-16 09:47:16 +08:00
|
|
|
#ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
|
2004-03-16 07:54:38 +08:00
|
|
|
} else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
|
|
|
|
if( ppolicy ) {
|
|
|
|
fprintf( stderr, "ppolicy control previously specified\n");
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
if( cvalue != NULL ) {
|
|
|
|
fprintf( stderr, "ppolicy: no control value expected\n" );
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if( crit ) {
|
|
|
|
fprintf( stderr, "ppolicy: critical flag not allowed\n" );
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
ppolicy = 1;
|
2004-03-16 09:47:16 +08:00
|
|
|
#endif
|
2004-03-16 07:54:38 +08:00
|
|
|
|
2003-09-17 02:56:04 +08:00
|
|
|
} else if ( strcasecmp( control, "preread" ) == 0 ) {
|
|
|
|
if( preread ) {
|
|
|
|
fprintf( stderr, "preread control previously specified\n");
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
preread = 1 + crit;
|
|
|
|
preread_attrs = cvalue;
|
|
|
|
|
|
|
|
} else if ( strcasecmp( control, "postread" ) == 0 ) {
|
|
|
|
if( postread ) {
|
|
|
|
fprintf( stderr, "postread control previously specified\n");
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
postread = 1 + crit;
|
|
|
|
postread_attrs = cvalue;
|
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
} else {
|
|
|
|
fprintf( stderr, "Invalid general control name: %s\n",
|
2003-05-17 09:37:01 +08:00
|
|
|
control );
|
2002-12-13 00:32:26 +08:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f': /* read from file */
|
|
|
|
if( infile != NULL ) {
|
|
|
|
fprintf( stderr, "%s: -f previously specified\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
infile = ber_strdup( optarg );
|
|
|
|
break;
|
|
|
|
case 'h': /* ldap host */
|
|
|
|
if( ldaphost != NULL ) {
|
|
|
|
fprintf( stderr, "%s: -h previously specified\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
ldaphost = ber_strdup( optarg );
|
|
|
|
break;
|
|
|
|
case 'H': /* ldap URI */
|
|
|
|
if( ldapuri != NULL ) {
|
|
|
|
fprintf( stderr, "%s: -H previously specified\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
ldapuri = ber_strdup( optarg );
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
#ifdef HAVE_CYRUS_SASL
|
|
|
|
if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
|
|
|
|
fprintf( stderr, "%s: incompatible previous "
|
2003-05-17 09:37:01 +08:00
|
|
|
"authentication choice\n",
|
|
|
|
prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
authmethod = LDAP_AUTH_SASL;
|
|
|
|
sasl_flags = LDAP_SASL_INTERACTIVE;
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
fprintf( stderr, "%s: was not compiled with SASL support\n",
|
2003-05-17 09:37:01 +08:00
|
|
|
prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
#endif
|
|
|
|
case 'k': /* kerberos bind */
|
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
|
|
|
|
if( authmethod != -1 ) {
|
|
|
|
fprintf( stderr, "%s: -k incompatible with previous "
|
2003-05-17 09:37:01 +08:00
|
|
|
"authentication choice\n", prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
authmethod = LDAP_AUTH_KRBV4;
|
|
|
|
#else
|
|
|
|
fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 'K': /* kerberos bind, part one only */
|
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
|
|
|
|
if( authmethod != -1 ) {
|
|
|
|
fprintf( stderr, "%s: incompatible with previous "
|
2003-05-17 09:37:01 +08:00
|
|
|
"authentication choice\n", prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
authmethod = LDAP_AUTH_KRBV41;
|
|
|
|
#else
|
|
|
|
fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
/* enable Manage DSA IT */
|
2003-03-28 02:11:18 +08:00
|
|
|
manageDSAit++;
|
2002-12-13 00:32:26 +08:00
|
|
|
break;
|
|
|
|
case 'n': /* print operations, don't actually do them */
|
2003-03-28 02:11:18 +08:00
|
|
|
not++;
|
2002-12-13 00:32:26 +08:00
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
#ifdef HAVE_CYRUS_SASL
|
|
|
|
if( sasl_secprops != NULL ) {
|
|
|
|
fprintf( stderr, "%s: -O previously specified\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
|
|
|
|
fprintf( stderr, "%s: incompatible previous "
|
2003-05-17 09:37:01 +08:00
|
|
|
"authentication choice\n", prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
authmethod = LDAP_AUTH_SASL;
|
|
|
|
sasl_secprops = ber_strdup( optarg );
|
|
|
|
#else
|
2003-05-17 09:37:01 +08:00
|
|
|
fprintf( stderr, "%s: not compiled with SASL support\n", prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
if( ldapport ) {
|
|
|
|
fprintf( stderr, "%s: -p previously specified\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
2004-06-18 16:30:46 +08:00
|
|
|
ival = strtol( optarg, &next, 10 );
|
|
|
|
if ( next == NULL || next[0] != '\0' ) {
|
|
|
|
fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
ldapport = ival;
|
2002-12-13 00:32:26 +08:00
|
|
|
break;
|
|
|
|
case 'P':
|
2004-06-18 16:30:46 +08:00
|
|
|
ival = strtol( optarg, &next, 10 );
|
|
|
|
if ( next == NULL || next[0] != '\0' ) {
|
|
|
|
fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
switch( ival ) {
|
2002-12-13 00:32:26 +08:00
|
|
|
case 2:
|
2003-01-21 04:11:57 +08:00
|
|
|
if( protocol == LDAP_VERSION3 ) {
|
2002-12-13 00:32:26 +08:00
|
|
|
fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
|
2003-05-17 09:37:01 +08:00
|
|
|
prog, protocol );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
2003-01-21 04:11:57 +08:00
|
|
|
protocol = LDAP_VERSION2;
|
2002-12-13 00:32:26 +08:00
|
|
|
break;
|
|
|
|
case 3:
|
2003-01-21 04:11:57 +08:00
|
|
|
if( protocol == LDAP_VERSION2 ) {
|
2002-12-13 00:32:26 +08:00
|
|
|
fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
|
2003-05-17 09:37:01 +08:00
|
|
|
prog, protocol );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
2003-01-21 04:11:57 +08:00
|
|
|
protocol = LDAP_VERSION3;
|
2002-12-13 00:32:26 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf( stderr, "%s: protocol version should be 2 or 3\n",
|
2003-05-17 09:37:01 +08:00
|
|
|
prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Q':
|
|
|
|
#ifdef HAVE_CYRUS_SASL
|
|
|
|
if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
|
|
|
|
fprintf( stderr, "%s: incompatible previous "
|
2003-05-17 09:37:01 +08:00
|
|
|
"authentication choice\n",
|
|
|
|
prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
authmethod = LDAP_AUTH_SASL;
|
|
|
|
sasl_flags = LDAP_SASL_QUIET;
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
fprintf( stderr, "%s: not compiled with SASL support\n",
|
2003-05-17 09:37:01 +08:00
|
|
|
prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
#endif
|
|
|
|
case 'R':
|
|
|
|
#ifdef HAVE_CYRUS_SASL
|
|
|
|
if( sasl_realm != NULL ) {
|
|
|
|
fprintf( stderr, "%s: -R previously specified\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
|
|
|
|
fprintf( stderr, "%s: incompatible previous "
|
2003-05-17 09:37:01 +08:00
|
|
|
"authentication choice\n",
|
|
|
|
prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
authmethod = LDAP_AUTH_SASL;
|
|
|
|
sasl_realm = ber_strdup( optarg );
|
|
|
|
#else
|
|
|
|
fprintf( stderr, "%s: not compiled with SASL support\n",
|
2003-05-17 09:37:01 +08:00
|
|
|
prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
#ifdef HAVE_CYRUS_SASL
|
|
|
|
if( sasl_authc_id != NULL ) {
|
|
|
|
fprintf( stderr, "%s: -U previously specified\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
|
|
|
|
fprintf( stderr, "%s: incompatible previous "
|
2003-05-17 09:37:01 +08:00
|
|
|
"authentication choice\n",
|
|
|
|
prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
authmethod = LDAP_AUTH_SASL;
|
|
|
|
sasl_authc_id = ber_strdup( optarg );
|
|
|
|
#else
|
|
|
|
fprintf( stderr, "%s: not compiled with SASL support\n",
|
2003-05-17 09:37:01 +08:00
|
|
|
prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 'v': /* verbose mode */
|
2003-03-28 02:11:18 +08:00
|
|
|
verbose++;
|
2002-12-13 00:32:26 +08:00
|
|
|
break;
|
2003-01-21 04:11:57 +08:00
|
|
|
case 'V': /* version */
|
|
|
|
version++;
|
|
|
|
break;
|
2002-12-13 00:32:26 +08:00
|
|
|
case 'w': /* password */
|
|
|
|
passwd.bv_val = ber_strdup( optarg );
|
|
|
|
{
|
|
|
|
char* p;
|
|
|
|
|
|
|
|
for( p = optarg; *p != '\0'; p++ ) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
passwd.bv_len = strlen( passwd.bv_val );
|
|
|
|
break;
|
|
|
|
case 'W':
|
2003-03-28 02:11:18 +08:00
|
|
|
want_bindpw++;
|
2002-12-13 00:32:26 +08:00
|
|
|
break;
|
|
|
|
case 'y':
|
|
|
|
pw_file = optarg;
|
|
|
|
break;
|
|
|
|
case 'Y':
|
|
|
|
#ifdef HAVE_CYRUS_SASL
|
|
|
|
if( sasl_mech != NULL ) {
|
|
|
|
fprintf( stderr, "%s: -Y previously specified\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
|
2003-05-17 09:37:01 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
"%s: incompatible with authentication choice\n", prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
authmethod = LDAP_AUTH_SASL;
|
|
|
|
sasl_mech = ber_strdup( optarg );
|
|
|
|
#else
|
2003-05-17 09:37:01 +08:00
|
|
|
fprintf( stderr, "%s: not compiled with SASL support\n", prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
|
|
|
|
fprintf( stderr, "%s: incompatible with previous "
|
2003-05-17 09:37:01 +08:00
|
|
|
"authentication choice\n", prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
authmethod = LDAP_AUTH_SIMPLE;
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
#ifdef HAVE_CYRUS_SASL
|
|
|
|
if( sasl_authz_id != NULL ) {
|
|
|
|
fprintf( stderr, "%s: -X previously specified\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
|
|
|
|
fprintf( stderr, "%s: -X incompatible with "
|
2003-05-17 09:37:01 +08:00
|
|
|
"authentication choice\n", prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
authmethod = LDAP_AUTH_SASL;
|
|
|
|
sasl_authz_id = ber_strdup( optarg );
|
|
|
|
#else
|
|
|
|
fprintf( stderr, "%s: not compiled with SASL support\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 'Z':
|
|
|
|
#ifdef HAVE_TLS
|
2003-03-28 01:30:43 +08:00
|
|
|
use_tls++;
|
2002-12-13 00:32:26 +08:00
|
|
|
#else
|
|
|
|
fprintf( stderr, "%s: not compiled with TLS support\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
default:
|
2003-05-17 09:37:01 +08:00
|
|
|
if( handle_private_option( i ) ) break;
|
2002-12-13 00:32:26 +08:00
|
|
|
fprintf( stderr, "%s: unrecognized option -%c\n",
|
2003-05-17 09:37:01 +08:00
|
|
|
prog, optopt );
|
2002-12-13 00:32:26 +08:00
|
|
|
usage();
|
|
|
|
}
|
2003-05-17 09:37:01 +08:00
|
|
|
}
|
2002-12-13 00:32:26 +08:00
|
|
|
|
2003-02-10 09:43:41 +08:00
|
|
|
{
|
|
|
|
/* prevent bad linking */
|
|
|
|
LDAPAPIInfo api;
|
|
|
|
api.ldapai_info_version = LDAP_API_INFO_VERSION;
|
|
|
|
|
2003-02-10 09:48:04 +08:00
|
|
|
if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
|
|
|
|
!= LDAP_OPT_SUCCESS )
|
|
|
|
{
|
2003-02-10 09:43:41 +08:00
|
|
|
fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
|
|
|
|
fprintf( stderr, "LDAP APIInfo version mismatch: "
|
|
|
|
"got %d, expected %d\n",
|
|
|
|
api.ldapai_info_version, LDAP_API_INFO_VERSION );
|
|
|
|
exit( EXIT_FAILURE );
|
2003-05-17 09:37:01 +08:00
|
|
|
}
|
2003-02-10 09:43:41 +08:00
|
|
|
|
|
|
|
if( api.ldapai_api_version != LDAP_API_VERSION ) {
|
|
|
|
fprintf( stderr, "LDAP API version mismatch: "
|
|
|
|
"got %d, expected %d\n",
|
|
|
|
api.ldapai_api_version, LDAP_API_VERSION );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
|
|
|
|
fprintf( stderr, "LDAP vendor name mismatch: "
|
|
|
|
"got %s, expected %s\n",
|
|
|
|
api.ldapai_vendor_name, LDAP_VENDOR_NAME );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
|
|
|
|
fprintf( stderr, "LDAP vendor version mismatch: "
|
|
|
|
"got %d, expected %d\n",
|
|
|
|
api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (version) {
|
|
|
|
fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
|
|
|
|
prog, __Version,
|
|
|
|
LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
|
|
|
|
if (version > 1) exit( EXIT_SUCCESS );
|
|
|
|
}
|
2003-01-21 04:11:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (protocol == -1)
|
|
|
|
protocol = LDAP_VERSION3;
|
2002-12-13 00:32:26 +08:00
|
|
|
|
2003-01-21 04:11:57 +08:00
|
|
|
if (authmethod == -1 && protocol > LDAP_VERSION2) {
|
2002-12-13 00:32:26 +08:00
|
|
|
#ifdef HAVE_CYRUS_SASL
|
|
|
|
authmethod = LDAP_AUTH_SASL;
|
|
|
|
#else
|
|
|
|
authmethod = LDAP_AUTH_SIMPLE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-05-22 08:13:00 +08:00
|
|
|
if( ldapuri == NULL ) {
|
|
|
|
if( ldapport && ( ldaphost == NULL )) {
|
|
|
|
fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
} else {
|
2002-12-13 00:32:26 +08:00
|
|
|
if( ldaphost != NULL ) {
|
|
|
|
fprintf( stderr, "%s: -H incompatible with -h\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
if( ldapport ) {
|
|
|
|
fprintf( stderr, "%s: -H incompatible with -p\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
}
|
2003-01-21 04:11:57 +08:00
|
|
|
if( protocol == LDAP_VERSION2 ) {
|
2004-03-16 07:54:38 +08:00
|
|
|
if( authzid || manageDSAit || noop || ppolicy ) {
|
2002-12-13 00:32:26 +08:00
|
|
|
fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
#ifdef HAVE_TLS
|
|
|
|
if( use_tls ) {
|
|
|
|
fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_CYRUS_SASL
|
|
|
|
if( authmethod == LDAP_AUTH_SASL ) {
|
|
|
|
fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
|
2003-05-17 09:37:01 +08:00
|
|
|
prog );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
|
2003-02-27 13:51:41 +08:00
|
|
|
if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
|
2002-12-13 00:32:26 +08:00
|
|
|
fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
|
2003-05-17 09:37:01 +08:00
|
|
|
prog, protocol );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LDAP *
|
|
|
|
tool_conn_setup( int not, void (*private_setup)( LDAP * ) )
|
|
|
|
{
|
|
|
|
LDAP *ld = NULL;
|
|
|
|
|
|
|
|
if ( debug ) {
|
|
|
|
if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
|
2003-05-17 09:37:01 +08:00
|
|
|
!= LBER_OPT_SUCCESS )
|
|
|
|
{
|
2004-10-19 09:40:38 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
"Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
|
2002-12-13 00:32:26 +08:00
|
|
|
}
|
|
|
|
if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
|
2003-05-17 09:37:01 +08:00
|
|
|
!= LDAP_OPT_SUCCESS )
|
|
|
|
{
|
2004-10-19 09:40:38 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
"Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
|
2002-12-13 00:32:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
(void) SIGNAL( SIGPIPE, SIG_IGN );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ( !not ) {
|
2003-12-14 11:15:28 +08:00
|
|
|
int rc;
|
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
|
2003-12-14 11:15:28 +08:00
|
|
|
/* construct URL */
|
|
|
|
LDAPURLDesc url;
|
|
|
|
memset( &url, 0, sizeof(url));
|
2002-12-13 00:32:26 +08:00
|
|
|
|
2003-12-14 11:15:28 +08:00
|
|
|
url.lud_scheme = "ldap";
|
|
|
|
url.lud_host = ldaphost;
|
|
|
|
url.lud_port = ldapport;
|
|
|
|
url.lud_scope = LDAP_SCOPE_DEFAULT;
|
2002-12-13 00:32:26 +08:00
|
|
|
|
2003-12-14 11:15:28 +08:00
|
|
|
ldapuri = ldap_url_desc2str( &url );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( verbose ) {
|
|
|
|
fprintf( stderr, "ldap_initialize( %s )\n",
|
|
|
|
ldapuri != NULL ? ldapuri : "<DEFAULT>" );
|
|
|
|
}
|
|
|
|
rc = ldap_initialize( &ld, ldapuri );
|
|
|
|
if( rc != LDAP_SUCCESS ) {
|
|
|
|
fprintf( stderr,
|
2004-09-20 12:41:19 +08:00
|
|
|
"Could not create LDAP session handle for URI=%s (%d): %s\n",
|
2004-07-05 20:03:39 +08:00
|
|
|
ldapuri, rc, ldap_err2string(rc) );
|
2003-12-14 11:15:28 +08:00
|
|
|
exit( EXIT_FAILURE );
|
2002-12-13 00:32:26 +08:00
|
|
|
}
|
|
|
|
|
2003-05-17 09:37:01 +08:00
|
|
|
if( private_setup ) private_setup( ld );
|
2002-12-13 00:32:26 +08:00
|
|
|
|
|
|
|
/* referrals */
|
|
|
|
if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
|
2003-05-17 09:37:01 +08:00
|
|
|
referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
|
2002-12-13 00:32:26 +08:00
|
|
|
{
|
|
|
|
fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
|
2003-05-17 09:37:01 +08:00
|
|
|
referrals ? "on" : "off" );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
2003-01-21 04:11:57 +08:00
|
|
|
if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
|
2003-05-17 09:37:01 +08:00
|
|
|
!= LDAP_OPT_SUCCESS )
|
2002-12-13 00:32:26 +08:00
|
|
|
{
|
|
|
|
fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
|
2003-05-17 09:37:01 +08:00
|
|
|
protocol );
|
2002-12-13 00:32:26 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( use_tls &&
|
2003-05-17 09:37:01 +08:00
|
|
|
( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ))
|
|
|
|
{
|
2002-12-13 00:32:26 +08:00
|
|
|
ldap_perror( ld, "ldap_start_tls" );
|
|
|
|
if ( use_tls > 1 ) {
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ld;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
tool_bind( LDAP *ld )
|
|
|
|
{
|
2004-03-16 09:47:16 +08:00
|
|
|
#ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
|
2004-03-16 07:54:38 +08:00
|
|
|
if ( ppolicy ) {
|
|
|
|
LDAPControl *ctrls[2], c;
|
|
|
|
c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
|
|
|
|
c.ldctl_value.bv_val = NULL;
|
|
|
|
c.ldctl_value.bv_len = 0;
|
|
|
|
c.ldctl_iscritical = 0;
|
|
|
|
ctrls[0] = &c;
|
|
|
|
ctrls[1] = NULL;
|
|
|
|
ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
|
|
|
|
}
|
2004-03-16 09:47:16 +08:00
|
|
|
#endif
|
2004-03-16 07:54:38 +08:00
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
if ( authmethod == LDAP_AUTH_SASL ) {
|
|
|
|
#ifdef HAVE_CYRUS_SASL
|
|
|
|
void *defaults;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if( sasl_secprops != NULL ) {
|
|
|
|
rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
|
|
|
|
(void *) sasl_secprops );
|
|
|
|
|
|
|
|
if( rc != LDAP_OPT_SUCCESS ) {
|
|
|
|
fprintf( stderr,
|
|
|
|
"Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
|
|
|
|
sasl_secprops );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defaults = lutil_sasl_defaults( ld,
|
|
|
|
sasl_mech,
|
|
|
|
sasl_realm,
|
|
|
|
sasl_authc_id,
|
|
|
|
passwd.bv_val,
|
|
|
|
sasl_authz_id );
|
|
|
|
|
|
|
|
rc = ldap_sasl_interactive_bind_s( ld, binddn,
|
|
|
|
sasl_mech, NULL, NULL,
|
|
|
|
sasl_flags, lutil_sasl_interact, defaults );
|
|
|
|
|
2003-04-06 08:48:48 +08:00
|
|
|
lutil_sasl_freedefs( defaults );
|
2002-12-13 00:32:26 +08:00
|
|
|
if( rc != LDAP_SUCCESS ) {
|
|
|
|
ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
fprintf( stderr, "%s: not compiled with SASL support\n",
|
|
|
|
prog );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
#endif
|
|
|
|
} else {
|
2004-03-16 07:54:38 +08:00
|
|
|
int msgid, err;
|
|
|
|
LDAPMessage *result;
|
|
|
|
LDAPControl **ctrls;
|
2004-03-16 10:59:03 +08:00
|
|
|
char msgbuf[256];
|
|
|
|
|
|
|
|
msgbuf[0] = 0;
|
2004-03-16 07:54:38 +08:00
|
|
|
|
|
|
|
if (( msgid = ldap_bind( ld, binddn, passwd.bv_val, authmethod )) == -1 )
|
2003-05-17 09:37:01 +08:00
|
|
|
{
|
2002-12-13 00:32:26 +08:00
|
|
|
ldap_perror( ld, "ldap_bind" );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
2004-03-16 07:54:38 +08:00
|
|
|
|
|
|
|
if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
|
|
|
|
ldap_perror( ld, "ldap_result" );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ldap_parse_result( ld, result, &err, NULL, NULL, NULL,
|
|
|
|
&ctrls, 1 ) != LDAP_SUCCESS ) {
|
|
|
|
ldap_perror( ld, "ldap_bind parse result" );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
2004-03-16 09:47:16 +08:00
|
|
|
#ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
|
2004-03-16 07:54:38 +08:00
|
|
|
if ( ctrls && ppolicy ) {
|
|
|
|
LDAPControl *ctrl;
|
2004-03-16 10:59:03 +08:00
|
|
|
int expire, grace, len = 0;
|
2004-03-16 07:54:38 +08:00
|
|
|
LDAPPasswordPolicyError pErr = -1;
|
|
|
|
|
|
|
|
ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE, ctrls );
|
|
|
|
if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
|
|
|
|
&expire, &grace, &pErr ) == LDAP_SUCCESS ) {
|
2004-03-16 10:59:03 +08:00
|
|
|
if ( pErr != PP_noError ){
|
|
|
|
msgbuf[0] = ';';
|
|
|
|
msgbuf[1] = ' ';
|
|
|
|
strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
|
|
|
|
len = strlen( msgbuf );
|
|
|
|
}
|
2004-03-16 07:54:38 +08:00
|
|
|
if ( expire >= 0 ) {
|
2004-03-16 10:59:03 +08:00
|
|
|
sprintf( msgbuf+len, " (Password expires in %d seconds)", expire );
|
2004-03-16 07:54:38 +08:00
|
|
|
} else if ( grace >= 0 ) {
|
2004-03-16 10:59:03 +08:00
|
|
|
sprintf( msgbuf+len, " (Password expired, %d grace logins remain)", grace );
|
2004-03-16 07:54:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-03-16 09:47:16 +08:00
|
|
|
#endif
|
2004-03-16 10:59:03 +08:00
|
|
|
if ( err != LDAP_SUCCESS || msgbuf[0] ) {
|
|
|
|
fprintf( stderr, "ldap_bind: %s%s\n", ldap_err2string( err ),
|
|
|
|
msgbuf );
|
2004-03-16 11:54:09 +08:00
|
|
|
if ( err != LDAP_SUCCESS ) {
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
2004-03-16 07:54:38 +08:00
|
|
|
}
|
2002-12-13 00:32:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Set server controls. Add controls extra_c[0..count-1], if set. */
|
|
|
|
void
|
|
|
|
tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
|
|
|
|
{
|
2002-12-15 00:34:59 +08:00
|
|
|
int i = 0, j, crit = 0, err;
|
2004-03-16 07:54:38 +08:00
|
|
|
LDAPControl c[8], **ctrls;
|
2002-12-15 00:34:59 +08:00
|
|
|
|
2003-02-09 14:14:06 +08:00
|
|
|
ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
|
2002-12-15 00:34:59 +08:00
|
|
|
if ( ctrls == NULL ) {
|
|
|
|
fprintf( stderr, "No memory\n" );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
2002-12-13 00:32:26 +08:00
|
|
|
|
2003-09-17 02:56:04 +08:00
|
|
|
if ( assertctl ) {
|
2003-10-14 02:09:15 +08:00
|
|
|
BerElementBuffer berbuf;
|
|
|
|
BerElement *ber = (BerElement *)&berbuf;
|
2003-06-10 10:19:30 +08:00
|
|
|
|
2003-09-17 02:56:04 +08:00
|
|
|
if( assertion == NULL || *assertion == '\0' ) {
|
2003-06-10 10:19:30 +08:00
|
|
|
fprintf( stderr, "Assertion=<empty>\n" );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
ber_init2( ber, NULL, LBER_USE_DER );
|
|
|
|
|
|
|
|
err = ldap_pvt_put_filter( ber, assertion );
|
|
|
|
if( err < 0 ) {
|
|
|
|
fprintf( stderr, "assertion encode failed (%d)\n", err );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
|
|
|
|
if( err < 0 ) {
|
|
|
|
fprintf( stderr, "assertion flatten failed (%d)\n", err );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
|
2003-09-17 02:56:04 +08:00
|
|
|
c[i].ldctl_iscritical = assertctl > 1;
|
2003-06-10 10:19:30 +08:00
|
|
|
ctrls[i] = &c[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2002-12-13 00:32:26 +08:00
|
|
|
if ( authzid ) {
|
|
|
|
c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
|
|
|
|
c[i].ldctl_value.bv_val = authzid;
|
|
|
|
c[i].ldctl_value.bv_len = strlen( authzid );
|
|
|
|
c[i].ldctl_iscritical = 1;
|
|
|
|
ctrls[i] = &c[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( manageDSAit ) {
|
|
|
|
c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
|
|
|
|
c[i].ldctl_value.bv_val = NULL;
|
|
|
|
c[i].ldctl_value.bv_len = 0;
|
|
|
|
c[i].ldctl_iscritical = manageDSAit > 1;
|
|
|
|
ctrls[i] = &c[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( noop ) {
|
|
|
|
c[i].ldctl_oid = LDAP_CONTROL_NOOP;
|
|
|
|
c[i].ldctl_value.bv_val = NULL;
|
|
|
|
c[i].ldctl_value.bv_len = 0;
|
|
|
|
c[i].ldctl_iscritical = noop > 1;
|
|
|
|
ctrls[i] = &c[i];
|
|
|
|
i++;
|
|
|
|
}
|
2004-03-16 07:54:38 +08:00
|
|
|
|
2003-09-17 02:56:04 +08:00
|
|
|
if ( preread ) {
|
|
|
|
char berbuf[LBER_ELEMENT_SIZEOF];
|
|
|
|
BerElement *ber = (BerElement *)berbuf;
|
2003-09-19 01:12:27 +08:00
|
|
|
char **attrs = NULL;
|
2003-09-17 02:56:04 +08:00
|
|
|
|
|
|
|
if( preread_attrs ) {
|
|
|
|
attrs = ldap_str2charray( preread_attrs, "," );
|
|
|
|
}
|
|
|
|
|
|
|
|
ber_init2( ber, NULL, LBER_USE_DER );
|
|
|
|
|
|
|
|
if( ber_printf( ber, "{v}", attrs ) == -1 ) {
|
|
|
|
fprintf( stderr, "preread attrs encode failed.\n" );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
|
|
|
|
if( err < 0 ) {
|
|
|
|
fprintf( stderr, "preread flatten failed (%d)\n", err );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
|
|
|
|
c[i].ldctl_iscritical = preread > 1;
|
|
|
|
ctrls[i] = &c[i];
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if( attrs ) ldap_charray_free( attrs );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( postread ) {
|
|
|
|
char berbuf[LBER_ELEMENT_SIZEOF];
|
|
|
|
BerElement *ber = (BerElement *)berbuf;
|
2003-09-19 01:12:27 +08:00
|
|
|
char **attrs = NULL;
|
2003-09-17 02:56:04 +08:00
|
|
|
|
|
|
|
if( postread_attrs ) {
|
|
|
|
attrs = ldap_str2charray( postread_attrs, "," );
|
|
|
|
}
|
|
|
|
|
|
|
|
ber_init2( ber, NULL, LBER_USE_DER );
|
|
|
|
|
|
|
|
if( ber_printf( ber, "{v}", attrs ) == -1 ) {
|
|
|
|
fprintf( stderr, "postread attrs encode failed.\n" );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
|
|
|
|
if( err < 0 ) {
|
|
|
|
fprintf( stderr, "postread flatten failed (%d)\n", err );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
|
|
|
|
c[i].ldctl_iscritical = postread > 1;
|
|
|
|
ctrls[i] = &c[i];
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if( attrs ) ldap_charray_free( attrs );
|
|
|
|
}
|
|
|
|
|
2003-05-17 09:37:01 +08:00
|
|
|
while ( count-- ) {
|
2002-12-13 00:32:26 +08:00
|
|
|
ctrls[i++] = extra_c++;
|
2003-05-17 09:37:01 +08:00
|
|
|
}
|
2002-12-13 00:32:26 +08:00
|
|
|
ctrls[i] = NULL;
|
|
|
|
|
|
|
|
err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
|
|
|
|
|
2002-12-15 00:34:59 +08:00
|
|
|
if ( err != LDAP_OPT_SUCCESS ) {
|
2003-05-17 09:37:01 +08:00
|
|
|
for ( j = 0; j < i; j++ ) {
|
|
|
|
if ( ctrls[j]->ldctl_iscritical ) crit = 1;
|
|
|
|
}
|
2002-12-13 00:32:26 +08:00
|
|
|
fprintf( stderr, "Could not set %scontrols\n",
|
2003-05-17 09:37:01 +08:00
|
|
|
crit ? "critical " : "" );
|
2002-12-15 00:34:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
free( ctrls );
|
|
|
|
if ( crit ) {
|
|
|
|
exit( EXIT_FAILURE );
|
2002-12-13 00:32:26 +08:00
|
|
|
}
|
|
|
|
}
|