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