forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tor_autoservice.c
238 lines (205 loc) · 6.67 KB
/
tor_autoservice.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 <arpa/inet.h>
#include <assert.h>
#include <ccan/err/err.h>
#include <ccan/io/io.h>
#include <ccan/rbuf/rbuf.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/str/hex/hex.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/str/str.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <common/wireaddr.h>
#include <connectd/tor_autoservice.h>
#include <errno.h>
#include <fcntl.h>
#include <lightningd/log.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <wire/wire.h>
#define MAX_TOR_COOKIE_LEN 32
#define MAX_TOR_SERVICE_READBUFFER_LEN 255
#define MAX_TOR_ONION_V2_ADDR_LEN 16
#define MAX_TOR_ONION_V3_ADDR_LEN 56
static void *buf_resize(struct membuf *mb, void *buf, size_t len)
{
tal_resize(&buf, len);
return buf;
}
static void tor_send_cmd(struct rbuf *rbuf, const char *cmd)
{
status_io(LOG_IO_OUT, "torcontrol", cmd, strlen(cmd));
if (!write_all(rbuf->fd, cmd, strlen(cmd)))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Writing '%s' to Tor socket", cmd);
status_io(LOG_IO_OUT, "torcontrol", "\r\n", 2);
if (!write_all(rbuf->fd, "\r\n", 2))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Writing CRLF to Tor socket");
}
static char *tor_response_line(struct rbuf *rbuf)
{
char *line;
while ((line = rbuf_read_str(rbuf, '\n')) != NULL) {
status_io(LOG_IO_IN, "torcontrol", line, strlen(line));
/* Weird response */
if (!strstarts(line, "250"))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Tor returned '%s'", line);
/* Last line */
if (strstarts(line, "250 "))
break;
return line + 4;
}
return NULL;
}
static void discard_remaining_response(struct rbuf *rbuf)
{
while (tor_response_line(rbuf));
}
static struct wireaddr *make_onion(const tal_t *ctx,
struct rbuf *rbuf,
const struct wireaddr *local)
{
char *line;
struct wireaddr *onion;
//V3 tor after 3.3.3.aplha FIXME: TODO SAIBATO
//sprintf((char *)reach->buffer,"ADD_ONION NEW:ED25519-V3 Port=9735,127.0.0.1:9735\r\n");
tor_send_cmd(rbuf,
tal_fmt(tmpctx, "ADD_ONION NEW:RSA1024 Port=%d,%s Flags=DiscardPK,Detach",
/* FIXME: We *could* allow user to set Tor port */
DEFAULT_PORT, fmt_wireaddr(tmpctx, local)));
while ((line = tor_response_line(rbuf)) != NULL) {
const char *name;
if (!strstarts(line, "ServiceID="))
continue;
line += strlen("ServiceID=");
/* Strip the trailing CR */
if (strchr(line, '\r'))
*strchr(line, '\r') = '\0';
name = tal_fmt(tmpctx, "%s.onion", line);
onion = tal(ctx, struct wireaddr);
if (!parse_wireaddr(name, onion, DEFAULT_PORT, false, NULL))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Tor gave bad onion name '%s'", name);
discard_remaining_response(rbuf);
return onion;
}
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Tor didn't give us a ServiceID");
}
/* https://gitweb.torproject.org/torspec.git/tree/control-spec.txt:
*
* MidReplyLine = StatusCode "-" ReplyLine
* DataReplyLine = StatusCode "+" ReplyLine CmdData
* EndReplyLine = StatusCode SP ReplyLine
* ReplyLine = [ReplyText] CRLF
* ReplyText = XXXX
* StatusCode = 3DIGIT
*/
static void negotiate_auth(struct rbuf *rbuf, const char *tor_password)
{
char *line;
char *cookiefile = NULL;
int cookiefileerrno = 0;
tor_send_cmd(rbuf, "PROTOCOLINFO 1");
while ((line = tor_response_line(rbuf)) != NULL) {
const char *p;
if (!strstarts(line, "AUTH METHODS="))
continue;
if (strstr(line, "NULL")) {
discard_remaining_response(rbuf);
tor_send_cmd(rbuf, "AUTHENTICATE");
discard_remaining_response(rbuf);
return;
} else if (strstr(line, "HASHEDPASSWORD")
&& strlen(tor_password)) {
discard_remaining_response(rbuf);
tor_send_cmd(rbuf,
tal_fmt(tmpctx, "AUTHENTICATE \"%s\"",
tor_password));
discard_remaining_response(rbuf);
return;
} else if ((p = strstr(line, "COOKIEFILE=\"")) != NULL) {
char *contents, *end;
p += strlen("COOKIEFILE=\"");
end = strstr(p, "\"");
if (!end)
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Tor protocolinfo bad line '%s'",
line);
*end = '\0';
/* If we can't access this, try other methods */
cookiefile = tal_strdup(tmpctx, p);
contents = grab_file(tmpctx, p);
if (!contents) {
cookiefileerrno = errno;
continue;
}
assert(tal_count(contents) != 0);
discard_remaining_response(rbuf);
tor_send_cmd(rbuf,
tal_fmt(tmpctx, "AUTHENTICATE %s",
tal_hexstr(tmpctx,
contents,
tal_count(contents)-1)));
discard_remaining_response(rbuf);
return;
}
}
/* Now report if we tried cookie file and it failed */
if (cookiefile)
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Could not open Tor cookie file '%s': %s",
cookiefile, strerror(cookiefileerrno));
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Tor protocolinfo did not give auth");
}
/* We need to have a bound address we can tell Tor to connect to */
static const struct wireaddr *
find_local_address(const struct wireaddr_internal *bindings)
{
for (size_t i = 0; i < tal_count(bindings); i++) {
if (bindings[i].itype != ADDR_INTERNAL_WIREADDR)
continue;
if (bindings[i].u.wireaddr.type != ADDR_TYPE_IPV4
&& bindings[i].u.wireaddr.type != ADDR_TYPE_IPV6)
continue;
return &bindings[i].u.wireaddr;
}
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"No local address found to tell Tor to connect to");
}
struct wireaddr *tor_autoservice(const tal_t *ctx,
const struct wireaddr *tor_serviceaddr,
const char *tor_password,
const struct wireaddr_internal *bindings)
{
int fd;
const struct wireaddr *laddr;
struct wireaddr *onion;
struct addrinfo *ai_tor;
struct rbuf rbuf;
char *buffer;
laddr = find_local_address(bindings);
ai_tor = wireaddr_to_addrinfo(tmpctx, tor_serviceaddr);
fd = socket(ai_tor->ai_family, SOCK_STREAM, 0);
if (fd < 0)
err(1, "Creating stream socket for Tor");
if (connect(fd, ai_tor->ai_addr, ai_tor->ai_addrlen) != 0)
err(1, "Connecting stream socket to Tor service");
buffer = tal_arr(tmpctx, char, rbuf_good_size(fd));
rbuf_init(&rbuf, fd, buffer, tal_count(buffer), buf_resize);
negotiate_auth(&rbuf, tor_password);
onion = make_onion(ctx, &rbuf, laddr);
/*on the other hand we can stay connected until ln finish to keep onion alive and then vanish */
//because when we run with Detach flag as we now do every start of LN creates a new addr while the old
//stays valid until reboot this might not be desired so we can also drop Detach and use the
//read_partial to keep it open until LN drops
//FIXME: SAIBATO we might not want to close this conn
close(fd);
return onion;
}