2 * Modifications for Lustre
4 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
6 * Copyright (c) 2011, Intel Corporation.
8 * Author: Eric Mei <ericm@clusterfs.com>
12 * Adapted in part from MIT Kerberos 5-1.2.1 slave/kprop.c and from
13 * http://docs.sun.com/?p=/doc/816-1331/6m7oo9sms&a=view
15 * Copyright (c) 2002 The Regents of the University of Michigan.
16 * All rights reserved.
18 * Andy Adamson <andros@umich.edu>
19 * J. Bruce Fields <bfields@umich.edu>
20 * Marius Aamodt Eriksen <marius@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.
74 #include <sys/types.h>
87 #include <gssapi/gssapi.h>
88 #if defined(HAVE_KRB5) && !defined(GSS_C_NT_HOSTBASED_SERVICE)
89 #include <gssapi/gssapi_generic.h>
90 #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
99 #include <libcfs/util/string.h>
101 #include "lsupport.h"
102 #include "lgss_utils.h"
103 #include "lgss_krb5_utils.h"
105 const char *lgss_svc_str[LGSS_SVC_MAX] = {
106 [LGSS_SVC_MGS] = LGSS_SVC_MGS_STR,
107 [LGSS_SVC_MDS] = LGSS_SVC_MDS_STR,
108 [LGSS_SVC_OSS] = LGSS_SVC_OSS_STR,
111 /****************************************
112 * inter-process locking *
113 ****************************************/
115 static struct lgss_mutex_s {
119 } lgss_mutexes[LGSS_MUTEX_MAX] = {
120 [LGSS_MUTEX_KRB5] = { "keyring", 0x4292d473, 0 },
123 static int lgss_mutex_get(struct lgss_mutex_s *mutex)
125 mutex->sem_id = semget(mutex->sem_key, 1, IPC_CREAT | IPC_EXCL | 0700);
126 if (mutex->sem_id != -1) {
127 if (semctl(mutex->sem_id, 0, SETVAL, 1) == -1) {
128 logmsg(LL_ERR, "initialize sem %x: %s\n",
129 mutex->sem_key, strerror(errno));
133 logmsg(LL_DEBUG, "created & initialized sem %x id %d for %s\n",
134 mutex->sem_key, mutex->sem_id, mutex->sem_name);
136 if (errno != EEXIST) {
137 logmsg(LL_ERR, "create sem %x: %s\n",
138 mutex->sem_key, strerror(errno));
142 /* already created by someone else, simply get it.
143 * Note there's still a small window of racing between create
144 * and initialize, a flaw in semaphore semantics */
145 mutex->sem_id = semget(mutex->sem_key, 0, 0700);
146 if (mutex->sem_id == -1) {
147 if (errno == ENOENT) {
148 logmsg(LL_WARN, "sem %x just disappeared "
149 "under us, try again\n", mutex->sem_key);
153 logmsg(LL_ERR, "get sem %x: %s\n", mutex->sem_key,
158 logmsg(LL_TRACE, "got sem %x id %d for %s\n",
159 mutex->sem_key, mutex->sem_id, mutex->sem_name);
165 int lgss_mutex_lock(lgss_mutex_id_t mid)
167 struct lgss_mutex_s *sem = &lgss_mutexes[mid];
168 struct sembuf sembuf;
171 lassert(mid < LGSS_MUTEX_MAX);
173 logmsg(LL_TRACE, "locking mutex %x for %s\n",
174 sem->sem_key, sem->sem_name);
177 rc = lgss_mutex_get(sem);
184 sembuf.sem_flg = SEM_UNDO;
186 if (semop(sem->sem_id, &sembuf, 1) != 0) {
187 logmsg(LL_ERR, "lock mutex %x: %s\n", sem->sem_key,
192 logmsg(LL_DEBUG, "locked mutex %x for %s\n",
193 sem->sem_key, sem->sem_name);
197 int lgss_mutex_unlock(lgss_mutex_id_t mid)
199 struct lgss_mutex_s *sem = &lgss_mutexes[mid];
200 struct sembuf sembuf;
202 lassert(mid < LGSS_MUTEX_MAX);
203 lassert(sem->sem_id >= 0);
205 logmsg(LL_TRACE, "unlocking mutex %x for %s\n",
206 sem->sem_key, sem->sem_name);
210 sembuf.sem_flg = SEM_UNDO;
212 if (semop(sem->sem_id, &sembuf, 1) != 0) {
213 logmsg(LL_ERR, "unlock mutex %x: %s\n", sem->sem_key,
218 logmsg(LL_DEBUG, "unlocked mutex %x for %s\n",
219 sem->sem_key, sem->sem_name);
223 /****************************************
225 ****************************************/
227 /* from kerberos source, gssapi_krb5.c */
228 gss_OID_desc krb5oid = {
230 .elements = "\052\206\110\206\367\022\001\002\002"
232 gss_OID_desc spkm3oid = {
234 .elements = "\053\006\001\005\005\001\003"
236 /* null and sk come from IU's oid space */
237 gss_OID_desc nulloid = {
239 .elements = "\053\006\001\004\001\311\146\215\126\001\000\000"
241 #ifdef HAVE_OPENSSL_SSK
242 gss_OID_desc skoid = {
244 .elements = "\053\006\001\004\001\311\146\215\126\001\000\001"
248 /****************************************
250 ****************************************/
252 loglevel_t g_log_level = LL_WARN;
254 static const char *const log_prefix[] = {
256 [LL_WARN] = "WARNING",
258 [LL_DEBUG] = "DEBUG",
259 [LL_TRACE] = "TRACE",
262 void lgss_set_loglevel(loglevel_t level)
264 lassert(level < LL_MAX);
268 void __logmsg(loglevel_t level, const char *func, const char *format, ...)
274 offset = scnprintf(buf, sizeof(buf), "[%d]:%s:%s(): ",
275 getpid(), log_prefix[level], func);
277 va_start(ap, format);
278 vsnprintf(buf + offset, sizeof(buf) - offset, format, ap);
281 syslog(LOG_INFO, "%s", buf);
284 void __logmsg_gss(loglevel_t level, const char *func, const gss_OID mech,
285 uint32_t major, uint32_t minor, const char *format, ...)
288 uint32_t maj_stat1, min_stat1;
289 uint32_t maj_stat2, min_stat2;
290 gss_buffer_desc maj_gss_buf = GSS_C_EMPTY_BUFFER;
291 gss_buffer_desc min_gss_buf = GSS_C_EMPTY_BUFFER;
293 char maj_buf[30], min_buf[30];
294 char *maj_msg, *min_msg;
296 uint32_t msg_ctx = 0;
298 /* Get major status message */
299 maj_stat1 = gss_display_status(&min_stat1, major, GSS_C_GSS_CODE,
300 mech, &msg_ctx, &maj_gss_buf);
301 if (maj_stat1 != GSS_S_COMPLETE) {
302 snprintf(maj_buf, sizeof(maj_buf), "(0x%08x)", major);
303 maj_msg = &maj_buf[0];
305 maj_msg = maj_gss_buf.value;
308 /* Get minor status message */
309 maj_stat2 = gss_display_status(&min_stat2, minor, GSS_C_MECH_CODE,
310 mech, &msg_ctx, &min_gss_buf);
311 if (maj_stat2 != GSS_S_COMPLETE) {
312 snprintf(min_buf, sizeof(min_buf), "(0x%08x)", minor);
313 min_msg = &min_buf[0];
315 min_msg = min_gss_buf.value;
318 /* arrange & log message */
319 offset = scnprintf(buf, sizeof(buf), "[%d]:%s:%s(): ",
320 getpid(), log_prefix[level], func);
322 va_start(ap, format);
323 offset += vscnprintf(buf + offset, sizeof(buf) - offset, format, ap);
326 snprintf(buf + offset, sizeof(buf) - offset, ": GSSAPI: %s - %s\n",
329 syslog(LOG_INFO, "%s", buf);
331 /* release buffers */
332 if (maj_gss_buf.length != 0)
333 gss_release_buffer(&min_stat1, &maj_gss_buf);
334 if (min_gss_buf.length != 0)
335 gss_release_buffer(&min_stat2, &min_gss_buf);
338 /****************************************
339 * client credentials *
340 ****************************************/
342 struct lgss_mech_type *lgss_name2mech(const char *mech_name)
344 if (strcmp(mech_name, "krb5") == 0)
345 return &lgss_mech_krb5;
346 if (strcmp(mech_name, "gssnull") == 0)
347 return &lgss_mech_null;
348 #ifdef HAVE_OPENSSL_SSK
349 if (strcmp(mech_name, "sk") == 0)
350 return &lgss_mech_sk;
355 int lgss_mech_initialize(struct lgss_mech_type *mech)
357 logmsg(LL_TRACE, "initialize mech %s\n", mech->lmt_name);
359 return mech->lmt_init();
363 void lgss_mech_finalize(struct lgss_mech_type *mech)
365 logmsg(LL_TRACE, "finalize mech %s\n", mech->lmt_name);
370 struct lgss_cred * lgss_create_cred(struct lgss_mech_type *mech)
372 struct lgss_cred *cred;
374 cred = malloc(sizeof(*cred));
376 memset(cred, 0, sizeof(*cred));
377 cred->lc_mech = mech;
380 logmsg(LL_TRACE, "create a %s cred at %p\n", mech->lmt_name, cred);
384 void lgss_destroy_cred(struct lgss_cred *cred)
386 lassert(cred->lc_mech != NULL);
387 lassert(cred->lc_mech_cred == NULL);
389 logmsg(LL_TRACE, "destroying a %s cred at %p\n",
390 cred->lc_mech->lmt_name, cred);
394 int lgss_prepare_cred(struct lgss_cred *cred)
396 struct lgss_mech_type *mech = cred->lc_mech;
398 lassert(mech != NULL);
400 logmsg(LL_TRACE, "preparing %s cred %p\n", mech->lmt_name, cred);
402 if (mech->lmt_prepare_cred)
403 return mech->lmt_prepare_cred(cred);
407 void lgss_release_cred(struct lgss_cred *cred)
409 struct lgss_mech_type *mech = cred->lc_mech;
411 lassert(mech != NULL);
413 logmsg(LL_TRACE, "releasing %s cred %p\n", mech->lmt_name, cred);
415 if (cred->lc_mech_cred) {
416 lassert(cred->lc_mech != NULL);
417 lassert(cred->lc_mech->lmt_release_cred != NULL);
419 cred->lc_mech->lmt_release_cred(cred);
423 int lgss_using_cred(struct lgss_cred *cred)
425 struct lgss_mech_type *mech = cred->lc_mech;
427 lassert(mech != NULL);
429 logmsg(LL_TRACE, "using %s cred %p\n", mech->lmt_name, cred);
431 if (mech->lmt_using_cred)
432 return mech->lmt_using_cred(cred);
436 int lgss_validate_cred(struct lgss_cred *cred, gss_buffer_desc *token,
437 gss_buffer_desc *ctx_token)
439 struct lgss_mech_type *mech = cred->lc_mech;
441 lassert(mech != NULL);
443 logmsg(LL_TRACE, "validate %s cred %p with token %p\n", mech->lmt_name,
446 if (mech->lmt_validate_cred)
447 return mech->lmt_validate_cred(cred, token, ctx_token);
452 /****************************************
454 ****************************************/
456 int lgss_get_service_str(char **string, uint32_t lsvc, uint64_t tgt_nid)
458 const int max_namelen = 512;
459 char namebuf[max_namelen];
462 lassert(*string == NULL);
464 if (lsvc >= LGSS_SVC_MAX) {
465 logmsg(LL_ERR, "invalid lgss service %d\n", lsvc);
469 if (lnet_nid2hostname(tgt_nid, namebuf, max_namelen)) {
470 logmsg(LL_ERR, "cannot resolve hostname from nid %"PRIx64"\n",
475 alloc_size = 32 + strlen(namebuf);
477 *string = malloc(alloc_size);
478 if (*string == NULL) {
479 logmsg(LL_ERR, "can't malloc %d bytes\n", alloc_size);
483 snprintf(*string, alloc_size, "%s@%s",
484 lgss_svc_str[lsvc], namebuf);
486 logmsg(LL_DEBUG, "constructed service string: %s\n", *string);