summaryrefslogtreecommitdiff
path: root/source4/winbind/wb_connect_lsa.c
blob: 219865b78c865d8f28c62b36079fa3f39f6a7370 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* 
   Unix SMB/CIFS implementation.

   Connect to the LSA pipe, given an smbcli_tree and possibly some
   credentials. Try ntlmssp, schannel and anon in that order.

   Copyright (C) Volker Lendecke 2005
   
   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"
#include "libcli/composite/composite.h"

#include "libcli/raw/libcliraw.h"
#include "librpc/gen_ndr/ndr_lsa_c.h"

/* Helper to initialize LSA with a specific auth methods. Verify by opening
 * the LSA policy. */

struct init_lsa_state {
	struct composite_context *ctx;
	struct dcerpc_pipe *lsa_pipe;

	uint8_t auth_type;
	struct cli_credentials *creds;

	struct lsa_ObjectAttribute objectattr;
	struct lsa_OpenPolicy2 openpolicy;
	struct policy_handle *handle;
};

static void init_lsa_recv_pipe(struct composite_context *ctx);
static void init_lsa_recv_anon_bind(struct composite_context *ctx);
static void init_lsa_recv_auth_bind(struct composite_context *ctx);
static void init_lsa_recv_openpol(struct rpc_request *req);

struct composite_context *wb_init_lsa_send(TALLOC_CTX *mem_ctx,
					   struct smbcli_tree *tree,
					   uint8_t auth_type,
					   struct cli_credentials *creds)
{
	struct composite_context *result, *ctx;
	struct init_lsa_state *state;

	result = composite_create(mem_ctx, tree->session->transport->socket->event.ctx);
	if (result == NULL) goto failed;

	state = talloc(result, struct init_lsa_state);
	if (state == NULL) goto failed;
	state->ctx = result;
	result->private_data = state;

	state->auth_type = auth_type;
	state->creds = creds;

	state->lsa_pipe = dcerpc_pipe_init(state, result->event_ctx);
	if (state->lsa_pipe == NULL) goto failed;

	ctx = dcerpc_pipe_open_smb_send(state->lsa_pipe, tree,
					"\\lsarpc");
	ctx->async.fn = init_lsa_recv_pipe;
	ctx->async.private_data = state;
	return result;
	
 failed:
	talloc_free(result);
	return NULL;
}

static void init_lsa_recv_pipe(struct composite_context *ctx)
{
	struct init_lsa_state *state =
		talloc_get_type(ctx->async.private_data,
				struct init_lsa_state);

	state->ctx->status = dcerpc_pipe_open_smb_recv(ctx);
	if (!composite_is_ok(state->ctx)) return;

	switch (state->auth_type) {
	case DCERPC_AUTH_TYPE_NONE:
		ctx = dcerpc_bind_auth_none_send(state, state->lsa_pipe,
						 &dcerpc_table_lsarpc);
		composite_continue(state->ctx, ctx, init_lsa_recv_anon_bind,
				   state);
		break;
	case DCERPC_AUTH_TYPE_NTLMSSP:
	case DCERPC_AUTH_TYPE_SCHANNEL:
	{
		uint8_t auth_type;
		if (lp_winbind_sealed_pipes()) {
			auth_type = DCERPC_AUTH_LEVEL_PRIVACY;
		} else {
			auth_type = DCERPC_AUTH_LEVEL_INTEGRITY;
		}
		if (state->creds == NULL) {
			composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
			return;
		}
		ctx = dcerpc_bind_auth_send(state, state->lsa_pipe,
					    &dcerpc_table_lsarpc,
					    state->creds, state->auth_type,
					    auth_type,
					    NULL);
		composite_continue(state->ctx, ctx, init_lsa_recv_auth_bind,
				   state);
		break;
	}
	default:
		composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
	}
}

