]> git.armaanb.net Git - opendoas.git/blob - libopenbsd/auth_userokay.c
Enable style option only if bsd_auth.h is available
[opendoas.git] / libopenbsd / auth_userokay.c
1 /*
2  * Copyright (c) 2015 Nathan Holstein <nathan.holstein@gmail.com>
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 <sys/types.h>
18 #include <err.h>
19 #include <errno.h>
20 #include <pwd.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <security/pam_appl.h>
26
27 #include "includes.h"
28
29 #define PAM_SERVICE_NAME "doas"
30
31 static char *
32 pam_prompt(const char *msg, int echo_on, int *pam)
33 {
34         char buf[PAM_MAX_RESP_SIZE];
35         int flags = RPP_REQUIRE_TTY | (echo_on ? RPP_ECHO_ON : RPP_ECHO_OFF);
36         char *ret = readpassphrase(msg, buf, sizeof(buf), flags);
37         if (!ret)
38                 *pam = PAM_CONV_ERR;
39         else if (!(ret = strdup(ret)))
40                 *pam = PAM_BUF_ERR;
41         explicit_bzero(buf, sizeof(buf));
42         return ret;
43 }
44
45 static int
46 pam_conv(int nmsgs, const struct pam_message **msgs,
47                 struct pam_response **rsps, __UNUSED void *ptr)
48 {
49         struct pam_response *rsp;
50         int i, style;
51         int pam = PAM_SUCCESS;
52
53         if (!(rsp = calloc(nmsgs, sizeof(struct pam_response))))
54                 errx(1, "couldn't malloc pam_response");
55         *rsps = rsp;
56
57         for (i = 0; i < nmsgs; i++) {
58                 switch (style = msgs[i]->msg_style) {
59                 case PAM_PROMPT_ECHO_OFF:
60                 case PAM_PROMPT_ECHO_ON:
61                         rsp[i].resp = pam_prompt(msgs[i]->msg,
62                                         style == PAM_PROMPT_ECHO_ON, &pam);
63                         break;
64
65                 case PAM_ERROR_MSG:
66                 case PAM_TEXT_INFO:
67                         if (fprintf(style == PAM_ERROR_MSG ? stderr : stdout,
68                                         "%s\n", msgs[i]->msg) < 0)
69                                 pam = PAM_CONV_ERR;
70                         break;
71
72                 default:
73                         errx(1, "invalid PAM msg_style %d", style);
74                 }
75         }
76
77         return PAM_SUCCESS;
78 }
79
80 int
81 auth_userokay(char *name, char *style, char *type, char *password)
82 {
83         static const struct pam_conv conv = {
84                 .conv = pam_conv,
85                 .appdata_ptr = NULL,
86         };
87
88         int ret, auth;
89         pam_handle_t *pamh = NULL;
90
91         if (!name)
92                 return 0;
93         if (style || type || password)
94                 errx(1, "auth_userokay(name, NULL, NULL, NULL)!\n");
95
96         ret = pam_start(PAM_SERVICE_NAME, name, &conv, &pamh);
97         if (ret != PAM_SUCCESS)
98                 errx(1, "pam_start(\"%s\", \"%s\", ?, ?): failed\n",
99                                 PAM_SERVICE_NAME, name);
100
101         auth = pam_authenticate(pamh, 0);
102
103         ret = pam_open_session(pamh, 0);
104         if (ret != PAM_SUCCESS)
105                 errx(1, "pam_open_session(): %s\n", pam_strerror(pamh, ret));
106
107         ret = pam_close_session(pamh, 0);
108         if (ret != PAM_SUCCESS)
109                 errx(1, "pam_close_session(): %s\n", pam_strerror(pamh, ret));
110
111         return auth == PAM_SUCCESS;
112 }