/*
 *  @(#)mitcookie.c	1.0	(CSC)	12-May-1992
 *
 * Copyright (c) 1992-1993 Jukka A. Ukkonen
 * All rights reserved.
 *
 * This software is not subject to any license of the following
 *  - American Telephone and Telegraph Company (AT&T),
 *  - UNIX System Laboratories (the owner of UNIX & SysV),
 *  - the Regents of the University of California (the owner of BSD) or
 *  - the Free Software Foundation (the owner of GNU).
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed at Centre for Scientific
 *	Computing by Jukka Ukkonen.
 *
 * 4. None of the names Jukka Antero Ukkonen or Centre for Scientific
 *    Computing may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * 5. The origin of this software must not be misrepresented, either by
 *    explicit claim or by omission.  Since few users ever read sources,
 *    credits must appear in the documentation.
 *
 * 6. Altered versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.  Since few users
 *    ever read sources, credits must appear in the documentation.
 *
 * 7. This notice may not be removed or altered.
 *
 * THIS SOFTWARE IS PROVIDED BY JUKKA ANTERO UKKONEN ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL JUKKA ANTERO UKKONEN BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *  To be used in scripts like startx etc. as follows
 *  -------------------------------------------------
 *	mitcookie | xauth source -
 *
 *  and remember to start your X-server like this
 *	X -auth $HOME/.Xauthority
 *  otherwise it is all in vain.
 *
 *  Compilation:
 *  ------------
 *  First you have to select four (4) instances of a suitable local
 *  combinations of tv_sec, tv_usec, MyPid and Parent (added & multiplied)
 *  to fill in the parameters to the four calls to srandom().
 *
 *  The best results are achieved by linking this with the resolv+
 *  library. If you do not have it, any other resolver routines will
 *  do. If your resolver library includes herror() you can still
 *  define RESOLV_PLUS.
 *
 *  Recommended compilation command:
 *  --------------------------------
 *  gcc -O -DRESOLV_PLUS -o mitcookie mitcookie.c -lresolv+
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/time.h>

main(argc,argv)
int argc;
char *argv[];
{
    char    hostname[256];
    struct hostent  *host,
		    *gethostbyname();
    long    value[4];
    char    *byte = (char *) &value[0];
    struct  timeval tv;
    struct  timezone tz;
    int	    MyPid = getpid (),
	    Parent = getppid ();

    gethostname (hostname, 255);
    hostname[255] = '\0';
    host = gethostbyname (hostname);

    if (!host) {
#if defined(__convex__) || defined(RESOLV_PLUS)
	herror (hostname);
#endif
	exit (1);
    }
    
    gettimeofday (&tv,&tz);

    srandom (/*
	      *	any suitable local combination of
	      *	tv.tv_sec, tv.tv_usec, MyPid & Parent
	      */);
    value[0] = random();
    srandom (/*
	      *	any suitable local combination of
	      *	tv.tv_sec, tv.tv_usec, MyPid & Parent
	      */);
    value[2] = random();

    gettimeofday (&tv,&tz);

    srandom (/*
	      *	any suitable local combination of
	      *	tv.tv_sec, tv.tv_usec, MyPid & Parent
	      */);
    value[1] = random();
    srandom (/*
	      *	any suitable local combination of
	      *	tv.tv_sec, tv.tv_usec, MyPid & Parent
	      */);
    value[3] = random();

    printf ("add %s:0 . %8.8x%8.8x%8.8x%8.8x\n",
	    host->h_name,
	    value[0], value[1], value[2], value[3]);
    printf ("add %s/unix:0 . %8.8x%8.8x%8.8x%8.8x\n",
	    hostname,
	    value[0], value[1], value[2], value[3]);
}
