]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
ber_scanf() returns unsigned long, not int
[openldap] / servers / slapd / bind.c
1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
2
3 /*
4  * Copyright (c) 1995 Regents of the University of Michigan.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms are permitted
8  * provided that this notice is preserved and that due credit is given
9  * to the University of Michigan at Ann Arbor. The name of the University
10  * may not be used to endorse or promote products derived from this
11  * software without specific prior written permission. This software
12  * is provided ``as is'' without express or implied warranty.
13  */
14
15 #include "portable.h"
16
17 #include <stdio.h>
18
19 #include <ac/string.h>
20 #include <ac/socket.h>
21
22 #include "slap.h"
23
24 extern Backend  *select_backend();
25 extern char     *suffixAlias();
26
27 extern char     *default_referral;
28
29 void
30 do_bind(
31     Connection  *conn,
32     Operation   *op
33 )
34 {
35         BerElement      *ber = op->o_ber;
36         int             version, method, len;
37         unsigned long   rc;
38         char            *dn;
39         struct berval   cred;
40         Backend         *be;
41
42         Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
43
44         /*
45          * Parse the bind request.  It looks like this:
46          *
47          *      BindRequest ::= SEQUENCE {
48          *              version         INTEGER,                 -- version
49          *              name            DistinguishedName,       -- dn
50          *              authentication  CHOICE {
51          *                      simple          [0] OCTET STRING -- passwd
52          *                      krbv42ldap      [1] OCTET STRING
53          *                      krbv42dsa       [1] OCTET STRING
54          *              }
55          *      }
56          */
57
58 #ifdef LDAP_COMPAT30
59         /*
60          * in version 3.0 there is an extra SEQUENCE tag after the
61          * BindRequest SEQUENCE tag.
62          */
63
64         {
65         BerElement      tber;
66         unsigned long   tlen, ttag;
67
68         tber = *op->o_ber;
69         ttag = ber_skip_tag( &tber, &tlen );
70         if ( ber_peek_tag( &tber, &tlen ) == LBER_SEQUENCE ) {
71                 Debug( LDAP_DEBUG_ANY, "version 3.0 detected\n", 0, 0, 0 );
72                 conn->c_version = 30;
73                 rc = ber_scanf(ber, "{{iato}}", &version, &dn, &method, &cred);
74         } else {
75                 rc = ber_scanf( ber, "{iato}", &version, &dn, &method, &cred );
76         }
77         }
78 #else
79         rc = ber_scanf( ber, "{iato}", &version, &dn, &method, &cred );
80 #endif
81         if ( rc == LBER_ERROR ) {
82                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
83                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
84                     "decoding error" );
85                 return;
86         }
87 #ifdef LDAP_COMPAT30
88         if ( conn->c_version == 30 ) {
89                 switch ( method ) {
90                 case LDAP_AUTH_SIMPLE_30:
91                         method = LDAP_AUTH_SIMPLE;
92                         break;
93 #ifdef HAVE_KERBEROS
94                 case LDAP_AUTH_KRBV41_30:
95                         method = LDAP_AUTH_KRBV41;
96                         break;
97                 case LDAP_AUTH_KRBV42_30:
98                         method = LDAP_AUTH_KRBV42;
99                         break;
100 #endif
101                 }
102         }
103 #endif /* compat30 */
104         dn_normalize( dn );
105
106         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d BIND dn=\"%s\" method=%d\n",
107             conn->c_connid, op->o_opid, dn, method, 0 );
108
109         if ( version != LDAP_VERSION2 ) {
110                 if ( dn != NULL ) {
111                         free( dn );
112                 }
113                 if ( cred.bv_val != NULL ) {
114                         free( cred.bv_val );
115                 }
116
117                 Debug( LDAP_DEBUG_ANY, "unknown version %d\n", version, 0, 0 );
118                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
119                     "version not supported" );
120                 return;
121         }
122
123         Debug( LDAP_DEBUG_TRACE, "do_bind: version %d dn (%s) method %d\n",
124             version, dn, method );
125
126         /* accept null binds */
127         if ( dn == NULL || *dn == '\0' ) {
128                 if ( dn != NULL ) {
129                         free( dn );
130                 }
131                 if ( cred.bv_val != NULL ) {
132                         free( cred.bv_val );
133                 }
134
135                 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
136                 return;
137         }
138
139         /*
140          * We could be serving multiple database backends.  Select the
141          * appropriate one, or send a referral to our "referral server"
142          * if we don't hold it.
143          */
144
145         if ( (be = select_backend( dn )) == NULL ) {
146                 free( dn );
147                 if ( cred.bv_val != NULL ) {
148                         free( cred.bv_val );
149                 }
150                 if ( cred.bv_len == 0 ) {
151                         send_ldap_result( conn, op, LDAP_SUCCESS,
152                                 NULL, NULL );
153                 } else if ( default_referral && *default_referral ) {
154                         send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS,
155                                 NULL, default_referral );
156                 } else {
157                         send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
158                                 NULL, default_referral );
159                 }
160                 return;
161         }
162
163         /* alias suffix */
164         dn = suffixAlias ( dn, op, be );
165
166         if ( be->be_bind != NULL ) {
167                 if ( (*be->be_bind)( be, conn, op, dn, method, &cred ) == 0 ) {
168                         pthread_mutex_lock( &conn->c_dnmutex );
169                         if ( conn->c_dn != NULL ) {
170                                 free( conn->c_dn );
171                         }
172                         conn->c_dn = strdup( dn );
173                         pthread_mutex_unlock( &conn->c_dnmutex );
174
175                         /* send this here to avoid a race condition */
176                         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
177                 }
178         } else {
179                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
180                     "Function not implemented" );
181         }
182
183         free( dn );
184         if ( cred.bv_val != NULL ) {
185                 free( cred.bv_val );
186         }
187 }