]> git.sur5r.net Git - openldap/blob - servers/slapd/slapacl.c
391bb624445670b698093e2156942aa4ae86eef7
[openldap] / servers / slapd / slapacl.c
1 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
2  *
3  * Copyright 2004 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 int
38 slapacl( int argc, char **argv )
39 {
40         int                     rc = EXIT_SUCCESS;
41         const char              *progname = "slapacl";
42         Connection              conn;
43         Operation               op;
44         Entry                   e = { 0 };
45         char                    *attr = NULL;
46
47         slap_tool_init( progname, SLAPACL, argc, argv );
48
49         argv = &argv[ optind ];
50         argc -= optind;
51
52         memset( &conn, 0, sizeof( Connection ) );
53         memset( &op, 0, sizeof( Operation ) );
54
55         connection_fake_init( &conn, &op, &conn );
56
57         if ( !BER_BVISNULL( &authcID ) ) {
58                 rc = slap_sasl_getdn( &conn, &op, &authcID, NULL,
59                                 &authcDN, SLAP_GETDN_AUTHCID );
60                 if ( rc != LDAP_SUCCESS ) {
61                         fprintf( stderr, "ID: <%s> check failed %d (%s)\n",
62                                         authcID.bv_val, rc,
63                                         ldap_err2string( rc ) );
64                         rc = 1;
65                         goto destroy;
66                 }
67
68         } else if ( !BER_BVISNULL( &authcDN ) ) {
69                 struct berval   ndn;
70
71                 rc = dnNormalize( 0, NULL, NULL, &authcDN, &ndn, NULL );
72                 if ( rc != LDAP_SUCCESS ) {
73                         fprintf( stderr, "autchDN=\"%s\" normalization failed %d (%s)\n",
74                                         authcDN.bv_val, rc,
75                                         ldap_err2string( rc ) );
76                         rc = 1;
77                         goto destroy;
78                 }
79                 ch_free( authcDN.bv_val );
80                 authcDN = ndn;
81         }
82
83
84         if ( !BER_BVISNULL( &authcDN ) ) {
85                 fprintf( stderr, "DN: \"%s\"\n", authcDN.bv_val );
86         }
87
88         assert( !BER_BVISNULL( &baseDN ) );
89         rc = dnPrettyNormal( NULL, &baseDN, &e.e_name, &e.e_nname, NULL );
90         if ( rc != LDAP_SUCCESS ) {
91                 fprintf( stderr, "base=\"%s\" normalization failed %d (%s)\n",
92                                 baseDN.bv_val, rc,
93                                 ldap_err2string( rc ) );
94                 rc = 1;
95                 goto destroy;
96         }
97
98         op.o_bd = be;
99         if ( !BER_BVISNULL( &authcDN ) ) {
100                 op.o_dn = authcDN;
101                 op.o_ndn = authcDN;
102         }
103
104         if ( argc == 0 ) {
105                 argc = 1;
106                 attr = slap_schema.si_ad_entry->ad_cname.bv_val;
107         }
108
109         for ( ; argc--; argv++ ) {
110                 slap_mask_t             mask;
111                 AttributeDescription    *desc = NULL;
112                 int                     rc;
113                 struct berval           val;
114                 const char              *text;
115                 char                    accessmaskbuf[ACCESSMASK_MAXLEN];
116                 char                    *accessstr;
117                 slap_access_t           access = ACL_AUTH;
118
119                 if ( attr == NULL ) {
120                         attr = argv[ 0 ];
121                 }
122
123                 val.bv_val = strchr( attr, ':' );
124                 if ( val.bv_val != NULL ) {
125                         val.bv_val[0] = '\0';
126                         val.bv_val++;
127                         val.bv_len = strlen( val.bv_val );
128                 }
129
130                 accessstr = strchr( attr, '/' );
131                 if ( accessstr != NULL ) {
132                         accessstr[0] = '\0';
133                         accessstr++;
134                         access = str2access( accessstr );
135                         if ( access == ACL_INVALID_ACCESS ) {
136                                 fprintf( stderr, "unknown access \"%s\" for attribute \"%s\"\n",
137                                                 accessstr, attr );
138                                 if ( continuemode ) {
139                                         continue;
140                                 }
141                                 break;
142                         }
143                 }
144
145                 rc = slap_str2ad( attr, &desc, &text );
146                 if ( rc != LDAP_SUCCESS ) {
147                         fprintf( stderr, "slap_str2ad(%s) failed %d (%s)\n",
148                                         attr, rc, ldap_err2string( rc ) );
149                         if ( continuemode ) {
150                                 continue;
151                         }
152                         break;
153                 }
154
155                 rc = access_allowed_mask( &op, &e, desc, &val, access,
156                                 NULL, &mask );
157
158                 if ( accessstr ) {
159                         fprintf( stderr, "%s access to %s%s%s: %s\n",
160                                         accessstr,
161                                         desc->ad_cname.bv_val,
162                                         val.bv_val ? "=" : "",
163                                         val.bv_val ? val.bv_val : "",
164                                         rc ? "ALLOWED" : "DENIED" );
165
166                 } else {
167                         fprintf( stderr, "%s%s%s: %s\n",
168                                         desc->ad_cname.bv_val,
169                                         val.bv_val ? "=" : "",
170                                         val.bv_val ? val.bv_val : "",
171                                         accessmask2str( mask, accessmaskbuf ) );
172                 }
173                 rc = 0;
174                 attr = NULL;
175         }
176
177 destroy:;
178         slap_tool_destroy();
179
180         return rc;
181 }
182