]> git.armaanb.net Git - opendoas.git/blob - shadow.c
Handle empty argv
[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
27 #       include <readpassphrase.h>
28 #else
29 #       include "sys-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 #include "doas.h"
39
40 #ifndef HOST_NAME_MAX
41 #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
42 #endif
43
44 int
45 shadowauth(const char *myname, int persist)
46 {
47         const char *hash;
48         char *encrypted;
49         struct passwd *pw;
50         char *challenge, *response, rbuf[1024], cbuf[128];
51
52 #ifdef USE_TIMESTAMP
53         int fd = -1;
54         int valid = 0;
55
56         if (persist)
57                 fd = timestamp_open(&valid, 5 * 60);
58         if (fd != -1 && valid == 1)
59                 goto good;
60 #else
61         (void) persist;
62 #endif
63
64         if ((pw = getpwnam(myname)) == NULL)
65                 err(1, "getpwnam");
66
67         hash = pw->pw_passwd;
68         if (hash[0] == 'x' && hash[1] == '\0') {
69                 struct spwd *sp;
70                 if ((sp = getspnam(myname)) == NULL) {
71                         return(5);
72                 }
73                 hash = sp->sp_pwdp;
74         } else if (hash[0] != '*') {
75                 return(5);
76         }
77
78         char host[HOST_NAME_MAX + 1];
79         if (gethostname(host, sizeof(host)))
80                 snprintf(host, sizeof(host), "?");
81         snprintf(cbuf, sizeof(cbuf),
82                         "\rdoas (%.32s@%.32s) password: ", myname, host);
83         challenge = cbuf;
84
85         response = readpassphrase(challenge, rbuf, sizeof(rbuf), RPP_REQUIRE_TTY);
86         if (response == NULL && errno == ENOTTY) {
87                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
88                         "tty required for %s", myname);
89                 errx(1, "a tty is required");
90         }
91         if (response == NULL)
92                 err(1, "readpassphrase");
93         if ((encrypted = crypt(response, hash)) == NULL) {
94                 explicit_bzero(rbuf, sizeof(rbuf));
95                 printf(getinsult());
96                 return(5);
97         }
98         explicit_bzero(rbuf, sizeof(rbuf));
99         if (strcmp(encrypted, hash) != 0) {
100                 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
101                 return(5);
102         }
103
104 #ifdef USE_TIMESTAMP
105 good:
106         if (fd != -1) {
107                 timestamp_set(fd, 5 * 60);
108                 close(fd);
109         }
110 #endif
111         return(0);
112 }