Import of tac_plus.v8.tar.gz: 173206 bytes, md5:
[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 #if defined(USE_LDAP)
40 #include <stdio.h>
41 #include <string.h>
42 #include <lber.h>
43 #include <ldap.h>
44 #include <ldap_cdefs.h>
45
46 #include "tac_plus.h"
47 #include "ldap.h"
48
49
50 int
51 ldap_verify(user, users_passwd, str_conn)
52 char *user, *users_passwd;      /* Username and gived password   */
53 char *str_conn;                 /* String connection to database */
54 {
55   char *buf;
56   char *ldapServer;
57   char *ldap_port;
58   LDAP *ld;
59   int port;
60   int err;
61
62 /* Don't allow null username and passwd */ 
63   if ( *user == '0' || *users_passwd == '0' ) return (1);
64
65   buf=(char *)malloc(strlen(str_conn)+1);
66   if (buf == NULL ){ 
67         report(LOG_DEBUG, "Error can't allocate memory");
68         return(1);
69   }
70   
71   strcpy(buf,str_conn);
72   ldapServer=strstr(buf, "://");
73   
74   if(ldapServer == NULL && strlen(ldapServer) <4 ) {
75         if (debug) {
76                 report(LOG_DEBUG, "Error parse ldap server");
77                 return(1);
78         }
79   } 
80   
81  ldapServer=ldapServer+3;
82
83  ldap_port=(char *)strstr(ldapServer, ":");
84
85  if (ldap_port != NULL ) {
86                 *ldap_port='\0';
87                 port=atoi(++ldap_port);
88  } else {
89         port = LDAP_PORT;
90  }
91  
92  if ( debug & DEBUG_AUTHEN_FLAG ) 
93   report(LOG_DEBUG, "In verify_ldap : Before ldap_init : ldapserver = %s port= %d", ldapServer, port);
94
95
96   if( (ld = ldap_init(ldapServer, port)) == NULL)
97     {
98       report(LOG_DEBUG, "Unable to connect to LDAP server:%s port:%d",ldapServer, port);
99       return 1;
100     }
101   
102   err=ldap_simple_bind_s(ld, user, users_passwd);
103   
104   if(err != LDAP_SUCCESS)
105     {
106       if ( debug & DEBUG_AUTHEN_FLAG ) 
107         report(LOG_DEBUG,"Error while bind : %d %s",err, ldap_err2string(err) );
108       return 1;
109     }         
110   else
111     {
112       /* Success */
113      if ( debug & DEBUG_AUTHEN_FLAG ) 
114                 report(LOG_DEBUG, "LDAP authentication Sucess ");
115      ldap_unbind_s(ld); 
116      return 0;
117     }
118 }
119 #endif /* LDAP */