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>
31 #include <security/pam_appl.h>
36 #define PAM_SERVICE_NAME "doas"
38 static pam_handle_t *pamh = NULL;
39 static char doas_prompt[128];
40 static sig_atomic_t volatile caught_signal = 0;
41 static int session_opened = 0;
42 static int cred_established = 0;
51 pamprompt(const char *msg, int echo_on, int *ret)
54 char *pass, buf[PAM_MAX_RESP_SIZE];
55 int flags = RPP_REQUIRE_TTY | (echo_on ? RPP_ECHO_ON : RPP_ECHO_OFF);
57 /* overwrite default prompt if it matches "Password:[ ]" */
58 if (strncmp(msg,"Password:", 9) == 0 &&
59 (msg[9] == '\0' || (msg[9] == ' ' && msg[10] == '\0')))
64 pass = readpassphrase(prompt, buf, sizeof(buf), flags);
67 else if (!(pass = strdup(pass)))
72 explicit_bzero(buf, sizeof(buf));
77 pamconv(int nmsgs, const struct pam_message **msgs,
78 struct pam_response **rsps, __UNUSED void *ptr)
80 struct pam_response *rsp;
84 if (!(rsp = calloc(nmsgs, sizeof(struct pam_response))))
85 errx(1, "couldn't malloc pam_response");
87 for (i = 0; i < nmsgs; i++) {
88 switch (style = msgs[i]->msg_style) {
89 case PAM_PROMPT_ECHO_OFF:
90 case PAM_PROMPT_ECHO_ON:
91 rsp[i].resp = pamprompt(msgs[i]->msg, style == PAM_PROMPT_ECHO_ON, &ret);
92 if (ret != PAM_SUCCESS)
98 if (fprintf(style == PAM_ERROR_MSG ? stderr : stdout,
99 "%s\n", msgs[i]->msg) < 0)
104 errx(1, "invalid PAM msg_style %d", style);
114 /* overwrite and free response buffers */
115 for (i = 0; i < nmsgs; i++) {
116 if (rsp[i].resp == NULL)
118 switch (style = msgs[i]->msg_style) {
119 case PAM_PROMPT_ECHO_OFF:
120 case PAM_PROMPT_ECHO_ON:
121 explicit_bzero(rsp[i].resp, strlen(rsp[i].resp));
133 if (session_opened != 0) {
134 ret = pam_close_session(pamh, 0);
135 if (ret != PAM_SUCCESS)
136 errx(1, "pam_close_session: %s", pam_strerror(pamh, ret));
138 if (cred_established != 0) {
139 ret = pam_setcred(pamh, PAM_DELETE_CRED | PAM_SILENT);
140 if (ret != PAM_SUCCESS)
141 warn("pam_setcred(?, PAM_DELETE_CRED | PAM_SILENT): %s",
142 pam_strerror(pamh, ret));
148 watchsession(pid_t child)
151 struct sigaction act, oldact;
156 if (sigprocmask(SIG_BLOCK, &sigs, NULL)) {
157 warn("failed to block signals");
162 /* setup signal handler */
163 act.sa_handler = catchsig;
164 sigemptyset(&act.sa_mask);
167 /* unblock SIGTERM and SIGALRM to catch them */
169 if (sigaddset(&sigs, SIGTERM) ||
170 sigaddset(&sigs, SIGALRM) ||
171 sigaddset(&sigs, SIGTSTP) ||
172 sigaction(SIGTERM, &act, &oldact) ||
173 sigprocmask(SIG_UNBLOCK, &sigs, NULL)) {
174 warn("failed to set signal handler");
179 /* wait for child to be terminated */
180 if (waitpid(child, &status, 0) != -1) {
181 if (WIFSIGNALED(status)) {
182 fprintf(stderr, "%s%s\n", strsignal(WTERMSIG(status)),
183 WCOREDUMP(status) ? " (core dumped)" : "");
184 status = WTERMSIG(status) + 128;
186 status = WEXITSTATUS(status);
188 else if (caught_signal)
189 status = caught_signal + 128;
195 fprintf(stderr, "\nSession terminated, killing shell\n");
196 kill(child, SIGTERM);
199 pamcleanup(PAM_SUCCESS);
204 kill(child, SIGKILL);
205 fprintf(stderr, " ...killed.\n");
207 /* unblock cached signal and resend */
208 sigaction(SIGTERM, &oldact, NULL);
209 if (caught_signal != SIGTERM)
210 caught_signal = SIGKILL;
211 kill(getpid(), caught_signal);
218 pamauth(const char *user, const char* ruser, int interactive, int nopass)
220 static const struct pam_conv conv = {
231 ret = pam_start(PAM_SERVICE_NAME, ruser, &conv, &pamh);
232 if (ret != PAM_SUCCESS)
233 errx(1, "pam_start(\"%s\", \"%s\", ?, ?): failed",
234 PAM_SERVICE_NAME, ruser);
236 ret = pam_set_item(pamh, PAM_RUSER, ruser);
237 if (ret != PAM_SUCCESS)
238 warn("pam_set_item(?, PAM_RUSER, \"%s\"): %s",
239 pam_strerror(pamh, ret), ruser);
241 if (isatty(0) && (ttydev = ttyname(0)) != NULL) {
242 if (strncmp(ttydev, "/dev/", 5) == 0)
245 ret = pam_set_item(pamh, PAM_TTY, ttydev);
246 if (ret != PAM_SUCCESS)
247 warn("pam_set_item(?, PAM_TTY, \"%s\"): %s",
248 ttydev, pam_strerror(pamh, ret));
253 errx(1, "Authorization required");
255 /* doas style prompt for pam */
256 char host[HOST_NAME_MAX + 1];
257 if (gethostname(host, sizeof(host)))
258 snprintf(host, sizeof(host), "?");
259 snprintf(doas_prompt, sizeof(doas_prompt),
260 "\rdoas (%.32s@%.32s) password: ", ruser, host);
263 ret = pam_authenticate(pamh, 0);
264 if (ret != PAM_SUCCESS) {
270 ret = pam_acct_mgmt(pamh, 0);
271 if (ret == PAM_NEW_AUTHTOK_REQD)
272 ret = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
274 /* account not vaild or changing the auth token failed */
275 if (ret != PAM_SUCCESS)
278 /* set PAM_USER to the user we want to be */
279 ret = pam_set_item(pamh, PAM_USER, user);
280 if (ret != PAM_SUCCESS)
281 warn("pam_set_item(?, PAM_USER, \"%s\"): %s", user,
282 pam_strerror(pamh, ret));
284 ret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
285 if (ret != PAM_SUCCESS)
286 warn("pam_setcred(?, PAM_ESTABLISH_CRED): %s", pam_strerror(pamh, ret));
288 cred_established = 1;
291 ret = pam_open_session(pamh, 0);
292 if (ret != PAM_SUCCESS)
293 errx(1, "pam_open_session: %s", pam_strerror(pamh, ret));
297 if ((child = fork()) == -1) {
298 pamcleanup(PAM_ABORT);
302 /* return as child */