1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Modifications for Lustre
5 * Copyright 2007, Cluster File Systems, Inc.
7 * Author: Eric Mei <ericm@clusterfs.com>
11 * Adapted in part from MIT Kerberos 5-1.2.1 slave/kprop.c and from
12 * http://docs.sun.com/?p=/doc/816-1331/6m7oo9sms&a=view
14 * Copyright (c) 2002-2004 The Regents of the University of Michigan.
15 * All rights reserved.
17 * Andy Adamson <andros@umich.edu>
18 * J. Bruce Fields <bfields@umich.edu>
19 * Marius Aamodt Eriksen <marius@umich.edu>
20 * Kevin Coffman <kwc@umich.edu>
26 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
27 * All Rights Reserved.
29 * Export of this software from the United States of America may
30 * require a specific license from the United States Government.
31 * It is the responsibility of any person or organization contemplating
32 * export to obtain such a license before exporting.
34 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
35 * distribute this software and its documentation for any purpose and
36 * without fee is hereby granted, provided that the above copyright
37 * notice appear in all copies and that both that copyright notice and
38 * this permission notice appear in supporting documentation, and that
39 * the name of M.I.T. not be used in advertising or publicity pertaining
40 * to distribution of the software without specific, written prior
41 * permission. Furthermore if you modify this software you must label
42 * your software as modified software and not distribute it in such a
43 * fashion that it might be confused with the original M.I.T. software.
44 * M.I.T. makes no representations about the suitability of
45 * this software for any purpose. It is provided "as is" without express
46 * or implied warranty.
50 * Copyright 1994 by OpenVision Technologies, Inc.
52 * Permission to use, copy, modify, distribute, and sell this software
53 * and its documentation for any purpose is hereby granted without fee,
54 * provided that the above copyright notice appears in all copies and
55 * that both that copyright notice and this permission notice appear in
56 * supporting documentation, and that the name of OpenVision not be used
57 * in advertising or publicity pertaining to distribution of the software
58 * without specific, written prior permission. OpenVision makes no
59 * representations about the suitability of this software for any
60 * purpose. It is provided "as is" without express or implied warranty.
62 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
63 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
64 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
65 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
66 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
67 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
68 * PERFORMANCE OF THIS SOFTWARE.
73 Copyright (c) 2004 The Regents of the University of Michigan.
76 Redistribution and use in source and binary forms, with or without
77 modification, are permitted provided that the following conditions
80 1. Redistributions of source code must retain the above copyright
81 notice, this list of conditions and the following disclaimer.
82 2. Redistributions in binary form must reproduce the above copyright
83 notice, this list of conditions and the following disclaimer in the
84 documentation and/or other materials provided with the distribution.
85 3. Neither the name of the University nor the names of its
86 contributors may be used to endorse or promote products derived
87 from this software without specific prior written permission.
89 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
90 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
91 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
92 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
93 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
94 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
95 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
96 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
97 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
98 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
99 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
107 #include <sys/param.h>
108 //#include <rpc/rpc.h>
109 #include <sys/types.h>
110 #include <sys/stat.h>
111 #include <sys/utsname.h>
112 #include <sys/socket.h>
113 #include <arpa/inet.h>
124 #include <gssapi/gssapi.h>
125 #ifdef USE_PRIVATE_KRB5_FUNCTIONS
126 #include <gssapi/gssapi_krb5.h>
130 #include "lgss_utils.h"
131 #include "lgss_krb5_utils.h"
133 static void lgss_krb5_mutex_lock(void)
135 if (lgss_mutex_lock(LGSS_MUTEX_KRB5)) {
136 logmsg(LL_ERR, "can't lock process, abort!\n");
141 static void lgss_krb5_mutex_unlock(void)
143 if (lgss_mutex_unlock(LGSS_MUTEX_KRB5)) {
144 logmsg(LL_WARN, "can't unlock process, other processes "
145 "might need to wait long time\n");
151 * - currently we only support "normal" cache types: "FILE" and "MEMORY".
154 #define krb5_err_msg(code) error_message(code)
156 const char *krb5_cc_type_mem = "MEMORY:";
157 const char *krb5_cc_type_file = "FILE:";
159 char *krb5_this_realm = NULL;
160 char *krb5_keytab_file = "/etc/krb5.keytab";
161 char *krb5_cc_type = "FILE:";
162 char *krb5_cc_dir = "/tmp";
163 char *krb5_cred_prefix = "krb5cc_";
164 char *krb5_cred_root_suffix = "lustre_root";
166 struct lgss_krb5_cred {
168 int kc_remove; /* remove cache upon release */
172 int lgss_krb5_set_ccache_name(const char *ccname)
174 #ifdef USE_GSS_KRB5_CCACHE_NAME
175 unsigned int maj_stat, min_stat;
177 maj_stat = gss_krb5_ccache_name(&min_stat, ccname, NULL);
178 if (maj_stat != GSS_S_COMPLETE) {
179 logmsg(LL_ERR, "failed to set ccache name\n");
184 * Set the KRB5CCNAME environment variable to tell the krb5 code
185 * which credentials cache to use. (Instead of using the private
186 * function above for which there is no generic gssapi equivalent)
188 if (setenv("KRB5CCNAME", ccname, 1)) {
189 logmsg(LL_ERR, "set env of krb5 ccname: %s\n",
194 logmsg(LL_DEBUG, "set cc: %s\n", ccname);
199 int lgss_krb5_get_local_realm(void)
201 krb5_context context = NULL;
202 krb5_error_code code;
205 if (krb5_this_realm != NULL)
208 code = krb5_init_context(&context);
210 logmsg(LL_ERR, "init ctx: %s\n", krb5_err_msg(code));
214 code = krb5_get_default_realm(context, &krb5_this_realm);
216 logmsg(LL_ERR, "get default realm: %s\n", krb5_err_msg(code));
220 logmsg(LL_DEBUG, "Local realm: %s\n", krb5_this_realm);
223 krb5_free_context(context);
228 int princ_is_local_realm(krb5_context ctx, krb5_principal princ)
230 return (lgss_krb5_strcasecmp(krb5_princ_realm(ctx, princ),
231 krb5_this_realm) == 0);
235 int svc_princ_is_local_host(krb5_context ctx,
236 krb5_principal princ,
239 struct utsname utsbuf;
240 struct hostent *host;
242 if (krb5_princ_component(ctx, princ, 1) == NULL) {
243 logmsg(loglevel, "service principal has no host part\n");
247 if (uname(&utsbuf)) {
248 logmsg(loglevel, "get UTS name: %s\n", strerror(errno));
252 host = gethostbyname(utsbuf.nodename);
254 logmsg(loglevel, "failed to get local hostname\n");
258 if (lgss_krb5_strcasecmp(krb5_princ_component(ctx, princ, 1),
260 logmsg(loglevel, "service principal: hostname %.*s "
261 "doesn't match localhost %s\n",
262 krb5_princ_component(ctx, princ, 1)->length,
263 krb5_princ_component(ctx, princ, 1)->data,
272 int lkrb5_cc_check_tgt_princ(krb5_context ctx,
274 krb5_principal princ)
276 logmsg(LL_TRACE, "principal: realm %.*s, type %d, size %d, name %.*s\n",
277 krb5_princ_realm(ctx, princ)->length,
278 krb5_princ_realm(ctx, princ)->data,
279 krb5_princ_type(ctx, princ),
280 krb5_princ_size(ctx, princ),
281 krb5_princ_name(ctx, princ)->length,
282 krb5_princ_name(ctx, princ)->data);
285 if (krb5_princ_type(ctx, princ) != KRB5_NT_PRINCIPAL) {
286 logmsg(LL_WARN, "principal type %d is not I want\n",
287 krb5_princ_type(ctx, princ));
291 /* check local realm */
292 if (!princ_is_local_realm(ctx, princ)) {
293 logmsg(LL_WARN, "principal realm %.*s not local: %s\n",
294 krb5_princ_realm(ctx, princ)->length,
295 krb5_princ_realm(ctx, princ)->data,
300 /* if it's mds service principal, check hostname */
301 if (lgss_krb5_strcmp(krb5_princ_name(ctx, princ),
302 LGSS_SVC_MDS_STR) == 0) {
303 if (svc_princ_is_local_host(ctx, princ, LL_WARN)) {
304 logmsg(LL_WARN, "mds service principal not belongs "
308 } else if (lgss_krb5_strcmp(krb5_princ_name(ctx, princ),
309 LGSS_USR_ROOT_STR)) {
312 logmsg(LL_WARN, "unexpected krb5 cc principal name %.*s\n",
313 krb5_princ_name(ctx, princ)->length,
314 krb5_princ_name(ctx, princ)->data);
318 logmsg(LL_TRACE, "principal is OK\n");
323 * find out whether current TGT cache is valid or not
326 int lkrb5_check_root_tgt_cc(krb5_context ctx,
331 krb5_ccache tgt_ccache;
333 krb5_principal princ;
334 krb5_cc_cursor cursor;
335 krb5_error_code code;
338 int rc = -1, found = 0;
340 if (strncmp(ccname, krb5_cc_type, strlen(krb5_cc_type_file))) {
341 logmsg(LL_ERR, "unexpected cc type\n");
345 ccfile = ccname + strlen(krb5_cc_type_file);
346 logmsg(LL_TRACE, "cc file name: %s\n", ccfile);
348 /* firstly make sure the cache file is there */
349 if (stat(ccfile, &statbuf)) {
350 logmsg(LL_DEBUG, "krb5 cc %s: %s\n", ccname, strerror(errno));
354 /* prepare parsing the cache file */
355 code = krb5_cc_resolve(ctx, ccname, &tgt_ccache);
357 logmsg(LL_ERR, "resolve krb5 cc %s: %s\n",
358 ccname, krb5_err_msg(code));
362 /* checks the principal */
363 code = krb5_cc_get_principal(ctx, tgt_ccache, &princ);
365 logmsg(LL_ERR, "get cc principal: %s\n", krb5_err_msg(code));
369 if (lkrb5_cc_check_tgt_princ(ctx, tgt_ccache, princ)) {
370 logmsg(LL_WARN, "cc principal is not valid\n");
377 code = krb5_cc_start_seq_get(ctx, tgt_ccache, &cursor);
379 logmsg(LL_ERR, "start cc iteration: %s\n", krb5_err_msg(code));
385 krb5_timestamp duration, delta;
387 code = krb5_cc_next_cred(ctx, tgt_ccache, &cursor, &cred);
391 logmsg(LL_DEBUG, "cred: server realm %.*s, type %d, name %.*s; "
392 "time (%d-%d, renew till %d), valid %d\n",
393 krb5_princ_realm(ctx, cred.server)->length,
394 krb5_princ_realm(ctx, cred.server)->data,
395 krb5_princ_type(ctx, cred.server),
396 krb5_princ_name(ctx, cred.server)->length,
397 krb5_princ_name(ctx, cred.server)->data,
398 cred.times.starttime, cred.times.endtime,
399 cred.times.renew_till, cred.times.endtime - now);
402 * we found the princ type is always 0 (KRB5_NT_UNKNOWN), why???
405 /* FIXME how about inter-realm TGT??? FIXME */
406 if (lgss_krb5_strcasecmp(krb5_princ_name(ctx, cred.server),
410 if (lgss_krb5_strcasecmp(krb5_princ_realm(ctx, cred.server),
414 /* check validity of time */
415 delta = 60 * 30; /* half an hour */
416 duration = cred.times.endtime - cred.times.starttime;
417 if (duration / 4 < delta)
418 delta = duration / 4;
420 if (cred.times.starttime <= now &&
421 cred.times.endtime >= now + delta) {
428 logmsg(LL_DEBUG, "doesn't find good TGT cache\n");
432 /* found a good cred, store it into @ccache */
433 logmsg(LL_DEBUG, "found good TGT cache\n");
435 code = krb5_cc_initialize(ctx, ccache, princ);
437 logmsg(LL_ERR, "init private cc: %s\n", krb5_err_msg(code));
441 code = krb5_cc_store_cred(ctx, ccache, &cred);
443 logmsg(LL_ERR, "store private cred: %s\n", krb5_err_msg(code));
447 logmsg(LL_DEBUG, "store private ccache OK\n");
451 krb5_cc_end_seq_get(ctx, tgt_ccache, &cursor);
453 krb5_free_principal(ctx, princ);
455 krb5_cc_close(ctx, tgt_ccache);
461 int lkrb5_get_root_tgt_keytab(krb5_context ctx,
464 krb5_principal princ,
467 krb5_get_init_creds_opt opts;
469 krb5_ccache tgt_ccache;
470 krb5_error_code code;
473 krb5_get_init_creds_opt_init(&opts);
474 krb5_get_init_creds_opt_set_address_list(&opts, NULL);
476 * by default krb5 library obtain ticket with lifetime shorter
477 * than the max value. we can change it here if we want. but
478 * seems not necessary now.
480 krb5_get_init_creds_opt_set_tkt_life(&opts, very-long-time);
485 * obtain TGT and store into global ccache
487 code = krb5_get_init_creds_keytab(ctx, &cred, princ, kt,
490 logmsg(LL_ERR, "failed to get root TGT for "
491 "principal %.*s: %s\n",
492 krb5_princ_name(ctx, princ)->length,
493 krb5_princ_name(ctx, princ)->data,
498 code = krb5_cc_resolve(ctx, ccname, &tgt_ccache);
500 logmsg(LL_ERR, "resolve cc %s: %s\n",
501 ccname, krb5_err_msg(code));
505 code = krb5_cc_initialize(ctx, tgt_ccache, princ);
507 logmsg(LL_ERR, "initialize cc %s: %s\n",
508 ccname, krb5_err_msg(code));
512 code = krb5_cc_store_cred(ctx, tgt_ccache, &cred);
514 logmsg(LL_ERR, "store cred to cc %s: %s\n",
515 ccname, krb5_err_msg(code));
519 logmsg(LL_INFO, "installed TGT of %.*s in cc %s\n",
520 krb5_princ_name(ctx, princ)->length,
521 krb5_princ_name(ctx, princ)->data,
525 * now store the cred into my own cc too
527 code = krb5_cc_initialize(ctx, ccache, princ);
529 logmsg(LL_ERR, "init mem cc: %s\n", krb5_err_msg(code));
533 code = krb5_cc_store_cred(ctx, ccache, &cred);
535 logmsg(LL_ERR, "store mm cred: %s\n", krb5_err_msg(code));
539 logmsg(LL_DEBUG, "stored TGT into mem cc OK\n");
542 krb5_cc_close(ctx, tgt_ccache);
544 krb5_free_cred_contents(ctx, &cred);
549 * obtain a new root TGT
552 int lkrb5_refresh_root_tgt_cc(krb5_context ctx,
557 krb5_keytab_entry kte;
558 krb5_kt_cursor cursor;
559 krb5_principal princ = NULL;
560 krb5_error_code code;
563 /* prepare parsing the keytab file */
564 code = krb5_kt_resolve(ctx, krb5_keytab_file, &kt);
566 logmsg(LL_ERR, "resolve keytab %s: %s\n",
567 krb5_keytab_file, krb5_err_msg(code));
571 code = krb5_kt_start_seq_get(ctx, kt, &cursor);
573 logmsg(LL_ERR, "start kt iteration: %s\n", krb5_err_msg(code));
577 /* iterate keytab to find proper a entry */
579 code = krb5_kt_next_entry(ctx, kt, &kte, &cursor);
583 logmsg(LL_TRACE, "kt entry: realm %.*s, type %d, "
584 "size %d, name %.*s\n",
585 krb5_princ_realm(ctx, kte.principal)->length,
586 krb5_princ_realm(ctx, kte.principal)->data,
587 krb5_princ_type(ctx, kte.principal),
588 krb5_princ_size(ctx, kte.principal),
589 krb5_princ_name(ctx, kte.principal)->length,
590 krb5_princ_name(ctx, kte.principal)->data);
592 if (!princ_is_local_realm(ctx, kte.principal))
595 /* lustre_root@realm */
596 if (lgss_krb5_strcmp(krb5_princ_name(ctx, kte.principal),
597 LGSS_USR_ROOT_STR) == 0) {
599 logmsg(LL_WARN, "already picked one? "
600 "how could it possible???\n");
604 code = krb5_copy_principal(ctx, kte.principal, &princ);
606 logmsg(LL_ERR, "copy lustre_root princ: %s\n",
612 /* lustre_mds/host@realm */
613 if (lgss_krb5_strcmp(krb5_princ_name(ctx, kte.principal),
614 LGSS_SVC_MDS_STR) == 0) {
615 krb5_principal princ2;
617 if (svc_princ_is_local_host(ctx, kte.principal,
619 logmsg(LL_TRACE, "mds service principal: "
620 "not belongs to this node\n");
624 /* select this one */
625 code = krb5_copy_principal(ctx, kte.principal, &princ2);
627 logmsg(LL_ERR, "copy lustre_mds princ: %s\n",
633 logmsg(LL_TRACE, "release a lustre_root one\n");
634 krb5_free_principal(ctx, princ);
641 krb5_kt_end_seq_get(ctx, kt, &cursor);
644 logmsg(LL_ERR, "can't find proper keytab entry\n");
648 /* obtain root TGT */
649 rc = lkrb5_get_root_tgt_keytab(ctx, ccache, kt, princ, ccname);
651 krb5_free_principal(ctx, princ);
653 krb5_kt_close(ctx, kt);
658 int lkrb5_prepare_root_cred(struct lgss_cred *cred)
662 krb5_error_code code;
663 struct lgss_krb5_cred *kcred;
667 lassert(krb5_this_realm != NULL);
669 kcred = (struct lgss_krb5_cred *) cred->lc_mech_cred;
671 /* compose the TGT cc name */
672 snprintf(tgtcc, sizeof(tgtcc), "%s%s/%s%s_%s",
673 krb5_cc_type, krb5_cc_dir, krb5_cred_prefix,
674 krb5_cred_root_suffix, krb5_this_realm);
675 logmsg(LL_DEBUG, "root krb5 TGT ccname: %s\n", tgtcc);
677 /* compose the memory cc name, since the only user is myself,
678 * the name could be fixed
680 snprintf(kcred->kc_ccname, sizeof(kcred->kc_ccname),
681 "%s/self", krb5_cc_type_mem);
682 logmsg(LL_TRACE, "private cc: %s\n", kcred->kc_ccname);
684 code = krb5_init_context(&ctx);
686 logmsg(LL_ERR, "initialize krb5 context: %s\n",
691 code = krb5_cc_resolve(ctx, kcred->kc_ccname, &ccache);
693 logmsg(LL_ERR, "resolve krb5 cc %s: %s\n",
694 kcred->kc_ccname, krb5_err_msg(code));
699 * search and/or obtain root TGT credential.
700 * it touched global (on-disk) tgt cache, do it inside mutex locking
702 lgss_krb5_mutex_lock();
704 rc = lkrb5_check_root_tgt_cc(ctx, ccache, tgtcc);
706 rc = lkrb5_refresh_root_tgt_cc(ctx, ccache, tgtcc);
709 rc = lgss_krb5_set_ccache_name(kcred->kc_ccname);
711 lgss_krb5_mutex_unlock();
713 krb5_cc_close(ctx, ccache);
715 krb5_free_context(ctx);
717 logmsg(LL_DEBUG, "prepare root credentail %s\n", rc ? "failed" : "OK");
722 int lkrb5_prepare_user_cred(struct lgss_cred *cred)
724 struct lgss_krb5_cred *kcred;
727 lassert(krb5_this_realm == NULL);
729 kcred = (struct lgss_krb5_cred *) cred->lc_mech_cred;
732 * here we just specified a fix ccname, instead of searching
733 * entire cc dir. is this OK??
735 snprintf(kcred->kc_ccname, sizeof(kcred->kc_ccname),
737 krb5_cc_type, krb5_cc_dir, krb5_cred_prefix, cred->lc_uid);
738 logmsg(LL_DEBUG, "using krb5 cache name: %s\n", kcred->kc_ccname);
740 rc = lgss_krb5_set_ccache_name(kcred->kc_ccname);
742 logmsg(LL_ERR, "can't set krb5 ccache name: %s\n",
749 int lgss_krb5_prepare_cred(struct lgss_cred *cred)
751 struct lgss_krb5_cred *kcred;
754 kcred = malloc(sizeof(*kcred));
756 logmsg(LL_ERR, "can't allocate krb5 cred\n");
760 kcred->kc_ccname[0] = '\0';
761 kcred->kc_remove = 0;
762 cred->lc_mech_cred = kcred;
764 if (cred->lc_fl_root || cred->lc_fl_mds) {
765 if (lgss_krb5_get_local_realm())
768 rc = lkrb5_prepare_root_cred(cred);
770 rc = lkrb5_prepare_user_cred(cred);
777 void lgss_krb5_release_cred(struct lgss_cred *cred)
779 struct lgss_krb5_cred *kcred;
781 kcred = (struct lgss_krb5_cred *) cred->lc_mech_cred;
784 cred->lc_mech_cred = NULL;
787 struct lgss_mech_type lgss_mech_krb5 =
790 .lmt_mech_n = LGSS_MECH_KRB5,
791 .lmt_prepare_cred = lgss_krb5_prepare_cred,
792 .lmt_release_cred = lgss_krb5_release_cred,