2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2004-2009 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 );
101 op->o_tmpmemctx = NULL;
103 conn.c_listener = &listener;
104 conn.c_listener_url = listener_url;
105 conn.c_peer_domain = peer_domain;
106 conn.c_peer_name = peer_name;
107 conn.c_sock_name = sock_name;
109 op->o_transport_ssf = transport_ssf;
110 op->o_tls_ssf = tls_ssf;
111 op->o_sasl_ssf = sasl_ssf;
113 if ( !BER_BVISNULL( &authcID ) ) {
114 if ( !BER_BVISNULL( &authcDN ) ) {
115 fprintf( stderr, "both authcID=\"%s\" "
116 "and authcDN=\"%s\" provided\n",
117 authcID.bv_val, authcDN.bv_val );
122 rc = slap_sasl_getdn( &conn, op, &authcID, NULL,
123 &authcDN, SLAP_GETDN_AUTHCID );
124 if ( rc != LDAP_SUCCESS ) {
125 fprintf( stderr, "authcID: <%s> check failed %d (%s)\n",
127 ldap_err2string( rc ) );
132 } else if ( !BER_BVISNULL( &authcDN ) ) {
135 rc = dnNormalize( 0, NULL, NULL, &authcDN, &ndn, NULL );
136 if ( rc != LDAP_SUCCESS ) {
137 fprintf( stderr, "autchDN=\"%s\" normalization failed %d (%s)\n",
139 ldap_err2string( rc ) );
143 ch_free( authcDN.bv_val );
147 if ( !BER_BVISNULL( &authzID ) ) {
148 if ( !BER_BVISNULL( &authzDN ) ) {
149 fprintf( stderr, "both authzID=\"%s\" "
150 "and authzDN=\"%s\" provided\n",
151 authzID.bv_val, authzDN.bv_val );
156 rc = slap_sasl_getdn( &conn, op, &authzID, NULL,
157 &authzDN, SLAP_GETDN_AUTHZID );
158 if ( rc != LDAP_SUCCESS ) {
159 fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
161 ldap_err2string( rc ) );
166 } else if ( !BER_BVISNULL( &authzDN ) ) {
169 rc = dnNormalize( 0, NULL, NULL, &authzDN, &ndn, NULL );
170 if ( rc != LDAP_SUCCESS ) {
171 fprintf( stderr, "autchDN=\"%s\" normalization failed %d (%s)\n",
173 ldap_err2string( rc ) );
177 ch_free( authzDN.bv_val );
182 if ( !BER_BVISNULL( &authcDN ) ) {
183 fprintf( stderr, "authcDN: \"%s\"\n", authcDN.bv_val );
186 if ( !BER_BVISNULL( &authzDN ) ) {
187 fprintf( stderr, "authzDN: \"%s\"\n", authzDN.bv_val );
190 if ( !BER_BVISNULL( &authzDN ) ) {
194 if ( !BER_BVISNULL( &authcDN ) ) {
195 op->o_conn->c_dn = authcDN;
196 op->o_conn->c_ndn = authcDN;
199 op->o_conn->c_dn = authzDN;
200 op->o_conn->c_ndn = authzDN;
203 } else if ( !BER_BVISNULL( &authcDN ) ) {
204 op->o_conn->c_dn = authcDN;
205 op->o_conn->c_ndn = authcDN;
210 assert( !BER_BVISNULL( &baseDN ) );
211 rc = dnPrettyNormal( NULL, &baseDN, &e.e_name, &e.e_nname, NULL );
212 if ( rc != LDAP_SUCCESS ) {
213 fprintf( stderr, "base=\"%s\" normalization failed %d (%s)\n",
215 ldap_err2string( rc ) );
221 if ( op->o_bd == NULL ) {
222 /* NOTE: if no database could be found (e.g. because
223 * accessing the rootDSE or so), use the frontendDB
224 * rules; might need work */
225 op->o_bd = frontendDB;
232 fprintf( stderr, "%s: no target database "
233 "has been found for baseDN=\"%s\"; "
234 "you may try with \"-u\" (dry run).\n",
235 baseDN.bv_val, progname );
240 if ( !be->be_entry_open ||
241 !be->be_entry_close ||
245 fprintf( stderr, "%s: target database "
246 "doesn't support necessary operations; "
247 "you may try with \"-u\" (dry run).\n",
253 if ( be->be_entry_open( be, 0 ) != 0 ) {
254 fprintf( stderr, "%s: could not open database.\n",
262 id = be->be_dn2id_get( be, &e.e_nname );
264 fprintf( stderr, "%s: unable to fetch ID of DN \"%s\"\n",
265 progname, e.e_nname.bv_val );
269 ep = be->be_entry_get( be, id );
271 fprintf( stderr, "%s: unable to fetch entry \"%s\" (%lu)\n",
272 progname, e.e_nname.bv_val, id );
281 (void)print_access( op, ep, slap_schema.si_ad_entry, NULL, NULL );
282 (void)print_access( op, ep, slap_schema.si_ad_children, NULL, NULL );
284 for ( a = ep->e_attrs; a; a = a->a_next ) {
287 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
288 (void)print_access( op, ep, a->a_desc,
296 for ( ; argc--; argv++ ) {
298 AttributeDescription *desc = NULL;
299 struct berval val = BER_BVNULL,
302 char accessmaskbuf[ACCESSMASK_MAXLEN];
304 slap_access_t access = ACL_AUTH;
306 if ( attr == NULL ) {
310 val.bv_val = strchr( attr, ':' );
311 if ( val.bv_val != NULL ) {
312 val.bv_val[0] = '\0';
314 val.bv_len = strlen( val.bv_val );
318 accessstr = strchr( attr, '/' );
319 if ( accessstr != NULL ) {
324 access = str2access( accessstr );
326 case ACL_INVALID_ACCESS:
327 fprintf( stderr, "unknown access \"%s\" for attribute \"%s\"\n",
333 fprintf( stderr, "\"none\" not allowed for attribute \"%s\"\n",
343 if ( continuemode ) {
350 rc = slap_str2ad( attr, &desc, &text );
351 if ( rc != LDAP_SUCCESS ) {
352 fprintf( stderr, "slap_str2ad(%s) failed %d (%s)\n",
353 attr, rc, ldap_err2string( rc ) );
354 if ( continuemode ) {
360 rc = access_allowed_mask( op, ep, desc, valp, access,
364 fprintf( stderr, "%s access to %s%s%s: %s\n",
366 desc->ad_cname.bv_val,
367 val.bv_val ? "=" : "",
368 val.bv_val ? val.bv_val : "",
369 rc ? "ALLOWED" : "DENIED" );
372 fprintf( stderr, "%s%s%s: %s\n",
373 desc->ad_cname.bv_val,
374 val.bv_val ? "=" : "",
375 val.bv_val ? val.bv_val : "",
376 accessmask2str( mask, accessmaskbuf, 1 ) );
383 if ( !BER_BVISNULL( &e.e_name ) ) {
384 ber_memfree( e.e_name.bv_val );
386 if ( !BER_BVISNULL( &e.e_nname ) ) {
387 ber_memfree( e.e_nname.bv_val );
389 if ( !dryrun && be ) {
390 if ( ep && ep != &e ) {
391 be_entry_release_r( op, ep );
394 be->be_entry_close( be );
397 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
399 backend_shutdown( bd );
404 if ( slap_tool_destroy())