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>
24 #ifdef HAVE_READPASSPHRASE_H
25 # include <readpassphrase.h>
27 # include "readpassphrase.h"
36 #include <security/pam_appl.h>
40 #define PAM_SERVICE_NAME "doas"
42 static pam_handle_t *pamh = NULL;
43 static char doas_prompt[128];
44 static sig_atomic_t volatile caught_signal = 0;
53 pamprompt(const char *msg, int echo_on, int *ret)
56 char *pass, buf[PAM_MAX_RESP_SIZE];
57 int flags = RPP_REQUIRE_TTY | (echo_on ? RPP_ECHO_ON : RPP_ECHO_OFF);
59 /* overwrite default prompt if it matches "Password:[ ]" */
60 if (strncmp(msg,"Password:", 9) == 0 &&
61 (msg[9] == '\0' || (msg[9] == ' ' && msg[10] == '\0')))
66 pass = readpassphrase(prompt, buf, sizeof(buf), flags);
69 else if (!(pass = strdup(pass)))
74 explicit_bzero(buf, sizeof(buf));
79 pamconv(int nmsgs, const struct pam_message **msgs,
80 struct pam_response **rsps, __UNUSED void *ptr)
82 struct pam_response *rsp;
86 if (!(rsp = calloc(nmsgs, sizeof(struct pam_response))))
87 err(1, "could not allocate pam_response");
89 for (i = 0; i < nmsgs; i++) {
90 switch (style = msgs[i]->msg_style) {
91 case PAM_PROMPT_ECHO_OFF:
92 case PAM_PROMPT_ECHO_ON:
93 rsp[i].resp = pamprompt(msgs[i]->msg, style == PAM_PROMPT_ECHO_ON, &ret);
94 if (ret != PAM_SUCCESS)
100 if (fprintf(style == PAM_ERROR_MSG ? stderr : stdout,
101 "%s\n", msgs[i]->msg) < 0)
106 errx(1, "invalid PAM msg_style %d", style);
116 /* overwrite and free response buffers */
117 for (i = 0; i < nmsgs; i++) {
118 if (rsp[i].resp == NULL)
120 switch (style = msgs[i]->msg_style) {
121 case PAM_PROMPT_ECHO_OFF:
122 case PAM_PROMPT_ECHO_ON:
123 explicit_bzero(rsp[i].resp, strlen(rsp[i].resp));
133 pamcleanup(int ret, int sess, int cred)
136 ret = pam_close_session(pamh, 0);
137 if (ret != PAM_SUCCESS)
138 errx(1, "pam_close_session: %s", pam_strerror(pamh, ret));
141 ret = pam_setcred(pamh, PAM_DELETE_CRED | PAM_SILENT);
142 if (ret != PAM_SUCCESS)
143 warn("pam_setcred(?, PAM_DELETE_CRED | PAM_SILENT): %s",
144 pam_strerror(pamh, ret));
150 watchsession(pid_t child, int sess, int cred)
153 struct sigaction act, oldact;
158 if (sigprocmask(SIG_BLOCK, &sigs, NULL)) {
159 warn("failed to block signals");
164 /* setup signal handler */
165 act.sa_handler = catchsig;
166 sigemptyset(&act.sa_mask);
169 /* unblock SIGTERM and SIGALRM to catch them */
171 if (sigaddset(&sigs, SIGTERM) ||
172 sigaddset(&sigs, SIGALRM) ||
173 sigaddset(&sigs, SIGTSTP) ||
174 sigaction(SIGTERM, &act, &oldact) ||
175 sigprocmask(SIG_UNBLOCK, &sigs, NULL)) {
176 warn("failed to set signal handler");
181 /* wait for child to be terminated */
182 if (waitpid(child, &status, 0) != -1) {
183 if (WIFSIGNALED(status)) {
184 fprintf(stderr, "%s%s\n", strsignal(WTERMSIG(status)),
185 WCOREDUMP(status) ? " (core dumped)" : "");
186 status = WTERMSIG(status) + 128;
188 status = WEXITSTATUS(status);
190 else if (caught_signal)
191 status = caught_signal + 128;
196 if (caught_signal && child != (pid_t)-1) {
197 fprintf(stderr, "\nSession terminated, killing shell\n");
198 kill(child, SIGTERM);
201 pamcleanup(PAM_SUCCESS, sess, cred);
204 if (child != (pid_t)-1) {
207 kill(child, SIGKILL);
208 fprintf(stderr, " ...killed.\n");
211 /* unblock cached signal and resend */
212 sigaction(SIGTERM, &oldact, NULL);
213 if (caught_signal != SIGTERM)
214 caught_signal = SIGKILL;
215 kill(getpid(), caught_signal);
222 pamauth(const char *user, const char *myname, int interactive, int nopass, int persist)
224 static const struct pam_conv conv = {
230 int ret, sess = 0, cred = 0;
239 if (!user || !myname)
240 errx(1, "Authorization failed");
242 ret = pam_start(PAM_SERVICE_NAME, myname, &conv, &pamh);
243 if (ret != PAM_SUCCESS)
244 errx(1, "pam_start(\"%s\", \"%s\", ?, ?): failed",
245 PAM_SERVICE_NAME, myname);
247 ret = pam_set_item(pamh, PAM_RUSER, myname);
248 if (ret != PAM_SUCCESS)
249 warn("pam_set_item(?, PAM_RUSER, \"%s\"): %s",
250 pam_strerror(pamh, ret), myname);
252 if (isatty(0) && (ttydev = ttyname(0)) != NULL) {
253 if (strncmp(ttydev, "/dev/", 5) == 0)
256 ret = pam_set_item(pamh, PAM_TTY, ttydev);
257 if (ret != PAM_SUCCESS)
258 warn("pam_set_item(?, PAM_TTY, \"%s\"): %s",
259 ttydev, pam_strerror(pamh, ret));
265 fd = timestamp_open(&valid, 5 * 60);
266 if (fd != -1 && valid == 1)
272 errx(1, "Authorization required");
274 /* doas style prompt for pam */
275 char host[HOST_NAME_MAX + 1];
276 if (gethostname(host, sizeof(host)))
277 snprintf(host, sizeof(host), "?");
278 snprintf(doas_prompt, sizeof(doas_prompt),
279 "\rdoas (%.32s@%.32s) password: ", myname, host);
282 ret = pam_authenticate(pamh, 0);
283 if (ret != PAM_SUCCESS) {
284 pamcleanup(ret, sess, cred);
285 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
286 errx(1, "Authorization failed");
291 ret = pam_acct_mgmt(pamh, 0);
292 if (ret == PAM_NEW_AUTHTOK_REQD)
293 ret = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
295 /* account not vaild or changing the auth token failed */
296 if (ret != PAM_SUCCESS) {
297 pamcleanup(ret, sess, cred);
298 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
299 errx(1, "Authorization failed");
302 /* set PAM_USER to the user we want to be */
303 ret = pam_set_item(pamh, PAM_USER, user);
304 if (ret != PAM_SUCCESS)
305 warn("pam_set_item(?, PAM_USER, \"%s\"): %s", user,
306 pam_strerror(pamh, ret));
308 ret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
309 if (ret != PAM_SUCCESS)
310 warn("pam_setcred(?, PAM_ESTABLISH_CRED): %s", pam_strerror(pamh, ret));
315 ret = pam_open_session(pamh, 0);
316 if (ret != PAM_SUCCESS)
317 errx(1, "pam_open_session: %s", pam_strerror(pamh, ret));
320 if ((child = fork()) == -1) {
321 pamcleanup(PAM_ABORT, sess, cred);
325 /* return as child */
336 timestamp_set(fd, 5 * 60);
340 watchsession(child, sess, cred);