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 (style = 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));
140 pamcleanup(int ret, int sess, int cred)
143 ret = pam_close_session(pamh, 0);
144 if (ret != PAM_SUCCESS)
145 errx(1, "pam_close_session: %s", pam_strerror(pamh, ret));
148 ret = pam_setcred(pamh, PAM_DELETE_CRED | PAM_SILENT);
149 if (ret != PAM_SUCCESS)
150 warn("pam_setcred(?, PAM_DELETE_CRED | PAM_SILENT): %s",
151 pam_strerror(pamh, ret));
157 watchsession(pid_t child, int sess, int cred)
160 struct sigaction act, oldact;
165 if (sigprocmask(SIG_BLOCK, &sigs, NULL)) {
166 warn("failed to block signals");
171 /* setup signal handler */
172 act.sa_handler = catchsig;
173 sigemptyset(&act.sa_mask);
176 /* unblock SIGTERM and SIGALRM to catch them */
178 if (sigaddset(&sigs, SIGTERM) ||
179 sigaddset(&sigs, SIGALRM) ||
180 sigaddset(&sigs, SIGTSTP) ||
181 sigaction(SIGTERM, &act, &oldact) ||
182 sigprocmask(SIG_UNBLOCK, &sigs, NULL)) {
183 warn("failed to set signal handler");
188 /* wait for child to be terminated */
189 if (waitpid(child, &status, 0) != -1) {
190 if (WIFSIGNALED(status)) {
191 fprintf(stderr, "%s%s\n", strsignal(WTERMSIG(status)),
192 WCOREDUMP(status) ? " (core dumped)" : "");
193 status = WTERMSIG(status) + 128;
195 status = WEXITSTATUS(status);
197 else if (caught_signal)
198 status = caught_signal + 128;
203 if (caught_signal && child != (pid_t)-1) {
204 fprintf(stderr, "\nSession terminated, killing shell\n");
205 kill(child, SIGTERM);
208 pamcleanup(PAM_SUCCESS, sess, cred);
211 if (child != (pid_t)-1) {
214 kill(child, SIGKILL);
215 fprintf(stderr, " ...killed.\n");
218 /* unblock cached signal and resend */
219 sigaction(SIGTERM, &oldact, NULL);
220 if (caught_signal != SIGTERM)
221 caught_signal = SIGKILL;
222 kill(getpid(), caught_signal);
229 pamauth(const char *user, const char *myname, int interactive, int nopass, int persist)
231 static const struct pam_conv conv = {
237 int ret, sess = 0, cred = 0;
246 if (!user || !myname)
247 errx(1, "Authorization failed");
249 ret = pam_start(PAM_SERVICE_NAME, myname, &conv, &pamh);
250 if (ret != PAM_SUCCESS)
251 errx(1, "pam_start(\"%s\", \"%s\", ?, ?): failed",
252 PAM_SERVICE_NAME, myname);
254 ret = pam_set_item(pamh, PAM_RUSER, myname);
255 if (ret != PAM_SUCCESS)
256 warn("pam_set_item(?, PAM_RUSER, \"%s\"): %s",
257 pam_strerror(pamh, ret), myname);
259 if (isatty(0) && (ttydev = ttyname(0)) != NULL) {
260 if (strncmp(ttydev, "/dev/", 5) == 0)
263 ret = pam_set_item(pamh, PAM_TTY, ttydev);
264 if (ret != PAM_SUCCESS)
265 warn("pam_set_item(?, PAM_TTY, \"%s\"): %s",
266 ttydev, pam_strerror(pamh, ret));
272 fd = timestamp_open(&valid, 5 * 60);
273 if (fd != -1 && valid == 1)
279 errx(1, "Authorization required");
281 /* doas style prompt for pam */
282 char host[HOST_NAME_MAX + 1];
283 if (gethostname(host, sizeof(host)))
284 snprintf(host, sizeof(host), "?");
285 snprintf(doas_prompt, sizeof(doas_prompt),
286 "\rdoas (%.32s@%.32s) password: ", myname, host);
289 ret = pam_authenticate(pamh, 0);
290 if (ret != PAM_SUCCESS) {
291 pamcleanup(ret, sess, cred);
292 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
293 errx(1, "Authorization failed");
298 ret = pam_acct_mgmt(pamh, 0);
299 if (ret == PAM_NEW_AUTHTOK_REQD)
300 ret = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
302 /* account not vaild or changing the auth token failed */
303 if (ret != PAM_SUCCESS) {
304 pamcleanup(ret, sess, cred);
305 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
306 errx(1, "Authorization failed");
309 /* set PAM_USER to the user we want to be */
310 ret = pam_set_item(pamh, PAM_USER, user);
311 if (ret != PAM_SUCCESS)
312 warn("pam_set_item(?, PAM_USER, \"%s\"): %s", user,
313 pam_strerror(pamh, ret));
315 ret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
316 if (ret != PAM_SUCCESS)
317 warn("pam_setcred(?, PAM_ESTABLISH_CRED): %s", pam_strerror(pamh, ret));
322 ret = pam_open_session(pamh, 0);
323 if (ret != PAM_SUCCESS)
324 errx(1, "pam_open_session: %s", pam_strerror(pamh, ret));
327 if ((child = fork()) == -1) {
328 pamcleanup(PAM_ABORT, sess, cred);
332 /* return as child */
343 timestamp_set(fd, 5 * 60);
347 watchsession(child, sess, cred);