123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- #include "includes.h"
- #include "session.h"
- #include "dbutil.h"
- #include "signkey.h"
- #include "auth.h"
- #if DROPBEAR_SVR_PUBKEY_OPTIONS_BUILT
- int svr_pubkey_allows_agentfwd() {
- if (ses.authstate.pubkey_options
- && ses.authstate.pubkey_options->no_agent_forwarding_flag) {
- return 0;
- }
- return 1;
- }
- int svr_pubkey_allows_tcpfwd() {
- if (ses.authstate.pubkey_options
- && ses.authstate.pubkey_options->no_port_forwarding_flag) {
- return 0;
- }
- return 1;
- }
- int svr_pubkey_allows_x11fwd() {
- if (ses.authstate.pubkey_options
- && ses.authstate.pubkey_options->no_x11_forwarding_flag) {
- return 0;
- }
- return 1;
- }
- int svr_pubkey_allows_pty() {
- if (ses.authstate.pubkey_options
- && ses.authstate.pubkey_options->no_pty_flag) {
- return 0;
- }
- return 1;
- }
- void svr_pubkey_set_forced_command(struct ChanSess *chansess) {
- if (ses.authstate.pubkey_options && ses.authstate.pubkey_options->forced_command) {
- TRACE(("Forced command '%s'", ses.authstate.pubkey_options->forced_command))
- if (chansess->cmd) {
-
- chansess->original_command = chansess->cmd;
- chansess->cmd = NULL;
- } else {
- chansess->original_command = m_strdup("");
- }
- chansess->cmd = m_strdup(ses.authstate.pubkey_options->forced_command);
- #if LOG_COMMANDS
- dropbear_log(LOG_INFO, "Command forced to '%s'", chansess->original_command);
- #endif
- }
- }
- void svr_pubkey_options_cleanup() {
- if (ses.authstate.pubkey_options) {
- if (ses.authstate.pubkey_options->forced_command) {
- m_free(ses.authstate.pubkey_options->forced_command);
- }
- m_free(ses.authstate.pubkey_options);
- }
- if (ses.authstate.pubkey_info) {
- m_free(ses.authstate.pubkey_info);
- }
- }
- static int match_option(buffer *options_buf, const char *opt_name) {
- const unsigned int len = strlen(opt_name);
- if (options_buf->len - options_buf->pos < len) {
- return DROPBEAR_FAILURE;
- }
- if (strncasecmp((const char *) buf_getptr(options_buf, len), opt_name, len) == 0) {
- buf_incrpos(options_buf, len);
- return DROPBEAR_SUCCESS;
- }
- return DROPBEAR_FAILURE;
- }
- int svr_add_pubkey_options(buffer *options_buf, int line_num, const char* filename) {
- int ret = DROPBEAR_FAILURE;
- TRACE(("enter addpubkeyoptions"))
- ses.authstate.pubkey_options = (struct PubKeyOptions*)m_malloc(sizeof( struct PubKeyOptions ));
- buf_setpos(options_buf, 0);
- while (options_buf->pos < options_buf->len) {
- if (match_option(options_buf, "no-port-forwarding") == DROPBEAR_SUCCESS) {
- dropbear_log(LOG_WARNING, "Port forwarding disabled.");
- ses.authstate.pubkey_options->no_port_forwarding_flag = 1;
- goto next_option;
- }
- if (match_option(options_buf, "no-agent-forwarding") == DROPBEAR_SUCCESS) {
- #if DROPBEAR_SVR_AGENTFWD
- dropbear_log(LOG_WARNING, "Agent forwarding disabled.");
- ses.authstate.pubkey_options->no_agent_forwarding_flag = 1;
- #endif
- goto next_option;
- }
- if (match_option(options_buf, "no-X11-forwarding") == DROPBEAR_SUCCESS) {
- #if DROPBEAR_X11FWD
- dropbear_log(LOG_WARNING, "X11 forwarding disabled.");
- ses.authstate.pubkey_options->no_x11_forwarding_flag = 1;
- #endif
- goto next_option;
- }
- if (match_option(options_buf, "no-pty") == DROPBEAR_SUCCESS) {
- dropbear_log(LOG_WARNING, "Pty allocation disabled.");
- ses.authstate.pubkey_options->no_pty_flag = 1;
- goto next_option;
- }
- if (match_option(options_buf, "restrict") == DROPBEAR_SUCCESS) {
- dropbear_log(LOG_WARNING, "Restrict option set");
- ses.authstate.pubkey_options->no_port_forwarding_flag = 1;
- #if DROPBEAR_SVR_AGENTFWD
- ses.authstate.pubkey_options->no_agent_forwarding_flag = 1;
- #endif
- #if DROPBEAR_X11FWD
- ses.authstate.pubkey_options->no_x11_forwarding_flag = 1;
- #endif
- ses.authstate.pubkey_options->no_pty_flag = 1;
- goto next_option;
- }
- if (match_option(options_buf, "command=\"") == DROPBEAR_SUCCESS) {
- int escaped = 0;
- const unsigned char* command_start = buf_getptr(options_buf, 0);
- if (ses.authstate.pubkey_options->forced_command) {
-
- goto bad_option;
- }
- while (options_buf->pos < options_buf->len) {
- const char c = buf_getbyte(options_buf);
- if (!escaped && c == '"') {
- const int command_len = buf_getptr(options_buf, 0) - command_start;
- ses.authstate.pubkey_options->forced_command = m_malloc(command_len);
- memcpy(ses.authstate.pubkey_options->forced_command,
- command_start, command_len-1);
- ses.authstate.pubkey_options->forced_command[command_len-1] = '\0';
- goto next_option;
- }
- escaped = (!escaped && c == '\\');
- }
- dropbear_log(LOG_WARNING, "Badly formatted command= authorized_keys option");
- goto bad_option;
- }
- next_option:
-
- if (options_buf->pos < options_buf->len
- && buf_getbyte(options_buf) != ',') {
- goto bad_option;
- }
-
- }
-
- ret = DROPBEAR_SUCCESS;
- goto end;
- bad_option:
- ret = DROPBEAR_FAILURE;
- svr_pubkey_options_cleanup();
- dropbear_log(LOG_WARNING, "Bad public key options at %s:%d", filename, line_num);
- end:
- TRACE(("leave addpubkeyoptions"))
- return ret;
- }
- #endif
|