static void init_lsa_recv_anon_bind(struct composite_context *ctx)
{
	struct init_lsa_state *state =
		talloc_get_type(ctx->async.private_data,
				struct init_lsa_state);
	struct rpc_request *req;

	state->ctx->status = dcerpc_bind_auth_none_recv(ctx);
	if (!composite_is_ok(state->ctx)) return;
			
	state->handle = talloc(state, struct policy_handle);
	if (composite_nomem(state->handle, state->ctx)) return;

	state->openpolicy.in.system_name =
		talloc_asprintf(state, "\\\\%s",
				dcerpc_server_name(state->lsa_pipe));
	ZERO_STRUCT(state->objectattr);
	state->openpolicy.in.attr = &state->objectattr;
	state->openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	state->openpolicy.out.handle = state->handle;

	req = dcerpc_lsa_OpenPolicy2_send(state->lsa_pipe, state,
					  &state->openpolicy);
	composite_continue_rpc(state->ctx, req, init_lsa_recv_openpol, state);
}

static void init_lsa_recv_auth_bind(struct composite_context *ctx)
{
	struct init_lsa_state *state =
		talloc_get_type(ctx->async.private_data,
				struct init_lsa_state);
	struct rpc_request *req;

	state->ctx->status = dcerpc_bind_auth_recv(ctx);
	if (!composite_is_ok(state->ctx)) return;
			
	state->handle = talloc(state, struct policy_handle);
	if (composite_nomem(state->handle, state->ctx)) return;

	state->openpolicy.in.system_name =
		talloc_asprintf(state, "\\\\%s",
				dcerpc_server_name(state->lsa_pipe));
	ZERO_STRUCT(state->objectattr);
	state->openpolicy.in.attr = &state->objectattr;
	state->openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	state->openpolicy.out.handle = state->handle;

	req = dcerpc_lsa_OpenPolicy2_send(state->lsa_pipe, state,
					  &state->openpolicy);
	composite_continue_rpc(state->ctx, req, init_lsa_recv_openpol, state);
}

static void init_lsa_recv_openpol(struct rpc_request *req)
{
	struct init_lsa_state *state =
		talloc_get_type(req->async.private_data,
				struct init_lsa_state);

	state->ctx->status = dcerpc_ndr_request_recv(req);
	if (!composite_is_ok(state->ctx)) return;
	state->ctx->status = state->openpolicy.out.result;
	if (!composite_is_ok(state->ctx)) return;

	composite_done(state->ctx);
}

NTSTATUS wb_init_lsa_recv(struct composite_context *c,
			  TALLOC_CTX *mem_ctx,
			  struct dcerpc_pipe **lsa_pipe,
			  struct policy_handle **lsa_policy)
{
	NTSTATUS status = composite_wait(c);
	if (NT_STATUS_IS_OK(status)) {
		struct init_lsa_state *state =
			talloc_get_type(c->private_data,
					struct init_lsa_state);
		*lsa_pipe = talloc_steal(mem_ctx, state->lsa_pipe);
		*lsa_policy = talloc_steal(mem_ctx, state->handle);
	}
	talloc_free(c);
	return status;
}


/*
 * Connect to LSA using the credentials, try NTLMSSP and SCHANNEL using the
 * given credentials. If both fail or no credentials are available, fall back
 * to an anonymous bind.
 */

struct connect_lsa_state {
	struct composite_context *ctx;
	struct smbcli_tree *tree;
	struct cli_credentials *credentials;

	uint8_t auth_type;
	struct dcerpc_pipe *lsa_pipe;
	struct policy_handle *lsa_policy;
};

static void connect_lsa_recv_ntlmssp(struct composite_context *ctx);
static void connect_lsa_recv_schannel(struct composite_context *ctx);
static void connect_lsa_recv_anon(struct composite_context *ctx);

struct composite_context *wb_connect_lsa_send(TALLOC_CTX *mem_ctx,
					      struct smbcli_tree *tree,
					      struct cli_credentials *credentials)
{
	struct composite_context *result, *ctx;
	struct connect_lsa_state *state;

	result = composite_create(mem_ctx, tree->session->transport->socket->event.ctx);
	if (result == NULL) goto failed;

