2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2004-2008 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"
42 AttributeDescription *desc,
48 char accessmaskbuf[ACCESSMASK_MAXLEN];
50 rc = access_allowed_mask( op, e, desc, nval, ACL_AUTH, NULL, &mask );
52 fprintf( stderr, "%s%s%s: %s\n",
53 desc->ad_cname.bv_val,
54 ( val && !BER_BVISNULL( val ) ) ? "=" : "",
55 ( val && !BER_BVISNULL( val ) ) ?
56 ( desc == slap_schema.si_ad_userPassword ?
57 "****" : val->bv_val ) : "",
58 accessmask2str( mask, accessmaskbuf, 1 ) );
64 slapacl( int argc, char **argv )
66 int rc = EXIT_SUCCESS;
67 const char *progname = "slapacl";
68 Connection conn = { 0 };
70 OperationBuffer opbuf;
72 Entry e = { 0 }, *ep = &e;
77 slap_tool_init( progname, SLAPACL, argc, argv );
82 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
83 if ( bd != be && backend_startup( bd ) ) {
84 fprintf( stderr, "backend_startup(#%d%s%s) failed\n",
86 bd->be_suffix ? ": " : "",
87 bd->be_suffix ? bd->be_suffix[0].bv_val : "" );
96 argv = &argv[ optind ];
99 connection_fake_init( &conn, &opbuf, &conn );
102 conn.c_listener = &listener;
103 conn.c_listener_url = listener_url;
104 conn.c_peer_domain = peer_domain;
105 conn.c_peer_name = peer_name;
106 conn.c_sock_name = sock_name;
108 op->o_transport_ssf = transport_ssf;
109 op->o_tls_ssf = tls_ssf;
110 op->o_sasl_ssf = sasl_ssf;
112 if ( !BER_BVISNULL( &authcID ) ) {
113 if ( !BER_BVISNULL( &authcDN ) ) {
114 fprintf( stderr, "both authcID=\"%s\" "
115 "and authcDN=\"%s\" provided\n",
116 authcID.bv_val, authcDN.bv_val );
121 rc = slap_sasl_getdn( &conn, op, &authcID, NULL,
122 &authcDN, SLAP_GETDN_AUTHCID );
123 if ( rc != LDAP_SUCCESS ) {
124 fprintf( stderr, "authcID: <%s> check failed %d (%s)\n",
126 ldap_err2string( rc ) );
131 } else if ( !BER_BVISNULL( &authcDN ) ) {
134 rc = dnNormalize( 0, NULL, NULL, &authcDN, &ndn, NULL );
135 if ( rc != LDAP_SUCCESS ) {
136 fprintf( stderr, "autchDN=\"%s\" normalization failed %d (%s)\n",
138 ldap_err2string( rc ) );
142 ch_free( authcDN.bv_val );
146 if ( !BER_BVISNULL( &authzID ) ) {
147 if ( !BER_BVISNULL( &authzDN ) ) {
148 fprintf( stderr, "both authzID=\"%s\" "
149 "and authzDN=\"%s\" provided\n",
150 authzID.bv_val, authzDN.bv_val );
155 rc = slap_sasl_getdn( &conn, op, &authzID, NULL,
156 &authzDN, SLAP_GETDN_AUTHZID );
157 if ( rc != LDAP_SUCCESS ) {
158 fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
160 ldap_err2string( rc ) );
165 } else if ( !BER_BVISNULL( &authzDN ) ) {
168 rc = dnNormalize( 0, NULL, NULL, &authzDN, &ndn, NULL );
169 if ( rc != LDAP_SUCCESS ) {
170 fprintf( stderr, "autchDN=\"%s\" normalization failed %d (%s)\n",
172 ldap_err2string( rc ) );
176 ch_free( authzDN.bv_val );
181 if ( !BER_BVISNULL( &authcDN ) ) {
182 fprintf( stderr, "authcDN: \"%s\"\n", authcDN.bv_val );
185 if ( !BER_BVISNULL( &authzDN ) ) {
186 fprintf( stderr, "authzDN: \"%s\"\n", authzDN.bv_val );
189 if ( !BER_BVISNULL( &authzDN ) ) {
193 if ( !BER_BVISNULL( &authcDN ) ) {
194 op->o_conn->c_dn = authcDN;
195 op->o_conn->c_ndn = authcDN;
198 op->o_conn->c_dn = authzDN;
199 op->o_conn->c_ndn = authzDN;
202 } else if ( !BER_BVISNULL( &authcDN ) ) {
203 op->o_conn->c_dn = authcDN;
204 op->o_conn->c_ndn = authcDN;
209 assert( !BER_BVISNULL( &baseDN ) );
210 rc = dnPrettyNormal( NULL, &baseDN, &e.e_name, &e.e_nname, NULL );
211 if ( rc != LDAP_SUCCESS ) {
212 fprintf( stderr, "base=\"%s\" normalization failed %d (%s)\n",
214 ldap_err2string( rc ) );
220 if ( op->o_bd == NULL ) {
221 /* NOTE: if no database could be found (e.g. because
222 * accessing the rootDSE or so), use the frontendDB
223 * rules; might need work */
224 op->o_bd = frontendDB;
231 fprintf( stderr, "%s: no target database "
232 "has been found for baseDN=\"%s\"; "
233 "you may try with \"-u\" (dry run).\n",
234 baseDN.bv_val, progname );
239 if ( !be->be_entry_open ||
240 !be->be_entry_close ||
244 fprintf( stderr, "%s: target database "
245 "doesn't support necessary operations; "
246 "you may try with \"-u\" (dry run).\n",
252 if ( be->be_entry_open( be, 0 ) != 0 ) {
253 fprintf( stderr, "%s: could not open database.\n",
261 id = be->be_dn2id_get( be, &e.e_nname );
263 fprintf( stderr, "%s: unable to fetch ID of DN \"%s\"\n",
264 progname, e.e_nname.bv_val );
268 ep = be->be_entry_get( be, id );
270 fprintf( stderr, "%s: unable to fetch entry \"%s\" (%lu)\n",
271 progname, e.e_nname.bv_val, id );
280 (void)print_access( op, ep, slap_schema.si_ad_entry, NULL, NULL );
281 (void)print_access( op, ep, slap_schema.si_ad_children, NULL, NULL );
283 for ( a = ep->e_attrs; a; a = a->a_next ) {
286 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
287 (void)print_access( op, ep, a->a_desc,
295 for ( ; argc--; argv++ ) {
297 AttributeDescription *desc = NULL;
298 struct berval val = BER_BVNULL,
301 char accessmaskbuf[ACCESSMASK_MAXLEN];
303 slap_access_t access = ACL_AUTH;
305 if ( attr == NULL ) {
309 val.bv_val = strchr( attr, ':' );
310 if ( val.bv_val != NULL ) {
311 val.bv_val[0] = '\0';
313 val.bv_len = strlen( val.bv_val );
317 accessstr = strchr( attr, '/' );
318 if ( accessstr != NULL ) {
323 access = str2access( accessstr );
325 case ACL_INVALID_ACCESS:
326 fprintf( stderr, "unknown access \"%s\" for attribute \"%s\"\n",
332 fprintf( stderr, "\"none\" not allowed for attribute \"%s\"\n",
342 if ( continuemode ) {
349 rc = slap_str2ad( attr, &desc, &text );
350 if ( rc != LDAP_SUCCESS ) {
351 fprintf( stderr, "slap_str2ad(%s) failed %d (%s)\n",
352 attr, rc, ldap_err2string( rc ) );
353 if ( continuemode ) {
359 rc = access_allowed_mask( op, ep, desc, valp, access,
363 fprintf( stderr, "%s access to %s%s%s: %s\n",
365 desc->ad_cname.bv_val,
366 val.bv_val ? "=" : "",
367 val.bv_val ? val.bv_val : "",
368 rc ? "ALLOWED" : "DENIED" );
371 fprintf( stderr, "%s%s%s: %s\n",
372 desc->ad_cname.bv_val,
373 val.bv_val ? "=" : "",
374 val.bv_val ? val.bv_val : "",
375 accessmask2str( mask, accessmaskbuf, 1 ) );
382 if ( !BER_BVISNULL( &e.e_name ) ) {
383 ber_memfree( e.e_name.bv_val );
385 if ( !BER_BVISNULL( &e.e_nname ) ) {
386 ber_memfree( e.e_nname.bv_val );
388 if ( !dryrun && be ) {
389 if ( ep && ep != &e ) {
390 be_entry_release_r( op, ep );
393 be->be_entry_close( be );
396 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
398 backend_shutdown( bd );
403 if ( slap_tool_destroy())