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