2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: mp_auth_functions.C /main/3 1999/10/14 18:43:11 mgreess $ */
24 /******************************************************************************
27 Copyright 1993, 1998 The Open Group
31 The above copyright notice and this permission notice shall be included in
32 all copies or substantial portions of the Software.
34 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
38 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
39 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 Except as contained in this notice, the name of The Open Group shall not be
42 used in advertising or otherwise to promote the sale, use or other dealings
43 in this Software without prior written authorization from The Open Group.
45 Author: Ralph Mor, X Consortium
46 ******************************************************************************/
48 * This file was copied and altered from libICE/authutil.c
49 * The 'Ice' prefix has been replaced by tt_ for functions
50 * and by _tt_ for data types.
57 #include "mp/mp_auth_functions.h"
62 extern char *getenv();
72 extern unsigned sleep ();
78 static int read_short(FILE *, unsigned short *);
79 static int read_string(FILE *, char **);
80 static int read_counted_string(FILE *, unsigned short *, char **);
81 static int write_short(FILE *, unsigned short);
82 static int write_string(FILE *, char *);
83 static int write_counted_string(FILE *, unsigned short, char *);
88 * The following routines are for manipulating the .TTauthority file
95 static char slashDotTTauthority[] = "/.TTauthority";
107 if (name = getenv ("TTAUTHORITY"))
110 name = getenv ("HOME");
117 int len1 = 0, len2 = 0;
119 if ((ptr1 = getenv("HOMEDRIVE")) && (ptr2 = getenv("HOMEDIR"))) {
120 len1 = strlen (ptr1);
121 len2 = strlen (ptr2);
122 } else if (ptr2 = getenv("USERNAME")) {
123 len1 = strlen (ptr1 = "/users/");
124 len2 = strlen (ptr2);
126 if ((len1 + len2 + 1) < PATH_MAX) {
127 sprintf (dir, "%s%s", ptr1, (ptr2) ? ptr2 : "");
135 size = strlen (name) + strlen (&slashDotTTauthority[1]) + 2;
141 buf = (char*) malloc ((unsigned) size);
148 strcat (buf, slashDotTTauthority + (name[1] == '\0' ? 1 : 0));
156 _tt_LockAuthFile(char *file_name, int retries, int timeout, long dead)
158 char creat_name[1025], link_name[1025];
163 if ((int) strlen (file_name) > 1022)
164 return (_tt_AuthLockError);
166 strcpy (creat_name, file_name);
167 strcat (creat_name, "-c");
168 strcpy (link_name, file_name);
169 strcat (link_name, "-l");
171 if (stat (creat_name, &statb) != -1)
173 now = time ((Time_t *) 0);
176 * NFS may cause ctime to be before now, special
177 * case a 0 deadtime to force lock removal
180 if (dead == 0 || now - statb.st_ctime > dead)
191 creat_fd = creat (creat_name, 0666);
196 return (_tt_AuthLockError);
204 if (link (creat_name, link_name) != -1)
205 return (_tt_AuthLockSuccess);
209 creat_fd = -1; /* force re-creat next time around */
214 return (_tt_AuthLockError);
217 sleep ((unsigned) timeout);
221 return (_tt_AuthLockTimeout);
227 _tt_UnlockAuthFile(char *file_name)
230 char creat_name[1025];
232 char link_name[1025];
234 if ((int) strlen (file_name) > 1022)
238 strcpy (creat_name, file_name);
239 strcat (creat_name, "-c");
241 strcpy (link_name, file_name);
242 strcat (link_name, "-l");
253 _tt_ReadAuthFileEntry(FILE *auth_file)
255 _tt_AuthFileEntry local;
256 _tt_AuthFileEntry *ret;
258 local.protocol_name = NULL;
259 local.protocol_data = NULL;
260 local.network_id = NULL;
261 local.auth_name = NULL;
262 local.auth_data = NULL;
264 if (!read_string (auth_file, &local.protocol_name))
267 if (!read_counted_string (auth_file,
268 &local.protocol_data_length, &local.protocol_data))
271 if (!read_string (auth_file, &local.network_id))
274 if (!read_string (auth_file, &local.auth_name))
277 if (!read_counted_string (auth_file,
278 &local.auth_data_length, &local.auth_data))
281 if (!(ret = (_tt_AuthFileEntry *) malloc (sizeof (_tt_AuthFileEntry))))
290 if (local.protocol_name) free (local.protocol_name);
291 if (local.protocol_data) free (local.protocol_data);
292 if (local.network_id) free (local.network_id);
293 if (local.auth_name) free (local.auth_name);
294 if (local.auth_data) free (local.auth_data);
302 _tt_FreeAuthFileEntry(_tt_AuthFileEntry *auth)
306 if (auth->protocol_name) free (auth->protocol_name);
307 if (auth->protocol_data) free (auth->protocol_data);
308 if (auth->network_id) free (auth->network_id);
309 if (auth->auth_name) free (auth->auth_name);
310 if (auth->auth_data) free (auth->auth_data);
311 free ((char *) auth);
318 _tt_WriteAuthFileEntry(FILE *auth_file, _tt_AuthFileEntry *auth)
320 if (!write_string (auth_file, auth->protocol_name))
323 if (!write_counted_string (auth_file,
324 auth->protocol_data_length, auth->protocol_data))
327 if (!write_string (auth_file, auth->network_id))
330 if (!write_string (auth_file, auth->auth_name))
333 if (!write_counted_string (auth_file,
334 auth->auth_data_length, auth->auth_data))
343 _tt_GetAuthFileEntry(char *protocol_name, char *network_id, char *auth_name)
347 _tt_AuthFileEntry *entry;
349 if (!(filename = _tt_AuthFileName ()))
352 if (access (filename, R_OK) != 0) /* checks REAL id */
355 if (!(auth_file = fopen (filename, "rb")))
360 if (!(entry = _tt_ReadAuthFileEntry (auth_file)))
363 if (strcmp (protocol_name, entry->protocol_name) == 0 &&
364 strcmp (network_id, entry->network_id) == 0 &&
365 strcmp (auth_name, entry->auth_name) == 0)
370 _tt_FreeAuthFileEntry (entry);
380 * MIT-MAGIC-COOKIE-1 is a sample authentication method implemented by
381 * the SI. It is not part of standard ICElib.
386 _tt_GenerateMagicCookie(int len)
394 if ((auth = (char *) malloc (len + 1)) == NULL)
400 X_GETTIMEOFDAY (&now);
401 ldata[0] = now.tv_sec;
402 ldata[1] = now.tv_usec;
403 seed = (int) ((ldata[0] << 11) ^ ldata[1]);
409 ldata[0] = time ((long *) 0);
410 ldata[1] = getpid ();
411 seed = (int) (ldata[0]) + (ldata[1] << 16);
415 for (i = 0; i < len; i++)
419 ( (value & 0xff000000) >> 24) ^
420 ( (value & 0xff0000) >> 16) ^
421 ( (value & 0xff00) >> 8) ^
436 read_short(FILE *file, unsigned short *shortp)
438 unsigned char file_short[2];
440 if (fread ((char *) file_short, (int) sizeof (file_short), 1, file) != 1)
443 *shortp = file_short[0] * 256 + file_short[1];
449 read_string(FILE *file, char **stringp)
454 if (!read_short (file, &len))
463 data = (char*) malloc ((unsigned) len + 1);
468 if (fread (data, (int) sizeof (char), (int) len, file) != len)
484 read_counted_string(FILE *file, unsigned short *countp, char **stringp)
489 if (!read_short (file, &len))
498 data = (char*) malloc ((unsigned) len);
503 if (fread (data, (int) sizeof (char), (int) len, file) != len)
518 write_short(FILE *file, unsigned short s)
520 unsigned char file_short[2];
522 file_short[0] = (s & (unsigned) 0xff00) >> 8;
523 file_short[1] = s & 0xff;
525 if (fwrite ((char *) file_short, (int) sizeof (file_short), 1, file) != 1)
533 write_string(FILE *file, char *string)
535 unsigned short count = strlen (string);
537 if (!write_short (file, count))
540 if (fwrite (string, (int) sizeof (char), (int) count, file) != count)
548 write_counted_string(FILE *file, unsigned short count, char *string)
550 if (!write_short (file, count))
553 if (fwrite (string, (int) sizeof (char), (int) count, file) != count)