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