	state = talloc(result, struct connect_lsa_state);
	if (state == NULL) goto failed;
	state->ctx = result;
	result->private_data = state;

	state->tree = tree;
	state->credentials = credentials;

	if (credentials == NULL) {
		ctx = wb_init_lsa_send(state, tree, DCERPC_AUTH_TYPE_NONE,
				       NULL);
		if (ctx == NULL) goto failed;
		ctx->async.fn = connect_lsa_recv_anon;
		ctx->async.private_data = state;
		return result;
	}

	ctx = wb_init_lsa_send(state, tree, DCERPC_AUTH_TYPE_NTLMSSP,
			       credentials);
	if (ctx == NULL) goto failed;
	ctx->async.fn = connect_lsa_recv_ntlmssp;
	ctx->async.private_data = state;
	return result;

 failed:
	talloc_free(result);
	return NULL;
}

static void connect_lsa_recv_ntlmssp(struct composite_context *ctx)
{
	struct connect_lsa_state *state =
		talloc_get_type(ctx->async.private_data,
				struct connect_lsa_state);

	state->ctx->status = wb_init_lsa_recv(ctx, state, &state->lsa_pipe,
					      &state->lsa_policy);

	if (NT_STATUS_IS_OK(state->ctx->status)) {
		state->auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
		composite_done(state->ctx);
		return;
	}

	ctx = wb_init_lsa_send(state, state->tree, DCERPC_AUTH_TYPE_SCHANNEL,
			       state->credentials);
	composite_continue(state->ctx, ctx,
			   connect_lsa_recv_schannel, state);
}

static void connect_lsa_recv_schannel(struct composite_context *ctx)
{
	struct connect_lsa_state *state =
		talloc_get_type(ctx->async.private_data,
				struct connect_lsa_state);

	state->ctx->status = wb_init_lsa_recv(ctx, state, &state->lsa_pipe,
					      &state->lsa_policy);

	if (NT_STATUS_IS_OK(state->ctx->status)) {
		state->auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
		composite_done(state->ctx);
		return;
	}

	ctx = wb_init_lsa_send(state, state->tree, DCERPC_AUTH_TYPE_NONE,
			       state->credentials);
	composite_continue(state->ctx, ctx,
			   connect_lsa_recv_anon, state);
}

static void connect_lsa_recv_anon(struct composite_context *ctx)
{
	struct connect_lsa_state *state =
		talloc_get_type(ctx->async.private_data,
				struct connect_lsa_state);

	state->ctx->status = wb_init_lsa_recv(ctx, state, &state->lsa_pipe,
					      &state->lsa_policy);
	if (!composite_is_ok(state->ctx)) return;

	state->auth_type = DCERPC_AUTH_TYPE_NONE;
	composite_done(state->ctx);
}

NTSTATUS wb_connect_lsa_recv(struct composite_context *c,
			     TALLOC_CTX *mem_ctx,
			     uint8_t *auth_type,
			     struct dcerpc_pipe **lsa_pipe,
			     struct policy_handle **lsa_policy)
{
	NTSTATUS status = composite_wait(c);
	if (NT_STATUS_IS_OK(status)) {
		struct connect_lsa_state *state =
			talloc_get_type(c->private_data,
					struct connect_lsa_state);
		*auth_type = state->auth_type;
		*lsa_pipe = talloc_steal(mem_ctx, state->lsa_pipe);
		*lsa_policy = talloc_steal(mem_ctx, state->lsa_policy);
	}
	talloc_free(c);
	return status;
}

NTSTATUS wb_connect_lsa(TALLOC_CTX *mem_ctx,
			struct smbcli_tree *tree,
			struct cli_credentials *credentials,
			uint8_t *auth_type,
			struct dcerpc_pipe **lsa_pipe,
			struct policy_handle **lsa_policy)
{
	struct composite_context *c;
	c = wb_connect_lsa_send(mem_ctx, tree, credentials);
	return wb_connect_lsa_recv(c, mem_ctx, auth_type, lsa_pipe,
				   lsa_policy);
}