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