-
Notifications
You must be signed in to change notification settings - Fork 79
/
channel_drop.c
43 lines (35 loc) · 1 KB
/
channel_drop.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
/*
20140129
Jan Mojzis
Public domain.
*/
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include "dropuidgid.h"
#include "newenv.h"
#include "channel.h"
int channel_droppriv(char *user, char **shell) {
struct passwd *pw;
char *name;
pw = getpwnam(user);
if (!pw) return 0;
if (isatty(0)) {
name = ttyname(0);
if (!name) return 0;
if (!newenv_env("SSH_TTY", name)) return 0;
/* setowner */
if (chown(name, pw->pw_uid, pw->pw_gid) == -1) return 0;
if (chmod(name, 0600) == -1) return 0;
}
/* drop privileges */
if (!dropuidgid(pw->pw_name, pw->pw_uid, pw->pw_gid)) return 0;
if (chdir(pw->pw_dir) == -1) return 0;
if (!newenv_env("HOME", pw->pw_dir)) return 0;
if (!newenv_env("USER", pw->pw_name)) return 0;
if (!newenv_env("LOGNAME", pw->pw_name)) return 0;
if (!newenv_env("LOGIN", pw->pw_name)) return 0;
if (!newenv_env("SHELL", pw->pw_shell)) return 0;
*shell = pw->pw_shell;
return 1;
}