s5.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* Copyright StrongLoop, Inc. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "s5.h"
  22. #include <errno.h>
  23. #include <stdint.h>
  24. #include <stdlib.h> /* abort() */
  25. #include <string.h> /* memset() */
  26. enum {
  27. s5_version,
  28. s5_nmethods,
  29. s5_methods,
  30. s5_auth_pw_version,
  31. s5_auth_pw_userlen,
  32. s5_auth_pw_username,
  33. s5_auth_pw_passlen,
  34. s5_auth_pw_password,
  35. s5_req_version,
  36. s5_req_cmd,
  37. s5_req_reserved,
  38. s5_req_atyp,
  39. s5_req_atyp_host,
  40. s5_req_daddr,
  41. s5_req_dport0,
  42. s5_req_dport1,
  43. s5_dead
  44. };
  45. void s5_init(s5_ctx *cx) {
  46. memset(cx, 0, sizeof(*cx));
  47. cx->state = s5_version;
  48. }
  49. s5_err s5_parse(s5_ctx *cx, uint8_t **data, size_t *size) {
  50. s5_err err;
  51. uint8_t *p;
  52. uint8_t c;
  53. size_t i;
  54. size_t n;
  55. p = *data;
  56. n = *size;
  57. i = 0;
  58. while (i < n) {
  59. c = p[i];
  60. i += 1;
  61. switch (cx->state) {
  62. case s5_version:
  63. if (c != 5) {
  64. err = s5_bad_version;
  65. goto out;
  66. }
  67. cx->state = s5_nmethods;
  68. break;
  69. case s5_nmethods:
  70. cx->arg0 = 0;
  71. cx->arg1 = c; /* Number of bytes to read. */
  72. cx->state = s5_methods;
  73. break;
  74. case s5_methods:
  75. if (cx->arg0 < cx->arg1) {
  76. switch (c) {
  77. case 0:
  78. cx->methods |= S5_AUTH_NONE;
  79. break;
  80. case 1:
  81. cx->methods |= S5_AUTH_GSSAPI;
  82. break;
  83. case 2:
  84. cx->methods |= S5_AUTH_PASSWD;
  85. break;
  86. /* Ignore everything we don't understand. */
  87. }
  88. cx->arg0 += 1;
  89. }
  90. if (cx->arg0 == cx->arg1) {
  91. err = s5_auth_select;
  92. goto out;
  93. }
  94. break;
  95. case s5_auth_pw_version:
  96. if (c != 1) {
  97. err = s5_bad_version;
  98. goto out;
  99. }
  100. cx->state = s5_auth_pw_userlen;
  101. break;
  102. case s5_auth_pw_userlen:
  103. cx->arg0 = 0;
  104. cx->userlen = c;
  105. cx->state = s5_auth_pw_username;
  106. break;
  107. case s5_auth_pw_username:
  108. if (cx->arg0 < cx->userlen) {
  109. cx->username[cx->arg0] = c;
  110. cx->arg0 += 1;
  111. }
  112. if (cx->arg0 == cx->userlen) {
  113. cx->username[cx->userlen] = '\0';
  114. cx->state = s5_auth_pw_passlen;
  115. }
  116. break;
  117. case s5_auth_pw_passlen:
  118. cx->arg0 = 0;
  119. cx->passlen = c;
  120. cx->state = s5_auth_pw_password;
  121. break;
  122. case s5_auth_pw_password:
  123. if (cx->arg0 < cx->passlen) {
  124. cx->password[cx->arg0] = c;
  125. cx->arg0 += 1;
  126. }
  127. if (cx->arg0 == cx->passlen) {
  128. cx->password[cx->passlen] = '\0';
  129. cx->state = s5_req_version;
  130. err = s5_auth_verify;
  131. goto out;
  132. }
  133. break;
  134. case s5_req_version:
  135. if (c != 5) {
  136. err = s5_bad_version;
  137. goto out;
  138. }
  139. cx->state = s5_req_cmd;
  140. break;
  141. case s5_req_cmd:
  142. switch (c) {
  143. case 1: /* TCP connect */
  144. cx->cmd = s5_cmd_tcp_connect;
  145. break;
  146. case 3: /* UDP associate */
  147. cx->cmd = s5_cmd_udp_assoc;
  148. break;
  149. default:
  150. err = s5_bad_cmd;
  151. goto out;
  152. }
  153. cx->state = s5_req_reserved;
  154. break;
  155. case s5_req_reserved:
  156. cx->state = s5_req_atyp;
  157. break;
  158. case s5_req_atyp:
  159. cx->arg0 = 0;
  160. switch (c) {
  161. case 1: /* IPv4, four octets. */
  162. cx->state = s5_req_daddr;
  163. cx->atyp = s5_atyp_ipv4;
  164. cx->arg1 = 4;
  165. break;
  166. case 3: /* Hostname. First byte is length. */
  167. cx->state = s5_req_atyp_host;
  168. cx->atyp = s5_atyp_host;
  169. cx->arg1 = 0;
  170. break;
  171. case 4: /* IPv6, sixteen octets. */
  172. cx->state = s5_req_daddr;
  173. cx->atyp = s5_atyp_ipv6;
  174. cx->arg1 = 16;
  175. break;
  176. default:
  177. err = s5_bad_atyp;
  178. goto out;
  179. }
  180. break;
  181. case s5_req_atyp_host:
  182. cx->arg1 = c;
  183. cx->state = s5_req_daddr;
  184. break;
  185. case s5_req_daddr:
  186. if (cx->arg0 < cx->arg1) {
  187. cx->daddr[cx->arg0] = c;
  188. cx->arg0 += 1;
  189. }
  190. if (cx->arg0 == cx->arg1) {
  191. cx->daddr[cx->arg1] = '\0';
  192. cx->state = s5_req_dport0;
  193. }
  194. break;
  195. case s5_req_dport0:
  196. cx->dport = c << 8;
  197. cx->state = s5_req_dport1;
  198. break;
  199. case s5_req_dport1:
  200. cx->dport |= c;
  201. cx->state = s5_dead;
  202. err = s5_exec_cmd;
  203. goto out;
  204. case s5_dead:
  205. break;
  206. default:
  207. abort();
  208. }
  209. }
  210. err = s5_ok;
  211. out:
  212. *data = p + i;
  213. *size = n - i;
  214. return err;
  215. }
  216. unsigned int s5_auth_methods(const s5_ctx *cx) {
  217. return cx->methods;
  218. }
  219. int s5_select_auth(s5_ctx *cx, s5_auth_method method) {
  220. int err;
  221. err = 0;
  222. switch (method) {
  223. case S5_AUTH_NONE:
  224. cx->state = s5_req_version;
  225. break;
  226. case S5_AUTH_PASSWD:
  227. cx->state = s5_auth_pw_version;
  228. break;
  229. default:
  230. err = -EINVAL;
  231. }
  232. return err;
  233. }
  234. const char *s5_strerror(s5_err err) {
  235. #define S5_ERR_GEN(_, name, errmsg) case s5_ ## name: return errmsg;
  236. switch (err) {
  237. S5_ERR_MAP(S5_ERR_GEN)
  238. default: ; /* Silence s5_max_errors -Wswitch warning. */
  239. }
  240. #undef S5_ERR_GEN
  241. return "Unknown error.";
  242. }