summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/rpc/sec/sec_svc.c
blob: ee4a31ebb0f0515dbdbc2ab44e195012c3f83903 (plain)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
 * 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 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 *  sec_svc.c, Server-side rpc security interface.
 */
#ifdef _KERNEL
#include <sys/param.h>
#include <sys/types.h>
#include <sys/debug.h>
#include <sys/systm.h>
#include <rpc/types.h>
#include <netinet/in.h>
#include <rpc/xdr.h>
#include <rpc/auth.h>
#include <rpc/clnt.h>
#include <rpc/rpc_msg.h>
#include <sys/tiuser.h>
#include <sys/tihdr.h>
#include <sys/t_kuser.h>
#include <sys/cmn_err.h>
#include <rpc/auth_des.h>
#include <rpc/auth_sys.h>
#include <rpc/rpcsec_gss.h>
#include <rpc/svc_auth.h>
#include <rpc/svc.h>
#else
#include <rpc/rpc.h>
#endif


enum auth_stat _svcauth_null(struct svc_req *, struct rpc_msg *);

/*
 *  NO-OP server wrap/unwrap svc_authany_ops using no-op svc_authany_wrap().
 */
/* ARGSUSED */
static int
svc_authany_wrap(SVCAUTH *auth, XDR *xdrs, xdrproc_t xfunc, caddr_t xwhere)
{
	return (*xfunc)(xdrs, xwhere);
}

struct svc_auth_ops svc_authany_ops = {
	svc_authany_wrap,
	svc_authany_wrap
};


/*
 * The call rpc message, msg has been obtained from the wire.  The msg contains
 * the raw form of credentials and verifiers.  authenticate returns AUTH_OK
 * if the msg is successfully authenticated.  If AUTH_OK then the routine also
 * does the following things:
 * set rqst->rq_xprt->verf to the appropriate response verifier;
 * sets rqst->rq_client_cred to the "cooked" form of the credentials.
 *
 * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
 * its length is set appropriately.
 *
 * The caller still owns and is responsible for msg->u.cmb.cred and
 * msg->u.cmb.verf.  The authentication system retains ownership of
 * rqst->rq_client_cred, the cooked credentials.
 *
 * There is an assumption that any flavor less than AUTH_NULL is
 * invalid.
 */
enum auth_stat
sec_svc_msg(struct svc_req *rqst, struct rpc_msg *msg, bool_t *no_dispatch)
{
	int cred_flavor;

	rqst->rq_cred = msg->rm_call.cb_cred;
	rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
	rqst->rq_xprt->xp_verf.oa_length = 0;
	/*
	 * Init the xp_auth to be no-op for all the flavors.
	 * Flavor specific routines will revise this when appropriate.
	 */
	rqst->rq_xprt->xp_auth.svc_ah_ops = svc_authany_ops;
	rqst->rq_xprt->xp_auth.svc_ah_private = NULL;
	*no_dispatch = FALSE;

	cred_flavor = rqst->rq_cred.oa_flavor;

	switch (cred_flavor) {
	case AUTH_NULL:
		rqst->rq_xprt->xp_cookie = (void *) AUTH_NULL;
		return (_svcauth_null(rqst, msg));

	case AUTH_UNIX:
		rqst->rq_xprt->xp_cookie = (void *) AUTH_UNIX;
		return (_svcauth_unix(rqst, msg));

	case AUTH_SHORT:
		rqst->rq_xprt->xp_cookie = (void *) AUTH_SHORT;
		return (_svcauth_short(rqst, msg));

	case AUTH_DES:
		rqst->rq_xprt->xp_cookie = (void *) AUTH_DES;
		return (_svcauth_des(rqst, msg));

	case RPCSEC_GSS:
		/*
		 * RPCSEC_GSS flavor routine takes an additional
		 * boolean parameter that gets set to TRUE when
		 * the call is not to be dispatched to the server.
		 */
		return (__svcrpcsec_gss(rqst, msg, no_dispatch));
	}
	return (AUTH_REJECTEDCRED);
}

