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.
19 #include <sys/types.h>
26 #ifdef HAVE_READPASSPHRASE
27 # include <readpassphrase.h>
29 # include "sys-readpassphrase.h"
38 #include <security/pam_appl.h>
45 #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
48 #define PAM_SERVICE_NAME "doas"
50 static pam_handle_t *pamh = NULL;
51 static char doas_prompt[128];
52 static sig_atomic_t volatile caught_signal = 0;
61 pamprompt(const char *msg, int echo_on, int *ret)
64 char *pass, buf[PAM_MAX_RESP_SIZE];
65 int flags = RPP_REQUIRE_TTY | (echo_on ? RPP_ECHO_ON : RPP_ECHO_OFF);
67 /* overwrite default prompt if it matches "Password:[ ]" */
68 if (strncmp(msg,"Password:", 9) == 0 &&
69 (msg[9] == '\0' || (msg[9] == ' ' && msg[10] == '\0')))
74 pass = readpassphrase(prompt, buf, sizeof(buf), flags);
77 else if (!(pass = strdup(pass)))
82 explicit_bzero(buf, sizeof(buf));
87 pamconv(int nmsgs, const struct pam_message **msgs,
88 struct pam_response **rsps, __UNUSED void *ptr)
90 struct pam_response *rsp;
94 if (!(rsp = calloc(nmsgs, sizeof(struct pam_response))))
95 err(1, "could not allocate pam_response");
97 for (i = 0; i < nmsgs; i++) {
98 switch (style = msgs[i]->msg_style) {
99 case PAM_PROMPT_ECHO_OFF:
100 case PAM_PROMPT_ECHO_ON:
101 rsp[i].resp = pamprompt(msgs[i]->msg, style == PAM_PROMPT_ECHO_ON, &ret);
102 if (ret != PAM_SUCCESS)
108 if (fprintf(style == PAM_ERROR_MSG ? stderr : stdout,
109 "%s\n", msgs[i]->msg) < 0)
114 errx(1, "invalid PAM msg_style %d", style);
124 /* overwrite and free response buffers */
125 for (i = 0; i < nmsgs; i++) {
126 if (rsp[i].resp == NULL)
128 switch (msgs[i]->msg_style) {
129 case PAM_PROMPT_ECHO_OFF:
130 case PAM_PROMPT_ECHO_ON:
131 explicit_bzero(rsp[i].resp, strlen(rsp[i].resp));
142 pamcleanup(int ret, int sess, int cred)
145 ret = pam_close_session(pamh, 0);
146 if (ret != PAM_SUCCESS)
147 errx(1, "pam_close_session: %s", pam_strerror(pamh, ret));
150 ret = pam_setcred(pamh, PAM_DELETE_CRED | PAM_SILENT);
151 if (ret != PAM_SUCCESS)
152 warn("pam_setcred(?, PAM_DELETE_CRED | PAM_SILENT): %s",
153 pam_strerror(pamh, ret));
159 watchsession(pid_t child, int sess, int cred)
162 struct sigaction act, oldact;
167 if (sigprocmask(SIG_BLOCK, &sigs, NULL)) {
168 warn("failed to block signals");
173 /* setup signal handler */
174 act.sa_handler = catchsig;
175 sigemptyset(&act.sa_mask);
178 /* unblock SIGTERM and SIGALRM to catch them */
180 if (sigaddset(&sigs, SIGTERM) ||
181 sigaddset(&sigs, SIGALRM) ||
182 sigaddset(&sigs, SIGTSTP) ||
183 sigaction(SIGTERM, &act, &oldact) ||
184 sigprocmask(SIG_UNBLOCK, &sigs, NULL)) {
185 warn("failed to set signal handler");
190 /* wait for child to be terminated */
191 if (waitpid(child, &status, 0) != -1) {
192 if (WIFSIGNALED(status)) {
193 fprintf(stderr, "%s%s\n", strsignal(WTERMSIG(status)),
194 WCOREDUMP(status) ? " (core dumped)" : "");
195 status = WTERMSIG(status) + 128;
197 status = WEXITSTATUS(status);
199 else if (caught_signal)
200 status = caught_signal + 128;
205 if (caught_signal && child != (pid_t)-1) {
206 fprintf(stderr, "\nSession terminated, killing shell\n");
207 kill(child, SIGTERM);
210 pamcleanup(PAM_SUCCESS, sess, cred);
213 if (child != (pid_t)-1) {
216 kill(child, SIGKILL);
217 fprintf(stderr, " ...killed.\n");
220 /* unblock cached signal and resend */
221 sigaction(SIGTERM, &oldact, NULL);
222 if (caught_signal != SIGTERM)
223 caught_signal = SIGKILL;
224 kill(getpid(), caught_signal);
231 pamauth(const char *user, const char *myname, int interactive, int nopass, int persist)
233 static const struct pam_conv conv = {
239 int ret, sess = 0, cred = 0;
248 if (!user || !myname)
251 ret = pam_start(PAM_SERVICE_NAME, myname, &conv, &pamh);
252 if (ret != PAM_SUCCESS)
253 errx(1, "pam_start(\"%s\", \"%s\", ?, ?): failed",
254 PAM_SERVICE_NAME, myname);
256 ret = pam_set_item(pamh, PAM_RUSER, myname);
257 if (ret != PAM_SUCCESS)
258 warn("pam_set_item(?, PAM_RUSER, \"%s\"): %s",
259 pam_strerror(pamh, ret), myname);
261 if (isatty(0) && (ttydev = ttyname(0)) != NULL) {
262 if (strncmp(ttydev, "/dev/", 5) == 0)
265 ret = pam_set_item(pamh, PAM_TTY, ttydev);
266 if (ret != PAM_SUCCESS)
267 warn("pam_set_item(?, PAM_TTY, \"%s\"): %s",
268 ttydev, pam_strerror(pamh, ret));
274 fd = timestamp_open(&valid, 5 * 60);
275 if (fd != -1 && valid == 1)
283 /* doas style prompt for pam */
284 char host[HOST_NAME_MAX + 1];
285 if (gethostname(host, sizeof(host)))
286 snprintf(host, sizeof(host), "?");
287 snprintf(doas_prompt, sizeof(doas_prompt),
288 "\rdoas (%.32s@%.32s) password: ", myname, host);
291 ret = pam_authenticate(pamh, 0);
292 if (ret != PAM_SUCCESS) {
293 pamcleanup(ret, sess, cred);
294 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
300 ret = pam_acct_mgmt(pamh, 0);
301 if (ret == PAM_NEW_AUTHTOK_REQD)
302 ret = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
304 /* account not vaild or changing the auth token failed */
305 if (ret != PAM_SUCCESS) {
306 pamcleanup(ret, sess, cred);
307 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
311 /* set PAM_USER to the user we want to be */
312 ret = pam_set_item(pamh, PAM_USER, user);
313 if (ret != PAM_SUCCESS)
314 warn("pam_set_item(?, PAM_USER, \"%s\"): %s", user,
315 pam_strerror(pamh, ret));
317 ret = pam_setcred(pamh, PAM_REINITIALIZE_CRED);
318 if (ret != PAM_SUCCESS)
319 warn("pam_setcred(?, PAM_REINITIALIZE_CRED): %s", pam_strerror(pamh, ret));
324 ret = pam_open_session(pamh, 0);
325 if (ret != PAM_SUCCESS)
326 errx(1, "pam_open_session: %s", pam_strerror(pamh, ret));
329 if ((child = fork()) == -1) {
330 pamcleanup(PAM_ABORT, sess, cred);
334 /* return as child */
345 timestamp_set(fd, 5 * 60);
349 watchsession(child, sess, cred);