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];
48 slap_access_t access = ACL_AUTH;
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 ? "****" : val->bv_val ) : "",
57 accessmask2str( mask, accessmaskbuf, 1 ) );
63 slapacl( int argc, char **argv )
65 int rc = EXIT_SUCCESS;
66 const char *progname = "slapacl";
67 Connection conn = { 0 };
69 char opbuf[OPERATION_BUFFER_SIZE];
71 Entry e = { 0 }, *ep = &e;
75 slap_tool_init( progname, SLAPACL, argc, argv );
77 argv = &argv[ optind ];
80 op = (Operation *)opbuf;
81 connection_fake_init( &conn, op, &conn );
83 conn.c_listener = &listener;
84 conn.c_listener_url = listener_url;
85 conn.c_peer_domain = peer_domain;
86 conn.c_peer_name = peer_name;
87 conn.c_sock_name = sock_name;
89 op->o_transport_ssf = transport_ssf;
90 op->o_tls_ssf = tls_ssf;
91 op->o_sasl_ssf = sasl_ssf;
93 if ( !BER_BVISNULL( &authcID ) ) {
94 if ( !BER_BVISNULL( &authcDN ) ) {
95 fprintf( stderr, "both authcID=\"%s\" "
96 "and authcDN=\"%s\" provided\n",
97 authcID.bv_val, authcDN.bv_val );
102 rc = slap_sasl_getdn( &conn, op, &authcID, NULL,
103 &authcDN, SLAP_GETDN_AUTHCID );
104 if ( rc != LDAP_SUCCESS ) {
105 fprintf( stderr, "authcID: <%s> check failed %d (%s)\n",
107 ldap_err2string( rc ) );
112 } else if ( !BER_BVISNULL( &authcDN ) ) {
115 rc = dnNormalize( 0, NULL, NULL, &authcDN, &ndn, NULL );
116 if ( rc != LDAP_SUCCESS ) {
117 fprintf( stderr, "autchDN=\"%s\" normalization failed %d (%s)\n",
119 ldap_err2string( rc ) );
123 ch_free( authcDN.bv_val );
127 if ( !BER_BVISNULL( &authzID ) ) {
128 if ( !BER_BVISNULL( &authzDN ) ) {
129 fprintf( stderr, "both authzID=\"%s\" "
130 "and authzDN=\"%s\" provided\n",
131 authzID.bv_val, authzDN.bv_val );
136 rc = slap_sasl_getdn( &conn, op, &authzID, NULL,
137 &authzDN, SLAP_GETDN_AUTHZID );
138 if ( rc != LDAP_SUCCESS ) {
139 fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
141 ldap_err2string( rc ) );
146 } else if ( !BER_BVISNULL( &authzDN ) ) {
149 rc = dnNormalize( 0, NULL, NULL, &authzDN, &ndn, NULL );
150 if ( rc != LDAP_SUCCESS ) {
151 fprintf( stderr, "autchDN=\"%s\" normalization failed %d (%s)\n",
153 ldap_err2string( rc ) );
157 ch_free( authzDN.bv_val );
162 if ( !BER_BVISNULL( &authcDN ) ) {
163 fprintf( stderr, "authcDN: \"%s\"\n", authcDN.bv_val );
166 if ( !BER_BVISNULL( &authzDN ) ) {
167 fprintf( stderr, "authzDN: \"%s\"\n", authzDN.bv_val );
170 if ( !BER_BVISNULL( &authzDN ) ) {
174 if ( !BER_BVISNULL( &authcDN ) ) {
175 op->o_conn->c_dn = authcDN;
176 op->o_conn->c_ndn = authcDN;
179 op->o_conn->c_dn = authzDN;
180 op->o_conn->c_ndn = authzDN;
183 } else if ( !BER_BVISNULL( &authcDN ) ) {
184 op->o_conn->c_dn = authcDN;
185 op->o_conn->c_ndn = authcDN;
190 assert( !BER_BVISNULL( &baseDN ) );
191 rc = dnPrettyNormal( NULL, &baseDN, &e.e_name, &e.e_nname, NULL );
192 if ( rc != LDAP_SUCCESS ) {
193 fprintf( stderr, "base=\"%s\" normalization failed %d (%s)\n",
195 ldap_err2string( rc ) );
201 if ( !dryrun && be ) {
204 if ( !be->be_entry_open ||
205 !be->be_entry_close ||
209 fprintf( stderr, "%s: target database "
210 "doesn't support necessary operations; "
211 "you may try with \"-u\" (dry run).\n",
217 if ( be->be_entry_open( be, 0 ) != 0 ) {
218 fprintf( stderr, "%s: could not open database.\n",
226 id = be->be_dn2id_get( be, &e.e_nname );
228 fprintf( stderr, "%s: unable to fetch ID of DN \"%s\"\n",
229 progname, e.e_nname.bv_val );
233 if ( be->be_id2entry_get( be, id, &ep ) != 0 ) {
234 fprintf( stderr, "%s: unable to fetch entry \"%s\" (%lu)\n",
235 progname, e.e_nname.bv_val, id );
244 (void)print_access( op, ep, slap_schema.si_ad_entry, NULL, NULL );
245 (void)print_access( op, ep, slap_schema.si_ad_children, NULL, NULL );
247 for ( a = ep->e_attrs; a; a = a->a_next ) {
250 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
251 (void)print_access( op, ep, a->a_desc,
259 for ( ; argc--; argv++ ) {
261 AttributeDescription *desc = NULL;
262 struct berval val = BER_BVNULL,
265 char accessmaskbuf[ACCESSMASK_MAXLEN];
267 slap_access_t access = ACL_AUTH;
269 if ( attr == NULL ) {
273 val.bv_val = strchr( attr, ':' );
274 if ( val.bv_val != NULL ) {
275 val.bv_val[0] = '\0';
277 val.bv_len = strlen( val.bv_val );
281 accessstr = strchr( attr, '/' );
282 if ( accessstr != NULL ) {
285 access = str2access( accessstr );
286 if ( access == ACL_INVALID_ACCESS ) {
287 fprintf( stderr, "unknown access \"%s\" for attribute \"%s\"\n",
289 if ( continuemode ) {
296 rc = slap_str2ad( attr, &desc, &text );
297 if ( rc != LDAP_SUCCESS ) {
298 fprintf( stderr, "slap_str2ad(%s) failed %d (%s)\n",
299 attr, rc, ldap_err2string( rc ) );
300 if ( continuemode ) {
306 rc = access_allowed_mask( op, ep, desc, valp, access,
310 fprintf( stderr, "%s access to %s%s%s: %s\n",
312 desc->ad_cname.bv_val,
313 val.bv_val ? "=" : "",
314 val.bv_val ? val.bv_val : "",
315 rc ? "ALLOWED" : "DENIED" );
318 fprintf( stderr, "%s%s%s: %s\n",
319 desc->ad_cname.bv_val,
320 val.bv_val ? "=" : "",
321 val.bv_val ? val.bv_val : "",
322 accessmask2str( mask, accessmaskbuf, 1 ) );
329 if ( !BER_BVISNULL( &e.e_name ) ) {
330 ber_memfree( e.e_name.bv_val );
332 if ( !BER_BVISNULL( &e.e_nname ) ) {
333 ber_memfree( e.e_nname.bv_val );
335 if ( !dryrun && be ) {
337 be_entry_release_r( op, ep );
340 be->be_entry_close( be );