r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / libads / ads_struct.c
blobb01e0879ef9cab970a7537b33064abd87656ccb7
1 /*
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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
23 /* return a ldap dn path from a string, given separators and field name
24 caller must free
26 char *ads_build_path(const char *realm, const char *sep, const char *field, int reverse)
28 char *p, *r;
29 int numbits = 0;
30 char *ret;
31 int len;
33 r = SMB_STRDUP(realm);
35 if (!r || !*r)
36 return r;
38 for (p=r; *p; p++)
39 if (strchr(sep, *p))
40 numbits++;
42 len = (numbits+1)*(strlen(field)+1) + strlen(r) + 1;
44 ret = (char *)SMB_MALLOC(len);
45 if (!ret)
46 return NULL;
48 strlcpy(ret,field, len);
49 p=strtok(r,sep);
50 if (p) {
51 strlcat(ret, p, len);
53 while ((p=strtok(NULL,sep))) {
54 char *s;
55 if (reverse)
56 asprintf(&s, "%s%s,%s", field, p, ret);
57 else
58 asprintf(&s, "%s,%s%s", ret, field, p);
59 free(ret);
60 ret = s;
64 free(r);
65 return ret;
68 /* return a dn of the form "dc=AA,dc=BB,dc=CC" from a
69 realm of the form AA.BB.CC
70 caller must free
72 char *ads_build_dn(const char *realm)
74 return ads_build_path(realm, ".", "dc=", 0);
77 /* return a DNS name in the for aa.bb.cc from the DN
78 "dc=AA,dc=BB,dc=CC". caller must free
80 char *ads_build_domain(const char *dn)
82 char *dnsdomain = NULL;
84 /* result should always be shorter than the DN */
86 if ( (dnsdomain = SMB_STRDUP( dn )) == NULL ) {
87 DEBUG(0,("ads_build_domain: malloc() failed!\n"));
88 return NULL;
91 strlower_m( dnsdomain );
92 all_string_sub( dnsdomain, "dc=", "", 0);
93 all_string_sub( dnsdomain, ",", ".", 0 );
95 return dnsdomain;
100 #ifndef LDAP_PORT
101 #define LDAP_PORT 389
102 #endif
105 initialise a ADS_STRUCT, ready for some ads_ ops
107 ADS_STRUCT *ads_init(const char *realm,
108 const char *workgroup,
109 const char *ldap_server)
111 ADS_STRUCT *ads;
113 ads = SMB_XMALLOC_P(ADS_STRUCT);
114 ZERO_STRUCTP(ads);
116 ads->server.realm = realm? SMB_STRDUP(realm) : NULL;
117 ads->server.workgroup = workgroup ? SMB_STRDUP(workgroup) : NULL;
118 ads->server.ldap_server = ldap_server? SMB_STRDUP(ldap_server) : NULL;
120 /* we need to know if this is a foreign realm */
121 if (realm && *realm && !strequal(lp_realm(), realm)) {
122 ads->server.foreign = 1;
124 if (workgroup && *workgroup && !strequal(lp_workgroup(), workgroup)) {
125 ads->server.foreign = 1;
128 /* the caller will own the memory by default */
129 ads->is_mine = 1;
131 return ads;
135 free the memory used by the ADS structure initialized with 'ads_init(...)'
137 void ads_destroy(ADS_STRUCT **ads)
139 if (ads && *ads) {
140 BOOL is_mine;
142 is_mine = (*ads)->is_mine;
143 #if HAVE_LDAP
144 if ((*ads)->ld) {
145 ldap_unbind((*ads)->ld);
147 #endif
148 SAFE_FREE((*ads)->server.realm);
149 SAFE_FREE((*ads)->server.workgroup);
150 SAFE_FREE((*ads)->server.ldap_server);
152 SAFE_FREE((*ads)->auth.realm);
153 SAFE_FREE((*ads)->auth.password);
154 SAFE_FREE((*ads)->auth.user_name);
155 SAFE_FREE((*ads)->auth.kdc_server);
157 SAFE_FREE((*ads)->config.realm);
158 SAFE_FREE((*ads)->config.bind_path);
159 SAFE_FREE((*ads)->config.ldap_server_name);
160 SAFE_FREE((*ads)->config.server_site_name);
161 SAFE_FREE((*ads)->config.client_site_name);
163 ZERO_STRUCTP(*ads);
165 if ( is_mine )
166 SAFE_FREE(*ads);