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>
28 #include <security/pam_appl.h>
33 #define PAM_SERVICE_NAME "doas"
35 static pam_handle_t *pamh = NULL;
36 static sig_atomic_t volatile caught_signal = 0;
39 prompt(const char *msg, int echo_on, int *pam)
41 char buf[PAM_MAX_RESP_SIZE];
42 int flags = RPP_REQUIRE_TTY | (echo_on ? RPP_ECHO_ON : RPP_ECHO_OFF);
43 char *ret = readpassphrase(msg, buf, sizeof(buf), flags);
46 else if (!(ret = strdup(ret)))
48 explicit_bzero(buf, sizeof(buf));
53 doas_pam_conv(int nmsgs, const struct pam_message **msgs,
54 struct pam_response **rsps, __UNUSED void *ptr)
56 struct pam_response *rsp;
58 int ret = PAM_SUCCESS;
60 if (!(rsp = calloc(nmsgs, sizeof(struct pam_response))))
61 errx(1, "couldn't malloc pam_response");
63 for (i = 0; i < nmsgs; i++) {
64 switch (style = msgs[i]->msg_style) {
65 case PAM_PROMPT_ECHO_OFF:
66 case PAM_PROMPT_ECHO_ON:
67 rsp[i].resp = prompt(msgs[i]->msg, style == PAM_PROMPT_ECHO_ON, &ret);
68 if (ret != PAM_SUCCESS)
74 if (fprintf(style == PAM_ERROR_MSG ? stderr : stdout,
75 "%s\n", msgs[i]->msg) < 0)
80 errx(1, "invalid PAM msg_style %d", style);
90 /* overwrite and free response buffers */
91 for (i = 0; i < nmsgs; i++) {
92 if (rsp[i].resp == NULL)
94 switch (style = msgs[i]->msg_style) {
95 case PAM_PROMPT_ECHO_OFF:
96 case PAM_PROMPT_ECHO_ON:
97 explicit_bzero(rsp[i].resp, strlen(rsp[i].resp));
113 doas_pam(char *name, int interactive, int nopass)
115 static const struct pam_conv conv = {
116 .conv = doas_pam_conv,
119 const char *ttydev, *tty;
126 ret = pam_start(PAM_SERVICE_NAME, name, &conv, &pamh);
127 if (ret != PAM_SUCCESS)
128 errx(1, "pam_start(\"%s\", \"%s\", ?, ?): failed\n",
129 PAM_SERVICE_NAME, name);
131 ret = pam_set_item(pamh, PAM_USER, name);
132 if (ret != PAM_SUCCESS)
133 errx(1, "pam_set_item(?, PAM_USER, \"%s\"): %s\n",
134 name, pam_strerror(pamh, ret));
136 ret = pam_set_item(pamh, PAM_RUSER, name);
137 if (ret != PAM_SUCCESS)
138 errx(1, "pam_set_item(?, PAM_RUSER, \"%s\"): %s\n",
139 name, pam_strerror(pamh, ret));
141 if (isatty(0) && (ttydev = ttyname(0)) != NULL) {
142 if (strncmp(ttydev, "/dev/", 5))
147 ret = pam_set_item(pamh, PAM_TTY, tty);
148 if (ret != PAM_SUCCESS)
149 errx(1, "pam_set_item(?, PAM_TTY, \"%s\"): %s\n",
150 tty, pam_strerror(pamh, ret));
155 errx(1, "Authorization required");
157 ret = pam_authenticate(pamh, 0);
158 if (ret != PAM_SUCCESS) {
159 ret = pam_end(pamh, ret);
160 if (ret != PAM_SUCCESS)
161 errx(1, "pam_end(): %s\n", pam_strerror(pamh, ret));
166 ret = pam_acct_mgmt(pamh, 0);
167 if (ret == PAM_NEW_AUTHTOK_REQD)
168 ret = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
170 /* account not vaild or changing the auth token failed */
171 if (ret != PAM_SUCCESS)
174 ret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
175 if (ret != PAM_SUCCESS)
176 errx(1, "pam_setcred(?, PAM_ESTABLISH_CRED): %s\n",
177 pam_strerror(pamh, ret));
180 ret = pam_open_session(pamh, 0);
181 if (ret != PAM_SUCCESS)
182 errx(1, "pam_open_session(): %s\n", pam_strerror(pamh, ret));
184 if ((child = fork()) == -1) {
185 pam_close_session(pamh, 0);
186 pam_end(pamh, PAM_ABORT);
190 /* return as child */
195 /* parent watches for signals and closes session */
197 struct sigaction act, oldact;
202 if (sigprocmask(SIG_BLOCK, &sigs, NULL)) {
203 warn("failed to block signals");
207 /* setup signal handler */
208 act.sa_handler = catchsig;
209 sigemptyset(&act.sa_mask);
212 /* unblock SIGTERM and SIGALRM to catch them */
214 if(sigaddset(&sigs, SIGTERM) ||
215 sigaddset(&sigs, SIGALRM) ||
216 sigaction(SIGTERM, &act, &oldact) ||
217 sigprocmask(SIG_UNBLOCK, &sigs, NULL)) {
218 warn("failed to set signal handler");
222 if (!caught_signal) {
223 /* wait for child to be terminated */
224 if (waitpid(child, &status, 0) != -1) {
225 if (WIFSIGNALED(status)) {
226 fprintf(stderr, "%s%s\n", strsignal(WTERMSIG(status)),
227 WCOREDUMP(status) ? " (core dumped)" : "");
228 status = WTERMSIG(status) + 128;
230 status = WEXITSTATUS(status);
233 else if (caught_signal)
234 status = caught_signal + 128;
240 fprintf(stderr, "\nSession terminated, killing shell\n");
241 kill(child, SIGTERM);
245 ret = pam_close_session(pamh, 0);
246 if (ret != PAM_SUCCESS)
247 errx(1, "pam_close_session(): %s\n", pam_strerror(pamh, ret));
249 pam_end(pamh, PAM_SUCCESS);
254 kill(child, SIGKILL);
255 fprintf(stderr, " ...killed.\n");
257 /* unblock cached signal and resend */
258 sigaction(SIGTERM, &oldact, NULL);
259 if (caught_signal != SIGTERM)
260 caught_signal = SIGKILL;
261 kill(getpid(), caught_signal);