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