annotate src/auth/db-ldap.c @ 4006:0e8f0647504b HEAD

Check that uris and hosts settings are correct.
author Timo Sirainen <tss@iki.fi>
date Sun, 12 Feb 2006 12:03:39 +0200
parents af15aab60ff1
children 92b572fbb88a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1 /* Copyright (C) 2003 Timo Sirainen */
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
3474
9096b7957413 Removed direct config.h including. I'm not sure why it was done before,
Timo Sirainen <tss@iki.fi>
parents: 3306
diff changeset
3 #include "common.h"
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #if defined(PASSDB_LDAP) || defined(USERDB_LDAP)
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6
1075
f1401fa7ab03 auth process fixes, LDAP seems to be working (with the kludge define or
Timo Sirainen <tss@iki.fi>
parents: 1062
diff changeset
7 #include "network.h"
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 #include "ioloop.h"
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 #include "hash.h"
1189
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
10 #include "str.h"
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 #include "settings.h"
3502
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
12 #include "userdb.h"
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13 #include "db-ldap.h"
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 #include <stddef.h>
1610
6850142c4e25 New configuration file code. Some syntax changes, but tries to be somewhat
Timo Sirainen <tss@iki.fi>
parents: 1330
diff changeset
16 #include <stdlib.h>
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17
1181
ac7dbb236b59 Rather than block for two seconds, we can just call ldap_result() again if
Timo Sirainen <tss@iki.fi>
parents: 1143
diff changeset
18 /* Older versions may require calling ldap_result() twice */
ac7dbb236b59 Rather than block for two seconds, we can just call ldap_result() again if
Timo Sirainen <tss@iki.fi>
parents: 1143
diff changeset
19 #if LDAP_VENDOR_VERSION <= 20112
1086
067130d609b7 Define OPENLDAP_ASYNC_WORKAROUND
Timo Sirainen <tss@iki.fi>
parents: 1075
diff changeset
20 # define OPENLDAP_ASYNC_WORKAROUND
067130d609b7 Define OPENLDAP_ASYNC_WORKAROUND
Timo Sirainen <tss@iki.fi>
parents: 1075
diff changeset
21 #endif
067130d609b7 Define OPENLDAP_ASYNC_WORKAROUND
Timo Sirainen <tss@iki.fi>
parents: 1075
diff changeset
22
2325
7613e0f68513 Fixed to compile with Solaris LDAP library
Timo Sirainen <tss@iki.fi>
parents: 1910
diff changeset
23 /* Solaris LDAP library doesn't have LDAP_OPT_SUCCESS */
7613e0f68513 Fixed to compile with Solaris LDAP library
Timo Sirainen <tss@iki.fi>
parents: 1910
diff changeset
24 #ifndef LDAP_OPT_SUCCESS
7613e0f68513 Fixed to compile with Solaris LDAP library
Timo Sirainen <tss@iki.fi>
parents: 1910
diff changeset
25 # define LDAP_OPT_SUCCESS LDAP_SUCCESS
7613e0f68513 Fixed to compile with Solaris LDAP library
Timo Sirainen <tss@iki.fi>
parents: 1910
diff changeset
26 #endif
7613e0f68513 Fixed to compile with Solaris LDAP library
Timo Sirainen <tss@iki.fi>
parents: 1910
diff changeset
27
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28 #define DEF(type, name) \
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 { type, #name, offsetof(struct ldap_settings, name) }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31 static struct setting_def setting_defs[] = {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32 DEF(SET_STR, hosts),
1910
b9005f93be70 Patch by Quentin Garnier:
Timo Sirainen <tss@iki.fi>
parents: 1897
diff changeset
33 DEF(SET_STR, uris),
1075
f1401fa7ab03 auth process fixes, LDAP seems to be working (with the kludge define or
Timo Sirainen <tss@iki.fi>
parents: 1062
diff changeset
34 DEF(SET_STR, dn),
f1401fa7ab03 auth process fixes, LDAP seems to be working (with the kludge define or
Timo Sirainen <tss@iki.fi>
parents: 1062
diff changeset
35 DEF(SET_STR, dnpass),
3771
4b6d962485b9 Added authentication bind support. Patch by J.M. Maurer.
Timo Sirainen <tss@iki.fi>
parents: 3731
diff changeset
36 DEF(SET_BOOL, auth_bind),
3840
935f12d0d2fe Added fast authbinding and auth_bind_userdn setting. Patch by Geff
Timo Sirainen <tss@iki.fi>
parents: 3771
diff changeset
37 DEF(SET_STR, auth_bind_userdn),
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38 DEF(SET_STR, deref),
1135
81930fff13cf passdb ldap added. fixes to userdb ldap.
Timo Sirainen <tss@iki.fi>
parents: 1086
diff changeset
39 DEF(SET_STR, scope),
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
40 DEF(SET_STR, base),
1282
e8894f2c776f Added ldap_version setting.
Timo Sirainen <tss@iki.fi>
parents: 1265
diff changeset
41 DEF(SET_INT, ldap_version),
1136
ad6343bd4479 Separate user and pass attrs/filters in config file
Timo Sirainen <tss@iki.fi>
parents: 1135
diff changeset
42 DEF(SET_STR, user_attrs),
ad6343bd4479 Separate user and pass attrs/filters in config file
Timo Sirainen <tss@iki.fi>
parents: 1135
diff changeset
43 DEF(SET_STR, user_filter),
ad6343bd4479 Separate user and pass attrs/filters in config file
Timo Sirainen <tss@iki.fi>
parents: 1135
diff changeset
44 DEF(SET_STR, pass_attrs),
1141
873634a5b472 Added user_global_uid and user_global_gid LDAP settings.
Timo Sirainen <tss@iki.fi>
parents: 1136
diff changeset
45 DEF(SET_STR, pass_filter),
1191
65e48854491d Added default_pass_scheme to LDAP. Support for more password schemes. Merged
Timo Sirainen <tss@iki.fi>
parents: 1189
diff changeset
46 DEF(SET_STR, default_pass_scheme),
3502
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
47 DEF(SET_STR, user_global_uid),
3913
af15aab60ff1 Settings' default listing wasn't ended properly, which could have caused
Timo Sirainen <tss@iki.fi>
parents: 3908
diff changeset
48 DEF(SET_STR, user_global_gid),
af15aab60ff1 Settings' default listing wasn't ended properly, which could have caused
Timo Sirainen <tss@iki.fi>
parents: 3908
diff changeset
49
af15aab60ff1 Settings' default listing wasn't ended properly, which could have caused
Timo Sirainen <tss@iki.fi>
parents: 3908
diff changeset
50 { 0, NULL, 0 }
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51 };
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53 struct ldap_settings default_ldap_settings = {
1910
b9005f93be70 Patch by Quentin Garnier:
Timo Sirainen <tss@iki.fi>
parents: 1897
diff changeset
54 MEMBER(hosts) NULL,
b9005f93be70 Patch by Quentin Garnier:
Timo Sirainen <tss@iki.fi>
parents: 1897
diff changeset
55 MEMBER(uris) NULL,
1075
f1401fa7ab03 auth process fixes, LDAP seems to be working (with the kludge define or
Timo Sirainen <tss@iki.fi>
parents: 1062
diff changeset
56 MEMBER(dn) NULL,
f1401fa7ab03 auth process fixes, LDAP seems to be working (with the kludge define or
Timo Sirainen <tss@iki.fi>
parents: 1062
diff changeset
57 MEMBER(dnpass) NULL,
3771
4b6d962485b9 Added authentication bind support. Patch by J.M. Maurer.
Timo Sirainen <tss@iki.fi>
parents: 3731
diff changeset
58 MEMBER(auth_bind) FALSE,
3840
935f12d0d2fe Added fast authbinding and auth_bind_userdn setting. Patch by Geff
Timo Sirainen <tss@iki.fi>
parents: 3771
diff changeset
59 MEMBER(auth_bind_userdn) NULL,
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60 MEMBER(deref) "never",
1135
81930fff13cf passdb ldap added. fixes to userdb ldap.
Timo Sirainen <tss@iki.fi>
parents: 1086
diff changeset
61 MEMBER(scope) "subtree",
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62 MEMBER(base) NULL,
1282
e8894f2c776f Added ldap_version setting.
Timo Sirainen <tss@iki.fi>
parents: 1265
diff changeset
63 MEMBER(ldap_version) 2,
3094
d78e9a31b6d8 Move default filters/attrs to setting defaults rather than check it from
Timo Sirainen <tss@iki.fi>
parents: 2994
diff changeset
64 MEMBER(user_attrs) "uid,homeDirectory,,,uidNumber,gidNumber",
d78e9a31b6d8 Move default filters/attrs to setting defaults rather than check it from
Timo Sirainen <tss@iki.fi>
parents: 2994
diff changeset
65 MEMBER(user_filter) "(&(objectClass=posixAccount)(uid=%u))",
d78e9a31b6d8 Move default filters/attrs to setting defaults rather than check it from
Timo Sirainen <tss@iki.fi>
parents: 2994
diff changeset
66 MEMBER(pass_attrs) "uid,userPassword",
d78e9a31b6d8 Move default filters/attrs to setting defaults rather than check it from
Timo Sirainen <tss@iki.fi>
parents: 2994
diff changeset
67 MEMBER(pass_filter) "(&(objectClass=posixAccount)(uid=%u))",
1191
65e48854491d Added default_pass_scheme to LDAP. Support for more password schemes. Merged
Timo Sirainen <tss@iki.fi>
parents: 1189
diff changeset
68 MEMBER(default_pass_scheme) "crypt",
3502
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
69 MEMBER(user_global_uid) "",
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
70 MEMBER(user_global_gid) ""
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
71 };
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
72
1143
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
73 static struct ldap_connection *ldap_connections = NULL;
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
74
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3840
diff changeset
75 static void ldap_conn_close(struct ldap_connection *conn, bool flush_requests);
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
77 static int deref2str(const char *str)
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78 {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79 if (strcasecmp(str, "never") == 0)
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80 return LDAP_DEREF_NEVER;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81 if (strcasecmp(str, "searching") == 0)
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
82 return LDAP_DEREF_SEARCHING;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
83 if (strcasecmp(str, "finding") == 0)
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
84 return LDAP_DEREF_FINDING;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85 if (strcasecmp(str, "always") == 0)
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
86 return LDAP_DEREF_ALWAYS;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
87
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88 i_fatal("LDAP: Unknown deref option '%s'", str);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
89 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
90
1135
81930fff13cf passdb ldap added. fixes to userdb ldap.
Timo Sirainen <tss@iki.fi>
parents: 1086
diff changeset
91 static int scope2str(const char *str)
81930fff13cf passdb ldap added. fixes to userdb ldap.
Timo Sirainen <tss@iki.fi>
parents: 1086
diff changeset
92 {
81930fff13cf passdb ldap added. fixes to userdb ldap.
Timo Sirainen <tss@iki.fi>
parents: 1086
diff changeset
93 if (strcasecmp(str, "base") == 0)
81930fff13cf passdb ldap added. fixes to userdb ldap.
Timo Sirainen <tss@iki.fi>
parents: 1086
diff changeset
94 return LDAP_SCOPE_BASE;
81930fff13cf passdb ldap added. fixes to userdb ldap.
Timo Sirainen <tss@iki.fi>
parents: 1086
diff changeset
95 if (strcasecmp(str, "onelevel") == 0)
81930fff13cf passdb ldap added. fixes to userdb ldap.
Timo Sirainen <tss@iki.fi>
parents: 1086
diff changeset
96 return LDAP_SCOPE_ONELEVEL;
81930fff13cf passdb ldap added. fixes to userdb ldap.
Timo Sirainen <tss@iki.fi>
parents: 1086
diff changeset
97 if (strcasecmp(str, "subtree") == 0)
81930fff13cf passdb ldap added. fixes to userdb ldap.
Timo Sirainen <tss@iki.fi>
parents: 1086
diff changeset
98 return LDAP_SCOPE_SUBTREE;
81930fff13cf passdb ldap added. fixes to userdb ldap.
Timo Sirainen <tss@iki.fi>
parents: 1086
diff changeset
99
81930fff13cf passdb ldap added. fixes to userdb ldap.
Timo Sirainen <tss@iki.fi>
parents: 1086
diff changeset
100 i_fatal("LDAP: Unknown scope option '%s'", str);
81930fff13cf passdb ldap added. fixes to userdb ldap.
Timo Sirainen <tss@iki.fi>
parents: 1086
diff changeset
101 }
81930fff13cf passdb ldap added. fixes to userdb ldap.
Timo Sirainen <tss@iki.fi>
parents: 1086
diff changeset
102
1210
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
103 const char *ldap_get_error(struct ldap_connection *conn)
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
104 {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
105 int ret, err;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
106
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
107 ret = ldap_get_option(conn->ld, LDAP_OPT_ERROR_NUMBER, (void *) &err);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
108 if (ret != LDAP_SUCCESS) {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
109 i_error("LDAP: Can't get error number: %s",
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110 ldap_err2string(ret));
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
111 return "??";
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
112 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
113
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114 return ldap_err2string(err);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
116
3771
4b6d962485b9 Added authentication bind support. Patch by J.M. Maurer.
Timo Sirainen <tss@iki.fi>
parents: 3731
diff changeset
117 void db_ldap_search(struct ldap_connection *conn, struct ldap_request *request,
4b6d962485b9 Added authentication bind support. Patch by J.M. Maurer.
Timo Sirainen <tss@iki.fi>
parents: 3731
diff changeset
118 int scope)
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
119 {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
120 int msgid;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
121
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
122 if (!conn->connected) {
2648
cc2e39912eb3 Added preinit() call to userdb/passdbs, which is called before dropping
Timo Sirainen <tss@iki.fi>
parents: 2325
diff changeset
123 if (!db_ldap_connect(conn)) {
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
124 request->callback(conn, request, NULL);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
125 return;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
126 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
127 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
128
3771
4b6d962485b9 Added authentication bind support. Patch by J.M. Maurer.
Timo Sirainen <tss@iki.fi>
parents: 3731
diff changeset
129 msgid = ldap_search(conn->ld, request->base, scope,
3731
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
130 request->filter, request->attributes, 0);
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
131 if (msgid == -1) {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
132 i_error("LDAP: ldap_search() failed (filter %s): %s",
3731
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
133 request->filter, ldap_get_error(conn));
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
134 request->callback(conn, request, NULL);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
135 return;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
136 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
137
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
138 hash_insert(conn->requests, POINTER_CAST(msgid), request);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
139 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
140
3731
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
141 static void ldap_conn_retry_requests(struct ldap_connection *conn)
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
142 {
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
143 struct hash_table *old_requests;
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
144 struct hash_iterate_context *iter;
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
145 void *key, *value;
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
146
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
147 i_assert(conn->connected);
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
148
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
149 if (hash_size(conn->requests) == 0)
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
150 return;
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
151
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
152 old_requests = conn->requests;
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
153 conn->requests = hash_create(default_pool, conn->pool, 0, NULL, NULL);
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
154
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
155 iter = hash_iterate_init(old_requests);
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
156 while (hash_iterate(iter, &key, &value)) {
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
157 struct ldap_request *request = value;
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
158
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
159 i_assert(conn->connected);
3771
4b6d962485b9 Added authentication bind support. Patch by J.M. Maurer.
Timo Sirainen <tss@iki.fi>
parents: 3731
diff changeset
160 db_ldap_search(conn, request, conn->set.ldap_scope);
3731
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
161 }
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
162 hash_iterate_deinit(iter);
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
163 hash_destroy(old_requests);
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
164 }
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
165
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
166 static void ldap_conn_reconnect(struct ldap_connection *conn)
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
167 {
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
168 ldap_conn_close(conn, FALSE);
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
169
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
170 if (!db_ldap_connect(conn)) {
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
171 /* failed to reconnect. fail all requests. */
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
172 ldap_conn_close(conn, TRUE);
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
173 }
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
174 }
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
175
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
176 static void ldap_input(void *context)
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
177 {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
178 struct ldap_connection *conn = context;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
179 struct ldap_request *request;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
180 struct timeval timeout;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
181 LDAPMessage *res;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
182 int ret, msgid;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
183
1210
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
184 while (conn->ld != NULL) {
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
185 memset(&timeout, 0, sizeof(timeout));
1181
ac7dbb236b59 Rather than block for two seconds, we can just call ldap_result() again if
Timo Sirainen <tss@iki.fi>
parents: 1143
diff changeset
186 ret = ldap_result(conn->ld, LDAP_RES_ANY, 1, &timeout, &res);
1075
f1401fa7ab03 auth process fixes, LDAP seems to be working (with the kludge define or
Timo Sirainen <tss@iki.fi>
parents: 1062
diff changeset
187 #ifdef OPENLDAP_ASYNC_WORKAROUND
1181
ac7dbb236b59 Rather than block for two seconds, we can just call ldap_result() again if
Timo Sirainen <tss@iki.fi>
parents: 1143
diff changeset
188 if (ret == 0) {
ac7dbb236b59 Rather than block for two seconds, we can just call ldap_result() again if
Timo Sirainen <tss@iki.fi>
parents: 1143
diff changeset
189 /* try again, there may be another in buffer */
ac7dbb236b59 Rather than block for two seconds, we can just call ldap_result() again if
Timo Sirainen <tss@iki.fi>
parents: 1143
diff changeset
190 ret = ldap_result(conn->ld, LDAP_RES_ANY, 1,
ac7dbb236b59 Rather than block for two seconds, we can just call ldap_result() again if
Timo Sirainen <tss@iki.fi>
parents: 1143
diff changeset
191 &timeout, &res);
ac7dbb236b59 Rather than block for two seconds, we can just call ldap_result() again if
Timo Sirainen <tss@iki.fi>
parents: 1143
diff changeset
192 }
1075
f1401fa7ab03 auth process fixes, LDAP seems to be working (with the kludge define or
Timo Sirainen <tss@iki.fi>
parents: 1062
diff changeset
193 #endif
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
194 if (ret <= 0) {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
195 if (ret < 0) {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
196 i_error("LDAP: ldap_result() failed: %s",
1210
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
197 ldap_get_error(conn));
3731
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
198 ldap_conn_reconnect(conn);
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
199 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
200 return;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
201 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
202
1210
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
203 msgid = ldap_msgid(res);
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
204 request = hash_lookup(conn->requests, POINTER_CAST(msgid));
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
205 if (request == NULL) {
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
206 i_error("LDAP: Reply with unknown msgid %d",
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
207 msgid);
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
208 } else {
1210
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
209 hash_remove(conn->requests, POINTER_CAST(msgid));
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
210 request->callback(conn, request, res);
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
211 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
212
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
213 ldap_msgfree(res);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
214 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
215 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
216
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3840
diff changeset
217 bool db_ldap_connect(struct ldap_connection *conn)
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
218 {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
219 int ret, fd;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
220
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
221 if (conn->connected)
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
222 return TRUE;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
223
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
224 if (conn->ld == NULL) {
1910
b9005f93be70 Patch by Quentin Garnier:
Timo Sirainen <tss@iki.fi>
parents: 1897
diff changeset
225 if (conn->set.uris != NULL) {
2325
7613e0f68513 Fixed to compile with Solaris LDAP library
Timo Sirainen <tss@iki.fi>
parents: 1910
diff changeset
226 #ifdef LDAP_HAVE_INITIALIZE
1910
b9005f93be70 Patch by Quentin Garnier:
Timo Sirainen <tss@iki.fi>
parents: 1897
diff changeset
227 if (ldap_initialize(&conn->ld, conn->set.uris) != LDAP_SUCCESS)
b9005f93be70 Patch by Quentin Garnier:
Timo Sirainen <tss@iki.fi>
parents: 1897
diff changeset
228 conn->ld = NULL;
2325
7613e0f68513 Fixed to compile with Solaris LDAP library
Timo Sirainen <tss@iki.fi>
parents: 1910
diff changeset
229 #else
7613e0f68513 Fixed to compile with Solaris LDAP library
Timo Sirainen <tss@iki.fi>
parents: 1910
diff changeset
230 i_fatal("LDAP: Your LDAP library doesn't support "
7613e0f68513 Fixed to compile with Solaris LDAP library
Timo Sirainen <tss@iki.fi>
parents: 1910
diff changeset
231 "'uris' setting, use 'hosts' instead.");
7613e0f68513 Fixed to compile with Solaris LDAP library
Timo Sirainen <tss@iki.fi>
parents: 1910
diff changeset
232 #endif
1910
b9005f93be70 Patch by Quentin Garnier:
Timo Sirainen <tss@iki.fi>
parents: 1897
diff changeset
233 } else
b9005f93be70 Patch by Quentin Garnier:
Timo Sirainen <tss@iki.fi>
parents: 1897
diff changeset
234 conn->ld = ldap_init(conn->set.hosts, LDAP_PORT);
b9005f93be70 Patch by Quentin Garnier:
Timo Sirainen <tss@iki.fi>
parents: 1897
diff changeset
235
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
236 if (conn->ld == NULL)
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
237 i_fatal("LDAP: ldap_init() failed with hosts: %s",
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
238 conn->set.hosts);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
239
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
240 ret = ldap_set_option(conn->ld, LDAP_OPT_DEREF,
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
241 (void *) &conn->set.ldap_deref);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
242 if (ret != LDAP_SUCCESS) {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
243 i_fatal("LDAP: Can't set deref option: %s",
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
244 ldap_err2string(ret));
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
245 }
1282
e8894f2c776f Added ldap_version setting.
Timo Sirainen <tss@iki.fi>
parents: 1265
diff changeset
246
e8894f2c776f Added ldap_version setting.
Timo Sirainen <tss@iki.fi>
parents: 1265
diff changeset
247 ret = ldap_set_option(conn->ld, LDAP_OPT_PROTOCOL_VERSION,
e8894f2c776f Added ldap_version setting.
Timo Sirainen <tss@iki.fi>
parents: 1265
diff changeset
248 (void *) &conn->set.ldap_version);
e8894f2c776f Added ldap_version setting.
Timo Sirainen <tss@iki.fi>
parents: 1265
diff changeset
249 if (ret != LDAP_OPT_SUCCESS) {
e8894f2c776f Added ldap_version setting.
Timo Sirainen <tss@iki.fi>
parents: 1265
diff changeset
250 i_fatal("LDAP: Can't set protocol version %u: %s",
e8894f2c776f Added ldap_version setting.
Timo Sirainen <tss@iki.fi>
parents: 1265
diff changeset
251 conn->set.ldap_version, ldap_err2string(ret));
e8894f2c776f Added ldap_version setting.
Timo Sirainen <tss@iki.fi>
parents: 1265
diff changeset
252 }
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
253 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
254
3771
4b6d962485b9 Added authentication bind support. Patch by J.M. Maurer.
Timo Sirainen <tss@iki.fi>
parents: 3731
diff changeset
255 /* FIXME: we shouldn't use blocking bind */
1075
f1401fa7ab03 auth process fixes, LDAP seems to be working (with the kludge define or
Timo Sirainen <tss@iki.fi>
parents: 1062
diff changeset
256 ret = ldap_simple_bind_s(conn->ld, conn->set.dn, conn->set.dnpass);
1210
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
257 if (ret == LDAP_SERVER_DOWN) {
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
258 i_error("LDAP: Can't connect to server: %s", conn->set.hosts);
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
259 return FALSE;
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
260 }
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
261 if (ret != LDAP_SUCCESS) {
1210
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
262 i_error("LDAP: ldap_simple_bind_s() failed (dn %s): %s",
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
263 conn->set.dn == NULL ? "(none)" : conn->set.dn,
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
264 ldap_get_error(conn));
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
265 return FALSE;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
266 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
267
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
268 conn->connected = TRUE;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
269
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
270 /* register LDAP input to ioloop */
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
271 ret = ldap_get_option(conn->ld, LDAP_OPT_DESC, (void *) &fd);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
272 if (ret != LDAP_SUCCESS) {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
273 i_fatal("LDAP: Can't get connection fd: %s",
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
274 ldap_err2string(ret));
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
275 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
276
1075
f1401fa7ab03 auth process fixes, LDAP seems to be working (with the kludge define or
Timo Sirainen <tss@iki.fi>
parents: 1062
diff changeset
277 net_set_nonblock(fd, TRUE);
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
278 conn->io = io_add(fd, IO_READ, ldap_input, conn);
3731
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
279
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
280 /* in case there are requests waiting, retry them */
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
281 ldap_conn_retry_requests(conn);
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
282 return TRUE;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
283 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
284
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3840
diff changeset
285 static void ldap_conn_close(struct ldap_connection *conn, bool flush_requests)
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
286 {
1897
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1709
diff changeset
287 struct hash_iterate_context *iter;
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1709
diff changeset
288 void *key, *value;
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1709
diff changeset
289
3731
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
290 if (flush_requests) {
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
291 iter = hash_iterate_init(conn->requests);
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
292 while (hash_iterate(iter, &key, &value)) {
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
293 struct ldap_request *request = value;
1897
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1709
diff changeset
294
3731
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
295 request->callback(conn, request, NULL);
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
296 }
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
297 hash_iterate_deinit(iter);
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
298 hash_clear(conn->requests, FALSE);
1897
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1709
diff changeset
299 }
1210
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
300
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
301 conn->connected = FALSE;
8e6addbf12b3 minor ldap fixes
Timo Sirainen <tss@iki.fi>
parents: 1191
diff changeset
302
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
303 if (conn->io != NULL)
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
304 io_remove(&conn->io);
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
305
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
306 if (conn->ld != NULL) {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
307 ldap_unbind(conn->ld);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
308 conn->ld = NULL;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
309 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
310 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
311
3158
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
312 void db_ldap_set_attrs(struct ldap_connection *conn, const char *attrlist,
3306
aebed9a9edac If both userdb and passdb used LDAP the later one was overriding first one's
Timo Sirainen <tss@iki.fi>
parents: 3212
diff changeset
313 char ***attr_names_r, struct hash_table *attr_map,
3158
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
314 const char *const default_attr_map[])
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
315 {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
316 const char *const *attr;
3158
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
317 char *name, *value, *p;
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
318 unsigned int i, size;
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
319
3158
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
320 if (*attrlist == '\0')
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
321 return;
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
322
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
323 t_push();
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
324 attr = t_strsplit(attrlist, ",");
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
325
3212
eb840633c9bf LDAP crashfixes.
Timo Sirainen <tss@iki.fi>
parents: 3161
diff changeset
326 /* @UNSAFE */
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
327 for (size = 0; attr[size] != NULL; size++) ;
3306
aebed9a9edac If both userdb and passdb used LDAP the later one was overriding first one's
Timo Sirainen <tss@iki.fi>
parents: 3212
diff changeset
328 *attr_names_r = p_new(conn->pool, char *, size + 1);
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
329
3158
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
330 for (i = 0; i < size; i++) {
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
331 p = strchr(attr[i], '=');
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
332 if (p == NULL) {
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
333 name = p_strdup(conn->pool, attr[i]);
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
334 value = *default_attr_map == NULL ? name :
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
335 p_strdup(conn->pool, *default_attr_map);
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
336 } else {
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
337 name = p_strdup_until(conn->pool, attr[i], p);
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
338 value = p_strdup(conn->pool, p + 1);
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
339 }
3158
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
340
3306
aebed9a9edac If both userdb and passdb used LDAP the later one was overriding first one's
Timo Sirainen <tss@iki.fi>
parents: 3212
diff changeset
341 (*attr_names_r)[i] = name;
3158
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
342 if (*name != '\0')
3306
aebed9a9edac If both userdb and passdb used LDAP the later one was overriding first one's
Timo Sirainen <tss@iki.fi>
parents: 3212
diff changeset
343 hash_insert(attr_map, name, value);
3158
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
344
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
345 if (*default_attr_map != NULL)
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
346 default_attr_map++;
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
347 }
3158
8849f2e380d1 userdb can now return extra parameters to master. Removed special handling
Timo Sirainen <tss@iki.fi>
parents: 3094
diff changeset
348 t_pop();
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
349 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
350
1330
7cde19dbe754 Moved auth_username_chars from db-pgsql to generic for all. Some other
Timo Sirainen <tss@iki.fi>
parents: 1282
diff changeset
351 #define IS_LDAP_ESCAPED_CHAR(c) \
7cde19dbe754 Moved auth_username_chars from db-pgsql to generic for all. Some other
Timo Sirainen <tss@iki.fi>
parents: 1282
diff changeset
352 ((c) == '*' || (c) == '(' || (c) == ')' || (c) == '\\')
7cde19dbe754 Moved auth_username_chars from db-pgsql to generic for all. Some other
Timo Sirainen <tss@iki.fi>
parents: 1282
diff changeset
353
1189
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
354 const char *ldap_escape(const char *str)
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
355 {
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
356 const char *p;
1330
7cde19dbe754 Moved auth_username_chars from db-pgsql to generic for all. Some other
Timo Sirainen <tss@iki.fi>
parents: 1282
diff changeset
357 string_t *ret;
1189
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
358
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
359 for (p = str; *p != '\0'; p++) {
1330
7cde19dbe754 Moved auth_username_chars from db-pgsql to generic for all. Some other
Timo Sirainen <tss@iki.fi>
parents: 1282
diff changeset
360 if (IS_LDAP_ESCAPED_CHAR(*p))
1189
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
361 break;
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
362 }
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
363
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
364 if (*p == '\0')
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
365 return str;
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
366
1330
7cde19dbe754 Moved auth_username_chars from db-pgsql to generic for all. Some other
Timo Sirainen <tss@iki.fi>
parents: 1282
diff changeset
367 ret = t_str_new((size_t) (p - str) + 64);
7cde19dbe754 Moved auth_username_chars from db-pgsql to generic for all. Some other
Timo Sirainen <tss@iki.fi>
parents: 1282
diff changeset
368 str_append_n(ret, str, (size_t) (p - str));
7cde19dbe754 Moved auth_username_chars from db-pgsql to generic for all. Some other
Timo Sirainen <tss@iki.fi>
parents: 1282
diff changeset
369
1189
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
370 for (; *p != '\0'; p++) {
1330
7cde19dbe754 Moved auth_username_chars from db-pgsql to generic for all. Some other
Timo Sirainen <tss@iki.fi>
parents: 1282
diff changeset
371 if (IS_LDAP_ESCAPED_CHAR(*p))
7cde19dbe754 Moved auth_username_chars from db-pgsql to generic for all. Some other
Timo Sirainen <tss@iki.fi>
parents: 1282
diff changeset
372 str_append_c(ret, '\\');
7cde19dbe754 Moved auth_username_chars from db-pgsql to generic for all. Some other
Timo Sirainen <tss@iki.fi>
parents: 1282
diff changeset
373 str_append_c(ret, *p);
1189
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
374 }
1330
7cde19dbe754 Moved auth_username_chars from db-pgsql to generic for all. Some other
Timo Sirainen <tss@iki.fi>
parents: 1282
diff changeset
375 return str_c(ret);
1189
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
376 }
2cb8e2136283 Escape special chars in username if needed.
Timo Sirainen <tss@iki.fi>
parents: 1182
diff changeset
377
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
378 static const char *parse_setting(const char *key, const char *value,
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
379 void *context)
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
380 {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
381 struct ldap_connection *conn = context;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
382
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
383 return parse_setting_from_defs(conn->pool, setting_defs,
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
384 &conn->set, key, value);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
385 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
386
1143
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
387 static struct ldap_connection *ldap_conn_find(const char *config_path)
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
388 {
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
389 struct ldap_connection *conn;
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
390
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
391 for (conn = ldap_connections; conn != NULL; conn = conn->next) {
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
392 if (strcmp(conn->config_path, config_path) == 0)
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
393 return conn;
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
394 }
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
395
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
396 return NULL;
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
397 }
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
398
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
399 struct ldap_connection *db_ldap_init(const char *config_path)
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
400 {
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
401 struct ldap_connection *conn;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
402 pool_t pool;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
403
1143
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
404 /* see if it already exists */
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
405 conn = ldap_conn_find(config_path);
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
406 if (conn != NULL) {
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
407 conn->refcount++;
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
408 return conn;
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
409 }
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
410
3908
afe21b6d4b68 Give a clear error message if SQL/LDAP configuration file path was left
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
411 if (*config_path == '\0')
afe21b6d4b68 Give a clear error message if SQL/LDAP configuration file path was left
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
412 i_fatal("LDAP: Configuration file path not given");
afe21b6d4b68 Give a clear error message if SQL/LDAP configuration file path was left
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
413
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
414 pool = pool_alloconly_create("ldap_connection", 1024);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
415 conn = p_new(pool, struct ldap_connection, 1);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
416 conn->pool = pool;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
417
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
418 conn->refcount = 1;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
419 conn->requests = hash_create(default_pool, pool, 0, NULL, NULL);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
420
1143
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
421 conn->config_path = p_strdup(pool, config_path);
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
422 conn->set = default_ldap_settings;
1610
6850142c4e25 New configuration file code. Some syntax changes, but tries to be somewhat
Timo Sirainen <tss@iki.fi>
parents: 1330
diff changeset
423 if (!settings_read(config_path, NULL, parse_setting, NULL, conn))
6850142c4e25 New configuration file code. Some syntax changes, but tries to be somewhat
Timo Sirainen <tss@iki.fi>
parents: 1330
diff changeset
424 exit(FATAL_DEFAULT);
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
425
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
426 if (conn->set.base == NULL)
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
427 i_fatal("LDAP: No base given");
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
428
4006
0e8f0647504b Check that uris and hosts settings are correct.
Timo Sirainen <tss@iki.fi>
parents: 3913
diff changeset
429 if (conn->set.uris == NULL && conn->set.hosts == NULL)
0e8f0647504b Check that uris and hosts settings are correct.
Timo Sirainen <tss@iki.fi>
parents: 3913
diff changeset
430 i_fatal("LDAP: No uris or hosts set");
0e8f0647504b Check that uris and hosts settings are correct.
Timo Sirainen <tss@iki.fi>
parents: 3913
diff changeset
431 #ifndef LDAP_HAVE_INITIALIZE
0e8f0647504b Check that uris and hosts settings are correct.
Timo Sirainen <tss@iki.fi>
parents: 3913
diff changeset
432 if (conn->set.uris == NULL) {
0e8f0647504b Check that uris and hosts settings are correct.
Timo Sirainen <tss@iki.fi>
parents: 3913
diff changeset
433 i_fatal("LDAP: Dovecot compiled without support for LDAP uris "
0e8f0647504b Check that uris and hosts settings are correct.
Timo Sirainen <tss@iki.fi>
parents: 3913
diff changeset
434 "(ldap_initialize not found)");
0e8f0647504b Check that uris and hosts settings are correct.
Timo Sirainen <tss@iki.fi>
parents: 3913
diff changeset
435 }
0e8f0647504b Check that uris and hosts settings are correct.
Timo Sirainen <tss@iki.fi>
parents: 3913
diff changeset
436 #endif
0e8f0647504b Check that uris and hosts settings are correct.
Timo Sirainen <tss@iki.fi>
parents: 3913
diff changeset
437
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
438 conn->set.ldap_deref = deref2str(conn->set.deref);
3502
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
439 conn->set.ldap_scope = scope2str(conn->set.scope);
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
440
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
441 if (*conn->set.user_global_uid == '\0')
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
442 conn->set.uid = (uid_t)-1;
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
443 else {
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
444 conn->set.uid =
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
445 userdb_parse_uid(NULL, conn->set.user_global_uid);
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
446 if (conn->set.uid == (uid_t)-1) {
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
447 i_fatal("LDAP: Invalid user_global_uid: %s",
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
448 conn->set.user_global_uid);
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
449 }
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
450 }
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
451
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
452 if (*conn->set.user_global_gid == '\0')
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
453 conn->set.gid = (gid_t)-1;
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
454 else {
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
455 conn->set.gid =
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
456 userdb_parse_gid(NULL, conn->set.user_global_gid);
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
457 if (conn->set.gid == (gid_t)-1) {
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
458 i_fatal("LDAP: Invalid user_global_gid: %s",
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
459 conn->set.user_global_gid);
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
460 }
5e78500f1aee user_global_uid and user_global_gid settings weren't working. Also changed
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
461 }
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
462
1143
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
463 conn->next = ldap_connections;
50f10a7a3bad Use the same LDAP connection for both userdb and passdb if config_path is
Timo Sirainen <tss@iki.fi>
parents: 1141
diff changeset
464 ldap_connections = conn;
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
465 return conn;
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
466 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
467
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
468 void db_ldap_unref(struct ldap_connection **_conn)
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
469 {
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
470 struct ldap_connection *conn = *_conn;
3657
0c10475d9968 Separated passdb_module's interface and the actual data struct. Now it's
Timo Sirainen <tss@iki.fi>
parents: 3502
diff changeset
471 struct ldap_connection **p;
0c10475d9968 Separated passdb_module's interface and the actual data struct. Now it's
Timo Sirainen <tss@iki.fi>
parents: 3502
diff changeset
472
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
473 *_conn = NULL;
3657
0c10475d9968 Separated passdb_module's interface and the actual data struct. Now it's
Timo Sirainen <tss@iki.fi>
parents: 3502
diff changeset
474 i_assert(conn->refcount >= 0);
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
475 if (--conn->refcount > 0)
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
476 return;
3657
0c10475d9968 Separated passdb_module's interface and the actual data struct. Now it's
Timo Sirainen <tss@iki.fi>
parents: 3502
diff changeset
477
0c10475d9968 Separated passdb_module's interface and the actual data struct. Now it's
Timo Sirainen <tss@iki.fi>
parents: 3502
diff changeset
478 for (p = &ldap_connections; *p != NULL; p = &(*p)->next) {
0c10475d9968 Separated passdb_module's interface and the actual data struct. Now it's
Timo Sirainen <tss@iki.fi>
parents: 3502
diff changeset
479 if (*p == conn) {
0c10475d9968 Separated passdb_module's interface and the actual data struct. Now it's
Timo Sirainen <tss@iki.fi>
parents: 3502
diff changeset
480 *p = conn->next;
0c10475d9968 Separated passdb_module's interface and the actual data struct. Now it's
Timo Sirainen <tss@iki.fi>
parents: 3502
diff changeset
481 break;
0c10475d9968 Separated passdb_module's interface and the actual data struct. Now it's
Timo Sirainen <tss@iki.fi>
parents: 3502
diff changeset
482 }
0c10475d9968 Separated passdb_module's interface and the actual data struct. Now it's
Timo Sirainen <tss@iki.fi>
parents: 3502
diff changeset
483 }
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
484
3731
0a7beabfe332 If LDAP lookup fails because connection gets closed, try retrying it again
Timo Sirainen <tss@iki.fi>
parents: 3657
diff changeset
485 ldap_conn_close(conn, TRUE);
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
486
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
487 hash_destroy(conn->requests);
3306
aebed9a9edac If both userdb and passdb used LDAP the later one was overriding first one's
Timo Sirainen <tss@iki.fi>
parents: 3212
diff changeset
488 if (conn->pass_attr_map != NULL)
aebed9a9edac If both userdb and passdb used LDAP the later one was overriding first one's
Timo Sirainen <tss@iki.fi>
parents: 3212
diff changeset
489 hash_destroy(conn->pass_attr_map);
aebed9a9edac If both userdb and passdb used LDAP the later one was overriding first one's
Timo Sirainen <tss@iki.fi>
parents: 3212
diff changeset
490 if (conn->user_attr_map != NULL)
aebed9a9edac If both userdb and passdb used LDAP the later one was overriding first one's
Timo Sirainen <tss@iki.fi>
parents: 3212
diff changeset
491 hash_destroy(conn->user_attr_map);
1062
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
492 pool_unref(conn->pool);
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
493 }
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
494
0522a0315d2f Cleanups, LDAP support compiles again and generally looks ok, even if it
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
495 #endif