]> git.sur5r.net Git - openldap/blob - servers/slapd/slapacl.c
Update copyright notices
[openldap] / servers / slapd / slapacl.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2004-2009 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         op->o_tmpmemctx = NULL;
102
103         conn.c_listener = &listener;
104         conn.c_listener_url = listener_url;
105         conn.c_peer_domain = peer_domain;
106         conn.c_peer_name = peer_name;
107         conn.c_sock_name = sock_name;
108         op->o_ssf = ssf;
109         op->o_transport_ssf = transport_ssf;
110         op->o_tls_ssf = tls_ssf;
111         op->o_sasl_ssf = sasl_ssf;
112
113         if ( !BER_BVISNULL( &authcID ) ) {
114                 if ( !BER_BVISNULL( &authcDN ) ) {
115                         fprintf( stderr, "both authcID=\"%s\" "
116                                         "and authcDN=\"%s\" provided\n",
117                                         authcID.bv_val, authcDN.bv_val );
118                         rc = 1;
119                         goto destroy;
120                 }
121
122                 rc = slap_sasl_getdn( &conn, op, &authcID, NULL,
123                                 &authcDN, SLAP_GETDN_AUTHCID );
124                 if ( rc != LDAP_SUCCESS ) {
125                         fprintf( stderr, "authcID: <%s> check failed %d (%s)\n",
126                                         authcID.bv_val, rc,
127                                         ldap_err2string( rc ) );
128                         rc = 1;
129                         goto destroy;
130                 }
131
132         } else if ( !BER_BVISNULL( &authcDN ) ) {
133                 struct berval   ndn;
134
135                 rc = dnNormalize( 0, NULL, NULL, &authcDN, &ndn, NULL );
136                 if ( rc != LDAP_SUCCESS ) {
137                         fprintf( stderr, "autchDN=\"%s\" normalization failed %d (%s)\n",
138                                         authcDN.bv_val, rc,
139                                         ldap_err2string( rc ) );
140                         rc = 1;
141                         goto destroy;
142                 }
143                 ch_free( authcDN.bv_val );
144                 authcDN = ndn;
145         }
146
147         if ( !BER_BVISNULL( &authzID ) ) {
148                 if ( !BER_BVISNULL( &authzDN ) ) {
149                         fprintf( stderr, "both authzID=\"%s\" "
150                                         "and authzDN=\"%s\" provided\n",
151                                         authzID.bv_val, authzDN.bv_val );
152                         rc = 1;
153                         goto destroy;
154                 }
155
156                 rc = slap_sasl_getdn( &conn, op, &authzID, NULL,
157                                 &authzDN, SLAP_GETDN_AUTHZID );
158                 if ( rc != LDAP_SUCCESS ) {
159                         fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
160                                         authzID.bv_val, rc,
161                                         ldap_err2string( rc ) );
162                         rc = 1;
163                         goto destroy;
164                 }
165
166         } else if ( !BER_BVISNULL( &authzDN ) ) {
167                 struct berval   ndn;
168
169                 rc = dnNormalize( 0, NULL, NULL, &authzDN, &ndn, NULL );
170                 if ( rc != LDAP_SUCCESS ) {
171                         fprintf( stderr, "autchDN=\"%s\" normalization failed %d (%s)\n",
172                                         authzDN.bv_val, rc,
173                                         ldap_err2string( rc ) );
174                         rc = 1;
175                         goto destroy;
176                 }
177                 ch_free( authzDN.bv_val );
178                 authzDN = ndn;
179         }
180
181
182         if ( !BER_BVISNULL( &authcDN ) ) {
183                 fprintf( stderr, "authcDN: \"%s\"\n", authcDN.bv_val );
184         }
185
186         if ( !BER_BVISNULL( &authzDN ) ) {
187                 fprintf( stderr, "authzDN: \"%s\"\n", authzDN.bv_val );
188         }
189
190         if ( !BER_BVISNULL( &authzDN ) ) {
191                 op->o_dn = authzDN;
192                 op->o_ndn = authzDN;
193                 
194                 if ( !BER_BVISNULL( &authcDN ) ) {
195                         op->o_conn->c_dn = authcDN;
196                         op->o_conn->c_ndn = authcDN;
197
198                 } else {
199                         op->o_conn->c_dn = authzDN;
200                         op->o_conn->c_ndn = authzDN;
201                 }
202
203         } else if ( !BER_BVISNULL( &authcDN ) ) {
204                 op->o_conn->c_dn = authcDN;
205                 op->o_conn->c_ndn = authcDN;
206                 op->o_dn = authcDN;
207                 op->o_ndn = authcDN;
208         }
209
210         assert( !BER_BVISNULL( &baseDN ) );
211         rc = dnPrettyNormal( NULL, &baseDN, &e.e_name, &e.e_nname, NULL );
212         if ( rc != LDAP_SUCCESS ) {
213                 fprintf( stderr, "base=\"%s\" normalization failed %d (%s)\n",
214                                 baseDN.bv_val, rc,
215                                 ldap_err2string( rc ) );
216                 rc = 1;
217                 goto destroy;
218         }
219
220         op->o_bd = be;
221         if ( op->o_bd == NULL ) {
222                 /* NOTE: if no database could be found (e.g. because
223                  * accessing the rootDSE or so), use the frontendDB
224                  * rules; might need work */
225                 op->o_bd = frontendDB;
226         }
227
228         if ( !dryrun ) {
229                 ID      id;
230
231                 if ( be == NULL ) {
232                         fprintf( stderr, "%s: no target database "
233                                 "has been found for baseDN=\"%s\"; "
234                                 "you may try with \"-u\" (dry run).\n",
235                                 baseDN.bv_val, progname );
236                         rc = 1;
237                         goto destroy;
238                 }
239
240                 if ( !be->be_entry_open ||
241                         !be->be_entry_close ||
242                         !be->be_dn2id_get ||
243                         !be->be_entry_get )
244                 {
245                         fprintf( stderr, "%s: target database "
246                                 "doesn't support necessary operations; "
247                                 "you may try with \"-u\" (dry run).\n",
248                                 progname );
249                         rc = 1;
250                         goto destroy;
251                 }
252
253                 if ( be->be_entry_open( be, 0 ) != 0 ) {
254                         fprintf( stderr, "%s: could not open database.\n",
255                                 progname );
256                         rc = 1;
257                         goto destroy;
258                 }
259
260                 doclose = 1;
261
262                 id = be->be_dn2id_get( be, &e.e_nname );
263                 if ( id == NOID ) {
264                         fprintf( stderr, "%s: unable to fetch ID of DN \"%s\"\n",
265                                 progname, e.e_nname.bv_val );
266                         rc = 1;
267                         goto destroy;
268                 }
269                 ep = be->be_entry_get( be, id );
270                 if ( ep == NULL ) {
271                         fprintf( stderr, "%s: unable to fetch entry \"%s\" (%lu)\n",
272                                 progname, e.e_nname.bv_val, id );
273                         rc = 1;
274                         goto destroy;
275
276                 }
277
278                 if ( argc == 0 ) {
279                         Attribute       *a;
280
281                         (void)print_access( op, ep, slap_schema.si_ad_entry, NULL, NULL );
282                         (void)print_access( op, ep, slap_schema.si_ad_children, NULL, NULL );
283
284                         for ( a = ep->e_attrs; a; a = a->a_next ) {
285                                 int     i;
286
287                                 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
288                                         (void)print_access( op, ep, a->a_desc,
289                                                         &a->a_vals[ i ],
290                                                         &a->a_nvals[ i ] );
291                                 }
292                         }
293                 }
294         }
295
296         for ( ; argc--; argv++ ) {
297                 slap_mask_t             mask;
298                 AttributeDescription    *desc = NULL;
299                 struct berval           val = BER_BVNULL,
300                                         *valp = NULL;
301                 const char              *text;
302                 char                    accessmaskbuf[ACCESSMASK_MAXLEN];
303                 char                    *accessstr;
304                 slap_access_t           access = ACL_AUTH;
305
306                 if ( attr == NULL ) {
307                         attr = argv[ 0 ];
308                 }
309
310                 val.bv_val = strchr( attr, ':' );
311                 if ( val.bv_val != NULL ) {
312                         val.bv_val[0] = '\0';
313                         val.bv_val++;
314                         val.bv_len = strlen( val.bv_val );
315                         valp = &val;
316                 }
317
318                 accessstr = strchr( attr, '/' );
319                 if ( accessstr != NULL ) {
320                         int     invalid = 0;
321
322                         accessstr[0] = '\0';
323                         accessstr++;
324                         access = str2access( accessstr );
325                         switch ( access ) {
326                         case ACL_INVALID_ACCESS:
327                                 fprintf( stderr, "unknown access \"%s\" for attribute \"%s\"\n",
328                                                 accessstr, attr );
329                                 invalid = 1;
330                                 break;
331
332                         case ACL_NONE:
333                                 fprintf( stderr, "\"none\" not allowed for attribute \"%s\"\n",
334                                                 attr );
335                                 invalid = 1;
336                                 break;
337
338                         default:
339                                 break;
340                         }
341
342                         if ( invalid ) {
343                                 if ( continuemode ) {
344                                         continue;
345                                 }
346                                 break;
347                         }
348                 }
349
350                 rc = slap_str2ad( attr, &desc, &text );
351                 if ( rc != LDAP_SUCCESS ) {
352                         fprintf( stderr, "slap_str2ad(%s) failed %d (%s)\n",
353                                         attr, rc, ldap_err2string( rc ) );
354                         if ( continuemode ) {
355                                 continue;
356                         }
357                         break;
358                 }
359
360                 rc = access_allowed_mask( op, ep, desc, valp, access,
361                                 NULL, &mask );
362
363                 if ( accessstr ) {
364                         fprintf( stderr, "%s access to %s%s%s: %s\n",
365                                         accessstr,
366                                         desc->ad_cname.bv_val,
367                                         val.bv_val ? "=" : "",
368                                         val.bv_val ? val.bv_val : "",
369                                         rc ? "ALLOWED" : "DENIED" );
370
371                 } else {
372                         fprintf( stderr, "%s%s%s: %s\n",
373                                         desc->ad_cname.bv_val,
374                                         val.bv_val ? "=" : "",
375                                         val.bv_val ? val.bv_val : "",
376                                         accessmask2str( mask, accessmaskbuf, 1 ) );
377                 }
378                 rc = 0;
379                 attr = NULL;
380         }
381
382 destroy:;
383         if ( !BER_BVISNULL( &e.e_name ) ) {
384                 ber_memfree( e.e_name.bv_val );
385         }
386         if ( !BER_BVISNULL( &e.e_nname ) ) {
387                 ber_memfree( e.e_nname.bv_val );
388         }
389         if ( !dryrun && be ) {
390                 if ( ep && ep != &e ) {
391                         be_entry_release_r( op, ep );
392                 }
393                 if ( doclose ) {
394                         be->be_entry_close( be );
395                 }
396
397                 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
398                         if ( bd != be ) {
399                                 backend_shutdown( bd );
400                         }
401                 }
402         }
403
404         if ( slap_tool_destroy())
405                 rc = EXIT_FAILURE;
406
407         return rc;
408 }
409