define LDAP_PORT when not available
[samba.git] / source3 / libads / ads_struct.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    ads (active directory) utility library
5    Copyright (C) Andrew Tridgell 2001
6    Copyright (C) Andrew Bartlett 2001
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 static char *ads_build_dn(const char *realm)
26 {
27         char *p, *r;
28         int numdots = 0;
29         char *ret;
30         int len;
31         
32         r = strdup(realm);
33
34         if (!r || !*r) return r;
35
36         for (p=r; *p; p++) {
37                 if (*p == '.') numdots++;
38         }
39
40         len = (numdots+1)*4 + strlen(r) + 1;
41
42         ret = malloc(len);
43         strlcpy(ret,"dc=", len);
44         p=strtok(r,"."); 
45         strlcat(ret, p, len);
46
47         while ((p=strtok(NULL,"."))) {
48                 strlcat(ret,",dc=", len);
49                 strlcat(ret, p, len);
50         }
51
52         free(r);
53
54         return ret;
55 }
56
57 #ifdef HAVE_KRB5
58
59 /*
60   get the default relm from krb5.conf
61 */
62 static char *get_default_realm(ADS_STRUCT *ads)
63 {
64         BOOL ret;
65         krb5_context context;
66         char *realm;
67
68         ret = krb5_init_context(&context);
69         if (ret) {
70                 DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
71                 return NULL;
72         }
73
74         ret = krb5_get_default_realm(context, &realm);
75         if (ret) {
76                 DEBUG(1,("krb5_get_default_realm failed (%s)\n", error_message(ret)));
77                 krb5_free_context(context);
78                 return NULL;
79         } else {
80                 DEBUG(5,("krb5_get_default_realm got (%s)\n", realm));
81         }
82         krb5_free_context(context);
83                 
84         return realm;
85 }
86
87 #else 
88 static char *get_default_realm(ADS_STRUCT *ads)
89 {
90         /* We can't do this if we don't have krb5, 
91            but save linking nightmares */
92         DEBUG(5,("get_default_realm:  not compiled with krb5.\n"));
93         return NULL;
94 }
95
96 #endif
97
98 #ifdef HAVE_LDAP
99 /*
100   find the ldap server from DNS
101 */
102 static char *find_ldap_server(ADS_STRUCT *ads)
103 {
104         char *list = NULL;
105
106         if (ldap_domain2hostlist(ads->realm, &list) == LDAP_SUCCESS) {
107                 char *p;
108                 p = strchr(list, ':');
109                 if (p) *p = 0;
110                 return list;
111         }
112
113         return NULL;
114 }
115
116 #else 
117
118 static char *find_ldap_server(ADS_STRUCT *ads)
119 {
120         /* Without LDAP this doesn't make much sense */
121         return NULL;
122 }
123
124 #endif 
125
126 #ifndef LDAP_PORT
127 #define LDAP_PORT 389
128 #endif
129
130 /*
131   initialise a ADS_STRUCT, ready for some ads_ ops
132 */
133 ADS_STRUCT *ads_init(const char *realm, 
134                      const char *ldap_server,
135                      const char *bind_path)
136 {
137         ADS_STRUCT *ads;
138         
139         ads = (ADS_STRUCT *)smb_xmalloc(sizeof(*ads));
140         memset(ads, 0, sizeof(*ads));
141         
142         ads->realm = realm? strdup(realm) : NULL;
143         ads->ldap_server = ldap_server? strdup(ldap_server) : NULL;
144         ads->bind_path = bind_path? strdup(bind_path) : NULL;
145         ads->ldap_port = LDAP_PORT;
146
147         if (!ads->realm) {
148                 ads->realm = lp_realm();
149                 if (!ads->realm[0]) {
150                         ads->realm = get_default_realm(ads);
151                 }
152         }
153         if (!ads->bind_path) {
154                 ads->bind_path = ads_build_dn(ads->realm);
155         }
156         if (!ads->ldap_server) {
157                 ads->ldap_server = lp_ads_server();
158                 if (!ads->ldap_server[0]) {
159                         ads->ldap_server = find_ldap_server(ads);
160                 }
161         }
162         if (!ads->kdc_server) {
163                 /* assume its the same as LDAP */
164                 ads->kdc_server = ads->ldap_server? strdup(ads->ldap_server) : NULL;
165         }
166
167         return ads;
168 }
169
170 /*
171   free the memory used by the ADS structure initialized with 'ads_init(...)'
172 */
173 void ads_destroy(ADS_STRUCT **ads)
174 {
175         if (False && (ads) && (*ads)) {
176                 if ((*ads)->ld) ldap_unbind((*ads)->ld);
177                 SAFE_FREE((*ads)->realm);
178                 SAFE_FREE((*ads)->ldap_server);
179                 SAFE_FREE((*ads)->kdc_server);
180                 SAFE_FREE((*ads)->bind_path);
181                 ZERO_STRUCTP(*ads);
182                 SAFE_FREE(*ads);
183         }
184 }
185