xref: /freebsd/lib/libc/rpc/svc_auth_unix.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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 /*
36  * svc_auth_unix.c
37  * Handles UNIX flavor authentication parameters on the service side of rpc.
38  * There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.
39  * _svcauth_unix does full blown unix style uid,gid+gids auth,
40  * _svcauth_short uses a shorthand auth to index into a cache of longhand auths.
41  * Note: the shorthand has been gutted for efficiency.
42  *
43  * Copyright (C) 1984, Sun Microsystems, Inc.
44  */
45 
46 #include "namespace.h"
47 #include <assert.h>
48 #include <stdio.h>
49 #include <string.h>
50 
51 #include <rpc/rpc.h>
52 #include "un-namespace.h"
53 
54 /*
55  * Unix longhand authenticator
56  */
57 enum auth_stat
58 _svcauth_unix(struct svc_req *rqst, struct rpc_msg *msg)
59 {
60 	enum auth_stat stat;
61 	XDR xdrs;
62 	struct authunix_parms *aup;
63 	int32_t *buf;
64 	struct area {
65 		struct authunix_parms area_aup;
66 		char area_machname[MAX_MACHINE_NAME+1];
67 		u_int area_gids[NGRPS];
68 	} *area;
69 	u_int auth_len;
70 	size_t str_len, gid_len;
71 	u_int i;
72 
73 	assert(rqst != NULL);
74 	assert(msg != NULL);
75 
76 	area = (struct area *) rqst->rq_clntcred;
77 	aup = &area->area_aup;
78 	aup->aup_machname = area->area_machname;
79 	aup->aup_gids = area->area_gids;
80 	auth_len = (u_int)msg->rm_call.cb_cred.oa_length;
81 	xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,XDR_DECODE);
82 	buf = XDR_INLINE(&xdrs, auth_len);
83 	if (buf != NULL) {
84 		aup->aup_time = IXDR_GET_INT32(buf);
85 		str_len = (size_t)IXDR_GET_U_INT32(buf);
86 		if (str_len > MAX_MACHINE_NAME) {
87 			stat = AUTH_BADCRED;
88 			goto done;
89 		}
90 		memmove(aup->aup_machname, buf, str_len);
91 		aup->aup_machname[str_len] = 0;
92 		str_len = RNDUP(str_len);
93 		buf += str_len / sizeof (int32_t);
94 		aup->aup_uid = (int)IXDR_GET_INT32(buf);
95 		aup->aup_gid = (int)IXDR_GET_INT32(buf);
96 		gid_len = (size_t)IXDR_GET_U_INT32(buf);
97 		if (gid_len > NGRPS) {
98 			stat = AUTH_BADCRED;
99 			goto done;
100 		}
101 		aup->aup_len = gid_len;
102 		for (i = 0; i < gid_len; i++) {
103 			aup->aup_gids[i] = (int)IXDR_GET_INT32(buf);
104 		}
105 		/*
106 		 * five is the smallest unix credentials structure -
107 		 * timestamp, hostname len (0), uid, gid, and gids len (0).
108 		 */
109 		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
110 			(void) printf("bad auth_len gid %ld str %ld auth %u\n",
111 			    (long)gid_len, (long)str_len, auth_len);
112 			stat = AUTH_BADCRED;
113 			goto done;
114 		}
115 	} else if (! xdr_authunix_parms(&xdrs, aup)) {
116 		xdrs.x_op = XDR_FREE;
117 		(void)xdr_authunix_parms(&xdrs, aup);
118 		stat = AUTH_BADCRED;
119 		goto done;
120 	}
121 
122        /* get the verifier */
123 	if ((u_int)msg->rm_call.cb_verf.oa_length) {
124 		rqst->rq_xprt->xp_verf.oa_flavor =
125 			msg->rm_call.cb_verf.oa_flavor;
126 		rqst->rq_xprt->xp_verf.oa_base =
127 			msg->rm_call.cb_verf.oa_base;
128 		rqst->rq_xprt->xp_verf.oa_length =
129 			msg->rm_call.cb_verf.oa_length;
130 	} else {
131 		rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
132 		rqst->rq_xprt->xp_verf.oa_length = 0;
133 	}
134 	stat = AUTH_OK;
135 done:
136 	XDR_DESTROY(&xdrs);
137 	return (stat);
138 }
139 
140 
141 /*
142  * Shorthand unix authenticator
143  * Looks up longhand in a cache.
144  */
145 /*ARGSUSED*/
146 enum auth_stat
147 _svcauth_short(struct svc_req *rqst, struct rpc_msg *msg)
148 {
149 	return (AUTH_REJECTEDCRED);
150 }
151