]> git.armaanb.net Git - opendoas.git/blob - pam.c
pam.c: remove dead assignment
[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
136         return PAM_CONV_ERR;
137 }
138
139 void
140 pamcleanup(int ret, int sess, int cred)
141 {
142         if (sess) {
143                 ret = pam_close_session(pamh, 0);
144                 if (ret != PAM_SUCCESS)
145                         errx(1, "pam_close_session: %s", pam_strerror(pamh, ret));
146         }
147         if (cred) {
148                 ret = pam_setcred(pamh, PAM_DELETE_CRED | PAM_SILENT);
149                 if (ret != PAM_SUCCESS)
150                         warn("pam_setcred(?, PAM_DELETE_CRED | PAM_SILENT): %s",
151                             pam_strerror(pamh, ret));
152         }
153         pam_end(pamh, ret);
154 }
155
156 void
157 watchsession(pid_t child, int sess, int cred)
158 {
159         sigset_t sigs;
160         struct sigaction act, oldact;
161         int status = 1;
162
163         /* block signals */
164         sigfillset(&sigs);
165         if (sigprocmask(SIG_BLOCK, &sigs, NULL)) {
166                 warn("failed to block signals");
167                 caught_signal = 1;
168                 goto close;
169         }
170
171         /* setup signal handler */
172         act.sa_handler = catchsig;
173         sigemptyset(&act.sa_mask);
174         act.sa_flags = 0;
175
176         /* unblock SIGTERM and SIGALRM to catch them */
177         sigemptyset(&sigs);
178         if (sigaddset(&sigs, SIGTERM) ||
179             sigaddset(&sigs, SIGALRM) ||
180             sigaddset(&sigs, SIGTSTP) ||
181             sigaction(SIGTERM, &act, &oldact) ||
182             sigprocmask(SIG_UNBLOCK, &sigs, NULL)) {
183                 warn("failed to set signal handler");
184                 caught_signal = 1;
185                 goto close;
186         }
187
188         /* wait for child to be terminated */
189         if (waitpid(child, &status, 0) != -1) {
190                 if (WIFSIGNALED(status)) {
191                         fprintf(stderr, "%s%s\n", strsignal(WTERMSIG(status)),
192                                         WCOREDUMP(status) ? " (core dumped)" : "");
193                         status = WTERMSIG(status) + 128;
194                 } else
195                         status = WEXITSTATUS(status);
196         }
197         else if (caught_signal)
198                 status = caught_signal + 128;
199         else
200                 status = 1;
201
202 close:
203         if (caught_signal && child != (pid_t)-1) {
204                 fprintf(stderr, "\nSession terminated, killing shell\n");
205                 kill(child, SIGTERM);
206         }
207
208         pamcleanup(PAM_SUCCESS, sess, cred);
209
210         if (caught_signal) {
211                 if (child != (pid_t)-1) {
212                         /* kill child */
213                         sleep(2);
214                         kill(child, SIGKILL);
215                         fprintf(stderr, " ...killed.\n");
216                 }
217
218                 /* unblock cached signal and resend */
219                 sigaction(SIGTERM, &oldact, NULL);
220                 if (caught_signal != SIGTERM)
221                         caught_signal = SIGKILL;
222                 kill(getpid(), caught_signal);
223         }
224
225         exit(status);
226 }
227
228 void
229 pamauth(const char *user, const char *myname, int interactive, int nopass, int persist)
230 {
231         static const struct pam_conv conv = {
232                 .conv = pamconv,
233                 .appdata_ptr = NULL,
234         };
235         const char *ttydev;
236         pid_t child;
237         int ret, sess = 0, cred = 0;
238
239 #ifdef USE_TIMESTAMP
240         int fd = -1;
241         int valid = 0;
242 #else
243         (void) persist;
244 #endif
245
246         if (!user || !myname)
247                 errx(1, "Authorization failed");
248
249         ret = pam_start(PAM_SERVICE_NAME, myname, &conv, &pamh);
250         if (ret != PAM_SUCCESS)
251                 errx(1, "pam_start(\"%s\", \"%s\", ?, ?): failed",
252                     PAM_SERVICE_NAME, myname);
253
254         ret = pam_set_item(pamh, PAM_RUSER, myname);
255         if (ret != PAM_SUCCESS)
256                 warn("pam_set_item(?, PAM_RUSER, \"%s\"): %s",
257                     pam_strerror(pamh, ret), myname);
258
259         if (isatty(0) && (ttydev = ttyname(0)) != NULL) {
260                 if (strncmp(ttydev, "/dev/", 5) == 0)
261                         ttydev += 5;
262
263                 ret = pam_set_item(pamh, PAM_TTY, ttydev);
264                 if (ret != PAM_SUCCESS)
265                         warn("pam_set_item(?, PAM_TTY, \"%s\"): %s",
266                             ttydev, pam_strerror(pamh, ret));
267         }
268
269
270 #ifdef USE_TIMESTAMP
271         if (persist)
272                 fd = timestamp_open(&valid, 5 * 60);
273         if (fd != -1 && valid == 1)
274                 nopass = 1;
275 #endif
276
277         if (!nopass) {
278                 if (!interactive)
279                         errx(1, "Authorization required");
280
281                 /* doas style prompt for pam */
282                 char host[HOST_NAME_MAX + 1];
283                 if (gethostname(host, sizeof(host)))
284                         snprintf(host, sizeof(host), "?");
285                 snprintf(doas_prompt, sizeof(doas_prompt),
286                     "\rdoas (%.32s@%.32s) password: ", myname, host);
287
288                 /* authenticate */
289                 ret = pam_authenticate(pamh, 0);
290                 if (ret != PAM_SUCCESS) {
291                         pamcleanup(ret, sess, cred);
292                         syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
293                         errx(1, "Authorization failed");
294                 }
295         }
296
297
298         ret = pam_acct_mgmt(pamh, 0);
299         if (ret == PAM_NEW_AUTHTOK_REQD)
300                 ret = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
301
302         /* account not vaild or changing the auth token failed */
303         if (ret != PAM_SUCCESS) {
304                 pamcleanup(ret, sess, cred);
305                 syslog(LOG_AUTHPRIV | LOG_NOTICE, "failed auth for %s", myname);
306                 errx(1, "Authorization failed");
307         }
308
309         /* set PAM_USER to the user we want to be */
310         ret = pam_set_item(pamh, PAM_USER, user);
311         if (ret != PAM_SUCCESS)
312                 warn("pam_set_item(?, PAM_USER, \"%s\"): %s", user,
313                     pam_strerror(pamh, ret));
314
315         ret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
316         if (ret != PAM_SUCCESS)
317                 warn("pam_setcred(?, PAM_ESTABLISH_CRED): %s", pam_strerror(pamh, ret));
318         else
319                 cred = 1;
320
321         /* open session */
322         ret = pam_open_session(pamh, 0);
323         if (ret != PAM_SUCCESS)
324                 errx(1, "pam_open_session: %s", pam_strerror(pamh, ret));
325         sess = 1;
326
327         if ((child = fork()) == -1) {
328                 pamcleanup(PAM_ABORT, sess, cred);
329                 err(1, "fork");
330         }
331
332         /* return as child */
333         if (child == 0) {
334 #ifdef USE_TIMESTAMP
335                 if (fd != -1)
336                         close(fd);
337 #endif
338                 return;
339         }
340
341 #ifdef USE_TIMESTAMP
342         if (fd != -1) {
343                 timestamp_set(fd, 5 * 60);
344                 close(fd);
345         }
346 #endif
347         watchsession(child, sess, cred);
348 }