]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/bind.c
First commit of Hallvard's backend documentation effort
[openldap] / servers / slapd / back-sql / bind.c
1 /*
2  *       Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
3  *
4  *       Redistribution and use in source and binary forms are permitted only
5  *       as authorized by the OpenLDAP Public License.  A copy of this
6  *       license is available at http://www.OpenLDAP.org/license.html or
7  *       in file LICENSE in the top-level directory of the distribution.
8  */
9
10 #include "portable.h"
11
12 #ifdef SLAPD_SQL
13
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include "slap.h"
17 #include "back-sql.h"
18 #include "sql-wrap.h"
19 #include "util.h"
20 #include "entry-id.h"
21
22 int backsql_bind(BackendDB *be,Connection *conn,Operation *op,
23         struct berval *dn,struct berval *ndn,int method,struct berval *cred,struct berval *edn)
24 {
25  backsql_info *bi=(backsql_info*)be->be_private;
26  backsql_entryID user_id,*res;
27  SQLHDBC dbh;
28  AttributeDescription *password = slap_schema.si_ad_userPassword;
29  Entry          *e,user_entry;
30  Attribute      *a;
31  backsql_srch_info bsi;
32  
33  Debug(LDAP_DEBUG_TRACE,"==>backsql_bind()\n",0,0,0);
34  
35  if ( be_isroot_pw( be, conn, ndn, cred ) )
36     {
37      ber_dupbv(edn, be_root_dn(be));
38      Debug(LDAP_DEBUG_TRACE,"<==backsql_bind() root bind\n",0,0,0);
39      return LDAP_SUCCESS;
40     }
41  
42  ber_dupbv(edn, ndn);
43  
44  if (method == LDAP_AUTH_SIMPLE)
45   {      
46    dbh=backsql_get_db_conn(be,conn);
47
48    if (!dbh)
49     {
50      Debug(LDAP_DEBUG_TRACE,"backsql_bind(): could not get connection handle - exiting\n",0,0,0);
51      send_ldap_result(conn,op,LDAP_OTHER,"","SQL-backend error",NULL,NULL);
52      return 1;
53     }
54   
55    res=backsql_dn2id(bi,&user_id,dbh,ndn->bv_val);
56    if (res==NULL)
57     {
58      Debug(LDAP_DEBUG_TRACE,"backsql_bind(): could not retrieve bind dn id - no such entry\n",0,0,0);
59      send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,NULL, NULL, NULL, NULL );
60      return 1;
61     }
62     
63    backsql_init_search(&bsi,bi,(char*)ndn->bv_val,LDAP_SCOPE_BASE,-1,-1,-1,NULL,dbh,
64                  be,conn,op,NULL);
65    e=backsql_id2entry(&bsi,&user_entry,&user_id);
66    if (e==NULL)
67     {
68      Debug(LDAP_DEBUG_TRACE,"backsql_bind(): error in backsql_id2entry() - auth failed\n",0,0,0);
69      send_ldap_result( conn, op, LDAP_OTHER,NULL, NULL, NULL, NULL );
70      return 1;
71     }
72     
73    if ( ! access_allowed( be, conn, op, e, password, NULL, ACL_AUTH, NULL ) )
74     {
75      send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS, NULL, NULL, NULL, NULL );
76      return 1;
77     }
78
79    if ( (a = attr_find( e->e_attrs, password )) == NULL )
80     {
81      send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH, NULL, NULL, NULL, NULL );
82      return 1;
83     }
84
85    if ( slap_passwd_check( conn, a, cred ) != 0 ) 
86     {
87      send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,NULL, NULL, NULL, NULL );
88      return 1;
89     }
90   }  
91  else /*method != SIMPLE */
92   {
93    send_ldap_result( conn, op, LDAP_STRONG_AUTH_NOT_SUPPORTED,
94                     NULL, "authentication method not supported", NULL, NULL );
95    return 1;
96   }
97  Debug(LDAP_DEBUG_TRACE,"<==backsql_bind()\n",0,0,0);
98  return 0;
99 }
100  
101 int backsql_unbind(BackendDB *be,Connection *conn,Operation *op)
102 {
103  Debug(LDAP_DEBUG_TRACE,"==>backsql_unbind()\n",0,0,0);
104  send_ldap_result(conn,op,LDAP_SUCCESS,NULL,NULL,NULL,0);
105  Debug(LDAP_DEBUG_TRACE,"<==backsql_unbind()\n",0,0,0);
106  return 0;
107 }
108
109 #endif /* SLAPD_SQL */