VERSION: Bump version number up to 3.6.18.
[Samba.git] / source3 / rpc_server / dcesrv_gssapi.c
blobf027961b309a618dc7cb6e574121ade212dea021
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 pac;
109 struct PAC_DATA *pac_data;
110 struct PAC_LOGON_INFO *logon_info = NULL;
111 enum ndr_err_code ndr_err;
112 unsigned int i;
113 bool is_mapped;
114 bool is_guest;
115 char *princ_name;
116 char *ntuser;
117 char *ntdomain;
118 char *username;
119 struct passwd *pw;
120 NTSTATUS status;
122 tmp_ctx = talloc_new(mem_ctx);
123 if (!tmp_ctx) {
124 return NT_STATUS_NO_MEMORY;
127 status = gse_get_pac_blob(gse_ctx, tmp_ctx, &pac);
128 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
129 /* TODO: Fetch user by principal name ? */
130 status = NT_STATUS_ACCESS_DENIED;
131 goto done;
133 if (!NT_STATUS_IS_OK(status)) {
134 goto done;
137 status = gse_get_client_name(gse_ctx, tmp_ctx, &princ_name);
138 if (!NT_STATUS_IS_OK(status)) {
139 goto done;
142 pac_data = talloc_zero(tmp_ctx, struct PAC_DATA);
143 if (!pac_data) {
144 status = NT_STATUS_NO_MEMORY;
145 goto done;
148 ndr_err = ndr_pull_struct_blob(&pac, pac_data, pac_data,
149 (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA);
150 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
151 DEBUG(1, ("Failed to parse the PAC for %s\n", princ_name));
152 status = ndr_map_error2ntstatus(ndr_err);
153 goto done;
156 /* get logon name and logon info */
157 for (i = 0; i < pac_data->num_buffers; i++) {
158 struct PAC_BUFFER *data_buf = &pac_data->buffers[i];
160 switch (data_buf->type) {
161 case PAC_TYPE_LOGON_INFO:
162 if (!data_buf->info) {
163 break;
165 logon_info = data_buf->info->logon_info.info;
166 break;
167 default:
168 break;
171 if (!logon_info) {
172 DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
173 status = NT_STATUS_NOT_FOUND;
174 goto done;
177 /* TODO: Should we check princ_name against account_name in
178 * logon_name ? Are they supposed to be identical, or can an
179 * account_name be different from the UPN ? */
181 status = get_user_from_kerberos_info(tmp_ctx, client_id->name,
182 princ_name, logon_info,
183 &is_mapped, &is_guest,
184 &ntuser, &ntdomain,
185 &username, &pw);
186 if (!NT_STATUS_IS_OK(status)) {
187 DEBUG(1, ("Failed to map kerberos principal to system user "
188 "(%s)\n", nt_errstr(status)));
189 status = NT_STATUS_ACCESS_DENIED;
190 goto done;
193 /* TODO: save PAC data in netsamlogon cache ? */
195 status = make_server_info_krb5(mem_ctx,
196 ntuser, ntdomain, username, pw,
197 logon_info, is_guest, server_info);
198 if (!NT_STATUS_IS_OK(status)) {
199 DEBUG(1, ("Failed to map kerberos pac to server info (%s)\n",
200 nt_errstr(status)));
201 status = NT_STATUS_ACCESS_DENIED;
202 goto done;
205 DEBUG(5, (__location__ "OK: user: %s domain: %s client: %s\n",
206 ntuser, ntdomain, client_id->name));
208 status = NT_STATUS_OK;
210 done:
211 TALLOC_FREE(tmp_ctx);
212 return status;