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