]> git.sur5r.net Git - openldap/blob - libraries/libldap/bind.c
336c229f0f612ab6cbb8d790fd7715d38b387e61
[openldap] / libraries / libldap / bind.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*  Portions
6  *  Copyright (c) 1990 Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  bind.c
10  */
11
12 #include "portable.h"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 #include <ac/socket.h>
18 #include <ac/string.h>
19 #include <ac/time.h>
20
21 #include "ldap-int.h"
22
23
24 /*
25  * ldap_bind - bind to the ldap server (and X.500).  The dn and password
26  * of the entry to which to bind are supplied, along with the authentication
27  * method to use.  The msgid of the bind request is returned on success,
28  * -1 if there's trouble.  Note, the kerberos support assumes the user already
29  * has a valid tgt for now.  ldap_result() should be called to find out the
30  * outcome of the bind request.
31  *
32  * Example:
33  *      ldap_bind( ld, "cn=manager, o=university of michigan, c=us", "secret",
34  *          LDAP_AUTH_SIMPLE )
35  */
36
37 int
38 ldap_bind( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd, int authmethod )
39 {
40         /*
41          * The bind request looks like this:
42          *      BindRequest ::= SEQUENCE {
43          *              version         INTEGER,
44          *              name            DistinguishedName,       -- who
45          *              authentication  CHOICE {
46          *                      simple          [0] OCTET STRING -- passwd
47 #ifdef HAVE_KERBEROS
48          *                      krbv42ldap      [1] OCTET STRING
49          *                      krbv42dsa       [2] OCTET STRING
50 #endif
51          *              }
52          *      }
53          * all wrapped up in an LDAPMessage sequence.
54          */
55
56         Debug( LDAP_DEBUG_TRACE, "ldap_bind\n", 0, 0, 0 );
57
58         switch ( authmethod ) {
59         case LDAP_AUTH_SIMPLE:
60                 return( ldap_simple_bind( ld, dn, passwd ) );
61
62 #ifdef HAVE_KERBEROS
63         case LDAP_AUTH_KRBV41:
64                 return( ldap_kerberos_bind1( ld, dn ) );
65
66         case LDAP_AUTH_KRBV42:
67                 return( ldap_kerberos_bind2( ld, dn ) );
68 #endif
69
70         default:
71                 ld->ld_errno = LDAP_AUTH_UNKNOWN;
72                 return( -1 );
73         }
74 }
75
76 /*
77  * ldap_bind_s - bind to the ldap server (and X.500).  The dn and password
78  * of the entry to which to bind are supplied, along with the authentication
79  * method to use.  This routine just calls whichever bind routine is
80  * appropriate and returns the result of the bind (e.g. LDAP_SUCCESS or
81  * some other error indication).  Note, the kerberos support assumes the
82  * user already has a valid tgt for now.
83  *
84  * Examples:
85  *      ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
86  *          "secret", LDAP_AUTH_SIMPLE )
87  *      ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
88  *          NULL, LDAP_AUTH_KRBV4 )
89  */
90 int
91 ldap_bind_s(
92         LDAP *ld,
93         LDAP_CONST char *dn,
94         LDAP_CONST char *passwd,
95         int authmethod )
96 {
97         Debug( LDAP_DEBUG_TRACE, "ldap_bind_s\n", 0, 0, 0 );
98
99         switch ( authmethod ) {
100         case LDAP_AUTH_SIMPLE:
101                 return( ldap_simple_bind_s( ld, dn, passwd ) );
102
103 #ifdef HAVE_KERBEROS
104         case LDAP_AUTH_KRBV4:
105                 return( ldap_kerberos_bind_s( ld, dn ) );
106
107         case LDAP_AUTH_KRBV41:
108                 return( ldap_kerberos_bind1_s( ld, dn ) );
109
110         case LDAP_AUTH_KRBV42:
111                 return( ldap_kerberos_bind2_s( ld, dn ) );
112 #endif
113
114         default:
115                 return( ld->ld_errno = LDAP_AUTH_UNKNOWN );
116         }
117 }
118
119
120 void
121 ldap_set_rebind_proc( LDAP *ld, int (*rebindproc)( LDAP *ld, char **dnp,
122         char **passwdp, int *authmethodp, int freeit ))
123 {
124         ld->ld_rebindproc = rebindproc;
125 }