xref: /minix3/crypto/external/bsd/heimdal/dist/kcm/client.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: client.c,v 1.1.1.2 2014/04/24 12:45:27 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2005, PADL Software Pty Ltd.
5ebfedea0SLionel Sambuc  * All rights reserved.
6ebfedea0SLionel Sambuc  *
7ebfedea0SLionel Sambuc  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc  * are met:
12ebfedea0SLionel Sambuc  *
13ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
18ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
19ebfedea0SLionel Sambuc  *
20ebfedea0SLionel Sambuc  * 3. Neither the name of PADL Software nor the names of its contributors
21ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
22ebfedea0SLionel Sambuc  *    without specific prior written permission.
23ebfedea0SLionel Sambuc  *
24ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
25ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
28ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34ebfedea0SLionel Sambuc  * SUCH DAMAGE.
35ebfedea0SLionel Sambuc  */
36ebfedea0SLionel Sambuc 
37ebfedea0SLionel Sambuc #include "kcm_locl.h"
38ebfedea0SLionel Sambuc #include <pwd.h>
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc krb5_error_code
kcm_ccache_resolve_client(krb5_context context,kcm_client * client,kcm_operation opcode,const char * name,kcm_ccache * ccache)41ebfedea0SLionel Sambuc kcm_ccache_resolve_client(krb5_context context,
42ebfedea0SLionel Sambuc 			  kcm_client *client,
43ebfedea0SLionel Sambuc 			  kcm_operation opcode,
44ebfedea0SLionel Sambuc 			  const char *name,
45ebfedea0SLionel Sambuc 			  kcm_ccache *ccache)
46ebfedea0SLionel Sambuc {
47ebfedea0SLionel Sambuc     krb5_error_code ret;
48ebfedea0SLionel Sambuc 
49ebfedea0SLionel Sambuc     ret = kcm_ccache_resolve(context, name, ccache);
50ebfedea0SLionel Sambuc     if (ret) {
51ebfedea0SLionel Sambuc 	kcm_log(1, "Failed to resolve cache %s: %s",
52ebfedea0SLionel Sambuc 		name, krb5_get_err_text(context, ret));
53ebfedea0SLionel Sambuc 	return ret;
54ebfedea0SLionel Sambuc     }
55ebfedea0SLionel Sambuc 
56ebfedea0SLionel Sambuc     ret = kcm_access(context, client, opcode, *ccache);
57ebfedea0SLionel Sambuc     if (ret) {
58ebfedea0SLionel Sambuc 	ret = KRB5_FCC_NOFILE; /* don't disclose */
59ebfedea0SLionel Sambuc 	kcm_release_ccache(context, *ccache);
60ebfedea0SLionel Sambuc     }
61ebfedea0SLionel Sambuc 
62ebfedea0SLionel Sambuc     return ret;
63ebfedea0SLionel Sambuc }
64ebfedea0SLionel Sambuc 
65ebfedea0SLionel Sambuc krb5_error_code
kcm_ccache_destroy_client(krb5_context context,kcm_client * client,const char * name)66ebfedea0SLionel Sambuc kcm_ccache_destroy_client(krb5_context context,
67ebfedea0SLionel Sambuc 			  kcm_client *client,
68ebfedea0SLionel Sambuc 			  const char *name)
69ebfedea0SLionel Sambuc {
70ebfedea0SLionel Sambuc     krb5_error_code ret;
71ebfedea0SLionel Sambuc     kcm_ccache ccache;
72ebfedea0SLionel Sambuc 
73ebfedea0SLionel Sambuc     ret = kcm_ccache_resolve(context, name, &ccache);
74ebfedea0SLionel Sambuc     if (ret) {
75ebfedea0SLionel Sambuc 	kcm_log(1, "Failed to resolve cache %s: %s",
76ebfedea0SLionel Sambuc 		name, krb5_get_err_text(context, ret));
77ebfedea0SLionel Sambuc 	return ret;
78ebfedea0SLionel Sambuc     }
79ebfedea0SLionel Sambuc 
80ebfedea0SLionel Sambuc     ret = kcm_access(context, client, KCM_OP_DESTROY, ccache);
81ebfedea0SLionel Sambuc     kcm_cleanup_events(context, ccache);
82ebfedea0SLionel Sambuc     kcm_release_ccache(context, ccache);
83ebfedea0SLionel Sambuc     if (ret)
84ebfedea0SLionel Sambuc 	return ret;
85ebfedea0SLionel Sambuc 
86ebfedea0SLionel Sambuc     return kcm_ccache_destroy(context, name);
87ebfedea0SLionel Sambuc }
88ebfedea0SLionel Sambuc 
89ebfedea0SLionel Sambuc krb5_error_code
kcm_ccache_new_client(krb5_context context,kcm_client * client,const char * name,kcm_ccache * ccache_p)90ebfedea0SLionel Sambuc kcm_ccache_new_client(krb5_context context,
91ebfedea0SLionel Sambuc 		      kcm_client *client,
92ebfedea0SLionel Sambuc 		      const char *name,
93ebfedea0SLionel Sambuc 		      kcm_ccache *ccache_p)
94ebfedea0SLionel Sambuc {
95ebfedea0SLionel Sambuc     krb5_error_code ret;
96ebfedea0SLionel Sambuc     kcm_ccache ccache;
97ebfedea0SLionel Sambuc 
98ebfedea0SLionel Sambuc     /* We insist the ccache name starts with UID or UID: */
99ebfedea0SLionel Sambuc     if (name_constraints != 0) {
100ebfedea0SLionel Sambuc 	char prefix[64];
101ebfedea0SLionel Sambuc 	size_t prefix_len;
102ebfedea0SLionel Sambuc 	int bad = 1;
103ebfedea0SLionel Sambuc 
104ebfedea0SLionel Sambuc 	snprintf(prefix, sizeof(prefix), "%ld:", (long)client->uid);
105ebfedea0SLionel Sambuc 	prefix_len = strlen(prefix);
106ebfedea0SLionel Sambuc 
107ebfedea0SLionel Sambuc 	if (strncmp(name, prefix, prefix_len) == 0)
108ebfedea0SLionel Sambuc 	    bad = 0;
109ebfedea0SLionel Sambuc 	else {
110ebfedea0SLionel Sambuc 	    prefix[prefix_len - 1] = '\0';
111ebfedea0SLionel Sambuc 	    if (strcmp(name, prefix) == 0)
112ebfedea0SLionel Sambuc 		bad = 0;
113ebfedea0SLionel Sambuc 	}
114ebfedea0SLionel Sambuc 
115ebfedea0SLionel Sambuc 	/* Allow root to create badly-named ccaches */
116ebfedea0SLionel Sambuc 	if (bad && !CLIENT_IS_ROOT(client))
117ebfedea0SLionel Sambuc 	    return KRB5_CC_BADNAME;
118ebfedea0SLionel Sambuc     }
119ebfedea0SLionel Sambuc 
120ebfedea0SLionel Sambuc     ret = kcm_ccache_resolve(context, name, &ccache);
121ebfedea0SLionel Sambuc     if (ret == 0) {
122ebfedea0SLionel Sambuc 	if ((ccache->uid != client->uid ||
123ebfedea0SLionel Sambuc 	     ccache->gid != client->gid) && !CLIENT_IS_ROOT(client))
124ebfedea0SLionel Sambuc 	    return KRB5_FCC_PERM;
125ebfedea0SLionel Sambuc     } else if (ret != KRB5_FCC_NOFILE && !(CLIENT_IS_ROOT(client) && ret == KRB5_FCC_PERM)) {
126ebfedea0SLionel Sambuc 		return ret;
127ebfedea0SLionel Sambuc     }
128ebfedea0SLionel Sambuc 
129ebfedea0SLionel Sambuc     if (ret == KRB5_FCC_NOFILE) {
130ebfedea0SLionel Sambuc 	ret = kcm_ccache_new(context, name, &ccache);
131ebfedea0SLionel Sambuc 	if (ret) {
132ebfedea0SLionel Sambuc 	    kcm_log(1, "Failed to initialize cache %s: %s",
133ebfedea0SLionel Sambuc 		    name, krb5_get_err_text(context, ret));
134ebfedea0SLionel Sambuc 	    return ret;
135ebfedea0SLionel Sambuc 	}
136ebfedea0SLionel Sambuc 
137ebfedea0SLionel Sambuc 	/* bind to current client */
138ebfedea0SLionel Sambuc 	ccache->uid = client->uid;
139ebfedea0SLionel Sambuc 	ccache->gid = client->gid;
140ebfedea0SLionel Sambuc 	ccache->session = client->session;
141ebfedea0SLionel Sambuc     } else {
142ebfedea0SLionel Sambuc 	ret = kcm_zero_ccache_data(context, ccache);
143ebfedea0SLionel Sambuc 	if (ret) {
144ebfedea0SLionel Sambuc 	    kcm_log(1, "Failed to empty cache %s: %s",
145ebfedea0SLionel Sambuc 		    name, krb5_get_err_text(context, ret));
146ebfedea0SLionel Sambuc 	    kcm_release_ccache(context, ccache);
147ebfedea0SLionel Sambuc 	    return ret;
148ebfedea0SLionel Sambuc 	}
149ebfedea0SLionel Sambuc 	kcm_cleanup_events(context, ccache);
150ebfedea0SLionel Sambuc     }
151ebfedea0SLionel Sambuc 
152ebfedea0SLionel Sambuc     ret = kcm_access(context, client, KCM_OP_INITIALIZE, ccache);
153ebfedea0SLionel Sambuc     if (ret) {
154ebfedea0SLionel Sambuc 	kcm_release_ccache(context, ccache);
155ebfedea0SLionel Sambuc 	kcm_ccache_destroy(context, name);
156ebfedea0SLionel Sambuc 	return ret;
157ebfedea0SLionel Sambuc     }
158ebfedea0SLionel Sambuc 
159ebfedea0SLionel Sambuc     /*
160ebfedea0SLionel Sambuc      * Finally, if the user is root and the cache was created under
161ebfedea0SLionel Sambuc      * another user's name, chown the cache to that user and their
162ebfedea0SLionel Sambuc      * default gid.
163ebfedea0SLionel Sambuc      */
164ebfedea0SLionel Sambuc     if (CLIENT_IS_ROOT(client)) {
165ebfedea0SLionel Sambuc 	unsigned long uid;
166ebfedea0SLionel Sambuc 	int matches = sscanf(name,"%ld:",&uid);
167ebfedea0SLionel Sambuc 	if (matches == 0)
168ebfedea0SLionel Sambuc 	    matches = sscanf(name,"%ld",&uid);
169ebfedea0SLionel Sambuc 	if (matches == 1) {
170ebfedea0SLionel Sambuc 	    struct passwd *pwd = getpwuid(uid);
171ebfedea0SLionel Sambuc 	    if (pwd != NULL) {
172ebfedea0SLionel Sambuc 		gid_t gid = pwd->pw_gid;
173ebfedea0SLionel Sambuc 		kcm_chown(context, client, ccache, uid, gid);
174ebfedea0SLionel Sambuc 	    }
175ebfedea0SLionel Sambuc 	}
176ebfedea0SLionel Sambuc     }
177ebfedea0SLionel Sambuc 
178ebfedea0SLionel Sambuc     *ccache_p = ccache;
179ebfedea0SLionel Sambuc     return 0;
180ebfedea0SLionel Sambuc }
181ebfedea0SLionel Sambuc 
182