]> git.armaanb.net Git - opendoas.git/blob - pam.c
doas: remove unnecessary configure checks, move shadow to its own file
[opendoas.git] / pam.c
1 /*
2  * Copyright (c) 2015 Nathan Holstein <nathan.holstein@gmail.com>
3  *
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.
7  *
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.
15  */
16
17 #include <sys/types.h>
18
19 #include <err.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <pwd.h>
23 #ifdef HAVE_READPASSPHRASE_H
24 #       include <readpassphrase.h>
25 #else
26 #       include "readpassphrase.h"
27 #endif
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/wait.h>
33 #include <unistd.h>
34
35 #include <security/pam_appl.h>
36
37 #include "includes.h"
38
39 #define PAM_SERVICE_NAME "doas"
40
41 static pam_handle_t *pamh = NULL;
42 static char doas_prompt[128];
43 static sig_atomic_t volatile caught_signal = 0;
44 static int session_opened = 0;
45 static int cred_established = 0;
46
47 static void
48 catchsig(int sig)
49 {
50         caught_signal = sig;
51 }
52
53 static char *
54 pamprompt(const char *msg, int echo_on, int *ret)
55 {
56         const char *prompt;
57         char *pass, buf[PAM_MAX_RESP_SIZE];
58         int flags = RPP_REQUIRE_TTY | (echo_on ? RPP_ECHO_ON : RPP_ECHO_OFF);
59
60         /* overwrite default prompt if it matches "Password:[ ]" */
61         if (strncmp(msg,"Password:", 9) == 0 &&
62             (msg[9] == '\0' || (msg[9] == ' ' && msg[10] == '\0')))
63                 prompt = doas_prompt;
64         else
65                 prompt = msg;
66
67         pass = readpassphrase(prompt, buf, sizeof(buf), flags);
68         if (!pass)
69                 *ret = PAM_CONV_ERR;
70         else if (!(pass = strdup(pass)))
71                 *ret = PAM_BUF_ERR;
72         else
73                 *ret = PAM_SUCCESS;
74
75         explicit_bzero(buf, sizeof(buf));
76         return pass;
77 }
78
79 static int
80 pamconv(int nmsgs, const struct pam_message **msgs,
81                 struct pam_response **rsps, __UNUSED void *ptr)
82 {
83         struct pam_response *rsp;
84         int i, style;
85         int ret;
86
87         if (!(rsp = calloc(nmsgs, sizeof(struct pam_response))))
88                 errx(1, "couldn't malloc pam_response");
89
90         for (i = 0; i < nmsgs; i++) {
91                 switch (style = msgs[i]->msg_style) {
92                 case PAM_PROMPT_ECHO_OFF:
93                 case PAM_PROMPT_ECHO_ON:
94                         rsp[i].resp = pamprompt(msgs[i]->msg, style == PAM_PROMPT_ECHO_ON, &ret);
95                         if (ret != PAM_SUCCESS)
96                                 goto fail;
97                         break;
98
99                 case PAM_ERROR_MSG:
100                 case PAM_TEXT_INFO:
101                         if (fprintf(style == PAM_ERROR_MSG ? stderr : stdout,
102                             "%s\n", msgs[i]->msg) < 0)
103                                 goto fail;
104                         break;
105
106                 default:
107                         errx(1, "invalid PAM msg_style %d", style);
108                 }
109         }
110
111         *rsps = rsp;
112         rsp = NULL;
113
114         return PAM_SUCCESS;
115
116 fail:
117         /* overwrite and free response buffers */
118         for (i = 0; i < nmsgs; i++) {
119                 if (rsp[i].resp == NULL)
120                         continue;
121                 switch (style = msgs[i]->msg_style) {
122                 case PAM_PROMPT_ECHO_OFF:
123                 case PAM_PROMPT_ECHO_ON:
124                         explicit_bzero(rsp[i].resp, strlen(rsp[i].resp));
125                         free(rsp[i].resp);
126                 }
127                 rsp[i].resp = NULL;
128         }
129
130         return PAM_CONV_ERR;
131 }
132
133 void
134 pamcleanup(int ret)
135 {
136         if (session_opened != 0) {
137                 ret = pam_close_session(pamh, 0);
138                 if (ret != PAM_SUCCESS)
139                         errx(1, "pam_close_session: %s", pam_strerror(pamh, ret));
140         }
141         if (cred_established != 0) {
142                 ret = pam_setcred(pamh, PAM_DELETE_CRED | PAM_SILENT);
143                 if (ret != PAM_SUCCESS)
144                         warn("pam_setcred(?, PAM_DELETE_CRED | PAM_SILENT): %s",
145                             pam_strerror(pamh, ret));
146         }
147         pam_end(pamh, ret);
148 }
149
150 void
151 watchsession(pid_t child)
152 {
153         sigset_t sigs;
154         struct sigaction act, oldact;
155         int status = 1;
156
157         /* block signals */
158         sigfillset(&sigs);
159         if (sigprocmask(SIG_BLOCK, &sigs, NULL)) {
160                 warn("failed to block signals");
161                 caught_signal = 1;
162                 goto close;
163         }
164
165         /* setup signal handler */
166         act.sa_handler = catchsig;
167         sigemptyset(&act.sa_mask);
168         act.sa_flags = 0;
169
170         /* unblock SIGTERM and SIGALRM to catch them */
171         sigemptyset(&sigs);
172         if (sigaddset(&sigs, SIGTERM) ||
173             sigaddset(&sigs, SIGALRM) ||
174             sigaddset(&sigs, SIGTSTP) ||
175             sigaction(SIGTERM, &act, &oldact) ||
176             sigprocmask(SIG_UNBLOCK, &sigs, NULL)) {
177                 warn("failed to set signal handler");
178                 caught_signal = 1;
179                 goto close;
180         }
181
182         /* wait for child to be terminated */
183         if (waitpid(child, &status, 0) != -1) {
184                 if (WIFSIGNALED(status)) {
185                         fprintf(stderr, "%s%s\n", strsignal(WTERMSIG(status)),
186                                         WCOREDUMP(status) ? " (core dumped)" : "");
187                         status = WTERMSIG(status) + 128;
188                 } else
189                         status = WEXITSTATUS(status);
190         }
191         else if (caught_signal)
192                 status = caught_signal + 128;
193         else
194                 status = 1;
195
196 close:
197         if (caught_signal && child != (pid_t)-1) {
198                 fprintf(stderr, "\nSession terminated, killing shell\n");
199                 kill(child, SIGTERM);
200         }
201
202         pamcleanup(PAM_SUCCESS);
203
204         if (caught_signal) {
205                 if (child != (pid_t)-1) {
206                         /* kill child */
207                         sleep(2);
208                         kill(child, SIGKILL);
209                         fprintf(stderr, " ...killed.\n");
210                 }
211
212                 /* unblock cached signal and resend */
213                 sigaction(SIGTERM, &oldact, NULL);
214                 if (caught_signal != SIGTERM)
215                         caught_signal = SIGKILL;
216                 kill(getpid(), caught_signal);
217         }
218
219         exit(status);
220 }
221
222 int
223 pamauth(const char *user, const char* ruser, int interactive, int nopass)
224 {
225         static const struct pam_conv conv = {
226                 .conv = pamconv,
227                 .appdata_ptr = NULL,
228         };
229         const char *ttydev;
230         pid_t child;
231         int ret;
232
233         if (!user || !ruser)
234                 return 0;
235
236         ret = pam_start(PAM_SERVICE_NAME, ruser, &conv, &pamh);
237         if (ret != PAM_SUCCESS)
238                 errx(1, "pam_start(\"%s\", \"%s\", ?, ?): failed",
239                     PAM_SERVICE_NAME, ruser);
240
241         ret = pam_set_item(pamh, PAM_RUSER, ruser);
242         if (ret != PAM_SUCCESS)
243                 warn("pam_set_item(?, PAM_RUSER, \"%s\"): %s",
244                     pam_strerror(pamh, ret), ruser);
245
246         if (isatty(0) && (ttydev = ttyname(0)) != NULL) {
247                 if (strncmp(ttydev, "/dev/", 5) == 0)
248                         ttydev += 5;
249
250                 ret = pam_set_item(pamh, PAM_TTY, ttydev);
251                 if (ret != PAM_SUCCESS)
252                         warn("pam_set_item(?, PAM_TTY, \"%s\"): %s",
253                             ttydev, pam_strerror(pamh, ret));
254         }
255
256         if (!nopass) {
257                 if (!interactive)
258                         errx(1, "Authorization required");
259
260                 /* doas style prompt for pam */
261                 char host[HOST_NAME_MAX + 1];
262                 if (gethostname(host, sizeof(host)))
263                         snprintf(host, sizeof(host), "?");
264                 snprintf(doas_prompt, sizeof(doas_prompt),
265                     "\rdoas (%.32s@%.32s) password: ", ruser, host);
266
267                 /* authenticate */
268                 ret = pam_authenticate(pamh, 0);
269                 if (ret != PAM_SUCCESS) {
270                         pamcleanup(ret);
271                         return 0;
272                 }
273         }
274
275         ret = pam_acct_mgmt(pamh, 0);
276         if (ret == PAM_NEW_AUTHTOK_REQD)
277                 ret = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
278
279         /* account not vaild or changing the auth token failed */
280         if (ret != PAM_SUCCESS)
281                 return 0;
282
283         /* set PAM_USER to the user we want to be */
284         ret = pam_set_item(pamh, PAM_USER, user);
285         if (ret != PAM_SUCCESS)
286                 warn("pam_set_item(?, PAM_USER, \"%s\"): %s", user,
287                     pam_strerror(pamh, ret));
288
289         ret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
290         if (ret != PAM_SUCCESS)
291                 warn("pam_setcred(?, PAM_ESTABLISH_CRED): %s", pam_strerror(pamh, ret));
292         else
293                 cred_established = 1;
294
295         /* open session */
296         ret = pam_open_session(pamh, 0);
297         if (ret != PAM_SUCCESS)
298                 errx(1, "pam_open_session: %s", pam_strerror(pamh, ret));
299         else
300                 session_opened = 1;
301
302         if ((child = fork()) == -1) {
303                 pamcleanup(PAM_ABORT);
304                 errx(1, "fork");
305         }
306
307         /* return as child */
308         if (child == 0)
309                 return 1;
310
311         watchsession(child);
312
313         return 0;
314 }