be97a64cb427a1f6767845ca932744581ece3fae
[gd/samba-autobuild/.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 "../lib/tsocket/tsocket.h"
25 #include "librpc/crypto/gse.h"
26 #include "auth.h"
27 #ifdef HAVE_KRB5
28 #include "libcli/auth/krb5_wrap.h"
29 #endif
30 NTSTATUS gssapi_server_auth_start(TALLOC_CTX *mem_ctx,
31                                   bool do_sign,
32                                   bool do_seal,
33                                   bool is_dcerpc,
34                                   DATA_BLOB *token_in,
35                                   DATA_BLOB *token_out,
36                                   struct gse_context **ctx)
37 {
38         struct gse_context *gse_ctx = NULL;
39         uint32_t add_flags = 0;
40         NTSTATUS status;
41
42         if (is_dcerpc) {
43                 add_flags = GSS_C_DCE_STYLE;
44         }
45
46         /* Let's init the gssapi machinery for this connection */
47         status = gse_init_server(mem_ctx, do_sign, do_seal,
48                                  add_flags, &gse_ctx);
49         if (!NT_STATUS_IS_OK(status)) {
50                 DEBUG(0, ("Failed to init dcerpc gssapi server (%s)\n",
51                           nt_errstr(status)));
52                 return status;
53         }
54
55         status = gse_get_server_auth_token(mem_ctx, gse_ctx,
56                                            token_in, token_out);
57         if (!NT_STATUS_IS_OK(status)) {
58                 DEBUG(0, ("Failed to parse initial client token (%s)\n",
59                           nt_errstr(status)));
60                 goto done;
61         }
62
63         *ctx = gse_ctx;
64         status = NT_STATUS_OK;
65
66 done:
67         if (!NT_STATUS_IS_OK(status)) {
68                 TALLOC_FREE(gse_ctx);
69         }
70
71         return status;
72 }
73
74 NTSTATUS gssapi_server_step(struct gse_context *gse_ctx,
75                             TALLOC_CTX *mem_ctx,
76                             DATA_BLOB *token_in,
77                             DATA_BLOB *token_out)
78 {
79         NTSTATUS status;
80
81         status = gse_get_server_auth_token(mem_ctx, gse_ctx,
82                                            token_in, token_out);
83         if (!NT_STATUS_IS_OK(status)) {
84                 return status;
85         }
86
87         if (gse_require_more_processing(gse_ctx)) {
88                 /* ask for next leg */
89                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
90         }
91
92         return NT_STATUS_OK;
93 }
94
95 NTSTATUS gssapi_server_check_flags(struct gse_context *gse_ctx)
96 {
97         return gse_verify_server_auth_flags(gse_ctx);
98 }
99
100 NTSTATUS gssapi_server_get_user_info(struct gse_context *gse_ctx,
101                                      TALLOC_CTX *mem_ctx,
102                                      const struct tsocket_address *remote_address,
103                                      struct auth_session_info **session_info)
104 {
105         TALLOC_CTX *tmp_ctx;
106         DATA_BLOB pac_blob;
107         struct PAC_DATA *pac_data = NULL;
108         struct PAC_LOGON_INFO *logon_info = NULL;
109         unsigned int i;
110         bool is_mapped;
111         bool is_guest;
112         char *princ_name;
113         char *ntuser;
114         char *ntdomain;
115         char *username;
116         char *rhost;
117         struct passwd *pw;
118         NTSTATUS status;
119         int rc;
120
121         tmp_ctx = talloc_new(mem_ctx);
122         if (!tmp_ctx) {
123                 return NT_STATUS_NO_MEMORY;
124         }
125
126         status = gse_get_pac_blob(gse_ctx, tmp_ctx, &pac_blob);
127         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
128                 /* TODO: Fetch user by principal name ? */
129                 status = NT_STATUS_ACCESS_DENIED;
130                 goto done;
131         }
132         if (!NT_STATUS_IS_OK(status)) {
133                 goto done;
134         }
135
136 #ifdef HAVE_KRB5
137         status = kerberos_decode_pac(tmp_ctx,
138                                      pac_blob,
139                                      NULL, NULL, NULL, NULL, 0, &pac_data);
140 #else
141         status = NT_STATUS_ACCESS_DENIED;
142 #endif
143         data_blob_free(&pac_blob);
144         if (!NT_STATUS_IS_OK(status)) {
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         /* get logon name and logon info */
154         for (i = 0; i < pac_data->num_buffers; i++) {
155                 struct PAC_BUFFER *data_buf = &pac_data->buffers[i];
156
157                 switch (data_buf->type) {
158                 case PAC_TYPE_LOGON_INFO:
159                         if (!data_buf->info) {
160                                 break;
161                         }
162                         logon_info = data_buf->info->logon_info.info;
163                         break;
164                 default:
165                         break;
166                 }
167         }
168         if (!logon_info) {
169                 DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
170                 status = NT_STATUS_NOT_FOUND;
171                 goto done;
172         }
173
174         rc = get_remote_hostname(remote_address,
175                                  &rhost,
176                                  tmp_ctx);
177         if (rc < 0) {
178                 status = NT_STATUS_NO_MEMORY;
179                 goto done;
180         }
181         if (strequal(rhost, "UNKNOWN")) {
182                 rhost = tsocket_address_inet_addr_string(remote_address,
183                                                          tmp_ctx);
184                 if (rhost == NULL) {
185                         status = NT_STATUS_NO_MEMORY;
186                         goto done;
187                 }
188         }
189
190         status = get_user_from_kerberos_info(tmp_ctx, rhost,
191                                              princ_name, logon_info,
192                                              &is_mapped, &is_guest,
193                                              &ntuser, &ntdomain,
194                                              &username, &pw);
195         if (!NT_STATUS_IS_OK(status)) {
196                 DEBUG(1, ("Failed to map kerberos principal to system user "
197                           "(%s)\n", nt_errstr(status)));
198                 status = NT_STATUS_ACCESS_DENIED;
199                 goto done;
200         }
201
202         /* TODO: save PAC data in netsamlogon cache ? */
203
204         status = make_session_info_krb5(mem_ctx,
205                                         ntuser, ntdomain, username, pw,
206                                         logon_info, is_guest, is_mapped, NULL /* No session key for now */,
207                                         session_info);
208         if (!NT_STATUS_IS_OK(status)) {
209                 DEBUG(1, ("Failed to map kerberos pac to server info (%s)\n",
210                           nt_errstr(status)));
211                 status = NT_STATUS_ACCESS_DENIED;
212                 goto done;
213         }
214
215         DEBUG(5, (__location__ "OK: user: %s domain: %s client: %s\n",
216                   ntuser, ntdomain, rhost));
217
218         status = NT_STATUS_OK;
219
220 done:
221         TALLOC_FREE(tmp_ctx);
222         return status;
223 }