Merge branch 'v3-2-test' of ssh://git.samba.org/data/git/samba into v3-2-simo
[samba.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
23 /* return a ldap dn path from a string, given separators and field name
24    caller must free
25 */
26 char *ads_build_path(const char *realm, const char *sep, const char *field, int reverse)
27 {
28         char *p, *r;
29         int numbits = 0;
30         char *ret;
31         int len;
32         
33         r = SMB_STRDUP(realm);
34
35         if (!r || !*r) {
36                 return r;
37         }
38
39         for (p=r; *p; p++) {
40                 if (strchr(sep, *p)) {
41                         numbits++;
42                 }
43         }
44
45         len = (numbits+1)*(strlen(field)+1) + strlen(r) + 1;
46
47         ret = (char *)SMB_MALLOC(len);
48         if (!ret) {
49                 free(r);
50                 return NULL;
51         }
52
53         strlcpy(ret,field, len);
54         p=strtok(r,sep); 
55         if (p) {
56                 strlcat(ret, p, len);
57         
58                 while ((p=strtok(NULL,sep))) {
59                         char *s;
60                         if (reverse)
61                                 asprintf(&s, "%s%s,%s", field, p, ret);
62                         else
63                                 asprintf(&s, "%s,%s%s", ret, field, p);
64                         free(ret);
65                         ret = SMB_STRDUP(s);
66                         free(s);
67                 }
68         }
69
70         free(r);
71         return ret;
72 }
73
74 /* return a dn of the form "dc=AA,dc=BB,dc=CC" from a 
75    realm of the form AA.BB.CC 
76    caller must free
77 */
78 char *ads_build_dn(const char *realm)
79 {
80         return ads_build_path(realm, ".", "dc=", 0);
81 }
82
83 /* return a DNS name in the for aa.bb.cc from the DN  
84    "dc=AA,dc=BB,dc=CC".  caller must free
85 */
86 char *ads_build_domain(const char *dn)
87 {
88         char *dnsdomain = NULL;
89         
90         /* result should always be shorter than the DN */
91
92         if ( (dnsdomain = SMB_STRDUP( dn )) == NULL ) {
93                 DEBUG(0,("ads_build_domain: malloc() failed!\n"));              
94                 return NULL;            
95         }       
96
97         strlower_m( dnsdomain );        
98         all_string_sub( dnsdomain, "dc=", "", 0);
99         all_string_sub( dnsdomain, ",", ".", 0 );
100
101         return dnsdomain;       
102 }
103
104
105
106 #ifndef LDAP_PORT
107 #define LDAP_PORT 389
108 #endif
109
110 /*
111   initialise a ADS_STRUCT, ready for some ads_ ops
112 */
113 ADS_STRUCT *ads_init(const char *realm, 
114                      const char *workgroup,
115                      const char *ldap_server)
116 {
117         ADS_STRUCT *ads;
118         int wrap_flags;
119         
120         ads = SMB_XMALLOC_P(ADS_STRUCT);
121         ZERO_STRUCTP(ads);
122         
123         ads->server.realm = realm? SMB_STRDUP(realm) : NULL;
124         ads->server.workgroup = workgroup ? SMB_STRDUP(workgroup) : NULL;
125         ads->server.ldap_server = ldap_server? SMB_STRDUP(ldap_server) : NULL;
126
127         /* we need to know if this is a foreign realm */
128         if (realm && *realm && !strequal(lp_realm(), realm)) {
129                 ads->server.foreign = 1;
130         }
131         if (workgroup && *workgroup && !strequal(lp_workgroup(), workgroup)) {
132                 ads->server.foreign = 1;
133         }
134
135         /* the caller will own the memory by default */
136         ads->is_mine = 1;
137
138         wrap_flags = lp_client_ldap_sasl_wrapping();
139         if (wrap_flags == -1) {
140                 wrap_flags = 0;
141         }
142
143         ads->auth.flags = wrap_flags;
144
145         return ads;
146 }
147
148 /*
149   free the memory used by the ADS structure initialized with 'ads_init(...)'
150 */
151 void ads_destroy(ADS_STRUCT **ads)
152 {
153         if (ads && *ads) {
154                 bool is_mine;
155
156                 is_mine = (*ads)->is_mine;
157 #if HAVE_LDAP
158                 ads_disconnect(*ads);
159 #endif
160                 SAFE_FREE((*ads)->server.realm);
161                 SAFE_FREE((*ads)->server.workgroup);
162                 SAFE_FREE((*ads)->server.ldap_server);
163
164                 SAFE_FREE((*ads)->auth.realm);
165                 SAFE_FREE((*ads)->auth.password);
166                 SAFE_FREE((*ads)->auth.user_name);
167                 SAFE_FREE((*ads)->auth.kdc_server);
168
169                 SAFE_FREE((*ads)->config.realm);
170                 SAFE_FREE((*ads)->config.bind_path);
171                 SAFE_FREE((*ads)->config.ldap_server_name);
172                 SAFE_FREE((*ads)->config.server_site_name);
173                 SAFE_FREE((*ads)->config.client_site_name);
174                 SAFE_FREE((*ads)->config.schema_path);
175                 SAFE_FREE((*ads)->config.config_path);
176                 
177                 ZERO_STRUCTP(*ads);
178
179                 if ( is_mine )
180                         SAFE_FREE(*ads);
181         }
182 }