r6028: A MAJOR update to intergrate the new credentails system fully with
[samba.git] / source4 / auth / auth_domain.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Authenticate a user to a domain controller
5
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
7    Copyright (C) Andrew Tridgell 2004
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "librpc/gen_ndr/ndr_netlogon.h"
26 #include "include/secrets.h"
27 #include "lib/ldb/include/ldb.h"
28 #include "auth/auth.h"
29
30 /* Authenticate a user with a challenge/response */
31 static NTSTATUS domain_check_password(struct auth_method_context *ctx,
32                                       TALLOC_CTX *mem_ctx,
33                                       const struct auth_usersupplied_info *user_info, 
34                                       struct auth_serversupplied_info **server_info)
35 {
36         NTSTATUS status;
37
38         struct dcerpc_pipe *p;
39         struct dcerpc_binding *b;
40         struct netr_LogonSamLogon r;
41         struct netr_Authenticator auth, auth2;
42         struct netr_NetworkInfo ninfo;
43
44         struct creds_CredentialState *creds;
45         struct cli_credentials *credentials;
46
47         const char **bindings = lp_passwordserver();
48         const char *binding;
49
50         if (bindings && bindings[0]) {
51                 binding = bindings[0];
52         }
53
54         if (!user_info->account_name) {
55                 return NT_STATUS_INVALID_PARAMETER;
56         }
57         if (!user_info->workstation_name) {
58                 return NT_STATUS_INVALID_PARAMETER;
59         }
60
61         credentials = cli_credentials_init(mem_ctx);
62         cli_credentials_set_conf(credentials);
63         status = cli_credentials_set_machine_account(credentials);
64
65         if (!NT_STATUS_IS_OK(status)) {
66                 return status;
67         }
68
69         /* Connect to DC (take a binding string for now) */
70
71         status = dcerpc_parse_binding(mem_ctx, binding, &b);
72         if (!NT_STATUS_IS_OK(status)) {
73                 printf("Bad binding string %s\n", binding);
74                 return NT_STATUS_INVALID_PARAMETER;
75         }
76
77         /* We like schannel */
78         b->flags &= ~DCERPC_AUTH_OPTIONS;
79         b->flags |= DCERPC_SCHANNEL_WORKSTATION | DCERPC_SEAL | DCERPC_SCHANNEL_128;
80
81         /* Setup schannel */
82         status = dcerpc_pipe_connect_b(mem_ctx, &p, b, 
83                                        DCERPC_NETLOGON_UUID,
84                                        DCERPC_NETLOGON_VERSION,
85                                        credentials);
86
87         if (!NT_STATUS_IS_OK(status)) {
88                 return status;
89         }
90
91         /* call domain logon */
92
93         status = dcerpc_schannel_creds(p->conn->security_state.generic_state, mem_ctx, &creds);
94         if (!NT_STATUS_IS_OK(status)) {
95                 return status;
96         }
97
98         ninfo.identity_info.domain_name.string = user_info->domain_name;
99         ninfo.identity_info.parameter_control = 0;
100         ninfo.identity_info.logon_id_low = 0;
101         ninfo.identity_info.logon_id_high = 0;
102         ninfo.identity_info.account_name.string = user_info->account_name;
103         ninfo.identity_info.workstation.string = user_info->workstation_name;
104         memcpy(ninfo.challenge, ctx->auth_ctx->challenge.data.data, sizeof(ninfo.challenge));
105
106         ninfo.nt.length = user_info->nt_resp.length;
107         ninfo.nt.data =  user_info->nt_resp.data;
108         ninfo.lm.length = user_info->lm_resp.length;
109         ninfo.lm.data = user_info->lm_resp.data;
110
111         r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
112         r.in.workstation = cli_credentials_get_workstation(credentials);
113         r.in.credential = &auth;
114         r.in.return_authenticator = &auth2;
115         r.in.logon_level = 2;
116         r.in.logon.network = &ninfo;
117         r.in.validation_level = 3;
118
119         ZERO_STRUCT(auth2);
120         creds_client_authenticator(creds, &auth);
121         
122         status = dcerpc_netr_LogonSamLogon(p, mem_ctx, &r);
123         
124         if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
125                 DEBUG(1, ("Credential chaining failed\n"));
126                 return NT_STATUS_ACCESS_DENIED;
127         }
128
129         /* make server info */
130
131         if (!NT_STATUS_IS_OK(status)) {
132                 return status;
133         }
134         
135         status = make_server_info_netlogon_validation(mem_ctx, 
136                                                       user_info->account_name, 
137                                                       r.in.validation_level, &r.out.validation,
138                                                       server_info);
139         return status;
140 }
141
142 static const struct auth_operations domain_ops = {
143         .name           = "domain",
144         .get_challenge  = auth_get_challenge_not_implemented,
145         .check_password = domain_check_password
146 };
147
148 NTSTATUS auth_domain_init(void)
149 {
150         NTSTATUS ret;
151
152         ret = auth_register(&domain_ops);
153         if (!NT_STATUS_IS_OK(ret)) {
154                 DEBUG(0,("Failed to register 'domain' auth backend!\n"));
155                 return ret;
156         }
157         return ret;
158 }