2 * Copyright (c) 2008 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 Copyright (c) 2000 The Regents of the University of Michigan.
34 Copyright (c) 2000 Dug Song <dugsong@UMICH.EDU>.
35 All rights reserved, all wrongs reversed.
37 Redistribution and use in source and binary forms, with or without
38 modification, are permitted provided that the following conditions
41 1. Redistributions of source code must retain the above copyright
42 notice, this list of conditions and the following disclaimer.
43 2. Redistributions in binary form must reproduce the above copyright
44 notice, this list of conditions and the following disclaimer in the
45 documentation and/or other materials provided with the distribution.
46 3. Neither the name of the University nor the names of its
47 contributors may be used to endorse or promote products derived
48 from this software without specific prior written permission.
50 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
51 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
52 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
57 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
58 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
59 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 $Id: svc_auth_gss.c,v 1.27 2002/01/15 15:43:00 andros Exp $
72 #include <sys/queue.h>
74 #include <rpc/rpcsec_gss.h>
75 #include "rpcsec_gss_int.h"
77 static bool_t svc_rpc_gss_initialised
= FALSE
;
79 static bool_t
svc_rpc_gss_wrap(SVCAUTH
*, XDR
*, xdrproc_t
, caddr_t
);
80 static bool_t
svc_rpc_gss_unwrap(SVCAUTH
*, XDR
*, xdrproc_t
, caddr_t
);
81 static enum auth_stat
svc_rpc_gss(struct svc_req
*, struct rpc_msg
*);
83 static struct svc_auth_ops svc_auth_gss_ops
= {
88 struct svc_rpc_gss_callback
{
89 SLIST_ENTRY(svc_rpc_gss_callback
) cb_link
;
90 rpc_gss_callback_t cb_callback
;
92 static SLIST_HEAD(svc_rpc_gss_callback_list
, svc_rpc_gss_callback
)
93 svc_rpc_gss_callbacks
= SLIST_HEAD_INITIALIZER(&svc_rpc_gss_callbacks
);
95 struct svc_rpc_gss_svc_name
{
96 SLIST_ENTRY(svc_rpc_gss_svc_name
) sn_link
;
100 gss_cred_id_t sn_cred
;
104 static SLIST_HEAD(svc_rpc_gss_svc_name_list
, svc_rpc_gss_svc_name
)
105 svc_rpc_gss_svc_names
= SLIST_HEAD_INITIALIZER(&svc_rpc_gss_svc_names
);
107 enum svc_rpc_gss_client_state
{
108 CLIENT_NEW
, /* still authenticating */
109 CLIENT_ESTABLISHED
, /* context established */
110 CLIENT_STALE
/* garbage to collect */
113 #define SVC_RPC_GSS_SEQWINDOW 128
115 struct svc_rpc_gss_client
{
116 TAILQ_ENTRY(svc_rpc_gss_client
) cl_link
;
117 TAILQ_ENTRY(svc_rpc_gss_client
) cl_alllink
;
119 time_t cl_expiration
; /* when to gc */
120 enum svc_rpc_gss_client_state cl_state
; /* client state */
121 bool_t cl_locked
; /* fixed service+qop */
122 gss_ctx_id_t cl_ctx
; /* context id */
123 gss_cred_id_t cl_creds
; /* delegated creds */
124 gss_name_t cl_cname
; /* client name */
125 struct svc_rpc_gss_svc_name
*cl_sname
; /* server name used */
126 rpc_gss_rawcred_t cl_rawcred
; /* raw credentials */
127 rpc_gss_ucred_t cl_ucred
; /* unix-style credentials */
128 bool_t cl_done_callback
; /* TRUE after call */
129 void *cl_cookie
; /* user cookie from callback */
130 gid_t cl_gid_storage
[NGRPS
];
131 gss_OID cl_mech
; /* mechanism */
132 gss_qop_t cl_qop
; /* quality of protection */
133 u_int cl_seq
; /* current sequence number */
134 u_int cl_win
; /* sequence window size */
135 u_int cl_seqlast
; /* sequence window origin */
136 uint32_t cl_seqmask
[SVC_RPC_GSS_SEQWINDOW
/32]; /* bitmask of seqnums */
137 gss_buffer_desc cl_verf
; /* buffer for verf checksum */
139 TAILQ_HEAD(svc_rpc_gss_client_list
, svc_rpc_gss_client
);
141 #define CLIENT_HASH_SIZE 256
142 #define CLIENT_MAX 128
143 struct svc_rpc_gss_client_list svc_rpc_gss_client_hash
[CLIENT_HASH_SIZE
];
144 struct svc_rpc_gss_client_list svc_rpc_gss_clients
;
145 static size_t svc_rpc_gss_client_count
;
146 static uint32_t svc_rpc_gss_next_clientid
= 1;
149 static void svc_rpc_gss_init(void) __attribute__ ((constructor
));
153 svc_rpc_gss_init(void)
157 if (!svc_rpc_gss_initialised
) {
158 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++)
159 TAILQ_INIT(&svc_rpc_gss_client_hash
[i
]);
160 TAILQ_INIT(&svc_rpc_gss_clients
);
161 svc_auth_reg(RPCSEC_GSS
, svc_rpc_gss
);
162 svc_rpc_gss_initialised
= TRUE
;
167 rpc_gss_set_callback(rpc_gss_callback_t
*cb
)
169 struct svc_rpc_gss_callback
*scb
;
171 scb
= malloc(sizeof(struct svc_rpc_gss_callback
));
173 _rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR
, ENOMEM
);
176 scb
->cb_callback
= *cb
;
177 SLIST_INSERT_HEAD(&svc_rpc_gss_callbacks
, scb
, cb_link
);
183 rpc_gss_set_svc_name(const char *principal
, const char *mechanism
,
184 u_int req_time
, u_int program
, u_int version
)
186 OM_uint32 maj_stat
, min_stat
;
187 struct svc_rpc_gss_svc_name
*sname
;
188 gss_buffer_desc namebuf
;
191 gss_OID_set_desc oid_set
;
196 if (!rpc_gss_mech_to_oid(mechanism
, &mech_oid
))
199 oid_set
.elements
= mech_oid
;
201 namebuf
.value
= (void *)(intptr_t) principal
;
202 namebuf
.length
= strlen(principal
);
204 maj_stat
= gss_import_name(&min_stat
, &namebuf
,
205 GSS_C_NT_HOSTBASED_SERVICE
, &name
);
206 if (maj_stat
!= GSS_S_COMPLETE
)
209 maj_stat
= gss_acquire_cred(&min_stat
, name
,
210 req_time
, &oid_set
, GSS_C_ACCEPT
, &cred
, NULL
, NULL
);
211 if (maj_stat
!= GSS_S_COMPLETE
)
214 gss_release_name(&min_stat
, &name
);
216 sname
= malloc(sizeof(struct svc_rpc_gss_svc_name
));
219 sname
->sn_principal
= strdup(principal
);
220 sname
->sn_mech
= mech_oid
;
221 sname
->sn_req_time
= req_time
;
222 sname
->sn_cred
= cred
;
223 sname
->sn_program
= program
;
224 sname
->sn_version
= version
;
225 SLIST_INSERT_HEAD(&svc_rpc_gss_svc_names
, sname
, sn_link
);
231 rpc_gss_get_principal_name(rpc_gss_principal_t
*principal
,
232 const char *mech
, const char *name
, const char *node
, const char *domain
)
234 OM_uint32 maj_stat
, min_stat
;
238 gss_name_t gss_name
, gss_mech_name
;
239 rpc_gss_principal_t result
;
243 if (!rpc_gss_mech_to_oid(mech
, &mech_oid
))
247 * Construct a gss_buffer containing the full name formatted
248 * as "name/node@domain" where node and domain are optional.
250 namelen
= strlen(name
);
252 namelen
+= strlen(node
) + 1;
255 namelen
+= strlen(domain
) + 1;
258 buf
.value
= malloc(namelen
);
259 buf
.length
= namelen
;
260 strcpy((char *) buf
.value
, name
);
262 strcat((char *) buf
.value
, "/");
263 strcat((char *) buf
.value
, node
);
266 strcat((char *) buf
.value
, "@");
267 strcat((char *) buf
.value
, domain
);
271 * Convert that to a gss_name_t and then convert that to a
272 * mechanism name in the selected mechanism.
274 maj_stat
= gss_import_name(&min_stat
, &buf
,
275 GSS_C_NT_USER_NAME
, &gss_name
);
277 if (maj_stat
!= GSS_S_COMPLETE
) {
278 log_status("gss_import_name", mech_oid
, maj_stat
, min_stat
);
281 maj_stat
= gss_canonicalize_name(&min_stat
, gss_name
, mech_oid
,
283 if (maj_stat
!= GSS_S_COMPLETE
) {
284 log_status("gss_canonicalize_name", mech_oid
, maj_stat
,
286 gss_release_name(&min_stat
, &gss_name
);
289 gss_release_name(&min_stat
, &gss_name
);
292 * Export the mechanism name and use that to construct the
293 * rpc_gss_principal_t result.
295 maj_stat
= gss_export_name(&min_stat
, gss_mech_name
, &buf
);
296 if (maj_stat
!= GSS_S_COMPLETE
) {
297 log_status("gss_export_name", mech_oid
, maj_stat
, min_stat
);
298 gss_release_name(&min_stat
, &gss_mech_name
);
301 gss_release_name(&min_stat
, &gss_mech_name
);
303 result
= malloc(sizeof(int) + buf
.length
);
305 gss_release_buffer(&min_stat
, &buf
);
308 result
->len
= buf
.length
;
309 memcpy(result
->name
, buf
.value
, buf
.length
);
310 gss_release_buffer(&min_stat
, &buf
);
317 rpc_gss_getcred(struct svc_req
*req
, rpc_gss_rawcred_t
**rcred
,
318 rpc_gss_ucred_t
**ucred
, void **cookie
)
320 struct svc_rpc_gss_client
*client
;
322 if (req
->rq_cred
.oa_flavor
!= RPCSEC_GSS
)
325 client
= req
->rq_clntcred
;
327 *rcred
= &client
->cl_rawcred
;
329 *ucred
= &client
->cl_ucred
;
331 *cookie
= client
->cl_cookie
;
336 rpc_gss_svc_max_data_length(struct svc_req
*req
, int max_tp_unit_len
)
338 struct svc_rpc_gss_client
*client
= req
->rq_clntcred
;
341 OM_uint32 maj_stat
, min_stat
;
344 switch (client
->cl_rawcred
.service
) {
345 case rpc_gss_svc_none
:
346 return (max_tp_unit_len
);
349 case rpc_gss_svc_default
:
350 case rpc_gss_svc_integrity
:
354 case rpc_gss_svc_privacy
:
362 maj_stat
= gss_wrap_size_limit(&min_stat
, client
->cl_ctx
, want_conf
,
363 client
->cl_qop
, max_tp_unit_len
, &max
);
365 if (maj_stat
== GSS_S_COMPLETE
) {
371 log_status("gss_wrap_size_limit", client
->cl_mech
,
377 static struct svc_rpc_gss_client
*
378 svc_rpc_gss_find_client(uint32_t clientid
)
380 struct svc_rpc_gss_client
*client
;
381 struct svc_rpc_gss_client_list
*list
;
384 log_debug("in svc_rpc_gss_find_client(%d)", clientid
);
386 list
= &svc_rpc_gss_client_hash
[clientid
% CLIENT_HASH_SIZE
];
387 TAILQ_FOREACH(client
, list
, cl_link
) {
388 if (client
->cl_id
== clientid
) {
390 * Move this client to the front of the LRU
393 TAILQ_REMOVE(&svc_rpc_gss_clients
, client
, cl_alllink
);
394 TAILQ_INSERT_HEAD(&svc_rpc_gss_clients
, client
,
403 static struct svc_rpc_gss_client
*
404 svc_rpc_gss_create_client(void)
406 struct svc_rpc_gss_client
*client
;
407 struct svc_rpc_gss_client_list
*list
;
409 log_debug("in svc_rpc_gss_create_client()");
411 client
= mem_alloc(sizeof(struct svc_rpc_gss_client
));
412 memset(client
, 0, sizeof(struct svc_rpc_gss_client
));
413 client
->cl_id
= svc_rpc_gss_next_clientid
++;
414 list
= &svc_rpc_gss_client_hash
[client
->cl_id
% CLIENT_HASH_SIZE
];
415 TAILQ_INSERT_HEAD(list
, client
, cl_link
);
416 TAILQ_INSERT_HEAD(&svc_rpc_gss_clients
, client
, cl_alllink
);
419 * Start the client off with a short expiration time. We will
420 * try to get a saner value from the client creds later.
422 client
->cl_state
= CLIENT_NEW
;
423 client
->cl_locked
= FALSE
;
424 client
->cl_expiration
= time(0) + 5*60;
425 svc_rpc_gss_client_count
++;
431 svc_rpc_gss_destroy_client(struct svc_rpc_gss_client
*client
)
433 struct svc_rpc_gss_client_list
*list
;
436 log_debug("in svc_rpc_gss_destroy_client()");
439 gss_delete_sec_context(&min_stat
,
440 &client
->cl_ctx
, GSS_C_NO_BUFFER
);
442 if (client
->cl_cname
)
443 gss_release_name(&min_stat
, &client
->cl_cname
);
445 if (client
->cl_rawcred
.client_principal
)
446 free(client
->cl_rawcred
.client_principal
);
448 if (client
->cl_verf
.value
)
449 gss_release_buffer(&min_stat
, &client
->cl_verf
);
451 list
= &svc_rpc_gss_client_hash
[client
->cl_id
% CLIENT_HASH_SIZE
];
452 TAILQ_REMOVE(list
, client
, cl_link
);
453 TAILQ_REMOVE(&svc_rpc_gss_clients
, client
, cl_alllink
);
454 svc_rpc_gss_client_count
--;
455 mem_free(client
, sizeof(*client
));
459 svc_rpc_gss_timeout_clients(void)
461 struct svc_rpc_gss_client
*client
;
462 struct svc_rpc_gss_client
*nclient
;
463 time_t now
= time(0);
465 log_debug("in svc_rpc_gss_timeout_clients()");
467 * First enforce the max client limit. We keep
468 * svc_rpc_gss_clients in LRU order.
470 while (svc_rpc_gss_client_count
> CLIENT_MAX
)
471 svc_rpc_gss_destroy_client(TAILQ_LAST(&svc_rpc_gss_clients
,
472 svc_rpc_gss_client_list
));
473 TAILQ_FOREACH_SAFE(client
, &svc_rpc_gss_clients
, cl_alllink
, nclient
) {
474 if (client
->cl_state
== CLIENT_STALE
475 || now
> client
->cl_expiration
) {
476 log_debug("expiring client %p", client
);
477 svc_rpc_gss_destroy_client(client
);
484 * OID<->string routines. These are uuuuugly.
487 gss_oid_to_str(OM_uint32
*minor_status
, gss_OID oid
, gss_buffer_t oid_str
)
490 unsigned long number
;
492 size_t string_length
;
497 /* Decoded according to krb5/gssapi_krb5.c */
499 /* First determine the size of the string */
503 cp
= (unsigned char *) oid
->elements
;
504 number
= (unsigned long) cp
[0];
505 sprintf(numstr
, "%ld ", number
/40);
506 string_length
+= strlen(numstr
);
507 sprintf(numstr
, "%ld ", number
%40);
508 string_length
+= strlen(numstr
);
509 for (i
=1; i
<oid
->length
; i
++) {
510 if ( (size_t) (numshift
+7) < (sizeof(unsigned long)*8)) {
511 number
= (number
<< 7) | (cp
[i
] & 0x7f);
516 return(GSS_S_FAILURE
);
518 if ((cp
[i
] & 0x80) == 0) {
519 sprintf(numstr
, "%ld ", number
);
520 string_length
+= strlen(numstr
);
526 * If we get here, we've calculated the length of "n n n ... n ". Add 4
527 * here for "{ " and "}\0".
530 if ((bp
= (char *) malloc(string_length
))) {
532 number
= (unsigned long) cp
[0];
533 sprintf(numstr
, "%ld ", number
/40);
535 sprintf(numstr
, "%ld ", number
%40);
538 cp
= (unsigned char *) oid
->elements
;
539 for (i
=1; i
<oid
->length
; i
++) {
540 number
= (number
<< 7) | (cp
[i
] & 0x7f);
541 if ((cp
[i
] & 0x80) == 0) {
542 sprintf(numstr
, "%ld ", number
);
548 oid_str
->length
= strlen(bp
)+1;
549 oid_str
->value
= (void *) bp
;
551 return(GSS_S_COMPLETE
);
554 return(GSS_S_FAILURE
);
559 svc_rpc_gss_build_ucred(struct svc_rpc_gss_client
*client
,
560 const gss_name_t name
)
562 OM_uint32 maj_stat
, min_stat
;
565 struct passwd pwd
, *pw
;
566 rpc_gss_ucred_t
*uc
= &client
->cl_ucred
;
571 uc
->gidlist
= client
->cl_gid_storage
;
573 maj_stat
= gss_pname_to_uid(&min_stat
, name
, client
->cl_mech
, &uid
);
574 if (maj_stat
!= GSS_S_COMPLETE
)
577 getpwuid_r(uid
, &pwd
, buf
, sizeof(buf
), &pw
);
580 uc
->uid
= pw
->pw_uid
;
581 uc
->gid
= pw
->pw_gid
;
582 uc
->gidlist
= client
->cl_gid_storage
;
583 getgrouplist(pw
->pw_name
, pw
->pw_gid
, uc
->gidlist
, &len
);
589 svc_rpc_gss_accept_sec_context(struct svc_rpc_gss_client
*client
,
590 struct svc_req
*rqst
,
591 struct rpc_gss_init_res
*gr
,
592 struct rpc_gss_cred
*gc
)
594 gss_buffer_desc recv_tok
;
596 OM_uint32 maj_stat
= 0, min_stat
= 0, ret_flags
;
597 OM_uint32 cred_lifetime
;
598 struct svc_rpc_gss_svc_name
*sname
;
600 log_debug("in svc_rpc_gss_accept_context()");
602 /* Deserialize arguments. */
603 memset(&recv_tok
, 0, sizeof(recv_tok
));
605 if (!svc_getargs(rqst
->rq_xprt
,
606 (xdrproc_t
) xdr_gss_buffer_desc
,
607 (caddr_t
) &recv_tok
)) {
608 client
->cl_state
= CLIENT_STALE
;
613 * First time round, try all the server names we have until
614 * one matches. Afterwards, stick with that one.
616 if (!client
->cl_sname
) {
617 SLIST_FOREACH(sname
, &svc_rpc_gss_svc_names
, sn_link
) {
618 if (sname
->sn_program
== rqst
->rq_prog
619 && sname
->sn_version
== rqst
->rq_vers
) {
620 gr
->gr_major
= gss_accept_sec_context(
625 GSS_C_NO_CHANNEL_BINDINGS
,
632 if (gr
->gr_major
== GSS_S_COMPLETE
633 || gr
->gr_major
== GSS_S_CONTINUE_NEEDED
) {
634 client
->cl_sname
= sname
;
640 gr
->gr_major
= gss_accept_sec_context(
643 client
->cl_sname
->sn_cred
,
645 GSS_C_NO_CHANNEL_BINDINGS
,
654 xdr_free((xdrproc_t
) xdr_gss_buffer_desc
, (char *) &recv_tok
);
657 * If we get an error from gss_accept_sec_context, send the
658 * reply anyway so that the client gets a chance to see what
661 if (gr
->gr_major
!= GSS_S_COMPLETE
&&
662 gr
->gr_major
!= GSS_S_CONTINUE_NEEDED
) {
663 log_status("accept_sec_context", client
->cl_mech
,
664 gr
->gr_major
, gr
->gr_minor
);
665 client
->cl_state
= CLIENT_STALE
;
669 gr
->gr_handle
.value
= &client
->cl_id
;
670 gr
->gr_handle
.length
= sizeof(uint32_t);
671 gr
->gr_win
= SVC_RPC_GSS_SEQWINDOW
;
673 /* Save client info. */
674 client
->cl_mech
= mech
;
675 client
->cl_qop
= GSS_C_QOP_DEFAULT
;
676 client
->cl_seq
= gc
->gc_seq
;
677 client
->cl_win
= gr
->gr_win
;
678 client
->cl_done_callback
= FALSE
;
680 if (gr
->gr_major
== GSS_S_COMPLETE
) {
681 gss_buffer_desc export_name
;
684 * Change client expiration time to be near when the
685 * client creds expire (or 24 hours if we can't figure
688 if (cred_lifetime
== GSS_C_INDEFINITE
)
689 cred_lifetime
= time(0) + 24*60*60;
691 client
->cl_expiration
= time(0) + cred_lifetime
;
694 * Fill in cred details in the rawcred structure.
696 client
->cl_rawcred
.version
= RPCSEC_GSS_VERSION
;
697 rpc_gss_oid_to_mech(mech
, &client
->cl_rawcred
.mechanism
);
698 maj_stat
= gss_export_name(&min_stat
, client
->cl_cname
,
700 if (maj_stat
!= GSS_S_COMPLETE
) {
701 log_status("gss_export_name", client
->cl_mech
,
705 client
->cl_rawcred
.client_principal
=
706 malloc(sizeof(*client
->cl_rawcred
.client_principal
)
707 + export_name
.length
);
708 client
->cl_rawcred
.client_principal
->len
= export_name
.length
;
709 memcpy(client
->cl_rawcred
.client_principal
->name
,
710 export_name
.value
, export_name
.length
);
711 gss_release_buffer(&min_stat
, &export_name
);
712 client
->cl_rawcred
.svc_principal
=
713 client
->cl_sname
->sn_principal
;
714 client
->cl_rawcred
.service
= gc
->gc_svc
;
717 * Use gss_pname_to_uid to map to unix creds. For
718 * kerberos5, this uses krb5_aname_to_localname.
720 svc_rpc_gss_build_ucred(client
, client
->cl_cname
);
724 gss_buffer_desc mechname
;
726 gss_oid_to_str(&min_stat
, mech
, &mechname
);
728 log_debug("accepted context for %s with "
729 "<mech %.*s, qop %d, svc %d>",
730 client
->cl_rawcred
.client_principal
->name
,
731 mechname
.length
, (char *)mechname
.value
,
732 client
->cl_qop
, client
->rawcred
.service
);
734 gss_release_buffer(&min_stat
, &mechname
);
742 svc_rpc_gss_validate(struct svc_rpc_gss_client
*client
, struct rpc_msg
*msg
,
745 struct opaque_auth
*oa
;
746 gss_buffer_desc rpcbuf
, checksum
;
747 OM_uint32 maj_stat
, min_stat
;
749 int32_t rpchdr
[128 / sizeof(int32_t)];
752 log_debug("in svc_rpc_gss_validate()");
754 memset(rpchdr
, 0, sizeof(rpchdr
));
756 /* Reconstruct RPC header for signing (from xdr_callmsg). */
758 IXDR_PUT_LONG(buf
, msg
->rm_xid
);
759 IXDR_PUT_ENUM(buf
, msg
->rm_direction
);
760 IXDR_PUT_LONG(buf
, msg
->rm_call
.cb_rpcvers
);
761 IXDR_PUT_LONG(buf
, msg
->rm_call
.cb_prog
);
762 IXDR_PUT_LONG(buf
, msg
->rm_call
.cb_vers
);
763 IXDR_PUT_LONG(buf
, msg
->rm_call
.cb_proc
);
764 oa
= &msg
->rm_call
.cb_cred
;
765 IXDR_PUT_ENUM(buf
, oa
->oa_flavor
);
766 IXDR_PUT_LONG(buf
, oa
->oa_length
);
768 memcpy((caddr_t
)buf
, oa
->oa_base
, oa
->oa_length
);
769 buf
+= RNDUP(oa
->oa_length
) / sizeof(int32_t);
771 rpcbuf
.value
= rpchdr
;
772 rpcbuf
.length
= (u_char
*)buf
- (u_char
*)rpchdr
;
774 checksum
.value
= msg
->rm_call
.cb_verf
.oa_base
;
775 checksum
.length
= msg
->rm_call
.cb_verf
.oa_length
;
777 maj_stat
= gss_verify_mic(&min_stat
, client
->cl_ctx
, &rpcbuf
, &checksum
,
780 if (maj_stat
!= GSS_S_COMPLETE
) {
781 log_status("gss_verify_mic", client
->cl_mech
,
783 client
->cl_state
= CLIENT_STALE
;
791 svc_rpc_gss_nextverf(struct svc_rpc_gss_client
*client
,
792 struct svc_req
*rqst
, u_int seq
)
794 gss_buffer_desc signbuf
;
795 OM_uint32 maj_stat
, min_stat
;
798 log_debug("in svc_rpc_gss_nextverf()");
801 signbuf
.value
= &nseq
;
802 signbuf
.length
= sizeof(nseq
);
804 if (client
->cl_verf
.value
)
805 gss_release_buffer(&min_stat
, &client
->cl_verf
);
807 maj_stat
= gss_get_mic(&min_stat
, client
->cl_ctx
, client
->cl_qop
,
808 &signbuf
, &client
->cl_verf
);
810 if (maj_stat
!= GSS_S_COMPLETE
) {
811 log_status("gss_get_mic", client
->cl_mech
, maj_stat
, min_stat
);
812 client
->cl_state
= CLIENT_STALE
;
815 rqst
->rq_xprt
->xp_verf
.oa_flavor
= RPCSEC_GSS
;
816 rqst
->rq_xprt
->xp_verf
.oa_base
= (caddr_t
)client
->cl_verf
.value
;
817 rqst
->rq_xprt
->xp_verf
.oa_length
= (u_int
)client
->cl_verf
.length
;
823 svc_rpc_gss_callback(struct svc_rpc_gss_client
*client
, struct svc_req
*rqst
)
825 struct svc_rpc_gss_callback
*scb
;
832 * See if we have a callback for this guy.
835 SLIST_FOREACH(scb
, &svc_rpc_gss_callbacks
, cb_link
) {
836 if (scb
->cb_callback
.program
== rqst
->rq_prog
837 && scb
->cb_callback
.version
== rqst
->rq_vers
) {
839 * This one matches. Call the callback and see
840 * if it wants to veto or something.
843 lock
.raw_cred
= &client
->cl_rawcred
;
844 cb_res
= scb
->cb_callback
.callback(rqst
,
851 client
->cl_state
= CLIENT_STALE
;
857 * The callback accepted the connection - it
858 * is responsible for freeing client->cl_creds
861 client
->cl_creds
= GSS_C_NO_CREDENTIAL
;
862 client
->cl_locked
= lock
.locked
;
863 client
->cl_cookie
= cookie
;
869 * Either no callback exists for this program/version or one
870 * of the callbacks rejected the connection. We just need to
871 * clean up the delegated client creds, if any.
873 if (client
->cl_creds
) {
875 gss_release_cred(&min_ver
, &client
->cl_creds
);
881 svc_rpc_gss_check_replay(struct svc_rpc_gss_client
*client
, uint32_t seq
)
886 if (seq
<= client
->cl_seqlast
) {
888 * The request sequence number is less than
889 * the largest we have seen so far. If it is
890 * outside the window or if we have seen a
891 * request with this sequence before, silently
894 offset
= client
->cl_seqlast
- seq
;
895 if (offset
>= client
->cl_win
)
899 if (client
->cl_seqmask
[word
] & (1 << bit
))
901 client
->cl_seqmask
[word
] |= (1 << bit
);
908 svc_rpc_gss_update_seq(struct svc_rpc_gss_client
*client
, uint32_t seq
)
911 uint32_t carry
, newcarry
;
913 if (seq
> client
->cl_seqlast
) {
915 * This request has a sequence number greater
916 * than any we have seen so far. Advance the
917 * seq window and set bit zero of the window
918 * (which corresponds to the new sequence
921 offset
= seq
- client
->cl_seqlast
;
922 while (offset
> 32) {
923 for (i
= (SVC_RPC_GSS_SEQWINDOW
/ 32) - 1;
925 client
->cl_seqmask
[i
] = client
->cl_seqmask
[i
-1];
927 client
->cl_seqmask
[0] = 0;
931 for (i
= 0; i
< SVC_RPC_GSS_SEQWINDOW
/ 32; i
++) {
932 newcarry
= client
->cl_seqmask
[i
] >> (32 - offset
);
933 client
->cl_seqmask
[i
] =
934 (client
->cl_seqmask
[i
] << offset
) | carry
;
937 client
->cl_seqmask
[0] |= 1;
938 client
->cl_seqlast
= seq
;
943 svc_rpc_gss(struct svc_req
*rqst
, struct rpc_msg
*msg
)
948 struct svc_rpc_gss_client
*client
;
949 struct rpc_gss_cred gc
;
950 struct rpc_gss_init_res gr
;
953 enum auth_stat result
;
955 log_debug("in svc_rpc_gss()");
957 /* Garbage collect old clients. */
958 svc_rpc_gss_timeout_clients();
960 /* Initialize reply. */
961 rqst
->rq_xprt
->xp_verf
= _null_auth
;
963 /* Deserialize client credentials. */
964 if (rqst
->rq_cred
.oa_length
<= 0)
965 return (AUTH_BADCRED
);
967 memset(&gc
, 0, sizeof(gc
));
969 xdrmem_create(&xdrs
, rqst
->rq_cred
.oa_base
,
970 rqst
->rq_cred
.oa_length
, XDR_DECODE
);
972 if (!xdr_rpc_gss_cred(&xdrs
, &gc
)) {
974 return (AUTH_BADCRED
);
979 if (gc
.gc_version
!= RPCSEC_GSS_VERSION
) {
980 result
= AUTH_BADCRED
;
984 /* Check the proc and find the client (or create it) */
985 if (gc
.gc_proc
== RPCSEC_GSS_INIT
) {
986 client
= svc_rpc_gss_create_client();
988 if (gc
.gc_handle
.length
!= sizeof(uint32_t)) {
989 result
= AUTH_BADCRED
;
992 uint32_t *p
= gc
.gc_handle
.value
;
993 client
= svc_rpc_gss_find_client(*p
);
996 * Can't find the client - we may have
997 * destroyed it - tell the other side to
1000 result
= RPCSEC_GSS_CREDPROBLEM
;
1004 rqst
->rq_clntcred
= client
;
1007 * The service and sequence number must be ignored for
1008 * RPCSEC_GSS_INIT and RPCSEC_GSS_CONTINUE_INIT.
1010 if (gc
.gc_proc
!= RPCSEC_GSS_INIT
1011 && gc
.gc_proc
!= RPCSEC_GSS_CONTINUE_INIT
) {
1013 * Check for sequence number overflow.
1015 if (gc
.gc_seq
>= MAXSEQ
) {
1016 result
= RPCSEC_GSS_CTXPROBLEM
;
1019 client
->cl_seq
= gc
.gc_seq
;
1022 * Check for valid service.
1024 if (gc
.gc_svc
!= rpc_gss_svc_none
&&
1025 gc
.gc_svc
!= rpc_gss_svc_integrity
&&
1026 gc
.gc_svc
!= rpc_gss_svc_privacy
) {
1027 result
= AUTH_BADCRED
;
1032 /* Handle RPCSEC_GSS control procedure. */
1033 switch (gc
.gc_proc
) {
1035 case RPCSEC_GSS_INIT
:
1036 case RPCSEC_GSS_CONTINUE_INIT
:
1037 if (rqst
->rq_proc
!= NULLPROC
) {
1038 result
= AUTH_REJECTEDCRED
;
1042 memset(&gr
, 0, sizeof(gr
));
1043 if (!svc_rpc_gss_accept_sec_context(client
, rqst
, &gr
, &gc
)) {
1044 result
= AUTH_REJECTEDCRED
;
1048 if (gr
.gr_major
== GSS_S_COMPLETE
) {
1049 if (!svc_rpc_gss_nextverf(client
, rqst
, gr
.gr_win
)) {
1050 result
= AUTH_REJECTEDCRED
;
1054 rqst
->rq_xprt
->xp_verf
.oa_flavor
= AUTH_NULL
;
1055 rqst
->rq_xprt
->xp_verf
.oa_length
= 0;
1058 call_stat
= svc_sendreply(rqst
->rq_xprt
,
1059 (xdrproc_t
) xdr_rpc_gss_init_res
,
1062 gss_release_buffer(&min_stat
, &gr
.gr_token
);
1065 result
= AUTH_FAILED
;
1069 if (gr
.gr_major
== GSS_S_COMPLETE
)
1070 client
->cl_state
= CLIENT_ESTABLISHED
;
1072 result
= RPCSEC_GSS_NODISPATCH
;
1075 case RPCSEC_GSS_DATA
:
1076 case RPCSEC_GSS_DESTROY
:
1077 if (!svc_rpc_gss_check_replay(client
, gc
.gc_seq
)) {
1078 result
= RPCSEC_GSS_NODISPATCH
;
1082 if (!svc_rpc_gss_validate(client
, msg
, &qop
)) {
1083 result
= RPCSEC_GSS_CREDPROBLEM
;
1087 if (!svc_rpc_gss_nextverf(client
, rqst
, gc
.gc_seq
)) {
1088 result
= RPCSEC_GSS_CTXPROBLEM
;
1092 svc_rpc_gss_update_seq(client
, gc
.gc_seq
);
1095 * Change the SVCAUTH ops on the transport to point at
1096 * our own code so that we can unwrap the arguments
1097 * and wrap the result. The caller will re-set this on
1098 * every request to point to a set of null wrap/unwrap
1101 SVC_AUTH(rqst
->rq_xprt
).svc_ah_ops
= &svc_auth_gss_ops
;
1102 SVC_AUTH(rqst
->rq_xprt
).svc_ah_private
= client
;
1104 if (gc
.gc_proc
== RPCSEC_GSS_DATA
) {
1106 * We might be ready to do a callback to the server to
1107 * see if it wants to accept/reject the connection.
1109 if (!client
->cl_done_callback
) {
1110 client
->cl_done_callback
= TRUE
;
1111 client
->cl_qop
= qop
;
1112 client
->cl_rawcred
.qop
= _rpc_gss_num_to_qop(
1113 client
->cl_rawcred
.mechanism
, qop
);
1114 if (!svc_rpc_gss_callback(client
, rqst
)) {
1115 result
= AUTH_REJECTEDCRED
;
1121 * If the server has locked this client to a
1122 * particular service+qop pair, enforce that
1125 if (client
->cl_locked
) {
1126 if (client
->cl_rawcred
.service
!= gc
.gc_svc
) {
1127 result
= AUTH_FAILED
;
1129 } else if (client
->cl_qop
!= qop
) {
1130 result
= AUTH_BADVERF
;
1136 * If the qop changed, look up the new qop
1139 if (client
->cl_qop
!= qop
) {
1140 client
->cl_qop
= qop
;
1141 client
->cl_rawcred
.qop
= _rpc_gss_num_to_qop(
1142 client
->cl_rawcred
.mechanism
, qop
);
1146 * Make sure we use the right service value
1149 client
->cl_rawcred
.service
= gc
.gc_svc
;
1153 if (rqst
->rq_proc
!= NULLPROC
) {
1154 result
= AUTH_REJECTEDCRED
;
1158 call_stat
= svc_sendreply(rqst
->rq_xprt
,
1159 (xdrproc_t
) xdr_void
, (caddr_t
) NULL
);
1162 result
= AUTH_FAILED
;
1166 svc_rpc_gss_destroy_client(client
);
1168 result
= RPCSEC_GSS_NODISPATCH
;
1174 result
= AUTH_BADCRED
;
1178 xdr_free((xdrproc_t
) xdr_rpc_gss_cred
, (char *) &gc
);
1183 svc_rpc_gss_wrap(SVCAUTH
*auth
, XDR
*xdrs
, xdrproc_t xdr_func
, caddr_t xdr_ptr
)
1185 struct svc_rpc_gss_client
*client
;
1187 log_debug("in svc_rpc_gss_wrap()");
1189 client
= (struct svc_rpc_gss_client
*) auth
->svc_ah_private
;
1190 if (client
->cl_state
!= CLIENT_ESTABLISHED
1191 || client
->cl_rawcred
.service
== rpc_gss_svc_none
) {
1192 return xdr_func(xdrs
, xdr_ptr
);
1194 return (xdr_rpc_gss_wrap_data(xdrs
, xdr_func
, xdr_ptr
,
1195 client
->cl_ctx
, client
->cl_qop
,
1196 client
->cl_rawcred
.service
, client
->cl_seq
));
1200 svc_rpc_gss_unwrap(SVCAUTH
*auth
, XDR
*xdrs
, xdrproc_t xdr_func
, caddr_t xdr_ptr
)
1202 struct svc_rpc_gss_client
*client
;
1204 log_debug("in svc_rpc_gss_unwrap()");
1206 client
= (struct svc_rpc_gss_client
*) auth
->svc_ah_private
;
1207 if (client
->cl_state
!= CLIENT_ESTABLISHED
1208 || client
->cl_rawcred
.service
== rpc_gss_svc_none
) {
1209 return xdr_func(xdrs
, xdr_ptr
);
1211 return (xdr_rpc_gss_unwrap_data(xdrs
, xdr_func
, xdr_ptr
,
1212 client
->cl_ctx
, client
->cl_qop
,
1213 client
->cl_rawcred
.service
, client
->cl_seq
));