043a1b2124749ae64428eded918fa9e7f3e633bf
[bbaumbach/samba-autobuild/.git] / source3 / libads / ads_struct.c
1 /* 
2    Unix SMB/CIFS implementation.
3    ads (active directory) utility library
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Andrew Bartlett 2001
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22 #include "ads.h"
23
24 /* return a ldap dn path from a string, given separators and field name
25    caller must free
26 */
27 char *ads_build_path(const char *realm, const char *sep, const char *field, int reverse)
28 {
29         char *p, *r;
30         int numbits = 0;
31         char *ret;
32         int len;
33         char *saveptr;
34
35         r = SMB_STRDUP(realm);
36
37         if (!r || !*r) {
38                 return r;
39         }
40
41         for (p=r; *p; p++) {
42                 if (strchr(sep, *p)) {
43                         numbits++;
44                 }
45         }
46
47         len = (numbits+1)*(strlen(field)+1) + strlen(r) + 1;
48
49         ret = (char *)SMB_MALLOC(len);
50         if (!ret) {
51                 free(r);
52                 return NULL;
53         }
54
55         if (strlcpy(ret,field, len) >= len) {
56                 /* Truncate ! */
57                 free(r);
58                 free(ret);
59                 return NULL;
60         }
61         p=strtok_r(r, sep, &saveptr);
62         if (p) {
63                 if (strlcat(ret, p, len) >= len) {
64                         free(r);
65                         free(ret);
66                         return NULL;
67                 }
68
69                 while ((p=strtok_r(NULL, sep, &saveptr)) != NULL) {
70                         int retval;
71                         char *s = NULL;
72                         if (reverse)
73                                 retval = asprintf(&s, "%s%s,%s", field, p, ret);
74                         else
75                                 retval = asprintf(&s, "%s,%s%s", ret, field, p);
76                         free(ret);
77                         if (retval == -1) {
78                                 free(r);
79                                 return NULL;
80                         }
81                         ret = SMB_STRDUP(s);
82                         free(s);
83                 }
84         }
85
86         free(r);
87         return ret;
88 }
89
90 /* return a dn of the form "dc=AA,dc=BB,dc=CC" from a 
91    realm of the form AA.BB.CC 
92    caller must free
93 */
94 char *ads_build_dn(const char *realm)
95 {
96         return ads_build_path(realm, ".", "dc=", 0);
97 }
98
99 /* return a DNS name in the for aa.bb.cc from the DN  
100    "dc=AA,dc=BB,dc=CC".  caller must free
101 */
102 char *ads_build_domain(const char *dn)
103 {
104         char *dnsdomain = NULL;
105
106         /* result should always be shorter than the DN */
107
108         if ( (dnsdomain = SMB_STRDUP( dn )) == NULL ) {
109                 DEBUG(0,("ads_build_domain: malloc() failed!\n"));              
110                 return NULL;            
111         }       
112
113         if (!strlower_m( dnsdomain )) {
114                 SAFE_FREE(dnsdomain);
115                 return NULL;
116         }
117
118         all_string_sub( dnsdomain, "dc=", "", 0);
119         all_string_sub( dnsdomain, ",", ".", 0 );
120
121         return dnsdomain;       
122 }
123
124
125
126 #ifndef LDAP_PORT
127 #define LDAP_PORT 389
128 #endif
129
130 /*
131   initialise a ADS_STRUCT, ready for some ads_ ops
132 */
133 ADS_STRUCT *ads_init(const char *realm, 
134                      const char *workgroup,
135                      const char *ldap_server,
136                      enum ads_sasl_state_e sasl_state)
137 {
138         ADS_STRUCT *ads;
139         int wrap_flags;
140
141         ads = SMB_XMALLOC_P(ADS_STRUCT);
142         ZERO_STRUCTP(ads);
143
144         ads->server.realm = realm? SMB_STRDUP(realm) : NULL;
145         ads->server.workgroup = workgroup ? SMB_STRDUP(workgroup) : NULL;
146         ads->server.ldap_server = ldap_server? SMB_STRDUP(ldap_server) : NULL;
147
148         /* the caller will own the memory by default */
149         ads->is_mine = 1;
150
151         wrap_flags = lp_client_ldap_sasl_wrapping();
152         if (wrap_flags == -1) {
153                 wrap_flags = 0;
154         }
155
156         switch (sasl_state) {
157         case ADS_SASL_PLAIN:
158                 break;
159         case ADS_SASL_SIGN:
160                 wrap_flags |= ADS_AUTH_SASL_SIGN;
161                 break;
162         case ADS_SASL_SEAL:
163                 wrap_flags |= ADS_AUTH_SASL_SEAL;
164                 break;
165         }
166
167         ads->auth.flags = wrap_flags;
168
169         /* Start with the configured page size when the connection is new,
170          * we will drop it by half we get a timeout.   */
171         ads->config.ldap_page_size     = lp_ldap_page_size();
172
173         return ads;
174 }
175
176 /****************************************************************
177 ****************************************************************/
178
179 bool ads_set_sasl_wrap_flags(ADS_STRUCT *ads, int flags)
180 {
181         if (!ads) {
182                 return false;
183         }
184
185         ads->auth.flags = flags;
186
187         return true;
188 }
189
190 /*
191   free the memory used by the ADS structure initialized with 'ads_init(...)'
192 */
193 void ads_destroy(ADS_STRUCT **ads)
194 {
195         if (ads && *ads) {
196                 bool is_mine;
197
198                 is_mine = (*ads)->is_mine;
199 #ifdef HAVE_LDAP
200                 ads_disconnect(*ads);
201 #endif
202                 SAFE_FREE((*ads)->server.realm);
203                 SAFE_FREE((*ads)->server.workgroup);
204                 SAFE_FREE((*ads)->server.ldap_server);
205
206                 SAFE_FREE((*ads)->auth.realm);
207                 SAFE_FREE((*ads)->auth.password);
208                 SAFE_FREE((*ads)->auth.user_name);
209                 SAFE_FREE((*ads)->auth.kdc_server);
210                 SAFE_FREE((*ads)->auth.ccache_name);
211
212                 SAFE_FREE((*ads)->config.realm);
213                 SAFE_FREE((*ads)->config.bind_path);
214                 SAFE_FREE((*ads)->config.ldap_server_name);
215                 SAFE_FREE((*ads)->config.server_site_name);
216                 SAFE_FREE((*ads)->config.client_site_name);
217                 SAFE_FREE((*ads)->config.schema_path);
218                 SAFE_FREE((*ads)->config.config_path);
219
220                 ZERO_STRUCTP(*ads);
221
222                 if ( is_mine )
223                         SAFE_FREE(*ads);
224         }
225 }