File: channel_drop.c

package info (click to toggle)
tinyssh 20250501-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,388 kB
  • sloc: ansic: 20,245; sh: 1,582; python: 1,449; makefile: 913
file content (43 lines) | stat: -rw-r--r-- 1,024 bytes parent folder | download | duplicates (4)
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;
}