2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2004-2015 The OpenLDAP Foundation.
5 * Portions Copyright 2004 Pierangelo Masarati.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Pierangelo Masarati for inclusion
18 * in OpenLDAP Software.
25 #include <ac/stdlib.h>
28 #include <ac/string.h>
29 #include <ac/socket.h>
30 #include <ac/unistd.h>
36 #include "slapcommon.h"
39 do_check( Connection *c, Operation *op, struct berval *id )
41 struct berval authcdn;
44 rc = slap_sasl_getdn( c, op, id, realm, &authcdn, SLAP_GETDN_AUTHCID );
45 if ( rc != LDAP_SUCCESS ) {
46 fprintf( stderr, "ID: <%s> check failed %d (%s)\n",
48 ldap_err2string( rc ) );
52 if ( !BER_BVISNULL( &authzID ) ) {
53 rc = slap_sasl_authorized( op, &authcdn, &authzID );
63 rc == LDAP_SUCCESS ? "OK" : "failed" );
66 fprintf( stderr, "ID: <%s> check succeeded\n"
70 op->o_tmpfree( authcdn.bv_val, op->o_tmpmemctx );
79 slapauth( int argc, char **argv )
81 int rc = EXIT_SUCCESS;
82 const char *progname = "slapauth";
83 Connection conn = {0};
84 OperationBuffer opbuf;
88 slap_tool_init( progname, SLAPAUTH, argc, argv );
90 argv = &argv[ optind ];
93 thrctx = ldap_pvt_thread_pool_context();
94 connection_fake_init( &conn, &opbuf, thrctx );
97 conn.c_sasl_bind_mech = mech;
99 if ( !BER_BVISNULL( &authzID ) ) {
100 struct berval authzdn;
102 rc = slap_sasl_getdn( &conn, op, &authzID, NULL, &authzdn,
103 SLAP_GETDN_AUTHZID );
104 if ( rc != LDAP_SUCCESS ) {
105 fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
107 ldap_err2string( rc ) );
109 BER_BVZERO( &authzID );
117 if ( !BER_BVISNULL( &authcID ) ) {
118 if ( !BER_BVISNULL( &authzID ) || argc == 0 ) {
119 rc = do_check( &conn, op, &authcID );
123 for ( ; argc--; argv++ ) {
124 struct berval authzdn;
126 ber_str2bv( argv[ 0 ], 0, 0, &authzID );
128 rc = slap_sasl_getdn( &conn, op, &authzID, NULL, &authzdn,
129 SLAP_GETDN_AUTHZID );
130 if ( rc != LDAP_SUCCESS ) {
131 fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
133 ldap_err2string( rc ) );
135 BER_BVZERO( &authzID );
136 if ( !continuemode ) {
143 rc = do_check( &conn, op, &authcID );
145 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
146 BER_BVZERO( &authzID );
148 if ( rc && !continuemode ) {
156 for ( ; argc--; argv++ ) {
159 ber_str2bv( argv[ 0 ], 0, 0, &id );
161 rc = do_check( &conn, op, &id );
163 if ( rc && !continuemode ) {
169 if ( !BER_BVISNULL( &authzID ) ) {
170 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
172 if ( slap_tool_destroy())