-
Notifications
You must be signed in to change notification settings - Fork 6
/
mailssl.e
544 lines (446 loc) · 15.6 KB
/
mailssl.e
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
-> SSL and mail sending options
OPT MODULE
MODULE 'amissl','amisslmaster','socket','net/socket','net/in','net/netdb','*errors','*stringlist'
CONST BIO_CTRL_FLUSH=11
CONST BIO_FLAGS_BASE64_NO_NL=$100
CONST BIO_CTRL_INFO=3
CONST AMISSLMASTER_MIN_VERSION=4
CONST AMISSL_CURRENT_VERSION=6
CONST SSL_VERIFY_PEER=1
CONST SSL_VERIFY_FAIL_IF_NO_PEER_CERT=2
CONST BIO_NOCLOSE=$0
CONST BIO_FP_TEXT=$10
CONST OPENSSL_LINE=0
CONST BIO_SET_FILE_PTR=$6a ->dont know what this is
CONST OPENSSL_INIT_LOAD_SSL_STRINGS=$200000
CONST OPENSSL_INIT_LOAD_CRYPTO_STRINGS=2
CONST AMISSL_SOCKETBASE=$80000001
CONST AMISSL_ERRNOPTR=$8000000B
EXPORT OBJECT mailConfig
smtpHost[255]:ARRAY OF CHAR
smtpPort:LONG
username[255]:ARRAY OF CHAR
password[255]:ARRAY OF CHAR
sysopEmail[255]:ARRAY OF CHAR
bbsEmail[255]:ARRAY OF CHAR
ssl: INT
mailOnNewUser: INT
mailOnSysopComment: INT
mailOnSysopPage: INT
ENDOBJECT
EXPORT DEF mailOptions: PTR TO mailConfig
DEF sslerrno
DEF ctx: LONG ->PTR TO SSL_CTX
DEF bio_err: LONG ->PTR TO BIO
EXPORT PROC sendMail(subject:PTR TO CHAR,bodytext:PTR TO CHAR, appendMsgBuf, msgBuf:PTR TO stringlist, lines, toemail:PTR TO CHAR)
DEF ssl,sock=0,errcode=0
DEF buffer[4096]:STRING; /* This should be dynamically allocated */
DEF bufsize=4096
DEF request[512]:STRING
DEF failed=FALSE,v,i
/* The following needs to be done once per socket */
/* Connect to the HTTPS server, directly or through a proxy */
sock:=connectToServer(mailOptions.smtpHost,mailOptions.smtpPort)
/* Check if connection was established */
IF (sock >= 0)
IF ctx
IF((ssl:=SsL_new(ctx)) <> NIL)
/* Associate the socket with the ssl structure */
SsL_set_fd(ssl, sock)
/* Perform SSL handshake */
IF((errcode:=SsL_connect(ssl)) >= 0)
IF ((errcode:=SsL_read(ssl, buffer,bufsize - 1)) >0)
buffer[errcode]:=0
WriteF('\s \d \d\n',buffer, errcode, 1);
ELSE
WriteF('Couldn''t read initial server message!\n');
ENDIF
StrCopy(request,'EHLO relay.example.com\n')
IF ((errcode:=SsL_write(ssl, request, StrLen(request))) <=0 )
WriteF('error sending EHLO\n');
ENDIF
IF((errcode:=SsL_read(ssl, buffer,bufsize - 1)) > 0)
buffer[errcode]:=0
v:=Val(buffer)
failed:=((v<200) OR (v>299))
WriteF('\s \d \d\n',buffer, errcode, 1);
ENDIF
IF (failed=FALSE)
StringF(request,'\n\s\n\s',mailOptions.username,mailOptions.password)
v:=StrLen(request)-1
FOR i:=0 TO v
IF request[i]="\n" THEN request[i]:=0
ENDFOR
base64enc(request,EstrLen(request),buffer)
StringF(request,'AUTH PLAIN \s\n',buffer)
IF ((errcode:=SsL_write(ssl, request, EstrLen(request))) <=0 )
WriteF('error sending AUTH\n');
ENDIF
IF((errcode:=SsL_read(ssl, buffer,bufsize - 1)) > 0)
buffer[errcode]:=0
v:=Val(buffer)
failed:=((v<200) OR (v>299))
WriteF('\s \d \d\n',buffer, errcode, 1);
ENDIF
ENDIF
IF (failed=FALSE)
StringF(request,'mail from:<\s>\n',mailOptions.bbsEmail)
IF ((errcode:=SsL_write(ssl, request, StrLen(request))) <=0 )
WriteF('error sending FROM\n');
ENDIF
IF((errcode:=SsL_read(ssl, buffer,bufsize - 1)) > 0)
buffer[errcode]:=0
v:=Val(buffer)
failed:=((v<200) OR (v>299))
WriteF('\s \d \d\n',buffer, errcode, 1);
ENDIF
ENDIF
IF (failed=FALSE)
StringF(request,'rcpt to:<\s>\n',toemail)
IF ((errcode:=SsL_write(ssl, request, StrLen(request))) <=0 )
WriteF('error sending TO\n');
ENDIF
IF((errcode:=SsL_read(ssl, buffer,bufsize - 1)) > 0)
buffer[errcode]:=0
v:=Val(buffer)
failed:=((v<200) OR (v>299))
WriteF('\s \d \d\n',buffer, errcode, 1);
ENDIF
ENDIF
IF (failed=FALSE)
StrCopy(request,'DATA\n')
IF ((errcode:=SsL_write(ssl, request, StrLen(request))) <=0 )
WriteF('error sending DATA\n');
ENDIF
IF((errcode:=SsL_read(ssl, buffer,bufsize - 1)) > 0)
buffer[errcode]:=0
v:=Val(buffer)
failed:=(((v<200) OR (v>299)) AND (v<>354))
WriteF('\s \d \d\n',buffer, errcode, 1);
ENDIF
ENDIF
IF (failed=FALSE)
StrCopy(request,'From: <\s>\b\n',mailOptions.bbsEmail)
IF ((errcode:=SsL_write(ssl, request, StrLen(request))) <=0 )
WriteF('error sending msg data\n');
ENDIF
StrCopy(request,'To: <toemail>\b\n')
IF ((errcode:=SsL_write(ssl, request, StrLen(request))) <=0 )
WriteF('error sending msg data\n');
ENDIF
StringF(request,'Subject: \s\b\n',subject)
IF ((errcode:=SsL_write(ssl, request, StrLen(request))) <=0 )
WriteF('error sending msg data\n');
ENDIF
StrCopy(request,'\b\n')
IF ((errcode:=SsL_write(ssl, request, StrLen(request))) <=0 )
WriteF('error sending msg data\n');
ENDIF
i:=0
WHILE(i<StrLen(bodytext))
IF(v:=InStr('\n',bodytext,i)<0)
v:=StrLen(bodytext)
StrCopy(request,bodytext+i,v-i)
i:=v
ELSE
StrCopy(request,bodytext+i,v-i)
StrAdd(request,'\b\n')
i:=v+1
ENDIF
IF ((errcode:=SsL_write(ssl, request, StrLen(request))) <=0 )
WriteF('error sending msg data\n');
ENDIF
ENDWHILE
IF appendMsgBuf
FOR i:=0 TO lines-1
StringF(request,'\s\b\n',msgBuf.item(i))
IF ((errcode:=SsL_write(ssl, request, StrLen(request))) <=0 )
WriteF('error sending msg data\n');
ENDIF
ENDFOR
ENDIF
StrCopy(request,'\b\n.\b\n')
IF ((errcode:=SsL_write(ssl, request, StrLen(request))) <=0 )
WriteF('error sending DATA\n');
ENDIF
IF((errcode:=SsL_read(ssl, buffer,bufsize - 1)) > 0)
buffer[errcode]:=0
WriteF('\s \d \d\n',buffer, errcode, 1);
ENDIF
StrCopy(request,'QUIT\n')
IF ((errcode:=SsL_write(ssl, request, StrLen(request))) <=0 )
WriteF('error sending QUIT\n');
ENDIF
ENDIF
ELSE
WriteF('Couldn''t establish SSL connection!\n');
ENDIF
/* If there were errors, print them */
->IF (errcode < 0) THEN ErR_print_errors(bio_err);
/* Send SSL close notification and close the socket */
SsL_shutdown(ssl);
SsL_free(ssl);
ELSE
WriteF('Couldn''t create new SSL handle!\n');
ENDIF
ELSE
->standard unencrypted smtp
IF ((errcode:=Recv(sock,buffer,bufsize - 1,0)) >0)
buffer[errcode]:=0
WriteF('\s \d \d\n',buffer, errcode, 1);
ELSE
WriteF('Couldn''t read initial server message!\n');
ENDIF
StrCopy(request,'EHLO relay.example.com\n')
IF ((errcode:=Send(sock, request, StrLen(request),0)) <=0 )
WriteF('error sending EHLO\n');
ENDIF
IF((errcode:=Recv(sock, buffer,bufsize - 1,0)) > 0)
buffer[errcode]:=0
v:=Val(buffer)
failed:=((v<200) OR (v>299))
WriteF('\s \d \d\n',buffer, errcode, 1);
ENDIF
IF (failed=FALSE)
StringF(request,'\n\s\n\s',mailOptions.username,mailOptions.password)
v:=StrLen(request)-1
FOR i:=0 TO v
IF request[i]="\n" THEN request[i]:=0
ENDFOR
base64enc(request,EstrLen(request),buffer)
StringF(request,'AUTH PLAIN \s\n',buffer)
IF ((errcode:=Send(sock, request, EstrLen(request),0)) <=0 )
WriteF('error sending AUTH\n');
ENDIF
IF((errcode:=Recv(sock, buffer,bufsize - 1,0)) > 0)
buffer[errcode]:=0
v:=Val(buffer)
failed:=((v<200) OR (v>299))
WriteF('\s \d \d\n',buffer, errcode, 1);
ENDIF
ENDIF
IF (failed=FALSE)
StringF(request,'mail from:<\s>\n',mailOptions.bbsEmail)
IF ((errcode:=Send(sock, request, StrLen(request),0)) <=0 )
WriteF('error sending FROM\n');
ENDIF
IF((errcode:=Recv(sock, buffer,bufsize - 1,0)) > 0)
buffer[errcode]:=0
v:=Val(buffer)
failed:=((v<200) OR (v>299))
WriteF('\s \d \d\n',buffer, errcode, 1);
ENDIF
ENDIF
IF (failed=FALSE)
StringF(request,'rcpt to:<\s>\n',toemail)
IF ((errcode:=Send(sock, request, StrLen(request),0)) <=0 )
WriteF('error sending TO\n');
ENDIF
IF((errcode:=Recv(sock, buffer,bufsize - 1,0)) > 0)
buffer[errcode]:=0
v:=Val(buffer)
failed:=((v<200) OR (v>299))
WriteF('\s \d \d\n',buffer, errcode, 1);
ENDIF
ENDIF
IF (failed=FALSE)
StrCopy(request,'DATA\n')
IF ((errcode:=Send(sock, request, StrLen(request),0)) <=0 )
WriteF('error sending DATA\n');
ENDIF
IF((errcode:=Recv(sock, buffer,bufsize - 1,0)) > 0)
buffer[errcode]:=0
v:=Val(buffer)
failed:=(((v<200) OR (v>299)) AND (v<>354))
WriteF('\s \d \d\n',buffer, errcode, 1);
ENDIF
ENDIF
IF (failed=FALSE)
StrCopy(request,'From: <\s>\b\n',mailOptions.bbsEmail)
IF ((errcode:=Send(sock, request, StrLen(request),0)) <=0 )
WriteF('error sending msg data\n');
ENDIF
StrCopy(request,'To: <toemail>\b\n')
IF ((errcode:=Send(sock, request, StrLen(request),0)) <=0 )
WriteF('error sending msg data\n');
ENDIF
StringF(request,'Subject: \s\b\n',subject)
IF ((errcode:=Send(sock, request, StrLen(request),0)) <=0 )
WriteF('error sending msg data\n');
ENDIF
StrCopy(request,'\b\n')
IF ((errcode:=Send(sock, request, StrLen(request),0)) <=0 )
WriteF('error sending msg data\n');
ENDIF
i:=0
WHILE(i<StrLen(bodytext))
IF(v:=InStr('\n',bodytext,i)<0)
v:=StrLen(bodytext)
StrCopy(request,bodytext+i,v-i)
i:=v
ELSE
StrCopy(request,bodytext+i,v-i)
StrAdd(request,'\b\n')
i:=v+1
ENDIF
IF ((errcode:=Send(sock, request, StrLen(request),0)) <=0 )
WriteF('error sending msg data\n');
ENDIF
ENDWHILE
IF appendMsgBuf
FOR i:=0 TO lines-1
StringF(request,'\s\b\n',msgBuf.item(i))
IF ((errcode:=Send(sock, request, StrLen(request),0)) <=0 )
WriteF('error sending msg data\n');
ENDIF
ENDFOR
ENDIF
StrCopy(request,'\b\n.\b\n')
IF ((errcode:=Send(sock, request, StrLen(request),0)) <=0 )
WriteF('error sending DATA\n');
ENDIF
IF((errcode:=Recv(sock, buffer,bufsize - 1,0)) > 0)
buffer[errcode]:=0
WriteF('\s \d \d\n',buffer, errcode, 1);
ENDIF
StrCopy(request,'QUIT\n')
IF ((errcode:=Send(sock, request, StrLen(request),0)) <=0 )
WriteF('error sending QUIT\n');
ENDIF
ENDIF
ENDIF
CloseSocket(sock);
ELSE
WriteF('Couldn''t connect to host!\n');
ENDIF
ENDPROC
PROC base64enc(data:PTR TO CHAR,len,output)
DEF b64
DEF mem
DEF done=FALSE,res=0,outstr,strlen
-> bio is simply a class that wraps BIO* and it free the BIO in the destructor.
b64:=BiO_new(BiO_f_base64()); ->// create BIO to perform base64
BiO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
mem:=BiO_new(BiO_s_mem()); ->// create BIO that holds the result
->// chain base64 with mem, so writing to b64 will encode base64 and write to mem.
BiO_push(b64, mem);
->// write data
WHILE(done=FALSE)
res:=BiO_write(b64, data, len);
IF(res <= 0) -> if failed
IF(BiO_fd_should_retry(b64)=FALSE)
->// encoding failed
/* Handle Error!!! */
RETURN 0
ENDIF
ELSE
->// success!
done:=TRUE;
ENDIF
ENDWHILE
BiO_ctrl(b64,BIO_CTRL_FLUSH,0,NIL)
strlen:=BiO_ctrl(mem,BIO_CTRL_INFO,0,{outstr})
StrCopy(output,outstr,strlen)
BiO_free(mem)
BiO_free(b64)
ENDPROC
/* Open and initialize AmiSSL */
EXPORT PROC initssl(createctx) HANDLE
DEF tags:PTR TO LONG
sslerrno:=0
ctx:=0
bio_err:=0
IF socketbase=NIL THEN socketbase:=OpenLibrary('bsdsocket.library', 4)
IF (socketbase=NIL)
WriteF('Couldn''t open bsdsocket.library v4!\n')
Raise(ERR_SSL)
ENDIF
amisslmasterbase:=OpenLibrary('amisslmaster.library',AMISSLMASTER_MIN_VERSION)
IF (amisslmasterbase=NIL)
WriteF('Couldn''t open amisslmaster.library v\d!\n',AMISSLMASTER_MIN_VERSION);
Raise(ERR_SSL)
ENDIF
IF (InitAmiSSLMaster(AMISSL_CURRENT_VERSION, TRUE))=NIL
WriteF('AmiSSL version is too old!\n');
Raise(ERR_SSL)
ENDIF
amisslbase:=OpenAmiSSL()
IF (amisslbase=NIL)
WriteF('Couldn''t open AmiSSL!\n');
Raise(ERR_SSL)
ENDIF
tags:=NEW [AMISSL_ERRNOPTR,{sslerrno},AMISSL_SOCKETBASE,socketbase,0]
IF (InitAmiSSLA(tags) <> 0)
FastDisposeList(tags)
WriteF('Couldn''t initialize AmiSSL!\n');
Raise(ERR_SSL)
ENDIF
FastDisposeList(tags)
/* Basic intialization. Next few steps (up to SSL_new()) need
* to be done only once per AmiSSL opener.
*/
OpENSSL_init_ssl(0,NIL) ->SSLeay_add_ssl_algorithms(); -$67C8(a6)
OpENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS OR OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NIL) ->SSL_load_error_strings();
/* Note: BIO writing routines are prepared for NULL BIO handle */
IF((bio_err:=BiO_new(BiO_s_file())) <> NIL) THEN BiO_ctrl(bio_err, BIO_SET_FILE_PTR, BIO_NOCLOSE OR BIO_FP_TEXT, Output()); ->BiO_set_fp_amiga(bio_err, Output(), BIO_NOCLOSE OR BIO_FP_TEXT);
/* Get a new SSL context */
IF (createctx)
ctx:=SsL_CTX_new(TlS_client_method())
IF (ctx=0)
WriteF('Couldn''t create ssl ctx!\n');
Raise(ERR_SSL)
ENDIF
SsL_CTX_set_default_verify_paths(ctx);
SsL_CTX_set_verify(ctx, SSL_VERIFY_PEER OR SSL_VERIFY_FAIL_IF_NO_PEER_CERT,NIL);
ENDIF
EXCEPT
cleanupssl()
RETURN FALSE
ENDPROC TRUE
EXPORT PROC cleanupssl()
IF ctx<>NIL
SsL_CTX_free(ctx);
ctx:=NIL
ENDIF
IF bio_err<>NIL
BiO_free(bio_err)
bio_err:=NIL
ENDIF
IF (amisslbase)
CleanupAmiSSLA([0]);
CloseAmiSSL();
amisslbase:=NIL
ENDIF
CloseLibrary(amisslmasterbase);
amisslmasterbase:=NIL;
ENDPROC
/* Connect to the specified server, either directly or through the specified
* proxy using HTTP CONNECT method.
*/
EXPORT PROC connectToServer(host:PTR TO CHAR, port)
DEF addr: PTR TO sockaddr_in
DEF is_ok = FALSE;
DEF sock=NIL;
DEF hostEnt: PTR TO hostent
DEF hostaddr: PTR TO LONG
/* Create a socket and connect to the server */
IF ((sock:=Socket(AF_INET, SOCK_STREAM, 0)) >= 0)
hostEnt:=GetHostByName(host)
hostaddr:=hostEnt.h_addr_list[]
hostaddr:=hostaddr[]
NEW addr
addr.sin_family:=AF_INET;
addr.sin_addr:=hostaddr[]; /* This should be checked against INADDR_NONE */
addr.sin_port:=port->htons(port);
IF (Connect(sock, addr, SIZEOF sockaddr_in) >= 0)
is_ok:=TRUE
ELSE
WriteF('Couldn''t connect to server\n');
ENDIF
END addr
IF (is_ok=FALSE)
CloseSocket(sock);
sock:=-1;
ENDIF
ENDIF
ENDPROC sock