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