]> git.armaanb.net Git - opendoas.git/blob - shadow.c
timestamp: error out if fstat and lstat st_ino and st_dev are not the same
[opendoas.git] / shadow.c
1 #if HAVE_CRYPT_H
2 #       include <crypt.h>
3 #endif
4 #include <err.h>
5 #include <errno.h>
6 #include <limits.h>
7 #include <pwd.h>
8 #ifdef HAVE_READPASSPHRASE_H
9 #       include <readpassphrase.h>
10 #else
11 #       include "readpassphrase.h"
12 #endif
13 #include <shadow.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <syslog.h>
17 #include <unistd.h>
18
19 #include "openbsd.h"
20
21 void
22 shadowauth(const char *myname, int persist)
23 {
24         const char *hash;
25         char *encrypted;
26         struct passwd *pw;
27         char *challenge, *response, rbuf[1024], cbuf[128];
28
29 #ifdef USE_TIMESTAMP
30         int fd = -1;
31         int valid = 0;
32
33         if (persist)
34                 fd = timestamp_open(&valid, 5 * 60);
35         if (fd != -1 && valid == 1)
36                 goto good;
37 #else
38         (void) persist;
39 #endif
40
41         if ((pw = getpwnam(myname)) == NULL)
42                 err(1, "getpwnam");
43
44         hash = pw->pw_passwd;
45         if (hash[0] == 'x' && hash[1] == '\0') {
46                 struct spwd *sp;
47                 if ((sp = getspnam(myname)) == NULL)
48                         errx(1, "Authorization failed");
49                 hash = sp->sp_pwdp;
50         } else if (hash[0] != '*') {
51                 errx(1, "Authorization failed");
52         }
53
54         char host[HOST_NAME_MAX + 1];
55         if (gethostname(host, sizeof(host)))
56                 snprintf(host, sizeof(host), "?");
57         snprintf(cbuf, sizeof(cbuf),
58                         "\rdoas (%.32s@%.32s) password: ", myname, host);
59         challenge = cbuf;
60
61         response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
62         if (response == NULL && errno == ENOTTY) {
63                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
64                         "tty required for %s", myname);
65                 errx(1, "a tty is required");
66         }
67         if (response == NULL)
68                 err(1, "readpassphrase");
69         if ((encrypted = crypt(response, hash)) == NULL) {
70                 explicit_bzero(rbuf, sizeof(rbuf));
71                 errx(1, "Authorization failed");
72         }
73         explicit_bzero(rbuf, sizeof(rbuf));
74         if (strcmp(encrypted, hash) != 0) {
75                 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
76                 errx(1, "Authorization failed");
77         }
78
79 #ifdef USE_TIMESTAMP
80 good:
81         if (fd != -1) {
82                 timestamp_set(fd, 5 * 60);
83                 close(fd);
84         }
85 #endif
86 }