| 1 |
|
| 2 |
/* |
| 3 |
|
| 4 |
Copyright 1988, 1998 The Open Group |
| 5 |
|
| 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. |
| 11 |
|
| 12 |
The above copyright notice and this permission notice shall be included in |
| 13 |
all copies or substantial portions of the Software. |
| 14 |
|
| 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. |
| 21 |
|
| 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. |
| 25 |
|
| 26 |
*/ |
| 27 |
|
| 28 |
#ifdef HAVE_CONFIG_H |
| 29 |
#include <config.h> |
| 30 |
#endif |
| 31 |
#include <nx-X11/Xauth.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#ifdef NX_TRANS_SOCKET |
| 34 |
#include <errno.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
static int |
| 38 |
read_short (unsigned short *shortp, FILE *file) |
| 39 |
{ |
| 40 |
unsigned char file_short[2]; |
| 41 |
|
| 42 |
#ifdef NX_TRANS_SOCKET |
| 43 |
/* |
| 44 |
* Added a check on EINTR to prevent the fread() call to be |
| 45 |
* interrupted by any signal not blocked by OsBlockSignals(). |
| 46 |
*/ |
| 47 |
|
| 48 |
for (;;) { |
| 49 |
if (fread ((char *) file_short, sizeof (file_short), 1, file) != 1) { |
| 50 |
if (errno == EINTR && ferror (file)) { |
| 51 |
perror ("Reading from auth file"); |
| 52 |
clearerr (file); |
| 53 |
continue; |
| 54 |
} |
| 55 |
return 0; |
| 56 |
} |
| 57 |
break; |
| 58 |
} |
| 59 |
#else |
| 60 |
if (fread ((char *) file_short, sizeof (file_short), 1, file) != 1) |
| 61 |
return 0; |
| 62 |
#endif |
| 63 |
*shortp = file_short[0] * 256 + file_short[1]; |
| 64 |
return 1; |
| 65 |
} |
| 66 |
|
| 67 |
static int |
| 68 |
read_counted_string (unsigned short *countp, char **stringp, FILE *file) |
| 69 |
{ |
| 70 |
unsigned short len; |
| 71 |
char *data; |
| 72 |
|
| 73 |
if (read_short (&len, file) == 0) |
| 74 |
return 0; |
| 75 |
if (len == 0) { |
| 76 |
data = NULL; |
| 77 |
} else { |
| 78 |
data = malloc ((unsigned) len); |
| 79 |
if (!data) |
| 80 |
return 0; |
| 81 |
#ifdef NX_TRANS_SOCKET |
| 82 |
for (;;) |
| 83 |
{ |
| 84 |
if (fread (data, sizeof (char), len, file) != len) |
| 85 |
{ |
| 86 |
if (errno == EINTR && ferror (file)) |
| 87 |
{ |
| 88 |
perror ("Reading from auth file"); |
| 89 |
clearerr (file); |
| 90 |
continue; |
| 91 |
} |
| 92 |
bzero (data, len); |
| 93 |
free (data); |
| 94 |
return 0; |
| 95 |
} |
| 96 |
break; |
| 97 |
} |
| 98 |
#else |
| 99 |
if (fread (data, sizeof (char), len, file) != len) { |
| 100 |
bzero (data, len); |
| 101 |
free (data); |
| 102 |
return 0; |
| 103 |
} |
| 104 |
#endif |
| 105 |
} |
| 106 |
*stringp = data; |
| 107 |
*countp = len; |
| 108 |
return 1; |
| 109 |
} |
| 110 |
|
| 111 |
Xauth * |
| 112 |
XauReadAuth (FILE *auth_file) |
| 113 |
{ |
| 114 |
Xauth local; |
| 115 |
Xauth *ret; |
| 116 |
|
| 117 |
if (read_short (&local.family, auth_file) == 0) |
| 118 |
return NULL; |
| 119 |
if (read_counted_string (&local.address_length, &local.address, auth_file) == 0) |
| 120 |
return NULL; |
| 121 |
if (read_counted_string (&local.number_length, &local.number, auth_file) == 0) { |
| 122 |
free (local.address); |
| 123 |
return NULL; |
| 124 |
} |
| 125 |
if (read_counted_string (&local.name_length, &local.name, auth_file) == 0) { |
| 126 |
free (local.address); |
| 127 |
free (local.number); |
| 128 |
return NULL; |
| 129 |
} |
| 130 |
if (read_counted_string (&local.data_length, &local.data, auth_file) == 0) { |
| 131 |
free (local.address); |
| 132 |
free (local.number); |
| 133 |
free (local.name); |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
ret = (Xauth *) malloc (sizeof (Xauth)); |
| 137 |
if (!ret) { |
| 138 |
free (local.address); |
| 139 |
free (local.number); |
| 140 |
free (local.name); |
| 141 |
if (local.data) { |
| 142 |
bzero (local.data, local.data_length); |
| 143 |
free (local.data); |
| 144 |
} |
| 145 |
return NULL; |
| 146 |
} |
| 147 |
*ret = local; |
| 148 |
return ret; |
| 149 |
} |