s3-spoolssd: propagate SIGHUP to children
[Samba.git] / source3 / libads / ads_struct.c
blobb6c8e995ed452a477ce936b24b5b47ca917cfbe9
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"
22 #include "ads.h"
24 /* return a ldap dn path from a string, given separators and field name
25 caller must free
27 char *ads_build_path(const char *realm, const char *sep, const char *field, int reverse)
29 char *p, *r;
30 int numbits = 0;
31 char *ret;
32 int len;
33 char *saveptr;
35 r = SMB_STRDUP(realm);
37 if (!r || !*r) {
38 return r;
41 for (p=r; *p; p++) {
42 if (strchr(sep, *p)) {
43 numbits++;
47 len = (numbits+1)*(strlen(field)+1) + strlen(r) + 1;
49 ret = (char *)SMB_MALLOC(len);
50 if (!ret) {
51 free(r);
52 return NULL;
55 strlcpy(ret,field, len);
56 p=strtok_r(r, sep, &saveptr);
57 if (p) {
58 strlcat(ret, p, len);
60 while ((p=strtok_r(NULL, sep, &saveptr)) != NULL) {
61 int retval;
62 char *s = NULL;
63 if (reverse)
64 retval = asprintf(&s, "%s%s,%s", field, p, ret);
65 else
66 retval = asprintf(&s, "%s,%s%s", ret, field, p);
67 free(ret);
68 if (retval == -1) {
69 free(r);
70 return NULL;
72 ret = SMB_STRDUP(s);
73 free(s);
77 free(r);
78 return ret;
81 /* return a dn of the form "dc=AA,dc=BB,dc=CC" from a
82 realm of the form AA.BB.CC
83 caller must free
85 char *ads_build_dn(const char *realm)
87 return ads_build_path(realm, ".", "dc=", 0);
90 /* return a DNS name in the for aa.bb.cc from the DN
91 "dc=AA,dc=BB,dc=CC". caller must free
93 char *ads_build_domain(const char *dn)
95 char *dnsdomain = NULL;
97 /* result should always be shorter than the DN */
99 if ( (dnsdomain = SMB_STRDUP( dn )) == NULL ) {
100 DEBUG(0,("ads_build_domain: malloc() failed!\n"));
101 return NULL;
104 strlower_m( dnsdomain );
105 all_string_sub( dnsdomain, "dc=", "", 0);
106 all_string_sub( dnsdomain, ",", ".", 0 );
108 return dnsdomain;
113 #ifndef LDAP_PORT
114 #define LDAP_PORT 389
115 #endif
118 initialise a ADS_STRUCT, ready for some ads_ ops
120 ADS_STRUCT *ads_init(const char *realm,
121 const char *workgroup,
122 const char *ldap_server)
124 ADS_STRUCT *ads;
125 int wrap_flags;
127 ads = SMB_XMALLOC_P(ADS_STRUCT);
128 ZERO_STRUCTP(ads);
130 ads->server.realm = realm? SMB_STRDUP(realm) : NULL;
131 ads->server.workgroup = workgroup ? SMB_STRDUP(workgroup) : NULL;
132 ads->server.ldap_server = ldap_server? SMB_STRDUP(ldap_server) : NULL;
134 /* we need to know if this is a foreign realm */
135 if (realm && *realm && !strequal(lp_realm(), realm)) {
136 ads->server.foreign = 1;
138 if (workgroup && *workgroup && !strequal(lp_workgroup(), workgroup)) {
139 ads->server.foreign = 1;
142 /* the caller will own the memory by default */
143 ads->is_mine = 1;
145 wrap_flags = lp_client_ldap_sasl_wrapping();
146 if (wrap_flags == -1) {
147 wrap_flags = 0;
150 ads->auth.flags = wrap_flags;
152 return ads;
155 /****************************************************************
156 ****************************************************************/
158 bool ads_set_sasl_wrap_flags(ADS_STRUCT *ads, int flags)
160 if (!ads) {
161 return false;
164 ads->auth.flags = flags;
166 return true;
170 free the memory used by the ADS structure initialized with 'ads_init(...)'
172 void ads_destroy(ADS_STRUCT **ads)
174 if (ads && *ads) {
175 bool is_mine;
177 is_mine = (*ads)->is_mine;
178 #if HAVE_LDAP
179 ads_disconnect(*ads);
180 #endif
181 SAFE_FREE((*ads)->server.realm);
182 SAFE_FREE((*ads)->server.workgroup);
183 SAFE_FREE((*ads)->server.ldap_server);
185 SAFE_FREE((*ads)->auth.realm);
186 SAFE_FREE((*ads)->auth.password);
187 SAFE_FREE((*ads)->auth.user_name);
188 SAFE_FREE((*ads)->auth.kdc_server);
190 SAFE_FREE((*ads)->config.realm);
191 SAFE_FREE((*ads)->config.bind_path);
192 SAFE_FREE((*ads)->config.ldap_server_name);
193 SAFE_FREE((*ads)->config.server_site_name);
194 SAFE_FREE((*ads)->config.client_site_name);
195 SAFE_FREE((*ads)->config.schema_path);
196 SAFE_FREE((*ads)->config.config_path);
198 ZERO_STRUCTP(*ads);
200 if ( is_mine )
201 SAFE_FREE(*ads);