/*
 *  sec_svc_getcred() gets unix cred of incoming security rpc requests.
 *  It also returns the prinicipal name and a cookie which is application
 *  dependent e.g. for nfs, it is the pseudo flavor.
 *
 *  return 0 on failure
 */
int
sec_svc_getcred(struct svc_req *req, cred_t *cr, caddr_t *principal,
	int *secmod)
{
	struct authunix_parms *aup;
	struct authdes_cred *adc;
	int flavor, stat;
	rpc_gss_rawcred_t *rcred;
	rpc_gss_ucred_t	*ucred;
	void *cookie;

	stat = 1;
	flavor = req->rq_cred.oa_flavor;

	*principal = NULL;
	switch (flavor) {
	case AUTH_UNIX:
		*secmod = AUTH_UNIX;
		aup = (struct authunix_parms *)req->rq_clntcred;
		if (crsetugid(cr, aup->aup_uid, aup->aup_gid) != 0)
			(void) crsetugid(cr, UID_NOBODY, GID_NOBODY);
		if (crsetgroups(cr, aup->aup_len, aup->aup_gids) != 0)
			(void) crsetgroups(cr, 0, NULL);
		break;

	case AUTH_NONE:
		*secmod = AUTH_NONE;
		break;

	case AUTH_DES:
		*secmod = AUTH_DES;
		adc = (struct authdes_cred *)req->rq_clntcred;
		stat = kauthdes_getucred(adc, cr);
		*principal = adc->adc_fullname.name;
		break;

	case RPCSEC_GSS:
		stat = rpc_gss_getcred(req, &rcred, &ucred, &cookie);
		*secmod = (int)(uintptr_t)cookie;	/* XX64 */
		if (ucred != NULL) {
			if (crsetugid(cr, ucred->uid, ucred->gid) != 0 ||
			    crsetgroups(cr, ucred->gidlen, ucred->gidlist) != 0)
				stat = 0;
		} else {
			(void) crsetugid(cr, UID_NOBODY, GID_NOBODY);
			(void) crsetgroups(cr, 0, NULL);
		}
		*principal = (caddr_t)rcred->client_principal;
		break;

	default:
		stat = 0;
		break;
	}

	return (stat);
}


/* ARGSUSED */
enum auth_stat
_svcauth_null(struct svc_req *rqst, struct rpc_msg *msg)
{
	return (AUTH_OK);
}


/*
 *  Load root principal names from user space to kernel space.
 *
 *  flavor - security flavor
 *  count - number of principal names to be loaded
 *  proots - address of the array of root names.
 *		input is the array address in the user space,
 *		output is the kernel address.
 *
 *  return 0 on failure.
 */
