summaryrefslogtreecommitdiff
path: root/source4/auth/auth_domain.c
blob: 257014990207d19dcc96d534e8efa5cd2e980c58 (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
/* 
   Unix SMB/CIFS implementation.

   Authenticate a user to a domain controller

   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
   Copyright (C) Andrew Tridgell 2004

   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 "librpc/gen_ndr/ndr_netlogon.h"
#include "include/secrets.h"
#include "lib/ldb/include/ldb.h"
#include "auth/auth.h"

/* Authenticate a user with a challenge/response */
static NTSTATUS domain_check_password(struct auth_method_context *ctx,
				      TALLOC_CTX *mem_ctx,
				      const struct auth_usersupplied_info *user_info, 
				      struct auth_serversupplied_info **server_info)
{
	NTSTATUS status;

	struct dcerpc_pipe *p;
	struct dcerpc_binding *b;
	struct netr_LogonSamLogon r;
	struct netr_Authenticator auth, auth2;
	struct netr_NetworkInfo ninfo;

	struct creds_CredentialState *creds;
	struct cli_credentials *credentials;

	const char **bindings = lp_passwordserver();
	const char *binding;

	if (bindings && bindings[0]) {
		binding = bindings[0];
	} else {
		return NT_STATUS_INVALID_PARAMETER;
	}

	if (!user_info->workstation_name) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	credentials = cli_credentials_init(mem_ctx);
	cli_credentials_set_conf(credentials);
	status = cli_credentials_set_machine_account(credentials);

	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	/* Connect to DC (take a binding string for now) */

	status = dcerpc_parse_binding(mem_ctx, binding, &b);
	if (!NT_STATUS_IS_OK(status)) {
		printf("Bad binding string %s\n", binding);
		return NT_STATUS_INVALID_PARAMETER;
	}

	/* We like schannel */
	b->flags &= ~DCERPC_AUTH_OPTIONS;
	b->flags |= DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128;

	/* Setup schannel */
	status = dcerpc_pipe_connect_b(mem_ctx, &p, b, 
				       DCERPC_NETLOGON_UUID,
				       DCERPC_NETLOGON_VERSION,
				       credentials, ctx->auth_ctx->event_ctx);

	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	/* call domain logon */

	status = dcerpc_schannel_creds(p->conn->security_state.generic_state, mem_ctx, &creds);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	ninfo.identity_info.domain_name.string = user_info->client.domain_name;
	ninfo.identity_info.parameter_control = 0;
	ninfo.identity_info.logon_id_low = 0;
	ninfo.identity_info.logon_id_high = 0;
	ninfo.identity_info.account_name.string = user_info->client.account_name;
	ninfo.identity_info.workstation.string = user_info->workstation_name;
	memcpy(ninfo.challenge, ctx->auth_ctx->challenge.data.data, sizeof(ninfo.challenge));

	ninfo.nt.length = user_info->password.response.nt.length;
	ninfo.nt.data = user_info->password.response.nt.data;
	ninfo.lm.length = user_info->password.response.lanman.length;
	ninfo.lm.data = user_info->password.response.lanman.data;

	r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
	r.in.workstation = cli_credentials_get_workstation(credentials);
	r.in.credential = &auth;
	r.in.return_authenticator = &auth2;
	r.in.logon_level = 2;
	r.in.logon.network = &ninfo;
	r.in.validation_level = 3;

	ZERO_STRUCT(auth2);
	creds_client_authenticator(creds, &auth);
	
	status = dcerpc_netr_LogonSamLogon(p, mem_ctx, &r);
	
	if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
		DEBUG(1, ("Credential chaining failed\n"));
		return NT_STATUS_ACCESS_DENIED;
	}

	/* make server info */

	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}
	
	status = make_server_info_netlogon_validation(mem_ctx, 
						      user_info->client.account_name, 
						      r.in.validation_level, &r.out.validation,
						      server_info);
	return status;
}

static const struct auth_operations domain_ops = {
	.name		= "domain",
	.get_challenge	= auth_get_challenge_not_implemented,
	.check_password	= domain_check_password
};

NTSTATUS auth_domain_init(void)
{
	NTSTATUS ret;

	ret = auth_register(&domain_ops);
	if (!NT_STATUS_IS_OK(ret)) {
		DEBUG(0,("Failed to register 'domain' auth backend!\n"));
		return ret;
	}
	return ret;
}