Initial commit
[xorg_rtime.git] / libXext-1.0.2 / src / XSecurity.c
blob161ade3b7c719683c2738fe7703817e3fc941ac8
1 /* $Xorg: XSecurity.c,v 1.6 2001/02/09 02:03:49 xorgcvs Exp $ */
2 /*
4 Copyright 1996, 1998 The Open Group
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation.
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 Except as contained in this notice, the name of The Open Group shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from The Open Group.
27 /* $XFree86: xc/lib/Xext/XSecurity.c,v 1.5 2002/10/16 00:37:27 dawes Exp $ */
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32 #include <X11/Xlibint.h>
33 #include <stdio.h>
34 #include <X11/extensions/Xext.h>
35 #include <X11/extensions/extutil.h>
36 #include <X11/extensions/securstr.h>
38 static XExtensionInfo _Security_info_data;
39 static XExtensionInfo *Security_info = &_Security_info_data;
40 static char *Security_extension_name = SECURITY_EXTENSION_NAME;
42 #define SecurityCheckExtension(dpy,i,val) \
43 XextCheckExtension (dpy, i, Security_extension_name, val)
44 #define SecuritySimpleCheckExtension(dpy,i) \
45 XextSimpleCheckExtension (dpy, i, Security_extension_name)
47 #define SecurityGetReq(name,req,info) GetReq (name, req); \
48 req->reqType = info->codes->major_opcode; \
49 req->securityReqType = X_##name;
51 /*****************************************************************************
52 * *
53 * private utility routines *
54 * *
55 *****************************************************************************/
58 * find_display - locate the display info block
60 static int close_display(Display *dpy, XExtCodes *codes);
61 static Bool wire_to_event(Display *dpy, XEvent *event, xEvent *wire);
62 static Status event_to_wire(Display *dpy, XEvent *event, xEvent *wire);
63 static char *error_string(Display *dpy, int code, XExtCodes *codes,
64 char *buf, int n);
66 static XExtensionHooks Security_extension_hooks = {
67 NULL, /* create_gc */
68 NULL, /* copy_gc */
69 NULL, /* flush_gc */
70 NULL, /* free_gc */
71 NULL, /* create_font */
72 NULL, /* free_font */
73 close_display, /* close_display */
74 wire_to_event, /* wire_to_event */
75 event_to_wire, /* event_to_wire */
76 NULL, /* error */
77 error_string /* error_string */
80 static char *security_error_list[] = {
81 "BadAuthorization"
82 "BadAuthorizationProtocol"
85 static XEXT_GENERATE_FIND_DISPLAY (find_display, Security_info,
86 Security_extension_name,
87 &Security_extension_hooks,
88 XSecurityNumberEvents, NULL)
90 static XEXT_GENERATE_CLOSE_DISPLAY (close_display, Security_info)
92 static
93 XEXT_GENERATE_ERROR_STRING(error_string, Security_extension_name,
94 XSecurityNumberErrors, security_error_list)
96 static Bool
97 wire_to_event(Display *dpy, XEvent *event, xEvent *wire)
99 XExtDisplayInfo *info = find_display(dpy);
101 SecurityCheckExtension (dpy, info, False);
103 switch ((wire->u.u.type & 0x7F) - info->codes->first_event)
105 case XSecurityAuthorizationRevoked:
107 xSecurityAuthorizationRevokedEvent *rwire =
108 (xSecurityAuthorizationRevokedEvent *)wire;
109 XSecurityAuthorizationRevokedEvent *revent =
110 (XSecurityAuthorizationRevokedEvent *)event;
112 revent->type = rwire->type & 0x7F;
113 revent->serial = _XSetLastRequestRead(dpy,
114 (xGenericReply *) wire);
115 revent->send_event = (rwire->type & 0x80) != 0;
116 revent->display = dpy;
117 revent->auth_id = rwire->authId;
118 return True;
121 return False;
124 static Status
125 event_to_wire(Display *dpy, XEvent *event, xEvent *wire)
127 XExtDisplayInfo *info = find_display(dpy);
129 SecurityCheckExtension(dpy, info, False);
131 switch ((event->type & 0x7F) - info->codes->first_event)
133 case XSecurityAuthorizationRevoked:
135 xSecurityAuthorizationRevokedEvent *rwire =
136 (xSecurityAuthorizationRevokedEvent *)wire;
137 XSecurityAuthorizationRevokedEvent *revent =
138 (XSecurityAuthorizationRevokedEvent *)event;
139 rwire->type = revent->type | (revent->send_event ? 0x80 : 0);
140 rwire->sequenceNumber = revent->serial & 0xFFFF;
141 return True;
144 return False;
147 /*****************************************************************************
149 * Security public interfaces *
151 *****************************************************************************/
153 Status XSecurityQueryExtension (
154 Display *dpy,
155 int *major_version_return,
156 int *minor_version_return)
158 XExtDisplayInfo *info = find_display (dpy);
159 xSecurityQueryVersionReply rep;
160 register xSecurityQueryVersionReq *req;
162 if (!XextHasExtension (info))
163 return (Status)0; /* failure */
165 LockDisplay (dpy);
166 SecurityGetReq (SecurityQueryVersion, req, info);
167 req->majorVersion = SECURITY_MAJOR_VERSION;
168 req->minorVersion = SECURITY_MINOR_VERSION;
170 if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
171 UnlockDisplay (dpy);
172 SyncHandle ();
173 return (Status)0; /* failure */
175 *major_version_return = rep.majorVersion;
176 *minor_version_return = rep.minorVersion;
177 UnlockDisplay (dpy);
179 SyncHandle ();
181 if (*major_version_return != SECURITY_MAJOR_VERSION)
182 return (Status)0; /* failure */
183 else
184 return (Status)1; /* success */
187 Xauth *
188 XSecurityAllocXauth(void)
190 return Xcalloc(1, sizeof(Xauth));
193 void
194 XSecurityFreeXauth(Xauth *auth)
196 XFree(auth);
199 static int
200 Ones(Mask mask)
202 register Mask y;
204 y = (mask >> 1) &033333333333;
205 y = mask - y - ((y >>1) & 033333333333);
206 return (((y + (y >> 3)) & 030707070707) % 077);
209 Xauth *
210 XSecurityGenerateAuthorization(
211 Display *dpy,
212 Xauth *auth_in,
213 unsigned long valuemask,
214 XSecurityAuthorizationAttributes *attributes,
215 XSecurityAuthorization *auth_id_return)
217 XExtDisplayInfo *info = find_display (dpy);
218 register xSecurityGenerateAuthorizationReq *req;
219 xSecurityGenerateAuthorizationReply rep;
220 Xauth *auth_return;
221 unsigned long values[3];
222 unsigned long *value = values;
223 unsigned int nvalues;
225 *auth_id_return = 0; /* in case we fail */
227 /* make sure extension is available */
229 SecurityCheckExtension (dpy, info, (Xauth *)NULL);
231 LockDisplay(dpy);
232 SecurityGetReq(SecurityGenerateAuthorization, req, info);
234 req->nbytesAuthProto = auth_in->name_length;
235 req->nbytesAuthData = auth_in->data_length;
237 /* adjust length to account for auth name and data */
238 req->length += (auth_in->name_length + (unsigned)3) >> 2;
239 req->length += (auth_in->data_length + (unsigned)3) >> 2;
241 /* adjust length to account for list of values */
242 req->valueMask = valuemask & XSecurityAllAuthorizationAttributes;
243 nvalues = Ones(req->valueMask);
244 req->length += nvalues;
246 /* send auth name and data */
247 Data(dpy, auth_in->name, auth_in->name_length);
248 Data(dpy, auth_in->data, auth_in->data_length);
250 /* send values */
251 if (valuemask & XSecurityTimeout) *value++ = attributes->timeout;
252 if (valuemask & XSecurityTrustLevel) *value++ = attributes->trust_level;
253 if (valuemask & XSecurityGroup) *value++ = attributes->group;
254 if (valuemask & XSecurityEventMask) *value++ = attributes->event_mask;
256 nvalues <<= 2;
257 Data32(dpy, (long *)values, (long)nvalues);
259 if (!_XReply (dpy, (xReply *) &rep, 0, xFalse)) {
260 UnlockDisplay (dpy);
261 SyncHandle ();
262 return (Xauth *)NULL;
265 *auth_id_return = rep.authId;
267 /* Allocate space for the Xauth struct and the auth name and data all
268 * in one hunk. This lets XSecurityFreeXauth not have to care
269 * about whether the auth was allocated here or in
270 * XSecurityAllocXauth; in both cases, you just free one pointer.
273 if ((auth_return = (Xauth *)Xcalloc(1,
274 (sizeof(Xauth) + auth_in->name_length + rep.dataLength))))
276 auth_return->data_length = rep.dataLength;
277 auth_return->data = (char *)&auth_return[1];
278 _XReadPad(dpy, auth_return->data, (long)rep.dataLength);
280 auth_return->name_length = auth_in->name_length;
281 auth_return->name = auth_return->data + auth_return->data_length;
282 memcpy(auth_return->name, auth_in->name, auth_return->name_length);
284 else
286 _XEatData(dpy, (unsigned long) (rep.dataLength + 3) & ~3);
289 UnlockDisplay (dpy);
290 SyncHandle ();
291 return auth_return;
293 } /* XSecurityGenerateAuthorization */
295 Status
296 XSecurityRevokeAuthorization(
297 Display *dpy,
298 XSecurityAuthorization auth_id)
300 XExtDisplayInfo *info = find_display (dpy);
301 xSecurityRevokeAuthorizationReq *req;
303 SecurityCheckExtension (dpy, info, 0);
304 LockDisplay(dpy);
305 SecurityGetReq(SecurityRevokeAuthorization, req, info);
306 req->authId = auth_id;
307 UnlockDisplay (dpy);
308 SyncHandle ();
309 return 1;
310 } /* XSecurityRevokeAuthorization */