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/>.
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,
32 struct gse_context **ctx)
34 struct gse_context *gse_ctx = NULL;
35 uint32_t add_flags = 0;
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",
56 status = gse_get_server_auth_token(mem_ctx, gse_ctx,
58 if (!NT_STATUS_IS_OK(status)) {
59 DEBUG(0, ("Failed to parse initial client token (%s)\n",
65 status = NT_STATUS_OK;
68 if (!NT_STATUS_IS_OK(status)) {
75 NTSTATUS gssapi_server_step(struct gse_context *gse_ctx,
82 status = gse_get_server_auth_token(mem_ctx, gse_ctx,
84 if (!NT_STATUS_IS_OK(status)) {
88 if (gse_require_more_processing(gse_ctx)) {
89 /* ask for next leg */
90 return NT_STATUS_MORE_PROCESSING_REQUIRED;
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,
103 struct client_address *client_id,
104 struct auth_serversupplied_info **server_info)
109 NTTIME tgs_authtime_nttime;
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;
126 tmp_ctx = talloc_new(mem_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;
137 if (!NT_STATUS_IS_OK(status)) {
141 bret = unwrap_pac(tmp_ctx, &auth_data, &pac);
143 DEBUG(1, ("Failed to unwrap PAC\n"));
144 status = NT_STATUS_ACCESS_DENIED;
148 status = gse_get_client_name(gse_ctx, tmp_ctx, &princ_name);
149 if (!NT_STATUS_IS_OK(status)) {
153 status = gse_get_authtime(gse_ctx, &tgs_authtime);
154 if (!NT_STATUS_IS_OK(status)) {
157 unix_to_nt_time(&tgs_authtime_nttime, tgs_authtime);
159 pac_data = talloc_zero(tmp_ctx, struct PAC_DATA);
161 status = NT_STATUS_NO_MEMORY;
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);
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) {
182 logon_info = data_buf->info->logon_info.info;
184 case PAC_TYPE_LOGON_NAME:
185 logon_name = &data_buf->info->logon_name;
192 DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
193 status = NT_STATUS_NOT_FOUND;
197 DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
198 status = NT_STATUS_NOT_FOUND;
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;
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,
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;
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",
236 status = NT_STATUS_ACCESS_DENIED;
240 DEBUG(5, (__location__ "OK: user: %s domain: %s client: %s\n",
241 ntuser, ntdomain, client_id->name));
243 status = NT_STATUS_OK;
246 TALLOC_FREE(tmp_ctx);