]> git.armaanb.net Git - opendoas.git/blob - shadow.c
use config.h and link objects instead of libopenbsd.a
[opendoas.git] / shadow.c
1 /*
2  * Copyright (c) 2020 Duncan Overbruck <mail@duncano.de>
3  *
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.
7  *
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.
15  */
16
17 #include "config.h"
18
19 #if HAVE_CRYPT_H
20 #       include <crypt.h>
21 #endif
22 #include <err.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <pwd.h>
26 #ifdef HAVE_READPASSPHRASE_H
27 #       include <readpassphrase.h>
28 #else
29 #       include "readpassphrase.h"
30 #endif
31 #include <shadow.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <syslog.h>
35 #include <unistd.h>
36
37 #include "openbsd.h"
38
39 #ifndef HOST_NAME_MAX
40 #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
41 #endif
42
43 void
44 shadowauth(const char *myname, int persist)
45 {
46         const char *hash;
47         char *encrypted;
48         struct passwd *pw;
49         char *challenge, *response, rbuf[1024], cbuf[128];
50
51 #ifdef USE_TIMESTAMP
52         int fd = -1;
53         int valid = 0;
54
55         if (persist)
56                 fd = timestamp_open(&valid, 5 * 60);
57         if (fd != -1 && valid == 1)
58                 goto good;
59 #else
60         (void) persist;
61 #endif
62
63         if ((pw = getpwnam(myname)) == NULL)
64                 err(1, "getpwnam");
65
66         hash = pw->pw_passwd;
67         if (hash[0] == 'x' && hash[1] == '\0') {
68                 struct spwd *sp;
69                 if ((sp = getspnam(myname)) == NULL)
70                         errx(1, "Authorization failed");
71                 hash = sp->sp_pwdp;
72         } else if (hash[0] != '*') {
73                 errx(1, "Authorization failed");
74         }
75
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);
81         challenge = cbuf;
82
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");
88         }
89         if (response == NULL)
90                 err(1, "readpassphrase");
91         if ((encrypted = crypt(response, hash)) == NULL) {
92                 explicit_bzero(rbuf, sizeof(rbuf));
93                 errx(1, "Authorization failed");
94         }
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");
99         }
100
101 #ifdef USE_TIMESTAMP
102 good:
103         if (fd != -1) {
104                 timestamp_set(fd, 5 * 60);
105                 close(fd);
106         }
107 #endif
108 }