protocol_lws_sshd_demo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * ws protocol handler plugin for sshd demo
  3. *
  4. * Written in 2010-2019 by Andy Green <[email protected]>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. *
  9. * The person who associated a work with this deed has dedicated
  10. * the work to the public domain by waiving all of his or her rights
  11. * to the work worldwide under copyright law, including all related
  12. * and neighboring rights, to the extent allowed by law. You can copy,
  13. * modify, distribute and perform the work, even for commercial purposes,
  14. * all without asking permission.
  15. *
  16. * These test plugins are intended to be adapted for use in your code, which
  17. * may be proprietary. So unlike the library itself, they are licensed
  18. * Public Domain.
  19. */
  20. #if !defined (LWS_PLUGIN_STATIC)
  21. #define LWS_DLL
  22. #define LWS_INTERNAL
  23. #include <libwebsockets.h>
  24. #endif
  25. #include <lws-ssh.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <errno.h>
  29. #define TEST_SERVER_KEY_PATH "/etc/lws-test-sshd-server-key"
  30. struct per_vhost_data__lws_sshd_demo {
  31. const struct lws_protocols *ssh_base_protocol;
  32. int privileged_fd;
  33. };
  34. /*
  35. * This is a copy of the lws ssh test public key, you can find it in
  36. * /usr[/local]/share/libwebsockets-test-server/lws-ssh-test-keys.pub
  37. * and the matching private key there too in .../lws-ssh-test-keys
  38. *
  39. * If the vhost with this protocol is using localhost:2222, you can test with
  40. * the matching private key like this:
  41. *
  42. * ssh -p 2222 -i /usr/local/share/libwebsockets-test-server/lws-ssh-test-keys [email protected]
  43. *
  44. * These keys are distributed for testing! Don't use them on a real system
  45. * unless you want anyone with a copy of lws to access it.
  46. */
  47. static const char *authorized_key =
  48. "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCnWiP+c+kSD6Lk+C6NA9KruApa45sbt"
  49. "94/dxT0bCITlAA/+PBk6mR1lwWgXYozOMdrHrqx34piqDyXnc4HabqCaOm/FrYhkCPL8z"
  50. "a26PMYqteSosuwKv//5iT6ZWhNnsMwExBwtV6MIq0MxAeWqxRnYNWpNM8iN6sFzkdG/YF"
  51. "dyHrIBTgwzM77NLCMl6GEkJErRCFppC2SwYxGa3BRrgUwX3LkV8HpMIaYHFo1Qgj7Scqm"
  52. "HwS2R75SOqi2aOWDpKjznARg9JgzDWSQi4seBMV2oL0BTwJANSDf+p0sQLsaKGJhpVpBQ"
  53. "yS2wUeyuGyytupWzEluQrajMZq52iotcogv5BfeulfTTFbJP4kuHOsSP0lsQ2lpMDQANS"
  54. "HEvXxzHJLDLXM9gXJzwJ+ZiRt6R+bfmP1nfN3MiWtxcIbBanWwQK6xTCKBe4wPaKta5EU"
  55. "6wsLPeakOIVzoeaOu/HsbtPZlwX0Mu/oUFcfKyKAhlkU15MOAIEfUPo8Yh52bWMlIlpZa"
  56. "4xWbLMGw3GrsrPPdcsAauyqvY4/NjjWQbWhP1SuUfvv5709PIiOUjVKK2HUwmR1ouch6X"
  57. "MQGXfMR1h1Wjvc+bkNs17gCIrQnFilAZLC3Sm3Opiz/4LO99Hw448G0RM2vQn0mJE46w"
  58. "Eu/B10U6Jf4Efojhh1dk85BD1LTIb+N3Q== ssh-test-key@lws";
  59. enum states {
  60. SSH_TEST_GREET,
  61. SSH_TEST_PRESSED,
  62. SSH_TEST_DONE,
  63. };
  64. static const char * const strings[] =
  65. {
  66. /* SSH_TEST_GREET */
  67. "Thanks for logging to lws sshd server demo.\n\r"
  68. "\n\r"
  69. "This demo is very simple, it waits for you to press\n\r"
  70. "a key, and acknowledges it. Then press another key\n\r"
  71. "and it will exit. But actually that demos the basic\n\r"
  72. "sshd functions underneath. You can use the ops struct\n\r"
  73. "members to add a pty / shell or whatever you want.\n\r"
  74. "\n\r"
  75. "Press a key...\n\r",
  76. /* SSH_TEST_PRESSED */
  77. "Thanks for pressing a key. Press another to exit.\n\r",
  78. /* SSH_TEST_DONE */
  79. "Bye!\n\r"
  80. };
  81. struct sshd_instance_priv {
  82. struct lws *wsi;
  83. enum states state;
  84. const char *ptr;
  85. int pos;
  86. int len;
  87. };
  88. static void
  89. enter_state(struct sshd_instance_priv *priv, enum states state)
  90. {
  91. priv->state = state;
  92. priv->ptr = strings[state];
  93. priv->pos = 0;
  94. priv->len = (int)strlen(priv->ptr);
  95. lws_callback_on_writable(priv->wsi);
  96. }
  97. /* ops: channel lifecycle */
  98. static int
  99. ssh_ops_channel_create(struct lws *wsi, void **_priv)
  100. {
  101. struct sshd_instance_priv *priv;
  102. priv = malloc(sizeof(struct sshd_instance_priv));
  103. *_priv = priv;
  104. if (!priv)
  105. return 1;
  106. memset(priv, 0, sizeof(*priv));
  107. priv->wsi = wsi;
  108. return 0;
  109. }
  110. static int
  111. ssh_ops_channel_destroy(void *_priv)
  112. {
  113. struct sshd_instance_priv *priv = _priv;
  114. free(priv);
  115. return 0;
  116. }
  117. /* ops: IO */
  118. static int
  119. ssh_ops_tx_waiting(void *_priv)
  120. {
  121. struct sshd_instance_priv *priv = _priv;
  122. if (priv->state == SSH_TEST_DONE &&
  123. priv->pos == priv->len)
  124. return -1; /* exit */
  125. if (priv->pos != priv->len)
  126. return LWS_STDOUT;
  127. return 0;
  128. }
  129. static size_t
  130. ssh_ops_tx(void *_priv, int stdch, uint8_t *buf, size_t len)
  131. {
  132. struct sshd_instance_priv *priv = _priv;
  133. size_t chunk = len;
  134. if (stdch != LWS_STDOUT)
  135. return 0;
  136. if ((size_t)(priv->len - priv->pos) < chunk)
  137. chunk = priv->len - priv->pos;
  138. if (!chunk)
  139. return 0;
  140. memcpy(buf, priv->ptr + priv->pos, chunk);
  141. priv->pos += (int)chunk;
  142. if (priv->state == SSH_TEST_DONE && priv->pos == priv->len) {
  143. /*
  144. * we are sending the last thing we want to send
  145. * before exiting. Make it ask again at ssh_ops_tx_waiting()
  146. * and we will exit then, after this has been sent
  147. */
  148. lws_callback_on_writable(priv->wsi);
  149. }
  150. return chunk;
  151. }
  152. static int
  153. ssh_ops_rx(void *_priv, struct lws *wsi, const uint8_t *buf, uint32_t len)
  154. {
  155. struct sshd_instance_priv *priv = _priv;
  156. if (priv->state < SSH_TEST_DONE)
  157. enter_state(priv, priv->state + 1);
  158. else
  159. return -1;
  160. return 0;
  161. }
  162. /* ops: storage for the (autogenerated) persistent server key */
  163. static size_t
  164. ssh_ops_get_server_key(struct lws *wsi, uint8_t *buf, size_t len)
  165. {
  166. struct per_vhost_data__lws_sshd_demo *vhd =
  167. (struct per_vhost_data__lws_sshd_demo *)
  168. lws_protocol_vh_priv_get(lws_get_vhost(wsi),
  169. lws_get_protocol(wsi));
  170. int n;
  171. if (lseek(vhd->privileged_fd, 0, SEEK_SET) < 0)
  172. return 0;
  173. n = read(vhd->privileged_fd, buf, (int)len);
  174. if (n < 0) {
  175. lwsl_err("%s: read failed: %d\n", __func__, n);
  176. n = 0;
  177. }
  178. return n;
  179. }
  180. static size_t
  181. ssh_ops_set_server_key(struct lws *wsi, uint8_t *buf, size_t len)
  182. {
  183. struct per_vhost_data__lws_sshd_demo *vhd =
  184. (struct per_vhost_data__lws_sshd_demo *)
  185. lws_protocol_vh_priv_get(lws_get_vhost(wsi),
  186. lws_get_protocol(wsi));
  187. int n;
  188. n = write(vhd->privileged_fd, buf, (int)len);
  189. if (n < 0) {
  190. lwsl_err("%s: read failed: %d\n", __func__, errno);
  191. n = 0;
  192. }
  193. return n;
  194. }
  195. /* ops: auth */
  196. static int
  197. ssh_ops_is_pubkey_authorized(const char *username, const char *type,
  198. const uint8_t *peer, int peer_len)
  199. {
  200. char *aps, *p, *ps;
  201. int n = (int)strlen(type), alen = 2048, ret = 2, len;
  202. size_t s = 0;
  203. lwsl_info("%s: checking pubkey for %s\n", __func__, username);
  204. s = strlen(authorized_key) + 1;
  205. aps = malloc(s);
  206. if (!aps) {
  207. lwsl_notice("OOM 1\n");
  208. goto bail_p1;
  209. }
  210. memcpy(aps, authorized_key, s);
  211. /* we only understand RSA */
  212. if (strcmp(type, "ssh-rsa")) {
  213. lwsl_notice("type is not ssh-rsa\n");
  214. goto bail_p1;
  215. }
  216. p = aps;
  217. if (strncmp(p, type, n)) {
  218. lwsl_notice("lead-in string does not match %s\n", type);
  219. goto bail_p1;
  220. }
  221. p += n;
  222. if (*p != ' ') {
  223. lwsl_notice("missing space at end of lead-in\n");
  224. goto bail_p1;
  225. }
  226. p++;
  227. ps = malloc(alen);
  228. if (!ps) {
  229. lwsl_notice("OOM 2\n");
  230. free(aps);
  231. goto bail;
  232. }
  233. len = lws_b64_decode_string(p, ps, alen);
  234. free(aps);
  235. if (len < 0) {
  236. lwsl_notice("key too big\n");
  237. goto bail;
  238. }
  239. if (peer_len > len) {
  240. lwsl_notice("peer_len %d bigger than decoded len %d\n",
  241. peer_len, len);
  242. goto bail;
  243. }
  244. /*
  245. * once we are past that, it's the same <len32>name
  246. * <len32>E<len32>N that the peer sends us
  247. */
  248. if (memcmp(peer, ps, peer_len)) {
  249. lwsl_info("%s: factors mismatch, rejecting key\n", __func__);
  250. goto bail;
  251. }
  252. lwsl_info("pubkey authorized\n");
  253. ret = 0;
  254. bail:
  255. free(ps);
  256. return ret;
  257. bail_p1:
  258. if (aps)
  259. free(aps);
  260. return 1;
  261. }
  262. static int
  263. ssh_ops_shell(void *_priv, struct lws *wsi, lws_ssh_finish_exec finish, void *finish_handle)
  264. {
  265. struct sshd_instance_priv *priv = _priv;
  266. /* for this demo, we don't open a real shell */
  267. enter_state(priv, SSH_TEST_GREET);
  268. return 0;
  269. }
  270. /* ops: banner */
  271. static size_t
  272. ssh_ops_banner(char *buf, size_t max_len, char *lang, size_t max_lang_len)
  273. {
  274. int n = lws_snprintf(buf, max_len, "\n"
  275. " |\\---/| lws-ssh Test Server\n"
  276. " | o_o | SSH Terminal Server\n"
  277. " \\_^_/ Copyright (C) 2017 Crash Barrier Ltd\n\n");
  278. lws_snprintf(lang, max_lang_len, "en/US");
  279. return n;
  280. }
  281. static void
  282. ssh_ops_disconnect_reason(uint32_t reason, const char *desc,
  283. const char *desc_lang)
  284. {
  285. lwsl_notice("DISCONNECT reason 0x%X, %s (lang %s)\n", reason, desc,
  286. desc_lang);
  287. }
  288. static const struct lws_ssh_ops ssh_ops = {
  289. .channel_create = ssh_ops_channel_create,
  290. .channel_destroy = ssh_ops_channel_destroy,
  291. .tx_waiting = ssh_ops_tx_waiting,
  292. .tx = ssh_ops_tx,
  293. .rx = ssh_ops_rx,
  294. .get_server_key = ssh_ops_get_server_key,
  295. .set_server_key = ssh_ops_set_server_key,
  296. .set_env = NULL,
  297. .pty_req = NULL,
  298. .child_process_io = NULL,
  299. .child_process_terminated = NULL,
  300. .exec = NULL,
  301. .shell = ssh_ops_shell,
  302. .is_pubkey_authorized = ssh_ops_is_pubkey_authorized,
  303. .banner = ssh_ops_banner,
  304. .disconnect_reason = ssh_ops_disconnect_reason,
  305. .server_string = "SSH-2.0-Libwebsockets",
  306. .api_version = 2,
  307. };
  308. static int
  309. callback_lws_sshd_demo(struct lws *wsi, enum lws_callback_reasons reason,
  310. void *user, void *in, size_t len)
  311. {
  312. struct per_vhost_data__lws_sshd_demo *vhd =
  313. (struct per_vhost_data__lws_sshd_demo *)
  314. lws_protocol_vh_priv_get(lws_get_vhost(wsi),
  315. lws_get_protocol(wsi));
  316. switch (reason) {
  317. case LWS_CALLBACK_PROTOCOL_INIT:
  318. vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
  319. lws_get_protocol(wsi),
  320. sizeof(struct per_vhost_data__lws_sshd_demo));
  321. /*
  322. * During this we still have the privs / caps we were started
  323. * with. So open an fd on the server key, either just for read
  324. * or for creat / trunc if doesn't exist. This allows us to
  325. * deal with it down /etc/.. when just after this we will lose
  326. * the privileges needed to read / write /etc/...
  327. */
  328. vhd->privileged_fd = lws_open(TEST_SERVER_KEY_PATH, O_RDONLY);
  329. if (vhd->privileged_fd == -1)
  330. vhd->privileged_fd = lws_open(TEST_SERVER_KEY_PATH,
  331. O_CREAT | O_TRUNC | O_RDWR, 0600);
  332. if (vhd->privileged_fd == -1) {
  333. lwsl_err("%s: Can't open %s\n", __func__,
  334. TEST_SERVER_KEY_PATH);
  335. return -1;
  336. }
  337. break;
  338. case LWS_CALLBACK_PROTOCOL_DESTROY:
  339. close(vhd->privileged_fd);
  340. break;
  341. case LWS_CALLBACK_VHOST_CERT_AGING:
  342. break;
  343. case LWS_CALLBACK_EVENT_WAIT_CANCELLED:
  344. break;
  345. default:
  346. if (!vhd->ssh_base_protocol) {
  347. vhd->ssh_base_protocol = lws_vhost_name_to_protocol(
  348. lws_get_vhost(wsi),
  349. "lws-ssh-base");
  350. if (vhd->ssh_base_protocol)
  351. user = lws_adjust_protocol_psds(wsi,
  352. vhd->ssh_base_protocol->per_session_data_size);
  353. }
  354. if (vhd->ssh_base_protocol)
  355. return vhd->ssh_base_protocol->callback(wsi, reason,
  356. user, in, len);
  357. else
  358. lwsl_notice("can't find lws-ssh-base\n");
  359. break;
  360. }
  361. return 0;
  362. }
  363. #define LWS_PLUGIN_PROTOCOL_LWS_SSHD_DEMO \
  364. { \
  365. "lws-sshd-demo", \
  366. callback_lws_sshd_demo, \
  367. 0, \
  368. 1024, /* rx buf size must be >= permessage-deflate rx size */ \
  369. 0, (void *)&ssh_ops, 0 \
  370. }
  371. #if !defined (LWS_PLUGIN_STATIC)
  372. static const struct lws_protocols protocols[] = {
  373. LWS_PLUGIN_PROTOCOL_LWS_SSHD_DEMO
  374. };
  375. LWS_VISIBLE const lws_plugin_protocol_t lws_sshd_demo = {
  376. .hdr = {
  377. "lws sshd demo",
  378. "lws_protocol_plugin",
  379. LWS_PLUGIN_API_MAGIC
  380. },
  381. .protocols = protocols,
  382. .count_protocols = LWS_ARRAY_SIZE(protocols),
  383. .extensions = NULL,
  384. .count_extensions = 0,
  385. };
  386. #endif