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