r4636: Per tridge's wish (and probably correct behaviour), don't key off a
[sfrench/samba-autobuild/.git] / source4 / librpc / rpc / dcerpc_auth.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Generic Authentication Interface
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004
8    Copyright (C) Stefan Metzmacher 2004
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26
27 /*
28   do a non-athenticated dcerpc bind
29 */
30 NTSTATUS dcerpc_bind_auth_none(struct dcerpc_pipe *p,
31                                const char *uuid, uint_t version)
32 {
33         TALLOC_CTX *tmp_ctx = talloc_new(p);
34         NTSTATUS status;
35
36         status = dcerpc_bind_byuuid(p, tmp_ctx, uuid, version);
37         talloc_free(tmp_ctx);
38
39         return status;
40 }
41
42 /*
43   perform a multi-part authenticated bind
44 */
45 NTSTATUS dcerpc_bind_auth(struct dcerpc_pipe *p, uint8_t auth_type, uint8_t auth_level,
46                            const char *uuid, uint_t version)
47 {
48         NTSTATUS status;
49         TALLOC_CTX *tmp_ctx = talloc_new(p);
50         DATA_BLOB credentials;
51         DATA_BLOB null_data_blob = data_blob(NULL, 0);
52
53         if (!p->conn->security_state.generic_state) {
54                 status = gensec_client_start(p, &p->conn->security_state.generic_state);
55                 if (!NT_STATUS_IS_OK(status)) goto done;
56
57                 status = gensec_start_mech_by_authtype(p->conn->security_state.generic_state, 
58                                                        auth_type, auth_level);
59                 if (!NT_STATUS_IS_OK(status)) goto done;
60         }
61
62         p->conn->security_state.auth_info = talloc(p, struct dcerpc_auth);
63         if (!p->conn->security_state.auth_info) {
64                 status = NT_STATUS_NO_MEMORY;
65                 goto done;
66         }
67
68         p->conn->security_state.auth_info->auth_type = auth_type;
69         p->conn->security_state.auth_info->auth_level = auth_level;
70         p->conn->security_state.auth_info->auth_pad_length = 0;
71         p->conn->security_state.auth_info->auth_reserved = 0;
72         p->conn->security_state.auth_info->auth_context_id = random();
73         p->conn->security_state.auth_info->credentials = null_data_blob;
74
75         status = gensec_update(p->conn->security_state.generic_state, tmp_ctx,
76                                null_data_blob,
77                                &credentials);
78         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
79                 goto done;
80         }
81
82         p->conn->security_state.auth_info->credentials = credentials;
83
84         status = dcerpc_bind_byuuid(p, tmp_ctx, uuid, version);
85         if (!NT_STATUS_IS_OK(status)) {
86                 goto done;
87         }
88
89         while (1) {
90                 status = gensec_update(p->conn->security_state.generic_state, tmp_ctx,
91                                        p->conn->security_state.auth_info->credentials,
92                                        &credentials);
93                 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
94                         break;
95                 }
96
97                 if (!credentials.length) {
98                         break;
99                 }
100
101                 p->conn->security_state.auth_info->credentials = credentials;
102                 
103                 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
104                         /* We are demanding a reply, so use a request that will get us one */
105                         status = dcerpc_alter_context(p, tmp_ctx, &p->syntax, &p->transfer_syntax);
106                         if (!NT_STATUS_IS_OK(status)) {
107                                 break;
108                         }
109                 } else {
110                         /* NO reply expected, so just send it */
111                         status = dcerpc_auth3(p->conn, tmp_ctx);
112                         credentials = data_blob(NULL, 0);
113                         if (!NT_STATUS_IS_OK(status)) {
114                                 break;
115                         }
116                 }
117         };
118
119 done:
120         talloc_free(tmp_ctx);
121
122         if (!NT_STATUS_IS_OK(status)) {
123                 talloc_free(p->conn->security_state.generic_state);
124                 ZERO_STRUCT(p->conn->security_state);
125         } else {
126                 /* Authenticated connections use the generic session key */
127                 p->conn->security_state.session_key = dcerpc_generic_session_key;
128         }
129
130         return status;
131 }
132
133 /*
134   setup GENSEC on a DCE-RPC pipe
135 */
136 NTSTATUS dcerpc_bind_auth_password(struct dcerpc_pipe *p,
137                                    const char *uuid, uint_t version,
138                                    const char *domain,
139                                    const char *username,
140                                    const char *password,
141                                    uint8_t auth_type)
142 {
143         NTSTATUS status;
144
145         if (!(p->conn->flags & (DCERPC_SIGN | DCERPC_SEAL))) {
146                 p->conn->flags |= DCERPC_CONNECT;
147         }
148
149         status = gensec_client_start(p, &p->conn->security_state.generic_state);
150         if (!NT_STATUS_IS_OK(status)) {
151                 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
152                 return status;
153         }
154
155         status = gensec_set_domain(p->conn->security_state.generic_state, domain);
156         if (!NT_STATUS_IS_OK(status)) {
157                 DEBUG(1, ("Failed to start set GENSEC client domain to %s: %s\n", 
158                           domain, nt_errstr(status)));
159                 return status;
160         }
161
162         status = gensec_set_username(p->conn->security_state.generic_state, username);
163         if (!NT_STATUS_IS_OK(status)) {
164                 DEBUG(1, ("Failed to start set GENSEC client username to %s: %s\n", 
165                           username, nt_errstr(status)));
166                 return status;
167         }
168
169         status = gensec_set_password(p->conn->security_state.generic_state, password);
170         if (!NT_STATUS_IS_OK(status)) {
171                 DEBUG(1, ("Failed to start set GENSEC client password: %s\n", 
172                           nt_errstr(status)));
173                 return status;
174         }
175
176         status = gensec_set_target_hostname(p->conn->security_state.generic_state, 
177                                             p->conn->transport.peer_name(p->conn));
178         if (!NT_STATUS_IS_OK(status)) {
179                 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n", 
180                           nt_errstr(status)));
181                 return status;
182         }
183
184         status = gensec_start_mech_by_authtype(p->conn->security_state.generic_state, 
185                                                auth_type,
186                                                dcerpc_auth_level(p->conn));
187         if (!NT_STATUS_IS_OK(status)) {
188                 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
189                           gensec_get_name_by_authtype(auth_type), nt_errstr(status)));
190                 return status;
191         }
192         
193         status = dcerpc_bind_auth(p, auth_type,
194                                   dcerpc_auth_level(p->conn),
195                                   uuid, version);
196         if (!NT_STATUS_IS_OK(status)) {
197                 DEBUG(2, ("Failed to bind to pipe with %s: %s\n", 
198                           gensec_get_name_by_authtype(auth_type), nt_errstr(status)));
199                 return status;
200         }
201
202         return status;
203 }