1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*7c478bd9Sstevel@tonic-gate 
8*7c478bd9Sstevel@tonic-gate /*
9*7c478bd9Sstevel@tonic-gate  * lib/krb5/os/ccdefname.c
10*7c478bd9Sstevel@tonic-gate  *
11*7c478bd9Sstevel@tonic-gate  * Copyright 1990 by the Massachusetts Institute of Technology.
12*7c478bd9Sstevel@tonic-gate  * All Rights Reserved.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
15*7c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
16*7c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
17*7c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
18*7c478bd9Sstevel@tonic-gate  *
19*7c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
20*7c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
21*7c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
22*7c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
23*7c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
24*7c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
25*7c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
26*7c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
27*7c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
28*7c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
29*7c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
30*7c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
31*7c478bd9Sstevel@tonic-gate  * or implied warranty.
32*7c478bd9Sstevel@tonic-gate  *
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * Return default cred. cache name.
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <k5-int.h>
38*7c478bd9Sstevel@tonic-gate #include <stdio.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*
41*7c478bd9Sstevel@tonic-gate  * Solaris kerberos:  use dirent.h to get maximum filename length MAXNAMLEN
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate #include <dirent.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate static krb5_error_code get_from_os(char *name_buf, int name_size)
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate 	krb5_error_code retval;
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 	retval = snprintf(name_buf, name_size, "FILE:/tmp/krb5cc_%d", getuid());
50*7c478bd9Sstevel@tonic-gate 	KRB5_LOG(KRB5_INFO, "get_from_os() FILE=%s\n", name_buf);
51*7c478bd9Sstevel@tonic-gate 	if (retval < 0)
52*7c478bd9Sstevel@tonic-gate 		return retval;
53*7c478bd9Sstevel@tonic-gate 	else
54*7c478bd9Sstevel@tonic-gate 		return 0;
55*7c478bd9Sstevel@tonic-gate }
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
58*7c478bd9Sstevel@tonic-gate KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
59*7c478bd9Sstevel@tonic-gate krb5_cc_set_default_name(context, name)
60*7c478bd9Sstevel@tonic-gate 	krb5_context context;
61*7c478bd9Sstevel@tonic-gate 	const char *name;
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate 	char name_buf[MAXNAMLEN];
64*7c478bd9Sstevel@tonic-gate 	char *new_name = getenv(KRB5_ENV_CCNAME);
65*7c478bd9Sstevel@tonic-gate 	int name_length;
66*7c478bd9Sstevel@tonic-gate 	krb5_error_code retval;
67*7c478bd9Sstevel@tonic-gate 	krb5_os_context os_ctx;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	if (!context || context->magic != KV5M_CONTEXT)
70*7c478bd9Sstevel@tonic-gate 		return KV5M_CONTEXT;
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	os_ctx = context->os_context;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	/*
75*7c478bd9Sstevel@tonic-gate 	 * Solaris kerberos:
76*7c478bd9Sstevel@tonic-gate 	 * Use the following in this order
77*7c478bd9Sstevel@tonic-gate 	 *	1) name from arg
78*7c478bd9Sstevel@tonic-gate 	 *	2) name from environment variable
79*7c478bd9Sstevel@tonic-gate 	 *	3) name from os based on UID
80*7c478bd9Sstevel@tonic-gate 	 * resulting string is pointed to by name
81*7c478bd9Sstevel@tonic-gate 	 */
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	if (!name) {
84*7c478bd9Sstevel@tonic-gate 		/* use environment variable or default */
85*7c478bd9Sstevel@tonic-gate 		if (new_name != 0) { /* so that it is in env variable */
86*7c478bd9Sstevel@tonic-gate 			name = new_name;
87*7c478bd9Sstevel@tonic-gate 		} else {
88*7c478bd9Sstevel@tonic-gate 			retval = get_from_os(name_buf, sizeof(name_buf));
89*7c478bd9Sstevel@tonic-gate 			if (retval)
90*7c478bd9Sstevel@tonic-gate 				return retval;
91*7c478bd9Sstevel@tonic-gate 			name = name_buf;
92*7c478bd9Sstevel@tonic-gate 		}
93*7c478bd9Sstevel@tonic-gate 	}
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	name_length = strlen(name);
96*7c478bd9Sstevel@tonic-gate 	if (name_length >= MAXNAMLEN || name_length <=0) {
97*7c478bd9Sstevel@tonic-gate 		KRB5_LOG(KRB5_ERR, "krb5_cc_set_default_name() "
98*7c478bd9Sstevel@tonic-gate 			"bad file size %d\n", name_length);
99*7c478bd9Sstevel@tonic-gate 		return -1;
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 	new_name = malloc(name_length+1);
102*7c478bd9Sstevel@tonic-gate         if (!new_name)
103*7c478bd9Sstevel@tonic-gate 		return ENOMEM;
104*7c478bd9Sstevel@tonic-gate 	strcpy(new_name, name);
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	if (!os_ctx->default_ccname
107*7c478bd9Sstevel@tonic-gate 	    || (strcmp(os_ctx->default_ccname, new_name) != 0)) {
108*7c478bd9Sstevel@tonic-gate 		/* the ccache changed... forget the old principal */
109*7c478bd9Sstevel@tonic-gate 		if (os_ctx->default_ccprincipal)
110*7c478bd9Sstevel@tonic-gate 			krb5_free_principal (context, os_ctx->default_ccprincipal);
111*7c478bd9Sstevel@tonic-gate 		os_ctx->default_ccprincipal = 0;  /* we don't care until we use it */
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	if (os_ctx->default_ccname)
115*7c478bd9Sstevel@tonic-gate 		free(os_ctx->default_ccname);
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	os_ctx->default_ccname = new_name;
118*7c478bd9Sstevel@tonic-gate 	return 0;
119*7c478bd9Sstevel@tonic-gate }
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate KRB5_DLLIMP const char FAR * KRB5_CALLCONV
123*7c478bd9Sstevel@tonic-gate krb5_cc_default_name(context)
124*7c478bd9Sstevel@tonic-gate     krb5_context context;
125*7c478bd9Sstevel@tonic-gate {
126*7c478bd9Sstevel@tonic-gate 	krb5_os_context os_ctx;
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	if (!context || context->magic != KV5M_CONTEXT)
129*7c478bd9Sstevel@tonic-gate 		return NULL;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	os_ctx = context->os_context;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	/*
134*7c478bd9Sstevel@tonic-gate 	 * Solaris kerberos:  this is a bug fix for service principals.
135*7c478bd9Sstevel@tonic-gate 	 * We need to always fetch the ccache name.
136*7c478bd9Sstevel@tonic-gate 	 */
137*7c478bd9Sstevel@tonic-gate 	krb5_cc_set_default_name(context, NULL);
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	KRB5_LOG(KRB5_INFO, "krb5_cc_default_name() FILE=%s\n",
140*7c478bd9Sstevel@tonic-gate         	os_ctx->default_ccname);
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	return(os_ctx->default_ccname);
143*7c478bd9Sstevel@tonic-gate }
144