4 * Copyright (c) 2002,2003 Matt Johnston
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
32 #ifdef ENABLE_CLI_PASSWORD_AUTH
34 #ifdef ENABLE_CLI_ASKPASS_HELPER
35 /* Returns 1 if we want to use the askpass program, 0 otherwise */
36 static int want_askpass()
38 char* askpass_prog
= NULL
;
40 askpass_prog
= getenv("SSH_ASKPASS");
41 return askpass_prog
&&
42 ((!isatty(STDIN_FILENO
) && getenv("DISPLAY") )
43 || getenv("SSH_ASKPASS_ALWAYS"));
46 /* returns a statically allocated password from a helper app, or NULL
48 static char *gui_getpass(const char *prompt
) {
51 int p
[2], maxlen
, len
, status
;
52 static char buf
[DROPBEAR_MAX_CLI_PASS
+ 1];
55 TRACE(("enter gui_getpass"))
57 helper
= getenv("SSH_ASKPASS");
60 TRACE(("leave gui_getpass: no askpass program"))
65 TRACE(("error creating child pipe"))
79 if (dup2(p
[1], STDOUT_FILENO
) < 0) {
80 TRACE(("error redirecting stdout"))
84 execlp(helper
, helper
, prompt
, (char *)0);
85 TRACE(("execlp error"))
92 len
= read(p
[0], buf
+ sizeof(buf
) - maxlen
, maxlen
);
103 while (waitpid(pid
, &status
, 0) < 0 && errno
== EINTR
)
105 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
108 len
= sizeof(buf
) - maxlen
;
110 if (len
> 0 && buf
[len
- 1] == '\n')
113 TRACE(("leave gui_getpass"))
116 #endif /* ENABLE_CLI_ASKPASS_HELPER */
118 void cli_auth_password() {
120 char* password
= NULL
;
123 TRACE(("enter cli_auth_password"))
126 snprintf(prompt
, sizeof(prompt
), "%s@%s's password: ",
127 cli_opts
.username
, cli_opts
.remotehost
);
128 #ifdef ENABLE_CLI_ASKPASS_HELPER
131 password
= gui_getpass(prompt
);
133 dropbear_exit("No password");
138 password
= getpass_or_cancel(prompt
);
141 buf_putbyte(ses
.writepayload
, SSH_MSG_USERAUTH_REQUEST
);
143 buf_putstring(ses
.writepayload
, cli_opts
.username
,
144 strlen(cli_opts
.username
));
146 buf_putstring(ses
.writepayload
, SSH_SERVICE_CONNECTION
,
147 SSH_SERVICE_CONNECTION_LEN
);
149 buf_putstring(ses
.writepayload
, AUTH_METHOD_PASSWORD
,
150 AUTH_METHOD_PASSWORD_LEN
);
152 buf_putbyte(ses
.writepayload
, 0); /* FALSE - so says the spec */
154 buf_putstring(ses
.writepayload
, password
, strlen(password
));
157 m_burn(password
, strlen(password
));
159 TRACE(("leave cli_auth_password"))
161 #endif /* ENABLE_CLI_PASSWORD_AUTH */