| 1 | /* |
| 2 | |
| 3 | Copyright 1988, 1998 The Open Group |
| 4 | |
| 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 |
| 9 | documentation. |
| 10 | |
| 11 | The above copyright notice and this permission notice shall be included |
| 12 | in all copies or substantial portions of the Software. |
| 13 | |
| 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 17 | IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | OTHER DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | Except as contained in this notice, the name of The Open Group shall |
| 23 | not be used in advertising or otherwise to promote the sale, use or |
| 24 | other dealings in this Software without prior written authorization |
| 25 | from The Open Group. |
| 26 | |
| 27 | */ |
| 28 | |
| 29 | /* |
| 30 | * MIT-MAGIC-COOKIE-1 authorization scheme |
| 31 | * Author: Keith Packard, MIT X Consortium |
| 32 | */ |
| 33 | |
| 34 | #ifdef HAVE_DIX_CONFIG_H |
| 35 | #include <dix-config.h> |
| 36 | #endif |
| 37 | |
| 38 | #include <X11/X.h> |
| 39 | #include "os.h" |
| 40 | #include "osdep.h" |
| 41 | #include "dixstruct.h" |
| 42 | |
| 43 | static struct auth { |
| 44 | struct auth *next; |
| 45 | unsigned short len; |
| 46 | char *data; |
| 47 | XID id; |
| 48 | } *mit_auth; |
| 49 | |
| 50 | int |
| 51 | MitAddCookie(unsigned short data_length, const char *data, XID id) |
| 52 | { |
| 53 | struct auth *new; |
| 54 | |
| 55 | new = malloc(sizeof(struct auth)); |
| 56 | if (!new) |
| 57 | return 0; |
| 58 | new->data = malloc((unsigned) data_length); |
| 59 | if (!new->data) { |
| 60 | free(new); |
| 61 | return 0; |
| 62 | } |
| 63 | new->next = mit_auth; |
| 64 | mit_auth = new; |
| 65 | memmove(new->data, data, (int) data_length); |
| 66 | new->len = data_length; |
| 67 | new->id = id; |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | XID |
| 72 | MitCheckCookie(unsigned short data_length, |
| 73 | const char *data, ClientPtr client, const char **reason) |
| 74 | { |
| 75 | struct auth *auth; |
| 76 | |
| 77 | for (auth = mit_auth; auth; auth = auth->next) { |
| 78 | if (data_length == auth->len && |
| 79 | memcmp(data, auth->data, (int) data_length) == 0) |
| 80 | return auth->id; |
| 81 | } |
| 82 | *reason = "Invalid MIT-MAGIC-COOKIE-1 key"; |
| 83 | return (XID) -1; |
| 84 | } |
| 85 | |
| 86 | int |
| 87 | MitResetCookie(void) |
| 88 | { |
| 89 | struct auth *auth, *next; |
| 90 | |
| 91 | for (auth = mit_auth; auth; auth = next) { |
| 92 | next = auth->next; |
| 93 | free(auth->data); |
| 94 | free(auth); |
| 95 | } |
| 96 | mit_auth = 0; |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | XID |
| 101 | MitToID(unsigned short data_length, char *data) |
| 102 | { |
| 103 | struct auth *auth; |
| 104 | |
| 105 | for (auth = mit_auth; auth; auth = auth->next) { |
| 106 | if (data_length == auth->len && |
| 107 | memcmp(data, auth->data, data_length) == 0) |
| 108 | return auth->id; |
| 109 | } |
| 110 | return (XID) -1; |
| 111 | } |
| 112 | |
| 113 | int |
| 114 | MitFromID(XID id, unsigned short *data_lenp, char **datap) |
| 115 | { |
| 116 | struct auth *auth; |
| 117 | |
| 118 | for (auth = mit_auth; auth; auth = auth->next) { |
| 119 | if (id == auth->id) { |
| 120 | *data_lenp = auth->len; |
| 121 | *datap = auth->data; |
| 122 | return 1; |
| 123 | } |
| 124 | } |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int |
| 129 | MitRemoveCookie(unsigned short data_length, const char *data) |
| 130 | { |
| 131 | struct auth *auth, *prev; |
| 132 | |
| 133 | prev = 0; |
| 134 | for (auth = mit_auth; auth; prev = auth, auth = auth->next) { |
| 135 | if (data_length == auth->len && |
| 136 | memcmp(data, auth->data, data_length) == 0) { |
| 137 | if (prev) |
| 138 | prev->next = auth->next; |
| 139 | else |
| 140 | mit_auth = auth->next; |
| 141 | free(auth->data); |
| 142 | free(auth); |
| 143 | return 1; |
| 144 | } |
| 145 | } |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | #ifdef XCSECURITY |
| 150 | |
| 151 | static char cookie[16]; /* 128 bits */ |
| 152 | |
| 153 | XID |
| 154 | MitGenerateCookie(unsigned data_length, |
| 155 | const char *data, |
| 156 | XID id, unsigned *data_length_return, char **data_return) |
| 157 | { |
| 158 | int i = 0; |
| 159 | int status; |
| 160 | |
| 161 | while (data_length--) { |
| 162 | cookie[i++] += *data++; |
| 163 | if (i >= sizeof(cookie)) |
| 164 | i = 0; |
| 165 | } |
| 166 | GenerateRandomData(sizeof(cookie), cookie); |
| 167 | status = MitAddCookie(sizeof(cookie), cookie, id); |
| 168 | if (!status) { |
| 169 | id = -1; |
| 170 | } |
| 171 | else { |
| 172 | *data_return = cookie; |
| 173 | *data_length_return = sizeof(cookie); |
| 174 | } |
| 175 | return id; |
| 176 | } |
| 177 | |
| 178 | #endif /* XCSECURITY */ |