1 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 * Copyright 2004-2005 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"
41 AttributeDescription *desc,
47 char accessmaskbuf[ACCESSMASK_MAXLEN];
49 rc = access_allowed_mask( op, e, desc, nval, ACL_AUTH, NULL, &mask );
51 fprintf( stderr, "%s%s%s: %s\n",
52 desc->ad_cname.bv_val,
53 ( val && !BER_BVISNULL( val ) ) ? "=" : "",
54 ( val && !BER_BVISNULL( val ) ) ?
55 ( desc == slap_schema.si_ad_userPassword ? "****" : val->bv_val ) : "",
56 accessmask2str( mask, accessmaskbuf, 1 ) );
62 slapacl( int argc, char **argv )
64 int rc = EXIT_SUCCESS;
65 const char *progname = "slapacl";
66 Connection conn = { 0 };
68 char opbuf[OPERATION_BUFFER_SIZE];
70 Entry e = { 0 }, *ep = &e;
75 slap_tool_init( progname, SLAPACL, argc, argv );
80 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
81 if ( bd != be && backend_startup( bd ) ) {
82 fprintf( stderr, "backend_startup(#%d%s%s) failed\n",
84 bd->be_suffix ? ": " : "",
85 bd->be_suffix ? bd->be_suffix[0].bv_val : "" );
92 argv = &argv[ optind ];
95 op = (Operation *)opbuf;
96 connection_fake_init( &conn, op, &conn );
98 conn.c_listener = &listener;
99 conn.c_listener_url = listener_url;
100 conn.c_peer_domain = peer_domain;
101 conn.c_peer_name = peer_name;
102 conn.c_sock_name = sock_name;
104 op->o_transport_ssf = transport_ssf;
105 op->o_tls_ssf = tls_ssf;
106 op->o_sasl_ssf = sasl_ssf;
108 if ( !BER_BVISNULL( &authcID ) ) {
109 if ( !BER_BVISNULL( &authcDN ) ) {
110 fprintf( stderr, "both authcID=\"%s\" "
111 "and authcDN=\"%s\" provided\n",
112 authcID.bv_val, authcDN.bv_val );
117 rc = slap_sasl_getdn( &conn, op, &authcID, NULL,
118 &authcDN, SLAP_GETDN_AUTHCID );
119 if ( rc != LDAP_SUCCESS ) {
120 fprintf( stderr, "authcID: <%s> check failed %d (%s)\n",
122 ldap_err2string( rc ) );
127 } else if ( !BER_BVISNULL( &authcDN ) ) {
130 rc = dnNormalize( 0, NULL, NULL, &authcDN, &ndn, NULL );
131 if ( rc != LDAP_SUCCESS ) {
132 fprintf( stderr, "autchDN=\"%s\" normalization failed %d (%s)\n",
134 ldap_err2string( rc ) );
138 ch_free( authcDN.bv_val );
142 if ( !BER_BVISNULL( &authzID ) ) {
143 if ( !BER_BVISNULL( &authzDN ) ) {
144 fprintf( stderr, "both authzID=\"%s\" "
145 "and authzDN=\"%s\" provided\n",
146 authzID.bv_val, authzDN.bv_val );
151 rc = slap_sasl_getdn( &conn, op, &authzID, NULL,
152 &authzDN, SLAP_GETDN_AUTHZID );
153 if ( rc != LDAP_SUCCESS ) {
154 fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
156 ldap_err2string( rc ) );
161 } else if ( !BER_BVISNULL( &authzDN ) ) {
164 rc = dnNormalize( 0, NULL, NULL, &authzDN, &ndn, NULL );
165 if ( rc != LDAP_SUCCESS ) {
166 fprintf( stderr, "autchDN=\"%s\" normalization failed %d (%s)\n",
168 ldap_err2string( rc ) );
172 ch_free( authzDN.bv_val );
177 if ( !BER_BVISNULL( &authcDN ) ) {
178 fprintf( stderr, "authcDN: \"%s\"\n", authcDN.bv_val );
181 if ( !BER_BVISNULL( &authzDN ) ) {
182 fprintf( stderr, "authzDN: \"%s\"\n", authzDN.bv_val );
185 if ( !BER_BVISNULL( &authzDN ) ) {
189 if ( !BER_BVISNULL( &authcDN ) ) {
190 op->o_conn->c_dn = authcDN;
191 op->o_conn->c_ndn = authcDN;
194 op->o_conn->c_dn = authzDN;
195 op->o_conn->c_ndn = authzDN;
198 } else if ( !BER_BVISNULL( &authcDN ) ) {
199 op->o_conn->c_dn = authcDN;
200 op->o_conn->c_ndn = authcDN;
205 assert( !BER_BVISNULL( &baseDN ) );
206 rc = dnPrettyNormal( NULL, &baseDN, &e.e_name, &e.e_nname, NULL );
207 if ( rc != LDAP_SUCCESS ) {
208 fprintf( stderr, "base=\"%s\" normalization failed %d (%s)\n",
210 ldap_err2string( rc ) );
216 if ( !dryrun && be ) {
219 if ( !be->be_entry_open ||
220 !be->be_entry_close ||
224 fprintf( stderr, "%s: target database "
225 "doesn't support necessary operations; "
226 "you may try with \"-u\" (dry run).\n",
232 if ( be->be_entry_open( be, 0 ) != 0 ) {
233 fprintf( stderr, "%s: could not open database.\n",
241 id = be->be_dn2id_get( be, &e.e_nname );
243 fprintf( stderr, "%s: unable to fetch ID of DN \"%s\"\n",
244 progname, e.e_nname.bv_val );
248 if ( be->be_id2entry_get( be, id, &ep ) != 0 ) {
249 fprintf( stderr, "%s: unable to fetch entry \"%s\" (%lu)\n",
250 progname, e.e_nname.bv_val, id );
259 (void)print_access( op, ep, slap_schema.si_ad_entry, NULL, NULL );
260 (void)print_access( op, ep, slap_schema.si_ad_children, NULL, NULL );
262 for ( a = ep->e_attrs; a; a = a->a_next ) {
265 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
266 (void)print_access( op, ep, a->a_desc,
274 for ( ; argc--; argv++ ) {
276 AttributeDescription *desc = NULL;
277 struct berval val = BER_BVNULL,
280 char accessmaskbuf[ACCESSMASK_MAXLEN];
282 slap_access_t access = ACL_AUTH;
284 if ( attr == NULL ) {
288 val.bv_val = strchr( attr, ':' );
289 if ( val.bv_val != NULL ) {
290 val.bv_val[0] = '\0';
292 val.bv_len = strlen( val.bv_val );
296 accessstr = strchr( attr, '/' );
297 if ( accessstr != NULL ) {
300 access = str2access( accessstr );
301 if ( access == ACL_INVALID_ACCESS ) {
302 fprintf( stderr, "unknown access \"%s\" for attribute \"%s\"\n",
304 if ( continuemode ) {
311 rc = slap_str2ad( attr, &desc, &text );
312 if ( rc != LDAP_SUCCESS ) {
313 fprintf( stderr, "slap_str2ad(%s) failed %d (%s)\n",
314 attr, rc, ldap_err2string( rc ) );
315 if ( continuemode ) {
321 rc = access_allowed_mask( op, ep, desc, valp, access,
325 fprintf( stderr, "%s access to %s%s%s: %s\n",
327 desc->ad_cname.bv_val,
328 val.bv_val ? "=" : "",
329 val.bv_val ? val.bv_val : "",
330 rc ? "ALLOWED" : "DENIED" );
333 fprintf( stderr, "%s%s%s: %s\n",
334 desc->ad_cname.bv_val,
335 val.bv_val ? "=" : "",
336 val.bv_val ? val.bv_val : "",
337 accessmask2str( mask, accessmaskbuf, 1 ) );
344 if ( !BER_BVISNULL( &e.e_name ) ) {
345 ber_memfree( e.e_name.bv_val );
347 if ( !BER_BVISNULL( &e.e_nname ) ) {
348 ber_memfree( e.e_nname.bv_val );
350 if ( !dryrun && be ) {
352 be_entry_release_r( op, ep );
355 be->be_entry_close( be );
358 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
360 backend_shutdown( bd );