s3-dcerpc: add server helpers for gssapi auth
[mat/samba.git] / source3 / rpc_server / dcesrv_gssapi.c
1 /*
2  *  GSSAPI Acceptor
3  *  DCERPC Server functions
4  *  Copyright (C) Simo Sorce 2010.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "includes.h"
22 #include "rpc_server/dcesrv_gssapi.h"
23 #include "../librpc/gen_ndr/ndr_krb5pac.h"
24 #include "librpc/crypto/gse.h"
25
26 NTSTATUS gssapi_server_auth_start(TALLOC_CTX *mem_ctx,
27                                   bool do_sign,
28                                   bool do_seal,
29                                   bool is_dcerpc,
30                                   DATA_BLOB *token_in,
31                                   DATA_BLOB *token_out,
32                                   struct gse_context **ctx)
33 {
34         struct gse_context *gse_ctx = NULL;
35         uint32_t add_flags = 0;
36         NTSTATUS status;
37
38         if (is_dcerpc) {
39                 add_flags = GSS_C_DCE_STYLE;
40         }
41
42         /* Let's init the gssapi machinery for this connection */
43         /* passing a NULL server name means the server will try
44          * to accept any connection regardless of the name used as
45          * long as it can find a decryption key */
46         /* by passing NULL, the code will attempt to set a default
47          * keytab based on configuration options */
48         status = gse_init_server(mem_ctx, do_sign, do_seal,
49                                  add_flags, NULL, &gse_ctx);
50         if (!NT_STATUS_IS_OK(status)) {
51                 DEBUG(0, ("Failed to init dcerpc gssapi server (%s)\n",
52                           nt_errstr(status)));
53                 return status;
54         }
55
56         status = gse_get_server_auth_token(mem_ctx, gse_ctx,
57                                            token_in, token_out);
58         if (!NT_STATUS_IS_OK(status)) {
59                 DEBUG(0, ("Failed to parse initial client token (%s)\n",
60                           nt_errstr(status)));
61                 goto done;
62         }
63
64         *ctx = gse_ctx;
65         status = NT_STATUS_OK;
66
67 done:
68         if (!NT_STATUS_IS_OK(status)) {
69                 TALLOC_FREE(gse_ctx);
70         }
71
72         return status;
73 }
74
75 NTSTATUS gssapi_server_step(struct gse_context *gse_ctx,
76                             TALLOC_CTX *mem_ctx,
77                             DATA_BLOB *token_in,
78                             DATA_BLOB *token_out)
79 {
80         NTSTATUS status;
81
82         status = gse_get_server_auth_token(mem_ctx, gse_ctx,
83                                            token_in, token_out);
84         if (!NT_STATUS_IS_OK(status)) {
85                 return status;
86         }
87
88         if (gse_require_more_processing(gse_ctx)) {
89                 /* ask for next leg */
90                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
91         }
92
93         return NT_STATUS_OK;
94 }
95
96 NTSTATUS gssapi_server_check_flags(struct gse_context *gse_ctx)
97 {
98         return gse_verify_server_auth_flags(gse_ctx);
99 }
100
101 NTSTATUS gssapi_server_get_user_info(struct gse_context *gse_ctx,
102                                      TALLOC_CTX *mem_ctx,
103                                      struct client_address *client_id,
104                                      struct auth_serversupplied_info **server_info)
105 {
106         TALLOC_CTX *tmp_ctx;
107         DATA_BLOB auth_data;
108         time_t tgs_authtime;
109         NTTIME tgs_authtime_nttime;
110         DATA_BLOB pac;
111         struct PAC_DATA *pac_data;
112         struct PAC_LOGON_NAME *logon_name = NULL;
113         struct PAC_LOGON_INFO *logon_info = NULL;
114         enum ndr_err_code ndr_err;
115         unsigned int i;
116         bool is_mapped;
117         bool is_guest;
118         char *princ_name;
119         char *ntuser;
120         char *ntdomain;
121         char *username;
122         struct passwd *pw;
123         NTSTATUS status;
124         bool bret;
125
126         tmp_ctx = talloc_new(mem_ctx);
127         if (!tmp_ctx) {
128                 return NT_STATUS_NO_MEMORY;
129         }
130
131         status = gse_get_authz_data(gse_ctx, tmp_ctx, &auth_data);
132         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
133                 /* TODO: Fetch user by principal name ? */
134                 status = NT_STATUS_ACCESS_DENIED;
135                 goto done;
136         }
137         if (!NT_STATUS_IS_OK(status)) {
138                 goto done;
139         }
140
141         bret = unwrap_pac(tmp_ctx, &auth_data, &pac);
142         if (!bret) {
143                 DEBUG(1, ("Failed to unwrap PAC\n"));
144                 status = NT_STATUS_ACCESS_DENIED;
145                 goto done;
146         }
147
148         status = gse_get_client_name(gse_ctx, tmp_ctx, &princ_name);
149         if (!NT_STATUS_IS_OK(status)) {
150                 goto done;
151         }
152
153         status = gse_get_authtime(gse_ctx, &tgs_authtime);
154         if (!NT_STATUS_IS_OK(status)) {
155                 goto done;
156         }
157         unix_to_nt_time(&tgs_authtime_nttime, tgs_authtime);
158
159         pac_data = talloc_zero(tmp_ctx, struct PAC_DATA);
160         if (!pac_data) {
161                 status = NT_STATUS_NO_MEMORY;
162                 goto done;
163         }
164
165         ndr_err = ndr_pull_struct_blob(&pac, pac_data, pac_data,
166                                 (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA);
167         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
168                 DEBUG(1, ("Failed to parse the PAC for %s\n", princ_name));
169                 status = ndr_map_error2ntstatus(ndr_err);
170                 goto done;
171         }
172
173         /* get logon name and logon info */
174         for (i = 0; i < pac_data->num_buffers; i++) {
175                 struct PAC_BUFFER *data_buf = &pac_data->buffers[i];
176
177                 switch (data_buf->type) {
178                 case PAC_TYPE_LOGON_INFO:
179                         if (!data_buf->info) {
180                                 break;
181                         }
182                         logon_info = data_buf->info->logon_info.info;
183                         break;
184                 case PAC_TYPE_LOGON_NAME:
185                         logon_name = &data_buf->info->logon_name;
186                         break;
187                 default:
188                         break;
189                 }
190         }
191         if (!logon_info) {
192                 DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
193                 status = NT_STATUS_NOT_FOUND;
194                 goto done;
195         }
196         if (!logon_name) {
197                 DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
198                 status = NT_STATUS_NOT_FOUND;
199                 goto done;
200         }
201
202         /* check time */
203         if (tgs_authtime_nttime != logon_name->logon_time) {
204                 DEBUG(1, ("Logon time mismatch between ticket and PAC!\n"
205                           "PAC Time = %s | Ticket Time = %s\n",
206                           nt_time_string(tmp_ctx, logon_name->logon_time),
207                           nt_time_string(tmp_ctx, tgs_authtime_nttime)));
208                 status = NT_STATUS_ACCESS_DENIED;
209                 goto done;
210         }
211
212         /* TODO: Should we check princ_name against account_name in
213          * logon_name ? Are they supposed to be identical, or can an
214          * account_name be different from the UPN ? */
215
216         status = get_user_from_kerberos_info(tmp_ctx, client_id->name,
217                                              princ_name, logon_info,
218                                              &is_mapped, &is_guest,
219                                              &ntuser, &ntdomain,
220                                              &username, &pw);
221         if (!NT_STATUS_IS_OK(status)) {
222                 DEBUG(1, ("Failed to map kerberos principal to system user "
223                           "(%s)\n", nt_errstr(status)));
224                 status = NT_STATUS_ACCESS_DENIED;
225                 goto done;
226         }
227
228         /* TODO: save PAC data in netsamlogon cache ? */
229
230         status = make_server_info_krb5(mem_ctx,
231                                         ntuser, ntdomain, username, pw,
232                                         logon_info, is_guest, server_info);
233         if (!NT_STATUS_IS_OK(status)) {
234                 DEBUG(1, ("Failed to map kerberos pac to server info (%s)\n",
235                           nt_errstr(status)));
236                 status = NT_STATUS_ACCESS_DENIED;
237                 goto done;
238         }
239
240         DEBUG(5, (__location__ "OK: user: %s domain: %s client: %s\n",
241                   ntuser, ntdomain, client_id->name));
242
243         status = NT_STATUS_OK;
244
245 done:
246         TALLOC_FREE(tmp_ctx);
247         return status;
248 }