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