openldap/servers/slapd/slapauth.c

174 lines
3.5 KiB
C
Raw Normal View History

2004-04-14 01:18:03 +08:00
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 2004 The OpenLDAP Foundation.
* Portions Copyright 2004 Pierangelo Masarati.
* 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 file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
/* ACKNOWLEDGEMENTS:
* This work was initially developed by Pierangelo Masarati for inclusion
* in OpenLDAP Software.
*/
#include "portable.h"
#include <stdio.h>
#include <ac/stdlib.h>
#include <ac/ctype.h>
#include <ac/string.h>
#include <ac/socket.h>
#include <ac/unistd.h>
#include <lber.h>
#include <ldif.h>
#include <lutil.h>
#include "slapcommon.h"
static int
do_check( Connection *c, Operation *op, struct berval *id )
{
2004-04-20 08:08:44 +08:00
struct berval authcdn;
2004-04-14 01:18:03 +08:00
int rc;
2004-04-20 08:08:44 +08:00
rc = slap_sasl_getdn( c, op, id, NULL, &authcdn, SLAP_GETDN_AUTHCID );
2004-04-14 01:18:03 +08:00
if ( rc != LDAP_SUCCESS ) {
fprintf( stderr, "ID: <%s> check failed %d (%s)\n",
id->bv_val, rc,
ldap_err2string( rc ) );
rc = 1;
} else {
if ( !BER_BVISNULL( &authzID ) ) {
2004-04-20 08:08:44 +08:00
rc = slap_sasl_authorized( op, &authcdn, &authzID );
2004-04-14 01:18:03 +08:00
fprintf( stderr,
"ID: <%s>\n"
"authcDN: <%s>\n"
"authzDN: <%s>\n"
"authorization %s\n",
id->bv_val,
2004-04-20 08:08:44 +08:00
authcdn.bv_val,
2004-04-14 01:18:03 +08:00
authzID.bv_val,
rc == LDAP_SUCCESS ? "OK" : "failed" );
} else {
fprintf( stderr, "ID: <%s> check succeeded\n"
"authcID: <%s>\n",
id->bv_val,
2004-04-20 08:08:44 +08:00
authcdn.bv_val );
op->o_tmpfree( authcdn.bv_val, op->o_tmpmemctx );
2004-04-14 01:18:03 +08:00
}
rc = 0;
}
return rc;
}
int
2004-04-16 14:12:13 +08:00
slapauth( int argc, char **argv )
2004-04-14 01:18:03 +08:00
{
int rc = EXIT_SUCCESS;
2004-04-16 14:12:13 +08:00
const char *progname = "slapauth";
2004-04-14 01:18:03 +08:00
Connection conn;
Operation op;
#ifdef NEW_LOGGING
lutil_log_initialize( argc, argv );
#endif
2004-04-16 14:12:13 +08:00
slap_tool_init( progname, SLAPAUTH, argc, argv );
2004-04-14 01:18:03 +08:00
argv = &argv[ optind ];
argc -= optind;
memset( &conn, 0, sizeof( Connection ) );
memset( &op, 0, sizeof( Operation ) );
connection_fake_init( &conn, &op, &conn );
if ( !BER_BVISNULL( &authzID ) ) {
2004-04-20 08:08:44 +08:00
struct berval authzdn;
2004-04-14 01:18:03 +08:00
2004-04-20 08:08:44 +08:00
rc = slap_sasl_getdn( &conn, &op, &authzID, NULL, &authzdn,
2004-04-14 01:18:03 +08:00
SLAP_GETDN_AUTHZID );
if ( rc != LDAP_SUCCESS ) {
fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
authzID.bv_val, rc,
ldap_err2string( rc ) );
rc = 1;
BER_BVZERO( &authzID );
goto destroy;
}
2004-04-20 08:08:44 +08:00
authzID = authzdn;
2004-04-14 01:18:03 +08:00
}
if ( !BER_BVISNULL( &authcID ) ) {
if ( !BER_BVISNULL( &authzID ) || argc == 0 ) {
rc = do_check( &conn, &op, &authcID );
goto destroy;
}
for ( ; argc--; argv++ ) {
2004-04-20 08:08:44 +08:00
struct berval authzdn;
2004-04-14 01:18:03 +08:00
ber_str2bv( argv[ 0 ], 0, 0, &authzID );
2004-04-20 08:08:44 +08:00
rc = slap_sasl_getdn( &conn, &op, &authzID, NULL, &authzdn,
2004-04-14 01:18:03 +08:00
SLAP_GETDN_AUTHZID );
if ( rc != LDAP_SUCCESS ) {
fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
authzID.bv_val, rc,
ldap_err2string( rc ) );
rc = 1;
BER_BVZERO( &authzID );
goto destroy;
}
2004-04-20 08:08:44 +08:00
authzID = authzdn;
2004-04-14 01:18:03 +08:00
rc = do_check( &conn, &op, &authcID );
op.o_tmpfree( authzID.bv_val, op.o_tmpmemctx );
BER_BVZERO( &authzID );
if ( rc ) {
goto destroy;
}
}
goto destroy;
}
for ( ; argc--; argv++ ) {
struct berval id;
ber_str2bv( argv[ 0 ], 0, 0, &id );
rc = do_check( &conn, &op, &id );
if ( rc ) {
goto destroy;
}
}
destroy:;
if ( !BER_BVISNULL( &authzID ) ) {
op.o_tmpfree( authzID.bv_val, op.o_tmpmemctx );
}
slap_tool_destroy();
return rc;
}