-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregopen.c
237 lines (215 loc) · 4.23 KB
/
regopen.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
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
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <libgen.h>
#include <limits.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char *argv0;
char *shell = "/bin/sh";
void defconfig(char buf[PATH_MAX]);
char *homedir(void);
int matches(char *item, char *regex);
void parse(FILE *f, char *pattern, int child);
void toshell(FILE *src, char *firstline, int spawnchild);
void usage(void);
void writeall(int fd, void *buf, size_t nbytes);
void writecmds(FILE *src, int dest, char *firstline);
#ifndef __OpenBSD__
#define pledge(x, y) (0)
#define unveil(x, y) (0)
#endif /* __OpenBSD__ */
void
usage(void)
{
fprintf(stderr, "usage: %s [-c] [-f configfile] item\n", argv0);
exit(1);
}
int
main(int argc, char *argv[])
{
char config[PATH_MAX];
FILE *f;
int ch;
int spawnchild = 0;
if(pledge("exec stdio proc rpath unveil", NULL) == -1)
err(1, "pledge");
argv0 = basename(argv[0]);
config[0] = '\0';
while((ch = getopt(argc, argv, "cf:")) != -1) {
switch(ch) {
case 'c':
spawnchild = 1;
break;
case 'f':
if(strlen(optarg) >= PATH_MAX)
errx(1, "config path is too long");
strcpy(config, optarg);
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if(argc != 1)
usage();
if(config[0] == '\0')
defconfig(config);
if(unveil(config, "r") == -1)
err(1, "unveil");
if(unveil(shell, "x") == -1)
err(1, "unveil");
if(unveil(NULL, NULL) == -1)
err(1, "unveil");
if((f = fopen(config, "r")) == NULL)
err(1, "could not open config file");
if(pledge("exec stdio proc", NULL) == -1)
err(1, "pledge");
parse(f, argv[0], spawnchild);
}
void
defconfig(char buf[PATH_MAX])
{
int ret;
ret = snprintf(buf, PATH_MAX, "%s/%s", homedir(), ".regopen");
if (ret < 0 || (size_t)ret >= PATH_MAX)
errx(1, "config path is too long");
}
char *
homedir(void)
{
char *hd;
if((hd = getenv("HOME")) != NULL)
return hd;
errx(1, "could not get home directory");
}
void
parse(FILE *f, char *item, int child)
{
enum State {
WAITMATCH,
WAITPROG
};
enum State state;
char *line = NULL;
size_t linesize = 0;
size_t linelen;
state = WAITMATCH;
/* get first matching string */
for(;;) {
linelen = getline(&line, &linesize, f);
if(linelen == -1) {
if(errno)
err(1, "error reading config");
else
exit(2); /* no matching rule/action */
}
line[linelen - 1] = '\0';
if(line[0] == '#')
continue;
if(state == WAITMATCH) {
if(line[0] == '\0' || line[0] == '\t')
continue;
if(matches(item, line)) {
state = WAITPROG;
continue;
}
} else {
if(line[0] == '\t') {
setenv("item", item, 1);
toshell(f, line, child);
}
}
}
/* NOT REACHED */
}
int
matches(char *item, char *pattern)
{
regex_t re;
regmatch_t match;
int fail;
fail = regcomp(&re, pattern, REG_EXTENDED);
if(fail)
errx(1, "regex compiling error");
fail = regexec(&re, item, 1, &match, 0);
if(fail && fail != REG_NOMATCH)
errx(1, "regex matching error");
regfree(&re);
if(fail || match.rm_so != 0 || match.rm_eo != strlen(item))
return 0;
else
return 1;
}
void
toshell(FILE *src, char *firstline, int spawnchild)
{
int pfd[2];
pid_t pid;
if(pipe(pfd) != 0)
errx(1, "pipe");
pid = fork();
if(pid == -1)
err(1, "fork");
if(spawnchild) {
if(pid == 0)
goto exec;
else
goto print;
}
else {
if(pid == 0)
goto print;
else
goto exec;
}
exec:
close(pfd[1]);
dup2(pfd[0], 0);
close(pfd[0]);
execl(shell, shell, NULL);
err(1, "exec");
print:
if(pledge("stdio", NULL) == -1)
err(1, "pledge");
close(pfd[0]);
writecmds(src, pfd[1], firstline);
close(pfd[1]);
exit(0);
}
void
writeall(int fd, void *buf, size_t nbytes)
{
size_t off;
ssize_t nw;
for(off = 0; off < nbytes; off += nw) {
nw = write(fd, (char *)buf + off, nbytes - off);
if(nw == -1)
err(1, "write");
if(nw == 0)
errx(1, "write: nothing written");
}
}
void
writecmds(FILE *src, int dest, char *firstline)
{
char *line = NULL;
size_t linesize = 0;
size_t linelen;
writeall(dest, firstline, strlen(firstline));
writeall(dest, "\n", 1);
for(;;) {
linelen = getline(&line, &linesize, src);
if(line[0] == '#')
continue;
if(linelen == -1 || line[0] != '\t')
break;
writeall(dest, line, linelen);
}
free(firstline);
free(line);
}