int
sec_svc_loadrootnames(int flavor, int count, caddr_t **proots, model_t model)
{
	caddr_t *roots, *oroots, root;
	char netname[MAXNETNAMELEN+1];
	struct rpc_gss_principal gsstmp, *gssname;
	uint_t i, j;
	size_t len, allocsz, oallocsz;

#ifdef lint
	model = model;
#endif

	/*
	 * Get list of names from user space
	 */
	allocsz = count * sizeof (caddr_t);
	oallocsz = count * SIZEOF_PTR(model);

	/*
	 * And now copy each individual principal name
	 */
	switch (flavor) {
	case AUTH_DES:
		roots = kmem_zalloc(allocsz, KM_SLEEP);
		oroots = kmem_alloc(oallocsz, KM_SLEEP);

		if (copyin(*proots, oroots, oallocsz))
			goto done;

		for (i = 0; i < count; i++) {
			/*
			 * copyinstr copies the complete string (including the
			 * NULL) and returns the len with the NULL byte
			 * included in the calculation as long as the max
			 * length is not exceeded.
			 */
#ifdef _SYSCALL32_IMPL
			if (model != DATAMODEL_NATIVE) {
				caddr32_t *tmp;

				tmp = (caddr32_t *)oroots;
				root = (caddr_t)(uintptr_t)tmp[i];
			} else
#endif
				root = oroots[i];
			if (copyinstr(root, netname, sizeof (netname), &len)) {
				for (j = 0; j < i; j++) {
					if (roots[j] != NULL)
						kmem_free(roots[j],
						    strlen(roots[j]) + 1);
				}
				goto done;
			}
			roots[i] = kmem_alloc(len, KM_SLEEP);
			bcopy(netname, roots[i], len);
		}
		kmem_free(oroots, oallocsz);
		*proots = roots;
		return (1);

	case RPCSEC_GSS:
		roots = kmem_alloc(allocsz, KM_SLEEP);
		oroots = kmem_alloc(oallocsz, KM_SLEEP);

		if (copyin(*proots, oroots, oallocsz))
			goto done;

		for (i = 0; i < count; i++) {
#ifdef _SYSCALL32_IMPL
			if (model != DATAMODEL_NATIVE) {
				caddr32_t *tmp;

				tmp = (caddr32_t *)oroots;
				root = (caddr_t)(uintptr_t)tmp[i];
			} else
#endif
				root = oroots[i];

			if (copyin(root, &gsstmp, sizeof (gsstmp))) {
				kmem_free(oroots, oallocsz);
				goto gssfreeup;
			}
			len = sizeof (gsstmp.len) + gsstmp.len;
			gssname = kmem_alloc(len, KM_SLEEP);
			if (copyin(root, gssname, len)) {
				kmem_free(gssname, len);
				kmem_free(oroots, oallocsz);
				goto gssfreeup;
			}
			roots[i] = (caddr_t)gssname;
		}
		kmem_free(oroots, oallocsz);
		*proots = roots;
		return (1);

	default:
		return (0);
	}

gssfreeup:
	for (j = 0; j < i; j++) {
		if (roots[j] != NULL) {
			gssname = (rpc_gss_principal_t)roots[j];
			kmem_free(roots[j], gssname->len +
			    sizeof (gssname->len));
		}
	}
done:
	kmem_free(roots, allocsz);
	return (0);
}


/*
 * Figure out everything we allocated in a root principal name list in
 * order to free it up.
 */
void
sec_svc_freerootnames(int flavor, int count, caddr_t *proots)
{
	int i;
	rpc_gss_principal_t gssname;

	switch (flavor) {
	case AUTH_DES:
		for (i = 0; i < count; i++)
			if (proots[i] != NULL)
				kmem_free(proots[i], strlen(proots[i]) + 1);
		break;

	case RPCSEC_GSS:
		for (i = 0; i < count; i++) {
			if (proots[i] == NULL)
				continue;
			gssname = (rpc_gss_principal_t)proots[i];
			kmem_free(proots[i], gssname->len + sizeof (int));
		}
		break;

	}
	kmem_free(proots, count * sizeof (caddr_t));
}

/*
 * Check if the  given principal name is in the root principal list
 */
bool_t
sec_svc_inrootlist(int flavor, caddr_t rootname, int count, caddr_t *roots)
{
	int i, tmp_len;
	rpc_gss_principal_t gssp, tmp_gssp;
	size_t namelen;

	switch (flavor) {
	case AUTH_DES:
		namelen = strlen(rootname) + 1;
		for (i = 0; i < count; i++)
			if (bcmp(rootname, roots[i], namelen) == 0)
				return (TRUE);
		break;

	case RPCSEC_GSS:
		gssp = (rpc_gss_principal_t)rootname;
		namelen = gssp->len;
		for (i = 0; i < count; i++) {
			tmp_gssp = (rpc_gss_principal_t)roots[i];
			tmp_len = tmp_gssp->len;
			if ((namelen == tmp_len) &&
			    (bcmp(&gssp->name[0],
			    &tmp_gssp->name[0], namelen) == 0))
				return (TRUE);
		}
		break;
	}
	return (FALSE);
}

/*
 * Miscellaneout "control" functions manipulating global RPC security
 * attributes for server applications.
 */
bool_t
sec_svc_control(uint_t cmd, void *argp)
{
	bool_t result = FALSE;		/* be paranoid */

	switch (cmd) {
	case RPC_SVC_SET_GSS_CALLBACK:
		result = rpc_gss_set_callback((rpc_gss_callback_t *)argp);
		break;
	default:
		cmn_err(CE_WARN, "sec_svc_control: bad command (%d)", cmd);
		result = FALSE;
		break;
	}

	return (result);
}