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