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/>.
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
)
35 r
= SMB_STRDUP(realm
);
42 if (strchr(sep
, *p
)) {
47 len
= (numbits
+1)*(strlen(field
)+1) + strlen(r
) + 1;
49 ret
= (char *)SMB_MALLOC(len
);
55 if (strlcpy(ret
,field
, len
) >= len
) {
60 p
=strtok_r(r
, sep
, &saveptr
);
62 if (strlcat(ret
, p
, len
) >= len
) {
67 while ((p
=strtok_r(NULL
, sep
, &saveptr
)) != NULL
) {
71 retval
= asprintf(&s
, "%s%s,%s", field
, p
, ret
);
73 retval
= asprintf(&s
, "%s,%s%s", ret
, field
, p
);
88 /* return a dn of the form "dc=AA,dc=BB,dc=CC" from a
89 realm of the form AA.BB.CC
92 char *ads_build_dn(const char *realm
)
94 return ads_build_path(realm
, ".", "dc=", 0);
97 /* return a DNS name in the for aa.bb.cc from the DN
98 "dc=AA,dc=BB,dc=CC". caller must free
100 char *ads_build_domain(const char *dn
)
102 char *dnsdomain
= NULL
;
104 /* result should always be shorter than the DN */
106 if ( (dnsdomain
= SMB_STRDUP( dn
)) == NULL
) {
107 DEBUG(0,("ads_build_domain: malloc() failed!\n"));
111 if (!strlower_m( dnsdomain
)) {
112 SAFE_FREE(dnsdomain
);
116 all_string_sub( dnsdomain
, "dc=", "", 0);
117 all_string_sub( dnsdomain
, ",", ".", 0 );
125 #define LDAP_PORT 389
129 initialise a ADS_STRUCT, ready for some ads_ ops
131 ADS_STRUCT
*ads_init(const char *realm
,
132 const char *workgroup
,
133 const char *ldap_server
)
138 ads
= SMB_XMALLOC_P(ADS_STRUCT
);
141 ads
->server
.realm
= realm
? SMB_STRDUP(realm
) : NULL
;
142 ads
->server
.workgroup
= workgroup
? SMB_STRDUP(workgroup
) : NULL
;
143 ads
->server
.ldap_server
= ldap_server
? SMB_STRDUP(ldap_server
) : NULL
;
145 /* we need to know if this is a foreign realm */
146 if (realm
&& *realm
&& !strequal(lp_realm(), realm
)) {
147 ads
->server
.foreign
= 1;
149 if (workgroup
&& *workgroup
&& !strequal(lp_workgroup(), workgroup
)) {
150 ads
->server
.foreign
= 1;
153 /* the caller will own the memory by default */
156 wrap_flags
= lp_client_ldap_sasl_wrapping();
157 if (wrap_flags
== -1) {
161 ads
->auth
.flags
= wrap_flags
;
163 /* Start with a page size of 1000 when the connection is new,
164 * we will drop it by half we get a timeout. */
165 ads
->config
.ldap_page_size
= 1000;
170 /****************************************************************
171 ****************************************************************/
173 bool ads_set_sasl_wrap_flags(ADS_STRUCT
*ads
, int flags
)
179 ads
->auth
.flags
= flags
;
185 free the memory used by the ADS structure initialized with 'ads_init(...)'
187 void ads_destroy(ADS_STRUCT
**ads
)
192 is_mine
= (*ads
)->is_mine
;
194 ads_disconnect(*ads
);
196 SAFE_FREE((*ads
)->server
.realm
);
197 SAFE_FREE((*ads
)->server
.workgroup
);
198 SAFE_FREE((*ads
)->server
.ldap_server
);
200 SAFE_FREE((*ads
)->auth
.realm
);
201 SAFE_FREE((*ads
)->auth
.password
);
202 SAFE_FREE((*ads
)->auth
.user_name
);
203 SAFE_FREE((*ads
)->auth
.kdc_server
);
204 SAFE_FREE((*ads
)->auth
.ccache_name
);
206 SAFE_FREE((*ads
)->config
.realm
);
207 SAFE_FREE((*ads
)->config
.bind_path
);
208 SAFE_FREE((*ads
)->config
.ldap_server_name
);
209 SAFE_FREE((*ads
)->config
.server_site_name
);
210 SAFE_FREE((*ads
)->config
.client_site_name
);
211 SAFE_FREE((*ads
)->config
.schema_path
);
212 SAFE_FREE((*ads
)->config
.config_path
);