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