3 Copyright 1996, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
30 #include <X11/Xlibint.h>
32 #include <X11/extensions/Xext.h>
33 #include <X11/extensions/extutil.h>
34 #include <X11/extensions/securproto.h>
35 #include <X11/extensions/security.h>
37 static XExtensionInfo _Security_info_data;
38 static XExtensionInfo *Security_info = &_Security_info_data;
39 static const char *Security_extension_name = SECURITY_EXTENSION_NAME;
41 #define SecurityCheckExtension(dpy,i,val) \
42 XextCheckExtension (dpy, i, Security_extension_name, val)
43 #define SecuritySimpleCheckExtension(dpy,i) \
44 XextSimpleCheckExtension (dpy, i, Security_extension_name)
46 #define SecurityGetReq(name,req,info) GetReq (name, req); \
47 req->reqType = info->codes->major_opcode; \
48 req->securityReqType = X_##name;
50 /*****************************************************************************
52 * private utility routines *
54 *****************************************************************************/
57 * find_display - locate the display info block
59 static int close_display(Display *dpy, XExtCodes *codes);
60 static Bool wire_to_event(Display *dpy, XEvent *event, xEvent *wire);
61 static Status event_to_wire(Display *dpy, XEvent *event, xEvent *wire);
62 static char *error_string(Display *dpy, int code, XExtCodes *codes,
65 static XExtensionHooks Security_extension_hooks = {
70 NULL, /* create_font */
72 close_display, /* close_display */
73 wire_to_event, /* wire_to_event */
74 event_to_wire, /* event_to_wire */
76 error_string /* error_string */
79 static const char *security_error_list[] = {
81 "BadAuthorizationProtocol"
84 static XEXT_GENERATE_FIND_DISPLAY (find_display, Security_info,
85 Security_extension_name,
86 &Security_extension_hooks,
87 XSecurityNumberEvents, NULL)
89 static XEXT_GENERATE_CLOSE_DISPLAY (close_display, Security_info)
92 XEXT_GENERATE_ERROR_STRING(error_string, Security_extension_name,
93 XSecurityNumberErrors, security_error_list)
96 wire_to_event(Display *dpy, XEvent *event, xEvent *wire)
98 XExtDisplayInfo *info = find_display(dpy);
100 SecurityCheckExtension (dpy, info, False);
102 switch ((wire->u.u.type & 0x7F) - info->codes->first_event)
104 case XSecurityAuthorizationRevoked:
106 xSecurityAuthorizationRevokedEvent *rwire =
107 (xSecurityAuthorizationRevokedEvent *)wire;
108 XSecurityAuthorizationRevokedEvent *revent =
109 (XSecurityAuthorizationRevokedEvent *)event;
111 revent->type = rwire->type & 0x7F;
112 revent->serial = _XSetLastRequestRead(dpy,
113 (xGenericReply *) wire);
114 revent->send_event = (rwire->type & 0x80) != 0;
115 revent->display = dpy;
116 revent->auth_id = rwire->authId;
124 event_to_wire(Display *dpy, XEvent *event, xEvent *wire)
126 XExtDisplayInfo *info = find_display(dpy);
128 SecurityCheckExtension(dpy, info, False);
130 switch ((event->type & 0x7F) - info->codes->first_event)
132 case XSecurityAuthorizationRevoked:
134 xSecurityAuthorizationRevokedEvent *rwire =
135 (xSecurityAuthorizationRevokedEvent *)wire;
136 XSecurityAuthorizationRevokedEvent *revent =
137 (XSecurityAuthorizationRevokedEvent *)event;
138 rwire->type = revent->type | (revent->send_event ? 0x80 : 0);
139 rwire->sequenceNumber = revent->serial & 0xFFFF;
146 /*****************************************************************************
148 * Security public interfaces *
150 *****************************************************************************/
152 Status XSecurityQueryExtension (
154 int *major_version_return,
155 int *minor_version_return)
157 XExtDisplayInfo *info = find_display (dpy);
158 xSecurityQueryVersionReply rep;
159 register xSecurityQueryVersionReq *req;
161 if (!XextHasExtension (info))
162 return (Status)0; /* failure */
165 SecurityGetReq (SecurityQueryVersion, req, info);
166 req->majorVersion = SECURITY_MAJOR_VERSION;
167 req->minorVersion = SECURITY_MINOR_VERSION;
169 if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
172 return (Status)0; /* failure */
174 *major_version_return = rep.majorVersion;
175 *minor_version_return = rep.minorVersion;
180 if (*major_version_return != SECURITY_MAJOR_VERSION)
181 return (Status)0; /* failure */
183 return (Status)1; /* success */
187 XSecurityAllocXauth(void)
189 return Xcalloc(1, sizeof(Xauth));
193 XSecurityFreeXauth(Xauth *auth)
203 y = (mask >> 1) &033333333333;
204 y = mask - y - ((y >>1) & 033333333333);
205 return (((y + (y >> 3)) & 030707070707) % 077);
209 XSecurityGenerateAuthorization(
212 unsigned long valuemask,
213 XSecurityAuthorizationAttributes *attributes,
214 XSecurityAuthorization *auth_id_return)
216 XExtDisplayInfo *info = find_display (dpy);
217 register xSecurityGenerateAuthorizationReq *req;
218 xSecurityGenerateAuthorizationReply rep;
220 unsigned long values[3];
221 unsigned long *value = values;
222 unsigned int nvalues;
224 *auth_id_return = 0; /* in case we fail */
226 /* make sure extension is available */
228 SecurityCheckExtension (dpy, info, (Xauth *)NULL);
231 SecurityGetReq(SecurityGenerateAuthorization, req, info);
233 req->nbytesAuthProto = auth_in->name_length;
234 req->nbytesAuthData = auth_in->data_length;
236 /* adjust length to account for auth name and data */
237 req->length += (auth_in->name_length + (unsigned)3) >> 2;
238 req->length += (auth_in->data_length + (unsigned)3) >> 2;
240 /* adjust length to account for list of values */
241 req->valueMask = valuemask & XSecurityAllAuthorizationAttributes;
242 nvalues = Ones(req->valueMask);
243 req->length += nvalues;
245 /* send auth name and data */
246 Data(dpy, auth_in->name, auth_in->name_length);
247 Data(dpy, auth_in->data, auth_in->data_length);
250 if (valuemask & XSecurityTimeout) *value++ = attributes->timeout;
251 if (valuemask & XSecurityTrustLevel) *value++ = attributes->trust_level;
252 if (valuemask & XSecurityGroup) *value++ = attributes->group;
253 if (valuemask & XSecurityEventMask) *value++ = attributes->event_mask;
256 Data32(dpy, (long *)values, (long)nvalues);
258 if (!_XReply (dpy, (xReply *) &rep, 0, xFalse)) {
261 return (Xauth *)NULL;
264 *auth_id_return = rep.authId;
266 /* Allocate space for the Xauth struct and the auth name and data all
267 * in one hunk. This lets XSecurityFreeXauth not have to care
268 * about whether the auth was allocated here or in
269 * XSecurityAllocXauth; in both cases, you just free one pointer.
272 if ((auth_return = (Xauth *)Xcalloc(1,
273 (sizeof(Xauth) + auth_in->name_length + rep.dataLength))))
275 auth_return->data_length = rep.dataLength;
276 auth_return->data = (char *)&auth_return[1];
277 _XReadPad(dpy, auth_return->data, (long)rep.dataLength);
279 auth_return->name_length = auth_in->name_length;
280 auth_return->name = auth_return->data + auth_return->data_length;
281 memcpy(auth_return->name, auth_in->name, auth_return->name_length);
285 _XEatData(dpy, (unsigned long) (rep.dataLength + 3) & ~3);
292 } /* XSecurityGenerateAuthorization */
295 XSecurityRevokeAuthorization(
297 XSecurityAuthorization auth_id)
299 XExtDisplayInfo *info = find_display (dpy);
300 xSecurityRevokeAuthorizationReq *req;
302 SecurityCheckExtension (dpy, info, 0);
304 SecurityGetReq(SecurityRevokeAuthorization, req, info);
305 req->authId = auth_id;
309 } /* XSecurityRevokeAuthorization */