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