-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathconsole_ssh.cpp
More file actions
1295 lines (1220 loc) · 38.8 KB
/
Copy pathconsole_ssh.cpp
File metadata and controls
1295 lines (1220 loc) · 38.8 KB
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
; Project: Open Vehicle Monitor System
; Date: 14th March 2017
;
; Changes:
; 1.0 Initial release
;
; (C) 2011 Michael Stegen / Stegen Electronics
; (C) 2011-2017 Mark Webb-Johnson
; (C) 2011 Sonny Chen @ EPRO/DX
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
; THE SOFTWARE.
*/
// We're using ESP_EARLY_LOG* (direct USB console output) for protocol debug logging.
// To enable protocol debug logging locally, uncomment:
// #define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
#include <sys/stat.h>
#define LWIP_POSIX_SOCKETS_IO_NAMES 0
#include <lwip/def.h>
#include <lwip/sockets.h>
#undef bind
#include "freertos/queue.h"
#include "esp_heap_caps.h"
#include "ovms_log.h"
// Unfortunately, both the compiler includes (stdc++) and wolfssl define "byte"
// to a different value, and the compiler is not OK with that.
// So we hide the compiler's definition - ugly hack.
#define byte do_not_redefine_byte
#include "ovms_events.h"
#undef byte
#include "ovms_netmanager.h"
#include "ovms_config.h"
#include "ovms_ota.h"
#include <wolfssl/wolfcrypt/memory.h>
#include <wolfssl/wolfcrypt/sha256.h>
#include <wolfssl/wolfcrypt/coding.h>
#include <wolfssl/wolfcrypt/rsa.h>
#include <wolfssl/wolfcrypt/sha.h>
#include <wolfssl/wolfcrypt/logging.h>
#include <wolfssh/ssh.h>
#include <wolfssh/log.h>
#include <wolfssh/internal.h>
#include "console_ssh.h"
static void wolfssh_logger(enum wolfSSH_LogLevel level, const char* const msg);
static void wolfssl_logger(int level, const char* const msg);
static void* wolfssl_malloc(size_t size);
static void wolfssl_free(void* ptr);
static void* wolfssl_realloc(void* ptr, size_t size);
static const char* const tag = "ssh";
static const char* const wolfssh_tag = "wolfssh";
static const char* const wolfssl_tag = "wolfssl";
static uint8_t CRLF[2] = { '\r', '\n' };
static const char newline = '\n';
//-----------------------------------------------------------------------------
// Class OvmsSSH
//-----------------------------------------------------------------------------
OvmsSSH MySSH __attribute__ ((init_priority (8300)));
static void MongooseHandler(struct mg_connection *nc, int ev, void *p)
{
MySSH.EventHandler(nc, ev, p);
}
void OvmsSSH::EventHandler(struct mg_connection *nc, int ev, void *p)
{
switch (ev)
{
case MG_EV_ACCEPT:
{
ESP_EARLY_LOGV(tag, "Event MG_EV_ACCEPT conn %p, data %p", nc, p);
ConsoleSSH* child = new ConsoleSSH(this, nc);
nc->user_data = child;
break;
}
case MG_EV_POLL:
{
// Reap consoles whose follow task has exited (GetFollowModeTask() now NULL).
for (auto it = m_reaping.begin(); it != m_reaping.end(); )
{
ConsoleSSH* z = *it;
if (z->GetFollowModeTask() == NULL)
{ delete z; it = m_reaping.erase(it); }
else
++it;
}
//ESP_EARLY_LOGV(tag, "Event MG_EV_ACCEPT conn %p, data %p", nc, p);
ConsoleSSH* child = (ConsoleSSH*)nc->user_data;
if (child)
{
child->Send();
child->Poll(0);
}
if (!m_keyed)
{
std::string skey = MyConfig.GetParamValueBinary("ssh.server", "key", std::string());
if (!skey.empty())
{
m_keyed = true;
int ret = wolfSSH_CTX_UsePrivateKey_buffer(m_ctx, (const uint8_t*)skey.data(),
skey.size(), WOLFSSH_FORMAT_ASN1);
if (ret < 0)
ESP_LOGE(tag, "Couldn't use configured server key, error %d: %s", ret,
GetErrorString(ret));
else
{
std::string fp = MyConfig.GetParamValue("ssh.info", "fingerprint", "[not available]");
ESP_LOGI(tag, "SSH server key installed with fingerprint %s", fp.c_str());
}
}
}
}
break;
case MG_EV_RECV:
{
ESP_EARLY_LOGV(tag, "Event MG_EV_RECV conn %p, data received %d", nc, *(int*)p);
ConsoleSSH* child = (ConsoleSSH*)nc->user_data;
child->Receive();
}
break;
case MG_EV_SEND:
{
ESP_EARLY_LOGV(tag, "Event MG_EV_SEND conn %p, data %p", nc, p);
ConsoleSSH* child = (ConsoleSSH*)nc->user_data;
child->Sent();
break;
}
case MG_EV_CLOSE:
{
ESP_EARLY_LOGV(tag, "Event MG_EV_CLOSE conn %p, data %p", nc, p);
ConsoleSSH* child = (ConsoleSSH*)nc->user_data;
if (child)
{
OvmsCommandTask* ft = child->GetFollowModeTask();
if (ft)
{
// Follow task still running: we cannot join here (we hold the Mongoose
// lock its write() needs). Mark closing so its in-flight write bails,
// ask it to stop, and defer delete until it has left the write path.
child->SetClosing();
ft->RequestStop();
m_reaping.push_back(child);
}
else
delete child; // no follow task: synchronous termination + free (unchanged)
}
nc->user_data = NULL;
}
break;
default:
break;
}
}
OvmsSSH::OvmsSSH()
{
ESP_LOGI(tag, "Initialising SSH (8300)");
m_keyed = false;
m_ctx = NULL;
using std::placeholders::_1;
using std::placeholders::_2;
MyEvents.RegisterEvent(tag,"network.mgr.init", std::bind(&OvmsSSH::NetManInit, this, _1, _2));
MyEvents.RegisterEvent(tag,"network.mgr.stop", std::bind(&OvmsSSH::NetManStop, this, _1, _2));
MyEvents.RegisterEvent(tag,"config.restore", std::bind(&OvmsSSH::ConfigRestore, this, _1, _2));
MyConfig.RegisterParam("ssh.server", "SSH server private key store", true, false);
MyConfig.RegisterParam("ssh.info", "SSH server information", false, true);
MyConfig.RegisterParam("ssh.keys", "SSH public key store", true, true);
}
void OvmsSSH::NetManInit(std::string event, void* data)
{
// Only initialise server for WIFI connections
// TODO: Disabled as this introduces a network interface ordering issue. It
// seems that the correct way to do this is to always start the mongoose
// listener, but to filter incoming connections to check that the
// destination address is a Wifi interface address.
// if (!(MyNetManager.m_connected_wifi || MyNetManager.m_wifi_ap))
// return;
int ret;
ESP_LOGI(tag, "Launching SSH Server");
wolfSSH_SetLoggingCb(&wolfssh_logger);
wolfSSH_Debugging_ON();
wolfSSL_SetLoggingCb(&wolfssl_logger);
wolfSSL_Debugging_ON();
wolfSSL_SetAllocators(wolfssl_malloc, wolfssl_free, wolfssl_realloc);
ret = wolfSSH_Init();
if (ret != WS_SUCCESS)
{
ESP_LOGE(tag, "Couldn't initialize wolfSSH, error %d: %s", ret,
GetErrorString(ret));
return;
}
m_ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL);
if (m_ctx == NULL)
{
::printf("\nInsufficient memory to allocate SSH context\n");
return;
}
wolfSSH_CTX_SetBanner(m_ctx, NULL);
wolfSSH_SetUserAuth(m_ctx, Authenticate);
std::string skey = MyConfig.GetParamValueBinary("ssh.server", "key", std::string());
if (skey.empty())
{
if (ovms_partition_table_get_type() == OVMS_FlashPartition_34)
{
ESP_LOGW(tag, "Partition update in progress, SSH server key generation inhibited");
}
else
{
ESP_LOGI(tag, "Generating SSH Server key, wait before attempting access.");
new RSAKeyGenerator();
}
}
else
{
m_keyed = true;
ret = wolfSSH_CTX_UsePrivateKey_buffer(m_ctx, (const uint8_t*)skey.data(),
skey.size(), WOLFSSH_FORMAT_ASN1);
if (ret < 0)
ESP_LOGE(tag, "Couldn't use configured server key, error %d: %s", ret,
GetErrorString(ret));
}
auto mglock = MongooseLock();
struct mg_mgr* mgr = MyNetManager.GetMongooseMgr();
if (!mgr)
{
ESP_LOGE(tag, "Network manager is not available");
return;
}
mg_connection* nc = mg_bind(mgr, ":22", MongooseHandler);
if (nc)
nc->user_data = NULL;
else
ESP_LOGE(tag, "Launching SSH Server failed");
}
void OvmsSSH::NetManStop(std::string event, void* data)
{
if (m_ctx)
{
ESP_LOGI(tag, "Stopping SSH Server");
wolfSSH_CTX_free(m_ctx);
if (wolfSSH_Cleanup() != WS_SUCCESS)
ESP_LOGE(tag, "Couldn't clean up wolfSSH.");
m_ctx = NULL;
}
}
void OvmsSSH::ConfigRestore(std::string event, void* data)
{
if (RSAKeyGenerator::KillInstance())
{
ESP_LOGW(tag, "RSAKeyGenerator killed by request");
}
}
int OvmsSSH::Authenticate(uint8_t type, WS_UserAuthData* data, void* ctx)
{
ConsoleSSH* cons = (ConsoleSSH*)ctx;
if (type == WOLFSSH_USERAUTH_PASSWORD)
{
std::string user((const char*)data->username, data->usernameSz);
std::string pw = MyConfig.GetParamValue("password", user, std::string());
if (pw.empty())
pw = MyConfig.GetParamValue("password", "module", std::string());
if (pw.empty())
{
if (MyConfig.IsDefined("ssh.keys", ""))
return WOLFSSH_USERAUTH_INVALID_PASSWORD; // Can't use null pw if have key
else
return WOLFSSH_USERAUTH_SUCCESS; // No pw or key set means no authentication
}
if (pw.size() != data->sf.password.passwordSz ||
memcmp(data->sf.password.password, pw.data(), pw.size()) != 0)
return WOLFSSH_USERAUTH_INVALID_PASSWORD;
}
else if (type == WOLFSSH_USERAUTH_PUBLICKEY)
{
std::string user((const char*)data->username, data->usernameSz);
std::string key = MyConfig.GetParamValue("ssh.keys", user, std::string());
if (key.empty())
return WOLFSSH_USERAUTH_INVALID_USER;
byte der[560];
word32 len = sizeof(der);
if (Base64_Decode((const byte*)key.data(), key.size(), der, &len) != 0 ||
len != data->sf.publicKey.publicKeySz ||
memcmp(data->sf.publicKey.publicKey, der, len) != 0)
return WOLFSSH_USERAUTH_INVALID_PUBLICKEY;
}
else
return WOLFSSH_USERAUTH_INVALID_AUTHTYPE;
cons->SetSecure(true); // Successful SSH authentication grants privileged access
return WOLFSSH_USERAUTH_SUCCESS;
}
//-----------------------------------------------------------------------------
// Class ConsoleSSH
//-----------------------------------------------------------------------------
int RecvCallback(WOLFSSH* ssh, void* data, word32 size, void* ctx);
int SendCallback(WOLFSSH* ssh, void* data, word32 size, void* ctx);
ConsoleSSH::ConsoleSSH(OvmsSSH* server, struct mg_connection* nc)
{
m_server = server;
m_connection = nc;
m_queue = xQueueCreate(100, sizeof(Event));
m_ssh = NULL;
m_state = ACCEPT;
m_drain = 0;
m_closing = false;
m_sent = true;
m_rekey = false;
m_needDir = false;
m_isDir = false;
m_verbose = false;
m_recursive = false;
m_file = NULL;
m_ssh = wolfSSH_new(m_server->ctx());
if (m_ssh == NULL)
{
::printf("Couldn't allocate SSH session data.\n");
return;
}
wolfSSH_SetIORecv(m_server->ctx(), ::RecvCallback);
wolfSSH_SetIOSend(m_server->ctx(), ::SendCallback);
wolfSSH_SetIOReadCtx(m_ssh, this);
wolfSSH_SetIOWriteCtx(m_ssh, this);
wolfSSH_SetUserAuthCtx(m_ssh, this);
/* Use the session object for its own highwater callback ctx */
wolfSSH_SetHighwaterCtx(m_ssh, (void*)m_ssh);
wolfSSH_SetHighwater(m_ssh, DEFAULT_HIGHWATER_MARK);
}
ConsoleSSH::~ConsoleSSH()
{
// Clean up any interactive command (e.g. a "vfs tail" follow-mode task) bound
// to this console before freeing the transport it writes to, otherwise it
// would dereference a freed writer/transport.
RunTerminationCallback();
WOLFSSH* ssh = m_ssh;
m_ssh = NULL;
wolfSSH_free(ssh);
m_dirs.clear();
// m_queue deleted by OvmsConsole::~OvmsConsole()
}
// Handle MG_EV_RECV event.
void ConsoleSSH::Receive()
{
OvmsConsole::Event event;
event.type = OvmsConsole::event_type_t::RECV;
BaseType_t ret = xQueueSendToBack(m_queue, (void * )&event, (portTickType)(1000 / portTICK_PERIOD_MS));
if (ret == pdPASS)
{
// Process the event we just queued in sequence with any queued logging.
Poll(0);
}
else
{
ESP_EARLY_LOGE(tag, "Timeout queueing message in ConsoleSSH::Receive\n");
mbuf *io = &m_connection->recv_mbuf;
mbuf_remove(io, io->len);
}
}
// Handle MG_EV_POLL event.
void ConsoleSSH::Send()
{
if (m_state != SOURCE_SEND || !m_sent)
{
if (m_drain > 0 && m_connection->send_mbuf.len == 0)
write("", 0); // Wake up output after draining
return;
}
int ret = 0;
while (true)
{
if (m_size == 0)
m_size = fread(m_buffer, sizeof(char), BUFFER_SIZE, m_file);
if (m_size <= 0)
break;
m_sent = false;
ret = wolfSSH_stream_send(m_ssh, (uint8_t*)m_buffer + m_index, m_size);
if (ret < 0)
break;
m_size -= ret;
if (m_size == 0)
m_index = 0;
else
{
m_index += ret;
return;
}
if (!m_sent)
return;
}
if (ret < 0)
{
// Would need to check ret != WS_WANT_WRITE here if mg_send is changed to
// return EWOUDBLOCK
ESP_EARLY_LOGE(tag, "Error %d in wolfSSH_stream_send: %s", ret, GetErrorString(ret));
m_connection->flags |= MG_F_SEND_AND_CLOSE;
m_state = CLOSING;
}
else if (m_size < 0)
{
ESP_EARLY_LOGE(tag, "Error %d reading file in source scp: %s", m_size, strerror(m_size));
m_connection->flags |= MG_F_SEND_AND_CLOSE;
m_state = CLOSING;
}
else // EOF on fread()
{
ESP_EARLY_LOGD(tag, "Sending %s completed", m_path.c_str());
m_state = SOURCE_RESPONSE;
wolfSSH_stream_send(m_ssh, (uint8_t*)"", 1);
}
fclose(m_file);
m_file = NULL;
}
// Handle MG_EV_SEND event.
void ConsoleSSH::Sent()
{
m_sent = true;
}
int ConsoleSSH::GetResponse()
{
// Read the single binary 0 byte ACK or the firat byte of an error message or
// protocol line.
int rc = wolfSSH_stream_read(m_ssh, (uint8_t*)m_buffer, 1);
if (rc < 1)
return rc;
if (m_state != SINK_LOOP || *m_buffer < ' ')
ESP_EARLY_LOGD(tag, "response() received protocol ack byte %d", *m_buffer);
if (*m_buffer != 0)
{
// Read the rest of the protocol line or error message, stopping on the
// newline and leaving anything else queued. We assume that it will all be
// sent in one IP packet so we don't have to worry about WS_WANT_READ.
char* p = m_buffer + 1;
do
rc = wolfSSH_stream_read(m_ssh, (uint8_t*)p, 1);
while (rc == 1 && *p++ != '\n' && p < &m_buffer[BUFFER_SIZE-1]);
*p-- = '\0';
if (*p == '\n')
*p = '\0';
}
return 1;
}
// Handle RECV event from queue.
void ConsoleSSH::HandleDeviceEvent(void* pEvent)
{
struct stat sbuf;
std::string msg;
size_t filename = 0;
Level level;
int rc = 0;
Event event = *(Event*)pEvent;
if (event.type != RECV)
{
ESP_LOGE(tag, "Unknown event type %d in ConsoleSSH", event.type);
return;
}
do
{
switch (m_state)
{
case ACCEPT:
{
rc = wolfSSH_accept(m_ssh);
if (rc != WS_SUCCESS && (rc = wolfSSH_get_error(m_ssh)) != WS_SUCCESS)
break;
if (wolfSSH_GetSessionType(m_ssh) == WOLFSSH_SESSION_SHELL)
{
Initialize("SSH");
m_state = SHELL;
}
else if (wolfSSH_GetSessionType(m_ssh) == WOLFSSH_SESSION_EXEC)
{
const char* cmdline = wolfSSH_GetSessionCommand(m_ssh);
if (!cmdline)
{
rc = WS_BAD_USAGE; // no command provided
break;
}
ESP_LOGD(tag, "SSH command request: %s", cmdline);
if (strncmp(cmdline, "scp -", 5) == 0)
{
bool from = false, to = false;
int i;
for (i = 5; ; ++i)
{
if (cmdline[i] == 'f') from = true;
if (cmdline[i] == 't') to = true;
if (cmdline[i] == 'd') m_needDir = true;
if (cmdline[i] == 'v') m_verbose = true;
if (cmdline[i] == 'r') m_recursive = true;
if (cmdline[i] == ' ' && cmdline[++i] != '-') break;
if (cmdline[i] == '\0') break;
}
m_path = &cmdline[i];
if (from == to)
rc = WS_BAD_USAGE;
else if (from)
m_state = SOURCE;
else // -t (to) case
m_state = SINK;
}
else
m_state = EXEC;
}
else // Session did not request a 'shell' or 'exec' program
rc = WS_UNIMPLEMENTED_E;
break;
}
// Normal interactive shell session
case SHELL:
{
rc = wolfSSH_stream_read(m_ssh, (uint8_t*)m_buffer, BUFFER_SIZE);
if (rc > 0)
{
// Translate CR (Enter) from ssh client into \n for microrl
for (int i = 0; i < rc; ++i)
{
if (m_buffer[i] == '\r')
m_buffer[i] = '\n';
}
ProcessChars(m_buffer, rc);
}
break;
}
// 'exec' session with command to be executed and results returned
case EXEC:
{
Initialize(NULL);
const char* cmdline = wolfSSH_GetSessionCommand(m_ssh);
ProcessChars(cmdline, strlen(cmdline));
ProcessChar('\n');
wolfSSH_stream_exit(m_ssh, 0); // XXX Need commands to return status
m_state = CLOSING;
break;
}
// SCP session pulling data from OVMS
case SOURCE:
{
// Only expecting to read a single binary 0 byte
rc = wolfSSH_stream_read(m_ssh, (uint8_t*)m_buffer, 1);
if (rc < 1)
break;
ESP_EARLY_LOGD(tag, "SOURCE received protocol ack byte %d", *m_buffer);
if (*m_buffer == 0)
m_state = SOURCE_LOOP;
else
rc = WS_BAD_USAGE; // client always sends 0 so this shouldn't happen
break;
}
case SOURCE_LOOP:
{
ESP_LOGD(tag, "SCP 'from' file %s", m_path.c_str());
msg.assign("\2scp: ").append(m_path).append(": ");
if (MyConfig.ProtectedPath(m_path.c_str()))
msg.append("protected path\n");
else
{
while (m_path.size() > 0 && m_path.at(m_path.size()-1) == '/')
m_path.resize(m_path.size()-1); // Trim off a trailing '/'
filename = m_path.find_last_of('/');
if (filename == std::string::npos)
filename = 0;
else
++filename;
int ret = 0;
// Need to fake these because stat says "Invalid argument"
if (m_path.compare("/store") == 0 || m_path.compare("/sd") == 0)
sbuf.st_mode = S_IFDIR; // Don't care about permissions
else
ret = stat(m_path.c_str(), &sbuf);
if (ret < 0)
msg.append(strerror(errno)).append("\n");
else if (S_ISDIR(sbuf.st_mode))
{
if (!m_recursive)
msg.append("received directory without -r\n");
else if ((level.dir = opendir(m_path.c_str())) == NULL)
msg.append(strerror(errno)).append("\n");
else
{
level.size = m_path.size();
m_dirs.push_front(level);
msg.assign("D0755 0 ").append(m_path.substr(filename));
ESP_LOGD(tag, "Source: %s", msg.c_str());
msg.append("\n");
wolfSSH_stream_send(m_ssh, (uint8_t*)msg.c_str(), msg.size());
m_state = SOURCE_RESPONSE;
break;
}
}
else if (S_ISREG(sbuf.st_mode))
{
if ((m_file = fopen(m_path.c_str(), "r")) == NULL)
msg.append(strerror(errno)).append("\n");
else
{
char num[12];
snprintf(num, sizeof(num), "%ld ", sbuf.st_size);
msg.assign("C0644 ").append(num).append(m_path.substr(filename));
ESP_LOGD(tag, "Source: %s", msg.c_str());
msg.append("\n");
wolfSSH_stream_send(m_ssh, (uint8_t*)msg.c_str(), msg.size());
m_state = SOURCE_RESPONSE;
break;
}
}
else
msg.append("not a regular file\n");
}
// Send the composed error message
wolfSSH_stream_send(m_ssh, (uint8_t*)msg.c_str(), msg.size());
m_state = SOURCE_RESPONSE;
ESP_LOGI(tag, "%.*s", msg.size()-2, msg.substr(1,msg.size()-1).c_str());
break;
}
case SOURCE_DIR:
{
level = m_dirs.front();
m_path.resize(level.size);
struct dirent* dp = readdir(level.dir);
if (dp == NULL)
{
closedir(level.dir);
m_dirs.pop_front();
ESP_LOGD(tag, "Source: E");
wolfSSH_stream_send(m_ssh, (uint8_t*)"E\n", 2);
m_state = SOURCE_RESPONSE;
break;
}
m_path.append("/").append(dp->d_name);
m_state = SOURCE_LOOP;
break;
}
case SOURCE_SEND:
case SOURCE_RESPONSE:
{
rc = GetResponse();
if (rc < 1)
break;
if (m_state == SOURCE_SEND)
ESP_LOGW(tag, "RECV not expected in SOURCE_SEND state");
if (m_buffer[0] == '\1')
{
ESP_LOGW(tag, "Source: %s", &m_buffer[1]);
if (m_file) // Abort the file we were going to send, if any
{
fclose(m_file);
m_file = NULL;
}
}
else if (m_buffer[0] == '\2')
{
ESP_LOGE(tag, "Source: %s", &m_buffer[1]);
wolfSSH_stream_exit(m_ssh, 1);
m_state = CLOSING;
break;
}
if (m_file)
{
m_state = SOURCE_SEND;
m_index = m_size = 0;
Send(); // Send the first buffer, more when sent
return;
}
if (!m_dirs.empty())
{
m_state = SOURCE_DIR; // working on a directory
break;
}
wolfSSH_stream_exit(m_ssh, 0);
m_state = CLOSING;
break;
}
// SCP session pushing data to OVMS
case SINK:
{
msg.assign("\2scp: ").append(m_path).append(": ");
while (m_path.size() > 1 && m_path.at(m_path.size()-1) == '/')
{
m_path.resize(m_path.size()-1); // Trim off a trailing '/'
m_needDir = true;
}
level.size = m_path.size();
m_dirs.push_front(level);
filename = std::string::npos;
int ret = 0;
// Need to fake these because stat says "Invalid argument"
if (m_path.compare("/store") == 0 || m_path.compare("/sd") == 0)
sbuf.st_mode = S_IFDIR; // Don't care about permissions
else
ret = stat(m_path.c_str(), &sbuf);
if (ret < 0 && errno == ENOENT && !m_needDir &&
(filename = m_path.find_last_of('/')) != std::string::npos)
{
// Check path dirname for case where we're creating a new file
int retd = stat(m_path.substr(0, filename).c_str(), &sbuf);
if (retd == 0)
ret = retd;
}
if (ret < 0)
msg.append(strerror(errno)).append("\n");
else if (S_ISDIR(sbuf.st_mode) || S_ISREG(sbuf.st_mode))
{
m_isDir = S_ISDIR(sbuf.st_mode) && filename == std::string::npos;
if (m_isDir || !m_needDir)
{
m_state = SINK_LOOP;
wolfSSH_stream_send(m_ssh, (uint8_t*)"", 1);
break;
}
msg.append(strerror(ENOTDIR)).append("\n");
}
else
msg.append("not a regular file\n"); // Probably can't hit this
wolfSSH_stream_send(m_ssh, (uint8_t*)msg.c_str(), msg.size());
ESP_LOGI(tag, "%.*s", msg.size()-2, msg.substr(1,msg.size()-1).c_str());
wolfSSH_stream_exit(m_ssh, 0);
m_state = CLOSING;
break;
}
case SINK_LOOP:
{
// Read the protocol line or error message.
rc = GetResponse();
if (rc < 1)
break;
if (*m_buffer == '\1')
{
ESP_LOGW(tag, "Sink: %s", &m_buffer[1]);
break; // Continue with more protocol lines
}
else if (*m_buffer == '\2')
{
ESP_LOGE(tag, "Sink: %s", &m_buffer[1]);
wolfSSH_stream_exit(m_ssh, 1);
m_state = CLOSING;
break;
}
ESP_LOGD(tag, "Sink: %s", m_buffer);
msg.assign("\2scp: ");
if (*m_buffer == 'T') // Times are ignored
{
wolfSSH_stream_send(m_ssh, (uint8_t*)"", 1);
break;
}
else if (*m_buffer == 'E')
{
level = m_dirs.front();
m_path.resize(level.size);
m_dirs.pop_front();
if (!m_dirs.empty())
{
wolfSSH_stream_send(m_ssh, (uint8_t*)"", 1);
break;
}
msg.append("protocol error: unbalanced D-E pair\n");
}
else if (*m_buffer == 'D' || *m_buffer == 'C')
{
char* end = m_buffer;
if (m_buffer[1] != '0' || m_buffer[2] < '0' || m_buffer[2] > '7' ||
m_buffer[3] < '0' || m_buffer[3] > '7' || m_buffer[4] < '0' || m_buffer[4] > '7' ||
m_buffer[5] != ' ')
msg.append("bad mode ").append(&m_buffer[1], 4).append("\n");
else if ((m_size = strtoul(&m_buffer[6], &end, 10)) < 0 || m_size > 10485760 || *end++ != ' ')
msg.append("size unacceptable: ").append(&m_buffer[6], end-&m_buffer[6]).append("\n");
else if ((strchr(end, '/') != NULL) || (strcmp(end, "..") == 0))
msg.append("unexpected filename: ").append(end).append("\n");
else
{
level = m_dirs.front();
m_path.resize(level.size);
if (m_isDir)
{
if (m_path.compare("/") != 0) // Probably impossible to target "/" on OVMS
m_path.append("/");
m_path.append(end);
}
bool exists = (stat(m_path.c_str(), &sbuf) == 0);
msg.append(m_path).append(": ");
if (MyConfig.ProtectedPath(m_path.c_str()))
msg.append("protected path\n");
else if (*m_buffer == 'D')
{
if (!m_recursive)
msg.append("received directory without -r\n");
else
{
level.size = m_path.size();
m_dirs.push_front(level);
if (exists)
{
if (!S_ISDIR(sbuf.st_mode))
msg.append(strerror(ENOTDIR)).append("\n");
else
{
wolfSSH_stream_send(m_ssh, (uint8_t*)"", 1);
break;
}
}
else
{
ESP_EARLY_LOGD(tag, "mkdir(\"%s\", 0777)", m_path.c_str());
if (mkdir(m_path.c_str(), 0777) < 0)
msg.append("mkdir: ").append(strerror(errno)).append("\n");
else
{
wolfSSH_stream_send(m_ssh, (uint8_t*)"", 1);
break;
}
}
}
}
else // 'C'
{
if (exists && !S_ISREG(sbuf.st_mode))
msg.append("not a regular file\n");
else
{
ESP_EARLY_LOGD(tag, "fopen(\"%s\", \"w\")", m_path.c_str());
if ((m_file = fopen(m_path.c_str(), "w")) == NULL)
msg.append("fopen: ").append(strerror(errno)).append("\n");
else
{
m_state = SINK_RECEIVE;
wolfSSH_stream_send(m_ssh, (uint8_t*)"", 1);
break;
}
}
}
}
}
else
msg.append("protocol error: ").append(m_buffer).append("\n");
wolfSSH_stream_send(m_ssh, (uint8_t*)msg.c_str(), msg.size());
ESP_LOGI(tag, "%.*s", msg.size()-2, msg.substr(1,msg.size()-1).c_str());
wolfSSH_stream_exit(m_ssh, 1);
break;
}
case SINK_RECEIVE:
{
int size = BUFFER_SIZE;
if (m_size < size)
size = m_size;
rc = wolfSSH_stream_read(m_ssh, (uint8_t*)m_buffer, size);
if (rc < 0)
break; // Probably WS_WANT_READ, loop end checks it
size = fwrite(m_buffer, sizeof(uint8_t), rc, m_file);
if (size < rc)
ESP_LOGE(tag, "Write error on %s: %s", m_path.c_str(), strerror(errno));
m_size -= rc;
if (m_size == 0)
{
fclose(m_file);
m_file = NULL;
m_state = SINK_RESPONSE;
wolfSSH_stream_send(m_ssh, (uint8_t*)"", 1);
}
break;
}
case SINK_RESPONSE:
{
rc = GetResponse();
if (rc < 1)
break;
if (*m_buffer == '\1') // Warning, so we continue
ESP_LOGW(tag, "Sink: %s", &m_buffer[1]);
else if (*m_buffer == '\2') // Error, so we exit
{
ESP_LOGE(tag, "Sink: %s", &m_buffer[1]);
wolfSSH_stream_exit(m_ssh, 1);
m_state = CLOSING;
break;
}
m_state = SINK_LOOP;
break;
}
case CLOSING:
ESP_EARLY_LOGD(tag, "Reached CLOSING");
// Try to read to let SSH process packets, but ignore anything delivered
rc = wolfSSH_stream_read(m_ssh, (uint8_t*)m_buffer, BUFFER_SIZE);
break;
}
} while (rc >= 0);
if (rc == WS_ERROR)
rc = wolfSSH_get_error(m_ssh);
if (rc == WS_WANT_READ || rc == WS_BAD_ARGUMENT) // Latter => channel closed
return;
if (rc == WS_EOF)
{
wolfSSH_stream_exit(m_ssh, 0);
m_state = CLOSING;
return;
}
ESP_LOGE(tag, "Error %d in reception: %s", rc, GetErrorString(rc));
m_connection->flags |= MG_F_SEND_AND_CLOSE;
}
// This is called to shut down the SSH connection when the "exit" command is input.
void ConsoleSSH::Exit()
{
printf("logout\n");
wolfSSH_stream_exit(m_ssh, 0);
}
int ConsoleSSH::puts(const char* s)
{
if (!m_ssh)
return -1;
write(s, strlen(s));
write(&newline, 1);
return 0;
}
int ConsoleSSH::printf(const char* fmt, ...)
{
if (!m_ssh)
return 0;
char *buffer = NULL;
va_list args;
va_start(args,fmt);
int ret = vasprintf(&buffer, fmt, args);
va_end(args);
if (ret < 0)
{
if (buffer)
free(buffer);
return ret;
}
ret = write(buffer, ret);
free(buffer);
return ret;
}
ssize_t ConsoleSSH::write(const void *buf, size_t nbyte)
{
// m_closing is checked first on purpose: once the connection is closing the
// mongoose nc (m_connection) may already be freed, so short-circuit before
// dereferencing it. Same reason SendCallback bails on IsClosing() first.
if (m_closing || !m_ssh || (m_connection->flags & MG_F_SEND_AND_CLOSE))
return 0;
int ret = 0;
if (m_drain > 0)
{
if (m_connection->send_mbuf.len > 0)
{