17c478bd9Sstevel@tonic-gate /*
2*24da5b34Srie  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate /*
97c478bd9Sstevel@tonic-gate  * lib/krb5/os/ccdefname.c
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * Copyright 1990 by the Massachusetts Institute of Technology.
127c478bd9Sstevel@tonic-gate  * All Rights Reserved.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
157c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
167c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
177c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
207c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
217c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
227c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
237c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
247c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
257c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
267c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
277c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
287c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
297c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
307c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
317c478bd9Sstevel@tonic-gate  * or implied warranty.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * Return default cred. cache name.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
37505d05c7Sgtb /*
38505d05c7Sgtb  * SUNW14resync - because of changes specific to Solaris, future
39505d05c7Sgtb  * resyncs should leave this file "as is" if possible.
40505d05c7Sgtb  */
41505d05c7Sgtb 
427c478bd9Sstevel@tonic-gate #include <k5-int.h>
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * Solaris kerberos:  use dirent.h to get maximum filename length MAXNAMLEN
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate #include <dirent.h>
497c478bd9Sstevel@tonic-gate 
50505d05c7Sgtb static krb5_error_code get_from_os(
51505d05c7Sgtb 	char *name_buf,
52505d05c7Sgtb 	int name_size)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	krb5_error_code retval;
557c478bd9Sstevel@tonic-gate 
56*24da5b34Srie 	/*
57*24da5b34Srie 	 * Solaris Kerberos
58*24da5b34Srie 	 * Use krb5_getuid() to select the mechanism to obtain the uid.
59*24da5b34Srie 	 */
60*24da5b34Srie 	retval = snprintf(name_buf, name_size, "FILE:/tmp/krb5cc_%d",
61*24da5b34Srie 	    krb5_getuid());
627c478bd9Sstevel@tonic-gate 	KRB5_LOG(KRB5_INFO, "get_from_os() FILE=%s\n", name_buf);
637c478bd9Sstevel@tonic-gate 	if (retval < 0)
647c478bd9Sstevel@tonic-gate 		return retval;
657c478bd9Sstevel@tonic-gate 	else
667c478bd9Sstevel@tonic-gate 		return 0;
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
70505d05c7Sgtb krb5_error_code KRB5_CALLCONV
71505d05c7Sgtb krb5_cc_set_default_name(
72505d05c7Sgtb 	krb5_context context,
73505d05c7Sgtb 	const char *name)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	char name_buf[MAXNAMLEN];
767c478bd9Sstevel@tonic-gate 	char *new_name = getenv(KRB5_ENV_CCNAME);
777c478bd9Sstevel@tonic-gate 	int name_length;
787c478bd9Sstevel@tonic-gate 	krb5_error_code retval;
797c478bd9Sstevel@tonic-gate 	krb5_os_context os_ctx;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if (!context || context->magic != KV5M_CONTEXT)
827c478bd9Sstevel@tonic-gate 		return KV5M_CONTEXT;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	os_ctx = context->os_context;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/*
877c478bd9Sstevel@tonic-gate 	 * Solaris kerberos:
887c478bd9Sstevel@tonic-gate 	 * Use the following in this order
897c478bd9Sstevel@tonic-gate 	 *	1) name from arg
907c478bd9Sstevel@tonic-gate 	 *	2) name from environment variable
917c478bd9Sstevel@tonic-gate 	 *	3) name from os based on UID
927c478bd9Sstevel@tonic-gate 	 * resulting string is pointed to by name
937c478bd9Sstevel@tonic-gate 	 */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	if (!name) {
967c478bd9Sstevel@tonic-gate 		/* use environment variable or default */
977c478bd9Sstevel@tonic-gate 		if (new_name != 0) { /* so that it is in env variable */
987c478bd9Sstevel@tonic-gate 			name = new_name;
997c478bd9Sstevel@tonic-gate 		} else {
1007c478bd9Sstevel@tonic-gate 			retval = get_from_os(name_buf, sizeof(name_buf));
1017c478bd9Sstevel@tonic-gate 			if (retval)
1027c478bd9Sstevel@tonic-gate 				return retval;
1037c478bd9Sstevel@tonic-gate 			name = name_buf;
1047c478bd9Sstevel@tonic-gate 		}
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	name_length = strlen(name);
1087c478bd9Sstevel@tonic-gate 	if (name_length >= MAXNAMLEN || name_length <=0) {
1097c478bd9Sstevel@tonic-gate 		KRB5_LOG(KRB5_ERR, "krb5_cc_set_default_name() "
1107c478bd9Sstevel@tonic-gate 			"bad file size %d\n", name_length);
1117c478bd9Sstevel@tonic-gate 		return -1;
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 	new_name = malloc(name_length+1);
1147c478bd9Sstevel@tonic-gate         if (!new_name)
1157c478bd9Sstevel@tonic-gate 		return ENOMEM;
1167c478bd9Sstevel@tonic-gate 	strcpy(new_name, name);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (os_ctx->default_ccname)
1197c478bd9Sstevel@tonic-gate 		free(os_ctx->default_ccname);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	os_ctx->default_ccname = new_name;
1227c478bd9Sstevel@tonic-gate 	return 0;
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 
126505d05c7Sgtb const char * KRB5_CALLCONV
127505d05c7Sgtb krb5_cc_default_name(krb5_context context)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	krb5_os_context os_ctx;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (!context || context->magic != KV5M_CONTEXT)
1327c478bd9Sstevel@tonic-gate 		return NULL;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	os_ctx = context->os_context;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/*
1377c478bd9Sstevel@tonic-gate 	 * Solaris kerberos:  this is a bug fix for service principals.
1387c478bd9Sstevel@tonic-gate 	 * We need to always fetch the ccache name.
1397c478bd9Sstevel@tonic-gate 	 */
1407c478bd9Sstevel@tonic-gate 	krb5_cc_set_default_name(context, NULL);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	KRB5_LOG(KRB5_INFO, "krb5_cc_default_name() FILE=%s\n",
1437c478bd9Sstevel@tonic-gate         	os_ctx->default_ccname);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	return(os_ctx->default_ccname);
1467c478bd9Sstevel@tonic-gate }
147