xref: /netbsd/lib/libc/rpc/svc_auth_unix.c (revision c4a72b64)
1 /*	$NetBSD: svc_auth_unix.c,v 1.17 2002/11/11 22:15:42 thorpej Exp $	*/
2 
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char *sccsid = "@(#)svc_auth_unix.c 1.28 88/02/08 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)svc_auth_unix.c	2.3 88/08/01 4.0 RPCSRC";
37 #else
38 __RCSID("$NetBSD: svc_auth_unix.c,v 1.17 2002/11/11 22:15:42 thorpej Exp $");
39 #endif
40 #endif
41 
42 /*
43  * svc_auth_unix.c
44  * Handles UNIX flavor authentication parameters on the service side of rpc.
45  * There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.
46  * _svcauth_unix does full blown unix style uid,gid+gids auth,
47  * _svcauth_short uses a shorthand auth to index into a cache of longhand auths.
48  * Note: the shorthand has been gutted for efficiency.
49  *
50  * Copyright (C) 1984, Sun Microsystems, Inc.
51  */
52 
53 #include "namespace.h"
54 
55 #include <assert.h>
56 #include <stdio.h>
57 #include <string.h>
58 
59 #include <rpc/rpc.h>
60 
61 /*
62  * Unix longhand authenticator
63  */
64 enum auth_stat
65 _svcauth_unix(rqst, msg)
66 	struct svc_req *rqst;
67 	struct rpc_msg *msg;
68 {
69 	enum auth_stat stat;
70 	XDR xdrs;
71 	struct authunix_parms *aup;
72 	int32_t *buf;
73 	struct area {
74 		struct authunix_parms area_aup;
75 		char area_machname[MAX_MACHINE_NAME+1];
76 		int area_gids[NGRPS];
77 	} *area;
78 	u_int auth_len;
79 	size_t str_len, gid_len, i;
80 
81 	_DIAGASSERT(rqst != NULL);
82 	_DIAGASSERT(msg != NULL);
83 
84 	area = (struct area *) rqst->rq_clntcred;
85 	aup = &area->area_aup;
86 	aup->aup_machname = area->area_machname;
87 	aup->aup_gids = area->area_gids;
88 	auth_len = (u_int)msg->rm_call.cb_cred.oa_length;
89 	xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,XDR_DECODE);
90 	buf = XDR_INLINE(&xdrs, auth_len);
91 	if (buf != NULL) {
92 		aup->aup_time = IXDR_GET_INT32(buf);
93 		str_len = (size_t)IXDR_GET_U_INT32(buf);
94 		if (str_len > MAX_MACHINE_NAME) {
95 			stat = AUTH_BADCRED;
96 			goto done;
97 		}
98 		memmove(aup->aup_machname, buf, str_len);
99 		aup->aup_machname[str_len] = 0;
100 		str_len = RNDUP(str_len);
101 		buf += str_len / sizeof (int32_t);
102 		aup->aup_uid = (int)IXDR_GET_INT32(buf);
103 		aup->aup_gid = (int)IXDR_GET_INT32(buf);
104 		gid_len = (size_t)IXDR_GET_U_INT32(buf);
105 		if (gid_len > NGRPS) {
106 			stat = AUTH_BADCRED;
107 			goto done;
108 		}
109 		aup->aup_len = gid_len;
110 		for (i = 0; i < gid_len; i++) {
111 			aup->aup_gids[i] = (int)IXDR_GET_INT32(buf);
112 		}
113 		/*
114 		 * five is the smallest unix credentials structure -
115 		 * timestamp, hostname len (0), uid, gid, and gids len (0).
116 		 */
117 		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
118 			(void) printf("bad auth_len gid %ld str %ld auth %u\n",
119 			    (long)gid_len, (long)str_len, auth_len);
120 			stat = AUTH_BADCRED;
121 			goto done;
122 		}
123 	} else if (! xdr_authunix_parms(&xdrs, aup)) {
124 		xdrs.x_op = XDR_FREE;
125 		(void)xdr_authunix_parms(&xdrs, aup);
126 		stat = AUTH_BADCRED;
127 		goto done;
128 	}
129 	rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
130 	rqst->rq_xprt->xp_verf.oa_length = 0;
131 	stat = AUTH_OK;
132 done:
133 	XDR_DESTROY(&xdrs);
134 	return (stat);
135 }
136 
137 
138 /*
139  * Shorthand unix authenticator
140  * Looks up longhand in a cache.
141  */
142 /*ARGSUSED*/
143 enum auth_stat
144 _svcauth_short(rqst, msg)
145 	struct svc_req *rqst;
146 	struct rpc_msg *msg;
147 {
148 	return (AUTH_REJECTEDCRED);
149 }
150