2 * Copyright (c) 2020 Duncan Overbruck <mail@duncano.de>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 #ifdef HAVE_READPASSPHRASE
27 # include <readpassphrase.h>
29 # include "sys-readpassphrase.h"
41 #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
45 shadowauth(const char *myname, int persist)
50 char *challenge, *response, rbuf[1024], cbuf[128];
57 fd = timestamp_open(&valid, 5 * 60);
58 if (fd != -1 && valid == 1)
64 if ((pw = getpwnam(myname)) == NULL)
68 if (hash[0] == 'x' && hash[1] == '\0') {
70 if ((sp = getspnam(myname)) == NULL)
71 errx(1, "Authentication failed");
73 } else if (hash[0] != '*') {
74 errx(1, "Authentication failed");
77 char host[HOST_NAME_MAX + 1];
78 if (gethostname(host, sizeof(host)))
79 snprintf(host, sizeof(host), "?");
80 snprintf(cbuf, sizeof(cbuf),
81 "\rdoas (%.32s@%.32s) password: ", myname, host);
84 response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
85 if (response == NULL && errno == ENOTTY) {
86 syslog(LOG_AUTHPRIV | LOG_NOTICE,
87 "tty required for %s", myname);
88 errx(1, "a tty is required");
91 err(1, "readpassphrase");
92 if ((encrypted = crypt(response, hash)) == NULL) {
93 explicit_bzero(rbuf, sizeof(rbuf));
94 errx(1, "Authentication failed");
96 explicit_bzero(rbuf, sizeof(rbuf));
97 if (strcmp(encrypted, hash) != 0) {
98 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
99 errx(1, "Authentication failed");
105 timestamp_set(fd, 5 * 60);