xref: /freebsd/lib/libc/rpc/svc_auth_unix.c (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2009, Sun Microsystems, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * - Redistributions of source code must retain the above copyright notice,
10  *   this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  *   this list of conditions and the following disclaimer in the documentation
13  *   and/or other materials provided with the distribution.
14  * - Neither the name of Sun Microsystems, Inc. nor the names of its
15  *   contributors may be used to endorse or promote products derived
16  *   from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char *sccsid2 = "@(#)svc_auth_unix.c 1.28 88/02/08 Copyr 1984 Sun Micro";
33 static char *sccsid = "@(#)svc_auth_unix.c	2.3 88/08/01 4.0 RPCSRC";
34 #endif
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 /*
39  * svc_auth_unix.c
40  * Handles UNIX flavor authentication parameters on the service side of rpc.
41  * There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.
42  * _svcauth_unix does full blown unix style uid,gid+gids auth,
43  * _svcauth_short uses a shorthand auth to index into a cache of longhand auths.
44  * Note: the shorthand has been gutted for efficiency.
45  *
46  * Copyright (C) 1984, Sun Microsystems, Inc.
47  */
48 
49 #include "namespace.h"
50 #include <assert.h>
51 #include <stdio.h>
52 #include <string.h>
53 
54 #include <rpc/rpc.h>
55 #include "un-namespace.h"
56 
57 /*
58  * Unix longhand authenticator
59  */
60 enum auth_stat
61 _svcauth_unix(struct svc_req *rqst, struct rpc_msg *msg)
62 {
63 	enum auth_stat stat;
64 	XDR xdrs;
65 	struct authunix_parms *aup;
66 	int32_t *buf;
67 	struct area {
68 		struct authunix_parms area_aup;
69 		char area_machname[MAX_MACHINE_NAME+1];
70 		u_int area_gids[NGRPS];
71 	} *area;
72 	u_int auth_len;
73 	size_t str_len, gid_len;
74 	u_int i;
75 
76 	assert(rqst != NULL);
77 	assert(msg != NULL);
78 
79 	area = (struct area *) rqst->rq_clntcred;
80 	aup = &area->area_aup;
81 	aup->aup_machname = area->area_machname;
82 	aup->aup_gids = area->area_gids;
83 	auth_len = (u_int)msg->rm_call.cb_cred.oa_length;
84 	xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,XDR_DECODE);
85 	buf = XDR_INLINE(&xdrs, auth_len);
86 	if (buf != NULL) {
87 		aup->aup_time = IXDR_GET_INT32(buf);
88 		str_len = (size_t)IXDR_GET_U_INT32(buf);
89 		if (str_len > MAX_MACHINE_NAME) {
90 			stat = AUTH_BADCRED;
91 			goto done;
92 		}
93 		memmove(aup->aup_machname, buf, str_len);
94 		aup->aup_machname[str_len] = 0;
95 		str_len = RNDUP(str_len);
96 		buf += str_len / sizeof (int32_t);
97 		aup->aup_uid = (int)IXDR_GET_INT32(buf);
98 		aup->aup_gid = (int)IXDR_GET_INT32(buf);
99 		gid_len = (size_t)IXDR_GET_U_INT32(buf);
100 		if (gid_len > NGRPS) {
101 			stat = AUTH_BADCRED;
102 			goto done;
103 		}
104 		aup->aup_len = gid_len;
105 		for (i = 0; i < gid_len; i++) {
106 			aup->aup_gids[i] = (int)IXDR_GET_INT32(buf);
107 		}
108 		/*
109 		 * five is the smallest unix credentials structure -
110 		 * timestamp, hostname len (0), uid, gid, and gids len (0).
111 		 */
112 		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
113 			(void) printf("bad auth_len gid %ld str %ld auth %u\n",
114 			    (long)gid_len, (long)str_len, auth_len);
115 			stat = AUTH_BADCRED;
116 			goto done;
117 		}
118 	} else if (! xdr_authunix_parms(&xdrs, aup)) {
119 		xdrs.x_op = XDR_FREE;
120 		(void)xdr_authunix_parms(&xdrs, aup);
121 		stat = AUTH_BADCRED;
122 		goto done;
123 	}
124 
125        /* get the verifier */
126 	if ((u_int)msg->rm_call.cb_verf.oa_length) {
127 		rqst->rq_xprt->xp_verf.oa_flavor =
128 			msg->rm_call.cb_verf.oa_flavor;
129 		rqst->rq_xprt->xp_verf.oa_base =
130 			msg->rm_call.cb_verf.oa_base;
131 		rqst->rq_xprt->xp_verf.oa_length =
132 			msg->rm_call.cb_verf.oa_length;
133 	} else {
134 		rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
135 		rqst->rq_xprt->xp_verf.oa_length = 0;
136 	}
137 	stat = AUTH_OK;
138 done:
139 	XDR_DESTROY(&xdrs);
140 	return (stat);
141 }
142 
143 
144 /*
145  * Shorthand unix authenticator
146  * Looks up longhand in a cache.
147  */
148 /*ARGSUSED*/
149 enum auth_stat
150 _svcauth_short(struct svc_req *rqst, struct rpc_msg *msg)
151 {
152 	return (AUTH_REJECTEDCRED);
153 }
154