1 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 * Copyright 2004 The OpenLDAP Foundation.
4 * Portions Copyright 2004 Pierangelo Masarati.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Pierangelo Masarati for inclusion
17 * in OpenLDAP Software.
24 #include <ac/stdlib.h>
27 #include <ac/string.h>
28 #include <ac/socket.h>
29 #include <ac/unistd.h>
35 #include "slapcommon.h"
38 do_check( Connection *c, Operation *op, struct berval *id )
40 struct berval authcdn;
43 rc = slap_sasl_getdn( c, op, id, NULL, &authcdn, SLAP_GETDN_AUTHCID );
44 if ( rc != LDAP_SUCCESS ) {
45 fprintf( stderr, "ID: <%s> check failed %d (%s)\n",
47 ldap_err2string( rc ) );
51 if ( !BER_BVISNULL( &authzID ) ) {
52 rc = slap_sasl_authorized( op, &authcdn, &authzID );
62 rc == LDAP_SUCCESS ? "OK" : "failed" );
65 fprintf( stderr, "ID: <%s> check succeeded\n"
69 op->o_tmpfree( authcdn.bv_val, op->o_tmpmemctx );
78 slapauth( int argc, char **argv )
80 int rc = EXIT_SUCCESS;
81 const char *progname = "slapauth";
85 slap_tool_init( progname, SLAPAUTH, argc, argv );
87 argv = &argv[ optind ];
90 memset( &conn, 0, sizeof( Connection ) );
91 memset( &op, 0, sizeof( Operation ) );
93 connection_fake_init( &conn, &op, &conn );
95 if ( !BER_BVISNULL( &authzID ) ) {
96 struct berval authzdn;
98 rc = slap_sasl_getdn( &conn, &op, &authzID, NULL, &authzdn,
100 if ( rc != LDAP_SUCCESS ) {
101 fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
103 ldap_err2string( rc ) );
105 BER_BVZERO( &authzID );
113 if ( !BER_BVISNULL( &authcID ) ) {
114 if ( !BER_BVISNULL( &authzID ) || argc == 0 ) {
115 rc = do_check( &conn, &op, &authcID );
119 for ( ; argc--; argv++ ) {
120 struct berval authzdn;
122 ber_str2bv( argv[ 0 ], 0, 0, &authzID );
124 rc = slap_sasl_getdn( &conn, &op, &authzID, NULL, &authzdn,
125 SLAP_GETDN_AUTHZID );
126 if ( rc != LDAP_SUCCESS ) {
127 fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
129 ldap_err2string( rc ) );
131 BER_BVZERO( &authzID );
132 if ( !continuemode ) {
139 rc = do_check( &conn, &op, &authcID );
141 op.o_tmpfree( authzID.bv_val, op.o_tmpmemctx );
142 BER_BVZERO( &authzID );
144 if ( rc && !continuemode ) {
152 for ( ; argc--; argv++ ) {
155 ber_str2bv( argv[ 0 ], 0, 0, &id );
157 rc = do_check( &conn, &op, &id );
159 if ( rc && !continuemode ) {
165 if ( !BER_BVISNULL( &authzID ) ) {
166 op.o_tmpfree( authzID.bv_val, op.o_tmpmemctx );