svr-authpubkeyoptions.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Dropbear - a SSH2 server
  3. *
  4. * Copyright (c) 2008 Frederic Moulins
  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. *
  25. * This file incorporates work covered by the following copyright and
  26. * permission notice:
  27. *
  28. * Author: Tatu Ylonen <ylo@cs.hut.fi>
  29. * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  30. * All rights reserved
  31. * As far as I am concerned, the code I have written for this software
  32. * can be used freely for any purpose. Any derived versions of this
  33. * software must be clearly marked as such, and if the derived work is
  34. * incompatible with the protocol description in the RFC file, it must be
  35. * called by a name other than "ssh" or "Secure Shell".
  36. *
  37. * This copyright and permission notice applies to the code parsing public keys
  38. * options string which can also be found in OpenSSH auth-options.c file
  39. * (auth_parse_options).
  40. *
  41. */
  42. /* Process pubkey options during a pubkey auth request */
  43. #include "includes.h"
  44. #include "session.h"
  45. #include "dbutil.h"
  46. #include "signkey.h"
  47. #include "auth.h"
  48. #ifdef ENABLE_SVR_PUBKEY_OPTIONS
  49. /* Returns 1 if pubkey allows agent forwarding,
  50. * 0 otherwise */
  51. int svr_pubkey_allows_agentfwd() {
  52. if (ses.authstate.pubkey_options
  53. && ses.authstate.pubkey_options->no_agent_forwarding_flag) {
  54. return 0;
  55. }
  56. return 1;
  57. }
  58. /* Returns 1 if pubkey allows tcp forwarding,
  59. * 0 otherwise */
  60. int svr_pubkey_allows_tcpfwd() {
  61. if (ses.authstate.pubkey_options
  62. && ses.authstate.pubkey_options->no_port_forwarding_flag) {
  63. return 0;
  64. }
  65. return 1;
  66. }
  67. /* Returns 1 if pubkey allows x11 forwarding,
  68. * 0 otherwise */
  69. int svr_pubkey_allows_x11fwd() {
  70. if (ses.authstate.pubkey_options
  71. && ses.authstate.pubkey_options->no_x11_forwarding_flag) {
  72. return 0;
  73. }
  74. return 1;
  75. }
  76. /* Returns 1 if pubkey allows pty, 0 otherwise */
  77. int svr_pubkey_allows_pty() {
  78. if (ses.authstate.pubkey_options
  79. && ses.authstate.pubkey_options->no_pty_flag) {
  80. return 0;
  81. }
  82. return 1;
  83. }
  84. /* Set chansession command to the one forced
  85. * by any 'command' public key option. */
  86. void svr_pubkey_set_forced_command(struct ChanSess *chansess) {
  87. if (ses.authstate.pubkey_options && ses.authstate.pubkey_options->forced_command) {
  88. if (chansess->cmd) {
  89. /* original_command takes ownership */
  90. chansess->original_command = chansess->cmd;
  91. chansess->cmd = NULL;
  92. } else {
  93. chansess->original_command = m_strdup("");
  94. }
  95. chansess->cmd = m_strdup(ses.authstate.pubkey_options->forced_command);
  96. #ifdef LOG_COMMANDS
  97. dropbear_log(LOG_INFO, "Command forced to '%s'", chansess->original_command);
  98. #endif
  99. }
  100. }
  101. /* Free potential public key options */
  102. void svr_pubkey_options_cleanup() {
  103. if (ses.authstate.pubkey_options) {
  104. if (ses.authstate.pubkey_options->forced_command) {
  105. m_free(ses.authstate.pubkey_options->forced_command);
  106. }
  107. m_free(ses.authstate.pubkey_options);
  108. ses.authstate.pubkey_options = NULL;
  109. }
  110. }
  111. /* helper for svr_add_pubkey_options. returns DROPBEAR_SUCCESS if the option is matched,
  112. and increments the options_buf */
  113. static int match_option(buffer *options_buf, const char *opt_name) {
  114. const unsigned int len = strlen(opt_name);
  115. if (options_buf->len - options_buf->pos < len) {
  116. return DROPBEAR_FAILURE;
  117. }
  118. if (strncasecmp((const char *) buf_getptr(options_buf, len), opt_name, len) == 0) {
  119. buf_incrpos(options_buf, len);
  120. return DROPBEAR_SUCCESS;
  121. }
  122. return DROPBEAR_FAILURE;
  123. }
  124. /* Parse pubkey options and set ses.authstate.pubkey_options accordingly.
  125. * Returns DROPBEAR_SUCCESS if key is ok for auth, DROPBEAR_FAILURE otherwise */
  126. int svr_add_pubkey_options(buffer *options_buf, int line_num, const char* filename) {
  127. int ret = DROPBEAR_FAILURE;
  128. TRACE(("enter addpubkeyoptions"))
  129. ses.authstate.pubkey_options = (struct PubKeyOptions*)m_malloc(sizeof( struct PubKeyOptions ));
  130. buf_setpos(options_buf, 0);
  131. while (options_buf->pos < options_buf->len) {
  132. if (match_option(options_buf, "no-port-forwarding") == DROPBEAR_SUCCESS) {
  133. dropbear_log(LOG_WARNING, "Port forwarding disabled.");
  134. ses.authstate.pubkey_options->no_port_forwarding_flag = 1;
  135. goto next_option;
  136. }
  137. #ifdef ENABLE_SVR_AGENTFWD
  138. if (match_option(options_buf, "no-agent-forwarding") == DROPBEAR_SUCCESS) {
  139. dropbear_log(LOG_WARNING, "Agent forwarding disabled.");
  140. ses.authstate.pubkey_options->no_agent_forwarding_flag = 1;
  141. goto next_option;
  142. }
  143. #endif
  144. #ifdef ENABLE_X11FWD
  145. if (match_option(options_buf, "no-X11-forwarding") == DROPBEAR_SUCCESS) {
  146. dropbear_log(LOG_WARNING, "X11 forwarding disabled.");
  147. ses.authstate.pubkey_options->no_x11_forwarding_flag = 1;
  148. goto next_option;
  149. }
  150. #endif
  151. if (match_option(options_buf, "no-pty") == DROPBEAR_SUCCESS) {
  152. dropbear_log(LOG_WARNING, "Pty allocation disabled.");
  153. ses.authstate.pubkey_options->no_pty_flag = 1;
  154. goto next_option;
  155. }
  156. if (match_option(options_buf, "command=\"") == DROPBEAR_SUCCESS) {
  157. int escaped = 0;
  158. const unsigned char* command_start = buf_getptr(options_buf, 0);
  159. while (options_buf->pos < options_buf->len) {
  160. const char c = buf_getbyte(options_buf);
  161. if (!escaped && c == '"') {
  162. const int command_len = buf_getptr(options_buf, 0) - command_start;
  163. ses.authstate.pubkey_options->forced_command = m_malloc(command_len);
  164. memcpy(ses.authstate.pubkey_options->forced_command,
  165. command_start, command_len-1);
  166. ses.authstate.pubkey_options->forced_command[command_len-1] = '\0';
  167. dropbear_log(LOG_WARNING, "Forced command '%s'",
  168. ses.authstate.pubkey_options->forced_command);
  169. goto next_option;
  170. }
  171. escaped = (!escaped && c == '\\');
  172. }
  173. dropbear_log(LOG_WARNING, "Badly formatted command= authorized_keys option");
  174. goto bad_option;
  175. }
  176. next_option:
  177. /*
  178. * Skip the comma, and move to the next option
  179. * (or break out if there are no more).
  180. */
  181. if (options_buf->pos < options_buf->len
  182. && buf_getbyte(options_buf) != ',') {
  183. goto bad_option;
  184. }
  185. /* Process the next option. */
  186. }
  187. /* parsed all options with no problem */
  188. ret = DROPBEAR_SUCCESS;
  189. goto end;
  190. bad_option:
  191. ret = DROPBEAR_FAILURE;
  192. svr_pubkey_options_cleanup();
  193. dropbear_log(LOG_WARNING, "Bad public key options at %s:%d", filename, line_num);
  194. end:
  195. TRACE(("leave addpubkeyoptions"))
  196. return ret;
  197. }
  198. #endif