]> git.armaanb.net Git - opendoas.git/blob - libopenbsd/auth_userokay.c
9c896253a8e15c21190bfe9c245ff3fe8ec8973e
[opendoas.git] / libopenbsd / auth_userokay.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 <errno.h>
19 #include <pwd.h>
20 #include <readpassphrase.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include <security/pam_appl.h>
25
26 #include "openbsd.h"
27
28 #define PAM_SERVICE "sudo"
29
30 #define __UNUSED __attribute__ ((unused))
31
32 static int
33 pam_conv(__UNUSED int huh, __UNUSED const struct pam_message **msg,
34                 __UNUSED struct pam_response **rsp, __UNUSED void *ptr)
35 {
36         return 0;
37 }
38
39 static struct pam_conv conv = {
40         .conv = pam_conv,
41         .appdata_ptr = NULL,
42 };
43
44 static int
45 check_pam(const char *user)
46 {
47         fprintf(stderr, "check_pam(%s)\n", user);
48
49         int ret;
50         pam_handle_t *pamh = NULL;
51
52         ret = pam_start(PAM_SERVICE, user, &conv, &pamh);
53         if (ret != 0) {
54                 fprintf(stderr, "pam_start(\"%s\", \"%s\", ?, ?): failed\n",
55                                 PAM_SERVICE, user);
56                 return -1;
57         }
58
59         if ((ret = pam_close_session(pamh, 0)) != 0) {
60                 fprintf(stderr, "pam_close_session(): %s\n", pam_strerror(pamh, ret));
61                 return -1;
62         }
63
64         return 0;
65 }
66
67 int
68 auth_userokay(char *name, char *style, char *type, char *password)
69 {
70         if (!name)
71                 return 0;
72         if (style || type || password) {
73                 fprintf(stderr, "auth_userokay(name, NULL, NULL, NULL)!\n");
74                 exit(1);
75         }
76
77         int ret = check_pam(name);
78         if (ret != 0) {
79                 fprintf(stderr, "PAM authentication failed\n");
80                 return 0;
81         }
82
83         /*
84         char passbuf[256];
85         if (readpassphrase("Password: ", passbuf, sizeof(passbuf),
86                         RPP_REQUIRE_TTY) == NULL)
87                 return 0;
88
89         explicit_bzero(passbuf, sizeof(passbuf));
90         */
91
92         fprintf(stderr, "failing auth check for %s\n", name);
93         return 0;
94 }
95