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