[net.sources] randpasswd for non-4.2BSD sites

charles@utastro.UUCP (Charles Sandel) (08/01/84)

Following is a "shar" archive file which contains the makings
for "randpasswd" for non-4.2BSD systems.  It uses rand(3) and srand(3)
instead of random(3), setstate(3) and initstate(3).
Enjoy, y'all!
--------------------------(cut here)----------------------------
: This is a shar archive.  Extract with sh, not csh.
echo x - Makefile
cat > Makefile << '!Funky!Stuff!'
DEST	      = /usr/local/randpasswd
HDRS	      = defs.h
LDFLAGS	      = -s
CFLAGS	      = -O
LIBS	      =
LINKER	      = cc
MAKEFILE      = Makefile
OBJS	      = randpasswd.o
PRINT	      = pr
PROGRAM	      = randpasswd
SRCS	      = randpasswd.c

all:		$(PROGRAM)

$(PROGRAM):     $(OBJS) $(LIBS)
		@echo -n "Loading $(PROGRAM) ... "
		@$(LINKER) $(LDFLAGS) $(OBJS) $(LIBS) -o $(PROGRAM)
		@echo "done"

clean:;		@rm -f $(OBJS)

depend:;	@mkmf -f $(MAKEFILE) PROGRAM=$(PROGRAM) DEST=$(DEST)

index:;		@ctags -wx $(HDRS) $(SRCS)

install:	$(PROGRAM)
		@echo Installing $(PROGRAM) in $(DEST)
		@install -s $(PROGRAM) $(DEST)

print:;		@$(PRINT) $(HDRS) $(SRCS)

program:        $(PROGRAM)

tags:           $(HDRS) $(SRCS); @ctags $(HDRS) $(SRCS)

update:		$(DEST)/$(PROGRAM)

$(DEST)/$(PROGRAM): $(SRCS) $(LIBS) $(HDRS) $(EXTHDRS)
		@make -f $(MAKEFILE) DEST=$(DEST) install
###
randpasswd.o: /usr/include/sys/time.h defs.h
!Funky!Stuff!
echo x - defs.h
cat > defs.h << '!Funky!Stuff!'
#define	MAXLENGTH	16	/* maximum length of passwords */
#define	MINLENGTH	6	/* minimum length of passwords */
#define	MAXDESLEN	10	/* maximum desired length of passwords */

/* defined indexes into the randrange array of structures */
#define	ANY		-1	/* produce any random value from any range */
#define	NUMERIC		0	/* index of the numeric range */
#define	LALPHA		1	/* index of the lower alpha range */
#define	UALPHA		2	/* index of the upper alpha range */
/* extra ranges to cover the entire 7-bit ASCII range */
/*#define	CONTROL		3	/* index of the range NUL through US */
/*#define	PUNCT1		4	/* index of the range SP through /  */
/*#define	PUNCT2		5	/* index of the range : through @  */
/*#define	PUNCT3		6	/* index of the range [ through `  */
/*#define	PUNCT4		7	/* index of the range { through DEL  */

#define	NRANGE		3	/* number of ranges used */


struct	randrange	{
		unsigned	seed;
		int	width;
		char	offset;
		};

/* define what characters may follow any character */
struct	after	{
	char	ch;
	char	*follow;
	};

struct	randrange	start, range[NRANGE] =
/* 	seed		width		offset */
{
	0,		10,		'0',
	0,		26,		'a',
	0,		26,		'A'
/* other ASCII ranges may be useful; uncomment the ones needed
,
	0,		32,		'\0',
	0,		16,		' ',
	0,		7,		':',
	0,		6,		'\0134',
	0,		5,		'{'
*/
};

/* ch		follow */
struct	after	aft[] = {
'a',	"abcdefghijklmnopqrstuvwxyz",
'b',	"abdeilorstuwy",
'c',	"aehikorstuwy",
'd',	"adeioruy",
'e',	"abcdefghijklmnopqrstuvwxyz",
'f',	"aefiloruy",
'g',	"aeghilnopruwy",
'h',	"aefimnorsy",
'i',	"abcdefghijklmnopqrstuvwxyz",
'j',	"aeghijklouy",
'k',	"acehijklorstuwxy",
'l',	"aeiklmnoptuvwy",
'm',	"abegiklmnopqrstuwxyz",
'n',	"adegiknorstuxyz",
'o',	"abcdefghijklmnopqrstuvwxyz",
'p',	"aehilopqrstuwyz",
'q',	"aeioruy",
'r',	"acdefgiklmnopqrstuvxyz",
's',	"acehiklmnopstuvwxyz",
't',	"aehiortuyz",
'u',	"abcdefghijklmnopqrstuvwxyz",
'v',	"aeiouvwy",
'w',	"aefghiklorsuvwy",
'x',	"aeilouxyz",
'y',	"acdeiklopqsuvwxyz",
'z',	"acehilopqrstuwyz"
};
!Funky!Stuff!
echo x - randpasswd.1l
cat > randpasswd.1l << '!Funky!Stuff!'
.TH RANDPASSWD 1L "24 July 1984"
.UC 4
.SH NAME
randpasswd \- generate a random password
.SH SYNOPSIS
.B randpasswd
[
.B \-w
]
[
.B length
]
.SH DESCRIPTION
This command generates a random password
which can be used as a login password, or as
an encryption key.
.PP
There are two optional arguments:

