/
pam_usertmp.c
235 lines (197 loc) · 5.03 KB
/
pam_usertmp.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
#if 0
set -- gcc -o ${0%%.*}.so -Wall -g -O2 -fPIC -shared $0
echo "$@"
exec "$@"
exit 1
#endif
/*
* Copyright (C) 2012 SUSE Linux Products GmbH
*
* Author: Ludwig Nussel <ludwig.nussel@suse.de>
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <limits.h>
#include <pwd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <security/pam_appl.h>
#include <security/pam_misc.h>
#include <errno.h>
#define PAM_SM_AUTH
#include <security/pam_modules.h>
#ifdef HAVE_GCCVISIBILITY
# define DLLEXPORT __attribute__ ((visibility("default")))
# define DLLLOCAL __attribute__ ((visibility("hidden")))
#else
# define DLLEXPORT
# define DLLLOCAL
#endif
#ifndef PAM_EXTERN
# define PAM_EXTERN
#endif
#define DIMOF(x) (sizeof(x)/sizeof(x[0]))
#ifndef _
#define _(x) (x)
#endif
const char* basepath = "/var/cache/user/";
const char* runbasepath = "/run/user/";
static void parse_args(const char* type, int argc, const char **argv)
{
char file[PATH_MAX] = "/etc/security/pam_usertmp.conf";
int i;
for(i=0; i < argc; ++i)
{
if(!strncmp(argv[i], "file=", 5))
{
strncat(file, argv[i]+5, sizeof(file)-1);
}
}
// TODO
}
static int get_uid(const char* name, uid_t* uid, gid_t* gid)
{
struct passwd pwd;
struct passwd *result;
char *buf;
size_t buflen;
if (!strcmp(name, "root")) {
*uid = *gid = 0;
return 0;
}
buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
if (buflen == -1)
buflen = 16384;
buf = alloca(buflen);
if (!buf)
goto fail;
getpwnam_r(name, &pwd, buf, buflen, &result);
if (!result)
goto fail;
*uid = pwd.pw_uid;
*gid = pwd.pw_gid;
return 0;
fail:
syslog(LOG_WARNING, "%s: failure in %s: %m", __FILE__, __FUNCTION__);
return -1;
}
int install_d(const char* path, mode_t mode, uid_t uid, gid_t gid)
{
if (mkdir(path, mode) && errno != EEXIST) {
syslog(LOG_WARNING, "%s: mkdir(%s) failed: %m", __FILE__, path);
return -1;
}
if (chown(path, uid, gid)) {
syslog(LOG_WARNING, "%s: chown(%s) failed: %m", __FILE__, path);
return -1;
}
return 0;
}
static void sanitize(char* path)
{
char* c;
for (c = path; *c; ++c)
if (*c == '/' || *c == ':')
*c = '_';
}
DLLEXPORT PAM_EXTERN
int pam_sm_open_session(pam_handle_t * pamh, int flags,int argc, const char **argv)
{
char path[PATH_MAX];
char userpath[PATH_MAX];
const void* ptr;
const char* user;
uid_t uid;
uid_t gid;
int ret = PAM_SUCCESS;
mode_t old_umask = umask(0);
parse_args("session", argc, argv);
ret = pam_get_item(pamh, PAM_USER, &ptr);
if(ret != PAM_SUCCESS)
return PAM_IGNORE;
else
user = ptr;
if (get_uid(user, &uid, &gid)) {
ret = PAM_IGNORE;
goto out;
}
if (install_d(basepath, 0755, 0, 0)) {
ret = PAM_IGNORE;
goto out;
}
strcpy(userpath, basepath);
strncat(userpath, user, sizeof(userpath)-strlen(basepath)-1);
// better safe than sorry, substitute potential nasty characters
sanitize(userpath+strlen(basepath));
if (install_d(userpath, 0700, uid, gid)) {
ret = PAM_IGNORE;
goto out;
}
strcpy(path, userpath);
strncat(path, "/tmp", sizeof(path)-strlen(basepath)-1);
if (install_d(path, 0700, uid, gid)) {
ret = PAM_IGNORE;
goto out;
}
ret = pam_misc_setenv(pamh, "TMPDIR", path, 0);
if(ret != PAM_SUCCESS) {
syslog(LOG_WARNING, "%s: failed to set TMPDIR: %m", __FILE__);
}
strcpy(path, userpath);
strncat(path, "/cache", sizeof(path)-strlen(basepath)-1);
if (install_d(path, 0700, uid, gid)) {
ret = PAM_IGNORE;
goto out;
}
ret = pam_misc_setenv(pamh, "XDG_CACHE_HOME", path, 0);
if(ret != PAM_SUCCESS) {
syslog(LOG_WARNING, "%s: failed to set XDG_CACHE_HOME: %m", __FILE__);
}
if (install_d(runbasepath, 0755, 0, 0)) {
ret = PAM_IGNORE;
goto out;
}
strcpy(path, runbasepath);
strncat(path, user, sizeof(path)-strlen(runbasepath)-1);
// better safe than sorry, subsitute potential nasty characters
sanitize(path+strlen(runbasepath));
if (install_d(path, 0700, uid, gid)) {
ret = PAM_IGNORE;
goto out;
}
ret = pam_misc_setenv(pamh, "XDG_RUNTIME_DIR", path, 0);
if(ret != PAM_SUCCESS) {
syslog(LOG_WARNING, "%s: failed to set XDG_RUNTIME_DIR: %m", __FILE__);
}
out:
umask(old_umask);
return ret;
}
DLLEXPORT PAM_EXTERN
int pam_sm_close_session(pam_handle_t * pamh, int flags,int argc, const char **argv)
{
return PAM_SUCCESS;
}