]> git.armaanb.net Git - opendoas.git/blob - pam.c
timestamp: error out if fstat and lstat st_ino and st_dev are not the same
[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 #include <sys/wait.h>
19
20 #include <err.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <pwd.h>
24 #ifdef HAVE_READPASSPHRASE_H
25 #       include <readpassphrase.h>
26 #else
27 #       include "readpassphrase.h"
28 #endif
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <syslog.h>
34 #include <unistd.h>
35
36 #include <security/pam_appl.h>
37
38 #include "includes.h"
39
40 #define PAM_SERVICE_NAME "doas"
41
42 static pam_handle_t *pamh = NULL;
43 static char doas_prompt[128];
44 static sig_atomic_t volatile caught_signal = 0;
45
46 static void
47 catchsig(int sig)
48 {
49         caught_signal = sig;
50 }
51
52 static char *
53 pamprompt(const char *msg, int echo_on, int *ret)
54 {
55         const char *prompt;
56         char *pass, buf[PAM_MAX_RESP_SIZE];
57         int flags = RPP_REQUIRE_TTY | (echo_on ? RPP_ECHO_ON : RPP_ECHO_OFF);
58
59         /* overwrite default prompt if it matches "Password:[ ]" */
60         if (strncmp(msg,"Password:", 9) == 0 &&
61             (msg[9] == '\0' || (msg[9] == ' ' && msg[10] == '\0')))
62                 prompt = doas_prompt;
63         else
64                 prompt = msg;
65
66         pass = readpassphrase(prompt, buf, sizeof(buf), flags);
67         if (!pass)
68                 *ret = PAM_CONV_ERR;
69         else if (!(pass = strdup(pass)))
70                 *ret = PAM_BUF_ERR;
71         else
72                 *ret = PAM_SUCCESS;
73
74         explicit_bzero(buf, sizeof(buf));
75         return pass;
76 }
77
78 static int
79 pamconv(int nmsgs, const struct pam_message **msgs,
80                 struct pam_response **rsps, __UNUSED void *ptr)
81 {
82         struct pam_response *rsp;
83         int i, style;
84         int ret;
85
86         if (!(rsp = calloc(nmsgs, sizeof(struct pam_response))))
87                 err(1, "could not allocate pam_response");
88
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)
95                                 goto fail;
96                         break;
97
98                 case PAM_ERROR_MSG:
99                 case PAM_TEXT_INFO:
100                         if (fprintf(style == PAM_ERROR_MSG ? stderr : stdout,
101                             "%s\n", msgs[i]->msg) < 0)
102                                 goto fail;
103                         break;
104
105                 default:
106                         errx(1, "invalid PAM msg_style %d", style);
107                 }
108         }
109
110         *rsps = rsp;
111         rsp = NULL;
112
113         return PAM_SUCCESS;
114
115 fail:
116         /* overwrite and free response buffers */
117         for (i = 0; i < nmsgs; i++) {
118                 if (rsp[i].resp == NULL)
119                         continue;
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));
124                         free(rsp[i].resp);
125                 }
126                 rsp[i].resp = NULL;
127         }
128
129         return PAM_CONV_ERR;
130 }
131
132 void
133 pamcleanup(int ret, int sess, int cred)
134 {
135         if (sess) {
136                 ret = pam_close_session(pamh, 0);
137                 if (ret != PAM_SUCCESS)
138                         errx(1, "pam_close_session: %s", pam_strerror(pamh, ret));
139         }
140         if (cred) {
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));
145         }
146         pam_end(pamh, ret);
147 }
148
149 void
150 watchsession(pid_t child, int sess, int cred)
151 {
152         sigset_t sigs;
153         struct sigaction act, oldact;
154         int status = 1;
155
156         /* block signals */
157         sigfillset(&sigs);
158         if (sigprocmask(SIG_BLOCK, &sigs, NULL)) {
159                 warn("failed to block signals");
160                 caught_signal = 1;
161                 goto close;
162         }
163
164         /* setup signal handler */
165         act.sa_handler = catchsig;
166         sigemptyset(&act.sa_mask);
167         act.sa_flags = 0;
168
169         /* unblock SIGTERM and SIGALRM to catch them */
170         sigemptyset(&sigs);
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");
177                 caught_signal = 1;
178                 goto close;
179         }
180
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;
187                 } else
188                         status = WEXITSTATUS(status);
189         }
190         else if (caught_signal)
191                 status = caught_signal + 128;
192         else
193                 status = 1;
194
195 close:
196         if (caught_signal && child != (pid_t)-1) {
197                 fprintf(stderr, "\nSession terminated, killing shell\n");
198                 kill(child, SIGTERM);
199         }
200
201         pamcleanup(PAM_SUCCESS, sess, cred);
202
203         if (caught_signal) {
204                 if (child != (pid_t)-1) {
205                         /* kill child */
206                         sleep(2);
207                         kill(child, SIGKILL);
208                         fprintf(stderr, " ...killed.\n");
209                 }
210
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);
216         }
217
218         exit(status);
219 }
220
221 void
222 pamauth(const char *user, const char *myname, int interactive, int nopass, int persist)
223 {
224         static const struct pam_conv conv = {
225                 .conv = pamconv,
226                 .appdata_ptr = NULL,
227         };
228         const char *ttydev;
229         pid_t child;
230         int ret, sess = 0, cred = 0;
231
232 #ifdef USE_TIMESTAMP
233         int fd = -1;
234         int valid = 0;
235 #else
236         (void) persist;
237 #endif
238
239         if (!user || !myname)
240                 errx(1, "Authorization failed");
241
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);
246
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);
251
252         if (isatty(0) && (ttydev = ttyname(0)) != NULL) {
253                 if (strncmp(ttydev, "/dev/", 5) == 0)
254                         ttydev += 5;
255
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));
260         }
261
262
263 #ifdef USE_TIMESTAMP
264         if (persist)
265                 fd = timestamp_open(&valid, 5 * 60);
266         if (fd != -1 && valid == 1)
267                 nopass = 1;
268 #endif
269
270         if (!nopass) {
271                 if (!interactive)
272                         errx(1, "Authorization required");
273
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);
280
281                 /* authenticate */
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");
287                 }
288         }
289
290
291         ret = pam_acct_mgmt(pamh, 0);
292         if (ret == PAM_NEW_AUTHTOK_REQD)
293                 ret = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
294
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");
300         }
301
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));
307
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));
311         else
312                 cred = 1;
313
314         /* open session */
315         ret = pam_open_session(pamh, 0);
316         if (ret != PAM_SUCCESS)
317                 errx(1, "pam_open_session: %s", pam_strerror(pamh, ret));
318         sess = 1;
319
320         if ((child = fork()) == -1) {
321                 pamcleanup(PAM_ABORT, sess, cred);
322                 err(1, "fork");
323         }
324
325         /* return as child */
326         if (child == 0) {
327 #ifdef USE_TIMESTAMP
328                 if (fd != -1)
329                         close(fd);
330 #endif
331                 return;
332         }
333
334 #ifdef USE_TIMESTAMP
335         if (fd != -1) {
336                 timestamp_set(fd, 5 * 60);
337                 close(fd);
338         }
339 #endif
340         watchsession(child, sess, cred);
341 }