-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxssd.c
More file actions
332 lines (280 loc) · 8.51 KB
/
Copy pathxssd.c
File metadata and controls
332 lines (280 loc) · 8.51 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
xssd - extremely simple sudo
This is a replacement for sudo. It is intended to be extremely
simple and therefore easy to verify for correctness.
Usage: xssd user command arguments
xssd then looks up the configuration file
/etc/xssd/<user>/<command>, which contains lines in
Keyword: Value format, or comments starting with "#".
Currently defined keywords are:
Command: The command which is really executed. Must be
absolute path.
User: A user which is allowed to execute this command.
Group: A group which is allowed to execute this command.
Env: An environment variable which is passed through to the
command. All other environment variables are discarded.
Ideas for enhancements: Configurable logging, initializing env
variables.
*/
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#ifndef LOG_PERROR
#define LOG_PERROR 0
#endif
char xssd_c_cvs_version[] =
"$Id: xssd.c,v 1.8 2003-11-06 18:19:14 hjp Exp $";
char *cmnd;
static void usage(void) {
fprintf(stderr, "Usage: %s [-q] user command arguments\n", cmnd);
exit(1);
}
/*
Skip whitespace in a string.
initial: initial offset
line: the string to be scanned
lp: (line pointer) Points to first non-ws character on exit.
*/
#define skip_ws(line, lp, initial) \
for ((lp) = (initial); \
isspace((unsigned char)(line)[(lp)]); \
(lp)++);
static void clean_fds(void) {
/*
* Make sure stdin/out/err are open.
* On Linux and some BSDs the kernel already ensures that for setuid
* programs, but on most other UNIXes don't.
*
* xssd itself isn't exploitable (at worst, an attacker could
* redirect stderr to the syslog), but the scripts started by xssd
* might be.
*
* We could also close all fds > 2 here, but that might prevent some
* legitimate uses, so we delegate that responsibilitly to the
* called programs.
*/
int fd;
do {
fd = open("/dev/null", O_RDONLY);
if (fd == -1) exit(1); /* tough luck :-) */
} while (fd <= 2);
close(fd);
}
int main(int argc, char **argv) {
FILE *fp;
char cfgfile[128];
char line[1024];
int linenr = 0;
int grant = 0;
char **env = NULL;
size_t env_a = 0;
size_t env_i = 0;
char *command = NULL;
char *user;
struct passwd *pw;
int log_opts = LOG_PID | LOG_PERROR;
int c;
clean_fds();
cmnd = argv[0];
while ((c = getopt(argc, argv, "q")) != EOF) {
switch(c) {
case 'q':
log_opts &= !LOG_PERROR;
break;
case '?':
usage();
default:
assert(0);
}
}
if (argc - optind < 2) usage();
user = argv[optind++];
openlog("xssd", log_opts, LOG_AUTH);
/* a / in either the username or the command name could be used to
* break out of /etc/xssd. Of course the user would actually have to
* exist to do harm, but better safe than sorry.
*/
if (strchr(user, '/')) {
syslog(LOG_ERR, "invalid username %s. [Ruid: %d]",
user, getuid());
exit(1);
}
if (strchr(argv[optind], '/')) {
syslog(LOG_ERR, "invalid command name %s. [Ruid: %d]",
argv[optind], getuid());
exit(1);
}
snprintf(cfgfile, sizeof(cfgfile), "/etc/xssd/%s/%s",
user, argv[optind]); /* check for return value unnecessary, because
fopen below will fail if no config file
exists
*/
if ((fp = fopen(cfgfile, "r")) == NULL) {
syslog(LOG_ERR, "%s: fopen failed: %s. [Ruid: %d]",
cfgfile, strerror(errno), getuid());
exit(1);
}
while (fgets(line, sizeof(line), fp)) {
int lp;
linenr++;
/* remove trailing white space */
lp = strlen(line)-1;
if (line[lp] != '\n') {
syslog(LOG_ERR, "config file %s[%d]: Line unterminated or too long. [Ruid: %d]",
cfgfile, linenr, getuid());
exit(1);
}
while (lp >= 0 && isspace((unsigned char)line[lp])) {
line[lp--] = '\0';
}
/* Check for keywords.
*/
if (*line == '#') {
continue;
}
if (strncmp(line, "Command:", strlen("Command:") ) == 0) {
int lp;
skip_ws(line, lp, strlen("Command:"));
command = strdup(line + lp);
continue;
}
if (strncmp(line, "User:", strlen("User:") ) == 0) {
int lp;
struct passwd *pw;
skip_ws(line, lp, strlen("User:"));
pw = getpwnam(line + lp);
if (pw && pw->pw_uid == getuid()) {
grant = 1;
}
continue;
}
if (strncmp(line, "Group:", strlen("Group:") ) == 0) {
int lp;
struct group *gr;
skip_ws(line, lp, strlen("Group:"));
gr = getgrnam(line + lp);
if (gr) {
if (gr->gr_gid == getgid()) {
grant = 1;
} else {
gid_t groups[NGROUPS_MAX] = {0};
int ngroups = getgroups(NGROUPS_MAX, groups);
int i;
for (i = 0; i < ngroups; i++) {
if (gr->gr_gid == groups[i]) {
grant = 1;
}
}
}
}
continue;
}
if (strncmp(line, "Env:", strlen("Env:") ) == 0) {
char *s;
int lp;
skip_ws(line, lp, strlen("Env:"));
fprintf(stderr, "Env: %s\n", line + lp);
s = getenv(line + lp);
if (s) {
if (env_i >= env_a) {
env_a = env_a * 3 / 2 + 10;
if ((env = realloc(env, env_a * sizeof(*env))) == NULL) {
syslog(LOG_ERR, "config file %s[%d]: cannot realloc env to %ld strings: %s. [Ruid: %d]",
cfgfile, linenr, (long)env_a, strerror(errno), getuid());
exit(1);
}
}
if ((env[env_i] = malloc(strlen(line + lp) + 1 + strlen(s) + 1)) == NULL) {
syslog(LOG_ERR, "config file %s[%d]: cannot strdup env variable %s: %s. [Ruid: %d]",
cfgfile, linenr, line + lp, strerror(errno), getuid());
}
sprintf(env[env_i], "%s=%s", line + lp, s);
env_i++;
}
continue;
}
/* if we get here, we have an unknown keyword or mistyped or ...
*/
fprintf(stderr, "Do not know what to do with \"%s\"\n", line);
exit(1);
}
fclose(fp);
if (env_i >= env_a) {
env_a = env_a * 3 / 2 + 10;
if ((env = realloc(env, env_a * sizeof(*env))) == NULL) {
syslog(LOG_ERR, "config file %s[%d]: cannot realloc env to %ld strings: %s. [Ruid: %d]",
cfgfile, linenr, (long)env_a, strerror(errno), getuid());
exit(1);
}
}
env[env_i] = NULL;
if (grant) {
syslog(LOG_INFO, "%s: access granted. [Ruid: %d]", cfgfile, getuid());
} else {
syslog(LOG_NOTICE, "%s: access denied. [Ruid: %d]", cfgfile, getuid());
exit(1);
}
pw = getpwnam (user);
if (pw == NULL) {
syslog(LOG_ERR, "%s: unknown target user %s. [Ruid: %d]",
cfgfile, user, getuid());
exit(1);
}
if (initgroups(user, pw->pw_gid) == -1) {
syslog(LOG_ERR, "%s: initgroups(%s, %d) failed: %s. [Ruid: %d]",
cfgfile, user, pw->pw_gid, strerror(errno), getuid());
exit(1);
}
if (setgid(pw->pw_gid) == -1) {
syslog(LOG_ERR, "%s: setgid(%d) failed: %s. [Ruid: %d]",
cfgfile, (int)pw->pw_gid, strerror(errno), getuid());
exit(1);
}
if (setuid(pw->pw_uid) == -1) {
syslog(LOG_ERR, "%s: setuid(%d) failed: %s. [Ruid: %d]",
cfgfile, (int)pw->pw_uid, strerror(errno), getuid());
exit(1);
}
syslog(LOG_INFO, "%s: execing %s. [Ruid: %d]",
cfgfile, command, getuid());
execve(command, argv + optind, env);
syslog(LOG_ERR, "%s: execve(%s) failed: %s. [Ruid: %d]",
cfgfile, command, strerror(errno), getuid());
return 1;
}
/*
$Log: xssd.c,v $
Revision 1.8 2003-11-06 18:19:14 hjp
HP-UX: fixed check for gcc, Use LOG_PERROR only if defined.
Revision 1.7 2002/11/29 11:59:57 hjp
Added -q option
Revision 1.6 2002/04/26 15:17:26 hjp
Close config file after use.
Make sure stdin/out/err are open.
Revision 1.5 2002/01/26 23:20:25 hjp
Don't allow / in command and user name to prevent /../ attack.
Exit on failure to set user or group ids.
(Thanks to Günther Leber for reporting these problems)
Fixed log level on successful execution.
CVS ----------------------------------------------------------------------
Revision 1.4 2001/11/19 08:23:06 hjp
Croak on unknown keywords. Made Comments explicit.
Thanks to Bernd Petrovitsch for the patch.
Revision 1.3 2001/11/12 20:39:58 hjp
Outch. We should really deny access not only say that we do.
Revision 1.2 2001/11/12 20:22:55 hjp
Removed wrong argument from syslog (Shouldn't gcc know that syslog
uses a printf-like format string?)
Revision 1.1 2001/11/12 10:24:55 hjp
Pre-Release
*/