added a REALLY gross hack into kerberos_kinit_password so that
[amitay/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 (!ads->realm) return NULL;
107
108         if (ldap_domain2hostlist(ads->realm, &list) == LDAP_SUCCESS) {
109                 char *p;
110                 p = strchr(list, ':');
111                 if (p) *p = 0;
112                 return list;
113         }
114
115         return NULL;
116 }
117
118 #else 
119
120 static char *find_ldap_server(ADS_STRUCT *ads)
121 {
122         /* Without LDAP this doesn't make much sense */
123         return NULL;
124 }
125
126 #endif 
127
128 #ifndef LDAP_PORT
129 #define LDAP_PORT 389
130 #endif
131
132 /*
133   initialise a ADS_STRUCT, ready for some ads_ ops
134 */
135 ADS_STRUCT *ads_init(const char *realm, 
136                      const char *ldap_server,
137                      const char *bind_path,
138                      const char *password)
139 {
140         ADS_STRUCT *ads;
141         
142         ads = (ADS_STRUCT *)smb_xmalloc(sizeof(*ads));
143         ZERO_STRUCTP(ads);
144         
145         ads->realm = realm? strdup(realm) : NULL;
146         ads->ldap_server = ldap_server? strdup(ldap_server) : NULL;
147         ads->bind_path = bind_path? strdup(bind_path) : NULL;
148         ads->ldap_port = LDAP_PORT;
149         if (password) ads->password = strdup(password);
150
151         if (!ads->realm) {
152                 ads->realm = strdup(lp_realm());
153                 if (!ads->realm[0]) {
154                         ads->realm = get_default_realm(ads);
155                 }
156                 if (!ads->realm) ads->realm = strdup("");
157         }
158         if (!ads->bind_path) {
159                 ads->bind_path = ads_build_dn(ads->realm);
160         }
161         if (!ads->ldap_server) {
162                 ads->ldap_server = strdup(lp_ads_server());
163                 if (!ads->ldap_server[0]) {
164                         ads->ldap_server = find_ldap_server(ads);
165                 }
166         }
167         if (!ads->kdc_server) {
168                 /* assume its the same as LDAP */
169                 ads->kdc_server = ads->ldap_server? strdup(ads->ldap_server) : NULL;
170         }
171
172         return ads;
173 }
174
175 /*
176   free the memory used by the ADS structure initialized with 'ads_init(...)'
177 */
178 void ads_destroy(ADS_STRUCT **ads)
179 {
180         if (ads && *ads) {
181                 if ((*ads)->ld) ldap_unbind((*ads)->ld);
182                 SAFE_FREE((*ads)->realm);
183                 SAFE_FREE((*ads)->ldap_server);
184                 SAFE_FREE((*ads)->kdc_server);
185                 SAFE_FREE((*ads)->bind_path);
186                 SAFE_FREE((*ads)->password);
187                 ZERO_STRUCTP(*ads);
188                 SAFE_FREE(*ads);
189         }
190 }
191