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