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