s3-vfs: rename open function to open_fn.
[Samba.git] / source3 / rpc_server / dcesrv_gssapi.c
blobf60f6ce245136a5f30de21f5d58d893f2da2a925
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"
25 #include "auth.h"
27 NTSTATUS gssapi_server_auth_start(TALLOC_CTX *mem_ctx,
28 bool do_sign,
29 bool do_seal,
30 bool is_dcerpc,
31 DATA_BLOB *token_in,
32 DATA_BLOB *token_out,
33 struct gse_context **ctx)
35 struct gse_context *gse_ctx = NULL;
36 uint32_t add_flags = 0;
37 NTSTATUS status;
39 if (is_dcerpc) {
40 add_flags = GSS_C_DCE_STYLE;
43 /* Let's init the gssapi machinery for this connection */
44 /* passing a NULL server name means the server will try
45 * to accept any connection regardless of the name used as
46 * long as it can find a decryption key */
47 /* by passing NULL, the code will attempt to set a default
48 * keytab based on configuration options */
49 status = gse_init_server(mem_ctx, do_sign, do_seal,
50 add_flags, NULL, &gse_ctx);
51 if (!NT_STATUS_IS_OK(status)) {
52 DEBUG(0, ("Failed to init dcerpc gssapi server (%s)\n",
53 nt_errstr(status)));
54 return status;
57 status = gse_get_server_auth_token(mem_ctx, gse_ctx,
58 token_in, token_out);
59 if (!NT_STATUS_IS_OK(status)) {
60 DEBUG(0, ("Failed to parse initial client token (%s)\n",
61 nt_errstr(status)));
62 goto done;
65 *ctx = gse_ctx;
66 status = NT_STATUS_OK;
68 done:
69 if (!NT_STATUS_IS_OK(status)) {
70 TALLOC_FREE(gse_ctx);
73 return status;
76 NTSTATUS gssapi_server_step(struct gse_context *gse_ctx,
77 TALLOC_CTX *mem_ctx,
78 DATA_BLOB *token_in,
79 DATA_BLOB *token_out)
81 NTSTATUS status;
83 status = gse_get_server_auth_token(mem_ctx, gse_ctx,
84 token_in, token_out);
85 if (!NT_STATUS_IS_OK(status)) {
86 return status;
89 if (gse_require_more_processing(gse_ctx)) {
90 /* ask for next leg */
91 return NT_STATUS_MORE_PROCESSING_REQUIRED;
94 return NT_STATUS_OK;
97 NTSTATUS gssapi_server_check_flags(struct gse_context *gse_ctx)
99 return gse_verify_server_auth_flags(gse_ctx);
102 NTSTATUS gssapi_server_get_user_info(struct gse_context *gse_ctx,
103 TALLOC_CTX *mem_ctx,
104 struct client_address *client_id,
105 struct auth_serversupplied_info **server_info)
107 TALLOC_CTX *tmp_ctx;
108 DATA_BLOB auth_data;
109 time_t tgs_authtime;
110 NTTIME tgs_authtime_nttime;
111 DATA_BLOB pac;
112 struct PAC_DATA *pac_data;
113 struct PAC_LOGON_NAME *logon_name = NULL;
114 struct PAC_LOGON_INFO *logon_info = NULL;
115 enum ndr_err_code ndr_err;
116 unsigned int i;
117 bool is_mapped;
118 bool is_guest;
119 char *princ_name;
120 char *ntuser;
121 char *ntdomain;
122 char *username;
123 struct passwd *pw;
124 NTSTATUS status;
125 bool bret;
127 tmp_ctx = talloc_new(mem_ctx);
128 if (!tmp_ctx) {
129 return NT_STATUS_NO_MEMORY;
132 status = gse_get_authz_data(gse_ctx, tmp_ctx, &auth_data);
133 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
134 /* TODO: Fetch user by principal name ? */
135 status = NT_STATUS_ACCESS_DENIED;
136 goto done;
138 if (!NT_STATUS_IS_OK(status)) {
139 goto done;
142 bret = unwrap_pac(tmp_ctx, &auth_data, &pac);
143 if (!bret) {
144 DEBUG(1, ("Failed to unwrap PAC\n"));
145 status = NT_STATUS_ACCESS_DENIED;
146 goto done;
149 status = gse_get_client_name(gse_ctx, tmp_ctx, &princ_name);
150 if (!NT_STATUS_IS_OK(status)) {
151 goto done;
154 status = gse_get_authtime(gse_ctx, &tgs_authtime);
155 if (!NT_STATUS_IS_OK(status)) {
156 goto done;
158 unix_to_nt_time(&tgs_authtime_nttime, tgs_authtime);
160 pac_data = talloc_zero(tmp_ctx, struct PAC_DATA);
161 if (!pac_data) {
162 status = NT_STATUS_NO_MEMORY;
163 goto done;
166 ndr_err = ndr_pull_struct_blob(&pac, pac_data, pac_data,
167 (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA);
168 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
169 DEBUG(1, ("Failed to parse the PAC for %s\n", princ_name));
170 status = ndr_map_error2ntstatus(ndr_err);
171 goto done;
174 /* get logon name and logon info */
175 for (i = 0; i < pac_data->num_buffers; i++) {
176 struct PAC_BUFFER *data_buf = &pac_data->buffers[i];
178 switch (data_buf->type) {
179 case PAC_TYPE_LOGON_INFO:
180 if (!data_buf->info) {
181 break;
183 logon_info = data_buf->info->logon_info.info;
184 break;
185 case PAC_TYPE_LOGON_NAME:
186 logon_name = &data_buf->info->logon_name;
187 break;
188 default:
189 break;
192 if (!logon_info) {
193 DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
194 status = NT_STATUS_NOT_FOUND;
195 goto done;
197 if (!logon_name) {
198 DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
199 status = NT_STATUS_NOT_FOUND;
200 goto done;
203 /* check time */
204 if (tgs_authtime_nttime != logon_name->logon_time) {
205 DEBUG(1, ("Logon time mismatch between ticket and PAC!\n"
206 "PAC Time = %s | Ticket Time = %s\n",
207 nt_time_string(tmp_ctx, logon_name->logon_time),
208 nt_time_string(tmp_ctx, tgs_authtime_nttime)));
209 status = NT_STATUS_ACCESS_DENIED;
210 goto done;
213 /* TODO: Should we check princ_name against account_name in
214 * logon_name ? Are they supposed to be identical, or can an
215 * account_name be different from the UPN ? */
217 status = get_user_from_kerberos_info(tmp_ctx, client_id->name,
218 princ_name, logon_info,
219 &is_mapped, &is_guest,
220 &ntuser, &ntdomain,
221 &username, &pw);
222 if (!NT_STATUS_IS_OK(status)) {
223 DEBUG(1, ("Failed to map kerberos principal to system user "
224 "(%s)\n", nt_errstr(status)));
225 status = NT_STATUS_ACCESS_DENIED;
226 goto done;
229 /* TODO: save PAC data in netsamlogon cache ? */
231 status = make_server_info_krb5(mem_ctx,
232 ntuser, ntdomain, username, pw,
233 logon_info, is_guest, server_info);
234 if (!NT_STATUS_IS_OK(status)) {
235 DEBUG(1, ("Failed to map kerberos pac to server info (%s)\n",
236 nt_errstr(status)));
237 status = NT_STATUS_ACCESS_DENIED;
238 goto done;
241 DEBUG(5, (__location__ "OK: user: %s domain: %s client: %s\n",
242 ntuser, ntdomain, client_id->name));
244 status = NT_STATUS_OK;
246 done:
247 TALLOC_FREE(tmp_ctx);
248 return status;