/*
** Copyright 1998 - 1999 Double Precision, Inc.  See COPYING for
** distribution information.
*/

#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<unistd.h>
#include	<fcntl.h>

#include	"auth.h"

static const char rcsid[] = "$Id: bd3106b7bc6d1092b8f262f4b376064b565affac $";

void authchain(int argc, char **argv, const char *buf)
{
int	pipes[2];
pid_t	p;
int	l, n;
char	**vec;
char	*prog;

	vec=authcopyargv(argc, argv, &prog);
	close(3);
	if (!prog || open("/dev/null", O_RDONLY) != 3)	authexit(1);

	if (pipe(pipes))
	{
		perror("pipe");
		authexit(1);
	}
	while ((p=fork()) < 0)
	{
		perror("fork");
		sleep(3);
	}
	close(3);

	if (p)
	{
		dup(pipes[0]);
		close(pipes[0]);
		close(pipes[1]);
		execv(prog, vec);
		perror(prog);
		authexit(1);
	}
	l=strlen(buf);
	close(pipes[0]);
	while (l)
	{
		n=write(pipes[1], buf, l);
		if (n <= 0)	break;
		buf += n;
		l -= n;
	}
	close(pipes[1]);
	authexit(1);
}
