r23779: Change from v2 or later to v3 or later.
[abartlet/samba.git/.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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.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         
34         r = SMB_STRDUP(realm);
35
36         if (!r || !*r)
37                 return r;
38
39         for (p=r; *p; p++)
40                 if (strchr(sep, *p))
41                         numbits++;
42
43         len = (numbits+1)*(strlen(field)+1) + strlen(r) + 1;
44
45         ret = (char *)SMB_MALLOC(len);
46         if (!ret)
47                 return NULL;
48
49         strlcpy(ret,field, len);
50         p=strtok(r,sep); 
51         if (p) {
52                 strlcat(ret, p, len);
53         
54                 while ((p=strtok(NULL,sep))) {
55                         char *s;
56                         if (reverse)
57                                 asprintf(&s, "%s%s,%s", field, p, ret);
58                         else
59                                 asprintf(&s, "%s,%s%s", ret, field, p);
60                         free(ret);
61                         ret = s;
62                 }
63         }
64
65         free(r);
66         return ret;
67 }
68
69 /* return a dn of the form "dc=AA,dc=BB,dc=CC" from a 
70    realm of the form AA.BB.CC 
71    caller must free
72 */
73 char *ads_build_dn(const char *realm)
74 {
75         return ads_build_path(realm, ".", "dc=", 0);
76 }
77
78 /* return a DNS name in the for aa.bb.cc from the DN  
79    "dc=AA,dc=BB,dc=CC".  caller must free
80 */
81 char *ads_build_domain(const char *dn)
82 {
83         char *dnsdomain = NULL;
84         
85         /* result should always be shorter than the DN */
86
87         if ( (dnsdomain = SMB_STRDUP( dn )) == NULL ) {
88                 DEBUG(0,("ads_build_domain: malloc() failed!\n"));              
89                 return NULL;            
90         }       
91
92         strlower_m( dnsdomain );        
93         all_string_sub( dnsdomain, "dc=", "", 0);
94         all_string_sub( dnsdomain, ",", ".", 0 );
95
96         return dnsdomain;       
97 }
98
99
100
101 #ifndef LDAP_PORT
102 #define LDAP_PORT 389
103 #endif
104
105 /*
106   initialise a ADS_STRUCT, ready for some ads_ ops
107 */
108 ADS_STRUCT *ads_init(const char *realm, 
109                      const char *workgroup,
110                      const char *ldap_server)
111 {
112         ADS_STRUCT *ads;
113         
114         ads = SMB_XMALLOC_P(ADS_STRUCT);
115         ZERO_STRUCTP(ads);
116         
117         ads->server.realm = realm? SMB_STRDUP(realm) : NULL;
118         ads->server.workgroup = workgroup ? SMB_STRDUP(workgroup) : NULL;
119         ads->server.ldap_server = ldap_server? SMB_STRDUP(ldap_server) : NULL;
120
121         /* we need to know if this is a foreign realm */
122         if (realm && *realm && !strequal(lp_realm(), realm)) {
123                 ads->server.foreign = 1;
124         }
125         if (workgroup && *workgroup && !strequal(lp_workgroup(), workgroup)) {
126                 ads->server.foreign = 1;
127         }
128
129         /* the caller will own the memory by default */
130         ads->is_mine = 1;
131
132         return ads;
133 }
134
135 /*
136   free the memory used by the ADS structure initialized with 'ads_init(...)'
137 */
138 void ads_destroy(ADS_STRUCT **ads)
139 {
140         if (ads && *ads) {
141                 BOOL is_mine;
142
143                 is_mine = (*ads)->is_mine;
144 #if HAVE_LDAP
145                 if ((*ads)->ld) {
146                         ldap_unbind((*ads)->ld);
147                 }
148 #endif
149                 SAFE_FREE((*ads)->server.realm);
150                 SAFE_FREE((*ads)->server.workgroup);
151                 SAFE_FREE((*ads)->server.ldap_server);
152
153                 SAFE_FREE((*ads)->auth.realm);
154                 SAFE_FREE((*ads)->auth.password);
155                 SAFE_FREE((*ads)->auth.user_name);
156                 SAFE_FREE((*ads)->auth.kdc_server);
157
158                 SAFE_FREE((*ads)->config.realm);
159                 SAFE_FREE((*ads)->config.bind_path);
160                 SAFE_FREE((*ads)->config.ldap_server_name);
161                 SAFE_FREE((*ads)->config.server_site_name);
162                 SAFE_FREE((*ads)->config.client_site_name);
163                 
164                 ZERO_STRUCTP(*ads);
165
166                 if ( is_mine )
167                         SAFE_FREE(*ads);
168         }
169 }