lib/courier-authlib/authmod.c
changeset 0 6f7a81934006
child 2 b3afb9f1e801
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 /*
       
     2 ** Copyright 1998 - 1999 Double Precision, Inc.  See COPYING for
       
     3 ** distribution information.
       
     4 */
       
     5 
       
     6 #include	"auth.h"
       
     7 #include	"authmod.h"
       
     8 #include	"authwait.h"
       
     9 #include	<stdio.h>
       
    10 #include	<string.h>
       
    11 #include	<stdlib.h>
       
    12 #include	<signal.h>
       
    13 #if	HAVE_UNISTD_H
       
    14 #include	<unistd.h>
       
    15 #endif
       
    16 
       
    17 static const char rcsid[]="$Id: authmod.c,v 1.1 2000/04/13 17:55:05 bruce Exp $";
       
    18 
       
    19 static char	authrec[BUFSIZ];
       
    20 static char	buf[BUFSIZ];
       
    21 
       
    22 void	authmod_init(
       
    23 		int argc,
       
    24 		char **argv,
       
    25 		const char **service,
       
    26 		const char **authtype,
       
    27 		char **authdata)
       
    28 {
       
    29 FILE	*fpin;
       
    30 int	waitstat;
       
    31 const char *a=getenv("AUTHENTICATED");
       
    32 char	*p;
       
    33 int	n;
       
    34 
       
    35 	if (a && *a)	/* Already a good guy */
       
    36 		authmod_success(argc, argv, a);
       
    37 
       
    38 	n=0;
       
    39 	fpin=fdopen(3, "r");
       
    40 	if (fpin)
       
    41 	{
       
    42 	int	c;
       
    43 
       
    44 		while ((c=getc(fpin)) != EOF)
       
    45 			if (n < sizeof(buf)-1)
       
    46 				buf[n++]=c;
       
    47 		buf[n]=0;
       
    48 	}
       
    49 
       
    50 	if (n == 0)
       
    51 	{
       
    52 		write(2, "AUTHFAILURE\n", 12);
       
    53 		authexit(1);
       
    54 	}
       
    55 
       
    56 	fclose(fpin);
       
    57 	close(3);	/* Insurance */
       
    58 	strcpy(authrec, buf);
       
    59 
       
    60 	signal(SIGCHLD, SIG_DFL);
       
    61 
       
    62 	while (wait(&waitstat) >= 0)
       
    63 		;
       
    64 
       
    65 	p=buf;
       
    66 	*service=p;
       
    67 	while (*p && *p != '\n')
       
    68 		++p;
       
    69 	if (*p)	*p++=0;
       
    70 	*authtype=p;
       
    71 	while (*p && *p != '\n')
       
    72 		++p;
       
    73 	if (*p)	*p++=0;
       
    74 	*authdata=p;
       
    75 }
       
    76 
       
    77 void authmod_success(int argc, char **argv, const char *a)
       
    78 {
       
    79 char	**vec, *prog;
       
    80 char	*b;
       
    81 
       
    82 	vec=authcopyargv(argc-1, argv+1, &prog);
       
    83 	if (!prog)	authexit(1);
       
    84 
       
    85 	b=malloc(sizeof("AUTHENTICATED=")+strlen(a));
       
    86 	if (!b)
       
    87 	{
       
    88 		perror("malloc");
       
    89 		authexit(1);
       
    90 	}
       
    91 	strcat(strcpy(b, "AUTHENTICATED="), a);
       
    92 	putenv(b);
       
    93 	execv(prog, vec);
       
    94 	perror("exec");
       
    95 	authexit(1);
       
    96 }
       
    97 
       
    98 void authmod_fail(int argc, char **argv)
       
    99 {
       
   100 	authchain(argc-1, argv+1, authrec);	/* Next module */
       
   101 }