server.c 18 KB

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