xref: /dragonfly/lib/libc/rpc/svc_auth_unix.c (revision 333227be)
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  * @(#)svc_auth_unix.c 1.28 88/02/08 Copyr 1984 Sun Micro
30  * @(#)svc_auth_unix.c	2.3 88/08/01 4.0 RPCSRC
31  * $FreeBSD: src/lib/libc/rpc/svc_auth_unix.c,v 1.8 1999/08/28 00:00:49 peter Exp $
32  * $DragonFly: src/lib/libc/rpc/svc_auth_unix.c,v 1.3 2004/10/25 19:38:02 drhodus Exp $
33  */
34 
35 /*
36  * svc_auth_unix.c
37  * Handles UNIX flavor authentication parameters on the service side of rpc.
38  * There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.
39  * _svcauth_unix does full blown unix style uid,gid+gids auth,
40  * _svcauth_short uses a shorthand auth to index into a cache of longhand auths.
41  * Note: the shorthand has been gutted for efficiency.
42  *
43  * Copyright (C) 1984, Sun Microsystems, Inc.
44  */
45 
46 #include <stdio.h>
47 #include <string.h>
48 #include <rpc/rpc.h>
49 
50 /*
51  * Unix longhand authenticator
52  */
53 enum auth_stat
54 _svcauth_unix(rqst, msg)
55 	struct svc_req *rqst;
56 	struct rpc_msg *msg;
57 {
58 	enum auth_stat stat;
59 	XDR xdrs;
60 	struct authunix_parms *aup;
61 	int32_t *buf;
62 	struct area {
63 		struct authunix_parms area_aup;
64 		char area_machname[MAX_MACHINE_NAME+1];
65 		int area_gids[NGRPS];
66 	} *area;
67 	u_int auth_len;
68 	int str_len, gid_len;
69 	int i;
70 
71 	area = (struct area *) rqst->rq_clntcred;
72 	aup = &area->area_aup;
73 	aup->aup_machname = area->area_machname;
74 	aup->aup_gids = area->area_gids;
75 	auth_len = (u_int)msg->rm_call.cb_cred.oa_length;
76 	xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,XDR_DECODE);
77 	buf = XDR_INLINE(&xdrs, auth_len);
78 	if (buf != NULL) {
79 		aup->aup_time = IXDR_GET_LONG(buf);
80 		str_len = IXDR_GET_U_LONG(buf);
81 		if (str_len > MAX_MACHINE_NAME) {
82 			stat = AUTH_BADCRED;
83 			goto done;
84 		}
85 		memcpy(aup->aup_machname, (caddr_t)buf, (u_int)str_len);
86 		aup->aup_machname[str_len] = 0;
87 		str_len = RNDUP(str_len);
88 		buf += str_len / sizeof (int32_t);
89 		aup->aup_uid = IXDR_GET_LONG(buf);
90 		aup->aup_gid = IXDR_GET_LONG(buf);
91 		gid_len = IXDR_GET_U_LONG(buf);
92 		if (gid_len > NGRPS) {
93 			stat = AUTH_BADCRED;
94 			goto done;
95 		}
96 		aup->aup_len = gid_len;
97 		for (i = 0; i < gid_len; i++) {
98 			aup->aup_gids[i] = IXDR_GET_LONG(buf);
99 		}
100 		/*
101 		 * five is the smallest unix credentials structure -
102 		 * timestamp, hostname len (0), uid, gid, and gids len (0).
103 		 */
104 		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
105 			(void) printf("bad auth_len gid %d str %d auth %d\n",
106 			    gid_len, str_len, auth_len);
107 			stat = AUTH_BADCRED;
108 			goto done;
109 		}
110 	} else if (! xdr_authunix_parms(&xdrs, aup)) {
111 		xdrs.x_op = XDR_FREE;
112 		(void)xdr_authunix_parms(&xdrs, aup);
113 		stat = AUTH_BADCRED;
114 		goto done;
115 	}
116 
117 	/* get the verifier */
118 	if ((u_int)msg->rm_call.cb_verf.oa_length) {
119 		rqst->rq_xprt->xp_verf.oa_flavor =
120 			msg->rm_call.cb_verf.oa_flavor;
121 		rqst->rq_xprt->xp_verf.oa_base =
122 			msg->rm_call.cb_verf.oa_base;
123 		rqst->rq_xprt->xp_verf.oa_length =
124 			msg->rm_call.cb_verf.oa_length;
125 	} else {
126 		rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
127 		rqst->rq_xprt->xp_verf.oa_length = 0;
128 	}
129 	stat = AUTH_OK;
130 done:
131 	XDR_DESTROY(&xdrs);
132 	return (stat);
133 }
134 
135 
136 /*
137  * Shorthand unix authenticator
138  * Looks up longhand in a cache.
139  */
140 /*ARGSUSED*/
141 enum auth_stat
142 _svcauth_short(rqst, msg)
143 	struct svc_req *rqst;
144 	struct rpc_msg *msg;
145 {
146 	return (AUTH_REJECTEDCRED);
147 }
148