common-runopts.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Dropbear - a SSH2 server
  3. *
  4. * Copyright (c) 2002,2003 Matt Johnston
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE. */
  24. #include "includes.h"
  25. #include "runopts.h"
  26. #include "signkey.h"
  27. #include "buffer.h"
  28. #include "dbutil.h"
  29. #include "auth.h"
  30. #include "algo.h"
  31. #include "dbrandom.h"
  32. runopts opts; /* GLOBAL */
  33. /* returns success or failure, and the keytype in *type. If we want
  34. * to restrict the type, type can contain a type to return */
  35. int readhostkey(const char * filename, sign_key * hostkey,
  36. enum signkey_type *type) {
  37. int ret = DROPBEAR_FAILURE;
  38. buffer *buf;
  39. buf = buf_new(MAX_PRIVKEY_SIZE);
  40. if (buf_readfile(buf, filename) == DROPBEAR_FAILURE) {
  41. goto out;
  42. }
  43. buf_setpos(buf, 0);
  44. addrandom(buf_getptr(buf, buf->len), buf->len);
  45. if (buf_get_priv_key(buf, hostkey, type) == DROPBEAR_FAILURE) {
  46. goto out;
  47. }
  48. ret = DROPBEAR_SUCCESS;
  49. out:
  50. buf_burn_free(buf);
  51. return ret;
  52. }
  53. #if DROPBEAR_USER_ALGO_LIST
  54. void
  55. parse_ciphers_macs() {
  56. int printed_help = 0;
  57. if (opts.cipher_list) {
  58. if (strcmp(opts.cipher_list, "help") == 0) {
  59. char *ciphers = algolist_string(sshciphers);
  60. dropbear_log(LOG_INFO, "Available ciphers: %s", ciphers);
  61. m_free(ciphers);
  62. printed_help = 1;
  63. } else {
  64. if (check_user_algos(opts.cipher_list, sshciphers, "cipher") == 0) {
  65. dropbear_exit("No valid ciphers specified for '-c'");
  66. }
  67. }
  68. }
  69. if (opts.mac_list) {
  70. if (strcmp(opts.mac_list, "help") == 0) {
  71. char *macs = algolist_string(sshhashes);
  72. dropbear_log(LOG_INFO, "Available MACs: %s", macs);
  73. m_free(macs);
  74. printed_help = 1;
  75. } else {
  76. if (check_user_algos(opts.mac_list, sshhashes, "MAC") == 0) {
  77. dropbear_exit("No valid MACs specified for '-m'");
  78. }
  79. }
  80. }
  81. if (printed_help) {
  82. dropbear_exit(".");
  83. }
  84. }
  85. #endif
  86. void print_version() {
  87. fprintf(stderr, "Dropbear v%s\n", DROPBEAR_VERSION);
  88. }
  89. void parse_recv_window(const char* recv_window_arg) {
  90. int ret;
  91. unsigned int rw;
  92. ret = m_str_to_uint(recv_window_arg, &rw);
  93. if (ret == DROPBEAR_FAILURE || rw == 0 || rw > MAX_RECV_WINDOW) {
  94. if (rw > MAX_RECV_WINDOW) {
  95. opts.recv_window = MAX_RECV_WINDOW;
  96. }
  97. dropbear_log(LOG_WARNING, "Bad recv window '%s', using %d",
  98. recv_window_arg, opts.recv_window);
  99. } else {
  100. opts.recv_window = rw;
  101. }
  102. }
  103. /* Splits addr:port. Handles IPv6 [2001:0011::4]:port style format.
  104. Returns first/second parts as malloced strings, second will
  105. be NULL if no separator is found.
  106. :port -> (NULL, "port")
  107. port -> (port, NULL)
  108. addr:port (addr, port)
  109. addr: -> (addr, "")
  110. Returns DROPBEAR_SUCCESS/DROPBEAR_FAILURE */
  111. int split_address_port(const char* spec, char **first, char ** second) {
  112. char *spec_copy = NULL, *addr = NULL, *colon = NULL;
  113. int ret = DROPBEAR_FAILURE;
  114. *first = NULL;
  115. *second = NULL;
  116. spec_copy = m_strdup(spec);
  117. addr = spec_copy;
  118. if (*addr == '[') {
  119. addr++;
  120. colon = strchr(addr, ']');
  121. if (!colon) {
  122. dropbear_log(LOG_WARNING, "Bad address '%s'", spec);
  123. goto out;
  124. }
  125. *colon = '\0';
  126. colon++;
  127. if (*colon == '\0') {
  128. /* No port part */
  129. colon = NULL;
  130. } else if (*colon != ':') {
  131. dropbear_log(LOG_WARNING, "Bad address '%s'", spec);
  132. goto out;
  133. }
  134. } else {
  135. /* search for ':', that separates address and port */
  136. colon = strrchr(addr, ':');
  137. }
  138. /* colon points to ':' now, or is NULL */
  139. if (colon) {
  140. /* Split the address/port */
  141. *colon = '\0';
  142. colon++;
  143. *second = m_strdup(colon);
  144. }
  145. if (strlen(addr)) {
  146. *first = m_strdup(addr);
  147. }
  148. ret = DROPBEAR_SUCCESS;
  149. out:
  150. m_free(spec_copy);
  151. return ret;
  152. }