s3-dcerpc: add server helpers for gssapi auth
[Samba.git] / source3 / rpc_server / dcesrv_gssapi.c
blob60cfca9b86de225fea5f73ea7c3d7f2dbd5a2eb3
1 /*
2 * GSSAPI Acceptor
3 * DCERPC Server functions
4 * Copyright (C) Simo Sorce 2010.
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.
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.
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/>.
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"
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)
34 struct gse_context *gse_ctx = NULL;
35 uint32_t add_flags = 0;
36 NTSTATUS status;
38 if (is_dcerpc) {
39 add_flags = GSS_C_DCE_STYLE;
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;
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;
64 *ctx = gse_ctx;
65 status = NT_STATUS_OK;
67 done:
68 if (!NT_STATUS_IS_OK(status)) {
69 TALLOC_FREE(gse_ctx);
72 return status;
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)
80 NTSTATUS status;
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;
88 if (gse_require_more_processing(gse_ctx)) {
89 /* ask for next leg */
90 return NT_STATUS_MORE_PROCESSING_REQUIRED;
93 return NT_STATUS_OK;
96 NTSTATUS gssapi_server_check_flags(struct gse_context *gse_ctx)
98 return gse_verify_server_auth_flags(gse_ctx);
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)
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;
126 tmp_ctx = talloc_new(mem_ctx);
127 if (!tmp_ctx) {
128 return NT_STATUS_NO_MEMORY;
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;
137 if (!NT_STATUS_IS_OK(status)) {
138 goto done;
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;
148 status = gse_get_client_name(gse_ctx, tmp_ctx, &princ_name);
149 if (!NT_STATUS_IS_OK(status)) {
150 goto done;
153 status = gse_get_authtime(gse_ctx, &tgs_authtime);
154 if (!NT_STATUS_IS_OK(status)) {
155 goto done;
157 unix_to_nt_time(&tgs_authtime_nttime, tgs_authtime);
159 pac_data = talloc_zero(tmp_ctx, struct PAC_DATA);
160 if (!pac_data) {
161 status = NT_STATUS_NO_MEMORY;
162 goto done;
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;
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];
177 switch (data_buf->type) {
178 case PAC_TYPE_LOGON_INFO:
179 if (!data_buf->info) {
180 break;
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;
191 if (!logon_info) {
192 DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
193 status = NT_STATUS_NOT_FOUND;
194 goto done;
196 if (!logon_name) {
197 DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
198 status = NT_STATUS_NOT_FOUND;
199 goto done;
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;
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 ? */
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;
228 /* TODO: save PAC data in netsamlogon cache ? */
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;
240 DEBUG(5, (__location__ "OK: user: %s domain: %s client: %s\n",
241 ntuser, ntdomain, client_id->name));
243 status = NT_STATUS_OK;
245 done:
246 TALLOC_FREE(tmp_ctx);
247 return status;