test: not only pretend to call smbconftort - really do it :-}
[Samba.git] / source / libads / ads_struct.c
blob8cc2f1215e24fe8a616b92b8addc8b6bff64518b
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;
32 char *saveptr;
34 r = SMB_STRDUP(realm);
36 if (!r || !*r) {
37 return r;
40 for (p=r; *p; p++) {
41 if (strchr(sep, *p)) {
42 numbits++;
46 len = (numbits+1)*(strlen(field)+1) + strlen(r) + 1;
48 ret = (char *)SMB_MALLOC(len);
49 if (!ret) {
50 free(r);
51 return NULL;
54 strlcpy(ret,field, len);
55 p=strtok_r(r, sep, &saveptr);
56 if (p) {
57 strlcat(ret, p, len);
59 while ((p=strtok_r(NULL, sep, &saveptr)) != NULL) {
60 char *s;
61 if (reverse)
62 asprintf(&s, "%s%s,%s", field, p, ret);
63 else
64 asprintf(&s, "%s,%s%s", ret, field, p);
65 free(ret);
66 ret = SMB_STRDUP(s);
67 free(s);
71 free(r);
72 return ret;
75 /* return a dn of the form "dc=AA,dc=BB,dc=CC" from a
76 realm of the form AA.BB.CC
77 caller must free
79 char *ads_build_dn(const char *realm)
81 return ads_build_path(realm, ".", "dc=", 0);
84 /* return a DNS name in the for aa.bb.cc from the DN
85 "dc=AA,dc=BB,dc=CC". caller must free
87 char *ads_build_domain(const char *dn)
89 char *dnsdomain = NULL;
91 /* result should always be shorter than the DN */
93 if ( (dnsdomain = SMB_STRDUP( dn )) == NULL ) {
94 DEBUG(0,("ads_build_domain: malloc() failed!\n"));
95 return NULL;
98 strlower_m( dnsdomain );
99 all_string_sub( dnsdomain, "dc=", "", 0);
100 all_string_sub( dnsdomain, ",", ".", 0 );
102 return dnsdomain;
107 #ifndef LDAP_PORT
108 #define LDAP_PORT 389
109 #endif
112 initialise a ADS_STRUCT, ready for some ads_ ops
114 ADS_STRUCT *ads_init(const char *realm,
115 const char *workgroup,
116 const char *ldap_server)
118 ADS_STRUCT *ads;
119 int wrap_flags;
121 ads = SMB_XMALLOC_P(ADS_STRUCT);
122 ZERO_STRUCTP(ads);
124 ads->server.realm = realm? SMB_STRDUP(realm) : NULL;
125 ads->server.workgroup = workgroup ? SMB_STRDUP(workgroup) : NULL;
126 ads->server.ldap_server = ldap_server? SMB_STRDUP(ldap_server) : NULL;
128 /* we need to know if this is a foreign realm */
129 if (realm && *realm && !strequal(lp_realm(), realm)) {
130 ads->server.foreign = 1;
132 if (workgroup && *workgroup && !strequal(lp_workgroup(), workgroup)) {
133 ads->server.foreign = 1;
136 /* the caller will own the memory by default */
137 ads->is_mine = 1;
139 wrap_flags = lp_client_ldap_sasl_wrapping();
140 if (wrap_flags == -1) {
141 wrap_flags = 0;
144 ads->auth.flags = wrap_flags;
146 return ads;
150 free the memory used by the ADS structure initialized with 'ads_init(...)'
152 void ads_destroy(ADS_STRUCT **ads)
154 if (ads && *ads) {
155 bool is_mine;
157 is_mine = (*ads)->is_mine;
158 #if HAVE_LDAP
159 ads_disconnect(*ads);
160 #endif
161 SAFE_FREE((*ads)->server.realm);
162 SAFE_FREE((*ads)->server.workgroup);
163 SAFE_FREE((*ads)->server.ldap_server);
165 SAFE_FREE((*ads)->auth.realm);
166 SAFE_FREE((*ads)->auth.password);
167 SAFE_FREE((*ads)->auth.user_name);
168 SAFE_FREE((*ads)->auth.kdc_server);
170 SAFE_FREE((*ads)->config.realm);
171 SAFE_FREE((*ads)->config.bind_path);
172 SAFE_FREE((*ads)->config.ldap_server_name);
173 SAFE_FREE((*ads)->config.server_site_name);
174 SAFE_FREE((*ads)->config.client_site_name);
175 SAFE_FREE((*ads)->config.schema_path);
176 SAFE_FREE((*ads)->config.config_path);
178 ZERO_STRUCTP(*ads);
180 if ( is_mine )
181 SAFE_FREE(*ads);