2 Unix SMB/CIFS implementation.
3 ads (active directory) utility library
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Andrew Bartlett 2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* return a ldap dn path from a string, given separators and field name
27 char *ads_build_path(const char *realm, const char *sep, const char *field, int reverse)
36 if (!r || !*r) return r;
39 if (strchr(sep, *p)) numbits++;
42 len = (numbits+1)*(strlen(field)+1) + strlen(r) + 1;
45 strlcpy(ret,field, len);
49 while ((p=strtok(NULL,sep))) {
52 asprintf(&s, "%s%s,%s", field, p, ret);
54 asprintf(&s, "%s,%s%s", ret, field, p);
65 /* return a dn of the form "dc=AA,dc=BB,dc=CC" from a
66 realm of the form AA.BB.CC
69 char *ads_build_dn(const char *realm)
71 return ads_build_path(realm, ".", "dc=", 0);
77 find the ldap server from DNS
79 static char *find_ldap_server(ADS_STRUCT *ads)
85 strcasecmp(ads->workgroup, lp_workgroup()) == 0 &&
86 ldap_domain2hostlist(ads->realm, &list) == LDAP_SUCCESS) {
88 p = strchr(list, ':');
93 /* get desperate, find the domain controller IP */
94 if (resolve_name(ads->workgroup, &ip, 0x1B)) {
95 return strdup(inet_ntoa(ip));
99 if (resolve_name(ads->workgroup, &ip, 0x1C)) {
100 return strdup(inet_ntoa(ip));
108 static char *find_ldap_server(ADS_STRUCT *ads)
110 /* Without LDAP this doesn't make much sense */
117 #define LDAP_PORT 389
121 initialise a ADS_STRUCT, ready for some ads_ ops
123 ADS_STRUCT *ads_init(const char *realm,
124 const char *workgroup,
125 const char *ldap_server,
126 const char *bind_path,
127 const char *password)
131 ads = (ADS_STRUCT *)smb_xmalloc(sizeof(*ads));
135 workgroup = lp_workgroup();
138 ads->realm = realm? strdup(realm) : NULL;
139 ads->workgroup = strdup(workgroup);
140 ads->ldap_server = ldap_server? strdup(ldap_server) : NULL;
141 ads->bind_path = bind_path? strdup(bind_path) : NULL;
142 ads->ldap_port = LDAP_PORT;
143 if (password) ads->password = strdup(password);
146 ads->realm = strdup(lp_realm());
147 if (!ads->realm[0]) {
148 SAFE_FREE(ads->realm);
151 if (!ads->bind_path && ads->realm) {
152 ads->bind_path = ads_build_dn(ads->realm);
154 if (!ads->ldap_server) {
155 if (strcasecmp(ads->workgroup, lp_workgroup()) == 0) {
156 ads->ldap_server = strdup(lp_ads_server());
158 if (!ads->ldap_server || !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;
170 /* a simpler ads_init() interface using all defaults */
171 ADS_STRUCT *ads_init_simple(void)
173 return ads_init(NULL, NULL, NULL, NULL, NULL);
177 free the memory used by the ADS structure initialized with 'ads_init(...)'
179 void ads_destroy(ADS_STRUCT **ads)
183 if ((*ads)->ld) ldap_unbind((*ads)->ld);
185 SAFE_FREE((*ads)->realm);
186 SAFE_FREE((*ads)->ldap_server);
187 SAFE_FREE((*ads)->ldap_server_name);
188 SAFE_FREE((*ads)->kdc_server);
189 SAFE_FREE((*ads)->bind_path);
190 SAFE_FREE((*ads)->password);
191 SAFE_FREE((*ads)->user_name);