1706f2543Smrg/*
2706f2543Smrg
3706f2543SmrgCopyright 1991, 1998  The Open Group
4706f2543Smrg
5706f2543SmrgPermission to use, copy, modify, distribute, and sell this software and its
6706f2543Smrgdocumentation for any purpose is hereby granted without fee, provided that
7706f2543Smrgthe above copyright notice appear in all copies and that both that
8706f2543Smrgcopyright notice and this permission notice appear in supporting
9706f2543Smrgdocumentation.
10706f2543Smrg
11706f2543SmrgThe above copyright notice and this permission notice shall be included
12706f2543Smrgin all copies or substantial portions of the Software.
13706f2543Smrg
14706f2543SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15706f2543SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16706f2543SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17706f2543SmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18706f2543SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19706f2543SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20706f2543SmrgOTHER DEALINGS IN THE SOFTWARE.
21706f2543Smrg
22706f2543SmrgExcept as contained in this notice, the name of The Open Group shall
23706f2543Smrgnot be used in advertising or otherwise to promote the sale, use or
24706f2543Smrgother dealings in this Software without prior written authorization
25706f2543Smrgfrom The Open Group.
26706f2543Smrg
27706f2543Smrg*/
28706f2543Smrg
29706f2543Smrg/*
30706f2543Smrg * SUN-DES-1 authentication mechanism
31706f2543Smrg * Author:  Mayank Choudhary, Sun Microsystems
32706f2543Smrg */
33706f2543Smrg
34706f2543Smrg
35706f2543Smrg#ifdef HAVE_DIX_CONFIG_H
36706f2543Smrg#include <dix-config.h>
37706f2543Smrg#endif
38706f2543Smrg
39706f2543Smrg#ifdef SECURE_RPC
40706f2543Smrg
41706f2543Smrg#include <X11/X.h>
42706f2543Smrg#include <X11/Xauth.h>
43706f2543Smrg#include "misc.h"
44706f2543Smrg#include "os.h"
45706f2543Smrg#include "osdep.h"
46706f2543Smrg#include "dixstruct.h"
47706f2543Smrg
48706f2543Smrg#include <rpc/rpc.h>
49706f2543Smrg
50706f2543Smrg#ifdef sun
51706f2543Smrg/* <rpc/auth.h> only includes this if _KERNEL is #defined... */
52706f2543Smrgextern bool_t xdr_opaque_auth(XDR *, struct opaque_auth *);
53706f2543Smrg#endif
54706f2543Smrg
55706f2543Smrgstatic enum auth_stat why;
56706f2543Smrg
57706f2543Smrgstatic char *
58706f2543Smrgauthdes_ezdecode(const char *inmsg, int len)
59706f2543Smrg{
60706f2543Smrg    struct rpc_msg  msg;
61706f2543Smrg    char            cred_area[MAX_AUTH_BYTES];
62706f2543Smrg    char            verf_area[MAX_AUTH_BYTES];
63706f2543Smrg    char            *temp_inmsg;
64706f2543Smrg    struct svc_req  r;
65706f2543Smrg    bool_t          res0, res1;
66706f2543Smrg    XDR             xdr;
67706f2543Smrg    SVCXPRT         xprt;
68706f2543Smrg
69706f2543Smrg    temp_inmsg = malloc(len);
70706f2543Smrg    if (temp_inmsg == NULL) {
71706f2543Smrg        why = AUTH_FAILED; /* generic error, since there is no AUTH_BADALLOC */
72706f2543Smrg        return NULL;
73706f2543Smrg    }
74706f2543Smrg    memmove(temp_inmsg, inmsg, len);
75706f2543Smrg
76706f2543Smrg    memset((char *)&msg, 0, sizeof(msg));
77706f2543Smrg    memset((char *)&r, 0, sizeof(r));
78706f2543Smrg    memset(cred_area, 0, sizeof(cred_area));
79706f2543Smrg    memset(verf_area, 0, sizeof(verf_area));
80706f2543Smrg
81706f2543Smrg    msg.rm_call.cb_cred.oa_base = cred_area;
82706f2543Smrg    msg.rm_call.cb_verf.oa_base = verf_area;
83706f2543Smrg    why = AUTH_FAILED;
84706f2543Smrg    xdrmem_create(&xdr, temp_inmsg, len, XDR_DECODE);
85706f2543Smrg
86706f2543Smrg    if ((r.rq_clntcred = malloc(MAX_AUTH_BYTES)) == NULL)
87706f2543Smrg        goto bad1;
88706f2543Smrg    r.rq_xprt = &xprt;
89706f2543Smrg
90706f2543Smrg    /* decode into msg */
91706f2543Smrg    res0 = xdr_opaque_auth(&xdr, &(msg.rm_call.cb_cred));
92706f2543Smrg    res1 = xdr_opaque_auth(&xdr, &(msg.rm_call.cb_verf));
93706f2543Smrg    if ( ! (res0 && res1) )
94706f2543Smrg         goto bad2;
95706f2543Smrg
96706f2543Smrg    /* do the authentication */
97706f2543Smrg
98706f2543Smrg    r.rq_cred = msg.rm_call.cb_cred;        /* read by opaque stuff */
99706f2543Smrg    if (r.rq_cred.oa_flavor != AUTH_DES) {
100706f2543Smrg        why = AUTH_TOOWEAK;
101706f2543Smrg        goto bad2;
102706f2543Smrg    }
103706f2543Smrg#ifdef SVR4
104706f2543Smrg    if ((why = __authenticate(&r, &msg)) != AUTH_OK) {
105706f2543Smrg#else
106706f2543Smrg    if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
107706f2543Smrg#endif
108706f2543Smrg            goto bad2;
109706f2543Smrg    }
110706f2543Smrg    return (((struct authdes_cred *) r.rq_clntcred)->adc_fullname.name);
111706f2543Smrg
112706f2543Smrgbad2:
113706f2543Smrg    free(r.rq_clntcred);
114706f2543Smrgbad1:
115706f2543Smrg    return ((char *)0); /* ((struct authdes_cred *) NULL); */
116706f2543Smrg}
117706f2543Smrg
118706f2543Smrgstatic XID  rpc_id = (XID) ~0L;
119706f2543Smrg
120706f2543Smrgstatic Bool
121706f2543SmrgCheckNetName (
122706f2543Smrg    unsigned char    *addr,
123706f2543Smrg    short	    len,
124706f2543Smrg    pointer	    closure
125706f2543Smrg)
126706f2543Smrg{
127706f2543Smrg    return (len == strlen ((char *) closure) &&
128706f2543Smrg	    strncmp ((char *) addr, (char *) closure, len) == 0);
129706f2543Smrg}
130706f2543Smrg
131706f2543Smrgstatic char rpc_error[MAXNETNAMELEN+50];
132706f2543Smrg
133706f2543Smrg_X_HIDDEN XID
134706f2543SmrgSecureRPCCheck (unsigned short data_length, const char *data,
135706f2543Smrg    ClientPtr client, char **reason)
136706f2543Smrg{
137706f2543Smrg    char *fullname;
138706f2543Smrg
139706f2543Smrg    if (rpc_id == (XID) ~0L) {
140706f2543Smrg	*reason = "Secure RPC authorization not initialized";
141706f2543Smrg    } else {
142706f2543Smrg	fullname = authdes_ezdecode(data, data_length);
143706f2543Smrg	if (fullname == (char *)0) {
144706f2543Smrg	    sprintf(rpc_error, "Unable to authenticate secure RPC client (why=%d)", why);
145706f2543Smrg	    *reason = rpc_error;
146706f2543Smrg	} else {
147706f2543Smrg	    if (ForEachHostInFamily (FamilyNetname, CheckNetName, fullname))
148706f2543Smrg		return rpc_id;
149706f2543Smrg	    sprintf(rpc_error, "Principal \"%s\" is not authorized to connect",
150706f2543Smrg			fullname);
151706f2543Smrg	    *reason = rpc_error;
152706f2543Smrg	}
153706f2543Smrg    }
154706f2543Smrg    return (XID) ~0L;
155706f2543Smrg}
156706f2543Smrg
157706f2543Smrg_X_HIDDEN void
158706f2543SmrgSecureRPCInit (void)
159706f2543Smrg{
160706f2543Smrg    if (rpc_id == ~0L)
161706f2543Smrg	AddAuthorization (9, "SUN-DES-1", 0, (char *) 0);
162706f2543Smrg}
163706f2543Smrg
164706f2543Smrg_X_HIDDEN int
165706f2543SmrgSecureRPCAdd (unsigned short data_length, const char *data, XID id)
166706f2543Smrg{
167706f2543Smrg    if (data_length)
168706f2543Smrg	AddHost ((pointer) 0, FamilyNetname, data_length, data);
169706f2543Smrg    rpc_id = id;
170706f2543Smrg    return 1;
171706f2543Smrg}
172706f2543Smrg
173706f2543Smrg_X_HIDDEN int
174706f2543SmrgSecureRPCReset (void)
175706f2543Smrg{
176706f2543Smrg    rpc_id = (XID) ~0L;
177706f2543Smrg    return 1;
178706f2543Smrg}
179706f2543Smrg
180706f2543Smrg_X_HIDDEN int
181706f2543SmrgSecureRPCFromID (XID id, unsigned short *data_lenp, char **datap)
182706f2543Smrg{
183706f2543Smrg    return 0;
184706f2543Smrg}
185706f2543Smrg
186706f2543Smrg_X_HIDDEN int
187706f2543SmrgSecureRPCRemove (unsigned short data_length, const char *data)
188706f2543Smrg{
189706f2543Smrg    return 0;
190706f2543Smrg}
191706f2543Smrg#endif /* SECURE_RPC */
192