server.c 18 KB

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