/*
	Authentication plugin for ppp 2.3.10
	Written by John Newbigin
	Copyright 1999 John Newbigin
	Based on the example by Paul Mackerras
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version
	2 of the License, or (at your option) any later version.

	Usage:
	pppd <device> <speed> file <options file> plugin ./plugins/text.so authpass user <user name on remote sustem>
	Type the password in on standard input.
	This is best done by a script which can read the password with echo off and then pipe it into pppd

*/

#include <stddef.h>
#include <time.h>
#include "pppd.h"

int attempt_auth = 0;

char jn_authpass[MAXSECRETLEN] = "";

static int authpass __P((char **));

static option_t my_options[] = {
	{"authpass", o_special_noarg, authpass,
	"read password from stdin"},
	{ NULL }
};

static int my_pap_passwd_hook(char *user, char *passwd2)
{
	// copy the password into passwd

	/*
	  the passwd we are passwd in always seems to be null, so I copy the password
	  to the global passwd and that seems to work.
	*/

	if(attempt_auth)
	{
		info("returning passwd");
		info("user = %p\n", user);
		info("passwd = %p\n", passwd);
		if(user != 0)
		{
			info("user = %s\n", user);
		}
		if(passwd != 0)
		{	
			info("passwd = %s\n", passwd);
			strncpy(passwd, jn_authpass, MAXSECRETLEN);
		}
		return 1; 
	}
	else
	{
		info("no password to return");
		return -1;
	}

}

void plugin_init(void)
{
	info("plugin_init");
	add_options(my_options);

	// hook the auth functions...
	pap_passwd_hook = my_pap_passwd_hook;
}

static int authpass(argv) char **argv;
{
	int i;
	info("authpass");

	fgets(jn_authpass, MAXSECRETLEN, stdin);

	// we have to get rid of the new line in the string....
	for(i = 0; i < MAXSECRETLEN; i++)
	{
		if(jn_authpass[i] == '\n')
		{
			jn_authpass[i] = 0;
			break;
		}
	}
	attempt_auth = 1;
	return 1;
}


