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