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);
80 initialise a ADS_STRUCT, ready for some ads_ ops
82 ADS_STRUCT *ads_init(const char *realm,
83 const char *workgroup,
84 const char *ldap_server)
88 ads = (ADS_STRUCT *)smb_xmalloc(sizeof(*ads));
91 ads->server.realm = realm? strdup(realm) : NULL;
92 ads->server.workgroup = workgroup ? strdup(workgroup) : NULL;
93 ads->server.ldap_server = ldap_server? strdup(ldap_server) : NULL;
95 /* we need to know if this is a foreign realm to know if we can
96 use lp_ads_server() */
97 if (realm && strcasecmp(lp_realm(), realm) != 0) {
98 ads->server.foreign = 1;
100 if (workgroup && strcasecmp(lp_workgroup(), workgroup) != 0) {
101 ads->server.foreign = 1;
107 /* a simpler ads_init() interface using all defaults */
108 ADS_STRUCT *ads_init_simple(void)
110 return ads_init(NULL, NULL, NULL);
114 free the memory used by the ADS structure initialized with 'ads_init(...)'
116 void ads_destroy(ADS_STRUCT **ads)
120 if ((*ads)->ld) ldap_unbind((*ads)->ld);
122 SAFE_FREE((*ads)->server.realm);
123 SAFE_FREE((*ads)->server.workgroup);
124 SAFE_FREE((*ads)->server.ldap_server);
125 SAFE_FREE((*ads)->server.ldap_uri);
127 SAFE_FREE((*ads)->auth.realm);
128 SAFE_FREE((*ads)->auth.password);
129 SAFE_FREE((*ads)->auth.user_name);
130 SAFE_FREE((*ads)->auth.kdc_server);
132 SAFE_FREE((*ads)->config.realm);
133 SAFE_FREE((*ads)->config.bind_path);
134 SAFE_FREE((*ads)->config.ldap_server_name);