Merge commit 'release-4-0-0alpha15' into master4-tmp
[kai/samba-autobuild/.git] / auth / kerberos / gssapi_pac.c
1 /*
2    Unix SMB/CIFS implementation.
3    kerberos authorization data (PAC) utility library
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2011
5    Copyright (C) Simo Sorce 2010.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #ifdef HAVE_KRB5
23
24 #include "libcli/auth/krb5_wrap.h"
25
26 #if 0
27 /* FIXME - need proper configure/waf test
28  * to determine if gss_mech_krb5 and friends
29  * exist. JRA.
30  */
31 /*
32  * These are not exported by Solaris -lkrb5
33  * Maybe move to libreplace somewhere?
34  */
35 static const gss_OID_desc krb5_gss_oid_array[] = {
36         /* this is the official, rfc-specified OID */
37         { 9, "\052\206\110\206\367\022\001\002\002" },
38         /* this is the pre-RFC mech OID */
39         { 5, "\053\005\001\005\002" },
40         /* this is the unofficial, incorrect mech OID emitted by MS */
41         { 9, "\052\206\110\202\367\022\001\002\002" },
42         { 0, 0 }
43 };
44
45 const gss_OID_desc * const gss_mech_krb5              = krb5_gss_oid_array+0;
46 const gss_OID_desc * const gss_mech_krb5_old          = krb5_gss_oid_array+1;
47 const gss_OID_desc * const gss_mech_krb5_wrong        = krb5_gss_oid_array+2;
48 #endif
49
50 /* The Heimdal OID for getting the PAC */
51 #define EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID_LENGTH 8
52 /*                                                      EXTRACTION OID             AUTHZ ID */
53 #define EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID "\x2a\x85\x70\x2b\x0d\x03" "\x81\x00"
54
55 NTSTATUS gssapi_obtain_pac_blob(TALLOC_CTX *mem_ctx,
56                                 gss_ctx_id_t gssapi_context,
57                                 gss_name_t gss_client_name,
58                                 DATA_BLOB *pac_blob)
59 {
60         NTSTATUS status;
61         OM_uint32 gss_maj, gss_min;
62 #ifdef HAVE_GSS_GET_NAME_ATTRIBUTE
63         gss_buffer_desc pac_buffer;
64         gss_buffer_desc pac_display_buffer;
65         gss_buffer_desc pac_name = {
66                 .value = discard_const("urn:mspac:"),
67                 .length = sizeof("urn:mspac:")-1
68         };
69         int more = -1;
70         int authenticated = false;
71         int complete = false;
72
73         gss_maj = gss_get_name_attribute(
74                 &gss_min, gss_client_name, &pac_name,
75                 &authenticated, &complete,
76                 &pac_buffer, &pac_display_buffer, &more);
77
78         if (gss_maj != 0) {
79                 DEBUG(0, ("obtaining PAC via GSSAPI gss_get_name_attribute failed: %s\n",
80                           gssapi_error_string(mem_ctx, gss_maj, gss_min, gss_mech_krb5)));
81                 return NT_STATUS_ACCESS_DENIED;
82         } else if (authenticated && complete) {
83                 /* The PAC blob is returned directly */
84                 *pac_blob = data_blob_talloc(mem_ctx, pac_buffer.value,
85                                             pac_buffer.length);
86
87                 if (!pac_blob->data) {
88                         status = NT_STATUS_NO_MEMORY;
89                 } else {
90                         status = NT_STATUS_OK;
91                 }
92
93                 gss_maj = gss_release_buffer(&gss_min, &pac_buffer);
94                 gss_maj = gss_release_buffer(&gss_min, &pac_display_buffer);
95                 return status;
96         } else {
97                 DEBUG(0, ("obtaining PAC via GSSAPI failed: authenticated: %s, complete: %s, more: %s\n",
98                           authenticated ? "true" : "false",
99                           complete ? "true" : "false",
100                           more ? "true" : "false"));
101                 return NT_STATUS_ACCESS_DENIED;
102         }
103
104 #elif defined(HAVE_GSS_INQUIRE_SEC_CONTEXT_BY_OID)
105         gss_OID_desc pac_data_oid = {
106                 .elements = discard_const(EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID),
107                 .length = EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID_LENGTH
108         };
109
110         gss_buffer_set_t set = GSS_C_NO_BUFFER_SET;
111
112         /* If we didn't have the routine to get a verified, validated
113          * PAC (supplied only by MIT at the time of writing), then try
114          * with the Heimdal OID (fetches the PAC directly and always
115          * validates) */
116         gss_maj = gss_inquire_sec_context_by_oid(
117                                 &gss_min, gssapi_context,
118                                 &pac_data_oid, &set);
119
120         /* First check for the error MIT gives for an unknown OID */
121         if (gss_maj == GSS_S_UNAVAILABLE) {
122                 DEBUG(1, ("unable to obtain a PAC against this GSSAPI library.  "
123                           "GSSAPI secured connections are available only with Heimdal or MIT Kerberos >= 1.8\n"));
124         } else if (gss_maj != 0) {
125                 DEBUG(2, ("obtaining PAC via GSSAPI gss_inqiure_sec_context_by_oid (Heimdal OID) failed: %s\n",
126                           gssapi_error_string(mem_ctx, gss_maj, gss_min, gss_mech_krb5)));
127         } else {
128                 if (set == GSS_C_NO_BUFFER_SET) {
129                         DEBUG(0, ("gss_inquire_sec_context_by_oid returned unknown "
130                                   "data in results.\n"));
131                         return NT_STATUS_INTERNAL_ERROR;
132                 }
133
134                 /* The PAC blob is returned directly */
135                 *pac_blob = data_blob_talloc(mem_ctx, set->elements[0].value,
136                                             set->elements[0].length);
137                 if (!pac_blob->data) {
138                         status = NT_STATUS_NO_MEMORY;
139                 } else {
140                         status = NT_STATUS_OK;
141                 }
142
143                 gss_maj = gss_release_buffer_set(&gss_min, &set);
144                 return status;
145         }
146 #else
147         DEBUG(1, ("unable to obtain a PAC against this GSSAPI library.  "
148                   "GSSAPI secured connections are available only with Heimdal or MIT Kerberos >= 1.8\n"));
149 #endif
150         return NT_STATUS_ACCESS_DENIED;
151 }
152 #endif