-
Notifications
You must be signed in to change notification settings - Fork 0
/
pam_supair.c
150 lines (129 loc) · 4.68 KB
/
pam_supair.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* pam_supair - a PAM module that allows passwordless su between
* configurable pairs of users.
*
* Copyright (C) 2011 Stanislaw T. Findeisen <stf at eisenbits.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
* Change history
*
* 2011-06-27: STF: Initial version.
*/
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h>
#define PAM_SM_AUTH
#include <security/pam_modules.h>
#include <security/pam_ext.h>
/** These are used in command line arguments. */
static const char DelimPair = ':';
static const char DelimUser = ',';
static short wantDebug = 0;
/**
* Returns the real user name. This memory is obtained with malloc,
* so you might want to free it.
*/
static char* getReqUserName() {
struct passwd *pw = getpwuid(getuid());
if (pw)
return strdup(pw->pw_name);
return NULL;
}
/** Checks if there is a passwd entry for the username (name). */
static short userExists (const pam_handle_t *pamh, const char *name) {
struct passwd *pw = getpwnam(name);
if (pw && (! strcmp(name, pw->pw_name)))
return 1;
return 0;
}
/** Checks if username (s) is sane (doesn't contain delimiters used in command line arguments). */
static short isUserNameSane(const char *s) {
if (index(s, DelimPair))
return 0;
if (index(s, DelimUser))
return 0;
return 1;
}
/** Parses general command line options (like debug etc.). */
static void parseArgs (const pam_handle_t *pamh, int argc, const char **argv)
{
for (; (0 <= --argc); ++argv) {
if (! strcmp(*argv, "debug")) {
wantDebug = 1;
break;
}
}
}
/** Checks if username ordered pair (reqName, targName) is present in the configuration. */
static short checkPair (const pam_handle_t *pamh, int argc, const char **argv, const char *reqName, const char *targName) {
for (; (0 <= --argc); ++argv) {
const char* as = *argv;
if (wantDebug)
pam_syslog(pamh, LOG_DEBUG, "argv: %s", as);
const char* p1 = index(as, DelimPair);
const char* p2 = rindex(as, DelimPair);
if (p1 && (p1 == p2)) {
const char* pr = strstr(as, reqName);
if (pr && (pr < p1)) {
const char* pt = strstr(as, targName);
for (; pt && pt <= p1; pt = strstr(1 + pt, targName));
if (pt && (p1 < pt)) {
if (wantDebug)
pam_syslog(pamh, LOG_DEBUG, "user pair (%s, %s) is found!", reqName, targName);
return 1;
}
}
} else {
pam_syslog(pamh, LOG_ERR, "wrong argument: %s", as);
}
}
return 0;
}
PAM_EXTERN int pam_sm_authenticate (pam_handle_t *pamh, int flags, int argc, const char **argv) {
const char* targetUser = NULL;
char* reqUser = NULL;
// parse generic options (debug etc.)
parseArgs(pamh, argc, argv);
if (PAM_SUCCESS == (pam_get_user(pamh, &targetUser, NULL))) {
if (wantDebug)
pam_syslog(pamh, LOG_DEBUG, "target user: %s", targetUser);
if ((isUserNameSane(targetUser)) && (userExists(pamh, targetUser))) {
// TODO pam_get_item(... PAM_RUSER ...) instead??
if ((reqUser = getReqUserName(pamh))) {
if (wantDebug)
pam_syslog(pamh, LOG_DEBUG, "req user: %s", reqUser);
if (! strcmp(reqUser, targetUser)) {
// wrong usage!
free(reqUser);
return PAM_AUTH_ERR;
}
if ((isUserNameSane(reqUser)) && (checkPair(pamh, argc, argv, reqUser, targetUser))) {
pam_syslog(pamh, LOG_INFO, "success (%s -> %s)", reqUser, targetUser);
free(reqUser);
return PAM_SUCCESS;
} else {
free(reqUser);
}
}
}
}
return PAM_AUTH_ERR;
}
PAM_EXTERN int pam_sm_setcred (pam_handle_t *pamh, int flags, int argc, const char **argv) {
return PAM_SUCCESS;
}