about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ui/account.go25
-rw-r--r--worker/imap/worker.go81
-rw-r--r--worker/types/messages.go26
3 files changed, 91 insertions, 41 deletions
diff --git a/ui/account.go b/ui/account.go
index 59544d4..0cd61c8 100644
--- a/ui/account.go
+++ b/ui/account.go
@@ -5,8 +5,6 @@ import (
 
 	tb "github.com/nsf/termbox-go"
 
-	"github.com/davecgh/go-spew/spew"
-
 	"git.sr.ht/~sircmpwn/aerc2/config"
 	"git.sr.ht/~sircmpwn/aerc2/worker"
 	"git.sr.ht/~sircmpwn/aerc2/worker/types"
@@ -64,12 +62,25 @@ func (acc *AccountTab) GetChannel() chan types.WorkerMessage {
 	return acc.Worker.GetMessages()
 }
 
+func (acc *AccountTab) postAction(msg types.WorkerMessage) {
+	acc.logger.Printf("-> %T\n", msg)
+	acc.Worker.PostAction(msg)
+}
+
 func (acc *AccountTab) HandleMessage(msg types.WorkerMessage) {
-	switch msg.InResponseTo().(type) {
-	case types.Configure:
-		// Avoid printing passwords
-		acc.logger.Printf("<- %T\n", msg)
+	acc.logger.Printf("<- %T\n", msg)
+	switch msg.(type) {
+	case types.Ack:
+		// no-op
+	case types.ApproveCertificate:
+		// TODO: Ask the user
+		acc.logger.Println("Approving certificate")
+		acc.postAction(types.Ack{
+			Message: types.RespondTo(msg),
+		})
 	default:
-		acc.logger.Printf("<- %s", spew.Sdump(msg))
+		acc.postAction(types.Unsupported{
+			Message: types.RespondTo(msg),
+		})
 	}
 }
diff --git a/worker/imap/worker.go b/worker/imap/worker.go
index d3ef715..ceff34d 100644
--- a/worker/imap/worker.go
+++ b/worker/imap/worker.go
@@ -1,12 +1,13 @@
 package imap
 
 import (
+	"crypto/tls"
+	"crypto/x509"
 	"fmt"
 	"log"
 	"net/url"
 	"strings"
 
-	"github.com/davecgh/go-spew/spew"
 	"github.com/emersion/go-imap"
 	"github.com/emersion/go-imap-idle"
 	"github.com/emersion/go-imap/client"
@@ -54,6 +55,45 @@ func (w *IMAPWorker) PostAction(msg types.WorkerMessage) {
 	w.actions <- msg
 }
 
+func (w *IMAPWorker) postMessage(msg types.WorkerMessage) {
+	w.logger.Printf("=> %T\n", msg)
+	w.messages <- msg
+}
+
+func (w *IMAPWorker) verifyPeerCert(msg types.WorkerMessage) func(
+	rawCerts [][]byte, _ [][]*x509.Certificate) error {
+
+	return func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
+		pool := x509.NewCertPool()
+		for _, rawCert := range rawCerts {
+			cert, err := x509.ParseCertificate(rawCert)
+			if err != nil {
+				return err
+			}
+			pool.AddCert(cert)
+		}
+
+		request := types.ApproveCertificate{
+			Message:  types.RespondTo(msg),
+			CertPool: pool,
+		}
+		w.postMessage(request)
+
+		response := <-w.actions
+		if response.InResponseTo() != request {
+			return fmt.Errorf("Expected UI to answer cert request")
+		}
+		switch response.(type) {
+		case types.Ack:
+			return nil
+		case types.Disconnect:
+			return fmt.Errorf("UI rejected certificate")
+		default:
+			return fmt.Errorf("Expected UI to answer cert request")
+		}
+	}
+}
+
 func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
 	switch msg := msg.(type) {
 	case types.Ping:
@@ -78,12 +118,14 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
 		w.config.scheme = u.Scheme
 		w.config.user = u.User
 	case types.Connect:
-		// TODO: populate TLS config
-
 		var (
 			c   *client.Client
 			err error
 		)
+		tlsConfig := &tls.Config{
+			InsecureSkipVerify:    true,
+			VerifyPeerCertificate: w.verifyPeerCert(&msg),
+		}
 		switch w.config.scheme {
 		case "imap":
 			c, err = client.Dial(w.config.addr)
@@ -92,12 +134,12 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
 			}
 
 			if !w.config.insecure {
-				if err := c.StartTLS(nil); err != nil {
+				if err := c.StartTLS(tlsConfig); err != nil {
 					return err
 				}
 			}
 		case "imaps":
-			c, err = client.DialTLS(w.config.addr, nil)
+			c, err = client.DialTLS(w.config.addr, tlsConfig)
 			if err != nil {
 				return err
 			}
@@ -131,40 +173,27 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
 	return nil
 }
 
-// Logs an action but censors passwords
-func (w *IMAPWorker) logAction(msg types.WorkerMessage) {
-	switch msg := msg.(type) {
-	case types.Configure:
-		src := msg.Config.Source
-		msg.Config.Source = "[obsfucated]"
-		w.logger.Printf("<= %s", spew.Sdump(msg))
-		msg.Config.Source = src
-	default:
-		w.logger.Printf("<= %s", spew.Sdump(msg))
-	}
-}
-
 func (w *IMAPWorker) Run() {
 	for {
 		select {
 		case msg := <-w.actions:
-			w.logAction(msg)
+			w.logger.Printf("<= %T\n", msg)
 			if err := w.handleMessage(msg); err == errUnsupported {
-				w.messages <- types.Unsupported{
+				w.postMessage(types.Unsupported{
 					Message: types.RespondTo(msg),
-				}
+				})
 			} else if err != nil {
-				w.messages <- types.Error{
+				w.postMessage(types.Error{
 					Message: types.RespondTo(msg),
 					Error:   err,
-				}
+				})
 			} else {
-				w.messages <- types.Ack{
+				w.postMessage(types.Ack{
 					Message: types.RespondTo(msg),
-				}
+				})
 			}
 		case update := <-w.updates:
-			w.logger.Printf("[= %s", spew.Sdump(update))
+			w.logger.Printf("[= %T", update)
 		}
 	}
 }
diff --git a/worker/types/messages.go b/worker/types/messages.go
index e83bd6b..5259342 100644
--- a/worker/types/messages.go
+++ b/worker/types/messages.go
@@ -1,6 +1,8 @@
 package types
 
 import (
+	"crypto/x509"
+
 	"git.sr.ht/~sircmpwn/aerc2/config"
 )
 
@@ -12,6 +14,16 @@ type Message struct {
 	inResponseTo WorkerMessage
 }
 
+func RespondTo(msg WorkerMessage) Message {
+	return Message{
+		inResponseTo: msg,
+	}
+}
+
+func (m Message) InResponseTo() WorkerMessage {
+	return m.inResponseTo
+}
+
 // Meta-messages
 
 type Ack struct {
@@ -27,7 +39,7 @@ type Unsupported struct {
 	Message
 }
 
-// Commands
+// Actions
 
 type Ping struct {
 	Message
@@ -46,12 +58,10 @@ type Disconnect struct {
 	Message
 }
 
-func RespondTo(msg WorkerMessage) Message {
-	return Message{
-		inResponseTo: msg,
-	}
-}
+// Messages
 
-func (m Message) InResponseTo() WorkerMessage {
-	return m.inResponseTo
+// Respond with an Ack to approve or Disconnect to reject
+type ApproveCertificate struct {
+	Message
+	CertPool *x509.CertPool
 }
a286c54 ^
afbd2693 ^
ba286c54 ^

88f423af ^
ba286c54 ^

bc9e6b79 ^
8aba52f4 ^
ba286c54 ^
e043029a ^






ba286c54 ^
88f423af ^
ba286c54 ^

32da6548 ^
8e9bf083 ^

de8975c0 ^


8e9bf083 ^
bfa4a2ef ^
883dbe19 ^
ad926352 ^

8e9bf083 ^


2b7894cc ^
d853284f ^
acd2d230 ^

9b177a9e ^
88f423af ^
acd2d230 ^

0d0578b6 ^
acd2d230 ^

524c7a86 ^
acd2d230 ^

2b7894cc ^
acd2d230 ^

d853284f ^


11181100 ^
32da6548 ^
665c3441 ^
a978bb12 ^
3aad0523 ^
a978bb12 ^
41fe8c22 ^
83385cdb ^


665c3441 ^
11181100 ^




3e325a61 ^
9b177a9e ^

3e325a61 ^
b4722632 ^
11b6e1bf ^
aa0c6383 ^
a0a4fd04 ^
11b6e1bf ^
b4722632 ^

11b6e1bf ^
e45afd5c ^
11181100 ^
8c8c18c6 ^
a0a4fd04 ^
a4b53550 ^
e45afd5c ^

e45afd5c ^
2ac91161 ^


03a4aa44 ^


3e325a61 ^
e45afd5c ^
3e325a61 ^
9b177a9e ^

3e325a61 ^
11b6e1bf ^
03a4aa44 ^
11181100 ^
8c8c18c6 ^
a0a4fd04 ^
a4b53550 ^
03a4aa44 ^

2ac91161 ^


03a4aa44 ^


3e325a61 ^
e45afd5c ^
3e325a61 ^
9b177a9e ^

3e325a61 ^
b4722632 ^
11b6e1bf ^
aa0c6383 ^
a0a4fd04 ^
11b6e1bf ^
b4722632 ^

11181100 ^
8c8c18c6 ^
a0a4fd04 ^
a4b53550 ^
e45afd5c ^
e45afd5c ^
2ac91161 ^


e45afd5c ^
03a4aa44 ^

3e325a61 ^
03a4aa44 ^
3e325a61 ^


0fb27dc4 ^

11b6e1bf ^
aa0c6383 ^
a0a4fd04 ^
c614cc28 ^
4e1ffa6b ^
0fb27dc4 ^
11181100 ^
8c8c18c6 ^
a0a4fd04 ^
a4b53550 ^
0fb27dc4 ^






3e325a61 ^







11b6e1bf ^
aa0c6383 ^
a0a4fd04 ^
c614cc28 ^
4e1ffa6b ^
3e325a61 ^
11b6e1bf ^
3e325a61 ^
11181100 ^
8c8c18c6 ^
a0a4fd04 ^
a4b53550 ^
3e325a61 ^









0fb27dc4 ^
3e325a61 ^




11b6e1bf ^
aa0c6383 ^
a0a4fd04 ^
c614cc28 ^
4e1ffa6b ^
3e325a61 ^
11b6e1bf ^
aa0c6383 ^
a0a4fd04 ^
11b6e1bf ^
3e325a61 ^

11b6e1bf ^
8c8c18c6 ^
a0a4fd04 ^
a4b53550 ^
3e325a61 ^














11b6e1bf ^
8c8c18c6 ^
a0a4fd04 ^
b6b7dd5a ^
4e1ffa6b ^
3e325a61 ^
11b6e1bf ^
8c8c18c6 ^
a0a4fd04 ^
b6b7dd5a ^
3e325a61 ^

11b6e1bf ^
3e325a61 ^
11181100 ^
8c8c18c6 ^
a0a4fd04 ^
a4b53550 ^
3e325a61 ^











9b177a9e ^

3e325a61 ^
11b6e1bf ^
8c8c18c6 ^
a0a4fd04 ^
a4b53550 ^
e45afd5c ^
41fe8c22 ^


2ac91161 ^


3e325a61 ^
53f2a4a3 ^


11181100 ^





4ec005e4 ^
53f2a4a3 ^
41fe8c22 ^
83385cdb ^


41fe8c22 ^
4ec005e4 ^




a9d55dec ^

4ec005e4 ^
4e1ffa6b ^
a0a4fd04 ^
4ec005e4 ^
4e1ffa6b ^
a9d55dec ^
4ec005e4 ^
4e1ffa6b ^
a0a4fd04 ^
a4b53550 ^
e69f9475 ^
a9d55dec ^







4ec005e4 ^
4e1ffa6b ^
9224331d ^
a4b53550 ^
e69f9475 ^
41fe8c22 ^


a9d55dec ^

53f2a4a3 ^


4ec005e4 ^





1b0ce852 ^
53f2a4a3 ^
150015fa ^

77e819e8 ^

150015fa ^
41fe8c22 ^
8c8c18c6 ^
41fe8c22 ^
a0a4fd04 ^
8c8c18c6 ^
a0a4fd04 ^
50271493 ^
a0a4fd04 ^
41fe8c22 ^

8c8c18c6 ^
41fe8c22 ^
8c8c18c6 ^
150015fa ^
80665ea0 ^
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423