2 Unix SMB/Netbios implementation.
4 ads (active directory) utility library
5 Copyright (C) Andrew Tridgell 2001
6 Copyright (C) Andrew Bartlett 2001
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 static char *ads_build_dn(const char *realm)
34 if (!r || !*r) return r;
37 if (*p == '.') numdots++;
40 len = (numdots+1)*4 + strlen(r) + 1;
43 strlcpy(ret,"dc=", len);
47 while ((p=strtok(NULL,"."))) {
48 strlcat(ret,",dc=", len);
60 get the default relm from krb5.conf
62 static char *get_default_realm(ADS_STRUCT *ads)
68 ret = krb5_init_context(&context);
70 DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
74 ret = krb5_get_default_realm(context, &realm);
76 DEBUG(1,("krb5_get_default_realm failed (%s)\n", error_message(ret)));
77 krb5_free_context(context);
80 DEBUG(5,("krb5_get_default_realm got (%s)\n", realm));
82 krb5_free_context(context);
88 static char *get_default_realm(ADS_STRUCT *ads)
90 /* We can't do this if we don't have krb5,
91 but save linking nightmares */
92 DEBUG(5,("get_default_realm: not compiled with krb5.\n"));
100 find the ldap server from DNS
102 static char *find_ldap_server(ADS_STRUCT *ads)
106 if (ldap_domain2hostlist(ads->realm, &list) == LDAP_SUCCESS) {
108 p = strchr(list, ':');
118 static char *find_ldap_server(ADS_STRUCT *ads)
120 /* Without LDAP this doesn't make much sense */
127 #define LDAP_PORT 389
131 initialise a ADS_STRUCT, ready for some ads_ ops
133 ADS_STRUCT *ads_init(const char *realm,
134 const char *ldap_server,
135 const char *bind_path)
139 ads = (ADS_STRUCT *)smb_xmalloc(sizeof(*ads));
140 memset(ads, 0, sizeof(*ads));
142 ads->realm = realm? strdup(realm) : NULL;
143 ads->ldap_server = ldap_server? strdup(ldap_server) : NULL;
144 ads->bind_path = bind_path? strdup(bind_path) : NULL;
145 ads->ldap_port = LDAP_PORT;
148 ads->realm = lp_realm();
149 if (!ads->realm[0]) {
150 ads->realm = get_default_realm(ads);
153 if (!ads->bind_path) {
154 ads->bind_path = ads_build_dn(ads->realm);
156 if (!ads->ldap_server) {
157 ads->ldap_server = lp_ads_server();
158 if (!ads->ldap_server[0]) {
159 ads->ldap_server = find_ldap_server(ads);
162 if (!ads->kdc_server) {
163 /* assume its the same as LDAP */
164 ads->kdc_server = ads->ldap_server? strdup(ads->ldap_server) : NULL;
171 free the memory used by the ADS structure initialized with 'ads_init(...)'
173 void ads_destroy(ADS_STRUCT **ads)
175 if (False && (ads) && (*ads)) {
176 if ((*ads)->ld) ldap_unbind((*ads)->ld);
177 SAFE_FREE((*ads)->realm);
178 SAFE_FREE((*ads)->ldap_server);
179 SAFE_FREE((*ads)->kdc_server);
180 SAFE_FREE((*ads)->bind_path);