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