]> git.sur5r.net Git - openldap/blob - servers/slapd/slapacl.c
6caf938af5dfba40fcd37aaae910a47a5fe46b5f
[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         assert( be != NULL );
60
61         if ( !BER_BVISNULL( &authcID ) ) {
62                 rc = slap_sasl_getdn( &conn, &op, &authcID, NULL, &authcDN, SLAP_GETDN_AUTHCID );
63                 if ( rc != LDAP_SUCCESS ) {
64                         fprintf( stderr, "ID: <%s> check failed %d (%s)\n",
65                                         authcID.bv_val, rc,
66                                         ldap_err2string( rc ) );
67                         rc = 1;
68                         goto destroy;
69                 }
70         }
71
72         if ( !BER_BVISNULL( &authcDN ) ) {
73                 fprintf( stderr, "DN: \"%s\"\n", authcDN.bv_val );
74         }
75
76         assert( !BER_BVISNULL( &baseDN ) );
77         rc = dnPrettyNormal( NULL, &baseDN, &e.e_name, &e.e_nname, NULL );
78         if ( rc != LDAP_SUCCESS ) {
79                 fprintf( stderr, "base=\"%s\" normalization failed %d (%s)\n",
80                                 baseDN.bv_val, rc,
81                                 ldap_err2string( rc ) );
82                 rc = 1;
83                 goto destroy;
84         }
85
86         op.o_bd = be;
87         if ( !BER_BVISNULL( &authcDN ) ) {
88                 op.o_dn = authcDN;
89                 op.o_ndn = authcDN;
90         }
91
92         for ( ; argc--; argv++ ) {
93                 slap_mask_t             mask;
94                 AttributeDescription    *desc = NULL;
95                 int                     rc;
96                 struct berval           val;
97                 const char              *text;
98                 char                    accessmaskbuf[ACCESSMASK_MAXLEN];
99                 char                    *accessstr;
100                 slap_access_t           access = ACL_AUTH;
101
102                 val.bv_val = strchr( argv[0], ':' );
103                 if ( val.bv_val != NULL ) {
104                         val.bv_val[0] = '\0';
105                         val.bv_val++;
106                         val.bv_len = strlen( val.bv_val );
107                 }
108
109                 accessstr = strchr( argv[0], '/' );
110                 if ( accessstr != NULL ) {
111                         accessstr[0] = '\0';
112                         accessstr++;
113                         access = str2access( accessstr );
114                         if ( access == ACL_INVALID_ACCESS ) {
115                                 fprintf( stderr, "unknown access \"%s\" for attribute \"%s\"\n",
116                                                 accessstr, argv[0] );
117                                 continue;
118                         }
119                 }
120
121                 rc = slap_str2ad( argv[0], &desc, &text );
122                 if ( rc != LDAP_SUCCESS ) {
123                         fprintf( stderr, "slap_str2ad(%s) failed %d (%s)\n",
124                                         argv[0], rc, ldap_err2string( rc ) );
125                         continue;
126                 }
127
128                 rc = access_allowed_mask( &op, &e, desc, &val, access,
129                                 NULL, &mask );
130
131                 fprintf( stderr, "%s%s%s: %s\n",
132                                 desc->ad_cname.bv_val,
133                                 val.bv_val ? "=" : "",
134                                 val.bv_val ? val.bv_val : "",
135                                 accessmask2str( mask, accessmaskbuf ) );
136         }
137
138 destroy:;
139         slap_tool_destroy();
140
141         return rc;
142 }
143