-
Notifications
You must be signed in to change notification settings - Fork 1
/
npmc.c
68 lines (55 loc) · 1.28 KB
/
npmc.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
#define _DEFAULT_SOURCE
#include <limits.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
#include "common.h"
#include "config.h"
#include "util.h"
char answer[PASSWORD_MAX_LEN + 1];
int answerlen = 0;
char abspath[PATH_MAX];
int
main(int argc, char *argv[])
{
const struct sockaddr_un sockaddr = {
.sun_family = AF_UNIX,
.sun_path = SOCKPATH
};
if (argc != 2)
return 1;
if (realpath(argv[1], abspath) == NULL) {
fprintf(stderr, "failed to get absolute path of %s\n", argv[1]);
goto end;
}
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) {
fprintf(stderr, "failed to create socket: %s\n", strerror(errno));
goto end;
}
if (connect(sock, (struct sockaddr *) &sockaddr, sizeof(sockaddr)) == -1) {
fprintf(stderr, "failed to connect to socket: %s\n", strerror(errno));
close(sock);
goto end;
}
xwrite(sock, abspath, strlen(abspath) + 1); // include terminator
FILE *sockfile = fdopen(sock, "rw");
if (!sockfile) {
perror("failed to open socket as FILE");
close(sock);
goto end;
}
get_password(sockfile, answer);
if (*answer)
printf("%s", answer);
closesock:
fclose(sockfile);
end:
return !(*answer);
}