2 * Copyright (c) 2015 Nathan Holstein <nathan.holstein@gmail.com>
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.
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.
17 #include <sys/types.h>
23 #ifdef HAVE_READPASSPHRASE_H
24 # include <readpassphrase.h>
26 # include "readpassphrase.h"
35 #include <security/pam_appl.h>
39 #define PAM_SERVICE_NAME "doas"
41 static pam_handle_t *pamh = NULL;
42 static char doas_prompt[128];
43 static sig_atomic_t volatile caught_signal = 0;
44 static int session_opened = 0;
45 static int cred_established = 0;
54 pamprompt(const char *msg, int echo_on, int *ret)
57 char *pass, buf[PAM_MAX_RESP_SIZE];
58 int flags = RPP_REQUIRE_TTY | (echo_on ? RPP_ECHO_ON : RPP_ECHO_OFF);
60 /* overwrite default prompt if it matches "Password:[ ]" */
61 if (strncmp(msg,"Password:", 9) == 0 &&
62 (msg[9] == '\0' || (msg[9] == ' ' && msg[10] == '\0')))
67 pass = readpassphrase(prompt, buf, sizeof(buf), flags);
70 else if (!(pass = strdup(pass)))
75 explicit_bzero(buf, sizeof(buf));
80 pamconv(int nmsgs, const struct pam_message **msgs,
81 struct pam_response **rsps, __UNUSED void *ptr)
83 struct pam_response *rsp;
87 if (!(rsp = calloc(nmsgs, sizeof(struct pam_response))))
88 errx(1, "couldn't malloc pam_response");
90 for (i = 0; i < nmsgs; i++) {
91 switch (style = msgs[i]->msg_style) {
92 case PAM_PROMPT_ECHO_OFF:
93 case PAM_PROMPT_ECHO_ON:
94 rsp[i].resp = pamprompt(msgs[i]->msg, style == PAM_PROMPT_ECHO_ON, &ret);
95 if (ret != PAM_SUCCESS)
101 if (fprintf(style == PAM_ERROR_MSG ? stderr : stdout,
102 "%s\n", msgs[i]->msg) < 0)
107 errx(1, "invalid PAM msg_style %d", style);
117 /* overwrite and free response buffers */
118 for (i = 0; i < nmsgs; i++) {
119 if (rsp[i].resp == NULL)
121 switch (style = msgs[i]->msg_style) {
122 case PAM_PROMPT_ECHO_OFF:
123 case PAM_PROMPT_ECHO_ON:
124 explicit_bzero(rsp[i].resp, strlen(rsp[i].resp));
136 if (session_opened != 0) {
137 ret = pam_close_session(pamh, 0);
138 if (ret != PAM_SUCCESS)
139 errx(1, "pam_close_session: %s", pam_strerror(pamh, ret));
141 if (cred_established != 0) {
142 ret = pam_setcred(pamh, PAM_DELETE_CRED | PAM_SILENT);
143 if (ret != PAM_SUCCESS)
144 warn("pam_setcred(?, PAM_DELETE_CRED | PAM_SILENT): %s",
145 pam_strerror(pamh, ret));
151 watchsession(pid_t child)
154 struct sigaction act, oldact;
159 if (sigprocmask(SIG_BLOCK, &sigs, NULL)) {
160 warn("failed to block signals");
165 /* setup signal handler */
166 act.sa_handler = catchsig;
167 sigemptyset(&act.sa_mask);
170 /* unblock SIGTERM and SIGALRM to catch them */
172 if (sigaddset(&sigs, SIGTERM) ||
173 sigaddset(&sigs, SIGALRM) ||
174 sigaddset(&sigs, SIGTSTP) ||
175 sigaction(SIGTERM, &act, &oldact) ||
176 sigprocmask(SIG_UNBLOCK, &sigs, NULL)) {
177 warn("failed to set signal handler");
182 /* wait for child to be terminated */
183 if (waitpid(child, &status, 0) != -1) {
184 if (WIFSIGNALED(status)) {
185 fprintf(stderr, "%s%s\n", strsignal(WTERMSIG(status)),
186 WCOREDUMP(status) ? " (core dumped)" : "");
187 status = WTERMSIG(status) + 128;
189 status = WEXITSTATUS(status);
191 else if (caught_signal)
192 status = caught_signal + 128;
197 if (caught_signal && child != (pid_t)-1) {
198 fprintf(stderr, "\nSession terminated, killing shell\n");
199 kill(child, SIGTERM);
202 pamcleanup(PAM_SUCCESS);
205 if (child != (pid_t)-1) {
208 kill(child, SIGKILL);
209 fprintf(stderr, " ...killed.\n");
212 /* unblock cached signal and resend */
213 sigaction(SIGTERM, &oldact, NULL);
214 if (caught_signal != SIGTERM)
215 caught_signal = SIGKILL;
216 kill(getpid(), caught_signal);
223 pamauth(const char *user, const char* ruser, int interactive, int nopass)
225 static const struct pam_conv conv = {
236 ret = pam_start(PAM_SERVICE_NAME, ruser, &conv, &pamh);
237 if (ret != PAM_SUCCESS)
238 errx(1, "pam_start(\"%s\", \"%s\", ?, ?): failed",
239 PAM_SERVICE_NAME, ruser);
241 ret = pam_set_item(pamh, PAM_RUSER, ruser);
242 if (ret != PAM_SUCCESS)
243 warn("pam_set_item(?, PAM_RUSER, \"%s\"): %s",
244 pam_strerror(pamh, ret), ruser);
246 if (isatty(0) && (ttydev = ttyname(0)) != NULL) {
247 if (strncmp(ttydev, "/dev/", 5) == 0)
250 ret = pam_set_item(pamh, PAM_TTY, ttydev);
251 if (ret != PAM_SUCCESS)
252 warn("pam_set_item(?, PAM_TTY, \"%s\"): %s",
253 ttydev, pam_strerror(pamh, ret));
258 errx(1, "Authorization required");
260 /* doas style prompt for pam */
261 char host[HOST_NAME_MAX + 1];
262 if (gethostname(host, sizeof(host)))
263 snprintf(host, sizeof(host), "?");
264 snprintf(doas_prompt, sizeof(doas_prompt),
265 "\rdoas (%.32s@%.32s) password: ", ruser, host);
268 ret = pam_authenticate(pamh, 0);
269 if (ret != PAM_SUCCESS) {
275 ret = pam_acct_mgmt(pamh, 0);
276 if (ret == PAM_NEW_AUTHTOK_REQD)
277 ret = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
279 /* account not vaild or changing the auth token failed */
280 if (ret != PAM_SUCCESS)
283 /* set PAM_USER to the user we want to be */
284 ret = pam_set_item(pamh, PAM_USER, user);
285 if (ret != PAM_SUCCESS)
286 warn("pam_set_item(?, PAM_USER, \"%s\"): %s", user,
287 pam_strerror(pamh, ret));
289 ret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
290 if (ret != PAM_SUCCESS)
291 warn("pam_setcred(?, PAM_ESTABLISH_CRED): %s", pam_strerror(pamh, ret));
293 cred_established = 1;
296 ret = pam_open_session(pamh, 0);
297 if (ret != PAM_SUCCESS)
298 errx(1, "pam_open_session: %s", pam_strerror(pamh, ret));
302 if ((child = fork()) == -1) {
303 pamcleanup(PAM_ABORT);
307 /* return as child */