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