1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun Copyright 1991, 1998 The Open Group
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun Permission to use, copy, modify, distribute, and sell this software and its
6*4882a593Smuzhiyun documentation for any purpose is hereby granted without fee, provided that
7*4882a593Smuzhiyun the above copyright notice appear in all copies and that both that
8*4882a593Smuzhiyun copyright notice and this permission notice appear in supporting
9*4882a593Smuzhiyun documentation.
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun The above copyright notice and this permission notice shall be included
12*4882a593Smuzhiyun in all copies or substantial portions of the Software.
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15*4882a593Smuzhiyun OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16*4882a593Smuzhiyun MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17*4882a593Smuzhiyun IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*4882a593Smuzhiyun OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*4882a593Smuzhiyun ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*4882a593Smuzhiyun OTHER DEALINGS IN THE SOFTWARE.
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun Except as contained in this notice, the name of The Open Group shall
23*4882a593Smuzhiyun not be used in advertising or otherwise to promote the sale, use or
24*4882a593Smuzhiyun other dealings in this Software without prior written authorization
25*4882a593Smuzhiyun from The Open Group.
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun */
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun * SUN-DES-1 authentication mechanism
31*4882a593Smuzhiyun * Author: Mayank Choudhary, Sun Microsystems
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
35*4882a593Smuzhiyun #include <dix-config.h>
36*4882a593Smuzhiyun #endif
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #ifdef SECURE_RPC
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #include <X11/X.h>
41*4882a593Smuzhiyun #include <X11/Xauth.h>
42*4882a593Smuzhiyun #include "misc.h"
43*4882a593Smuzhiyun #include "os.h"
44*4882a593Smuzhiyun #include "osdep.h"
45*4882a593Smuzhiyun #include "dixstruct.h"
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun #include <rpc/rpc.h>
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #ifdef __sun
50*4882a593Smuzhiyun /* <rpc/auth.h> only includes this if _KERNEL is #defined... */
51*4882a593Smuzhiyun extern bool_t xdr_opaque_auth(XDR *, struct opaque_auth *);
52*4882a593Smuzhiyun #endif
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun static enum auth_stat why;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun static char *
authdes_ezdecode(const char * inmsg,int len)57*4882a593Smuzhiyun authdes_ezdecode(const char *inmsg, int len)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun struct rpc_msg msg;
60*4882a593Smuzhiyun char cred_area[MAX_AUTH_BYTES];
61*4882a593Smuzhiyun char verf_area[MAX_AUTH_BYTES];
62*4882a593Smuzhiyun char *temp_inmsg;
63*4882a593Smuzhiyun struct svc_req r;
64*4882a593Smuzhiyun bool_t res0, res1;
65*4882a593Smuzhiyun XDR xdr;
66*4882a593Smuzhiyun SVCXPRT xprt;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun temp_inmsg = malloc(len);
69*4882a593Smuzhiyun if (temp_inmsg == NULL) {
70*4882a593Smuzhiyun why = AUTH_FAILED; /* generic error, since there is no AUTH_BADALLOC */
71*4882a593Smuzhiyun return NULL;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun memmove(temp_inmsg, inmsg, len);
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun memset((char *) &msg, 0, sizeof(msg));
76*4882a593Smuzhiyun memset((char *) &r, 0, sizeof(r));
77*4882a593Smuzhiyun memset(cred_area, 0, sizeof(cred_area));
78*4882a593Smuzhiyun memset(verf_area, 0, sizeof(verf_area));
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun msg.rm_call.cb_cred.oa_base = cred_area;
81*4882a593Smuzhiyun msg.rm_call.cb_verf.oa_base = verf_area;
82*4882a593Smuzhiyun why = AUTH_FAILED;
83*4882a593Smuzhiyun xdrmem_create(&xdr, temp_inmsg, len, XDR_DECODE);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun if ((r.rq_clntcred = malloc(MAX_AUTH_BYTES)) == NULL)
86*4882a593Smuzhiyun goto bad1;
87*4882a593Smuzhiyun r.rq_xprt = &xprt;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /* decode into msg */
90*4882a593Smuzhiyun res0 = xdr_opaque_auth(&xdr, &(msg.rm_call.cb_cred));
91*4882a593Smuzhiyun res1 = xdr_opaque_auth(&xdr, &(msg.rm_call.cb_verf));
92*4882a593Smuzhiyun if (!(res0 && res1))
93*4882a593Smuzhiyun goto bad2;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /* do the authentication */
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun r.rq_cred = msg.rm_call.cb_cred; /* read by opaque stuff */
98*4882a593Smuzhiyun if (r.rq_cred.oa_flavor != AUTH_DES) {
99*4882a593Smuzhiyun why = AUTH_TOOWEAK;
100*4882a593Smuzhiyun goto bad2;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun #ifdef SVR4
103*4882a593Smuzhiyun if ((why = __authenticate(&r, &msg)) != AUTH_OK) {
104*4882a593Smuzhiyun #else
105*4882a593Smuzhiyun if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
106*4882a593Smuzhiyun #endif
107*4882a593Smuzhiyun goto bad2;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun return (((struct authdes_cred *) r.rq_clntcred)->adc_fullname.name);
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun bad2:
112*4882a593Smuzhiyun free(r.rq_clntcred);
113*4882a593Smuzhiyun bad1:
114*4882a593Smuzhiyun return ((char *) 0); /* ((struct authdes_cred *) NULL); */
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun static XID rpc_id = (XID) ~0L;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun static Bool
120*4882a593Smuzhiyun CheckNetName(unsigned char *addr, short len, void *closure)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun return (len == strlen((char *) closure) &&
123*4882a593Smuzhiyun strncmp((char *) addr, (char *) closure, len) == 0);
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun static char rpc_error[MAXNETNAMELEN + 50];
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun _X_HIDDEN XID
129*4882a593Smuzhiyun SecureRPCCheck(unsigned short data_length, const char *data,
130*4882a593Smuzhiyun ClientPtr client, const char **reason)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun char *fullname;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun if (rpc_id == (XID) ~0L) {
135*4882a593Smuzhiyun *reason = "Secure RPC authorization not initialized";
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun else {
138*4882a593Smuzhiyun fullname = authdes_ezdecode(data, data_length);
139*4882a593Smuzhiyun if (fullname == (char *) 0) {
140*4882a593Smuzhiyun snprintf(rpc_error, sizeof(rpc_error),
141*4882a593Smuzhiyun "Unable to authenticate secure RPC client (why=%d)", why);
142*4882a593Smuzhiyun *reason = rpc_error;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun else {
145*4882a593Smuzhiyun if (ForEachHostInFamily(FamilyNetname, CheckNetName, fullname))
146*4882a593Smuzhiyun return rpc_id;
147*4882a593Smuzhiyun snprintf(rpc_error, sizeof(rpc_error),
148*4882a593Smuzhiyun "Principal \"%s\" is not authorized to connect", fullname);
149*4882a593Smuzhiyun *reason = rpc_error;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun return (XID) ~0L;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun _X_HIDDEN void
156*4882a593Smuzhiyun SecureRPCInit(void)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun if (rpc_id == ~0L)
159*4882a593Smuzhiyun AddAuthorization(9, "SUN-DES-1", 0, (char *) 0);
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun _X_HIDDEN int
163*4882a593Smuzhiyun SecureRPCAdd(unsigned short data_length, const char *data, XID id)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun if (data_length)
166*4882a593Smuzhiyun AddHost((void *) 0, FamilyNetname, data_length, data);
167*4882a593Smuzhiyun rpc_id = id;
168*4882a593Smuzhiyun return 1;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun _X_HIDDEN int
172*4882a593Smuzhiyun SecureRPCReset(void)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun rpc_id = (XID) ~0L;
175*4882a593Smuzhiyun return 1;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun _X_HIDDEN int
179*4882a593Smuzhiyun SecureRPCFromID(XID id, unsigned short *data_lenp, char **datap)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun return 0;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun _X_HIDDEN int
185*4882a593Smuzhiyun SecureRPCRemove(unsigned short data_length, const char *data)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun return 0;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun #endif /* SECURE_RPC */
190