]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/bind.c
Extend value_match to extract an asserted value from a full value
[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 void backsql_init_search(backsql_srch_info *bsi,backsql_info *bi,char *nbase,int scope,
23                  int slimit,int tlimit,time_t stoptime,Filter *filter,
24                  SQLHDBC dbh,BackendDB *be,Connection *conn,Operation *op,char **attrs);
25
26 int backsql_bind(BackendDB *be,Connection *conn,Operation *op,
27         const char *dn,const char *ndn,int method,struct berval *cred,char** edn)
28 {
29  backsql_info *bi=(backsql_info*)be->be_private;
30  backsql_entryID user_id,*res;
31  SQLHDBC dbh;
32  AttributeDescription *password = slap_schema.si_ad_userPassword;
33  Entry          *e,user_entry;
34  Attribute      *a;
35  backsql_srch_info bsi;
36  
37  Debug(LDAP_DEBUG_TRACE,"==>backsql_bind()\n",0,0,0);
38  
39  if ( be_isroot_pw( be, conn, ndn, cred ) )
40     {
41      *edn=ch_strdup(be_root_dn(be));
42      Debug(LDAP_DEBUG_TRACE,"<==backsql_bind() root bind\n",0,0,0);
43      return LDAP_SUCCESS;
44     }
45  
46  *edn=ch_strdup(ndn);
47  
48  if (method == LDAP_AUTH_SIMPLE)
49   {      
50    dbh=backsql_get_db_conn(be,conn);
51
52    if (!dbh)
53     {
54      Debug(LDAP_DEBUG_TRACE,"backsql_bind(): could not get connection handle - exiting\n",0,0,0);
55      send_ldap_result(conn,op,LDAP_OTHER,"","SQL-backend error",NULL,NULL);
56      return 1;
57     }
58   
59    res=backsql_dn2id(bi,&user_id,dbh,ndn);
60    if (res==NULL)
61     {
62      Debug(LDAP_DEBUG_TRACE,"backsql_bind(): could not retrieve bind dn id - no such entry\n",0,0,0);
63      send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,NULL, NULL, NULL, NULL );
64      return 1;
65     }
66     
67    backsql_init_search(&bsi,bi,(char*)ndn,LDAP_SCOPE_BASE,-1,-1,-1,NULL,dbh,
68                  be,conn,op,NULL);
69    e=backsql_id2entry(&bsi,&user_entry,&user_id);
70    if (e==NULL)
71     {
72      Debug(LDAP_DEBUG_TRACE,"backsql_bind(): error in backsql_id2entry() - auth failed\n",0,0,0);
73      send_ldap_result( conn, op, LDAP_OTHER,NULL, NULL, NULL, NULL );
74      return 1;
75     }
76     
77    if ( ! access_allowed( be, conn, op, e,password, NULL, ACL_AUTH ) )
78     {
79      send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS, NULL, NULL, NULL, NULL );
80      return 1;
81     }
82
83    if ( (a = attr_find( e->e_attrs, password )) == NULL )
84     {
85      send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH, NULL, NULL, NULL, NULL );
86      return 1;
87     }
88
89    if ( slap_passwd_check( conn, a, cred ) != 0 ) 
90     {
91      send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,NULL, NULL, NULL, NULL );
92      return 1;
93     }
94   }  
95  else /*method != SIMPLE */
96   {
97    send_ldap_result( conn, op, LDAP_STRONG_AUTH_NOT_SUPPORTED,
98                     NULL, "authentication method not supported", NULL, NULL );
99    return 1;
100   }
101  Debug(LDAP_DEBUG_TRACE,"<==backsql_bind()\n",0,0,0);
102  return 0;
103 }
104  
105 int backsql_unbind(BackendDB *be,Connection *conn,Operation *op)
106 {
107  Debug(LDAP_DEBUG_TRACE,"==>backsql_unbind()\n",0,0,0);
108  send_ldap_result(conn,op,LDAP_SUCCESS,NULL,NULL,NULL,0);
109  Debug(LDAP_DEBUG_TRACE,"<==backsql_unbind()\n",0,0,0);
110  return 0;
111 }
112
113 #endif /* SLAPD_SQL */