1*84d9c625SLionel Sambuc /* $NetBSD: svc_auth_unix.c,v 1.21 2013/03/11 20:19:29 tron Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras *
6*84d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc * met:
92fe8fb19SBen Gras *
10*84d9c625SLionel Sambuc * * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc * * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc * copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc * disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc * provided with the distribution.
16*84d9c625SLionel Sambuc * * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc * contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc * from this software without specific prior written permission.
192fe8fb19SBen Gras *
20*84d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras */
332fe8fb19SBen Gras
342fe8fb19SBen Gras #include <sys/cdefs.h>
352fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
362fe8fb19SBen Gras #if 0
372fe8fb19SBen Gras static char *sccsid = "@(#)svc_auth_unix.c 1.28 88/02/08 Copyr 1984 Sun Micro";
382fe8fb19SBen Gras static char *sccsid = "@(#)svc_auth_unix.c 2.3 88/08/01 4.0 RPCSRC";
392fe8fb19SBen Gras #else
40*84d9c625SLionel Sambuc __RCSID("$NetBSD: svc_auth_unix.c,v 1.21 2013/03/11 20:19:29 tron Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras
442fe8fb19SBen Gras /*
452fe8fb19SBen Gras * svc_auth_unix.c
462fe8fb19SBen Gras * Handles UNIX flavor authentication parameters on the service side of rpc.
472fe8fb19SBen Gras * There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.
482fe8fb19SBen Gras * _svcauth_unix does full blown unix style uid,gid+gids auth,
492fe8fb19SBen Gras * _svcauth_short uses a shorthand auth to index into a cache of longhand auths.
502fe8fb19SBen Gras * Note: the shorthand has been gutted for efficiency.
512fe8fb19SBen Gras *
522fe8fb19SBen Gras * Copyright (C) 1984, Sun Microsystems, Inc.
532fe8fb19SBen Gras */
542fe8fb19SBen Gras
552fe8fb19SBen Gras #include "namespace.h"
562fe8fb19SBen Gras
572fe8fb19SBen Gras #include <assert.h>
582fe8fb19SBen Gras #include <stdio.h>
592fe8fb19SBen Gras #include <string.h>
602fe8fb19SBen Gras
612fe8fb19SBen Gras #include <rpc/rpc.h>
622fe8fb19SBen Gras
632fe8fb19SBen Gras /*
642fe8fb19SBen Gras * Unix longhand authenticator
652fe8fb19SBen Gras */
662fe8fb19SBen Gras enum auth_stat
_svcauth_unix(struct svc_req * rqst,struct rpc_msg * msg)67f14fb602SLionel Sambuc _svcauth_unix(struct svc_req *rqst, struct rpc_msg *msg)
682fe8fb19SBen Gras {
692fe8fb19SBen Gras enum auth_stat stat;
702fe8fb19SBen Gras XDR xdrs;
712fe8fb19SBen Gras struct authunix_parms *aup;
722fe8fb19SBen Gras int32_t *buf;
732fe8fb19SBen Gras struct area {
742fe8fb19SBen Gras struct authunix_parms area_aup;
752fe8fb19SBen Gras char area_machname[MAX_MACHINE_NAME+1];
762fe8fb19SBen Gras int area_gids[NGRPS];
772fe8fb19SBen Gras } *area;
782fe8fb19SBen Gras u_int auth_len;
792fe8fb19SBen Gras size_t str_len, gid_len, i;
802fe8fb19SBen Gras
812fe8fb19SBen Gras _DIAGASSERT(rqst != NULL);
822fe8fb19SBen Gras _DIAGASSERT(msg != NULL);
832fe8fb19SBen Gras
842fe8fb19SBen Gras area = (struct area *) rqst->rq_clntcred;
852fe8fb19SBen Gras aup = &area->area_aup;
862fe8fb19SBen Gras aup->aup_machname = area->area_machname;
872fe8fb19SBen Gras aup->aup_gids = area->area_gids;
882fe8fb19SBen Gras auth_len = (u_int)msg->rm_call.cb_cred.oa_length;
892fe8fb19SBen Gras xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,XDR_DECODE);
902fe8fb19SBen Gras buf = XDR_INLINE(&xdrs, auth_len);
912fe8fb19SBen Gras if (buf != NULL) {
922fe8fb19SBen Gras aup->aup_time = IXDR_GET_INT32(buf);
932fe8fb19SBen Gras str_len = (size_t)IXDR_GET_U_INT32(buf);
942fe8fb19SBen Gras if (str_len > MAX_MACHINE_NAME) {
952fe8fb19SBen Gras stat = AUTH_BADCRED;
962fe8fb19SBen Gras goto done;
972fe8fb19SBen Gras }
982fe8fb19SBen Gras memmove(aup->aup_machname, buf, str_len);
992fe8fb19SBen Gras aup->aup_machname[str_len] = 0;
1002fe8fb19SBen Gras str_len = RNDUP(str_len);
1012fe8fb19SBen Gras buf += str_len / sizeof (int32_t);
1022fe8fb19SBen Gras aup->aup_uid = (int)IXDR_GET_INT32(buf);
1032fe8fb19SBen Gras aup->aup_gid = (int)IXDR_GET_INT32(buf);
1042fe8fb19SBen Gras gid_len = (size_t)IXDR_GET_U_INT32(buf);
1052fe8fb19SBen Gras if (gid_len > NGRPS) {
1062fe8fb19SBen Gras stat = AUTH_BADCRED;
1072fe8fb19SBen Gras goto done;
1082fe8fb19SBen Gras }
109f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(u_int, gid_len));
110f14fb602SLionel Sambuc aup->aup_len = (u_int)gid_len;
1112fe8fb19SBen Gras for (i = 0; i < gid_len; i++) {
1122fe8fb19SBen Gras aup->aup_gids[i] = (int)IXDR_GET_INT32(buf);
1132fe8fb19SBen Gras }
1142fe8fb19SBen Gras /*
1152fe8fb19SBen Gras * five is the smallest unix credentials structure -
1162fe8fb19SBen Gras * timestamp, hostname len (0), uid, gid, and gids len (0).
1172fe8fb19SBen Gras */
1182fe8fb19SBen Gras if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
1192fe8fb19SBen Gras (void) printf("bad auth_len gid %ld str %ld auth %u\n",
1202fe8fb19SBen Gras (long)gid_len, (long)str_len, auth_len);
1212fe8fb19SBen Gras stat = AUTH_BADCRED;
1222fe8fb19SBen Gras goto done;
1232fe8fb19SBen Gras }
1242fe8fb19SBen Gras } else if (! xdr_authunix_parms(&xdrs, aup)) {
1252fe8fb19SBen Gras xdrs.x_op = XDR_FREE;
1262fe8fb19SBen Gras (void)xdr_authunix_parms(&xdrs, aup);
1272fe8fb19SBen Gras stat = AUTH_BADCRED;
1282fe8fb19SBen Gras goto done;
1292fe8fb19SBen Gras }
1302fe8fb19SBen Gras rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
1312fe8fb19SBen Gras rqst->rq_xprt->xp_verf.oa_length = 0;
1322fe8fb19SBen Gras stat = AUTH_OK;
1332fe8fb19SBen Gras done:
1342fe8fb19SBen Gras XDR_DESTROY(&xdrs);
1352fe8fb19SBen Gras return (stat);
1362fe8fb19SBen Gras }
1372fe8fb19SBen Gras
1382fe8fb19SBen Gras
1392fe8fb19SBen Gras /*
1402fe8fb19SBen Gras * Shorthand unix authenticator
1412fe8fb19SBen Gras * Looks up longhand in a cache.
1422fe8fb19SBen Gras */
1432fe8fb19SBen Gras /*ARGSUSED*/
1442fe8fb19SBen Gras enum auth_stat
_svcauth_short(struct svc_req * rqst,struct rpc_msg * msg)145f14fb602SLionel Sambuc _svcauth_short(struct svc_req *rqst, struct rpc_msg *msg)
1462fe8fb19SBen Gras {
1472fe8fb19SBen Gras return (AUTH_REJECTEDCRED);
1482fe8fb19SBen Gras }
149