1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
/*
** Copyright 1998 - 1999 Double Precision, Inc. See COPYING for
** distribution information.
*/
#include "auth.h"
#include "authmod.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <signal.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <time.h>
#include "authwait.h"
static const char rcsid[]="$Id: authmoduser2.c,v 1.7 2000/05/02 04:31:49 mrsam Exp $";
int authmoduser(int argc, char **argv, unsigned timeout, unsigned errsleep)
{
const char *p;
int rc;
char namebuf[60];
int n;
time_t t, expinterval;
char *q, *r;
int waitstat;
signal(SIGCHLD, SIG_DFL);
while (wait(&waitstat) >= 0)
;
p=getenv("AUTHUSER");
if (!p && argc && argv[0][0] == '/')
/* Set AUTHUSER from argv[0] */
{
q=malloc(sizeof("AUTHUSER=")+strlen(argv[0]));
if (!q)
{
perror("malloc");
authexit(1);
}
strcat(strcpy(q, "AUTHUSER="), argv[0]);
putenv(q);
}
else if (!p || *p != '/')
{
write(2, argv[0], strlen(argv[0]));
write(2, ": AUTHUSER is not initialized to a complete path\n",
49);
authexit(1);
}
putenv("AUTHENTICATED=");
if (argc < 2)
{
write(2, "AUTHFAILURE\n", 12);
authexit(1);
}
p=getenv("AUTHARGC");
rc= p && *p && *p != '0' ? 0:1;
sprintf(namebuf, "AUTHARGC=%d", argc);
r=strdup(namebuf);
if (!r)
{
perror("strdup");
authexit(1);
}
putenv(r);
for (n=0; n<argc; n++)
{
char *p;
sprintf(namebuf, "AUTHARGV%d=", n);
p=malloc(strlen(namebuf)+1+strlen(argv[n]));
if (!p)
{
perror("malloc");
authexit(1);
}
strcat(strcpy(p, namebuf), argv[n]);
putenv(p);
}
if (rc == 0 && errsleep) sleep(errsleep);
time(&t);
p=getenv("AUTHEXPIRE");
if (p && isdigit((int)(unsigned char)*p))
{
expinterval=0;
do
{
expinterval=expinterval * 10 + (*p++ - '0');
} while ( isdigit((int)(unsigned char)*p) );
}
else
{
time_t n;
expinterval=t + timeout;
q=namebuf+sizeof(namebuf)-1;
*q=0;
n=expinterval;
do
{
*--q = '0' + ( n % 10 );
} while ( (n = n/10) != 0);
q -= sizeof("AUTHEXPIRE=")-1;
memcpy(q, "AUTHEXPIRE=", sizeof("AUTHEXPIRE=")-1);
q=strdup(q);
if (!q)
{
perror("strdup");
authexit(1);
}
putenv(q);
}
if (timeout)
{
if (expinterval <= t) authexit(1);
alarm(expinterval - t);
}
return (rc);
}
|