4 * Copyright (c) 2005 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_INTERACT_AUTH
34 static unsigned char* get_response(unsigned char* prompt
)
37 unsigned char* response
= NULL
;
38 /* not a password, but a reasonable limit */
39 char buf
[DROPBEAR_MAX_CLI_PASS
];
42 fprintf(stderr
, "%s", prompt
);
44 tty
= fopen(_PATH_TTY
, "r");
46 ret
= fgets(buf
, sizeof(buf
), tty
);
49 ret
= fgets(buf
, sizeof(buf
), stdin
);
53 response
= (unsigned char*)m_strdup("");
55 unsigned int buflen
= strlen(buf
);
56 /* fgets includes newlines */
57 if (buflen
> 0 && buf
[buflen
-1] == '\n')
59 response
= (unsigned char*)m_strdup(buf
);
62 m_burn(buf
, sizeof(buf
));
67 void recv_msg_userauth_info_request() {
69 unsigned char *name
= NULL
;
70 unsigned char *instruction
= NULL
;
71 unsigned int num_prompts
= 0;
74 unsigned char *prompt
= NULL
;
75 unsigned int echo
= 0;
76 unsigned char *response
= NULL
;
78 TRACE(("enter recv_msg_recv_userauth_info_request"))
80 /* Let the user know what password/host they are authing for */
81 if (!cli_ses
.interact_request_received
) {
82 fprintf(stderr
, "Login for %s@%s\n", cli_opts
.username
,
85 cli_ses
.interact_request_received
= 1;
87 name
= buf_getstring(ses
.payload
, NULL
);
88 instruction
= buf_getstring(ses
.payload
, NULL
);
91 buf_eatstring(ses
.payload
);
93 num_prompts
= buf_getint(ses
.payload
);
95 if (num_prompts
>= DROPBEAR_MAX_CLI_INTERACT_PROMPTS
) {
96 dropbear_exit("Too many prompts received for keyboard-interactive");
99 /* we'll build the response as we go */
101 buf_putbyte(ses
.writepayload
, SSH_MSG_USERAUTH_INFO_RESPONSE
);
102 buf_putint(ses
.writepayload
, num_prompts
);
104 if (strlen(name
) > 0) {
106 fprintf(stderr
, "%s", name
);
110 if (strlen(instruction
) > 0) {
111 cleantext(instruction
);
112 fprintf(stderr
, "%s", instruction
);
116 for (i
= 0; i
< num_prompts
; i
++) {
117 unsigned int response_len
= 0;
118 prompt
= buf_getstring(ses
.payload
, NULL
);
121 echo
= buf_getbool(ses
.payload
);
124 unsigned char* p
= getpass_or_cancel(prompt
);
125 response
= m_strdup(p
);
126 m_burn(p
, strlen(p
));
128 response
= get_response(prompt
);
131 response_len
= strlen(response
);
132 buf_putstring(ses
.writepayload
, response
, response_len
);
133 m_burn(response
, response_len
);
141 TRACE(("leave recv_msg_recv_userauth_info_request"))
144 void cli_auth_interactive() {
146 TRACE(("enter cli_auth_interactive"))
149 buf_putbyte(ses
.writepayload
, SSH_MSG_USERAUTH_REQUEST
);
152 buf_putstring(ses
.writepayload
, cli_opts
.username
,
153 strlen(cli_opts
.username
));
156 buf_putstring(ses
.writepayload
, SSH_SERVICE_CONNECTION
,
157 SSH_SERVICE_CONNECTION_LEN
);
160 buf_putstring(ses
.writepayload
, AUTH_METHOD_INTERACT
,
161 AUTH_METHOD_INTERACT_LEN
);
163 /* empty language tag */
164 buf_putstring(ses
.writepayload
, "", 0);
166 /* empty submethods */
167 buf_putstring(ses
.writepayload
, "", 0);
170 cli_ses
.interact_request_received
= 0;
172 TRACE(("leave cli_auth_interactive"))
175 #endif /* ENABLE_CLI_INTERACT_AUTH */