1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* key_call.c, Interface to keyserver
* key_encryptsession(agent, deskey, cr)-encrypt a session key to talk to agent
* key_decryptsession(agent, deskey) - decrypt ditto
* key_gendes(deskey) - generate a secure des key
* key_getnetname(netname, cr) - get the netname from the keyserv
* netname2user(...) - get unix credential for given name (kernel only)
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/pathname.h>
#include <sys/sysmacros.h>
#include <sys/vnode.h>
#include <sys/uio.h>
#include <sys/debug.h>
#include <sys/utsname.h>
#include <sys/cmn_err.h>
#include <rpc/rpc.h>
#include <rpc/key_prot.h>
#define KEY_TIMEOUT 30 /* per-try timeout in seconds */
#define KEY_NRETRY 6 /* number of retries */
struct auth_globals {
struct knetconfig auth_config;
char auth_keyname[SYS_NMLN+16];
};
static struct timeval keytrytimeout = { KEY_TIMEOUT, 0 };
static enum clnt_stat key_call(rpcproc_t, xdrproc_t, char *, xdrproc_t, char *,
cred_t *);
/* ARGSUSED */
void *
auth_zone_init(zoneid_t zoneid)
{
struct auth_globals *authg;
authg = kmem_zalloc(sizeof (*authg), KM_SLEEP);
return (authg);
}
/* ARGSUSED */
void
auth_zone_fini(zoneid_t zoneid, void *data)
{
struct auth_globals *authg = data;
kmem_free(authg, sizeof (*authg));
}
enum clnt_stat
key_encryptsession(char *remotename, des_block *deskey, cred_t *cr)
{
cryptkeyarg arg;
cryptkeyres res;
enum clnt_stat stat;
RPCLOG(8, "key_encryptsession(%s, ", remotename);
RPCLOG(8, "%x", *(uint32_t *)deskey);
RPCLOG(8, "%x)\n", *(((uint32_t *)(deskey))+1));
arg.remotename = remotename;
arg.deskey = *deskey;
if ((stat = key_call(KEY_ENCRYPT, xdr_cryptkeyarg, (char *)&arg,
xdr_cryptkeyres, (char *)&res, cr)) != RPC_SUCCESS) {
RPCLOG(1, "key_encryptsession(%d, ", (int)crgetuid(cr));
RPCLOG(1, "%s): ", remotename);
RPCLOG(1, "rpc status %d ", stat);
RPCLOG(1, "(%s)\n", clnt_sperrno(stat));
return (stat);
}
if (res.status != KEY_SUCCESS) {
RPCLOG(1, "key_encryptsession(%d, ", (int)crgetuid(cr));
RPCLOG(1, "%s): ", remotename);
RPCLOG(1, "key status %d\n", res.status);
return (RPC_FAILED); /* XXX */
}
*deskey = res.cryptkeyres_u.deskey;
return (RPC_SUCCESS);
}
enum clnt_stat
key_decryptsession(char *remotename, des_block *deskey)
{
cryptkeyarg arg;
cryptkeyres res;
enum clnt_stat stat;
RPCLOG(8, "key_decryptsession(%s, ", remotename);
RPCLOG(2, "%x", *(uint32_t *)deskey);
RPCLOG(2, "%x)\n", *(((uint32_t *)(deskey))+1));
arg.remotename = remotename;
arg.deskey = *deskey;
if ((stat = key_call(KEY_DECRYPT, xdr_cryptkeyarg, (char *)&arg,
xdr_cryptkeyres, (char *)&res, kcred)) != RPC_SUCCESS) {
RPCLOG(1, "key_decryptsession(%s): ", remotename);
RPCLOG(1, "rpc status %d ", stat);
RPCLOG(1, "(%s)\n", clnt_sperrno(stat));
return (stat);
}
if (res.status != KEY_SUCCESS) {
RPCLOG(1, "key_decryptsession(%s): ", remotename);
RPCLOG(1, "key status %d\n", res.status);
return (RPC_FAILED); /* XXX */
}
*deskey = res.cryptkeyres_u.deskey;
return (RPC_SUCCESS);
}
enum clnt_stat
key_gendes(des_block *key)
{
return (key_call(KEY_GEN, xdr_void, NULL, xdr_des_block, (char *)key,
CRED()));
}
/*
* Call up to keyserv to get the netname of the client based
* on its uid. The netname is written into the string that "netname"
* points to; the caller is responsible for ensuring that sufficient space
* is available.
*/
enum clnt_stat
key_getnetname(netname, cr)
char *netname;
cred_t *cr;
{
key_netstres kres;
enum clnt_stat stat;
/*
* Look up the keyserv interface routines to see if
* netname is stored there.
*/
kres.key_netstres_u.knet.st_netname = netname;
if ((stat = key_call((rpcproc_t)KEY_NET_GET, xdr_void, NULL,
xdr_key_netstres, (char *)&kres, cr)) != RPC_SUCCESS) {
RPCLOG(1, "key_getnetname(%d): ", (int)crgetuid(cr));
RPCLOG(1, "rpc status %d ", stat);
RPCLOG(1, "(%s)\n", clnt_sperrno(stat));
return (stat);
}
if (kres.status != KEY_SUCCESS) {
RPCLOG(1, "key_getnetname(%d): ", (int)crgetuid(cr));
RPCLOG(1, "key status %d\n", kres.status);
return (RPC_FAILED);
}
return (RPC_SUCCESS);
}
enum clnt_stat
netname2user(char *name, uid_t *uid, gid_t *gid, int *len, gid_t *groups)
{
struct getcredres res;
enum clnt_stat stat;
res.getcredres_u.cred.gids.gids_val = (uint_t *)groups;
if ((stat = key_call(KEY_GETCRED, xdr_netnamestr, (char *)&name,
xdr_getcredres, (char *)&res, CRED())) != RPC_SUCCESS) {
RPCLOG(1, "netname2user(%s): ", name);
RPCLOG(1, "rpc status %d ", stat);
RPCLOG(1, "(%s)\n", clnt_sperrno(stat));
return (stat);
}
if (res.status != KEY_SUCCESS) {
RPCLOG(1, "netname2user(%s): ", name);
RPCLOG(1, "key status %d\n", res.status);
return (RPC_FAILED); /* XXX */
}
*uid = res.getcredres_u.cred.uid;
*gid = res.getcredres_u.cred.gid;
*len = res.getcredres_u.cred.gids.gids_len;
return (RPC_SUCCESS);
}
#define NC_LOOPBACK "loopback" /* XXX */
char loopback_name[] = NC_LOOPBACK;
static enum clnt_stat
key_call(rpcproc_t procn, xdrproc_t xdr_args, caddr_t args,
xdrproc_t xdr_rslt, caddr_t rslt, cred_t *cr)
{
struct netbuf netaddr;
CLIENT *client;
enum clnt_stat stat;
vnode_t *vp;
int error;
struct auth_globals *authg;
char *keyname;
struct knetconfig *configp;
k_sigset_t smask;
authg = zone_getspecific(auth_zone_key, curproc->p_zone);
keyname = authg->auth_keyname;
configp = &authg->auth_config;
/*
* Using a global here is obviously busted and fraught with danger.
*/
(void) strcpy(keyname, uts_nodename());
netaddr.len = strlen(keyname);
(void) strcpy(&keyname[netaddr.len], ".keyserv");
netaddr.buf = keyname;
/*
* 8 = strlen(".keyserv");
*/
netaddr.len = netaddr.maxlen = netaddr.len + 8;
/*
* filch a knetconfig structure.
*/
if (configp->knc_rdev == 0) {
if ((error = lookupname("/dev/ticlts", UIO_SYSSPACE,
FOLLOW, NULLVPP, &vp)) != 0) {
RPCLOG(1, "key_call: lookupname: %d\n", error);
return (RPC_UNKNOWNPROTO);
}
configp->knc_rdev = vp->v_rdev;
configp->knc_protofmly = loopback_name;
VN_RELE(vp);
}
configp->knc_semantics = NC_TPI_CLTS;
RPCLOG(8, "key_call: procn %d, ", procn);
RPCLOG(8, "rdev %lx, ", configp->knc_rdev);
RPCLOG(8, "len %d, ", netaddr.len);
RPCLOG(8, "maxlen %d, ", netaddr.maxlen);
RPCLOG(8, "name %p\n", (void *)netaddr.buf);
/*
* now call the proper stuff.
*/
error = clnt_tli_kcreate(configp, &netaddr, KEY_PROG, KEY_VERS,
0, KEY_NRETRY, cr, &client);
if (error != 0) {
RPCLOG(1, "key_call: clnt_tli_kcreate: error %d\n", error);
switch (error) {
case EINTR:
return (RPC_INTR);
case ETIMEDOUT:
return (RPC_TIMEDOUT);
default:
return (RPC_FAILED); /* XXX */
}
}
auth_destroy(client->cl_auth);
client->cl_auth = authloopback_create();
if (client->cl_auth == NULL) {
clnt_destroy(client);
RPCLOG(1, "key_call: authloopback_create: error %d\n", EINTR);
return (RPC_INTR);
}
/* Mask out all signals except SIGHUP, SIGQUIT, and SIGTERM. */
sigintr(&smask, 0);
stat = clnt_call(client, procn, xdr_args, args, xdr_rslt, rslt,
keytrytimeout);
sigunintr(&smask);
auth_destroy(client->cl_auth);
clnt_destroy(client);
if (stat != RPC_SUCCESS) {
RPCLOG(1, "key_call: keyserver clnt_call failed: stat %d ",
stat);
RPCLOG(1, "(%s)\n", clnt_sperrno(stat));
RPCLOG0(1, "\n");
return (stat);
}
RPCLOG(8, "key call: (%d) ok\n", procn);
return (RPC_SUCCESS);
}
|