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 (!ads->realm) return NULL;
108 if (ldap_domain2hostlist(ads->realm, &list) == LDAP_SUCCESS) {
110 p = strchr(list, ':');
120 static char *find_ldap_server(ADS_STRUCT *ads)
122 /* Without LDAP this doesn't make much sense */
129 #define LDAP_PORT 389
133 initialise a ADS_STRUCT, ready for some ads_ ops
135 ADS_STRUCT *ads_init(const char *realm,
136 const char *ldap_server,
137 const char *bind_path,
138 const char *password)
142 ads = (ADS_STRUCT *)smb_xmalloc(sizeof(*ads));
145 ads->realm = realm? strdup(realm) : NULL;
146 ads->ldap_server = ldap_server? strdup(ldap_server) : NULL;
147 ads->bind_path = bind_path? strdup(bind_path) : NULL;
148 ads->ldap_port = LDAP_PORT;
149 if (password) ads->password = strdup(password);
152 ads->realm = strdup(lp_realm());
153 if (!ads->realm[0]) {
154 ads->realm = get_default_realm(ads);
156 if (!ads->realm) ads->realm = strdup("");
158 if (!ads->bind_path) {
159 ads->bind_path = ads_build_dn(ads->realm);
161 if (!ads->ldap_server) {
162 ads->ldap_server = strdup(lp_ads_server());
163 if (!ads->ldap_server[0]) {
164 ads->ldap_server = find_ldap_server(ads);
167 if (!ads->kdc_server) {
168 /* assume its the same as LDAP */
169 ads->kdc_server = ads->ldap_server? strdup(ads->ldap_server) : NULL;
176 free the memory used by the ADS structure initialized with 'ads_init(...)'
178 void ads_destroy(ADS_STRUCT **ads)
181 if ((*ads)->ld) ldap_unbind((*ads)->ld);
182 SAFE_FREE((*ads)->realm);
183 SAFE_FREE((*ads)->ldap_server);
184 SAFE_FREE((*ads)->kdc_server);
185 SAFE_FREE((*ads)->bind_path);
186 SAFE_FREE((*ads)->password);