xref: /titanic_51/usr/src/lib/gss_mechs/mech_krb5/krb5/os/ccdefname.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
1 /*
2  * Copyright 2007 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 	/*
57 	 * Solaris Kerberos
58 	 * Use krb5_getuid() to select the mechanism to obtain the uid.
59 	 */
60 	retval = snprintf(name_buf, name_size, "FILE:/tmp/krb5cc_%d",
61 	    krb5_getuid());
62 	KRB5_LOG(KRB5_INFO, "get_from_os() FILE=%s\n", name_buf);
63 	if (retval < 0)
64 		return retval;
65 	else
66 		return 0;
67 }
68 
69 /*ARGSUSED*/
70 krb5_error_code KRB5_CALLCONV
71 krb5_cc_set_default_name(
72 	krb5_context context,
73 	const char *name)
74 {
75 	char name_buf[MAXNAMLEN];
76 	char *new_name = getenv(KRB5_ENV_CCNAME);
77 	int name_length;
78 	krb5_error_code retval;
79 	krb5_os_context os_ctx;
80 
81 	if (!context || context->magic != KV5M_CONTEXT)
82 		return KV5M_CONTEXT;
83 
84 	os_ctx = context->os_context;
85 
86 	/*
87 	 * Solaris kerberos:
88 	 * Use the following in this order
89 	 *	1) name from arg
90 	 *	2) name from environment variable
91 	 *	3) name from os based on UID
92 	 * resulting string is pointed to by name
93 	 */
94 
95 	if (!name) {
96 		/* use environment variable or default */
97 		if (new_name != 0) { /* so that it is in env variable */
98 			name = new_name;
99 		} else {
100 			retval = get_from_os(name_buf, sizeof(name_buf));
101 			if (retval)
102 				return retval;
103 			name = name_buf;
104 		}
105 	}
106 
107 	name_length = strlen(name);
108 	if (name_length >= MAXNAMLEN || name_length <=0) {
109 		KRB5_LOG(KRB5_ERR, "krb5_cc_set_default_name() "
110 			"bad file size %d\n", name_length);
111 		return -1;
112 	}
113 	new_name = malloc(name_length+1);
114         if (!new_name)
115 		return ENOMEM;
116 	strcpy(new_name, name);
117 
118 	if (os_ctx->default_ccname)
119 		free(os_ctx->default_ccname);
120 
121 	os_ctx->default_ccname = new_name;
122 	return 0;
123 }
124 
125 
126 const char * KRB5_CALLCONV
127 krb5_cc_default_name(krb5_context context)
128 {
129 	krb5_os_context os_ctx;
130 
131 	if (!context || context->magic != KV5M_CONTEXT)
132 		return NULL;
133 
134 	os_ctx = context->os_context;
135 
136 	/*
137 	 * Solaris kerberos:  this is a bug fix for service principals.
138 	 * We need to always fetch the ccache name.
139 	 */
140 	krb5_cc_set_default_name(context, NULL);
141 
142 	KRB5_LOG(KRB5_INFO, "krb5_cc_default_name() FILE=%s\n",
143         	os_ctx->default_ccname);
144 
145 	return(os_ctx->default_ccname);
146 }
147