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