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