-
Notifications
You must be signed in to change notification settings - Fork 1
/
skt-server.c
239 lines (183 loc) · 5.3 KB
/
skt-server.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
#include "util_qr/qr_code.h"
#include "util_tsl_server/tsl_server.h"
#include "util_network_info/network_info.h"
#include "util_gpg/gpg_session.h"
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
#include <errno.h>
#define PORT 5556 /* listen to 5556 port */
int server_fd;
void open_server() {
char urlbuf[1024];
const char schema[] = "OPGPSKT";
urlbuf[sizeof(urlbuf)-1] = 0;
char pskhex[PSK_BYTES*2 + 1];
struct network_info info;
server_create(pskhex, sizeof(pskhex));
get_info(&info);
printf("%s - %s %ld %d %ld\n", info.ssid, info.ip, strlen(pskhex), PSK_BYTES, sizeof(pskhex));
snprintf(urlbuf, sizeof(urlbuf)-1, "%s:%s/%d/%s%s%s", schema, info.ip, PORT, pskhex, "/SSID:", info.ssid);
create_and_print_qr(urlbuf, stdout);
free(info.ssid);
free(info.ip);
server_fd = server_bind(PORT);
}
gpgme_key_t *list_of_keys = NULL;
size_t number_of_keys;
void update_and_print_keys(gpgme_ctx_t *ctx) {
if (list_of_keys != NULL) {
gpgsession_free_secret_keys(&list_of_keys, number_of_keys);
}
gpgsession_gather_secret_keys(ctx, &list_of_keys, &number_of_keys);
printf("Select a key to share:\n");
for (size_t c = 0; c < number_of_keys; c++) {
printf("[%ld] key %s\n", c, list_of_keys[c]->fpr);
}
}
int send_key(gpgme_ctx_t * const ctx, gpgme_key_t key, const int fd) {
int rc = 0;
gpgme_error_t gerr = 0;
gpgme_export_mode_t mode = GPGME_EXPORT_MODE_MINIMAL | GPGME_EXPORT_MODE_SECRET;
char *pattern = NULL;
gpgme_data_t data = NULL;
rc = asprintf(&pattern, "0x%s", key->fpr);
if (rc == -1) {
fprintf(stderr, "failed to malloc appropriately!\n");
return -1;
}
/* create buffer for data exchange with gpgme*/
gerr = gpgme_data_new(&data);
if(gerr) {
fprintf(stderr, "failed to init data buffer: (%d) %s\n", gerr, gpgme_strerror(gerr));
return -1;
}
ssize_t read_bytes;
/* FIXME: blocking! */
gerr = gpgme_op_export(*ctx, pattern, mode, data);
free(pattern);
read_bytes = gpgme_data_seek (data, 0, SEEK_END);
if(read_bytes == -1) {
printf("data-seek-err: %s\n", gpgme_strerror(errno));
}
read_bytes = gpgme_data_seek (data, 0, SEEK_SET);
if (gerr) {
gpgme_data_release(data);
fprintf(stderr, "failed to export key: (%d) %s\n", gerr, gpgme_strerror(gerr));
return -1;
}
/* write keys to stderr */
char buf[1000];
while ((read_bytes = gpgme_data_read (data, buf, sizeof(buf))) > 0) {
ssize_t written = client_write(fd, buf, read_bytes); /* FIXME: blocking */
if (written != read_bytes) {
fprintf(stderr, "failed to wite key\n");
gpgme_data_release(data);
return -1;
}
}
if (read_bytes < 0) {
fprintf(stderr, "failed to read key: (%d) %s\n", gerr, gpgme_strerror(gerr));
}
gpgme_data_release(data);
return 0;
}
void loop() {
fd_set rfds;
struct timeval tv;
int retval;
int client_fd = -1;
int is_running = 1;
gpgme_ctx_t ctx;
if (gpgsession_new(&ctx, false) != 0) {
fprintf(stderr, "failed to generate gpg session\n");
return;
}
while (is_running) {
/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;
/* Watch server to see when it has input. */
FD_ZERO(&rfds);
if (server_fd == -1) {
printf("Impossible to bind the server port\n");
exit(-1);
}
FD_SET(server_fd, &rfds);
/* Watch stdin (fd 0) to see when it has input. */
FD_SET(STDIN_FILENO, &rfds);
/* Watch client to see when it has input. */
if (client_fd != -1) {
//set back
FD_SET(client_fd, &rfds);
}
retval = select(FD_SETSIZE, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */
if (retval == -1){
perror("select()");
is_running = 0;
}else if (retval){
//listen for user input
if (FD_ISSET(STDIN_FILENO, &rfds)) {
static char line[256];
if(fgets(line, sizeof line, stdin) != NULL) {
char *end;
uintmax_t num = strtoumax(line, &end, 10);
if (errno == ERANGE || *end != '\n'){
printf("Invalid command\n");
}else if (num >= number_of_keys) {
printf("Invalid selection\n");
}else if(client_fd != -1) {
printf("Sending key %s\n", list_of_keys[num]->fpr);
int err = send_key(&ctx, list_of_keys[num], client_fd);
if (!err) {
printf("Sent\n");
}else{
printf("Error\n");
}
}
}
}
//listen for new connection
if (FD_ISSET(server_fd, &rfds)) {
//only one connection at time
if (client_fd != -1){
printf(" - forcing client disconnect for a new client\n");
client_close(client_fd);
client_fd = -1;
}
client_fd = server_accept();
printf(" - client connected\n");
update_and_print_keys(&ctx);
}
//if client connected, check for input
if (client_fd != -1 && FD_ISSET(client_fd, &rfds)){
uint8_t buff[100];
int ris;
do{
ris = client_update( client_fd, buff, sizeof(buff) );
if (ris == -1) {
client_fd = -1;
printf(" - client disconnected\n");
}else{
if ( gpgsession_add_data(&ctx, (const char * const)buff, ris ) ) { // we imported a new key
printf(" - client sent a key\n");
update_and_print_keys(&ctx);
}
}
}while(ris > 0);
}
}else{
//printf("No data within five seconds.\n");
}
}
}
int main(void) {
open_server();
loop();
printf("server closing\n");
server_close();
return 0;
}