summaryrefslogtreecommitdiff
path: root/source3/libads/ads_struct.c
blob: af0b5d4143100ac95c1ba8f600c427428b2c3de0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* 
   Unix SMB/CIFS implementation.
   ads (active directory) utility library
   Copyright (C) Andrew Tridgell 2001
   Copyright (C) Andrew Bartlett 2001
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

/* return a ldap dn path from a string, given separators and field name
   caller must free
*/
char *ads_build_path(const char *realm, const char *sep, const char *field, int reverse)
{
	char *p, *r;
	int numbits = 0;
	char *ret;
	int len;
	
	r = strdup(realm);

	if (!r || !*r) return r;

	for (p=r; *p; p++) {
		if (strchr(sep, *p)) numbits++;
	}

	len = (numbits+1)*(strlen(field)+1) + strlen(r) + 1;

	ret = malloc(len);
	strlcpy(ret,field, len);
	p=strtok(r,sep); 
	strlcat(ret, p, len);

	while ((p=strtok(NULL,sep))) {
		char *s;
		if (reverse) {
			asprintf(&s, "%s%s,%s", field, p, ret);
		} else {
			asprintf(&s, "%s,%s%s", ret, field, p);
		}
		free(ret);
		ret = s;
	}

	free(r);

	return ret;
}

/* return a dn of the form "dc=AA,dc=BB,dc=CC" from a 
   realm of the form AA.BB.CC 
   caller must free
*/
char *ads_build_dn(const char *realm)
{
	return ads_build_path(realm, ".", "dc=", 0);
}


#ifdef HAVE_LDAP
/*
  find the ldap server from DNS
*/
static char *find_ldap_server(ADS_STRUCT *ads)
{
	char *list = NULL;
	struct in_addr ip;

	if (ads->realm &&
	    strcasecmp(ads->workgroup, lp_workgroup()) == 0 &&
	    ldap_domain2hostlist(ads->realm, &list) == LDAP_SUCCESS) {
		char *p;
		p = strchr(list, ':');
		if (p) *p = 0;
		return list;
	}

	/* get desperate, find the domain controller IP */
	if (resolve_name(ads->workgroup, &ip, 0x1B)) {
		return strdup(inet_ntoa(ip));
	}
	
	/* or a BDC ... */
	if (resolve_name(ads->workgroup, &ip, 0x1C)) {
		return strdup(inet_ntoa(ip));
	}

	return NULL;
}

#else 

static char *find_ldap_server(ADS_STRUCT *ads)
{
	/* Without LDAP this doesn't make much sense */
	return NULL;
}

#endif 

#ifndef LDAP_PORT
#define LDAP_PORT 389
#endif

/*
  initialise a ADS_STRUCT, ready for some ads_ ops
*/
ADS_STRUCT *ads_init(const char *realm, 
		     const char *workgroup,
		     const char *ldap_server,
		     const char *bind_path,
		     const char *password)
{
	ADS_STRUCT *ads;
	
	ads = (ADS_STRUCT *)smb_xmalloc(sizeof(*ads));
	ZERO_STRUCTP(ads);
	
	if (!workgroup) {
		workgroup = lp_workgroup();
	}

	ads->realm = realm? strdup(realm) : NULL;
	ads->workgroup = strdup(workgroup);
	ads->ldap_server = ldap_server? strdup(ldap_server) : NULL;
	ads->bind_path = bind_path? strdup(bind_path) : NULL;
	ads->ldap_port = LDAP_PORT;
	if (password) ads->password = strdup(password);

	if (!ads->realm) {
		ads->realm = strdup(lp_realm());
		if (!ads->realm[0]) {
			SAFE_FREE(ads->realm);
		}
	}

	if (!ads->realm && strchr_m(ads->workgroup, '.')) {
		/* the smb.conf has specified the realm in 'workgroup =' */
		ads->realm = strdup(ads->workgroup);
	}

	if (!ads->bind_path && ads->realm) {
		ads->bind_path = ads_build_dn(ads->realm);
	}
	if (!ads->ldap_server) {
		if (strcasecmp(ads->workgroup, lp_workgroup()) == 0) {
			ads->ldap_server = strdup(lp_ads_server());
		}
		if (!ads->ldap_server || !ads->ldap_server[0]) {
			SAFE_FREE(ads->ldap_server);
			ads->ldap_server = find_ldap_server(ads);
		}
	}
	if (!ads->kdc_server) {
		/* assume its the same as LDAP */
		ads->kdc_server = ads->ldap_server? strdup(ads->ldap_server) : NULL;
	}

	if (ads->ldap_server) {
		/* its very useful knowing the IP of the ldap server */
		ads->ldap_ip = *interpret_addr2(ads->ldap_server);
	}

	return ads;
}

/* a simpler ads_init() interface using all defaults */
ADS_STRUCT *ads_init_simple(void)
{
	return ads_init(NULL, NULL, NULL, NULL, NULL);
}

/*
  free the memory used by the ADS structure initialized with 'ads_init(...)'
*/
void ads_destroy(ADS_STRUCT **ads)
{
	if (ads && *ads) {
#if HAVE_LDAP
		if ((*ads)->ld) ldap_unbind((*ads)->ld);
#endif
		SAFE_FREE((*ads)->realm);
		SAFE_FREE((*ads)->ldap_server);
		SAFE_FREE((*ads)->ldap_server_name);
		SAFE_FREE((*ads)->kdc_server);
		SAFE_FREE((*ads)->bind_path);
		SAFE_FREE((*ads)->password);
		SAFE_FREE((*ads)->user_name);
		ZERO_STRUCTP(*ads);
		SAFE_FREE(*ads);
	}
}