forked from FredrikAleksander/pam_rundir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pam_rundir.c
311 lines (266 loc) · 7.44 KB
/
pam_rundir.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* pam_rundir - Copyright (C) 2015 Olivier Brunel
*
* pam_rundir.c
* Copyright (C) 2015 Olivier Brunel <[email protected]>
*
* 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. If not, see http://www.gnu.org/licenses/
*/
#include "config.h"
#include <dirent.h>
#include <errno.h>
#include <linux/securebits.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <utmp.h>
#define PAM_SM_SESSION
#include <security/pam_modules.h>
#define FLAG_NAME "pam_rundir_has_counted"
static int
array_pos (char *id, char *ttys_ids[], int len)
{
for (int i = 0; i < len; ++i)
{
if (strcmp (ttys_ids[i], "") == 0)
{
ttys_ids[i] = strdup(id);
return i;
}
if (strcmp (ttys_ids[i], id) == 0)
return i;
}
return -1;
}
static int
ensure_parent_dir (void)
{
int r = 1;
mode_t um = umask (S_IWOTH);
if (mkdir (PARENT_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0
&& errno != EEXIST)
r = 0;
umask (um);
return r;
}
static int
get_number_of_ttys (void)
{
DIR *dir;
struct dirent *ent;
int count = 0;
if ((dir = opendir ("/dev")) != NULL)
{
while ((ent = readdir (dir)) != NULL)
{
if (strncmp ("tty", ent->d_name, 3) == 0)
++count;
}
}
return count;
}
static int
intlen (int n)
{
int l;
for (l = 1; ; ++l)
{
if (n < 10)
break;
n /= 10;
}
return l;
}
static int
rmrf (const char *path)
{
int r = 0;
DIR *dir;
struct dirent *dp;
int lp;
if (unlink (path) == 0)
return 0;
else if (errno != EISDIR)
return -1;
dir = opendir (path);
if (!dir)
return -1;
lp = strlen (path);
for (dp = readdir (dir); dp != NULL; dp = readdir (dir))
{
if (strcmp (dp->d_name, ".") != 0 && strcmp (dp->d_name, "..") != 0)
{
int l = lp + strlen (dp->d_name) + 2;
char name[l];
snprintf(name, l, "%s/%s", path, dp->d_name);
r += rmrf (name);
}
}
closedir (dir);
if (rmdir (path) < 0)
--r;
return r;
}
static int
user_has_session (const char *user)
{
int ttys = get_number_of_ttys ();
char *ttys_ids[ttys];
int ttys_login[ttys];
for (int i = 0; i < ttys; ++i) {
ttys_ids[i] = "";
ttys_login[i] = 0;
}
/* Loop through all the utmp entries.
* Store the id in the char array.
* Update the login state at every iteration. */
setutent();
struct utmp *utmp_entry = getutent();
while (utmp_entry)
{
if (utmp_entry->ut_type == LOGIN_PROCESS)
{
// This is a logout. The user is unknown.
int pos = array_pos (utmp_entry->ut_id, ttys_ids, ttys);
ttys_login[pos] = 0;
} else if (utmp_entry->ut_type == USER_PROCESS)
{
// This is a login.
if (strcmp (utmp_entry->ut_user, user) == 0)
{
int pos = array_pos (utmp_entry->ut_id, ttys_ids, ttys);
ttys_login[pos] = 1;
}
}
utmp_entry = getutent();
}
for (int i = 0; i < ttys; ++i)
{
if (strcmp (ttys_ids[i], ""))
free(ttys_ids[i]);
}
int logins = 0;
for (int i = 0; i < ttys; ++i)
{
if (ttys_login[i] == 1)
++logins;
}
/* If there is only one login, then we know the user has
* logged out. If somehow it is under 1, just assume the
* user has no sessions. */
if (logins <= 1)
{
return 0;
} else {
return 1;
}
}
PAM_EXTERN int
pam_sm_close_session (pam_handle_t *pamh, int flags, int argc, const char **argv)
{
int r;
const char *user;
struct passwd *pw;
int l;
/* did we add to the counter on open_session (i.e. anything to do) ? */
r = pam_get_data (pamh, FLAG_NAME, (const void **) &pw);
if (r != PAM_SUCCESS)
return (r == PAM_NO_MODULE_DATA) ? PAM_SUCCESS : r;
if (geteuid() != 0)
return PAM_SESSION_ERR;
if (!ensure_parent_dir ())
return PAM_SESSION_ERR;
r = pam_get_user (pamh, &user, NULL);
if (r != PAM_SUCCESS)
return PAM_USER_UNKNOWN;
pw = getpwnam (user);
if (!pw)
return PAM_USER_UNKNOWN;
/* get length for uid as ascii string, i.e. in file/folder name */
l = intlen ((int) pw->pw_uid);
/* construct user dir, e.g: "/run/user/1000" */
char dir[sizeof (PARENT_DIR) + l + 2];
snprintf(dir, sizeof (PARENT_DIR) + l + 2, "%s/%d", PARENT_DIR, pw->pw_uid);
if (!user_has_session (user))
r = rmrf (dir);
return (r == 0) ? PAM_SUCCESS : PAM_SESSION_ERR;
}
PAM_EXTERN int
pam_sm_open_session (pam_handle_t *pamh, int flags, int argc, const char **argv)
{
int r;
const char *user;
struct passwd *pw;
int l;
if (geteuid() != 0)
return PAM_SESSION_ERR;
if (!ensure_parent_dir ())
return PAM_SESSION_ERR;
r = pam_get_user (pamh, &user, NULL);
if (r != PAM_SUCCESS)
return PAM_USER_UNKNOWN;
pw = getpwnam (user);
if (!pw)
return PAM_USER_UNKNOWN;
/* get length for uid as ascii string, i.e. in file/folder name */
l = intlen ((int) pw->pw_uid);
/* construct user dir, e.g: "/run/user/1000" */
char dir[sizeof (PARENT_DIR) + l + 2];
snprintf(dir, sizeof (PARENT_DIR) + l + 2, "%s/%d", PARENT_DIR, pw->pw_uid);
/* flag for processing on close_session */
if (pam_set_data (pamh, FLAG_NAME, (void *) 1, NULL) != PAM_SUCCESS)
{
/* well shit... try to revert, though we can't do nothing if it
* fails. A PAM_BUF_ERR (only possible error) should be pretty rare
* though (especially combined with a failure to re-write). */
goto done;
}
/* to bypass permission checks for mkdir, in case it isn't group
* writable */
int secbits = -1;
secbits = prctl (PR_GET_SECUREBITS);
if (secbits != -1)
prctl (PR_SET_SECUREBITS, (unsigned long) secbits | SECBIT_NO_SETUID_FIXUP);
/* set euid and egid so if we do create the dir, it is owned by the user */
if (seteuid (pw->pw_uid) < 0 || setegid (pw->pw_gid) < 0)
goto done;
if (mkdir (dir, S_IRWXU) == 0 || errno == EEXIST)
{
char buf[sizeof (VAR_NAME) + strlen (dir) + 1];
snprintf(buf, sizeof (VAR_NAME) + strlen (dir) + 1, "%s=%s", VAR_NAME, dir);
pam_putenv (pamh, buf);
}
/* restore */
if (seteuid (0) < 0 || setegid (0) < 0)
goto done;
done:
if (secbits != -1)
prctl (PR_SET_SECUREBITS, (unsigned long) secbits);
return (r == 0) ? PAM_SUCCESS : PAM_SESSION_ERR;
}
#ifdef PAM_STATIC
struct pam_module _pam_rundir_modstruct = {
"pam_rundir",
NULL,
NULL,
NULL,
pam_sm_open_session,
pam_sm_close_session,
NULL
};
#endif