.TP
.B \-w
If included, an attempt will be made to generate a password which, though
nonsensical, might sound like a "real word".
The resulting password will be in all lower-case ASCII characters.
If omitted, the resulting password my contain lower- or upper-case
ASCII characters, or ASCII numerics.
.TP
.B length
"Length" is a number indicating the desired length
of the resulting password.  "Length" may be between
6 and 16.  Larger or smaller requests are rounded
up or down to a legal value.  If this argument is
omitted, the resulting length will be a random value
between 6 and 10.
.PP
For most uses, you should probably get
.B randpasswd
to generate passwords until you find one you like.  Try the
following from csh:
.ti +5
while (1)
.ti +10
randpasswd -w
.ti +5
end

or this from sh:
.ti +5
while (true)
.ti +5
do
.ti +10
randpasswd -w
.ti +5
done
.PP
.SH "SEE ALSO"
login(1), passwd(5), crypt(3)
.br
Robert Morris and Ken Thompson,
.I UNIX password security
.SH BUGS
The attempt to generate a "real word" is simplistic, and
makes no attempt at simulating the rules of English grammar.
It is, however, effective if you try enough times.
.SH DIAGNOSTICS
None.  Silence is Golden.
.SH AUTHOR
Charles Sandel
.br
University of Texas
.br
Department of Astronomy
.br
Austin, Texas  78712
!Funky!Stuff!
echo x - randpasswd.c
cat > randpasswd.c << '!Funky!Stuff!'
/*
	randpasswd - generate a random password
	UNIX System V version

	Calling syntax:

	randpasswd [-w] [length]

		where "length" is an optional numeric argument 
		specifying the length of the password needed.
		If "length" is lesser or greater than MINLENGTH or
		MAXLENGTH, it is rounded up or down to a legal value.
		Passwords of length between MINLENGTH and MAXLENGTH
		are therefore
		possible, but if no argument is provided, a password of
		random length between MINLENGTH and MAXDESLEN is produced.
		If the argument "-w" is specified, "randpasswd" will attempt
		to generate a "real-sounding" English word, in lower-case
		alpha, sans numerics.

	Compilation:  cc -O -s randpasswd.c -o randpasswd

	Author: Charles Sandel
		University of Texas
		Department of Astronomy
		Austin, Texas  78712
		July 24, 1984

		uucp:  utastro!charles
		arpa:  charles@utastro.UTEXAS.ARPA

*/

#include	"defs.h"

char	passwd[MAXLENGTH+1];

main(argc, argv)
char	*argv[];
{
	int	i, length, word=0;
	long	t, time();

	/* initialize generic state */
	srand(getpid());
	start.seed = rand();

	length = -1;

	for(i=1; i<argc && i<3; i++)
	{
		if(strcmp("-w", argv[i]) == 0)
			word++;
		else
		{
			length = atoi(argv[i]);
			if(length < MINLENGTH)
				length = MINLENGTH;
			if(length > MAXLENGTH)
				length = MAXLENGTH;
		}
	}

	if(length == -1)
		length = rand()%(MAXDESLEN - MINLENGTH + 1) + MINLENGTH;

	/* initalize range states & seeds */
	for(i=0; i<NRANGE; i++)
	{
		if((t = time((long *)0)) == -1) {
			perror("time:");
			exit(-1);
			}
		range[i].seed = t + rand();
	}

	/* make a password */
	if(word != 0)
	{
		passwd[0] = randchar(LALPHA);
		srand(range[LALPHA].seed);
		for(i=1; i<length; i++)
			passwd[i] = wordchar(passwd[i-1]);
	}
	else
		for(i=0; i<length; i++)
			passwd[i] = randchar(ANY);

	passwd[++i] = '\0';

	printf("%s\n", passwd);
}

randchar(inx)
{
	long	r;
	char	c;

	if(inx == ANY || inx < 0 || inx > NRANGE-1)
	{
		/* set generic state and get a random index into the array */
		srand(start.seed);
		r = rand()%NRANGE;
	}
	else
		r = inx;

	/* set state for that range and get a random character from it */
	srand(range[r].seed);
	c = rand() % range[r].width + range[r].offset;

	return(c);
}

wordchar(pc)
char	pc;
{
	int	len, i, r;
	char	c;

	i = pc - 'a';
	len = strlen(aft[i].follow);
	r = rand()%len;

	c = aft[i].follow[r];

	return(c);
	}
!Funky!Stuff!
-- 
                     *>> Charles Sandel <<*
      uucp:  {ut-sally, ut-ngp, noao, charm}!utastro!charles
            arpa:  chaz@ut-ngp  or  charles@ut-sally
                      at&t:  (512) 471-4461 x439