2 Unix SMB/CIFS implementation.
6 Copyright (C) Simo Sorce 2003
7 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
8 Copyright (C) Gerald Carter 2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #define DBGC_CLASS DBGC_CONFIG
36 #define LDAP_OBJ_SAMBA_CONFIG "sambaConfig"
37 #define LDAP_OBJ_SAMBA_SHARE "sambaShare"
38 #define LDAP_OBJ_SAMBA_OPTION "sambaConfigOption"
40 #define LDAP_ATTR_LIST_END 0
41 #define LDAP_ATTR_BOOL 1
42 #define LDAP_ATTR_INTEGER 2
43 #define LDAP_ATTR_STRING 3
44 #define LDAP_ATTR_LIST 4
45 #define LDAP_ATTR_NAME 5
48 struct ldap_config_state
{
49 struct smbldap_state
*smbldap_state
;
53 ATTRIB_MAP_ENTRY option_attr_list
[] = {
54 { LDAP_ATTR_NAME
, "sambaOptionName" },
55 { LDAP_ATTR_LIST
, "sambaListOption" },
56 { LDAP_ATTR_STRING
, "sambaStringOption" },
57 { LDAP_ATTR_INTEGER
, "sambaIntegerOption" },
58 { LDAP_ATTR_BOOL
, "sambaBoolOption" },
59 { LDAP_ATTR_LIST_END
, NULL
}
62 static struct ldap_config_state ldap_state
;
63 static char *config_base_dn
;
65 static NTSTATUS
ldap_config_close(void);
70 start with global, then with others
71 for each section parse all options
74 static NTSTATUS
parse_section(
76 BOOL (*pfunc
)(const char *, const char *))
79 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
80 LDAPMessage
*result
= NULL
;
81 LDAPMessage
*entry
= NULL
;
85 char **attr_list
= NULL
;
89 mem_ctx
= talloc_init("parse_section");
91 /* search for the options */
92 pstr_sprintf(filter
, "objectClass=%s",
93 LDAP_OBJ_SAMBA_OPTION
);
95 DEBUG(0, ("Searching for:[%s]\n", filter
));
97 attr_list
= get_attr_list(option_attr_list
);
98 rc
= smbldap_search(ldap_state
.smbldap_state
,
99 dn
, LDAP_SCOPE_ONELEVEL
,
100 filter
, attr_list
, 0, &result
);
102 if (rc
!= LDAP_SUCCESS
) {
103 DEBUG(0,("parse_section: %s object not found\n", LDAP_OBJ_SAMBA_CONFIG
));
107 count
= ldap_count_entries(ldap_state
.smbldap_state
->ldap_struct
, result
);
108 entry
= ldap_first_entry(ldap_state
.smbldap_state
->ldap_struct
, result
);
112 if (!smbldap_get_single_attribute(ldap_state
.smbldap_state
->ldap_struct
, entry
, "sambaOptionName", option_name
)) {
116 option_value
[0] = '\0';
117 for (o
= 1; option_attr_list
[o
].name
!= NULL
; o
++) {
118 if (smbldap_get_single_attribute(ldap_state
.smbldap_state
->ldap_struct
, entry
, option_attr_list
[o
].name
, option_value
)) {
122 if (option_value
[0] != '\0') {
123 if (!pfunc(option_name
, option_value
)) {
127 DEBUG(0,("parse_section: Missing value for option: %s\n", option_name
));
131 entry
= ldap_next_entry(ldap_state
.smbldap_state
->ldap_struct
, entry
);
137 talloc_destroy(mem_ctx
);
138 free_attr_list(attr_list
);
139 if (result
) ldap_msgfree(result
);
144 /*****************************************************************************
145 load configuration from ldap
146 *****************************************************************************/
148 static NTSTATUS
ldap_config_load(
149 BOOL (*sfunc
)(const char *),
150 BOOL (*pfunc
)(const char *, const char *))
153 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
154 LDAPMessage
*result
= NULL
;
155 LDAPMessage
*entry
= NULL
;
158 char *config_dn
= NULL
;
162 const char *config_attr_list
[] = {"description", NULL
};
163 const char *share_attr_list
[] = {"sambaShareName", "description", NULL
};
167 mem_ctx
= talloc_init("ldap_config_load");
169 /* search for the base config dn */
170 pstr_sprintf(filter
, "objectClass=%s",
171 LDAP_OBJ_SAMBA_CONFIG
);
173 DEBUG(0, ("Searching for:[%s]\n", filter
));
175 rc
= smbldap_search(ldap_state
.smbldap_state
,
176 config_base_dn
, LDAP_SCOPE_SUBTREE
,
177 filter
, config_attr_list
, 0, &result
);
179 if (rc
!= LDAP_SUCCESS
) {
180 DEBUG(0,("ldap_config_load: %s object not found\n", LDAP_OBJ_SAMBA_CONFIG
));
184 count
= ldap_count_entries(ldap_state
.smbldap_state
->ldap_struct
, result
);
186 DEBUG(0,("ldap_config_load: single %s object not found\n", LDAP_OBJ_SAMBA_CONFIG
));
190 if (!(temp
= smbldap_get_dn(ldap_state
.smbldap_state
->ldap_struct
, result
))) {
193 config_dn
= talloc_strdup(mem_ctx
, temp
);
199 entry
= ldap_first_entry(ldap_state
.smbldap_state
->ldap_struct
, result
);
201 if (!smbldap_get_single_attribute(ldap_state
.smbldap_state
->ldap_struct
, entry
, "description", attr_text
)) {
202 DEBUG(0, ("ldap_config_load: no description field in %s object\n", LDAP_OBJ_SAMBA_CONFIG
));
205 if (result
) ldap_msgfree(result
);
206 /* TODO: finish up the last section, see loadparm's lp_load()*/
208 /* retrive the section list */
209 pstr_sprintf(filter
, "objectClass=%s",
210 LDAP_OBJ_SAMBA_SHARE
);
212 DEBUG(0, ("Searching for:[%s]\n", filter
));
214 rc
= smbldap_search(ldap_state
.smbldap_state
,
215 config_dn
, LDAP_SCOPE_SUBTREE
,
216 filter
, share_attr_list
, 0, &result
);
218 if (rc
!= LDAP_SUCCESS
) {
219 DEBUG(0,("ldap_config_load: %s object not found\n", LDAP_OBJ_SAMBA_CONFIG
));
223 count
= ldap_count_entries(ldap_state
.smbldap_state
->ldap_struct
, result
);
224 DEBUG(0, ("config_ldap: Found %d shares\n", count
));
228 share_dn
= talloc(mem_ctx
, (count
+ 1) * sizeof(char *));
229 share_name
= talloc(mem_ctx
, (count
) * sizeof(char *));
230 if (!share_dn
|| !share_name
) {
231 DEBUG(0,("config_ldap: Out of memory!\n"));
234 entry
= ldap_first_entry(ldap_state
.smbldap_state
->ldap_struct
, result
);
237 if (!(temp
= smbldap_get_dn(ldap_state
.smbldap_state
->ldap_struct
, entry
))) {
240 if (!smbldap_get_single_attribute(ldap_state
.smbldap_state
->ldap_struct
, entry
, "sambaShareName", attr_text
)) {
243 share_dn
[i
] = talloc_strdup(mem_ctx
, temp
);
244 share_name
[i
] = talloc_strdup(mem_ctx
, attr_text
);
245 if (!share_dn
[i
] || !share_name
[i
]) {
246 DEBUG(0,("config_ldap: Out of memory!\n"));
250 DEBUG(0, ("config_ldap: Found share [%s] (%s)\n", attr_text
, temp
));
253 entry
= ldap_next_entry(ldap_state
.smbldap_state
->ldap_struct
, entry
);
255 if (entry
&& (count
== i
)) {
256 DEBUG(0, ("Error too many entryes in ldap result\n"));
263 /* parse global section*/
264 if (!sfunc("global")) {
267 if (!NT_STATUS_IS_OK(parse_section(config_dn
, pfunc
))) {
269 } else { /* parse shares */
272 for (i
= 0; share_dn
[i
] != NULL
; i
++) {
273 if (!sfunc(share_name
[i
])) {
276 if (!NT_STATUS_IS_OK(parse_section(share_dn
[i
], pfunc
))) {
283 talloc_destroy(mem_ctx
);
284 if (result
) ldap_msgfree(result
);
289 /*****************************************************************************
290 Initialise config_ldap module
291 *****************************************************************************/
293 static NTSTATUS
ldap_config_init(char *params
)
296 const char *location
;
299 ldap_state
.mem_ctx
= talloc_init("config_ldap");
300 if (!ldap_state
.mem_ctx
) {
301 return NT_STATUS_NO_MEMORY
;
304 /* we assume only location is passed through an inline parameter
305 * other options go via parametrical options */
309 location
= lp_parm_const_string(GLOBAL_SECTION_SNUM
, "config_ldap", "url", "ldap://localhost");
311 DEBUG(0,("config_ldap: location=%s\n", location
));
312 basedn
= lp_parm_const_string(GLOBAL_SECTION_SNUM
, "config_ldap", "basedn", NULL
);
313 if (basedn
) config_base_dn
= smb_xstrdup(basedn
);
315 if (!NT_STATUS_IS_OK(nt_status
=
316 smbldap_init(ldap_state
.mem_ctx
, location
,
317 &ldap_state
.smbldap_state
))) {
318 talloc_destroy(ldap_state
.mem_ctx
);
319 DEBUG(0,("config_ldap: smbldap_init failed!\n"));
326 /*****************************************************************************
328 *****************************************************************************/
330 static NTSTATUS
ldap_config_close(void)
333 smbldap_free_struct(&(ldap_state
).smbldap_state
);
334 talloc_destroy(ldap_state
.mem_ctx
);
336 DEBUG(5,("The connection to the LDAP server was closed\n"));
337 /* maybe free the results here --metze */
342 static struct config_functions functions
= {
348 NTSTATUS
config_ldap_init(void)
350 return smb_register_config(SAMBA_CONFIG_INTERFACE_VERSION
, "ldap", &functions
);