-
Notifications
You must be signed in to change notification settings - Fork 1
/
npm-agent.c
341 lines (283 loc) · 6.13 KB
/
npm-agent.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#define _DEFAULT_SOURCE
/* npm-agent uses a simple socket protocol to receive and respond to requests:
* a client sends a null-terminated path over a socket, with a maximum length
* PATH_MAX.
* npm-agent responds with a null-terminated password with a max length of
* PASSWORD_MAX_LEN characters.
*/
#include <errno.h>
#include <limits.h>
#include <poll.h>
#include <signal.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#include "common.h"
#include "config.h"
#include "util.h"
#ifndef TIMEOUT
#define TIMEOUT 10000
#endif
#define LISTENER 0
#define TIMER 1
#define CLIENT 2
char *corecmd[] = { NPM_CORE, "-d", NULL, NULL };
bool cached = false;
char inbuf[PATH_MAX];
char master[PASSWORD_MAX_LEN + 2];
ssize_t masterlen;
char *inptr = inbuf;
size_t inlen;
struct pollfd fds[3];
int timerpipe[2];
void
clear_master()
{
cached = false;
explicit_bzero(master, sizeof(master));
masterlen = 0;
}
int
get_master()
{
int stdoutpipe[2], stdinpipe[2], status = 1;
if (pipe(stdoutpipe) == -1) {
perror("failed to create stdout pipe");
return status;
}
if (pipe(stdinpipe) == -1) {
perror("failed to create stdin pipe");
return status;
}
pid_t pid = fork();
switch (pid) {
case -1:
fprintf(stderr, "fork failed\n");
return status;
case 0:
close(stdoutpipe[0]);
dup2(stdoutpipe[1], 1);
close(stdinpipe[1]);
dup2(stdinpipe[0], 0);
if (execvp(getpasscmd[0], getpasscmd) == -1) {
perror("failed to start password retrieval program");
exit(1);
}
default:
close(stdoutpipe[1]);
close(stdinpipe[0]);
close(stdinpipe[1]);
FILE *stdoutfile = fdopen(stdoutpipe[0], "r");
if (!stdoutfile) {
perror("failed to open npm-core stdout as FILE");
goto here;
}
if ((masterlen = get_password(stdoutfile, master)) == -1) {
fprintf(stderr, "failed to read password from pipe\n");
return -1;
}
master[masterlen++] = '\n';
here:
fclose(stdoutfile);
close(stdoutpipe[0]);
waitpid(pid, &status, 0);
}
cached = true;
return status;
}
int
run_core()
{
int stdinpipe[2], status;
if (pipe(stdinpipe) == -1) {
perror("failed to create stdin pipe");
return 1;
}
pid_t pid = fork();
switch (pid) {
case -1:
perror("fork failed");
return 1;
case 0:
close(stdinpipe[1]);
dup2(stdinpipe[0], 0);
dup2(fds[CLIENT].fd, 1);
corecmd[2] = inbuf;
if (execvp(corecmd[0], corecmd) == -1) {
perror("failed to run core");
exit(1);
}
break;
default:
close(stdinpipe[0]);
FILE *stdinfile = fdopen(stdinpipe[1], "w");
if (!stdinfile) {
perror("failed to open npm-core stdin as FILE");
close(stdinpipe[1]);
goto here;
}
if (fwrite(master, 1, masterlen, stdinfile) == -1) {
perror("failed to write master password to pipe");
return 1;
}
fclose(stdinfile);
here:
waitpid(pid, &status, 0);
}
return status;
}
struct itimerval timerspec = {
.it_interval = {0},
.it_value = { TIMEOUT/1000, (TIMEOUT % 1000) * 1000 * 1000 },
};
void
agent()
{
int status;
if (!cached) {
if (get_master() != 0)
return;
if (setitimer(ITIMER_REAL, &timerspec, NULL) == -1) {
perror("failed to set cache timeout");
clear_master();
return;
}
}
// if the password is wrong, we don't cache it
if ((status = run_core()) != 0)
clear_master();
}
bool running = true;
void
handler()
{
running = false;
}
void
alarm_handler()
{
int ret;
while ((ret = write(timerpipe[1], "a", 1)) < 1) {
if (ret == -1) {
perror("failed to write to timerpipe");
break;
}
}
}
int
main()
{
struct sockaddr_un sockaddr = {
.sun_family = AF_UNIX,
.sun_path = SOCKPATH
};
int ret;
struct sigaction sa_term = {
.sa_handler = handler,
};
struct sigaction sa_alarm = {
.sa_handler = alarm_handler,
};
if (mlock(master, sizeof(master)) == -1) {
perror("failed to mlock master buffer");
goto error;
}
if (sigaction(SIGINT, &sa_term, NULL) == -1) {
perror("sigaction(SIGINT)");
goto error;
}
if (sigaction(SIGTERM, &sa_term, NULL) == -1) {
perror("sigaction(SIGTERM)");
goto error;
}
if (sigaction(SIGALRM, &sa_alarm, NULL) == -1) {
perror("sigaction(SIGALRM)");
goto error;
}
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) {
perror("failed to create socket");
goto error_socket;
}
if (bind(sock, (struct sockaddr *) &sockaddr, sizeof(sockaddr)) == -1) {
perror("failed to bind to socket");
goto error_socket;
}
if (listen(sock, 50) == -1) {
perror("failed to set socket to listening");
goto error;
}
if (pipe(timerpipe) == -1) {
perror("failed to create timer pipe");
goto error;
}
fds[LISTENER].fd = sock;
fds[LISTENER].events = POLLIN;
fds[TIMER].fd = timerpipe[0];
fds[TIMER].events = POLLIN;
fds[CLIENT].fd = -1;
while (running) {
if (poll(fds, sizeof(fds) / sizeof(fds[0]), -1) == -1) {
if (errno == EINTR)
continue;
perror("poll failed");
goto error;
}
// new connection
if (fds[LISTENER].revents & POLLIN) {
if (fds[CLIENT].fd < 0) {
fds[CLIENT].fd = accept(fds[LISTENER].fd, NULL, NULL);
if (fds[CLIENT].fd == -1)
perror("accept failed");
inptr = inbuf;
inlen = 0;
memset(inbuf, 0, sizeof(inbuf));
fds[CLIENT].events = POLLIN;
}
}
// timer expired
if (fds[TIMER].revents & POLLIN) {
uint64_t val;
read(fds[TIMER].fd, &val, sizeof(val));
clear_master();
}
// incoming data
if (fds[CLIENT].revents & POLLIN) {
ret = read(fds[CLIENT].fd, inptr, sizeof(inbuf) - inlen);
if (ret == -1) {
perror("failed to read from client");
goto error;
}
inlen += ret;
inptr += ret;
// if there is a null, the path is complete
if (memchr(inbuf, 0, inlen)) {
fds[CLIENT].revents &= ~POLLIN;
agent();
close(fds[CLIENT].fd);
fds[CLIENT].fd = -1;
continue;
}
// if the buffer is full without a null, the client is misbehaving,
// so close the connection and clear inbuf
if (inlen == sizeof(inbuf)) {
close(fds[CLIENT].fd);
fds[CLIENT].fd = -1;
}
}
}
error:
unlink(SOCKPATH);
error_socket:
close(sock);
clear_master();
return 1;
}