]> git.sur5r.net Git - openldap/blob - servers/slapd/slapauth.c
ITS#5860 - more for entry cache counts
[openldap] / servers / slapd / slapauth.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 do_check( Connection *c, Operation *op, struct berval *id )
40 {
41         struct berval   authcdn;
42         int             rc;
43
44         rc = slap_sasl_getdn( c, op, id, realm, &authcdn, SLAP_GETDN_AUTHCID );
45         if ( rc != LDAP_SUCCESS ) {
46                 fprintf( stderr, "ID: <%s> check failed %d (%s)\n",
47                                 id->bv_val, rc,
48                                 ldap_err2string( rc ) );
49                 rc = 1;
50                         
51         } else {
52                 if ( !BER_BVISNULL( &authzID ) ) {
53                         rc = slap_sasl_authorized( op, &authcdn, &authzID );
54
55                         fprintf( stderr,
56                                         "ID:      <%s>\n"
57                                         "authcDN: <%s>\n"
58                                         "authzDN: <%s>\n"
59                                         "authorization %s\n",
60                                         id->bv_val,
61                                         authcdn.bv_val,
62                                         authzID.bv_val,
63                                         rc == LDAP_SUCCESS ? "OK" : "failed" );
64
65                 } else {
66                         fprintf( stderr, "ID: <%s> check succeeded\n"
67                                         "authcID:     <%s>\n",
68                                         id->bv_val,
69                                         authcdn.bv_val );
70                         op->o_tmpfree( authcdn.bv_val, op->o_tmpmemctx );
71                 }
72                 rc = 0;
73         }
74
75         return rc;
76 }
77
78 int
79 slapauth( int argc, char **argv )
80 {
81         int                     rc = EXIT_SUCCESS;
82         const char              *progname = "slapauth";
83         Connection              conn = {0};
84         OperationBuffer opbuf;
85         Operation               *op;
86
87         slap_tool_init( progname, SLAPAUTH, argc, argv );
88
89         argv = &argv[ optind ];
90         argc -= optind;
91
92         connection_fake_init( &conn, &opbuf, &conn );
93         op = &opbuf.ob_op;
94
95         conn.c_sasl_bind_mech = mech;
96
97         if ( !BER_BVISNULL( &authzID ) ) {
98                 struct berval   authzdn;
99                 
100                 rc = slap_sasl_getdn( &conn, op, &authzID, NULL, &authzdn,
101                                 SLAP_GETDN_AUTHZID );
102                 if ( rc != LDAP_SUCCESS ) {
103                         fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
104                                         authzID.bv_val, rc,
105                                         ldap_err2string( rc ) );
106                         rc = 1;
107                         BER_BVZERO( &authzID );
108                         goto destroy;
109                 } 
110
111                 authzID = authzdn;
112         }
113
114
115         if ( !BER_BVISNULL( &authcID ) ) {
116                 if ( !BER_BVISNULL( &authzID ) || argc == 0 ) {
117                         rc = do_check( &conn, op, &authcID );
118                         goto destroy;
119                 }
120
121                 for ( ; argc--; argv++ ) {
122                         struct berval   authzdn;
123                 
124                         ber_str2bv( argv[ 0 ], 0, 0, &authzID );
125
126                         rc = slap_sasl_getdn( &conn, op, &authzID, NULL, &authzdn,
127                                         SLAP_GETDN_AUTHZID );
128                         if ( rc != LDAP_SUCCESS ) {
129                                 fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
130                                                 authzID.bv_val, rc,
131                                                 ldap_err2string( rc ) );
132                                 rc = -1;
133                                 BER_BVZERO( &authzID );
134                                 if ( !continuemode ) {
135                                         goto destroy;
136                                 }
137                         }
138
139                         authzID = authzdn;
140
141                         rc = do_check( &conn, op, &authcID );
142
143                         op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
144                         BER_BVZERO( &authzID );
145
146                         if ( rc && !continuemode ) {
147                                 goto destroy;
148                         }
149                 }
150
151                 goto destroy;
152         }
153
154         for ( ; argc--; argv++ ) {
155                 struct berval   id;
156
157                 ber_str2bv( argv[ 0 ], 0, 0, &id );
158
159                 rc = do_check( &conn, op, &id );
160
161                 if ( rc && !continuemode ) {
162                         goto destroy;
163                 }
164         }
165
166 destroy:;
167         if ( !BER_BVISNULL( &authzID ) ) {
168                 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
169         }
170         if ( slap_tool_destroy())
171                 rc = EXIT_FAILURE;
172
173         return rc;
174 }
175