]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
Add support for unsolicited notifications.
[openldap] / servers / slapd / back-ldap / bind.c
1 /* bind.c - ldap backend bind function */
2
3 /*
4  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
5  * 
6  * Permission is granted to anyone to use this software for any purpose
7  * on any computer system, and to alter it and redistribute it, subject
8  * to the following restrictions:
9  * 
10  * 1. The author is not responsible for the consequences of use of this
11  *    software, no matter how awful, even if they arise from flaws in it.
12  * 
13  * 2. The origin of this software must not be misrepresented, either by
14  *    explicit claim or by omission.  Since few users ever read sources,
15  *    credits should appear in the documentation.
16  * 
17  * 3. Altered versions must be plainly marked as such, and must not be
18  *    misrepresented as being the original software.  Since few users
19  *    ever read sources, credits should appear in the documentation.
20  * 
21  * 4. This notice may not be removed or altered.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/socket.h>
29 #include <ac/string.h>
30
31 #include "slap.h"
32 #include "back-ldap.h"
33
34 int
35 ldap_back_bind(
36     Backend             *be,
37     Connection          *conn,
38     Operation           *op,
39     char                *dn,
40     int                 method,
41         char            *mech,
42     struct berval       *cred,
43         char            **edn
44 )
45 {
46         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
47         struct ldapconn *lc;
48
49         *edn = NULL;
50
51         lc = ldap_back_getconn(li, conn, op);
52         if (!lc)
53                 return( -1 );
54
55         if (ldap_bind_s(lc->ld, dn, cred->bv_val, method) != LDAP_SUCCESS)
56                 return( ldap_back_op_result(lc, op) );
57
58         lc->bound = 1;
59         return( 0 );
60 }
61
62 struct ldapconn *
63 ldap_back_getconn(struct ldapinfo *li, Connection *conn, Operation *op)
64 {
65         struct ldapconn *lc;
66         LDAP *ld;
67
68         ldap_pvt_thread_mutex_lock( &li->conn_mutex );
69         for (lc = li->lcs; lc; lc=lc->next)
70                 if (lc->conn == conn)
71                         break;
72         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
73
74         /* Looks like we didn't get a bind. Open a new session... */
75         if (!lc) {
76                 ld = ldap_init(li->host, li->port);
77                 if (!ld) {
78                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
79                         "ldap_init failed" );
80                         return( NULL );
81                 }
82                 lc = (struct ldapconn *)ch_malloc(sizeof(struct ldapconn));
83                 lc->conn = conn;
84                 lc->ld = ld;
85                 lc->bound = 0;
86                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
87                 lc->next = li->lcs;
88                 li->lcs = lc;
89                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
90         }
91         return( lc );
92 }
93
94 ldap_back_dobind(struct ldapconn *lc, Operation *op)
95 {
96         if (lc->bound)
97                 return;
98
99         if (ldap_bind_s(lc->ld, lc->conn->c_cdn, NULL, LDAP_AUTH_SIMPLE) !=
100                 LDAP_SUCCESS)
101                 ldap_back_op_result(lc, op);
102         else
103                 lc->bound = 1;
104 }
105
106 ldap_back_op_result(struct ldapconn *lc, Operation *op)
107 {
108         int err;
109         char *msg;
110         char *match;
111
112         ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER, &err);
113         ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &msg);
114         ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
115         send_ldap_result( lc->conn, op, err, match, msg);
116         free(match);
117         free(msg);
118         return( (err==LDAP_SUCCESS) ? 0 : -1 );
119 }