net ads info now reports the IP of the LDAP server as well as its name - very useful...
[metze/samba/wip.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) 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 #ifdef HAVE_LDAP
76 /*
77   find the ldap server from DNS
78 */
79 static char *find_ldap_server(ADS_STRUCT *ads)
80 {
81         char *list = NULL;
82         struct in_addr ip;
83
84         if (ads->realm &&
85             strcasecmp(ads->workgroup, lp_workgroup()) == 0 &&
86             ldap_domain2hostlist(ads->realm, &list) == LDAP_SUCCESS) {
87                 char *p;
88                 p = strchr(list, ':');
89                 if (p) *p = 0;
90                 return list;
91         }
92
93         /* get desperate, find the domain controller IP */
94         if (resolve_name(ads->workgroup, &ip, 0x1B)) {
95                 return strdup(inet_ntoa(ip));
96         }
97         
98         /* or a BDC ... */
99         if (resolve_name(ads->workgroup, &ip, 0x1C)) {
100                 return strdup(inet_ntoa(ip));
101         }
102
103         return NULL;
104 }
105
106 #else 
107
108 static char *find_ldap_server(ADS_STRUCT *ads)
109 {
110         /* Without LDAP this doesn't make much sense */
111         return NULL;
112 }
113
114 #endif 
115
116 #ifndef LDAP_PORT
117 #define LDAP_PORT 389
118 #endif
119
120 /*
121   initialise a ADS_STRUCT, ready for some ads_ ops
122 */
123 ADS_STRUCT *ads_init(const char *realm, 
124                      const char *workgroup,
125                      const char *ldap_server,
126                      const char *bind_path,
127                      const char *password)
128 {
129         ADS_STRUCT *ads;
130         
131         ads = (ADS_STRUCT *)smb_xmalloc(sizeof(*ads));
132         ZERO_STRUCTP(ads);
133         
134         if (!workgroup) {
135                 workgroup = lp_workgroup();
136         }
137
138         ads->realm = realm? strdup(realm) : NULL;
139         ads->workgroup = strdup(workgroup);
140         ads->ldap_server = ldap_server? strdup(ldap_server) : NULL;
141         ads->bind_path = bind_path? strdup(bind_path) : NULL;
142         ads->ldap_port = LDAP_PORT;
143         if (password) ads->password = strdup(password);
144
145         if (!ads->realm) {
146                 ads->realm = strdup(lp_realm());
147                 if (!ads->realm[0]) {
148                         SAFE_FREE(ads->realm);
149                 }
150         }
151
152         if (!ads->realm && strchr_m(ads->workgroup, '.')) {
153                 /* the smb.conf has specified the realm in 'workgroup =' */
154                 ads->realm = strdup(ads->workgroup);
155         }
156
157         if (!ads->bind_path && ads->realm) {
158                 ads->bind_path = ads_build_dn(ads->realm);
159         }
160         if (!ads->ldap_server) {
161                 if (strcasecmp(ads->workgroup, lp_workgroup()) == 0) {
162                         ads->ldap_server = strdup(lp_ads_server());
163                 }
164                 if (!ads->ldap_server || !ads->ldap_server[0]) {
165                         SAFE_FREE(ads->ldap_server);
166                         ads->ldap_server = find_ldap_server(ads);
167                 }
168         }
169         if (!ads->kdc_server) {
170                 /* assume its the same as LDAP */
171                 ads->kdc_server = ads->ldap_server? strdup(ads->ldap_server) : NULL;
172         }
173
174         if (ads->ldap_server) {
175                 /* its very useful knowing the IP of the ldap server */
176                 ads->ldap_ip = *interpret_addr2(ads->ldap_server);
177         }
178
179         return ads;
180 }
181
182 /* a simpler ads_init() interface using all defaults */
183 ADS_STRUCT *ads_init_simple(void)
184 {
185         return ads_init(NULL, NULL, NULL, NULL, NULL);
186 }
187
188 /*
189   free the memory used by the ADS structure initialized with 'ads_init(...)'
190 */
191 void ads_destroy(ADS_STRUCT **ads)
192 {
193         if (ads && *ads) {
194 #if HAVE_LDAP
195                 if ((*ads)->ld) ldap_unbind((*ads)->ld);
196 #endif
197                 SAFE_FREE((*ads)->realm);
198                 SAFE_FREE((*ads)->ldap_server);
199                 SAFE_FREE((*ads)->ldap_server_name);
200                 SAFE_FREE((*ads)->kdc_server);
201                 SAFE_FREE((*ads)->bind_path);
202                 SAFE_FREE((*ads)->password);
203                 SAFE_FREE((*ads)->user_name);
204                 ZERO_STRUCTP(*ads);
205                 SAFE_FREE(*ads);
206         }
207 }