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
|
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include "common.h"
#define BACKLOG 5
struct CacheEntry {
struct CacheEntry* next;
char* entry_name;
char* secret;
};
struct CacheEntry cache;
volatile sig_atomic_t done = 0;
void term(int signum) {
done = 1;
}
char* pass_secret_for_actual(char* entry_name, int* exit_code) {
int link[2];
pid_t pid;
if (pipe(link) == -1) {
die("pipe() failed!");
}
if ((pid = fork()) == -1) {
die("fork() failed!");
}
if (pid == 0) {
dup2 (link[1], STDOUT_FILENO);
close(link[0]);
close(link[1]);
execlp("pass", "pass", entry_name, NULL);
die("execlp() failed!");
} else {
if (close(link[1]) == -1) {
fprintf(stderr, "%s\n", "close() failed!");
}
char* final = malloc(BUF_SIZE);
if (!final) {
die("malloc() failed!");
}
char* buf = malloc(BUF_SIZE);
int nbytes = 0;
while(0 != (nbytes = read(link[0], buf, sizeof(buf)))) {
buf[nbytes] = 0;
if (strlen(final) + strlen(buf) < BUF_SIZE) {
strcat(final, buf);
}
}
if (waitpid(pid, exit_code, 0) == -1) {
fprintf(stderr, "%s\n", "waitpid() failed!");
return NULL;
} else {
return final;
}
}
// TODO
*exit_code = 0;
return "test";
}
char* pass_secret_for(char* entry_name, int* exit_code) {
struct CacheEntry* current = &cache;
while (current->next) {
if (!strcmp(entry_name, current->next->entry_name)) {
// cache hit
*exit_code = 0;
return current->next->secret;
}
current = current->next;
}
// cache miss
char* secret = pass_secret_for_actual(entry_name, exit_code);
if (secret && WIFEXITED(*exit_code) && (WEXITSTATUS(*exit_code) == 0)) {
struct CacheEntry* new_entry = malloc(sizeof(struct CacheEntry));
if (!new_entry) {
die("malloc() failed!");
}
new_entry->next = NULL;
char* new_entry_name = malloc(strlen(entry_name) + 1);
if (!new_entry_name) {
die("malloc() failed!");
}
strcpy(new_entry_name, entry_name);
new_entry->entry_name = new_entry_name;
new_entry->secret = secret;
current->next = new_entry;
return new_entry->secret;
} else {
return secret;
}
}
void destroy_cache(void) {
struct CacheEntry* current = cache.next;
while (current) {
struct CacheEntry* next = current->next;
free(current->entry_name);
free(current->secret);
free(current);
current = next;
}
}
int main(int argc, char *argv[]) {
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = term;
sigaction(SIGTERM, &action, NULL);
struct sockaddr_un addr;
int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sfd == -1) {
die("socket() failed!");
}
char* spath = get_socket_path();
if (strlen(spath) > sizeof(addr.sun_path) - 1) {
die("Server socket path too long!");
}
if (remove(spath) == -1 && errno != ENOENT) {
die("remove() failed!");
}
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, spath, sizeof(addr.sun_path) - 1);
if (bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) {
die("bind() failed!");
}
if (listen(sfd, BACKLOG) == -1) {
die("listen() failed!");
}
printf("Listening at: %s\n", spath);
cache.next = NULL;
cache.entry_name = NULL;
cache.secret = NULL;
char buf[BUF_SIZE];
while (!done) {
int cfd = accept(sfd, NULL, NULL);
printf("Accepted socket fd = %d\n", cfd);
if (read(cfd, buf, BUF_SIZE) > 0) {
int exit_code = 0;
char* secret = pass_secret_for(buf, &exit_code);
if (!secret || !(WIFEXITED(exit_code) && WEXITSTATUS(exit_code) == 0)) {
secret = "";
}
if (write(cfd, secret, strlen(secret) + 1) != strlen(secret) + 1) {
fprintf(stderr, "%s\n", "write() failed!");
}
}
if (close(cfd) == -1) {
fprintf(stderr, "%s\n", "close() failed!");
}
}
close(sfd);
destroy_cache();
free_socket_path(spath);
}
|