/*====================================================================*
 *
 *   mac2pw.c - Atheros device password generator;
 *
 *   generate unique password strings for a range of device hardware
 *   addresses; print paired addresses and passwords to stdout;
 *
 *   Many Atheros programs expect the user to enter a password to
 *   access a device; the password is encoded to product the 16-bit
 *   Device Access Key (DAK) stored in the PIB; 
 *
 *   Vendors must publish the device password so that end users can 
 *   reproduce the same 16-byte hexadecimal value later; a password
 *   is more user-friendly than a 16-byte hexadecimal value;
 *
 *   given a range of MAC address, this program will produce unique
 *   passwords so vendors can program devices and print labels that 
 *   ship devices;
 *
 *   This software and documentation is the property of Atheros 
 *   Corporation, Ocala, Florida. It is provided 'as is' without 
 *   expressed or implied warranty of any kind to anyone for any 
 *   reason. Atheros assumes no responsibility or liability for 
 *   errors or omissions in the software or documentation and 
 *   reserves the right to make changes without notification. 
 *   
 *   Atheros customers may modify and distribute the software 
 *   without obligation to Atheros. Since use of this software 
 *   is optional, users shall bear sole responsibility and 
 *   liability for any consequences of it's use. 
 *
 *.  Atheros Powerline Toolkit for HomePlug AV;
 *:  Published 2006-2008 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *   Contributor(s):
 *	Charles Maier <charles.maier@atheros.com>
 *
 *--------------------------------------------------------------------*/

#define _GETOPT_H

/*====================================================================*
 *   system header files;
 *--------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <errno.h>

/*====================================================================*
 *   custom header files;
 *--------------------------------------------------------------------*/

#include "../tools/getoptv.h"
#include "../tools/putoptv.h"
#include "../tools/memory.h"
#include "../tools/number.h"
#include "../tools/types.h"
#include "../tools/flags.h"
#include "../tools/error.h"
#include "../crypt/keys.h"

/*====================================================================*
 *   custom source files;
 *--------------------------------------------------------------------*/

#ifndef MAKEFILE
#include "../tools/getoptv.c"
#include "../tools/putoptv.c"
#include "../tools/version.c"
#include "../tools/todigit.c"
#include "../tools/uintspec.c"
#include "../tools/memincr.c"
#include "../tools/error.c"
#endif

#ifndef MAKEFILE
#include "../crypt/MACPasswords.c"
#endif

/*====================================================================*
 *   program constants; 
 *--------------------------------------------------------------------*/

#ifndef ETHER_ADDR_LEN
#define ETHER_ADDR_LEN 6 /* normally defined in ethernet.h or if_ether.h */
#endif

/*====================================================================*
 *
 *   void function (const char * string, const char * secret, uint32_t range, unsigned count, unsigned bunch, unsigned space, flag_t flags) 
 * 
 *   parse an Ethernet hardware address string into vendor and device
 *   ID substrings; print a specified number of consecutive addresses
 *   and password strings having a defined letter count and bunching;
 *
 *.  Atheros Powerline Toolkit for HomePlug AV;
 *:  Published 2006-2008 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *   Contributor(s):
 *	Charles Maier <charles.maier@atheros.com>
 *
 *--------------------------------------------------------------------*/

static void function (const char * string, const char * secret, uint32_t range, unsigned count, unsigned bunch, unsigned space, flag_t flags) 

{
	const char * offset = string;
	uint32_t vendor = 0;
	uint32_t device = 0;
	unsigned radix = 0x10;
	unsigned width;
	unsigned digit;
	for (width = 0; width < ETHER_ADDR_LEN; width++) 
	{
		if ((digit = todigit (*offset)) < radix) 
		{
			vendor *= radix;
			vendor += digit;
			offset++;
			continue;
		}
		error (1, EINVAL, "Bad MAC Address: %s", string);
	}
	if (!vendor) 
	{
		error (1, EPERM, "Vendor ID can't be zero");
	}
	for (width = 0; width < ETHER_ADDR_LEN; width++) 
	{
		if ((digit = todigit (*offset)) < radix) 
		{
			device *= radix;
			device += digit;
			offset++;
			continue;
		}
		error (1, EINVAL, "Bad MAC Address: %s", string);
	}
	if (!device) 
	{
		error (1, EPERM, "Device ID can't be zero");
	}
	if (*offset) 
	{
		error (1, EINVAL, "Bad MAC address: %s", string);
	}
	if (range > (DEVICE_MAX - device)) 
	{
		error (1, ERANGE, "Want %d passwords but only %d left in range", range, (DEVICE_MAX - device));
	}
	MACPasswords (vendor, device, range, count, bunch, space, flags);
	return;
}

/*====================================================================*
 *   
 *   int main (int argc, const char * argv []);
 *
 *.  Atheros Powerline Toolkit for HomePlug AV;
 *:  Published 2006-2008 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *   Contributor(s):
 *	Charles Maier <charles.maier@atheros.com>
 *
 *--------------------------------------------------------------------*/

int main (int argc, const char * argv []) 

{
	static const char *optv [] = 
	{
		"b:l:n:qs:uv",
		"address [address] [...]",
		"Atheros device password generator",
		"b n\tbunching factor [4]",
		"l n\tpassword letters [16]",
		"n n\tgenerate n consecutive passwords [1]",
		"q\tomit device address on output",
		"v\tprepend PTS flag on output",
		(const char *)(0) 
	};
	const char * secret = (const char *)(0);
	uint32_t range = 1;
	unsigned count = 16;
	unsigned bunch = 4;
	unsigned space = '-';
	flag_t flags = (flag_t)(0);
	signed c;
	optind = 1;
	opterr = 1;
	while ((c = getoptv (argc, argv, optv)) != -1) 
	{
		switch ((char)(c)) 
		{
		case 'b':
			bunch = (unsigned)(uintspec (optarg, 0, UCHAR_MAX));
			break;
		case 'l':
			count = (unsigned)(uintspec (optarg, 12, 64));
			break;
		case 'n':
			range = (uint32_t)(uintspec (optarg, 0, DEVICE_MAX));
			break;
		case 'q':
			_setbits (flags, PASSWORD_SILENCE);
			break;
		case 's':
			secret = optarg;
			break;
		case 'u':
		case 'v':
			_setbits (flags, PASSWORD_VERBOSE);
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;
	while ((argv) && (*argv)) 
	{
		function (*argv, secret, range, count, bunch, space, flags);
		argc--;
		argv++;
	}
	return (0);
}