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"
40 #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
44 shadowauth(const char *myname, int persist)
49 char *challenge, *response, rbuf[1024], cbuf[128];
56 fd = timestamp_open(&valid, 5 * 60);
57 if (fd != -1 && valid == 1)
63 if ((pw = getpwnam(myname)) == NULL)
67 if (hash[0] == 'x' && hash[1] == '\0') {
69 if ((sp = getspnam(myname)) == NULL)
70 errx(1, "Authorization failed");
72 } else if (hash[0] != '*') {
73 errx(1, "Authorization failed");
76 char host[HOST_NAME_MAX + 1];
77 if (gethostname(host, sizeof(host)))
78 snprintf(host, sizeof(host), "?");
79 snprintf(cbuf, sizeof(cbuf),
80 "\rdoas (%.32s@%.32s) password: ", myname, host);
83 response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
84 if (response == NULL && errno == ENOTTY) {
85 syslog(LOG_AUTHPRIV | LOG_NOTICE,
86 "tty required for %s", myname);
87 errx(1, "a tty is required");
90 err(1, "readpassphrase");
91 if ((encrypted = crypt(response, hash)) == NULL) {
92 explicit_bzero(rbuf, sizeof(rbuf));
93 errx(1, "Authorization failed");
95 explicit_bzero(rbuf, sizeof(rbuf));
96 if (strcmp(encrypted, hash) != 0) {
97 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
98 errx(1, "Authorization failed");
104 timestamp_set(fd, 5 * 60);