This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[kai/samba.git] / source / 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 2 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 = strdup(realm);
35
36         if (!r || !*r) return r;
37
38         for (p=r; *p; p++) {
39                 if (strchr(sep, *p)) numbits++;
40         }
41
42         len = (numbits+1)*(strlen(field)+1) + strlen(r) + 1;
43
44         ret = malloc(len);
45         strlcpy(ret,field, len);
46         p=strtok(r,sep); 
47         strlcat(ret, p, len);
48
49         while ((p=strtok(NULL,sep))) {
50                 char *s;
51                 if (reverse) {
52                         asprintf(&s, "%s%s,%s", field, p, ret);
53                 } else {
54                         asprintf(&s, "%s,%s%s", ret, field, p);
55                 }
56                 free(ret);
57                 ret = s;
58         }
59
60         free(r);
61
62         return ret;
63 }
64
65 /* return a dn of the form "dc=AA,dc=BB,dc=CC" from a 
66    realm of the form AA.BB.CC 
67    caller must free
68 */
69 char *ads_build_dn(const char *realm)
70 {
71         return ads_build_path(realm, ".", "dc=", 0);
72 }
73
74
75 #ifndef LDAP_PORT
76 #define LDAP_PORT 389
77 #endif
78
79 /*
80   initialise a ADS_STRUCT, ready for some ads_ ops
81 */
82 ADS_STRUCT *ads_init(const char *realm, 
83                      const char *workgroup,
84                      const char *ldap_server)
85 {
86         ADS_STRUCT *ads;
87         
88         ads = (ADS_STRUCT *)smb_xmalloc(sizeof(*ads));
89         ZERO_STRUCTP(ads);
90         
91         ads->server.realm = realm? strdup(realm) : NULL;
92         ads->server.workgroup = workgroup ? strdup(workgroup) : NULL;
93         ads->server.ldap_server = ldap_server? strdup(ldap_server) : NULL;
94
95         /* we need to know if this is a foreign realm to know if we can
96            use lp_ads_server() */
97         if (realm && strcasecmp(lp_realm(), realm) != 0) {
98                 ads->server.foreign = 1;
99         }
100         if (workgroup && strcasecmp(lp_workgroup(), workgroup) != 0) {
101                 ads->server.foreign = 1;
102         }
103
104         return ads;
105 }
106
107 /* a simpler ads_init() interface using all defaults */
108 ADS_STRUCT *ads_init_simple(void)
109 {
110         return ads_init(NULL, NULL, NULL);
111 }
112
113 /*
114   free the memory used by the ADS structure initialized with 'ads_init(...)'
115 */
116 void ads_destroy(ADS_STRUCT **ads)
117 {
118         if (ads && *ads) {
119 #if HAVE_LDAP
120                 if ((*ads)->ld) ldap_unbind((*ads)->ld);
121 #endif
122                 SAFE_FREE((*ads)->server.realm);
123                 SAFE_FREE((*ads)->server.workgroup);
124                 SAFE_FREE((*ads)->server.ldap_server);
125
126                 SAFE_FREE((*ads)->auth.realm);
127                 SAFE_FREE((*ads)->auth.password);
128                 SAFE_FREE((*ads)->auth.user_name);
129                 SAFE_FREE((*ads)->auth.kdc_server);
130
131                 SAFE_FREE((*ads)->config.realm);
132                 SAFE_FREE((*ads)->config.bind_path);
133                 SAFE_FREE((*ads)->config.ldap_server_name);
134
135                 ZERO_STRUCTP(*ads);
136                 SAFE_FREE(*ads);
137         }
138 }