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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
/* Copyright 2018 Cumulus Networks, Inc. All rights reserved.
*
* 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 2 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 - see the file COPYING.
*/
/*
* support routines for Cumulus Linux RADIUS client support.
* They create the flat file mapping for the session, and create
* the home directory if needed.
* See the libnss-mapuser source for how the flat file database
* is used.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <libaudit.h>
#include "pam_radius_auth.h"
static const char mapdir[] = "/run/mapuser";
static unsigned get_sessionid(void)
{
int fd = -1, cnt = 0;
unsigned id = 0U;
static char buf[12];
fd = open("/proc/self/sessionid", O_RDONLY);
if (fd != -1) {
cnt = read(fd, buf, sizeof(buf));
close(fd);
}
if (fd != -1 && cnt > 0) {
id = strtoul(buf, NULL, 0);
}
return id;
}
/*
* Write the mapping file used by libnss-mapuser into mapdir/SESSIONID
* This info is used by the mapuser and mapuid NSS plugins to return
* correct information for users that are logged in using mapping (that
* is, are not present in the password database(s)).
* This allows very simple configuration of RADIUS clients, since it's
* no longer necessary to add all user accounts to the local /etc/passwd
* file (or use LDAP, etc.).
*/
void
__write_mapfile(pam_handle_t * pamh, const char *user, uid_t uid,
int privileged, int debug)
{
char tmstr[64], tmpstr[64];
struct timeval tv = { 0, 0 };
struct tm *tmv;
int res = 0;
unsigned session;
uid_t auid;
pid_t pid;
FILE *f;
(void)gettimeofday(&tv, NULL);
tmv = localtime(&tv.tv_sec);
*tmstr = '\0';
if (tmv)
res = strftime(tmpstr, sizeof tmpstr, "%FT%T", tmv);
if (!res && !*tmstr)
snprintf(tmpstr, sizeof tmpstr, "%llu",
(unsigned long long)tv.tv_sec);
snprintf(tmstr, sizeof tmstr, "%s.%u", tmpstr, (unsigned)tv.tv_usec);
auid = audit_getloginuid();
if (auid == ~0U) { /* normal case */
audit_setloginuid(uid);
auid = audit_getloginuid();
}
session = get_sessionid();
pid = getpid();
if (auid == 0 || auid == ~0U || session == ~0U) {
/* if these aren't valid, we can't use the mapfile, so
* don't create it
*/
if (debug)
pam_syslog(pamh, LOG_DEBUG, "Skipping mapfile user=%s"
" auid=%u session=%u", user, auid, session);
return;
}
/* won't hurt if it already exists, same overhead as stat() first */
mkdir(mapdir, 0755);
snprintf(tmpstr, sizeof tmpstr, "%s/%u", mapdir, session);
/*
* Only create if it doesn't exist. It might exist if we are called
* from su or sudo after a login, for example
*/
f = fopen(tmpstr, "wx");
if (!f) {
if (errno != EEXIST)
pam_syslog(pamh, LOG_WARNING,
"Can't create mapfile %s for user (%s): %m",
tmpstr, user);
return;
}
res =
fprintf(f,
"%s\nuser=%s\npid=%u\nauid=%u\nsession=%u\nprivileged=%s\n",
tmstr, user, pid, auid, session, privileged ? "yes" : "no");
if (fclose(f) == EOF || res <= 0)
pam_syslog(pamh, LOG_WARNING, "Error writing mapfile %s for"
" user (%s): %m", tmpstr, user);
}
/*
* Remove the mapping file used by libnss-mapuser into mapdir/SESSIONID
* based on the session. called from pam's sm_close entry point.
* return 0 if not removed, 1 if removed. This is so we can avoid
* talking to the RADIUS server if the close entry point isn't for
* one of our sessions.
*/
int __remove_mapfile(pam_handle_t * pamh, const char *user, int debug)
{
unsigned session;
uid_t auid;
pid_t pid;
int auidmatch = 0, sessmatch = 0, pidmatch = 0, usermatch = 0;
char mapfile[64], linebuf[128];
FILE *f;
if (!user)
return 0; /* shouldn't ever happen */
pid = getpid();
session = get_sessionid();
if (!session || session == (uid_t) - 1)
return 0;
snprintf(mapfile, sizeof mapfile, "%s/%u", mapdir, session);
f = fopen(mapfile, "r");
if (!f)
return 0;
auid = audit_getloginuid();
while (fgets(linebuf, sizeof linebuf, f)) {
unsigned long val;
char *ok;
if (!strncmp(linebuf, "session=", 8)) {
val = strtoul(linebuf + 8, &ok, 0);
if (val == session && ok != (linebuf + 8))
sessmatch = 1;
} else if (!strncmp(linebuf, "user=", 5)) {
strtok(linebuf + 5, " \t\n\r\f");
if (!strcmp(user, linebuf + 5))
usermatch = 1;
} else if (!strncmp(linebuf, "auid=", 5)) {
val = strtoul(linebuf + 5, &ok, 0);
if (val == auid && ok != (linebuf + 5))
auidmatch = 1;
} else if (!strncmp(linebuf, "pid=", 4)) {
val = strtoul(linebuf + 4, &ok, 0);
if (val == pid && ok != (linebuf + 4))
pidmatch = 1;
}
}
fclose(f);
if (auidmatch && pidmatch && sessmatch && usermatch) {
if (unlink(mapfile))
pam_syslog(pamh, LOG_WARNING,
"Remove mapfile %s for user %s failed: %m",
mapfile, user);
}
else if (debug)
pam_syslog(pamh, LOG_DEBUG, "mapfile %s user %s not removed,"
" doesn't match", mapfile, user);
return 1;
}
/*
* check to see if the home directory for the user exists
* and create it using the mkhomedir_helper if it does not.
* The code is based on the pam_mkhomedir plugin source
* It must be called after the mapping file is written, or
* getpwnam() won't be able to return the correct information
* using the libnss-mapuser plugin (which is what we expect
* for this RADIUS client).
*/
void
__chk_homedir(pam_handle_t * pamh, const char *user, const char *homedir,
int debug)
{
int rc, retval, child, restore = 0;
struct stat st;
struct sigaction newsa, oldsa;
const char *path = "/sbin/mkhomedir_helper";
if (stat(homedir, &st) == 0)
return;
if (debug)
pam_syslog(pamh, LOG_NOTICE,
"creating home directory %s for user %s", homedir,
user);
/*
* Ensure that when child process exits that the program using PAM
* doesn't get a signal it isn't expecting, which might kill the
* program, or confuse it.
*/
memset(&newsa, '\0', sizeof(newsa));
newsa.sa_handler = SIG_DFL;
if (sigaction(SIGCHLD, &newsa, &oldsa) == 0)
restore = 1;
child = fork();
if (child == -1) {
pam_syslog(pamh, LOG_ERR, "fork to exec %s %s failed: %m",
path, user);
return;
}
if (child == 0) {
execl(path, path, user, NULL);
pam_syslog(pamh, LOG_ERR, "exec %s %s failed: %m", path, user);
exit(1);
}
while ((rc = waitpid(child, &retval, 0)) < 0 && errno == EINTR) ;
if (rc < 0)
pam_syslog(pamh, LOG_ERR,
"waitpid for exec of %s %s failed: %m", path, user);
else if (!WIFEXITED(retval))
pam_syslog(pamh, LOG_ERR, "%s %s abnormal exit: 0x%x", path,
user, retval);
else {
retval = WEXITSTATUS(retval);
if (retval)
pam_syslog(pamh, LOG_ERR, "%s %s abnormal exit: %d",
path, user, retval);
}
if (restore)
sigaction(SIGCHLD, &oldsa, NULL);
}
|