2 * Adapted in part from MIT Kerberos 5-1.2.1 slave/kprop.c and from
3 * http://docs.sun.com/?p=/doc/816-1331/6m7oo9sms&a=view
5 * Copyright (c) 2002 The Regents of the University of Michigan.
8 * Andy Adamson <andros@umich.edu>
9 * J. Bruce Fields <bfields@umich.edu>
10 * Marius Aamodt Eriksen <marius@umich.edu>
16 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
17 * All Rights Reserved.
19 * Export of this software from the United States of America may
20 * require a specific license from the United States Government.
21 * It is the responsibility of any person or organization contemplating
22 * export to obtain such a license before exporting.
24 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
25 * distribute this software and its documentation for any purpose and
26 * without fee is hereby granted, provided that the above copyright
27 * notice appear in all copies and that both that copyright notice and
28 * this permission notice appear in supporting documentation, and that
29 * the name of M.I.T. not be used in advertising or publicity pertaining
30 * to distribution of the software without specific, written prior
31 * permission. Furthermore if you modify this software you must label
32 * your software as modified software and not distribute it in such a
33 * fashion that it might be confused with the original M.I.T. software.
34 * M.I.T. makes no representations about the suitability of
35 * this software for any purpose. It is provided "as is" without express
36 * or implied warranty.
40 * Copyright 1994 by OpenVision Technologies, Inc.
42 * Permission to use, copy, modify, distribute, and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appears in all copies and
45 * that both that copyright notice and this permission notice appear in
46 * supporting documentation, and that the name of OpenVision not be used
47 * in advertising or publicity pertaining to distribution of the software
48 * without specific, written prior permission. OpenVision makes no
49 * representations about the suitability of this software for any
50 * purpose. It is provided "as is" without express or implied warranty.
52 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
53 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
54 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
55 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
56 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
57 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
58 * PERFORMANCE OF THIS SOFTWARE.
67 #include <sys/types.h>
70 #include <sys/socket.h>
71 #include <netinet/in.h>
72 #include <sys/param.h>
75 #include <gssapi/gssapi.h>
76 #if defined(HAVE_KRB5) && !defined(GSS_C_NT_HOSTBASED_SERVICE)
77 #include <gssapi/gssapi_generic.h>
78 #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
92 /* Global gssd_credentials handle */
93 gss_cred_id_t gssd_cred_mds;
94 gss_cred_id_t gssd_cred_oss;
95 int gssd_cred_mds_valid = 0;
96 int gssd_cred_oss_valid = 0;
98 char *mds_local_realm = NULL;
99 char *oss_local_realm = NULL;
101 gss_OID g_mechOid = GSS_C_NULL_OID;;
105 display_status_1(char *m, u_int32_t code, int type, const gss_OID mech)
107 u_int32_t maj_stat, min_stat;
108 gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
109 u_int32_t msg_ctx = 0;
116 case GSS_C_MECH_CODE:
117 typestr = "mechanism";
125 maj_stat = gss_display_status(&min_stat, code,
126 type, mech, &msg_ctx, &msg);
127 if (maj_stat != GSS_S_COMPLETE) {
128 printerr(0, "ERROR: in call to "
129 "gss_display_status called from %s\n", m);
132 printerr(0, "ERROR: GSS-API: (%s) error in %s(): %s\n",
133 typestr, m, (char *)msg.value);
137 (void) gss_release_buffer(&min_stat, &msg);
146 display_status_2(char *m, u_int32_t major, u_int32_t minor, const gss_OID mech)
148 u_int32_t maj_stat1, min_stat1;
149 u_int32_t maj_stat2, min_stat2;
150 gss_buffer_desc maj_gss_buf = GSS_C_EMPTY_BUFFER;
151 gss_buffer_desc min_gss_buf = GSS_C_EMPTY_BUFFER;
152 char maj_buf[30], min_buf[30];
154 u_int32_t msg_ctx = 0;
156 /* Get major status message */
157 maj_stat1 = gss_display_status(&min_stat1, major,
158 GSS_C_GSS_CODE, mech, &msg_ctx, &maj_gss_buf);
160 if (maj_stat1 != GSS_S_COMPLETE) {
161 snprintf(maj_buf, sizeof(maj_buf), "(0x%08x)", major);
164 maj = maj_gss_buf.value;
167 /* Get minor status message */
168 maj_stat2 = gss_display_status(&min_stat2, minor,
169 GSS_C_MECH_CODE, mech, &msg_ctx, &min_gss_buf);
171 if (maj_stat2 != GSS_S_COMPLETE) {
172 snprintf(min_buf, sizeof(min_buf), "(0x%08x)", minor);
175 min = min_gss_buf.value;
178 printerr(0, "ERROR: GSS-API: error in %s(): %s - %s\n",
181 if (maj_gss_buf.length != 0)
182 (void) gss_release_buffer(&min_stat1, &maj_gss_buf);
183 if (min_gss_buf.length != 0)
184 (void) gss_release_buffer(&min_stat2, &min_gss_buf);
188 pgsserr(char *msg, u_int32_t maj_stat, u_int32_t min_stat, const gss_OID mech)
190 display_status_2(msg, maj_stat, min_stat, mech);
194 int extract_realm_name(gss_buffer_desc *name, char **realm)
199 sname = malloc(name->length + 1);
201 printerr(0, "out of memory\n");
205 memcpy(sname, name->value, name->length);
206 sname[name->length] = '\0';
207 printerr(1, "service principal: %s\n", sname);
209 c = strchr(sname, '@');
211 printerr(2, "no realm found in principal, use default\n");
212 *realm = strdup(this_realm);
214 printerr(0, "failed to duplicate default realm\n");
221 printerr(0, "failed to duplicated realm\n");
231 int gssd_acquire_cred(char *server_name, gss_cred_id_t *cred,
232 char **local_realm, int *valid)
234 gss_buffer_desc name;
235 gss_name_t target_name;
236 u_int32_t maj_stat, min_stat;
237 u_int32_t ignore_maj_stat, ignore_min_stat;
239 gss_buffer_desc pbuf;
243 name.value = (void *)server_name;
244 name.length = strlen(server_name);
246 maj_stat = gss_import_name(&min_stat, &name,
247 (const gss_OID) GSS_C_NT_HOSTBASED_SERVICE,
250 if (maj_stat != GSS_S_COMPLETE) {
251 pgsserr("gss_import_name", maj_stat, min_stat, g_mechOid);
255 maj_stat = gss_display_name(&min_stat, target_name, &name, &name_type);
256 if (maj_stat != GSS_S_COMPLETE) {
257 pgsserr(0, maj_stat, min_stat, g_mechOid);
260 if (extract_realm_name(&name, local_realm))
263 maj_stat = gss_acquire_cred(&min_stat, target_name, 0,
264 GSS_C_NULL_OID_SET, GSS_C_ACCEPT,
267 if (maj_stat != GSS_S_COMPLETE) {
268 pgsserr("gss_acquire_cred", maj_stat, min_stat, g_mechOid);
269 ignore_maj_stat = gss_display_name(&ignore_min_stat,
270 target_name, &pbuf, NULL);
271 if (ignore_maj_stat == GSS_S_COMPLETE) {
272 printerr(0, "Unable to obtain credentials for '%.*s'\n",
273 pbuf.length, pbuf.value);
274 ignore_maj_stat = gss_release_buffer(&ignore_min_stat,
280 ignore_maj_stat = gss_release_name(&ignore_min_stat, &target_name);
282 if (maj_stat != GSS_S_COMPLETE)
287 int gssd_prepare_creds(int must_srv_mds, int must_srv_oss)
289 if (gssd_acquire_cred(GSSD_SERVICE_MDS, &gssd_cred_mds,
290 &mds_local_realm, &gssd_cred_mds_valid)) {
295 if (gssd_acquire_cred(GSSD_SERVICE_OSS, &gssd_cred_oss,
296 &oss_local_realm, &gssd_cred_oss_valid)) {
301 if (!gssd_cred_mds_valid && !gssd_cred_oss_valid) {
302 printerr(0, "can't obtain both mds & oss creds, exit\n");
306 if (gssd_cred_mds_valid)
307 printerr(0, "Ready to serve Lustre MDS in realm %s\n",
308 mds_local_realm ? mds_local_realm : "N/A");
309 if (gssd_cred_oss_valid)
310 printerr(0, "Ready to serve Lustre OSS in realm %s\n",
311 oss_local_realm ? oss_local_realm : "N/A");
316 gss_cred_id_t gssd_select_svc_cred(int lustre_svc)
318 switch (lustre_svc) {
319 case LUSTRE_GSS_SVC_MDS:
320 if (!gssd_cred_mds_valid) {
321 printerr(0, "ERROR: service cred for mds not ready\n");
324 printerr(2, "select mds service cred\n");
325 return gssd_cred_mds;
326 case LUSTRE_GSS_SVC_OSS:
327 if (!gssd_cred_oss_valid) {
328 printerr(0, "ERROR: service cred for oss not ready\n");
331 printerr(2, "select oss service cred\n");
332 return gssd_cred_oss;
334 printerr(0, "ERROR: invalid lustre svc id %d\n", lustre_svc);
340 int gssd_check_mechs(void)
342 u_int32_t maj_stat, min_stat;
343 gss_OID_set supported_mechs = GSS_C_NO_OID_SET;
346 maj_stat = gss_indicate_mechs(&min_stat, &supported_mechs);
347 if (maj_stat != GSS_S_COMPLETE) {
348 printerr(0, "Unable to obtain list of supported mechanisms. "
349 "Check that gss library is properly configured.\n");
352 if (supported_mechs == GSS_C_NO_OID_SET ||
353 supported_mechs->count == 0) {
354 printerr(0, "Unable to obtain list of supported mechanisms. "
355 "Check that gss library is properly configured.\n");
358 maj_stat = gss_release_oid_set(&min_stat, &supported_mechs);
364 /*********************************
365 * FIXME should be in krb5_util.c
366 *********************************/
368 #include "krb5_util.h"
370 /* realm of this node */
371 char *this_realm = NULL;
373 int gssd_get_local_realm(void)
375 krb5_context context = NULL;
376 krb5_error_code code;
379 if (this_realm != NULL)
382 code = krb5_init_context(&context);
384 printerr(0, "ERROR: get default realm: init ctx: %s\n",
385 error_message(code));
389 code = krb5_get_default_realm(context, &this_realm);
391 printerr(0, "ERROR: get default realm: %s\n",
392 error_message(code));
397 printerr(1, "Local realm: %s\n", this_realm);
399 krb5_free_context(context);