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>
44 #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
47 #define PAM_SERVICE_NAME "doas"
49 static pam_handle_t *pamh = NULL;
50 static char doas_prompt[128];
51 static sig_atomic_t volatile caught_signal = 0;
60 pamprompt(const char *msg, int echo_on, int *ret)
63 char *pass, buf[PAM_MAX_RESP_SIZE];
64 int flags = RPP_REQUIRE_TTY | (echo_on ? RPP_ECHO_ON : RPP_ECHO_OFF);
66 /* overwrite default prompt if it matches "Password:[ ]" */
67 if (strncmp(msg,"Password:", 9) == 0 &&
68 (msg[9] == '\0' || (msg[9] == ' ' && msg[10] == '\0')))
73 pass = readpassphrase(prompt, buf, sizeof(buf), flags);
76 else if (!(pass = strdup(pass)))
81 explicit_bzero(buf, sizeof(buf));
86 pamconv(int nmsgs, const struct pam_message **msgs,
87 struct pam_response **rsps, __UNUSED void *ptr)
89 struct pam_response *rsp;
93 if (!(rsp = calloc(nmsgs, sizeof(struct pam_response))))
94 err(1, "could not allocate pam_response");
96 for (i = 0; i < nmsgs; i++) {
97 switch (style = msgs[i]->msg_style) {
98 case PAM_PROMPT_ECHO_OFF:
99 case PAM_PROMPT_ECHO_ON:
100 rsp[i].resp = pamprompt(msgs[i]->msg, style == PAM_PROMPT_ECHO_ON, &ret);
101 if (ret != PAM_SUCCESS)
107 if (fprintf(style == PAM_ERROR_MSG ? stderr : stdout,
108 "%s\n", msgs[i]->msg) < 0)
113 errx(1, "invalid PAM msg_style %d", style);
123 /* overwrite and free response buffers */
124 for (i = 0; i < nmsgs; i++) {
125 if (rsp[i].resp == NULL)
127 switch (msgs[i]->msg_style) {
128 case PAM_PROMPT_ECHO_OFF:
129 case PAM_PROMPT_ECHO_ON:
130 explicit_bzero(rsp[i].resp, strlen(rsp[i].resp));
141 pamcleanup(int ret, int sess, int cred)
144 ret = pam_close_session(pamh, 0);
145 if (ret != PAM_SUCCESS)
146 errx(1, "pam_close_session: %s", pam_strerror(pamh, ret));
149 ret = pam_setcred(pamh, PAM_DELETE_CRED | PAM_SILENT);
150 if (ret != PAM_SUCCESS)
151 warn("pam_setcred(?, PAM_DELETE_CRED | PAM_SILENT): %s",
152 pam_strerror(pamh, ret));
158 watchsession(pid_t child, int sess, int cred)
161 struct sigaction act, oldact;
166 if (sigprocmask(SIG_BLOCK, &sigs, NULL)) {
167 warn("failed to block signals");
172 /* setup signal handler */
173 act.sa_handler = catchsig;
174 sigemptyset(&act.sa_mask);
177 /* unblock SIGTERM and SIGALRM to catch them */
179 if (sigaddset(&sigs, SIGTERM) ||
180 sigaddset(&sigs, SIGALRM) ||
181 sigaddset(&sigs, SIGTSTP) ||
182 sigaction(SIGTERM, &act, &oldact) ||
183 sigprocmask(SIG_UNBLOCK, &sigs, NULL)) {
184 warn("failed to set signal handler");
189 /* wait for child to be terminated */
190 if (waitpid(child, &status, 0) != -1) {
191 if (WIFSIGNALED(status)) {
192 fprintf(stderr, "%s%s\n", strsignal(WTERMSIG(status)),
193 WCOREDUMP(status) ? " (core dumped)" : "");
194 status = WTERMSIG(status) + 128;
196 status = WEXITSTATUS(status);
198 else if (caught_signal)
199 status = caught_signal + 128;
204 if (caught_signal && child != (pid_t)-1) {
205 fprintf(stderr, "\nSession terminated, killing shell\n");
206 kill(child, SIGTERM);
209 pamcleanup(PAM_SUCCESS, sess, cred);
212 if (child != (pid_t)-1) {
215 kill(child, SIGKILL);
216 fprintf(stderr, " ...killed.\n");
219 /* unblock cached signal and resend */
220 sigaction(SIGTERM, &oldact, NULL);
221 if (caught_signal != SIGTERM)
222 caught_signal = SIGKILL;
223 kill(getpid(), caught_signal);
230 pamauth(const char *user, const char *myname, int interactive, int nopass, int persist)
232 static const struct pam_conv conv = {
238 int ret, sess = 0, cred = 0;
247 if (!user || !myname)
248 errx(1, "Authorization failed");
250 ret = pam_start(PAM_SERVICE_NAME, myname, &conv, &pamh);
251 if (ret != PAM_SUCCESS)
252 errx(1, "pam_start(\"%s\", \"%s\", ?, ?): failed",
253 PAM_SERVICE_NAME, myname);
255 ret = pam_set_item(pamh, PAM_RUSER, myname);
256 if (ret != PAM_SUCCESS)
257 warn("pam_set_item(?, PAM_RUSER, \"%s\"): %s",
258 pam_strerror(pamh, ret), myname);
260 if (isatty(0) && (ttydev = ttyname(0)) != NULL) {
261 if (strncmp(ttydev, "/dev/", 5) == 0)
264 ret = pam_set_item(pamh, PAM_TTY, ttydev);
265 if (ret != PAM_SUCCESS)
266 warn("pam_set_item(?, PAM_TTY, \"%s\"): %s",
267 ttydev, pam_strerror(pamh, ret));
273 fd = timestamp_open(&valid, 5 * 60);
274 if (fd != -1 && valid == 1)
280 errx(1, "Authorization required");
282 /* doas style prompt for pam */
283 char host[HOST_NAME_MAX + 1];
284 if (gethostname(host, sizeof(host)))
285 snprintf(host, sizeof(host), "?");
286 snprintf(doas_prompt, sizeof(doas_prompt),
287 "\rdoas (%.32s@%.32s) password: ", myname, host);
290 ret = pam_authenticate(pamh, 0);
291 if (ret != PAM_SUCCESS) {
292 pamcleanup(ret, sess, cred);
293 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
294 errx(1, "Authorization failed");
299 ret = pam_acct_mgmt(pamh, 0);
300 if (ret == PAM_NEW_AUTHTOK_REQD)
301 ret = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
303 /* account not vaild or changing the auth token failed */
304 if (ret != PAM_SUCCESS) {
305 pamcleanup(ret, sess, cred);
306 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
307 errx(1, "Authorization failed");
310 /* set PAM_USER to the user we want to be */
311 ret = pam_set_item(pamh, PAM_USER, user);
312 if (ret != PAM_SUCCESS)
313 warn("pam_set_item(?, PAM_USER, \"%s\"): %s", user,
314 pam_strerror(pamh, ret));
316 ret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
317 if (ret != PAM_SUCCESS)
318 warn("pam_setcred(?, PAM_ESTABLISH_CRED): %s", pam_strerror(pamh, ret));
323 ret = pam_open_session(pamh, 0);
324 if (ret != PAM_SUCCESS)
325 errx(1, "pam_open_session: %s", pam_strerror(pamh, ret));
328 if ((child = fork()) == -1) {
329 pamcleanup(PAM_ABORT, sess, cred);
333 /* return as child */
344 timestamp_set(fd, 5 * 60);
348 watchsession(child, sess, cred);