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;
74 slap_tool_init( progname, SLAPACL, argc, argv );
76 argv = &argv[ optind ];
79 op = (Operation *)opbuf;
80 connection_fake_init( &conn, op, &conn );
82 conn.c_listener = &listener;
83 conn.c_listener_url = listener_url;
84 conn.c_peer_domain = peer_domain;
85 conn.c_peer_name = peer_name;
86 conn.c_sock_name = sock_name;
88 op->o_transport_ssf = transport_ssf;
89 op->o_tls_ssf = tls_ssf;
90 op->o_sasl_ssf = sasl_ssf;
92 if ( !BER_BVISNULL( &authcID ) ) {
93 if ( !BER_BVISNULL( &authcDN ) ) {
94 fprintf( stderr, "both authcID=\"%s\" "
95 "and authcDN=\"%s\" provided\n",
96 authcID.bv_val, authcDN.bv_val );
101 rc = slap_sasl_getdn( &conn, op, &authcID, NULL,
102 &authcDN, SLAP_GETDN_AUTHCID );
103 if ( rc != LDAP_SUCCESS ) {
104 fprintf( stderr, "authcID: <%s> check failed %d (%s)\n",
106 ldap_err2string( rc ) );
111 } else if ( !BER_BVISNULL( &authcDN ) ) {
114 rc = dnNormalize( 0, NULL, NULL, &authcDN, &ndn, NULL );
115 if ( rc != LDAP_SUCCESS ) {
116 fprintf( stderr, "autchDN=\"%s\" normalization failed %d (%s)\n",
118 ldap_err2string( rc ) );
122 ch_free( authcDN.bv_val );
126 if ( !BER_BVISNULL( &authzID ) ) {
127 if ( !BER_BVISNULL( &authzDN ) ) {
128 fprintf( stderr, "both authzID=\"%s\" "
129 "and authzDN=\"%s\" provided\n",
130 authzID.bv_val, authzDN.bv_val );
135 rc = slap_sasl_getdn( &conn, op, &authzID, NULL,
136 &authzDN, SLAP_GETDN_AUTHZID );
137 if ( rc != LDAP_SUCCESS ) {
138 fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
140 ldap_err2string( rc ) );
145 } else if ( !BER_BVISNULL( &authzDN ) ) {
148 rc = dnNormalize( 0, NULL, NULL, &authzDN, &ndn, NULL );
149 if ( rc != LDAP_SUCCESS ) {
150 fprintf( stderr, "autchDN=\"%s\" normalization failed %d (%s)\n",
152 ldap_err2string( rc ) );
156 ch_free( authzDN.bv_val );
161 if ( !BER_BVISNULL( &authcDN ) ) {
162 fprintf( stderr, "authcDN: \"%s\"\n", authcDN.bv_val );
165 if ( !BER_BVISNULL( &authzDN ) ) {
166 fprintf( stderr, "authzDN: \"%s\"\n", authzDN.bv_val );
169 assert( !BER_BVISNULL( &baseDN ) );
170 rc = dnPrettyNormal( NULL, &baseDN, &e.e_name, &e.e_nname, NULL );
171 if ( rc != LDAP_SUCCESS ) {
172 fprintf( stderr, "base=\"%s\" normalization failed %d (%s)\n",
174 ldap_err2string( rc ) );
180 if ( !BER_BVISNULL( &authzDN ) ) {
184 if ( !BER_BVISNULL( &authcDN ) ) {
185 op->o_conn->c_dn = authcDN;
186 op->o_conn->c_ndn = authcDN;
189 if ( !dryrun && be ) {
192 if ( !be->be_entry_open ||
193 !be->be_entry_close ||
197 fprintf( stderr, "%s: target database "
198 "doesn't support necessary operations; "
199 "you may try with \"-u\" (dry run).\n",
205 if ( be->be_entry_open( be, 0 ) != 0 ) {
206 fprintf( stderr, "%s: could not open database.\n",
212 id = be->be_dn2id_get( be, &e.e_nname );
214 fprintf( stderr, "%s: unable to fetch ID of DN \"%s\"\n",
215 progname, e.e_nname.bv_val );
219 if ( be->be_id2entry_get( be, id, &ep ) != 0 ) {
220 fprintf( stderr, "%s: unable to fetch entry \"%s\" (%lu)\n",
221 progname, e.e_nname.bv_val, id );
230 (void)print_access( op, ep, slap_schema.si_ad_entry, NULL, NULL );
231 (void)print_access( op, ep, slap_schema.si_ad_children, NULL, NULL );
233 for ( a = ep->e_attrs; a; a = a->a_next ) {
236 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
237 (void)print_access( op, ep, a->a_desc,
245 for ( ; argc--; argv++ ) {
247 AttributeDescription *desc = NULL;
248 struct berval val = BER_BVNULL,
251 char accessmaskbuf[ACCESSMASK_MAXLEN];
253 slap_access_t access = ACL_AUTH;
255 if ( attr == NULL ) {
259 val.bv_val = strchr( attr, ':' );
260 if ( val.bv_val != NULL ) {
261 val.bv_val[0] = '\0';
263 val.bv_len = strlen( val.bv_val );
267 accessstr = strchr( attr, '/' );
268 if ( accessstr != NULL ) {
271 access = str2access( accessstr );
272 if ( access == ACL_INVALID_ACCESS ) {
273 fprintf( stderr, "unknown access \"%s\" for attribute \"%s\"\n",
275 if ( continuemode ) {
282 rc = slap_str2ad( attr, &desc, &text );
283 if ( rc != LDAP_SUCCESS ) {
284 fprintf( stderr, "slap_str2ad(%s) failed %d (%s)\n",
285 attr, rc, ldap_err2string( rc ) );
286 if ( continuemode ) {
292 rc = access_allowed_mask( op, ep, desc, valp, access,
296 fprintf( stderr, "%s access to %s%s%s: %s\n",
298 desc->ad_cname.bv_val,
299 val.bv_val ? "=" : "",
300 val.bv_val ? val.bv_val : "",
301 rc ? "ALLOWED" : "DENIED" );
304 fprintf( stderr, "%s%s%s: %s\n",
305 desc->ad_cname.bv_val,
306 val.bv_val ? "=" : "",
307 val.bv_val ? val.bv_val : "",
308 accessmask2str( mask, accessmaskbuf, 1 ) );
315 ber_memfree( e.e_name.bv_val );
316 ber_memfree( e.e_nname.bv_val );
317 if ( !dryrun && be ) {
319 be_entry_release_r( op, ep );
321 be->be_entry_close( be );