xref: /onnv-gate/usr/src/lib/gss_mechs/mech_krb5/krb5/os/ccdefname.c (revision 4271:dda1ded496b7)
10Sstevel@tonic-gate /*
2*4271Srie  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
70Sstevel@tonic-gate 
80Sstevel@tonic-gate /*
90Sstevel@tonic-gate  * lib/krb5/os/ccdefname.c
100Sstevel@tonic-gate  *
110Sstevel@tonic-gate  * Copyright 1990 by the Massachusetts Institute of Technology.
120Sstevel@tonic-gate  * All Rights Reserved.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * Export of this software from the United States of America may
150Sstevel@tonic-gate  *   require a specific license from the United States Government.
160Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
170Sstevel@tonic-gate  *   export to obtain such a license before exporting.
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
200Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
210Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
220Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
230Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
240Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
250Sstevel@tonic-gate  * to distribution of the software without specific, written prior
260Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
270Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
280Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
290Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
300Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
310Sstevel@tonic-gate  * or implied warranty.
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * Return default cred. cache name.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
37781Sgtb /*
38781Sgtb  * SUNW14resync - because of changes specific to Solaris, future
39781Sgtb  * resyncs should leave this file "as is" if possible.
40781Sgtb  */
41781Sgtb 
420Sstevel@tonic-gate #include <k5-int.h>
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * Solaris kerberos:  use dirent.h to get maximum filename length MAXNAMLEN
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate #include <dirent.h>
490Sstevel@tonic-gate 
get_from_os(char * name_buf,int name_size)50781Sgtb static krb5_error_code get_from_os(
51781Sgtb 	char *name_buf,
52781Sgtb 	int name_size)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	krb5_error_code retval;
550Sstevel@tonic-gate 
56*4271Srie 	/*
57*4271Srie 	 * Solaris Kerberos
58*4271Srie 	 * Use krb5_getuid() to select the mechanism to obtain the uid.
59*4271Srie 	 */
60*4271Srie 	retval = snprintf(name_buf, name_size, "FILE:/tmp/krb5cc_%d",
61*4271Srie 	    krb5_getuid());
620Sstevel@tonic-gate 	KRB5_LOG(KRB5_INFO, "get_from_os() FILE=%s\n", name_buf);
630Sstevel@tonic-gate 	if (retval < 0)
640Sstevel@tonic-gate 		return retval;
650Sstevel@tonic-gate 	else
660Sstevel@tonic-gate 		return 0;
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*ARGSUSED*/
70781Sgtb krb5_error_code KRB5_CALLCONV
krb5_cc_set_default_name(krb5_context context,const char * name)71781Sgtb krb5_cc_set_default_name(
72781Sgtb 	krb5_context context,
73781Sgtb 	const char *name)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate 	char name_buf[MAXNAMLEN];
760Sstevel@tonic-gate 	char *new_name = getenv(KRB5_ENV_CCNAME);
770Sstevel@tonic-gate 	int name_length;
780Sstevel@tonic-gate 	krb5_error_code retval;
790Sstevel@tonic-gate 	krb5_os_context os_ctx;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if (!context || context->magic != KV5M_CONTEXT)
820Sstevel@tonic-gate 		return KV5M_CONTEXT;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	os_ctx = context->os_context;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	/*
870Sstevel@tonic-gate 	 * Solaris kerberos:
880Sstevel@tonic-gate 	 * Use the following in this order
890Sstevel@tonic-gate 	 *	1) name from arg
900Sstevel@tonic-gate 	 *	2) name from environment variable
910Sstevel@tonic-gate 	 *	3) name from os based on UID
920Sstevel@tonic-gate 	 * resulting string is pointed to by name
930Sstevel@tonic-gate 	 */
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	if (!name) {
960Sstevel@tonic-gate 		/* use environment variable or default */
970Sstevel@tonic-gate 		if (new_name != 0) { /* so that it is in env variable */
980Sstevel@tonic-gate 			name = new_name;
990Sstevel@tonic-gate 		} else {
1000Sstevel@tonic-gate 			retval = get_from_os(name_buf, sizeof(name_buf));
1010Sstevel@tonic-gate 			if (retval)
1020Sstevel@tonic-gate 				return retval;
1030Sstevel@tonic-gate 			name = name_buf;
1040Sstevel@tonic-gate 		}
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	name_length = strlen(name);
1080Sstevel@tonic-gate 	if (name_length >= MAXNAMLEN || name_length <=0) {
1090Sstevel@tonic-gate 		KRB5_LOG(KRB5_ERR, "krb5_cc_set_default_name() "
1100Sstevel@tonic-gate 			"bad file size %d\n", name_length);
1110Sstevel@tonic-gate 		return -1;
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 	new_name = malloc(name_length+1);
1140Sstevel@tonic-gate         if (!new_name)
1150Sstevel@tonic-gate 		return ENOMEM;
1160Sstevel@tonic-gate 	strcpy(new_name, name);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (os_ctx->default_ccname)
1190Sstevel@tonic-gate 		free(os_ctx->default_ccname);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	os_ctx->default_ccname = new_name;
1220Sstevel@tonic-gate 	return 0;
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 
126781Sgtb const char * KRB5_CALLCONV
krb5_cc_default_name(krb5_context context)127781Sgtb krb5_cc_default_name(krb5_context context)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	krb5_os_context os_ctx;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	if (!context || context->magic != KV5M_CONTEXT)
1320Sstevel@tonic-gate 		return NULL;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	os_ctx = context->os_context;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	/*
1370Sstevel@tonic-gate 	 * Solaris kerberos:  this is a bug fix for service principals.
1380Sstevel@tonic-gate 	 * We need to always fetch the ccache name.
1390Sstevel@tonic-gate 	 */
1400Sstevel@tonic-gate 	krb5_cc_set_default_name(context, NULL);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	KRB5_LOG(KRB5_INFO, "krb5_cc_default_name() FILE=%s\n",
1430Sstevel@tonic-gate         	os_ctx->default_ccname);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	return(os_ctx->default_ccname);
1460Sstevel@tonic-gate }
147