#include <sys/stat.h>
#include <sys/time.h>
#include <signal.h>
#include <ctype.h>
#include <err.h>
#include <pwd.h>
#include <readpassphrase.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <limits.h>
#include <login_cap.h>
#include <bsd_auth.h>
void bye(int);
void hi(int);
void usage(void);
int no_timeout = 0;
int
main(int argc, char *argv[])
{
char hostname[HOST_NAME_MAX+1], s[BUFSIZ], s1[BUFSIZ], date[256];
char hash[_PASSWORD_LEN];
char *p, *style, *nstyle, *ttynam;
struct itimerval ntimer, otimer;
struct timeval timeout;
int ch, sectimeout, usemine, cnt, tries = 10, backoff = 3;
const char *errstr;
struct passwd *pw;
struct tm *timp;
time_t curtime;
login_cap_t *lc;
sectimeout = 0;
style = NULL;
usemine = 0;
memset(&timeout, 0, sizeof(timeout));
if (pledge("stdio rpath wpath getpw tty proc exec", NULL) == -1)
err(1, "pledge");
if (!(pw = getpwuid(getuid())))
errx(1, "unknown uid %u.", getuid());
lc = login_getclass(pw->pw_class);
if (lc != NULL) {
tries = login_getcapnum(lc, "login-tries", 10, 10);
backoff = login_getcapnum(lc, "login-backoff", 3, 3);
}
while ((ch = getopt(argc, argv, "a:npt:")) != -1) {
switch (ch) {
case 'a':
if (lc) {
style = login_getstyle(lc, optarg, "auth-lock");
if (style == NULL)
errx(1,
"invalid authentication style: %s",
optarg);
}
usemine = 1;
break;
case 't':
sectimeout = strtonum(optarg, 1, INT_MAX, &errstr);
if (errstr)
errx(1, "timeout %s: %s", errstr, optarg);
break;
case 'p':
usemine = 1;
break;
case 'n':
no_timeout = 1;
break;
default:
usage();
}
}
if (sectimeout == 0)
no_timeout = 1;
gethostname(hostname, sizeof(hostname));
if (usemine && lc == NULL)
errx(1, "login class not found");
if (!(ttynam = ttyname(STDIN_FILENO)))
errx(1, "not a terminal?");
curtime = time(NULL);
timp = localtime(&curtime);
strftime(date, sizeof(date), "%c", timp);
if (!usemine) {
if (!readpassphrase("Key: ", s, sizeof(s), RPP_ECHO_OFF) ||
*s == '\0')
exit(0);
readpassphrase("Again: ", s1, sizeof(s1), RPP_ECHO_OFF);
if (strcmp(s1, s)) {
warnx("\apasswords didn't match.");
exit(1);
}
crypt_newhash(s, "bcrypt", hash, sizeof(hash));
explicit_bzero(s, sizeof(s));
explicit_bzero(s1, sizeof(s1));
}
signal(SIGINT, hi);
signal(SIGQUIT, hi);
signal(SIGTSTP, hi);
signal(SIGALRM, bye);
if (!no_timeout) {
timeout.tv_sec = (time_t)sectimeout * 60;
memset(&ntimer, 0, sizeof(ntimer));
ntimer.it_value = timeout;
setitimer(ITIMER_REAL, &ntimer, &otimer);
}
if (no_timeout) {
fprintf(stderr,
"%s: %s on %s. no timeout\ntime now is %s\n",
getprogname(), ttynam, hostname, date);
} else {
fprintf(stderr,
"%s: %s on %s. timeout in %d minutes\ntime now is %s\n",
getprogname(), ttynam, hostname, sectimeout, date);
}
for (cnt = 0;;) {
if (!readpassphrase("Key: ", s, sizeof(s), RPP_ECHO_OFF))
continue;
if (strlen(s) == 0) {
hi(0);
continue;
}
if (usemine) {
if ((strcmp(s, "s/key") == 0 &&
(nstyle = login_getstyle(lc, "skey", "auth-lock")))
|| ((nstyle = style) && strcmp(s, nstyle) == 0))
p = NULL;
else
p = s;
if (auth_userokay(pw->pw_name, nstyle, "auth-lock",
p)) {
explicit_bzero(s, sizeof(s));
break;
}
} else if (crypt_checkpass(s, hash) == 0) {
explicit_bzero(s, sizeof(s));
explicit_bzero(hash, sizeof(hash));
break;
}
putc('\a', stderr);
cnt %= tries;
if (++cnt > backoff) {
sigset_t set, oset;
sigfillset(&set);
sigprocmask(SIG_BLOCK, &set, &oset);
sleep((u_int)((cnt - backoff) * tries / 2));
sigprocmask(SIG_SETMASK, &oset, NULL);
}
}
exit(0);
}
void
hi(int signo)
{
struct itimerval left;
dprintf(STDERR_FILENO, "%s: type in the unlock key.",
getprogname());
if (!no_timeout) {
getitimer(ITIMER_REAL, &left);
dprintf(STDERR_FILENO, " timeout in %lld:%02d minutes",
(long long)(left.it_value.tv_sec / 60),
(int)(left.it_value.tv_sec % 60));
}
dprintf(STDERR_FILENO, "\n");
}
void
bye(int signo)
{
if (!no_timeout)
warnx("timeout");
_exit(1);
}
void
usage(void)
{
fprintf(stderr, "usage: %s [-np] [-a style] [-t timeout]\n",
getprogname());
exit(1);
}