1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 2004-2021 The OpenLDAP Foundation.
* Portions Copyright 2004 Pierangelo Masarati.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
/* ACKNOWLEDGEMENTS:
* This work was initially developed by Pierangelo Masarati for inclusion
* in OpenLDAP Software.
*/
#include "portable.h"
#include <stdio.h>
#include <ac/stdlib.h>
#include <ac/ctype.h>
#include <ac/string.h>
#include <ac/socket.h>
#include <ac/unistd.h>
#include <lber.h>
#include <ldif.h>
#include <lutil.h>
#include "slapcommon.h"
static int
print_access(
Operation *op,
Entry *e,
AttributeDescription *desc,
struct berval *val,
struct berval *nval )
{
int rc;
slap_mask_t mask;
char accessmaskbuf[ACCESSMASK_MAXLEN];
rc = access_allowed_mask( op, e, desc, nval, ACL_AUTH, NULL, &mask );
fprintf( stderr, "%s%s%s: %s\n",
desc->ad_cname.bv_val,
( val && !BER_BVISNULL( val ) ) ? "=" : "",
( val && !BER_BVISNULL( val ) ) ?
( desc == slap_schema.si_ad_userPassword ?
"****" : val->bv_val ) : "",
accessmask2str( mask, accessmaskbuf, 1 ) );
return rc;
}
int
slapacl( int argc, char **argv )
{
int rc = EXIT_SUCCESS;
const char *progname = "slapacl";
Connection conn = { 0 };
Listener listener;
OperationBuffer opbuf;
Operation *op = NULL;
Entry e = { 0 }, *ep = &e;
char *attr = NULL;
int doclose = 0;
BackendDB *bd;
void *thrctx;
slap_tool_init( progname, SLAPACL, argc, argv );
if ( !dryrun ) {
int i = 0;
LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
if ( bd != be && backend_startup( bd ) ) {
fprintf( stderr, "backend_startup(#%d%s%s) failed\n",
i,
bd->be_suffix ? ": " : "",
bd->be_suffix ? bd->be_suffix[0].bv_val : "" );
rc = 1;
goto destroy;
}
i++;
}
}
argv = &argv[ optind ];
argc -= optind;
thrctx = ldap_pvt_thread_pool_context();
connection_fake_init( &conn, &opbuf, thrctx );
op = &opbuf.ob_op;
op->o_tmpmemctx = NULL;
conn.c_listener = &listener;
conn.c_listener_url = listener_url;
conn.c_peer_domain = peer_domain;
conn.c_peer_name = peer_name;
conn.c_sock_name = sock_name;
op->o_ssf = ssf;
op->o_transport_ssf = transport_ssf;
op->o_tls_ssf = tls_ssf;
op->o_sasl_ssf = sasl_ssf;
if ( !BER_BVISNULL( &authcID ) ) {
if ( !BER_BVISNULL( &authcDN ) ) {
fprintf( stderr, "both authcID=\"%s\" "
"and authcDN=\"%s\" provided\n",
authcID.bv_val, authcDN.bv_val );
rc = 1;
goto destroy;
}
rc = slap_sasl_getdn( &conn, op, &authcID, NULL,
&authcDN, SLAP_GETDN_AUTHCID );
if ( rc != LDAP_SUCCESS ) {
fprintf( stderr, "authcID: <%s> check failed %d (%s)\n",
authcID.bv_val, rc,
ldap_err2string( rc ) );
rc = 1;
goto destroy;
}
} else if ( !BER_BVISNULL( &authcDN ) ) {
struct berval ndn;
rc = dnNormalize( 0, NULL, NULL, &authcDN, &ndn, NULL );
if ( rc != LDAP_SUCCESS ) {
fprintf( stderr, "autchDN=\"%s\" normalization failed %d (%s)\n",
authcDN.bv_val, rc,
ldap_err2string( rc ) );
rc = 1;
goto destroy;
}
ch_free( authcDN.bv_val );
authcDN = ndn;
}
if ( !BER_BVISNULL( &authzID ) ) {
if ( !BER_BVISNULL( &authzDN ) ) {
fprintf( stderr, "both authzID=\"%s\" "
"and authzDN=\"%s\" provided\n",
authzID.bv_val, authzDN.bv_val );
rc = 1;
goto destroy;
}
rc = slap_sasl_getdn( &conn, op, &authzID, NULL,
&authzDN, SLAP_GETDN_AUTHZID );
if ( rc != LDAP_SUCCESS ) {
fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
authzID.bv_val, rc,
ldap_err2string( rc ) );
rc = 1;
goto destroy;
}
} else if ( !BER_BVISNULL( &authzDN ) ) {
struct berval ndn;
rc = dnNormalize( 0, NULL, NULL, &authzDN, &ndn, NULL );
if ( rc != LDAP_SUCCESS ) {
fprintf( stderr, "autchDN=\"%s\" normalization failed %d (%s)\n",
authzDN.bv_val, rc,
ldap_err2string( rc ) );
rc = 1;
goto destroy;
}
ch_free( authzDN.bv_val );
authzDN = ndn;
}
if ( !BER_BVISNULL( &authcDN ) ) {
fprintf( stderr, "authcDN: \"%s\"\n", authcDN.bv_val );
}
if ( !BER_BVISNULL( &authzDN ) ) {
fprintf( stderr, "authzDN: \"%s\"\n", authzDN.bv_val );
}
if ( !BER_BVISNULL( &authzDN ) ) {
op->o_dn = authzDN;
op->o_ndn = authzDN;
if ( !BER_BVISNULL( &authcDN ) ) {
op->o_conn->c_dn = authcDN;
op->o_conn->c_ndn = authcDN;
} else {
op->o_conn->c_dn = authzDN;
op->o_conn->c_ndn = authzDN;
}
} else if ( !BER_BVISNULL( &authcDN ) ) {
op->o_conn->c_dn = authcDN;
op->o_conn->c_ndn = authcDN;
op->o_dn = authcDN;
op->o_ndn = authcDN;
}
assert( !BER_BVISNULL( &baseDN ) );
rc = dnPrettyNormal( NULL, &baseDN, &e.e_name, &e.e_nname, NULL );
if ( rc != LDAP_SUCCESS ) {
fprintf( stderr, "base=\"%s\" normalization failed %d (%s)\n",
baseDN.bv_val, rc,
ldap_err2string( rc ) );
rc = 1;
goto destroy;
}
op->o_bd = be;
if ( op->o_bd == NULL ) {
/* NOTE: if no database could be found (e.g. because
* accessing the rootDSE or so), use the frontendDB
* rules; might need work */
op->o_bd = frontendDB;
}
if ( !dryrun ) {
ID id;
if ( be == NULL ) {
fprintf( stderr, "%s: no target database "
"has been found for baseDN=\"%s\"; "
"you may try with \"-u\" (dry run).\n",
baseDN.bv_val, progname );
rc = 1;
goto destroy;
}
if ( !be->be_entry_open ||
!be->be_entry_close ||
!be->be_dn2id_get ||
!be->be_entry_get )
{
fprintf( stderr, "%s: target database "
"doesn't support necessary operations; "
"you may try with \"-u\" (dry run).\n",
progname );
rc = 1;
goto destroy;
}
if ( be->be_entry_open( be, 0 ) != 0 ) {
fprintf( stderr, "%s: could not open database.\n",
progname );
rc = 1;
goto destroy;
}
doclose = 1;
id = be->be_dn2id_get( be, &e.e_nname );
if ( id == NOID ) {
fprintf( stderr, "%s: unable to fetch ID of DN \"%s\"\n",
progname, e.e_nname.bv_val );
rc = 1;
goto destroy;
}
ep = be->be_entry_get( be, id );
if ( ep == NULL ) {
fprintf( stderr, "%s: unable to fetch entry \"%s\" (%lu)\n",
progname, e.e_nname.bv_val, id );
rc = 1;
goto destroy;
}
if ( argc == 0 ) {
Attribute *a;
(void)print_access( op, ep, slap_schema.si_ad_entry, NULL, NULL );
(void)print_access( op, ep, slap_schema.si_ad_children, NULL, NULL );
for ( a = ep->e_attrs; a; a = a->a_next ) {
int i;
for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
(void)print_access( op, ep, a->a_desc,
&a->a_vals[ i ],
&a->a_nvals[ i ] );
}
}
}
}
for ( ; argc--; argv++ ) {
slap_mask_t mask;
AttributeDescription *desc = NULL;
struct berval val = BER_BVNULL,
*valp = NULL;
const char *text;
char accessmaskbuf[ACCESSMASK_MAXLEN];
char *accessstr;
slap_access_t access = ACL_AUTH;
if ( attr == NULL ) {
attr = argv[ 0 ];
}
val.bv_val = strchr( attr, ':' );
if ( val.bv_val != NULL ) {
val.bv_val[0] = '\0';
val.bv_val++;
val.bv_len = strlen( val.bv_val );
valp = &val;
}
accessstr = strchr( attr, '/' );
if ( accessstr != NULL ) {
int invalid = 0;
accessstr[0] = '\0';
accessstr++;
access = str2access( accessstr );
switch ( access ) {
case ACL_INVALID_ACCESS:
fprintf( stderr, "unknown access \"%s\" for attribute \"%s\"\n",
accessstr, attr );
invalid = 1;
break;
case ACL_NONE:
fprintf( stderr, "\"none\" not allowed for attribute \"%s\"\n",
attr );
invalid = 1;
break;
default:
break;
}
if ( invalid ) {
if ( continuemode ) {
continue;
}
break;
}
}
rc = slap_str2ad( attr, &desc, &text );
if ( rc != LDAP_SUCCESS ) {
fprintf( stderr, "slap_str2ad(%s) failed %d (%s)\n",
attr, rc, ldap_err2string( rc ) );
if ( continuemode ) {
continue;
}
break;
}
rc = access_allowed_mask( op, ep, desc, valp, access,
NULL, &mask );
if ( accessstr ) {
fprintf( stderr, "%s access to %s%s%s: %s\n",
accessstr,
desc->ad_cname.bv_val,
val.bv_val ? "=" : "",
val.bv_val ? val.bv_val : "",
rc ? "ALLOWED" : "DENIED" );
} else {
fprintf( stderr, "%s%s%s: %s\n",
desc->ad_cname.bv_val,
val.bv_val ? "=" : "",
val.bv_val ? val.bv_val : "",
accessmask2str( mask, accessmaskbuf, 1 ) );
}
rc = 0;
attr = NULL;
}
destroy:;
if ( !BER_BVISNULL( &e.e_name ) ) {
ber_memfree( e.e_name.bv_val );
}
if ( !BER_BVISNULL( &e.e_nname ) ) {
ber_memfree( e.e_nname.bv_val );
}
if ( !dryrun && be ) {
if ( ep && ep != &e ) {
be_entry_release_r( op, ep );
}
if ( doclose ) {
be->be_entry_close( be );
}
LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
if ( bd != be ) {
backend_shutdown( bd );
}
}
}
if ( slap_tool_destroy())
rc = EXIT_FAILURE;
return rc;
}
|