server.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. static void *server_worker(void* vArgs)
  124. {
  125. int ret;
  126. thread_ctx_t* threadCtx = (thread_ctx_t*)vArgs;
  127. if (!threadCtx->nonBlock)
  128. ret = wolfSSH_accept(threadCtx->ssh);
  129. else
  130. ret = NonBlockSSH_accept(threadCtx->ssh);
  131. cli_state_t *cli_state;
  132. // create the new CLI context
  133. if (ret == WS_SUCCESS && (cli_state = alloc_state())) {
  134. cli_state->num_connect = threadCtx->ssh;
  135. cli_state->input_state = CLI_CMD;
  136. cli_state->send = cli_send;
  137. cli_hello(cli_state);
  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_state, buf[0]); // TODO handle rxSz > 1
  150. if (buf[0] == 3 || buf[0] == 4 || cli_state->state == STATE_CLOSE) {
  151. stop = 1;
  152. }
  153. } else {
  154. stop = 1;
  155. }
  156. } while (!stop);
  157. cli_state->state = STATE_UNUSED;
  158. } else if (ret == WS_SCP_COMPLETE) {
  159. printf("scp file transfer completed\n");
  160. } else if (ret == WS_SFTP_COMPLETE) {
  161. printf("Use example/echoserver/echoserver for SFTP\n");
  162. }
  163. wolfSSH_stream_exit(threadCtx->ssh, 0);
  164. WCLOSESOCKET(threadCtx->fd);
  165. wolfSSH_free(threadCtx->ssh);
  166. free(threadCtx);
  167. return 0;
  168. }
  169. #ifndef NO_FILESYSTEM
  170. static int load_file(const char* fileName, byte* buf, word32 bufSz)
  171. {
  172. FILE* file;
  173. word32 fileSz;
  174. word32 readSz;
  175. if (fileName == NULL) return 0;
  176. if (WFOPEN(&file, fileName, "rb") != 0)
  177. return 0;
  178. fseek(file, 0, SEEK_END);
  179. fileSz = (word32)ftell(file);
  180. rewind(file);
  181. if (fileSz > bufSz) {
  182. fclose(file);
  183. return 0;
  184. }
  185. readSz = (word32)fread(buf, 1, fileSz, file);
  186. if (readSz < fileSz) {
  187. fclose(file);
  188. return 0;
  189. }
  190. fclose(file);
  191. return fileSz;
  192. }
  193. #endif /* !NO_FILESYSTEM */
  194. /* returns buffer size on success */
  195. static int load_key(byte isEcc, byte* buf, word32 bufSz)
  196. {
  197. word32 sz = 0;
  198. #ifndef NO_FILESYSTEM
  199. const char* bufName;
  200. bufName = isEcc ? "./keys/server-key-ecc.der" :
  201. "./keys/server-key-rsa.der" ;
  202. sz = load_file(bufName, buf, bufSz);
  203. #else
  204. /* using buffers instead */
  205. if (isEcc) {
  206. if (sizeof_ecc_key_der_256 > bufSz) {
  207. return 0;
  208. }
  209. WMEMCPY(buf, ecc_key_der_256, sizeof_ecc_key_der_256);
  210. sz = sizeof_ecc_key_der_256;
  211. }
  212. else {
  213. if (sizeof_rsa_key_der_2048 > bufSz) {
  214. return 0;
  215. }
  216. WMEMCPY(buf, rsa_key_der_2048, sizeof_rsa_key_der_2048);
  217. sz = sizeof_rsa_key_der_2048;
  218. }
  219. #endif
  220. return sz;
  221. }
  222. static INLINE void c32toa(word32 u32, byte* c)
  223. {
  224. c[0] = (u32 >> 24) & 0xff;
  225. c[1] = (u32 >> 16) & 0xff;
  226. c[2] = (u32 >> 8) & 0xff;
  227. c[3] = u32 & 0xff;
  228. }
  229. /* Map user names to passwords */
  230. /* Use arrays for username and p. The password or public key can
  231. * be hashed and the hash stored here. Then I won't need the type. */
  232. typedef struct PwMap {
  233. byte type;
  234. byte username[32];
  235. word32 usernameSz;
  236. byte p[SHA256_DIGEST_SIZE];
  237. struct PwMap* next;
  238. } PwMap;
  239. typedef struct PwMapList {
  240. PwMap* head;
  241. } PwMapList;
  242. static PwMap* PwMapNew(PwMapList* list, byte type, const byte* username,
  243. word32 usernameSz, const byte* p, word32 pSz)
  244. {
  245. PwMap* map;
  246. map = (PwMap*)malloc(sizeof(PwMap));
  247. if (map != NULL) {
  248. Sha256 sha;
  249. byte flatSz[4];
  250. map->type = type;
  251. if (usernameSz >= sizeof(map->username))
  252. usernameSz = sizeof(map->username) - 1;
  253. memcpy(map->username, username, usernameSz + 1);
  254. map->username[usernameSz] = 0;
  255. map->usernameSz = usernameSz;
  256. wc_InitSha256(&sha);
  257. c32toa(pSz, flatSz);
  258. wc_Sha256Update(&sha, flatSz, sizeof(flatSz));
  259. wc_Sha256Update(&sha, p, pSz);
  260. wc_Sha256Final(&sha, map->p);
  261. map->next = list->head;
  262. list->head = map;
  263. }
  264. return map;
  265. }
  266. static void PwMapListDelete(PwMapList* list)
  267. {
  268. if (list != NULL) {
  269. PwMap* head = list->head;
  270. while (head != NULL) {
  271. PwMap* cur = head;
  272. head = head->next;
  273. memset(cur, 0, sizeof(PwMap));
  274. free(cur);
  275. }
  276. }
  277. }
  278. static const char samplePasswordBuffer[] =
  279. "jill:upthehill\n"
  280. "jack:fetchapail\n";
  281. static const char samplePublicKeyEccBuffer[] =
  282. "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAA"
  283. "BBBNkI5JTP6D0lF42tbxX19cE87hztUS6FSDoGvPfiU0CgeNSbI+aFdKIzTP5CQEJSvm25"
  284. "qUzgDtH7oyaQROUnNvk= hansel\n"
  285. "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAA"
  286. "BBBKAtH8cqaDbtJFjtviLobHBmjCtG56DMkP6A4M2H9zX2/YCg1h9bYS7WHd9UQDwXO1Hh"
  287. "IZzRYecXh7SG9P4GhRY= gretel\n";
  288. static const char samplePublicKeyRsaBuffer[] =
  289. "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9P3ZFowOsONXHD5MwWiCciXytBRZGho"
  290. "MNiisWSgUs5HdHcACuHYPi2W6Z1PBFmBWT9odOrGRjoZXJfDDoPi+j8SSfDGsc/hsCmc3G"
  291. "p2yEhUZUEkDhtOXyqjns1ickC9Gh4u80aSVtwHRnJZh9xPhSq5tLOhId4eP61s+a5pwjTj"
  292. "nEhBaIPUJO2C/M0pFnnbZxKgJlX7t1Doy7h5eXxviymOIvaCZKU+x5OopfzM/wFkey0EPW"
  293. "NmzI5y/+pzU5afsdeEWdiQDIQc80H6Pz8fsoFPvYSG+s4/wz0duu7yeeV1Ypoho65Zr+pE"
  294. "nIf7dO0B8EblgWt+ud+JI8wrAhfE4x hansel\n"
  295. "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqDwRVTRVk/wjPhoo66+Mztrc31KsxDZ"
  296. "+kAV0139PHQ+wsueNpba6jNn5o6mUTEOrxrz0LMsDJOBM7CmG0983kF4gRIihECpQ0rcjO"
  297. "P6BSfbVTE9mfIK5IsUiZGd8SoE9kSV2pJ2FvZeBQENoAxEFk0zZL9tchPS+OCUGbK4SDjz"
  298. "uNZl/30Mczs73N3MBzi6J1oPo7sFlqzB6ecBjK2Kpjus4Y1rYFphJnUxtKvB0s+hoaadru"
  299. "biE57dK6BrH5iZwVLTQKux31uCJLPhiktI3iLbdlGZEctJkTasfVSsUizwVIyRjhVKmbdI"
  300. "RGwkU38D043AR1h0mUoGCPIKuqcFMf gretel\n";
  301. static int LoadPasswordBuffer(byte* buf, word32 bufSz, PwMapList* list)
  302. {
  303. char* str = (char*)buf;
  304. char* delimiter;
  305. char* username;
  306. char* password;
  307. /* Each line of passwd.txt is in the format
  308. * username:password\n
  309. * This function modifies the passed-in buffer. */
  310. if (list == NULL)
  311. return -1;
  312. if (buf == NULL || bufSz == 0)
  313. return 0;
  314. while (*str != 0) {
  315. delimiter = strchr(str, ':');
  316. if (delimiter == NULL) {
  317. return -1;
  318. }
  319. username = str;
  320. *delimiter = 0;
  321. password = delimiter + 1;
  322. str = strchr(password, '\n');
  323. if (str == NULL) {
  324. return -1;
  325. }
  326. *str = 0;
  327. str++;
  328. if (PwMapNew(list, WOLFSSH_USERAUTH_PASSWORD,
  329. (byte*)username, (word32)strlen(username),
  330. (byte*)password, (word32)strlen(password)) == NULL ) {
  331. return -1;
  332. }
  333. }
  334. return 0;
  335. }
  336. static int LoadPublicKeyBuffer(byte* buf, word32 bufSz, PwMapList* list)
  337. {
  338. char* str = (char*)buf;
  339. char* delimiter;
  340. byte* publicKey64;
  341. word32 publicKey64Sz;
  342. byte* username;
  343. word32 usernameSz;
  344. byte publicKey[300];
  345. word32 publicKeySz;
  346. /* Each line of passwd.txt is in the format
  347. * ssh-rsa AAAB3BASE64ENCODEDPUBLICKEYBLOB username\n
  348. * This function modifies the passed-in buffer. */
  349. if (list == NULL)
  350. return -1;
  351. if (buf == NULL || bufSz == 0)
  352. return 0;
  353. while (*str != 0) {
  354. /* Skip the public key type. This example will always be ssh-rsa. */
  355. delimiter = strchr(str, ' ');
  356. if (delimiter == NULL) {
  357. return -1;
  358. }
  359. str = delimiter + 1;
  360. delimiter = strchr(str, ' ');
  361. if (delimiter == NULL) {
  362. return -1;
  363. }
  364. publicKey64 = (byte*)str;
  365. *delimiter = 0;
  366. publicKey64Sz = (word32)(delimiter - str);
  367. str = delimiter + 1;
  368. delimiter = strchr(str, '\n');
  369. if (delimiter == NULL) {
  370. return -1;
  371. }
  372. username = (byte*)str;
  373. *delimiter = 0;
  374. usernameSz = (word32)(delimiter - str);
  375. str = delimiter + 1;
  376. publicKeySz = sizeof(publicKey);
  377. if (Base64_Decode(publicKey64, publicKey64Sz,
  378. publicKey, &publicKeySz) != 0) {
  379. return -1;
  380. }
  381. if (PwMapNew(list, WOLFSSH_USERAUTH_PUBLICKEY,
  382. username, usernameSz,
  383. publicKey, publicKeySz) == NULL ) {
  384. return -1;
  385. }
  386. }
  387. return 0;
  388. }
  389. static int wsUserAuth(byte authType,
  390. WS_UserAuthData* authData,
  391. void* ctx)
  392. {
  393. PwMapList* list;
  394. PwMap* map;
  395. byte authHash[SHA256_DIGEST_SIZE];
  396. if (ctx == NULL) {
  397. printf("wsUserAuth: ctx not set");
  398. return WOLFSSH_USERAUTH_FAILURE;
  399. }
  400. if (authType != WOLFSSH_USERAUTH_PASSWORD &&
  401. authType != WOLFSSH_USERAUTH_PUBLICKEY) {
  402. return WOLFSSH_USERAUTH_FAILURE;
  403. }
  404. /* Hash the password or public key with its length. */
  405. {
  406. Sha256 sha;
  407. byte flatSz[4];
  408. wc_InitSha256(&sha);
  409. if (authType == WOLFSSH_USERAUTH_PASSWORD) {
  410. c32toa(authData->sf.password.passwordSz, flatSz);
  411. wc_Sha256Update(&sha, flatSz, sizeof(flatSz));
  412. wc_Sha256Update(&sha,
  413. authData->sf.password.password,
  414. authData->sf.password.passwordSz);
  415. }
  416. else if (authType == WOLFSSH_USERAUTH_PUBLICKEY) {
  417. c32toa(authData->sf.publicKey.publicKeySz, flatSz);
  418. wc_Sha256Update(&sha, flatSz, sizeof(flatSz));
  419. wc_Sha256Update(&sha,
  420. authData->sf.publicKey.publicKey,
  421. authData->sf.publicKey.publicKeySz);
  422. }
  423. wc_Sha256Final(&sha, authHash);
  424. }
  425. list = (PwMapList*)ctx;
  426. map = list->head;
  427. while (map != NULL) {
  428. if (authData->usernameSz == map->usernameSz &&
  429. memcmp(authData->username, map->username, map->usernameSz) == 0) {
  430. if (authData->type == map->type) {
  431. if (memcmp(map->p, authHash, SHA256_DIGEST_SIZE) == 0) {
  432. return WOLFSSH_USERAUTH_SUCCESS;
  433. }
  434. else {
  435. return (authType == WOLFSSH_USERAUTH_PASSWORD ?
  436. WOLFSSH_USERAUTH_INVALID_PASSWORD :
  437. WOLFSSH_USERAUTH_INVALID_PUBLICKEY);
  438. }
  439. }
  440. else {
  441. return WOLFSSH_USERAUTH_INVALID_AUTHTYPE;
  442. }
  443. }
  444. map = map->next;
  445. }
  446. return WOLFSSH_USERAUTH_INVALID_USER;
  447. }
  448. static void ssh_server(void *arg)
  449. {
  450. (void)arg;
  451. WSTARTTCP();
  452. #ifdef DEBUG_WOLFSSH
  453. wolfSSH_Debugging_ON();
  454. #endif
  455. wolfSSH_Init();
  456. WOLFSSH_CTX* ctx = NULL;
  457. PwMapList pwMapList;
  458. SOCKET_T listenFd = 0;
  459. word32 defaultHighwater = EXAMPLE_HIGHWATER_MARK;
  460. word32 threadCount = 0;
  461. word16 port = 22;
  462. const char multipleConnections = 1;
  463. char useEcc = 1;
  464. int ch;
  465. char nonBlock = 0;
  466. if (wolfSSH_Init() != WS_SUCCESS) {
  467. printf("Couldn't initialize wolfSSH.\n");
  468. exit(EXIT_FAILURE);
  469. }
  470. ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL);
  471. if (ctx == NULL) {
  472. printf("Couldn't allocate SSH CTX data.\n");
  473. exit(EXIT_FAILURE);
  474. }
  475. memset(&pwMapList, 0, sizeof(pwMapList));
  476. wolfSSH_SetUserAuth(ctx, wsUserAuth);
  477. wolfSSH_CTX_SetBanner(ctx, serverBanner);
  478. {
  479. const char* bufName;
  480. byte buf[SCRATCH_BUFFER_SZ];
  481. word32 bufSz;
  482. bufSz = load_key(useEcc, buf, SCRATCH_BUFFER_SZ);
  483. if (bufSz == 0) {
  484. printf("Couldn't load key.\n");
  485. exit(EXIT_FAILURE);
  486. }
  487. if (wolfSSH_CTX_UsePrivateKey_buffer(ctx, buf, bufSz,
  488. WOLFSSH_FORMAT_ASN1) < 0) {
  489. printf("Couldn't use key buffer.\n");
  490. exit(EXIT_FAILURE);
  491. }
  492. bufSz = (word32)strlen(samplePasswordBuffer);
  493. memcpy(buf, samplePasswordBuffer, bufSz);
  494. buf[bufSz] = 0;
  495. LoadPasswordBuffer(buf, bufSz, &pwMapList);
  496. bufName = useEcc ? samplePublicKeyEccBuffer :
  497. samplePublicKeyRsaBuffer;
  498. bufSz = (word32)strlen(bufName);
  499. memcpy(buf, bufName, bufSz);
  500. buf[bufSz] = 0;
  501. LoadPublicKeyBuffer(buf, bufSz, &pwMapList);
  502. }
  503. tcp_listen(&listenFd, &port, 1, false, false);
  504. do {
  505. SOCKET_T clientFd = 0;
  506. SOCKADDR_IN_T clientAddr;
  507. socklen_t clientAddrSz = sizeof(clientAddr);
  508. #ifndef SINGLE_THREADED
  509. THREAD_TYPE thread;
  510. #endif
  511. WOLFSSH* ssh;
  512. thread_ctx_t* threadCtx;
  513. threadCtx = (thread_ctx_t*)malloc(sizeof(thread_ctx_t));
  514. if (threadCtx == NULL) {
  515. printf("Couldn't allocate thread context data.\n");
  516. exit(EXIT_FAILURE);
  517. }
  518. ssh = wolfSSH_new(ctx);
  519. if (ssh == NULL) {
  520. printf("Couldn't allocate SSH data.\n");
  521. exit(EXIT_FAILURE);
  522. }
  523. wolfSSH_SetUserAuthCtx(ssh, &pwMapList);
  524. /* Use the session object for its own highwater callback ctx */
  525. if (defaultHighwater > 0) {
  526. wolfSSH_SetHighwaterCtx(ssh, (void*)ssh);
  527. wolfSSH_SetHighwater(ssh, defaultHighwater);
  528. }
  529. clientFd = accept(listenFd, (struct sockaddr*)&clientAddr,
  530. &clientAddrSz);
  531. if (clientFd == -1)
  532. err_sys("tcp accept failed");
  533. if (nonBlock)
  534. tcp_set_nonblocking(&clientFd);
  535. wolfSSH_set_fd(ssh, (int)clientFd);
  536. threadCtx->ssh = ssh;
  537. threadCtx->fd = clientFd;
  538. threadCtx->id = threadCount++;
  539. threadCtx->nonBlock = nonBlock;
  540. #ifndef SINGLE_THREADED
  541. ThreadStart(server_worker, threadCtx, &thread);
  542. if (multipleConnections)
  543. ThreadDetach(thread);
  544. else
  545. ThreadJoin(thread);
  546. #else
  547. server_worker(threadCtx);
  548. #endif /* SINGLE_THREADED */
  549. } while (multipleConnections);
  550. PwMapListDelete(&pwMapList);
  551. wolfSSH_CTX_free(ctx);
  552. if (wolfSSH_Cleanup() != WS_SUCCESS) {
  553. printf("Couldn't clean up wolfSSH.\n");
  554. exit(EXIT_FAILURE);
  555. }
  556. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  557. wc_ecc_fp_free(); /* free per thread cache */
  558. #endif
  559. return 0;
  560. }
  561. void ssh_server_init(void)
  562. {
  563. xTaskCreate(ssh_server, ( char * ) "ssh_server", 24*configMINIMAL_STACK_SIZE + EXAMPLE_BUFFER_SZ, NULL, tskIDLE_PRIORITY + 1, NULL);
  564. }