Coverage Report

Created: 2026-08-13 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dropbear/src/cli-authpasswd.c
Line
Count
Source
1
/*
2
 * Dropbear SSH
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
25
#include "includes.h"
26
#include "buffer.h"
27
#include "dbutil.h"
28
#include "session.h"
29
#include "ssh.h"
30
#include "runopts.h"
31
#include "atomicio.h"
32
33
#if DROPBEAR_CLI_PASSWORD_AUTH
34
35
#if DROPBEAR_CLI_ASKPASS_HELPER
36
/* Returns 1 if we want to use the askpass program, 0 otherwise */
37
static int want_askpass()
38
{
39
  char* askpass_prog = NULL;
40
41
  askpass_prog = getenv("SSH_ASKPASS");
42
  return askpass_prog && 
43
    ((!isatty(STDIN_FILENO) && getenv("DISPLAY") )
44
      || getenv("SSH_ASKPASS_ALWAYS"));
45
}
46
47
/* returns a statically allocated password from a helper app, or NULL
48
 * on failure */
49
static char *gui_getpass(const char *prompt) {
50
51
  pid_t pid;
52
  int p[2], status;
53
  static char buf[DROPBEAR_MAX_CLI_PASS + 1];
54
  size_t len;
55
  char* helper = NULL;
56
57
  TRACE(("enter gui_getpass"))
58
59
  helper = getenv("SSH_ASKPASS");
60
  if (!helper)
61
  {
62
    TRACE(("leave gui_getpass: no askpass program"))
63
    return NULL;
64
  }
65
66
  if (pipe(p) < 0) {
67
    TRACE(("error creating child pipe"))
68
    return NULL;
69
  }
70
71
  pid = fork();
72
73
  if (pid < 0) {
74
    TRACE(("fork error"))
75
    return NULL;
76
  }
77
78
  if (!pid) {
79
    /* child */
80
    close(p[0]);
81
    if (dup2(p[1], STDOUT_FILENO) < 0) {
82
      TRACE(("error redirecting stdout"))
83
      exit(1);
84
    }
85
    close(p[1]);
86
    execlp(helper, helper, prompt, (char *)0);
87
    TRACE(("execlp error"))
88
    exit(1);
89
  }
90
91
  close(p[1]);
92
  len = atomicio(read, p[0], buf, sizeof(buf)-1);
93
  close(p[0]);
94
95
  while (waitpid(pid, &status, 0) < 0 && errno == EINTR)
96
    ;
97
  if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
98
    return(NULL);
99
100
  buf[len] = '\0';
101
  if (len > 0 && buf[len - 1] == '\n')
102
    buf[len - 1] = '\0';
103
104
  TRACE(("leave gui_getpass"))
105
  return(buf);
106
}
107
#endif /* DROPBEAR_CLI_ASKPASS_HELPER */
108
109
0
void cli_auth_password() {
110
111
0
  char* password = NULL;
112
0
  char prompt[80];
113
114
0
  DEBUG1(("enter cli_auth_password"))
115
0
  CHECKCLEARTOWRITE();
116
117
0
  snprintf(prompt, sizeof(prompt), "%s@%s's password: ", 
118
0
        cli_opts.username, cli_opts.remotehost);
119
#if DROPBEAR_CLI_ASKPASS_HELPER
120
  if (want_askpass())
121
  {
122
    password = gui_getpass(prompt);
123
    if (!password) {
124
      dropbear_exit("No password");
125
    }
126
  } else
127
#endif
128
0
  {
129
0
    password = getpass_or_cancel(prompt);
130
0
  }
131
132
0
  buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_REQUEST);
133
134
0
  buf_putstring(ses.writepayload, cli_opts.username,
135
0
      strlen(cli_opts.username));
136
137
0
  buf_putstring(ses.writepayload, SSH_SERVICE_CONNECTION,
138
0
      SSH_SERVICE_CONNECTION_LEN);
139
140
0
  buf_putstring(ses.writepayload, AUTH_METHOD_PASSWORD,
141
0
      AUTH_METHOD_PASSWORD_LEN);
142
143
0
  buf_putbyte(ses.writepayload, 0); /* FALSE - so says the spec */
144
145
0
  buf_putstring(ses.writepayload, password, strlen(password));
146
147
0
  encrypt_packet();
148
0
  m_burn(password, strlen(password));
149
0
  cli_ses.is_trivial_auth = 0;
150
0
  TRACE(("leave cli_auth_password"))
151
0
}
152
#endif  /* DROPBEAR_CLI_PASSWORD_AUTH */