-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxsec.c
132 lines (102 loc) · 2.61 KB
/
xsec.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
/*
* Copyright Tian Hao <[email protected]>
* License: GPLv3
* It is an opensource (free) software
*/
#define _POSIX_C_SOURCE 200809L
#define _XOPEN_SOURCE 700
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <limits.h>
#include <errno.h>
#include <pwd.h>
#include "tools.h"
extern int killxsvr();
#ifdef CONFIG_ENABLE_XSEC
static int setenv_XAUTHORITY(const char *xauth_file, const char* home)
{
if (xauth_file) {
mydm_print("Xauthority file name: %s\n", xauth_file);
return setenv("XAUTHORITY", xauth_file, 1);
}
char filename[PATH_MAX + 1];
snprintf(filename, PATH_MAX, "%s/.Xauthority", home);
mydm_print("Xauthority file name: %s\n", filename);
return setenv("XAUTHORITY", filename, 1);
}
static struct passwd *getpwnamuid(const char *username)
{
if (username) {
return getpwnam(username);
} else {
return getpwuid(getuid());
}
}
int xauth_magic_cookie_prepare_filename(const char *username, const char *xauth_file)
{
struct passwd *pwd;
if ((pwd = getpwnamuid(username)) == NULL)
return -1;
return setenv_XAUTHORITY(xauth_file, pwd->pw_dir);
}
int xauth_magic_cookie_gen(const char *display, const char *username)
{
pid_t pid;
int status;
struct passwd *pwd;
char *filename;
if ((pwd = getpwnamuid(username)) == NULL)
return -1;
if ((filename = getenv("XAUTHORITY")) == NULL)
return -1;
block_signal(SIGCHLD);
if ((pid = fork()) < 0)
err_quit("fork");
if (pid == 0) {
unblock_signal(SIGCHLD);
my_signal(SIGTTIN, SIG_IGN, 1);
my_signal(SIGTTOU, SIG_IGN, 1);
execlp("xauth", "xauth", "generate", display, "MIT-MAGIC-COOKIE-1", NULL);
mydm_print("exec xauth error: %s\n", strerror(errno));
return -1;
}
while (waitpid(pid, &status, 0) != pid) {
if (errno != EINTR) {
return -1;
}
}
unblock_signal(SIGCHLD);
if ((!WIFEXITED(status)) || (WEXITSTATUS(status) != 0)) {
mydm_print("xauth_magic_cookie_gen: xauth exited failed.\n");
return -1;
}
if (chmod(filename, S_IRUSR|S_IWUSR) < 0)
return -1;
if (chown(filename, pwd->pw_uid, pwd->pw_gid) < 0)
return -1;
return 0;
}
#else
void uncompiled_function(const char* name)
{
mydm_print("%s: this feature is not compiled.\n", name);
killxsvr();
}
int xauth_magic_cookie_prepare_filename(const char *username, const char *xauth_file)
{
uncompiled_function("xauth_magic_cookie_prepare_filename");
return 0;
}
int xauth_magic_cookie_gen(const char *display, const char *username)
{
uncompiled_function("xauth_magic_cookie_gen");
return 0;
}
#endif /* CONFIG_ENABLE_XSEC */