xref: /netbsd-src/lib/libc/rpc/svc_auth_unix.c (revision 037708cbd4616ccd0d7d0381ebd3964d6696c188)
1 /*	$NetBSD: svc_auth_unix.c,v 1.4 1996/12/20 20:47:56 cgd 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 #if defined(LIBC_SCCS) && !defined(lint)
33 /*static char *sccsid = "from: @(#)svc_auth_unix.c 1.28 88/02/08 Copyr 1984 Sun Micro";*/
34 /*static char *sccsid = "from: @(#)svc_auth_unix.c	2.3 88/08/01 4.0 RPCSRC";*/
35 static char *rcsid = "$NetBSD: svc_auth_unix.c,v 1.4 1996/12/20 20:47:56 cgd Exp $";
36 #endif
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 <stdio.h>
50 #include <string.h>
51 #include <rpc/rpc.h>
52 
53 /*
54  * Unix longhand authenticator
55  */
56 enum auth_stat
57 _svcauth_unix(rqst, msg)
58 	register struct svc_req *rqst;
59 	register struct rpc_msg *msg;
60 {
61 	register enum auth_stat stat;
62 	XDR xdrs;
63 	register struct authunix_parms *aup;
64 	register int32_t *buf;
65 	struct area {
66 		struct authunix_parms area_aup;
67 		char area_machname[MAX_MACHINE_NAME+1];
68 		int area_gids[NGRPS];
69 	} *area;
70 	u_int auth_len;
71 	int str_len, gid_len;
72 	register int i;
73 
74 	area = (struct area *) rqst->rq_clntcred;
75 	aup = &area->area_aup;
76 	aup->aup_machname = area->area_machname;
77 	aup->aup_gids = area->area_gids;
78 	auth_len = (u_int)msg->rm_call.cb_cred.oa_length;
79 	xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,XDR_DECODE);
80 	buf = XDR_INLINE(&xdrs, auth_len);
81 	if (buf != NULL) {
82 		aup->aup_time = IXDR_GET_LONG(buf);
83 		str_len = IXDR_GET_U_LONG(buf);
84 		if (str_len > MAX_MACHINE_NAME) {
85 			stat = AUTH_BADCRED;
86 			goto done;
87 		}
88 		bcopy((caddr_t)buf, aup->aup_machname, (u_int)str_len);
89 		aup->aup_machname[str_len] = 0;
90 		str_len = RNDUP(str_len);
91 		buf += str_len / sizeof (int32_t);
92 		aup->aup_uid = IXDR_GET_LONG(buf);
93 		aup->aup_gid = IXDR_GET_LONG(buf);
94 		gid_len = IXDR_GET_U_LONG(buf);
95 		if (gid_len > NGRPS) {
96 			stat = AUTH_BADCRED;
97 			goto done;
98 		}
99 		aup->aup_len = gid_len;
100 		for (i = 0; i < gid_len; i++) {
101 			aup->aup_gids[i] = IXDR_GET_LONG(buf);
102 		}
103 		/*
104 		 * five is the smallest unix credentials structure -
105 		 * timestamp, hostname len (0), uid, gid, and gids len (0).
106 		 */
107 		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
108 			(void) printf("bad auth_len gid %d str %d auth %d\n",
109 			    gid_len, str_len, auth_len);
110 			stat = AUTH_BADCRED;
111 			goto done;
112 		}
113 	} else if (! xdr_authunix_parms(&xdrs, aup)) {
114 		xdrs.x_op = XDR_FREE;
115 		(void)xdr_authunix_parms(&xdrs, aup);
116 		stat = AUTH_BADCRED;
117 		goto done;
118 	}
119 	rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
120 	rqst->rq_xprt->xp_verf.oa_length = 0;
121 	stat = AUTH_OK;
122 done:
123 	XDR_DESTROY(&xdrs);
124 	return (stat);
125 }
126 
127 
128 /*
129  * Shorthand unix authenticator
130  * Looks up longhand in a cache.
131  */
132 /*ARGSUSED*/
133 enum auth_stat
134 _svcauth_short(rqst, msg)
135 	struct svc_req *rqst;
136 	struct rpc_msg *msg;
137 {
138 	return (AUTH_REJECTEDCRED);
139 }
140