1 /******************************************************************************
4 Copyright 1993, 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
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.
26 Author: Ralph Mor, X Consortium
27 ******************************************************************************/
32 #include <X11/ICE/ICElib.h>
33 #include "ICElibint.h"
34 #include <X11/ICE/ICEutil.h>
42 extern char* getenv(const char*);
49 extern unsigned sleep ();
55 static Status read_short (FILE *file, unsigned short *shortp);
56 static Status read_string (FILE *file, char **stringp);
57 static Status read_counted_string (FILE *file, unsigned short *countp, char **stringp);
58 static Status write_short (FILE *file, unsigned short s);
59 static Status write_string (FILE *file, char *string);
60 static Status write_counted_string (FILE *file, unsigned short count, char *string);
65 * The following routines are for manipulating the .ICEauthority file
66 * These are utility functions - they are not part of the standard
67 * ICE library specification.
71 IceAuthFileName (void)
73 static char slashDotICEauthority[] = "/.ICEauthority";
78 #if defined(WIN32) || defined(__UNIXOS2__)
85 if ((name = getenv ("ICEAUTHORITY")))
88 name = getenv ("HOME");
95 int len1 = 0, len2 = 0;
97 if ((ptr1 = getenv("HOMEDRIVE")) && (ptr2 = getenv("HOMEDIR"))) {
100 } else if ((ptr2 = getenv("USERNAME"))) {
101 len1 = strlen (ptr1 = "/users/");
102 len2 = strlen (ptr2);
104 if ((len1 + len2 + 1) < PATH_MAX) {
105 sprintf (dir, "%s%s", ptr1, (ptr2) ? ptr2 : "");
118 size = strlen (name) + strlen (&slashDotICEauthority[1]) + 2;
124 buf = malloc ((unsigned) size);
131 strcat (buf, slashDotICEauthority + (name[1] == '\0' ? 1 : 0));
146 char creat_name[1025], link_name[1025];
151 if ((int) strlen (file_name) > 1022)
152 return (IceAuthLockError);
154 strcpy (creat_name, file_name);
155 strcat (creat_name, "-c");
156 strcpy (link_name, file_name);
157 strcat (link_name, "-l");
159 if (stat (creat_name, &statb) != -1)
161 now = time ((Time_t *) 0);
164 * NFS may cause ctime to be before now, special
165 * case a 0 deadtime to force lock removal
168 if (dead == 0 || now - statb.st_ctime > dead)
179 creat_fd = creat (creat_name, 0666);
184 return (IceAuthLockError);
192 if (link (creat_name, link_name) != -1)
193 return (IceAuthLockSuccess);
197 creat_fd = -1; /* force re-creat next time around */
202 return (IceAuthLockError);
205 sleep ((unsigned) timeout);
209 return (IceAuthLockTimeout);
220 char creat_name[1025];
222 char link_name[1025];
224 if ((int) strlen (file_name) > 1022)
228 strcpy (creat_name, file_name);
229 strcat (creat_name, "-c");
231 strcpy (link_name, file_name);
232 strcat (link_name, "-l");
243 IceReadAuthFileEntry (
247 IceAuthFileEntry local;
248 IceAuthFileEntry *ret;
250 local.protocol_name = NULL;
251 local.protocol_data = NULL;
252 local.network_id = NULL;
253 local.auth_name = NULL;
254 local.auth_data = NULL;
256 if (!read_string (auth_file, &local.protocol_name))
259 if (!read_counted_string (auth_file,
260 &local.protocol_data_length, &local.protocol_data))
263 if (!read_string (auth_file, &local.network_id))
266 if (!read_string (auth_file, &local.auth_name))
269 if (!read_counted_string (auth_file,
270 &local.auth_data_length, &local.auth_data))
273 if (!(ret = (IceAuthFileEntry *) malloc (sizeof (IceAuthFileEntry))))
282 if (local.protocol_name) free (local.protocol_name);
283 if (local.protocol_data) free (local.protocol_data);
284 if (local.network_id) free (local.network_id);
285 if (local.auth_name) free (local.auth_name);
286 if (local.auth_data) free (local.auth_data);
294 IceFreeAuthFileEntry (
295 IceAuthFileEntry *auth
300 if (auth->protocol_name) free (auth->protocol_name);
301 if (auth->protocol_data) free (auth->protocol_data);
302 if (auth->network_id) free (auth->network_id);
303 if (auth->auth_name) free (auth->auth_name);
304 if (auth->auth_data) free (auth->auth_data);
305 free ((char *) auth);
312 IceWriteAuthFileEntry (
314 IceAuthFileEntry *auth
317 if (!write_string (auth_file, auth->protocol_name))
320 if (!write_counted_string (auth_file,
321 auth->protocol_data_length, auth->protocol_data))
324 if (!write_string (auth_file, auth->network_id))
327 if (!write_string (auth_file, auth->auth_name))
330 if (!write_counted_string (auth_file,
331 auth->auth_data_length, auth->auth_data))
340 IceGetAuthFileEntry (
341 const char *protocol_name,
342 const char *network_id,
343 const char *auth_name
348 IceAuthFileEntry *entry;
350 if (!(filename = IceAuthFileName ()))
353 if (access (filename, R_OK) != 0) /* checks REAL id */
356 if (!(auth_file = fopen (filename, "rb")))
361 if (!(entry = IceReadAuthFileEntry (auth_file)))
364 if (strcmp (protocol_name, entry->protocol_name) == 0 &&
365 strcmp (network_id, entry->network_id) == 0 &&
366 strcmp (auth_name, entry->auth_name) == 0)
371 IceFreeAuthFileEntry (entry);
386 read_short (FILE *file, unsigned short *shortp)
388 unsigned char file_short[2];
390 if (fread ((char *) file_short, (int) sizeof (file_short), 1, file) != 1)
393 *shortp = file_short[0] * 256 + file_short[1];
399 read_string (FILE *file, char **stringp)
405 if (!read_short (file, &len))
408 data = malloc ((unsigned) len + 1);
415 if (fread (data, (int) sizeof (char), (int) len, file) != len)
431 read_counted_string (FILE *file, unsigned short *countp, char **stringp)
436 if (!read_short (file, &len))
445 data = malloc ((unsigned) len);
450 if (fread (data, (int) sizeof (char), (int) len, file) != len)
465 write_short (FILE *file, unsigned short s)
467 unsigned char file_short[2];
469 file_short[0] = (s & (unsigned) 0xff00) >> 8;
470 file_short[1] = s & 0xff;
472 if (fwrite ((char *) file_short, (int) sizeof (file_short), 1, file) != 1)
480 write_string (FILE *file, char *string)
482 unsigned short count = strlen (string);
484 if (!write_short (file, count))
487 if (fwrite (string, (int) sizeof (char), (int) count, file) != count)
495 write_counted_string (FILE *file, unsigned short count, char *string)
497 if (!write_short (file, count))
500 if (fwrite (string, (int) sizeof (char), (int) count, file) != count)