-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsm_ldap.c
119 lines (91 loc) · 2.02 KB
/
sm_ldap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifdef USE_LDAP
#include <lber.h>
#include <ldap.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include "config.h"
#include "sm_ldap.h"
char *ldap_auth_err_codes[6] =
{
"OK",
"Failed to open",
"Search failed",
"Failed to get dn",
"Bind failed",
"Auth bind failed"
};
int ldap_auth (const char *username, char *passwd, char **pop_hosts)
{
/* Authenticates username against ldap by using the USER_FILTER
to find their DN, and attempting to bind to that DN with the
supplied passwd.
If pop_hosts != NULL, then it will fill it with the users
listed mailbox hosts. (to a maximum number of MAX_LDAP_POPS)
Returns < 0 for failure, 0 for success or >0 for the number of
pop hosts
*/
LDAP *myLDAP;
LDAPMessage *result;
char *dn;
char filter[128];
char **value;
myLDAP = ldap_open (SM_LDAP_HOST, SM_LDAP_PORT);
if (myLDAP == NULL)
{
return (1);
}
if (ldap_simple_bind_s (myLDAP, "", "") != LDAP_SUCCESS)
{
return (2);
}
snprintf (filter, MAX_FILTER, USER_FILTER, username);
if (ldap_search_s (myLDAP, SM_LDAP_BASEDN, LDAP_SCOPE_SUBTREE, filter, NULL, 0, &result) != LDAP_SUCCESS)
{
return (3);
}
dn = ldap_get_dn (myLDAP, result);
if (!dn)
{
return (4);
} else
if (SM_LDAP_AUTH)
{
if (ldap_simple_bind_s (myLDAP, dn, passwd) != LDAP_SUCCESS)
{
return (5);
}
}
// We are authenticated!
//
if (pop_hosts == NULL)
{
ldap_unbind (myLDAP);
return (0);
}
// Need to find the LDAP listed pop servers, if any
value = ldap_get_values (myLDAP, result, SM_LDAP_POP);
if (value)
{
// There are some pop servers listed
int i;
i = 0;
while (value[i] && (i < MAX_LDAP_POPS))
{
pop_hosts[i] = strdup (value[i]);
if (LOG_LEVEL > 40)
{
if (DEBUG)
{
fprintf (stderr, "Adding popserver %s for %s from LDAP\n", pop_hosts[i], username);
}
syslog (LOG_NOTICE, "Adding popserver %s for %s from LDAP\n", pop_hosts[i], username);
}
i++;
}
return (i);
}
return (0);
}
#endif