]> git.sur5r.net Git - openldap/blob - servers/slapd/slapauth.c
73b2ceb93da12d5e7509ce91d998fa06af863251
[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 = {0};
83         char                    opbuf[OPERATION_BUFFER_SIZE];
84         Operation               *op;
85
86         slap_tool_init( progname, SLAPAUTH, argc, argv );
87
88         argv = &argv[ optind ];
89         argc -= optind;
90
91         op = (Operation *)opbuf;
92         connection_fake_init( &conn, op, &conn );
93
94         if ( !BER_BVISNULL( &authzID ) ) {
95                 struct berval   authzdn;
96                 
97                 rc = slap_sasl_getdn( &conn, op, &authzID, NULL, &authzdn,
98                                 SLAP_GETDN_AUTHZID );
99                 if ( rc != LDAP_SUCCESS ) {
100                         fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
101                                         authzID.bv_val, rc,
102                                         ldap_err2string( rc ) );
103                         rc = 1;
104                         BER_BVZERO( &authzID );
105                         goto destroy;
106                 } 
107
108                 authzID = authzdn;
109         }
110
111
112         if ( !BER_BVISNULL( &authcID ) ) {
113                 if ( !BER_BVISNULL( &authzID ) || argc == 0 ) {
114                         rc = do_check( &conn, op, &authcID );
115                         goto destroy;
116                 }
117
118                 for ( ; argc--; argv++ ) {
119                         struct berval   authzdn;
120                 
121                         ber_str2bv( argv[ 0 ], 0, 0, &authzID );
122
123                         rc = slap_sasl_getdn( &conn, op, &authzID, NULL, &authzdn,
124                                         SLAP_GETDN_AUTHZID );
125                         if ( rc != LDAP_SUCCESS ) {
126                                 fprintf( stderr, "authzID: <%s> check failed %d (%s)\n",
127                                                 authzID.bv_val, rc,
128                                                 ldap_err2string( rc ) );
129                                 rc = -1;
130                                 BER_BVZERO( &authzID );
131                                 if ( !continuemode ) {
132                                         goto destroy;
133                                 }
134                         }
135
136                         authzID = authzdn;
137
138                         rc = do_check( &conn, op, &authcID );
139
140                         op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
141                         BER_BVZERO( &authzID );
142
143                         if ( rc && !continuemode ) {
144                                 goto destroy;
145                         }
146                 }
147
148                 goto destroy;
149         }
150
151         for ( ; argc--; argv++ ) {
152                 struct berval   id;
153
154                 ber_str2bv( argv[ 0 ], 0, 0, &id );
155
156                 rc = do_check( &conn, op, &id );
157
158                 if ( rc && !continuemode ) {
159                         goto destroy;
160                 }
161         }
162
163 destroy:;
164         if ( !BER_BVISNULL( &authzID ) ) {
165                 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
166         }
167         slap_tool_destroy();
168
169         return rc;
170 }
171