-
Notifications
You must be signed in to change notification settings - Fork 55
/
exedir.c
167 lines (130 loc) · 3.19 KB
/
exedir.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
#define _GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "common.h"
#include "exedir.h"
#define RAPPEL_DIR "rappel"
extern struct options_t options;
static const
int _reopen_ro(
const int h,
const char *const path)
{
REQUIRE (close(h) == 0);
const int ro_h = open(path, O_RDONLY | O_CLOEXEC);
REQUIRE (ro_h >= 0);
return ro_h;
}
static
void _clean_rappel_dir(void)
{
char path[PATH_MAX] = { 0 };
int ret = snprintf(path, sizeof(path), "%s/exe", options.rappel_dir);
if (ret < 0) {
fprintf(stderr, "Path exceeds max path length: %s/exe", options.rappel_dir);
exit(EXIT_FAILURE);
}
DIR *exedir = opendir(path);
REQUIRE (exedir != NULL);
struct dirent *f;
while ((f = readdir(exedir))) {
if (!strcmp(f->d_name, ".") || !strcmp(f->d_name, ".."))
continue;
if (!strcmp(f->d_name, "history"))
continue;
ret = snprintf(path, sizeof(path), "%s/exe/%s", options.rappel_dir, f->d_name);
if (ret < 0) {
fprintf(stderr, "Path exceeds max path length: %s/exe/%s", options.rappel_dir, f->d_name);
exit(EXIT_FAILURE);
}
if (unlink(path) == -1)
fprintf(stderr, "Cannot unlink %s: %s\n", f->d_name, strerror(errno));
}
REQUIRE (closedir(exedir) == 0);
}
void init_rappel_dir(void)
{
const char *home = getenv("HOME");
const char *xdg_data_home = getenv("XDG_DATA_HOME");
if (!home) {
fprintf(stderr, "HOME not set");
exit(EXIT_FAILURE);
}
if (!xdg_data_home) {
snprintf(
options.rappel_dir,
sizeof(options.rappel_dir),
"%s/.%s",
home,
RAPPEL_DIR
);
} else {
snprintf(
options.rappel_dir,
sizeof(options.rappel_dir),
"%s/%s",
xdg_data_home,
RAPPEL_DIR
);
}
if (mkdir(options.rappel_dir, 0755) == -1)
REQUIRE (errno == EEXIST);
char path[PATH_MAX] = { 0 };
int ret = snprintf(path, sizeof(path), "%s/exe", options.rappel_dir);
if (ret < 0) {
fprintf(stderr, "Path exceeds max path length: %s/exe", options.rappel_dir);
exit(EXIT_FAILURE);
}
if (mkdir(path, 0755) == -1)
REQUIRE (errno == EEXIST);
_clean_rappel_dir();
}
const
int write_exe(
const uint8_t *data,
const size_t data_sz,
const char *name)
{
if (name)
return write_named_file(data, data_sz, name);
else
return write_tmp_file(data, data_sz);
}
const
int write_named_file(
const uint8_t *data,
const size_t data_sz,
const char *name)
{
const int h = open(name, O_WRONLY | O_CREAT,
S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
REQUIRE (h >= 0);
write_data(h, data, data_sz);
return _reopen_ro(h, name);
}
const
int write_tmp_file(
const uint8_t *data,
const size_t data_sz)
{
char path[PATH_MAX] = { 0 };
int ret = snprintf(path, sizeof(path), "%s/exe/rappel-exe.XXXXXX", options.rappel_dir);
if (ret < 0) {
fprintf(stderr, "Path exceeds max path length: %s/exe/rappel-exe.XXXXXX", options.rappel_dir);
exit(EXIT_FAILURE);
}
const int h = mkstemp(path);
REQUIRE (h >= 0);
write_data(h, data, data_sz);
REQUIRE (fchmod(h, S_IXUSR | S_IRUSR | S_IWUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0);
const int h_ro = _reopen_ro(h, path);
return h_ro;
}