"referenced entity .* not found" message fixed to be fatal
[tac_plus.git] / ldap_author.c
1 /*
2      Verify that this user/password is valid per a database LDAP server
3      Return 1 if verified, 0 otherwise.
4
5      Format of connection string (look like internet URL):
6
7        ldap://LDAP-hostname
8
9      -------------------------------------------------------
10      patrick.harpes@tudor.lu            http://www.santel.lu
11                                         http://www.tudor.lu
12
13
14
15      Dependencies: You need to get the OpenLDAP libraries
16                    from http://www.openldap.org
17
18       License: tac_ldap is free software; you can redistribute it
19                and/or modify it under the terms of the GNU General Public License
20                as published by the Free Software Foundation; either version 2,
21                or (at your option) any later version.
22 --------------------------------------------------------------------------
23                                 Changes:
24  Ok i am back again..:)
25  I changed lot of thing.. First off all i add port feature to ldap string.
26  And also add more check for buffer overflows.
27
28 Connect format would be:
29        ldap://LDAP-hostname:100
30
31 Port name isn't required.. I would like to change format with :
32        ldap://LDAP-hostname:100/dn_for_user&dn_for_passwd
33
34  devrim seral <devrim@gazi.edu.tr>
35
36 */
37
38
39 #include "tac_plus.h"
40
41 #ifdef USE_LDAP
42
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <lber.h>
47 #include <ldap.h>
48 #include <ldap_cdefs.h>
49
50 #include "ldap_author.h"
51 #include "main.h"
52 #include "utils.h"
53 #include "report.h"
54
55
56 int ldap_verify TAC_ARGS((const char *user, const char *users_passwd, const char *str_conn));
57
58 int
59 ldap_verify(user, users_passwd, str_conn)
60 const char *user;               /* username ... */
61 const char *users_passwd;       /* ... and given password */
62 const char *str_conn;                   /* string connection to database */
63 {
64   char *buf;
65   char *ldapServer;
66   char *ldap_port;
67   LDAP *ld;
68   int port;
69   int err;
70
71 /* Don't allow null username and passwd */
72   if ( *user == '0' || *users_passwd == '0' ) return (1);
73
74   buf = (char *) tac_malloc(strlen(str_conn)+1);
75
76   strcpy(buf,str_conn);
77   ldapServer=strstr(buf, "://");
78
79   if(ldapServer == NULL && strlen(ldapServer) <4 ) {
80         if (debug) {
81                 report(LOG_DEBUG, "Error parse ldap server");
82                 return(1);
83         }
84   }
85
86  ldapServer=ldapServer+3;
87
88  ldap_port=(char *)strstr(ldapServer, ":");
89
90  if (ldap_port != NULL ) {
91                 *ldap_port='\0';
92                 port=atoi(++ldap_port);
93  } else {
94         port = LDAP_PORT;
95  }
96
97  if ( debug & DEBUG_AUTHEN_FLAG )
98   report(LOG_DEBUG, "In verify_ldap : Before ldap_init : ldapserver = %s port= %d", ldapServer, port);
99
100
101   if( (ld = ldap_init(ldapServer, port)) == NULL)
102     {
103       report(LOG_DEBUG, "Unable to connect to LDAP server:%s port:%d",ldapServer, port);
104       return 1;
105     }
106
107   err=ldap_simple_bind_s(ld, (/* de-const */ char *) user, (/* de-const */ char *) users_passwd);
108
109   if(err != LDAP_SUCCESS)
110     {
111       if ( debug & DEBUG_AUTHEN_FLAG )
112         report(LOG_DEBUG,"Error while bind : %d %s",err, ldap_err2string(err) );
113       return 1;
114     }
115   else
116     {
117       /* Success */
118      if ( debug & DEBUG_AUTHEN_FLAG )
119                 report(LOG_DEBUG, "LDAP authentication Sucess ");
120      ldap_unbind_s(ld);
121      return 0;
122     }
123 }
124
125 #else /* USE_LDAP */
126
127 TAC_SOURCEFILE_EMPTY
128
129 #endif /* USE_LDAP */