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