lib/courier-authlib/chain.c
author "Tomas Zeman <tzeman@volny.cz>"
Wed, 16 Jan 2008 22:39:43 +0100
changeset 0 6f7a81934006
child 2 b3afb9f1e801
permissions -rw-r--r--
Imported vmailmgr-0.96.9

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

#if	HAVE_CONFIG_H
#include	"config.h"
#endif
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif
#if	HAVE_FCNTL_H
#include	<fcntl.h>
#endif

#include	"auth.h"

static const char rcsid[]="$Id: chain.c,v 1.1 2000/04/13 17:55:05 bruce Exp $";

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);
	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(3);
	close(pipes[0]);
	while (l)
	{
		n=write(pipes[1], buf, l);
		if (n <= 0)	break;
		buf += n;
		l -= n;
	}
	close(pipes[1]);
	authexit(1);
}