]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/gssd/context_mit.c
configure changed to corrently report the default for --enable-mount
[nfs-utils.git] / utils / gssd / context_mit.c
1 /*
2   Copyright (c) 2004 The Regents of the University of Michigan.
3   All rights reserved.
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in the
13      documentation and/or other materials provided with the distribution.
14   3. Neither the name of the University nor the names of its
15      contributors may be used to endorse or promote products derived
16      from this software without specific prior written permission.
17
18   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21   DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32
33 #ifndef HAVE_LUCID_CONTEXT_SUPPORT
34 #ifdef HAVE_KRB5
35
36 #include <stdio.h>
37 #include <syslog.h>
38 #include <string.h>
39 #include <gssapi/gssapi.h>
40 #include <rpc/rpc.h>
41 #include <rpc/auth_gss.h>
42 #include "gss_util.h"
43 #include "gss_oids.h"
44 #include "err_util.h"
45 #include "context.h"
46
47 #include <krb5.h>
48
49 #if (KRB5_VERSION > 131)
50 /* XXX argggg, there's gotta be a better way than just duplicating this
51  * whole struct.  Unfortunately, this is in a "private" header file,
52  * so this is our best choice at this point :-/
53  *
54  * XXX Does this match the Heimdal definition?  */
55
56 typedef struct _krb5_gss_ctx_id_rec {
57    unsigned int initiate : 1;   /* nonzero if initiating, zero if accepting */
58    unsigned int established : 1;
59    unsigned int big_endian : 1;
60    unsigned int have_acceptor_subkey : 1;
61    unsigned int seed_init : 1;  /* XXX tested but never actually set */
62 #ifdef CFX_EXERCISE
63    unsigned int testing_unknown_tokid : 1; /* for testing only */
64 #endif
65    OM_uint32 gss_flags;
66    unsigned char seed[16];
67    krb5_principal here;
68    krb5_principal there;
69    krb5_keyblock *subkey;
70    int signalg;
71    size_t cksum_size;
72    int sealalg;
73    krb5_keyblock *enc;
74    krb5_keyblock *seq;
75    krb5_timestamp endtime;
76    krb5_flags krb_flags;
77    /* XXX these used to be signed.  the old spec is inspecific, and
78       the new spec specifies unsigned.  I don't believe that the change
79       affects the wire encoding. */
80    uint64_t seq_send;           /* gssint_uint64 */
81    uint64_t seq_recv;           /* gssint_uint64 */
82    void *seqstate;
83    krb5_auth_context auth_context;
84    gss_OID_desc *mech_used;     /* gss_OID_desc */
85     /* Protocol spec revision
86        0 => RFC 1964 with 3DES and RC4 enhancements
87        1 => draft-ietf-krb-wg-gssapi-cfx-01
88        No others defined so far.  */
89    int proto;
90    krb5_cksumtype cksumtype;    /* for "main" subkey */
91    krb5_keyblock *acceptor_subkey; /* CFX only */
92    krb5_cksumtype acceptor_subkey_cksumtype;
93 #ifdef CFX_EXERCISE
94     gss_buffer_desc init_token;
95 #endif
96 } krb5_gss_ctx_id_rec, *krb5_gss_ctx_id_t;
97
98 #else   /* KRB5_VERSION > 131 */
99
100 typedef struct _krb5_gss_ctx_id_rec {
101         int initiate;
102         u_int32_t gss_flags;
103         int seed_init;
104         unsigned char seed[16];
105         krb5_principal here;
106         krb5_principal there;
107         krb5_keyblock *subkey;
108         int signalg;
109         int cksum_size;
110         int sealalg;
111         krb5_keyblock *enc;
112         krb5_keyblock *seq;
113         krb5_timestamp endtime;
114         krb5_flags krb_flags;
115         krb5_ui_4 seq_send;
116         krb5_ui_4 seq_recv;
117         void *seqstate;
118         int established;
119         int big_endian;
120         krb5_auth_context auth_context;
121         gss_OID_desc *mech_used;
122         int nctypes;
123         krb5_cksumtype *ctypes;
124 } krb5_gss_ctx_id_rec, *krb5_gss_ctx_id_t;
125
126 #endif /* KRB5_VERSION */
127
128
129 static int
130 write_keyblock(char **p, char *end, struct _krb5_keyblock *arg)
131 {
132         gss_buffer_desc tmp;
133
134         if (WRITE_BYTES(p, end, arg->enctype)) return -1;
135         tmp.length = arg->length;
136         tmp.value = arg->contents;
137         if (write_buffer(p, end, &tmp)) return -1;
138         return 0;
139 }
140
141 /*
142  * We really shouldn't know about glue-layer context structure, but
143  * we need to get at the real krb5 context pointer.  This should be
144  * removed as soon as we say there is no support for MIT Kerberos
145  * prior to 1.4 -- which gives us "legal" access to the context info.
146  */
147 typedef struct gss_union_ctx_id_t {
148         gss_OID         mech_type;
149         gss_ctx_id_t    internal_ctx_id;
150 } gss_union_ctx_id_desc, *gss_union_ctx_id_t;
151
152 int
153 serialize_krb5_ctx(gss_ctx_id_t ctx, gss_buffer_desc *buf)
154 {
155         krb5_gss_ctx_id_t kctx = ((gss_union_ctx_id_t)ctx)->internal_ctx_id;
156         char *p, *end;
157         static int constant_one = 1;
158         static int constant_zero = 0;
159         uint32_t word_seq_send;
160
161         if (!(buf->value = calloc(1, MAX_CTX_LEN)))
162                 goto out_err;
163         p = buf->value;
164         end = buf->value + MAX_CTX_LEN;
165
166         if (kctx->initiate) {
167                 if (WRITE_BYTES(&p, end, constant_one)) goto out_err;
168         }
169         else {
170                 if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
171         }
172         if (kctx->seed_init) {
173                 if (WRITE_BYTES(&p, end, constant_one)) goto out_err;
174         }
175         else {
176                 if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
177         }
178         if (write_bytes(&p, end, &kctx->seed, sizeof(kctx->seed)))
179                 goto out_err;
180         if (WRITE_BYTES(&p, end, kctx->signalg)) goto out_err;
181         if (WRITE_BYTES(&p, end, kctx->sealalg)) goto out_err;
182         if (WRITE_BYTES(&p, end, kctx->endtime)) goto out_err;
183         word_seq_send = kctx->seq_send;
184         if (WRITE_BYTES(&p, end, word_seq_send)) goto out_err;
185         if (write_oid(&p, end, kctx->mech_used)) goto out_err;
186
187         printerr(2, "serialize_krb5_ctx: serializing keys with "
188                  "enctype %d and length %d\n",
189                  kctx->enc->enctype, kctx->enc->length);
190
191         if (write_keyblock(&p, end, kctx->enc)) goto out_err;
192         if (write_keyblock(&p, end, kctx->seq)) goto out_err;
193
194         buf->length = p - (char *)buf->value;
195         return 0;
196 out_err:
197         printerr(0, "ERROR: failed serializing krb5 context for kernel\n");
198         if (buf->value) free(buf->value);
199         buf->length = 0;
200         return -1;
201 }
202
203 #endif /* HAVE_KRB5 */
204 #endif /* HAVE_LUCID_CONTEXT_SUPPORT */