Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/auth_gss.c

raw symbols SCIP
1 /*
2 auth_gss.c
3
4 RPCSEC_GSS client routines.
5
6 Copyright (c) 2000 The Regents of the University of Michigan.
7 All rights reserved.
8
9 Copyright (c) 2000 Dug Song <[email protected]>.
10 All rights reserved, all wrongs reversed.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions
14 are met:
15
16 1. Redistributions of source code must retain the above copyright
17 notice, this list of conditions and the following disclaimer.
18 2. Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
21 3. Neither the name of the University nor the names of its
22 contributors may be used to endorse or promote products derived
23 from this software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
37 */
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <rpc/types.h>
45 #include <rpc/xdr.h>
46 #include <rpc/auth.h>
47 #include <rpc/auth_gss.h>
48 #include <rpc/rpcsec_gss.h>
49 #include <rpc/clnt.h>
50 #include <netinet/in.h>
51 #include <reentrant.h>
52
53 #include "debug.h"
54
55 static void authgss_nextverf(AUTH *);
56 static bool_t authgss_marshal(AUTH *, XDR *);
57 static bool_t authgss_refresh(AUTH *, void *);
58 static bool_t authgss_validate(AUTH *, struct opaque_auth *);
59 static void authgss_destroy(AUTH *);
60 static void authgss_destroy_context(AUTH *);
61 static bool_t authgss_wrap(AUTH *, XDR *, xdrproc_t, caddr_t);
62 static bool_t authgss_unwrap(AUTH *, XDR *, xdrproc_t, caddr_t);
63
64 void rpc_gss_set_error(int);
65 void rpc_gss_clear_error(void);
66 bool_t rpc_gss_oid_to_mech(rpc_gss_OID, char **);
67
68 /*
69 * from mit-krb5-1.2.1 mechglue/mglueP.h:
70 * Array of context IDs typed by mechanism OID
71 */
72 typedef struct gss_union_ctx_id_t {
73 gss_OID mech_type;
74 gss_ctx_id_t internal_ctx_id;
75 } gss_union_ctx_id_desc, *gss_union_ctx_id_t;
76
77 static struct auth_ops authgss_ops = {
78 authgss_nextverf,
79 authgss_marshal,
80 authgss_validate,
81 authgss_refresh,
82 authgss_destroy,
83 authgss_wrap,
84 authgss_unwrap
85 };
86
87
88 /* useful as i add more mechanisms */
89 void
90 print_rpc_gss_sec(struct rpc_gss_sec *ptr)
91 {
92 int i;
93 char *p;
94
95 if (libtirpc_debug_level < 4 || log_stderr == 0)
96 return;
97
98 gss_log_debug("rpc_gss_sec:");
99 if(ptr->mech == NULL)
100 gss_log_debug("NULL gss_OID mech");
101 else {
102 fprintf(stderr, " mechanism_OID: {");
103 p = (char *)ptr->mech->elements;
104 for (i=0; i < ptr->mech->length; i++)
105 /* First byte of OIDs encoded to save a byte */
106 if (i == 0) {
107 int first, second;
108 if (*p < 40) {
109 first = 0;
110 second = *p;
111 }
112 else if (40 <= *p && *p < 80) {
113 first = 1;
114 second = *p - 40;
115 }
116 else if (80 <= *p && *p < 127) {
117 first = 2;
118 second = *p - 80;
119 }
120 else {
121 /* Invalid value! */
122 first = -1;
123 second = -1;
124 }
125 fprintf(stderr, " %u %u", first, second);
126 p++;
127 }
128 else {
129 fprintf(stderr, " %u", (unsigned char)*p++);
130 }
131 fprintf(stderr, " }\n");
132 }
133 fprintf(stderr, " qop: %d\n", ptr->qop);
134 fprintf(stderr, " service: %d\n", ptr->svc);
135 fprintf(stderr, " cred: %p\n", ptr->cred);
136 }
137
138 extern pthread_mutex_t auth_ref_lock;
139
140 struct rpc_gss_data {
141 bool_t established; /* context established */
142 gss_buffer_desc gc_wire_verf; /* save GSS_S_COMPLETE NULL RPC verfier
143 * to process at end of context negotiation*/
144 CLIENT *clnt; /* client handle */
145 gss_name_t name; /* service name */
146 struct rpc_gss_sec sec; /* security tuple */
147 gss_ctx_id_t ctx; /* context id */
148 struct rpc_gss_cred gc; /* client credentials */
149 u_int win; /* sequence window */
150 int time_req; /* init_sec_context time_req */
151 gss_channel_bindings_t icb; /* input channel bindings */
152 int refcnt; /* reference count gss AUTHs */
153 };
154
155 #define AUTH_PRIVATE(auth) ((struct rpc_gss_data *)auth->ah_private)
156
157 static struct timeval AUTH_TIMEOUT = { 25, 0 };
158
159 static void
160 authgss_auth_get(AUTH *auth)
161 {
162 struct rpc_gss_data *gd = AUTH_PRIVATE(auth);
163
164 mutex_lock(&auth_ref_lock);
165 ++gd->refcnt;
166 mutex_unlock(&auth_ref_lock);
167 }
168
169 static int
170 authgss_auth_put(AUTH *auth)
171 {
172 int refcnt;
173 struct rpc_gss_data *gd = AUTH_PRIVATE(auth);
174
175 mutex_lock(&auth_ref_lock);
176 refcnt = --gd->refcnt;
177 mutex_unlock(&auth_ref_lock);
178 return refcnt;
179 }
180
181 AUTH *
182 authgss_create(CLIENT *clnt, gss_name_t name, struct rpc_gss_sec *sec)
183 {
184 AUTH *auth, *save_auth;
185 struct rpc_gss_data *gd;
186 OM_uint32 min_stat = 0;
187
188 gss_log_debug("in authgss_create()");
189
190 memset(&rpc_createerr, 0, sizeof(rpc_createerr));
191
192 if ((auth = calloc(sizeof(*auth), 1)) == NULL) {
193 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
194 rpc_createerr.cf_error.re_errno = ENOMEM;
195 return (NULL);
196 }
197 if ((gd = calloc(sizeof(*gd), 1)) == NULL) {
198 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
199 rpc_createerr.cf_error.re_errno = ENOMEM;
200 free(auth);
201 return (NULL);
202 }
203 LIBTIRPC_DEBUG(3, ("authgss_create: name is %p", name));
204 if (name != GSS_C_NO_NAME) {
205 if (gss_duplicate_name(&min_stat, name, &gd->name)
206 != GSS_S_COMPLETE) {
207 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
208 rpc_createerr.cf_error.re_errno = ENOMEM;
209 free(auth);
210 free(gd);
211 return (NULL);
212 }
213 }
214 else
215 gd->name = name;
216
217 LIBTIRPC_DEBUG(3, ("authgss_create: gd->name is %p", gd->name));
218 gd->clnt = clnt;
219 gd->ctx = GSS_C_NO_CONTEXT;
220 gd->sec = *sec;
221
222 gd->gc.gc_v = RPCSEC_GSS_VERSION;
223 gd->gc.gc_proc = RPCSEC_GSS_INIT;
224 gd->gc.gc_svc = gd->sec.svc;
225
226 auth->ah_ops = &authgss_ops;
227 auth->ah_private = (caddr_t)gd;
228
229 save_auth = clnt->cl_auth;
230 clnt->cl_auth = auth;
231
232 if (!authgss_refresh(auth, NULL))
233 auth = NULL;
234 else
235 authgss_auth_get(auth); /* Reference for caller */
236
237 clnt->cl_auth = save_auth;
238
239 return (auth);
240 }
241
242 AUTH *
243 authgss_create_default(CLIENT *clnt, char *service, struct rpc_gss_sec *sec)
244 {
245 AUTH *auth;
246 OM_uint32 maj_stat = 0, min_stat = 0;
247 gss_buffer_desc sname;
248 gss_name_t name = GSS_C_NO_NAME;
249
250 gss_log_debug("in authgss_create_default()");
251
252
253 sname.value = service;
254 sname.length = strlen(service);
255
256 maj_stat = gss_import_name(&min_stat, &sname,
257 (gss_OID)GSS_C_NT_HOSTBASED_SERVICE,
258 &name);
259
260 if (maj_stat != GSS_S_COMPLETE) {
261 gss_log_status("authgss_create_default: gss_import_name",
262 maj_stat, min_stat);
263 rpc_createerr.cf_stat = RPC_AUTHERROR;
264 return (NULL);
265 }
266
267 auth = authgss_create(clnt, name, sec);
268
269 if (name != GSS_C_NO_NAME) {
270 LIBTIRPC_DEBUG(3, ("authgss_create_default: freeing name %p", name));
271 gss_release_name(&min_stat, &name);
272 }
273
274 return (auth);
275 }
276
277 bool_t
278 authgss_get_private_data(AUTH *auth, struct authgss_private_data *pd)
279 {
280 struct rpc_gss_data *gd;
281
282 gss_log_debug("in authgss_get_private_data()");
283
284 if (!auth || !pd)
285 return (FALSE);
286
287 gd = AUTH_PRIVATE(auth);
288
289 if (!gd || !gd->established)
290 return (FALSE);
291
292 pd->pd_ctx = gd->ctx;
293 pd->pd_ctx_hndl = gd->gc.gc_ctx;
294 pd->pd_seq_win = gd->win;
295 /*
296 * We've given this away -- don't try to use it ourself any more
297 * Caller should call authgss_free_private_data to free data.
298 * This also ensures that authgss_destroy_context() won't try to
299 * send an RPCSEC_GSS_DESTROY request which might inappropriately
300 * destroy the context.
301 */
302 gd->ctx = GSS_C_NO_CONTEXT;
303 gd->gc.gc_ctx.length = 0;
304 gd->gc.gc_ctx.value = NULL;
305
306 return (TRUE);
307 }
308
309 bool_t
310 authgss_free_private_data(struct authgss_private_data *pd)
311 {
312 OM_uint32 min_stat;
313 gss_log_debug("in authgss_free_private_data()");
314
315 if (!pd)
316 return (FALSE);
317
318 if (pd->pd_ctx != GSS_C_NO_CONTEXT)
319 gss_delete_sec_context(&min_stat, &pd->pd_ctx, NULL);
320 gss_release_buffer(&min_stat, &pd->pd_ctx_hndl);
321 memset(&pd->pd_ctx_hndl, 0, sizeof(pd->pd_ctx_hndl));
322 pd->pd_seq_win = 0;
323
324 return (TRUE);
325 }
326
327 static void
328 authgss_nextverf(AUTH *auth)
329 {
330 gss_log_debug("in authgss_nextverf()");
331 /* no action necessary */
332 }
333
334 static bool_t
335 authgss_marshal(AUTH *auth, XDR *xdrs)
336 {
337 XDR tmpxdrs;
338 char tmp[MAX_AUTH_BYTES];
339 struct rpc_gss_data *gd;
340 gss_buffer_desc rpcbuf, checksum;
341 OM_uint32 maj_stat, min_stat;
342 bool_t xdr_stat;
343
344 gss_log_debug("in authgss_marshal()");
345
346 gd = AUTH_PRIVATE(auth);
347
348 if (gd->established)
349 gd->gc.gc_seq++;
350
351 xdrmem_create(&tmpxdrs, tmp, sizeof(tmp), XDR_ENCODE);
352
353 if (!xdr_rpc_gss_cred(&tmpxdrs, &gd->gc)) {
354 XDR_DESTROY(&tmpxdrs);
355 return (FALSE);
356 }
357 auth->ah_cred.oa_flavor = RPCSEC_GSS;
358 auth->ah_cred.oa_base = tmp;
359 auth->ah_cred.oa_length = XDR_GETPOS(&tmpxdrs);
360
361 XDR_DESTROY(&tmpxdrs);
362
363 if (!xdr_opaque_auth(xdrs, &auth->ah_cred))
364 return (FALSE);
365
366 if (gd->gc.gc_proc == RPCSEC_GSS_INIT ||
367 gd->gc.gc_proc == RPCSEC_GSS_CONTINUE_INIT) {
368 return (xdr_opaque_auth(xdrs, &_null_auth));
369 }
370 /* Checksum serialized RPC header, up to and including credential. */
371 rpcbuf.length = XDR_GETPOS(xdrs);
372 XDR_SETPOS(xdrs, 0);
373 rpcbuf.value = XDR_INLINE(xdrs, rpcbuf.length);
374
375 maj_stat = gss_get_mic(&min_stat, gd->ctx, gd->sec.qop,
376 &rpcbuf, &checksum);
377
378 if (maj_stat != GSS_S_COMPLETE) {
379 gss_log_status("authgss_marshal: gss_get_mic",
380 maj_stat, min_stat);
381 if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
382 gd->established = FALSE;
383 authgss_destroy_context(auth);
384 }
385 return (FALSE);
386 }
387 auth->ah_verf.oa_flavor = RPCSEC_GSS;
388 auth->ah_verf.oa_base = checksum.value;
389 auth->ah_verf.oa_length = checksum.length;
390
391 xdr_stat = xdr_opaque_auth(xdrs, &auth->ah_verf);
392 gss_release_buffer(&min_stat, &checksum);
393
394 return (xdr_stat);
395 }
396
397 static bool_t
398 authgss_validate(AUTH *auth, struct opaque_auth *verf)
399 {
400 struct rpc_gss_data *gd;
401 u_int num, qop_state;
402 gss_buffer_desc signbuf, checksum;
403 OM_uint32 maj_stat, min_stat;
404
405 gss_log_debug("in authgss_validate()");
406
407 gd = AUTH_PRIVATE(auth);
408
409 if (gd->established == FALSE) {
410 /* would like to do this only on NULL rpc --
411 * gc->established is good enough.
412 * save the on the wire verifier to validate last
413 * INIT phase packet after decode if the major
414 * status is GSS_S_COMPLETE
415 */
416 if ((gd->gc_wire_verf.value =
417 mem_alloc(verf->oa_length)) == NULL) {
418 fprintf(stderr, "gss_validate: out of memory\n");
419 return (FALSE);
420 }
421 memcpy(gd->gc_wire_verf.value, verf->oa_base, verf->oa_length);
422 gd->gc_wire_verf.length = verf->oa_length;
423 return (TRUE);
424 }
425
426 if (gd->gc.gc_proc == RPCSEC_GSS_INIT ||
427 gd->gc.gc_proc == RPCSEC_GSS_CONTINUE_INIT) {
428 num = htonl(gd->win);
429 }
430 else num = htonl(gd->gc.gc_seq);
431
432 signbuf.value = &num;
433 signbuf.length = sizeof(num);
434
435 checksum.value = verf->oa_base;
436 checksum.length = verf->oa_length;
437
438 maj_stat = gss_verify_mic(&min_stat, gd->ctx, &signbuf,
439 &checksum, &qop_state);
440
441 if (maj_stat != GSS_S_COMPLETE || qop_state != gd->sec.qop) {
442 gss_log_status("authgss_validate: gss_verify_mic",
443 maj_stat, min_stat);
444 if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
445 gd->established = FALSE;
446 authgss_destroy_context(auth);
447 }
448 return (FALSE);
449 }
450 return (TRUE);
451 }
452
453 static bool_t
454 _rpc_gss_refresh(AUTH *auth, rpc_gss_options_ret_t *options_ret)
455 {
456 struct rpc_gss_data *gd;
457 struct rpc_gss_init_res gr;
458 gss_buffer_desc *recv_tokenp, send_token;
459 OM_uint32 maj_stat, min_stat, call_stat, ret_flags,
460 time_ret;
461 gss_OID actual_mech_type;
462 char *mechanism;
463
464 gss_log_debug("in authgss_refresh()");
465
466 gd = AUTH_PRIVATE(auth);
467
468 if (gd->established)
469 return (TRUE);
470
471 /* GSS context establishment loop. */
472 memset(&gr, 0, sizeof(gr));
473 recv_tokenp = GSS_C_NO_BUFFER;
474
475 print_rpc_gss_sec(&gd->sec);
476
477 for (;;) {
478 /* print the token we just received */
479 if (recv_tokenp != GSS_C_NO_BUFFER) {
480 gss_log_debug("The token we just received (length %lu):",
481 recv_tokenp->length);
482 gss_log_hexdump(recv_tokenp->value, recv_tokenp->length, 0);
483 }
484 maj_stat = gss_init_sec_context(&min_stat,
485 gd->sec.cred,
486 &gd->ctx,
487 gd->name,
488 gd->sec.mech,
489 gd->sec.req_flags,
490 gd->time_req,
491 gd->icb,
492 recv_tokenp,
493 &actual_mech_type,
494 &send_token,
495 &ret_flags,
496 &time_ret);
497
498 if (recv_tokenp != GSS_C_NO_BUFFER) {
499 gss_release_buffer(&min_stat, &gr.gr_token);
500 recv_tokenp = GSS_C_NO_BUFFER;
501 }
502 if (maj_stat != GSS_S_COMPLETE &&
503 maj_stat != GSS_S_CONTINUE_NEEDED) {
504 gss_log_status("gss_init_sec_context", maj_stat, min_stat);
505 options_ret->major_status = maj_stat;
506 options_ret->minor_status = min_stat;
507 break;
508 }
509 if (send_token.length != 0) {
510 memset(&gr, 0, sizeof(gr));
511
512 /* print the token we are about to send */
513 gss_log_debug("The token being sent (length %lu):",
514 send_token.length);
515 gss_log_hexdump(send_token.value, send_token.length, 0);
516
517 call_stat = clnt_call(gd->clnt, NULLPROC,
518 (xdrproc_t)xdr_rpc_gss_init_args,
519 &send_token,
520 (xdrproc_t)xdr_rpc_gss_init_res,
521 (caddr_t)&gr, AUTH_TIMEOUT);
522
523 gss_release_buffer(&min_stat, &send_token);
524
525 if (call_stat != RPC_SUCCESS ||
526 (gr.gr_major != GSS_S_COMPLETE &&
527 gr.gr_major != GSS_S_CONTINUE_NEEDED)) {
528 options_ret->major_status = gr.gr_major;
529 options_ret->minor_status = gr.gr_minor;
530 if (call_stat != RPC_SUCCESS) {
531 struct rpc_err err;
532 clnt_geterr(gd->clnt, &err);
533 LIBTIRPC_DEBUG(1, ("authgss_refresh: %s errno: %s",
534 clnt_sperrno(call_stat), strerror(err.re_errno)));
535 } else
536 gss_log_status("authgss_refresh:",
537 gr.gr_major, gr.gr_minor);
538 return FALSE;
539 }
540
541 if (gr.gr_ctx.length != 0) {
542 if (gd->gc.gc_ctx.value)
543 gss_release_buffer(&min_stat,
544 &gd->gc.gc_ctx);
545 gd->gc.gc_ctx = gr.gr_ctx;
546 }
547 if (gr.gr_token.length != 0) {
548 if (maj_stat != GSS_S_CONTINUE_NEEDED)
549 break;
550 recv_tokenp = &gr.gr_token;
551 }
552 gd->gc.gc_proc = RPCSEC_GSS_CONTINUE_INIT;
553 }
554
555 /* GSS_S_COMPLETE => check gss header verifier,
556 * usually checked in gss_validate
557 */
558 if (maj_stat == GSS_S_COMPLETE) {
559 gss_buffer_desc bufin;
560 gss_buffer_desc bufout;
561 u_int seq, qop_state = 0;
562
563 seq = htonl(gr.gr_win);
564 bufin.value = (unsigned char *)&seq;
565 bufin.length = sizeof(seq);
566 bufout.value = (unsigned char *)gd->gc_wire_verf.value;
567 bufout.length = gd->gc_wire_verf.length;
568
569 maj_stat = gss_verify_mic(&min_stat, gd->ctx,
570 &bufin, &bufout, &qop_state);
571
572 if (maj_stat != GSS_S_COMPLETE
573 || qop_state != gd->sec.qop) {
574 gss_log_status("authgss_refresh: gss_verify_mic",
575 maj_stat, min_stat);
576 if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
577 gd->established = FALSE;
578 authgss_destroy_context(auth);
579 }
580 rpc_gss_set_error(EPERM);
581 options_ret->major_status = maj_stat;
582 options_ret->minor_status = min_stat;
583 return (FALSE);
584 }
585
586 options_ret->major_status = GSS_S_COMPLETE;
587 options_ret->minor_status = 0;
588 options_ret->rpcsec_version = gd->gc.gc_v;
589 options_ret->ret_flags = ret_flags;
590 options_ret->time_ret = time_ret;
591 options_ret->gss_context = gd->ctx;
592 options_ret->actual_mechanism[0] = '\0';
593 if (rpc_gss_oid_to_mech(actual_mech_type, &mechanism)) {
594 strncpy(options_ret->actual_mechanism,
595 mechanism,
596 (sizeof(options_ret->actual_mechanism)-1));
597 }
598
599 gd->established = TRUE;
600 gd->gc.gc_proc = RPCSEC_GSS_DATA;
601 gd->gc.gc_seq = 0;
602 gd->win = gr.gr_win;
603 break;
604 }
605 }
606 /* End context negotiation loop. */
607 if (gd->gc.gc_proc != RPCSEC_GSS_DATA) {
608 if (gr.gr_token.length != 0)
609 gss_release_buffer(&min_stat, &gr.gr_token);
610
611 authgss_destroy(auth);
612 auth = NULL;
613 rpc_createerr.cf_stat = RPC_AUTHERROR;
614 rpc_gss_set_error(EPERM);
615
616 return (FALSE);
617 }
618 return (TRUE);
619 }
620
621 static bool_t
622 authgss_refresh(AUTH *auth, void *dummy)
623 {
624 rpc_gss_options_ret_t ret;
625
626 memset(&ret, 0, sizeof(ret));
627 return _rpc_gss_refresh(auth, &ret);
628 }
629
630 bool_t
631 authgss_service(AUTH *auth, int svc)
632 {
633 struct rpc_gss_data *gd;
634
635 gss_log_debug("in authgss_service()");
636
637 if (!auth)
638 return(FALSE);
639 gd = AUTH_PRIVATE(auth);
640 if (!gd || !gd->established)
641 return (FALSE);
642 gd->sec.svc = svc;
643 gd->gc.gc_svc = svc;
644 return (TRUE);
645 }
646
647 static void
648 authgss_destroy_context(AUTH *auth)
649 {
650 struct rpc_gss_data *gd;
651 OM_uint32 min_stat;
652
653 gss_log_debug("in authgss_destroy_context()");
654
655 gd = AUTH_PRIVATE(auth);
656
657 if (gd->gc.gc_ctx.length != 0) {
658 if (gd->established) {
659 AUTH *save_auth = NULL;
660
661 /* Make sure we use the right auth_ops */
662 if (gd->clnt->cl_auth != auth) {
663 save_auth = gd->clnt->cl_auth;
664 gd->clnt->cl_auth = auth;
665 }
666
667 gd->gc.gc_proc = RPCSEC_GSS_DESTROY;
668 clnt_call(gd->clnt, NULLPROC, (xdrproc_t)xdr_void, NULL,
669 (xdrproc_t)xdr_void, NULL, AUTH_TIMEOUT);
670
671 if (save_auth != NULL)
672 gd->clnt->cl_auth = save_auth;
673 }
674 gss_release_buffer(&min_stat, &gd->gc.gc_ctx);
675 /* XXX ANDROS check size of context - should be 8 */
676 memset(&gd->gc.gc_ctx, 0, sizeof(gd->gc.gc_ctx));
677 }
678 if (gd->ctx != GSS_C_NO_CONTEXT) {
679 gss_delete_sec_context(&min_stat, &gd->ctx, NULL);
680 gd->ctx = GSS_C_NO_CONTEXT;
681 }
682
683 /* free saved wire verifier (if any) */
684 mem_free(gd->gc_wire_verf.value, gd->gc_wire_verf.length);
685 gd->gc_wire_verf.value = NULL;
686 gd->gc_wire_verf.length = 0;
687
688 gd->established = FALSE;
689 }
690
691 static void
692 authgss_destroy(AUTH *auth)
693 {
694 if(authgss_auth_put(auth) == 0)
695 {
696 struct rpc_gss_data *gd;
697 OM_uint32 min_stat;
698
699 gss_log_debug("in authgss_destroy()");
700
701 gd = AUTH_PRIVATE(auth);
702
703 authgss_destroy_context(auth);
704
705 LIBTIRPC_DEBUG(3, ("authgss_destroy: freeing name %p", gd->name));
706 if (gd->name != GSS_C_NO_NAME)
707 gss_release_name(&min_stat, &gd->name);
708
709 free(gd);
710 free(auth);
711 }
712 }
713
714 static bool_t
715 authgss_wrap(AUTH *auth, XDR *xdrs, xdrproc_t xdr_func, caddr_t xdr_ptr)
716 {
717 struct rpc_gss_data *gd;
718
719 gss_log_debug("in authgss_wrap()");
720
721 gd = AUTH_PRIVATE(auth);
722
723 if (!gd->established || gd->sec.svc == RPCSEC_GSS_SVC_NONE) {
724 return ((*xdr_func)(xdrs, xdr_ptr));
725 }
726 return (xdr_rpc_gss_data(xdrs, xdr_func, xdr_ptr,
727 gd->ctx, gd->sec.qop,
728 gd->sec.svc, gd->gc.gc_seq));
729 }
730
731 static bool_t
732 authgss_unwrap(AUTH *auth, XDR *xdrs, xdrproc_t xdr_func, caddr_t xdr_ptr)
733 {
734 struct rpc_gss_data *gd;
735
736 gss_log_debug("in authgss_unwrap()");
737
738 gd = AUTH_PRIVATE(auth);
739
740 if (!gd->established || gd->sec.svc == RPCSEC_GSS_SVC_NONE) {
741 return ((*xdr_func)(xdrs, xdr_ptr));
742 }
743 return (xdr_rpc_gss_data(xdrs, xdr_func, xdr_ptr,
744 gd->ctx, gd->sec.qop,
745 gd->sec.svc, gd->gc.gc_seq));
746 }
747
748 static AUTH *
749 _rpc_gss_seccreate_error(int system_error)
750 {
751 rpc_gss_set_error(system_error);
752 return NULL;
753 }
754
755 /*
756 * External API: Create a GSS security context for this "clnt"
757 *
758 * clnt: a valid RPC CLIENT structure
759 * principal: NUL-terminated C string containing GSS principal
760 * mechanism: NUL-terminated C string containing GSS mechanism name
761 * service: GSS security value to use
762 * qop: NUL-terminated C string containing QOP name
763 * req: pointer to additional request parameters, or NULL
764 * ret: pointer to additional results, or NULL
765 *
766 * Returns pointer to a filled in RPC AUTH structure or NULL.
767 * Caller must free returned structure with AUTH_DESTROY.
768 */
769 AUTH *
770 rpc_gss_seccreate(CLIENT *clnt, char *principal, char *mechanism,
771 rpc_gss_service_t service, char *qop,
772 rpc_gss_options_req_t *req, rpc_gss_options_ret_t *ret)
773 {
774 rpc_gss_options_ret_t options_ret;
775 OM_uint32 maj_stat, min_stat;
776 struct rpc_gss_sec sec = {
777 .req_flags = GSS_C_MUTUAL_FLAG,
778 .cred = GSS_C_NO_CREDENTIAL,
779 };
780 struct rpc_gss_data *gd;
781 AUTH *auth, *save_auth;
782 gss_buffer_desc sname;
783
784 if (clnt == NULL || principal == NULL)
785 return _rpc_gss_seccreate_error(EINVAL);
786
787 if (rpc_gss_mech_to_oid(mechanism, &sec.mech) == FALSE)
788 return NULL;
789
790 sec.qop = GSS_C_QOP_DEFAULT;
791 if (qop != NULL) {
792 u_int qop_num;
793 if (rpc_gss_qop_to_num(qop, mechanism, &qop_num) == FALSE)
794 return NULL;
795 sec.qop = qop_num;
796 }
797
798 switch (service) {
799 case rpcsec_gss_svc_none:
800 sec.svc = RPCSEC_GSS_SVC_NONE;
801 break;
802 case rpcsec_gss_svc_integrity:
803 sec.svc = RPCSEC_GSS_SVC_INTEGRITY;
804 break;
805 case rpcsec_gss_svc_privacy:
806 sec.svc = RPCSEC_GSS_SVC_PRIVACY;
807 break;
808 case rpcsec_gss_svc_default:
809 sec.svc = RPCSEC_GSS_SVC_INTEGRITY;
810 break;
811 default:
812 return _rpc_gss_seccreate_error(ENOENT);
813 }
814
815 if (ret == NULL)
816 ret = &options_ret;
817 memset(ret, 0, sizeof(*ret));
818
819 auth = calloc(1, sizeof(*auth));
820 gd = calloc(1, sizeof(*gd));
821 if (auth == NULL || gd == NULL) {
822 free(gd);
823 free(auth);
824 return _rpc_gss_seccreate_error(ENOMEM);
825 }
826
827 sname.value = principal;
828 sname.length = strlen(principal);
829 maj_stat = gss_import_name(&min_stat, &sname,
830 (gss_OID)GSS_C_NT_HOSTBASED_SERVICE,
831 &gd->name);
832 if (maj_stat != GSS_S_COMPLETE) {
833 ret->major_status = maj_stat;
834 ret->minor_status = min_stat;
835 free(gd);
836 free(auth);
837 return _rpc_gss_seccreate_error(ENOMEM);
838 }
839
840 gd->clnt = clnt;
841 gd->ctx = GSS_C_NO_CONTEXT;
842 gd->sec = sec;
843
844 if (req) {
845 gd->sec.req_flags = req->req_flags;
846 gd->time_req = req->time_req;
847 gd->sec.cred = req->my_cred;
848 gd->icb = req->input_channel_bindings;
849 }
850
851 gd->gc.gc_v = RPCSEC_GSS_VERSION;
852 gd->gc.gc_proc = RPCSEC_GSS_INIT;
853 gd->gc.gc_svc = sec.svc;
854
855 auth->ah_ops = &authgss_ops;
856 auth->ah_private = (caddr_t)gd;
857
858 save_auth = clnt->cl_auth;
859 clnt->cl_auth = auth;
860
861 if (_rpc_gss_refresh(auth, ret) == FALSE) {
862 auth = NULL;
863 } else {
864 rpc_gss_clear_error();
865 authgss_auth_get(auth);
866 }
867
868 clnt->cl_auth = save_auth;
869
870 return auth;
871 }
872
873 /*
874 * External API: Modify an AUTH's service and QOP settings
875 *
876 * auth: a valid RPC AUTH structure
877 * service: GSS security value to use
878 * qop: NUL-terminated C string containing QOP name
879 *
880 * Returns TRUE if successful, otherwise FALSE is returned.
881 */
882 bool_t
883 rpc_gss_set_defaults(AUTH *auth, rpc_gss_service_t service, char *qop)
884 {
885 struct rpc_gss_data *gd;
886 char *mechanism;
887 u_int qop_num;
888
889 if (auth == NULL) {
890 rpc_gss_set_error(EINVAL);
891 return FALSE;
892 }
893
894 gd = AUTH_PRIVATE(auth);
895 if (gd == NULL) {
896 rpc_gss_set_error(EINVAL);
897 return FALSE;
898 }
899
900 if (rpc_gss_oid_to_mech(gd->sec.mech, &mechanism) == FALSE)
901 return FALSE;
902
903 qop_num = GSS_C_QOP_DEFAULT;
904 if (qop != NULL) {
905 if (rpc_gss_qop_to_num(qop, mechanism, &qop_num) == FALSE)
906 return FALSE;
907 }
908
909 switch (service) {
910 case rpcsec_gss_svc_none:
911 gd->gc.gc_svc = gd->sec.svc = RPCSEC_GSS_SVC_NONE;
912 break;
913 case rpcsec_gss_svc_integrity:
914 gd->gc.gc_svc = gd->sec.svc = RPCSEC_GSS_SVC_INTEGRITY;
915 break;
916 case rpcsec_gss_svc_privacy:
917 gd->gc.gc_svc = gd->sec.svc = RPCSEC_GSS_SVC_PRIVACY;
918 break;
919 case rpcsec_gss_svc_default:
920 gd->gc.gc_svc = gd->sec.svc = RPCSEC_GSS_SVC_INTEGRITY;
921 break;
922 default:
923 rpc_gss_set_error(ENOENT);
924 return FALSE;
925 }
926
927 gd->sec.qop = (gss_qop_t)qop_num;
928 rpc_gss_clear_error();
929 return TRUE;
930 }
931
932 /*
933 * External API: Return maximum data size for a security mechanism and transport
934 *
935 * auth: a valid RPC AUTH structure
936 * maxlen: transport's maximum data size, in bytes
937 *
938 * Returns maximum data size given transformations done by current
939 * security setting of "auth", in bytes, or zero if that value
940 * cannot be determined.
941 */
942 int
943 rpc_gss_max_data_length(AUTH *auth, int maxlen)
944 {
945 OM_uint32 max_input_size, maj_stat, min_stat;
946 struct rpc_gss_data *gd;
947 int conf_req_flag;
948 int result;
949
950 if (auth == NULL) {
951 rpc_gss_set_error(EINVAL);
952 return 0;
953 }
954
955 gd = AUTH_PRIVATE(auth);
956 if (gd == NULL) {
957 rpc_gss_set_error(EINVAL);
958 return 0;
959 }
960
961 switch (gd->sec.svc) {
962 case RPCSEC_GSS_SVC_NONE:
963 rpc_gss_clear_error();
964 return maxlen;
965 case RPCSEC_GSS_SVC_INTEGRITY:
966 conf_req_flag = 0;
967 break;
968 case RPCSEC_GSS_SVC_PRIVACY:
969 conf_req_flag = 1;
970 break;
971 default:
972 rpc_gss_set_error(ENOENT);
973 return 0;
974 }
975
976 result = 0;
977 maj_stat = gss_wrap_size_limit(&min_stat, gd->ctx, conf_req_flag,
978 gd->sec.qop, maxlen, &max_input_size);
979 if (maj_stat == GSS_S_COMPLETE)
980 if ((int)max_input_size > 0)
981 result = (int)max_input_size;
982 rpc_gss_clear_error();
983 return result;
984 }
985
986 bool_t
987 is_authgss_client(CLIENT *clnt)
988 {
989 return (clnt->cl_auth->ah_ops == &authgss_ops);
990 }