Home | History | Annotate | Line # | Download | only in rpc
svc_auth_unix.c revision 1.18.58.1
      1 /*	$NetBSD: svc_auth_unix.c,v 1.18.58.1 2013/03/14 22:03:09 riz Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010, Oracle America, Inc.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions are
      8  * met:
      9  *
     10  *     * Redistributions of source code must retain the above copyright
     11  *       notice, this list of conditions and the following disclaimer.
     12  *     * Redistributions in binary form must reproduce the above
     13  *       copyright notice, this list of conditions and the following
     14  *       disclaimer in the documentation and/or other materials
     15  *       provided with the distribution.
     16  *     * Neither the name of the "Oracle America, Inc." nor the names of its
     17  *       contributors may be used to endorse or promote products derived
     18  *       from this software without specific prior written permission.
     19  *
     20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     25  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #if defined(LIBC_SCCS) && !defined(lint)
     36 #if 0
     37 static char *sccsid = "@(#)svc_auth_unix.c 1.28 88/02/08 Copyr 1984 Sun Micro";
     38 static char *sccsid = "@(#)svc_auth_unix.c	2.3 88/08/01 4.0 RPCSRC";
     39 #else
     40 __RCSID("$NetBSD: svc_auth_unix.c,v 1.18.58.1 2013/03/14 22:03:09 riz Exp $");
     41 #endif
     42 #endif
     43 
     44 /*
     45  * svc_auth_unix.c
     46  * Handles UNIX flavor authentication parameters on the service side of rpc.
     47  * There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.
     48  * _svcauth_unix does full blown unix style uid,gid+gids auth,
     49  * _svcauth_short uses a shorthand auth to index into a cache of longhand auths.
     50  * Note: the shorthand has been gutted for efficiency.
     51  *
     52  * Copyright (C) 1984, Sun Microsystems, Inc.
     53  */
     54 
     55 #include "namespace.h"
     56 
     57 #include <assert.h>
     58 #include <stdio.h>
     59 #include <string.h>
     60 
     61 #include <rpc/rpc.h>
     62 
     63 /*
     64  * Unix longhand authenticator
     65  */
     66 enum auth_stat
     67 _svcauth_unix(rqst, msg)
     68 	struct svc_req *rqst;
     69 	struct rpc_msg *msg;
     70 {
     71 	enum auth_stat stat;
     72 	XDR xdrs;
     73 	struct authunix_parms *aup;
     74 	int32_t *buf;
     75 	struct area {
     76 		struct authunix_parms area_aup;
     77 		char area_machname[MAX_MACHINE_NAME+1];
     78 		int area_gids[NGRPS];
     79 	} *area;
     80 	u_int auth_len;
     81 	size_t str_len, gid_len, i;
     82 
     83 	_DIAGASSERT(rqst != NULL);
     84 	_DIAGASSERT(msg != NULL);
     85 
     86 	area = (struct area *) rqst->rq_clntcred;
     87 	aup = &area->area_aup;
     88 	aup->aup_machname = area->area_machname;
     89 	aup->aup_gids = area->area_gids;
     90 	auth_len = (u_int)msg->rm_call.cb_cred.oa_length;
     91 	xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,XDR_DECODE);
     92 	buf = XDR_INLINE(&xdrs, auth_len);
     93 	if (buf != NULL) {
     94 		aup->aup_time = IXDR_GET_INT32(buf);
     95 		str_len = (size_t)IXDR_GET_U_INT32(buf);
     96 		if (str_len > MAX_MACHINE_NAME) {
     97 			stat = AUTH_BADCRED;
     98 			goto done;
     99 		}
    100 		memmove(aup->aup_machname, buf, str_len);
    101 		aup->aup_machname[str_len] = 0;
    102 		str_len = RNDUP(str_len);
    103 		buf += str_len / sizeof (int32_t);
    104 		aup->aup_uid = (int)IXDR_GET_INT32(buf);
    105 		aup->aup_gid = (int)IXDR_GET_INT32(buf);
    106 		gid_len = (size_t)IXDR_GET_U_INT32(buf);
    107 		if (gid_len > NGRPS) {
    108 			stat = AUTH_BADCRED;
    109 			goto done;
    110 		}
    111 		aup->aup_len = gid_len;
    112 		for (i = 0; i < gid_len; i++) {
    113 			aup->aup_gids[i] = (int)IXDR_GET_INT32(buf);
    114 		}
    115 		/*
    116 		 * five is the smallest unix credentials structure -
    117 		 * timestamp, hostname len (0), uid, gid, and gids len (0).
    118 		 */
    119 		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
    120 			(void) printf("bad auth_len gid %ld str %ld auth %u\n",
    121 			    (long)gid_len, (long)str_len, auth_len);
    122 			stat = AUTH_BADCRED;
    123 			goto done;
    124 		}
    125 	} else if (! xdr_authunix_parms(&xdrs, aup)) {
    126 		xdrs.x_op = XDR_FREE;
    127 		(void)xdr_authunix_parms(&xdrs, aup);
    128 		stat = AUTH_BADCRED;
    129 		goto done;
    130 	}
    131 	rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
    132 	rqst->rq_xprt->xp_verf.oa_length = 0;
    133 	stat = AUTH_OK;
    134 done:
    135 	XDR_DESTROY(&xdrs);
    136 	return (stat);
    137 }
    138 
    139 
    140 /*
    141  * Shorthand unix authenticator
    142  * Looks up longhand in a cache.
    143  */
    144 /*ARGSUSED*/
    145 enum auth_stat
    146 _svcauth_short(rqst, msg)
    147 	struct svc_req *rqst;
    148 	struct rpc_msg *msg;
    149 {
    150 	return (AUTH_REJECTEDCRED);
    151 }
    152