server.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /* server.c
  2. *
  3. * Copyright (C) 2014-2019 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSH.
  6. *
  7. * wolfSSH is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSH is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with wolfSSH. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #define WOLFSSH_TEST_SERVER
  21. #define WOLFSSH_TEST_THREADING
  22. #include <stdbool.h>
  23. #include "FreeRTOS.h"
  24. #include "task.h"
  25. #include "wolfssh_test.h"
  26. #include "cli.h"
  27. #include "CLI_Commands.h"
  28. #ifdef WOLFSSL_USER_SETTINGS
  29. #include <wolfssl/wolfcrypt/settings.h>
  30. #else
  31. #include <wolfssl/options.h>
  32. #endif
  33. #include <wolfssl/wolfcrypt/sha256.h>
  34. #include <wolfssl/wolfcrypt/coding.h>
  35. #include <wolfssh/ssh.h>
  36. //#include <wolfssh/test.h>
  37. #include <wolfssl/wolfcrypt/ecc.h>
  38. #include "examples/server/server.h"
  39. #ifdef NO_FILESYSTEM
  40. #include <wolfssh/certs_test.h>
  41. #endif
  42. static const char serverBanner[] = "BT-6709 command server\n";
  43. typedef struct {
  44. WOLFSSH* ssh;
  45. SOCKET_T fd;
  46. word32 id;
  47. char nonBlock;
  48. } thread_ctx_t;
  49. #ifndef EXAMPLE_HIGHWATER_MARK
  50. #define EXAMPLE_HIGHWATER_MARK 0x3FFF8000 /* 1GB - 32kB */
  51. #endif
  52. #ifndef EXAMPLE_BUFFER_SZ
  53. #define EXAMPLE_BUFFER_SZ 256
  54. #endif
  55. #define SCRATCH_BUFFER_SZ 1200
  56. static byte find_char(const byte* str, const byte* buf, word32 bufSz)
  57. {
  58. const byte* cur;
  59. while (bufSz) {
  60. cur = str;
  61. while (*cur != '\0') {
  62. if (*cur == *buf)
  63. return *cur;
  64. cur++;
  65. }
  66. buf++;
  67. bufSz--;
  68. }
  69. return 0;
  70. }
  71. /*
  72. static int dump_stats(thread_ctx_t* ctx)
  73. {
  74. char stats[1024];
  75. word32 statsSz;
  76. word32 txCount, rxCount, seq, peerSeq;
  77. wolfSSH_GetStats(ctx->ssh, &txCount, &rxCount, &seq, &peerSeq);
  78. WSNPRINTF(stats, sizeof(stats),
  79. "Statistics for Thread #%u:\r\n"
  80. " txCount = %u\r\n rxCount = %u\r\n"
  81. " seq = %u\r\n peerSeq = %u\r\n",
  82. ctx->id, txCount, rxCount, seq, peerSeq);
  83. statsSz = (word32)strlen(stats);
  84. fprintf(stderr, "%s", stats);
  85. return wolfSSH_stream_send(ctx->ssh, (byte*)stats, statsSz);
  86. }
  87. */
  88. static int NonBlockSSH_accept(WOLFSSH* ssh)
  89. {
  90. int ret;
  91. int error;
  92. SOCKET_T sockfd;
  93. int select_ret = 0;
  94. ret = wolfSSH_accept(ssh);
  95. error = wolfSSH_get_error(ssh);
  96. sockfd = (SOCKET_T)wolfSSH_get_fd(ssh);
  97. while (ret != WS_SUCCESS &&
  98. (error == WS_WANT_READ || error == WS_WANT_WRITE))
  99. {
  100. if (error == WS_WANT_READ)
  101. printf("... client would read block\n");
  102. else if (error == WS_WANT_WRITE)
  103. printf("... client would write block\n");
  104. select_ret = tcp_select(sockfd, 1);
  105. if (select_ret == WS_SELECT_RECV_READY ||
  106. select_ret == WS_SELECT_ERROR_READY ||
  107. error == WS_WANT_WRITE)
  108. {
  109. ret = wolfSSH_accept(ssh);
  110. error = wolfSSH_get_error(ssh);
  111. }
  112. else if (select_ret == WS_SELECT_TIMEOUT)
  113. error = WS_WANT_READ;
  114. else
  115. error = WS_FATAL_ERROR;
  116. }
  117. return ret;
  118. }
  119. static void cli_send(intptr_t fd, const char *str, unsigned len)
  120. {
  121. wolfSSH_stream_send((WOLFSSH *)fd, str, len);
  122. }
  123. extern cli_state_t cli_states[MAX_SESSIONS];
  124. static void *server_worker(void* vArgs)
  125. {
  126. int ret;
  127. thread_ctx_t* threadCtx = (thread_ctx_t*)vArgs;
  128. if (!threadCtx->nonBlock)
  129. ret = wolfSSH_accept(threadCtx->ssh);
  130. else
  131. ret = NonBlockSSH_accept(threadCtx->ssh);
  132. if (ret == WS_SUCCESS) {
  133. // create the new CLI context
  134. cli_states[threadCtx->fd].num_connect = threadCtx->ssh;
  135. cli_states[threadCtx->fd].input_state = CLI_CMD;
  136. cli_states[threadCtx->fd].state = STATE_NORMAL;
  137. cli_states[threadCtx->fd].send = cli_send;
  138. bool stop = false;
  139. do {
  140. uint8_t buf[EXAMPLE_BUFFER_SZ];
  141. int rxSz = 0;
  142. do {
  143. rxSz = wolfSSH_stream_read(threadCtx->ssh, buf, sizeof(buf));
  144. if (rxSz <= 0) {
  145. rxSz = wolfSSH_get_error(threadCtx->ssh);
  146. }
  147. } while (rxSz == WS_WANT_READ || rxSz == WS_WANT_WRITE);
  148. if (rxSz > 0) {
  149. cli_getchar(cli_states + threadCtx->fd, buf[0]); // TODO handle rxSz > 1
  150. } else {
  151. stop = 1;
  152. }
  153. } while (!stop);
  154. } else if (ret == WS_SCP_COMPLETE) {
  155. printf("scp file transfer completed\n");
  156. } else if (ret == WS_SFTP_COMPLETE) {
  157. printf("Use example/echoserver/echoserver for SFTP\n");
  158. }
  159. wolfSSH_stream_exit(threadCtx->ssh, 0);
  160. WCLOSESOCKET(threadCtx->fd);
  161. wolfSSH_free(threadCtx->ssh);
  162. free(threadCtx);
  163. return 0;
  164. }
  165. #ifndef NO_FILESYSTEM
  166. static int load_file(const char* fileName, byte* buf, word32 bufSz)
  167. {
  168. FILE* file;
  169. word32 fileSz;
  170. word32 readSz;
  171. if (fileName == NULL) return 0;
  172. if (WFOPEN(&file, fileName, "rb") != 0)
  173. return 0;
  174. fseek(file, 0, SEEK_END);
  175. fileSz = (word32)ftell(file);
  176. rewind(file);
  177. if (fileSz > bufSz) {
  178. fclose(file);
  179. return 0;
  180. }
  181. readSz = (word32)fread(buf, 1, fileSz, file);
  182. if (readSz < fileSz) {
  183. fclose(file);
  184. return 0;
  185. }
  186. fclose(file);
  187. return fileSz;
  188. }
  189. #endif /* !NO_FILESYSTEM */
  190. /* returns buffer size on success */
  191. static int load_key(byte isEcc, byte* buf, word32 bufSz)
  192. {
  193. word32 sz = 0;
  194. #ifndef NO_FILESYSTEM
  195. const char* bufName;
  196. bufName = isEcc ? "./keys/server-key-ecc.der" :
  197. "./keys/server-key-rsa.der" ;
  198. sz = load_file(bufName, buf, bufSz);
  199. #else
  200. /* using buffers instead */
  201. if (isEcc) {
  202. if (sizeof_ecc_key_der_256 > bufSz) {
  203. return 0;
  204. }
  205. WMEMCPY(buf, ecc_key_der_256, sizeof_ecc_key_der_256);
  206. sz = sizeof_ecc_key_der_256;
  207. }
  208. else {
  209. if (sizeof_rsa_key_der_2048 > bufSz) {
  210. return 0;
  211. }
  212. WMEMCPY(buf, rsa_key_der_2048, sizeof_rsa_key_der_2048);
  213. sz = sizeof_rsa_key_der_2048;
  214. }
  215. #endif
  216. return sz;
  217. }
  218. static INLINE void c32toa(word32 u32, byte* c)
  219. {
  220. c[0] = (u32 >> 24) & 0xff;
  221. c[1] = (u32 >> 16) & 0xff;
  222. c[2] = (u32 >> 8) & 0xff;
  223. c[3] = u32 & 0xff;
  224. }
  225. /* Map user names to passwords */
  226. /* Use arrays for username and p. The password or public key can
  227. * be hashed and the hash stored here. Then I won't need the type. */
  228. typedef struct PwMap {
  229. byte type;
  230. byte username[32];
  231. word32 usernameSz;
  232. byte p[SHA256_DIGEST_SIZE];
  233. struct PwMap* next;
  234. } PwMap;
  235. typedef struct PwMapList {
  236. PwMap* head;
  237. } PwMapList;
  238. static PwMap* PwMapNew(PwMapList* list, byte type, const byte* username,
  239. word32 usernameSz, const byte* p, word32 pSz)
  240. {
  241. PwMap* map;
  242. map = (PwMap*)malloc(sizeof(PwMap));
  243. if (map != NULL) {
  244. Sha256 sha;
  245. byte flatSz[4];
  246. map->type = type;
  247. if (usernameSz >= sizeof(map->username))
  248. usernameSz = sizeof(map->username) - 1;
  249. memcpy(map->username, username, usernameSz + 1);
  250. map->username[usernameSz] = 0;
  251. map->usernameSz = usernameSz;
  252. wc_InitSha256(&sha);
  253. c32toa(pSz, flatSz);
  254. wc_Sha256Update(&sha, flatSz, sizeof(flatSz));
  255. wc_Sha256Update(&sha, p, pSz);
  256. wc_Sha256Final(&sha, map->p);
  257. map->next = list->head;
  258. list->head = map;
  259. }
  260. return map;
  261. }
  262. static void PwMapListDelete(PwMapList* list)
  263. {
  264. if (list != NULL) {
  265. PwMap* head = list->head;
  266. while (head != NULL) {
  267. PwMap* cur = head;
  268. head = head->next;
  269. memset(cur, 0, sizeof(PwMap));
  270. free(cur);
  271. }
  272. }
  273. }
  274. static const char samplePasswordBuffer[] =
  275. "jill:upthehill\n"
  276. "jack:fetchapail\n";
  277. static const char samplePublicKeyEccBuffer[] =
  278. "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAA"
  279. "BBBNkI5JTP6D0lF42tbxX19cE87hztUS6FSDoGvPfiU0CgeNSbI+aFdKIzTP5CQEJSvm25"
  280. "qUzgDtH7oyaQROUnNvk= hansel\n"
  281. "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAA"
  282. "BBBKAtH8cqaDbtJFjtviLobHBmjCtG56DMkP6A4M2H9zX2/YCg1h9bYS7WHd9UQDwXO1Hh"
  283. "IZzRYecXh7SG9P4GhRY= gretel\n";
  284. static const char samplePublicKeyRsaBuffer[] =
  285. "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9P3ZFowOsONXHD5MwWiCciXytBRZGho"
  286. "MNiisWSgUs5HdHcACuHYPi2W6Z1PBFmBWT9odOrGRjoZXJfDDoPi+j8SSfDGsc/hsCmc3G"
  287. "p2yEhUZUEkDhtOXyqjns1ickC9Gh4u80aSVtwHRnJZh9xPhSq5tLOhId4eP61s+a5pwjTj"
  288. "nEhBaIPUJO2C/M0pFnnbZxKgJlX7t1Doy7h5eXxviymOIvaCZKU+x5OopfzM/wFkey0EPW"
  289. "NmzI5y/+pzU5afsdeEWdiQDIQc80H6Pz8fsoFPvYSG+s4/wz0duu7yeeV1Ypoho65Zr+pE"
  290. "nIf7dO0B8EblgWt+ud+JI8wrAhfE4x hansel\n"
  291. "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqDwRVTRVk/wjPhoo66+Mztrc31KsxDZ"
  292. "+kAV0139PHQ+wsueNpba6jNn5o6mUTEOrxrz0LMsDJOBM7CmG0983kF4gRIihECpQ0rcjO"
  293. "P6BSfbVTE9mfIK5IsUiZGd8SoE9kSV2pJ2FvZeBQENoAxEFk0zZL9tchPS+OCUGbK4SDjz"
  294. "uNZl/30Mczs73N3MBzi6J1oPo7sFlqzB6ecBjK2Kpjus4Y1rYFphJnUxtKvB0s+hoaadru"
  295. "biE57dK6BrH5iZwVLTQKux31uCJLPhiktI3iLbdlGZEctJkTasfVSsUizwVIyRjhVKmbdI"
  296. "RGwkU38D043AR1h0mUoGCPIKuqcFMf gretel\n";
  297. static int LoadPasswordBuffer(byte* buf, word32 bufSz, PwMapList* list)
  298. {
  299. char* str = (char*)buf;
  300. char* delimiter;
  301. char* username;
  302. char* password;
  303. /* Each line of passwd.txt is in the format
  304. * username:password\n
  305. * This function modifies the passed-in buffer. */
  306. if (list == NULL)
  307. return -1;
  308. if (buf == NULL || bufSz == 0)
  309. return 0;
  310. while (*str != 0) {
  311. delimiter = strchr(str, ':');
  312. if (delimiter == NULL) {
  313. return -1;
  314. }
  315. username = str;
  316. *delimiter = 0;
  317. password = delimiter + 1;
  318. str = strchr(password, '\n');
  319. if (str == NULL) {
  320. return -1;
  321. }
  322. *str = 0;
  323. str++;
  324. if (PwMapNew(list, WOLFSSH_USERAUTH_PASSWORD,
  325. (byte*)username, (word32)strlen(username),
  326. (byte*)password, (word32)strlen(password)) == NULL ) {
  327. return -1;
  328. }
  329. }
  330. return 0;
  331. }
  332. static int LoadPublicKeyBuffer(byte* buf, word32 bufSz, PwMapList* list)
  333. {
  334. char* str = (char*)buf;
  335. char* delimiter;
  336. byte* publicKey64;
  337. word32 publicKey64Sz;
  338. byte* username;
  339. word32 usernameSz;
  340. byte publicKey[300];
  341. word32 publicKeySz;
  342. /* Each line of passwd.txt is in the format
  343. * ssh-rsa AAAB3BASE64ENCODEDPUBLICKEYBLOB username\n
  344. * This function modifies the passed-in buffer. */
  345. if (list == NULL)
  346. return -1;
  347. if (buf == NULL || bufSz == 0)
  348. return 0;
  349. while (*str != 0) {
  350. /* Skip the public key type. This example will always be ssh-rsa. */
  351. delimiter = strchr(str, ' ');
  352. if (delimiter == NULL) {
  353. return -1;
  354. }
  355. str = delimiter + 1;
  356. delimiter = strchr(str, ' ');
  357. if (delimiter == NULL) {
  358. return -1;
  359. }
  360. publicKey64 = (byte*)str;
  361. *delimiter = 0;
  362. publicKey64Sz = (word32)(delimiter - str);
  363. str = delimiter + 1;
  364. delimiter = strchr(str, '\n');
  365. if (delimiter == NULL) {
  366. return -1;
  367. }
  368. username = (byte*)str;
  369. *delimiter = 0;
  370. usernameSz = (word32)(delimiter - str);
  371. str = delimiter + 1;
  372. publicKeySz = sizeof(publicKey);
  373. if (Base64_Decode(publicKey64, publicKey64Sz,
  374. publicKey, &publicKeySz) != 0) {
  375. return -1;
  376. }
  377. if (PwMapNew(list, WOLFSSH_USERAUTH_PUBLICKEY,
  378. username, usernameSz,
  379. publicKey, publicKeySz) == NULL ) {
  380. return -1;
  381. }
  382. }
  383. return 0;
  384. }
  385. static int wsUserAuth(byte authType,
  386. WS_UserAuthData* authData,
  387. void* ctx)
  388. {
  389. PwMapList* list;
  390. PwMap* map;
  391. byte authHash[SHA256_DIGEST_SIZE];
  392. if (ctx == NULL) {
  393. printf("wsUserAuth: ctx not set");
  394. return WOLFSSH_USERAUTH_FAILURE;
  395. }
  396. if (authType != WOLFSSH_USERAUTH_PASSWORD &&
  397. authType != WOLFSSH_USERAUTH_PUBLICKEY) {
  398. return WOLFSSH_USERAUTH_FAILURE;
  399. }
  400. /* Hash the password or public key with its length. */
  401. {
  402. Sha256 sha;
  403. byte flatSz[4];
  404. wc_InitSha256(&sha);
  405. if (authType == WOLFSSH_USERAUTH_PASSWORD) {
  406. c32toa(authData->sf.password.passwordSz, flatSz);
  407. wc_Sha256Update(&sha, flatSz, sizeof(flatSz));
  408. wc_Sha256Update(&sha,
  409. authData->sf.password.password,
  410. authData->sf.password.passwordSz);
  411. }
  412. else if (authType == WOLFSSH_USERAUTH_PUBLICKEY) {
  413. c32toa(authData->sf.publicKey.publicKeySz, flatSz);
  414. wc_Sha256Update(&sha, flatSz, sizeof(flatSz));
  415. wc_Sha256Update(&sha,
  416. authData->sf.publicKey.publicKey,
  417. authData->sf.publicKey.publicKeySz);
  418. }
  419. wc_Sha256Final(&sha, authHash);
  420. }
  421. list = (PwMapList*)ctx;
  422. map = list->head;
  423. while (map != NULL) {
  424. if (authData->usernameSz == map->usernameSz &&
  425. memcmp(authData->username, map->username, map->usernameSz) == 0) {
  426. if (authData->type == map->type) {
  427. if (memcmp(map->p, authHash, SHA256_DIGEST_SIZE) == 0) {
  428. return WOLFSSH_USERAUTH_SUCCESS;
  429. }
  430. else {
  431. return (authType == WOLFSSH_USERAUTH_PASSWORD ?
  432. WOLFSSH_USERAUTH_INVALID_PASSWORD :
  433. WOLFSSH_USERAUTH_INVALID_PUBLICKEY);
  434. }
  435. }
  436. else {
  437. return WOLFSSH_USERAUTH_INVALID_AUTHTYPE;
  438. }
  439. }
  440. map = map->next;
  441. }
  442. return WOLFSSH_USERAUTH_INVALID_USER;
  443. }
  444. static void ssh_server(void *arg)
  445. {
  446. (void)arg;
  447. WSTARTTCP();
  448. #ifdef DEBUG_WOLFSSH
  449. wolfSSH_Debugging_ON();
  450. #endif
  451. wolfSSH_Init();
  452. WOLFSSH_CTX* ctx = NULL;
  453. PwMapList pwMapList;
  454. SOCKET_T listenFd = 0;
  455. word32 defaultHighwater = EXAMPLE_HIGHWATER_MARK;
  456. word32 threadCount = 0;
  457. word16 port = 22;
  458. const char multipleConnections = 1;
  459. char useEcc = 1;
  460. int ch;
  461. char nonBlock = 0;
  462. if (wolfSSH_Init() != WS_SUCCESS) {
  463. printf("Couldn't initialize wolfSSH.\n");
  464. exit(EXIT_FAILURE);
  465. }
  466. ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL);
  467. if (ctx == NULL) {
  468. printf("Couldn't allocate SSH CTX data.\n");
  469. exit(EXIT_FAILURE);
  470. }
  471. memset(&pwMapList, 0, sizeof(pwMapList));
  472. wolfSSH_SetUserAuth(ctx, wsUserAuth);
  473. wolfSSH_CTX_SetBanner(ctx, serverBanner);
  474. {
  475. const char* bufName;
  476. byte buf[SCRATCH_BUFFER_SZ];
  477. word32 bufSz;
  478. bufSz = load_key(useEcc, buf, SCRATCH_BUFFER_SZ);
  479. if (bufSz == 0) {
  480. printf("Couldn't load key.\n");
  481. exit(EXIT_FAILURE);
  482. }
  483. if (wolfSSH_CTX_UsePrivateKey_buffer(ctx, buf, bufSz,
  484. WOLFSSH_FORMAT_ASN1) < 0) {
  485. printf("Couldn't use key buffer.\n");
  486. exit(EXIT_FAILURE);
  487. }
  488. bufSz = (word32)strlen(samplePasswordBuffer);
  489. memcpy(buf, samplePasswordBuffer, bufSz);
  490. buf[bufSz] = 0;
  491. LoadPasswordBuffer(buf, bufSz, &pwMapList);
  492. bufName = useEcc ? samplePublicKeyEccBuffer :
  493. samplePublicKeyRsaBuffer;
  494. bufSz = (word32)strlen(bufName);
  495. memcpy(buf, bufName, bufSz);
  496. buf[bufSz] = 0;
  497. LoadPublicKeyBuffer(buf, bufSz, &pwMapList);
  498. }
  499. tcp_listen(&listenFd, &port, 1, false, false);
  500. do {
  501. SOCKET_T clientFd = 0;
  502. SOCKADDR_IN_T clientAddr;
  503. socklen_t clientAddrSz = sizeof(clientAddr);
  504. #ifndef SINGLE_THREADED
  505. THREAD_TYPE thread;
  506. #endif
  507. WOLFSSH* ssh;
  508. thread_ctx_t* threadCtx;
  509. threadCtx = (thread_ctx_t*)malloc(sizeof(thread_ctx_t));
  510. if (threadCtx == NULL) {
  511. printf("Couldn't allocate thread context data.\n");
  512. exit(EXIT_FAILURE);
  513. }
  514. ssh = wolfSSH_new(ctx);
  515. if (ssh == NULL) {
  516. printf("Couldn't allocate SSH data.\n");
  517. exit(EXIT_FAILURE);
  518. }
  519. wolfSSH_SetUserAuthCtx(ssh, &pwMapList);
  520. /* Use the session object for its own highwater callback ctx */
  521. if (defaultHighwater > 0) {
  522. wolfSSH_SetHighwaterCtx(ssh, (void*)ssh);
  523. wolfSSH_SetHighwater(ssh, defaultHighwater);
  524. }
  525. clientFd = accept(listenFd, (struct sockaddr*)&clientAddr,
  526. &clientAddrSz);
  527. if (clientFd == -1)
  528. err_sys("tcp accept failed");
  529. if (nonBlock)
  530. tcp_set_nonblocking(&clientFd);
  531. wolfSSH_set_fd(ssh, (int)clientFd);
  532. threadCtx->ssh = ssh;
  533. threadCtx->fd = clientFd;
  534. threadCtx->id = threadCount++;
  535. threadCtx->nonBlock = nonBlock;
  536. #ifndef SINGLE_THREADED
  537. ThreadStart(server_worker, threadCtx, &thread);
  538. if (multipleConnections)
  539. ThreadDetach(thread);
  540. else
  541. ThreadJoin(thread);
  542. #else
  543. server_worker(threadCtx);
  544. #endif /* SINGLE_THREADED */
  545. } while (multipleConnections);
  546. PwMapListDelete(&pwMapList);
  547. wolfSSH_CTX_free(ctx);
  548. if (wolfSSH_Cleanup() != WS_SUCCESS) {
  549. printf("Couldn't clean up wolfSSH.\n");
  550. exit(EXIT_FAILURE);
  551. }
  552. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  553. wc_ecc_fp_free(); /* free per thread cache */
  554. #endif
  555. return 0;
  556. }
  557. void ssh_server_init(void)
  558. {
  559. vRegisterCLICommands();
  560. xTaskCreate(ssh_server, ( char * ) "ssh_server", 16*configMINIMAL_STACK_SIZE + EXAMPLE_BUFFER_SZ, NULL, tskIDLE_PRIORITY + 1, NULL);
  561. }