]> git.sesse.net Git - rdpsrv/blob - Xserver/programs/Xserver/os/secauth.c
Import X server from vnc-3.3.7.
[rdpsrv] / Xserver / programs / Xserver / os / secauth.c
1 /* $XConsortium: secauth.c /main/4 1996/11/27 16:57:14 swick $ */
2 /*
3 Copyright (c) 1996  X Consortium
4
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 OTHER DEALINGS IN THE SOFTWARE.
23
24 Except as contained in this notice, the name of the X Consortium shall
25 not be used in advertising or otherwise to promote the sale, use or
26 other dealings in this Software without prior written authorization
27 from the X Consortium.
28 */
29
30 #include "X.h"
31 #include "os.h"
32 #include "osdep.h"
33 #include "dixstruct.h"
34
35 /*================================================================
36    BEGIN ORL VNC modification
37    Need function prototype for SecurityGetSitePolicyStrings even when
38    XCSECURITY isn't defined */
39
40 /* REMOVE
41 #ifdef XCSECURITY
42 */
43 #define _SECURITY_SERVER
44 #include "extensions/security.h"
45 /* REMOVE
46 #endif
47 */
48
49 /* END ORL VNC modification
50 ================================================================*/
51
52 static char InvalidPolicyReason[] = "invalid policy specification";
53 static char PolicyViolationReason[] = "policy violation";
54
55 static Bool
56 AuthCheckSitePolicy(data_lengthP, dataP, client, reason)
57     unsigned short *data_lengthP;
58     char        **dataP;
59     ClientPtr   client;
60     char        **reason;
61 {
62     char        *policy = *dataP;
63     int         length;
64     Bool        permit;
65     int         nPolicies;
66     char        **sitePolicies;
67     int         nSitePolicies;
68     Bool        found = FALSE;
69
70     if ((length = *data_lengthP) < 2) {
71         *reason = InvalidPolicyReason;
72         return FALSE;
73     }
74
75     permit = (*policy++ == 0);
76     nPolicies = *policy++;
77
78     length -= 2;
79
80     sitePolicies = SecurityGetSitePolicyStrings(&nSitePolicies);
81
82     while (nPolicies) {
83         int strLen, sitePolicy;
84
85         if (length == 0) {
86             *reason = InvalidPolicyReason;
87             return FALSE;
88         }
89
90         strLen = *policy++;
91         if (--length < strLen) {
92             *reason = InvalidPolicyReason;
93             return FALSE;
94         }
95
96         if (!found)
97         {
98             for (sitePolicy = 0; sitePolicy < nSitePolicies; sitePolicy++)
99             {
100                 char *testPolicy = sitePolicies[sitePolicy];
101                 if ((strLen == strlen(testPolicy)) &&
102                     (strncmp(policy, testPolicy, strLen) == 0))
103                 {
104                     found = TRUE; /* need to continue parsing the policy... */
105                     break;
106                 }
107             }
108         }
109
110         policy += strLen;
111         length -= strLen;
112         nPolicies--;
113     }
114
115     if (found != permit)
116     {
117         *reason = PolicyViolationReason;
118         return FALSE;
119     }
120
121     *data_lengthP = length;
122     *dataP = policy;
123     return TRUE;
124 }
125
126 XID
127 AuthSecurityCheck (data_length, data, client, reason)
128     unsigned short      data_length;
129     char        *data;
130     ClientPtr client;
131     char        **reason;
132 {
133 #ifdef XCSECURITY
134     OsCommPtr oc = (OsCommPtr)client->osPrivate;
135     register ConnectionInputPtr oci = oc->input;
136     xConnSetupPrefix csp;
137     xReq freq;
138
139     if (client->clientState == ClientStateCheckedSecurity)
140     {
141         *reason = "repeated security check not permitted";
142         return (XID) -1;
143     }
144     else if (data_length > 0)
145     {
146         char policy_mask = *data++;
147
148         if (--data_length == 1) {
149             *reason = InvalidPolicyReason;
150             return (XID) -1;
151         }
152
153         if (policy_mask & 0x01) /* Extensions policy */
154         {
155          /* AuthCheckExtensionPolicy(&data_length, &data, client, reason) */
156             *reason = "security policy not implemented";
157             return (XID) -1;
158         }
159
160         if (policy_mask & 0x02) /* Site policy */
161         {
162             if (!AuthCheckSitePolicy(&data_length, &data, client, reason))
163                 return (XID) -1;
164         }
165
166         if (data_length > 0) {  /* did we consume the whole policy? */
167             *reason = InvalidPolicyReason;
168             return (XID) -1;
169         }
170
171     }
172     else if (!GetAccessControl())
173     {
174         *reason = "server host access control is disabled";
175         return (XID) -1;
176     }
177
178     client->clientState = ClientStateCheckingSecurity;
179
180     csp.success = 2 /* Authenticate */;
181     csp.lengthReason = 0;
182     csp.length = 0;
183     csp.majorVersion = X_PROTOCOL;
184     csp.minorVersion = X_PROTOCOL_REVISION;
185     if (client->swapped)
186         WriteSConnSetupPrefix(client, &csp);
187     else
188         (void)WriteToClient(client, sz_xConnSetupPrefix, (char *) &csp);
189
190     /*
191      * Next time the client sends the real auth data, we want
192      * ProcEstablishConnection to be called.
193      */
194
195     freq.reqType = 1;
196     freq.length = (sz_xReq + sz_xConnClientPrefix) >> 2;
197     client->swapped = FALSE;
198     if (!InsertFakeRequest(client, (char *)&freq, sz_xReq))
199     {
200         *reason = "internal error";
201         return (XID) -1;
202     }
203
204     return (XID) 0;
205 #else
206     *reason = "method not supported";
207     return (XID) -1;
208 #endif
209 }