svr-authpubkeyoptions.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #if DROPBEAR_SVR_PUBKEY_OPTIONS_BUILT
  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. TRACE(("Forced command '%s'", ses.authstate.pubkey_options->forced_command))
  89. if (chansess->cmd) {
  90. /* original_command takes ownership */
  91. chansess->original_command = chansess->cmd;
  92. chansess->cmd = NULL;
  93. } else {
  94. chansess->original_command = m_strdup("");
  95. }
  96. chansess->cmd = m_strdup(ses.authstate.pubkey_options->forced_command);
  97. #if LOG_COMMANDS
  98. dropbear_log(LOG_INFO, "Command forced to '%s'", chansess->original_command);
  99. #endif
  100. }
  101. }
  102. /* Free potential public key options */
  103. void svr_pubkey_options_cleanup() {
  104. if (ses.authstate.pubkey_options) {
  105. if (ses.authstate.pubkey_options->forced_command) {
  106. m_free(ses.authstate.pubkey_options->forced_command);
  107. }
  108. m_free(ses.authstate.pubkey_options);
  109. }
  110. if (ses.authstate.pubkey_info) {
  111. m_free(ses.authstate.pubkey_info);
  112. }
  113. }
  114. /* helper for svr_add_pubkey_options. returns DROPBEAR_SUCCESS if the option is matched,
  115. and increments the options_buf */
  116. static int match_option(buffer *options_buf, const char *opt_name) {
  117. const unsigned int len = strlen(opt_name);
  118. if (options_buf->len - options_buf->pos < len) {
  119. return DROPBEAR_FAILURE;
  120. }
  121. if (strncasecmp((const char *) buf_getptr(options_buf, len), opt_name, len) == 0) {
  122. buf_incrpos(options_buf, len);
  123. return DROPBEAR_SUCCESS;
  124. }
  125. return DROPBEAR_FAILURE;
  126. }
  127. /* Parse pubkey options and set ses.authstate.pubkey_options accordingly.
  128. * Returns DROPBEAR_SUCCESS if key is ok for auth, DROPBEAR_FAILURE otherwise */
  129. int svr_add_pubkey_options(buffer *options_buf, int line_num, const char* filename) {
  130. int ret = DROPBEAR_FAILURE;
  131. TRACE(("enter addpubkeyoptions"))
  132. ses.authstate.pubkey_options = (struct PubKeyOptions*)m_malloc(sizeof( struct PubKeyOptions ));
  133. buf_setpos(options_buf, 0);
  134. while (options_buf->pos < options_buf->len) {
  135. if (match_option(options_buf, "no-port-forwarding") == DROPBEAR_SUCCESS) {
  136. dropbear_log(LOG_WARNING, "Port forwarding disabled.");
  137. ses.authstate.pubkey_options->no_port_forwarding_flag = 1;
  138. goto next_option;
  139. }
  140. if (match_option(options_buf, "no-agent-forwarding") == DROPBEAR_SUCCESS) {
  141. #if DROPBEAR_SVR_AGENTFWD
  142. dropbear_log(LOG_WARNING, "Agent forwarding disabled.");
  143. ses.authstate.pubkey_options->no_agent_forwarding_flag = 1;
  144. #endif
  145. goto next_option;
  146. }
  147. if (match_option(options_buf, "no-X11-forwarding") == DROPBEAR_SUCCESS) {
  148. #if DROPBEAR_X11FWD
  149. dropbear_log(LOG_WARNING, "X11 forwarding disabled.");
  150. ses.authstate.pubkey_options->no_x11_forwarding_flag = 1;
  151. #endif
  152. goto next_option;
  153. }
  154. if (match_option(options_buf, "no-pty") == DROPBEAR_SUCCESS) {
  155. dropbear_log(LOG_WARNING, "Pty allocation disabled.");
  156. ses.authstate.pubkey_options->no_pty_flag = 1;
  157. goto next_option;
  158. }
  159. if (match_option(options_buf, "restrict") == DROPBEAR_SUCCESS) {
  160. dropbear_log(LOG_WARNING, "Restrict option set");
  161. ses.authstate.pubkey_options->no_port_forwarding_flag = 1;
  162. #if DROPBEAR_SVR_AGENTFWD
  163. ses.authstate.pubkey_options->no_agent_forwarding_flag = 1;
  164. #endif
  165. #if DROPBEAR_X11FWD
  166. ses.authstate.pubkey_options->no_x11_forwarding_flag = 1;
  167. #endif
  168. ses.authstate.pubkey_options->no_pty_flag = 1;
  169. goto next_option;
  170. }
  171. if (match_option(options_buf, "command=\"") == DROPBEAR_SUCCESS) {
  172. int escaped = 0;
  173. const unsigned char* command_start = buf_getptr(options_buf, 0);
  174. if (ses.authstate.pubkey_options->forced_command) {
  175. /* multiple command= options */
  176. goto bad_option;
  177. }
  178. while (options_buf->pos < options_buf->len) {
  179. const char c = buf_getbyte(options_buf);
  180. if (!escaped && c == '"') {
  181. const int command_len = buf_getptr(options_buf, 0) - command_start;
  182. ses.authstate.pubkey_options->forced_command = m_malloc(command_len);
  183. memcpy(ses.authstate.pubkey_options->forced_command,
  184. command_start, command_len-1);
  185. ses.authstate.pubkey_options->forced_command[command_len-1] = '\0';
  186. goto next_option;
  187. }
  188. escaped = (!escaped && c == '\\');
  189. }
  190. dropbear_log(LOG_WARNING, "Badly formatted command= authorized_keys option");
  191. goto bad_option;
  192. }
  193. next_option:
  194. /*
  195. * Skip the comma, and move to the next option
  196. * (or break out if there are no more).
  197. */
  198. if (options_buf->pos < options_buf->len
  199. && buf_getbyte(options_buf) != ',') {
  200. goto bad_option;
  201. }
  202. /* Process the next option. */
  203. }
  204. /* parsed all options with no problem */
  205. ret = DROPBEAR_SUCCESS;
  206. goto end;
  207. bad_option:
  208. ret = DROPBEAR_FAILURE;
  209. svr_pubkey_options_cleanup();
  210. dropbear_log(LOG_WARNING, "Bad public key options at %s:%d", filename, line_num);
  211. end:
  212. TRACE(("leave addpubkeyoptions"))
  213. return ret;
  214. }
  215. #endif