blob: 7851578d5093d7b83fb36452ccc1672b7059eded [file] [log] [blame]
The Android Open Source Projectf7fc46c2009-03-03 19:29:23 -08001/*
2 * Dropbear SSH
3 *
4 * Copyright (c) 2005 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
32#ifdef ENABLE_CLI_INTERACT_AUTH
33
34static unsigned char* get_response(unsigned char* prompt)
35{
36 FILE* tty = NULL;
37 unsigned char* response = NULL;
38 /* not a password, but a reasonable limit */
39 char buf[DROPBEAR_MAX_CLI_PASS];
40 char* ret = NULL;
41
42 fprintf(stderr, "%s", prompt);
43
44 tty = fopen(_PATH_TTY, "r");
45 if (tty) {
46 ret = fgets(buf, sizeof(buf), tty);
47 fclose(tty);
48 } else {
49 ret = fgets(buf, sizeof(buf), stdin);
50 }
51
52 if (ret == NULL) {
53 response = (unsigned char*)m_strdup("");
54 } else {
55 unsigned int buflen = strlen(buf);
56 /* fgets includes newlines */
57 if (buflen > 0 && buf[buflen-1] == '\n')
58 buf[buflen-1] = '\0';
59 response = (unsigned char*)m_strdup(buf);
60 }
61
62 m_burn(buf, sizeof(buf));
63
64 return response;
65}
66
67void recv_msg_userauth_info_request() {
68
69 unsigned char *name = NULL;
70 unsigned char *instruction = NULL;
71 unsigned int num_prompts = 0;
72 unsigned int i;
73
74 unsigned char *prompt = NULL;
75 unsigned int echo = 0;
76 unsigned char *response = NULL;
77
78 TRACE(("enter recv_msg_recv_userauth_info_request"))
79
Steve Kondik5b3bfb92009-12-15 17:51:46 -050080 /* 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,
83 cli_opts.remotehost);
84 }
The Android Open Source Projectf7fc46c2009-03-03 19:29:23 -080085 cli_ses.interact_request_received = 1;
86
87 name = buf_getstring(ses.payload, NULL);
88 instruction = buf_getstring(ses.payload, NULL);
89
90 /* language tag */
91 buf_eatstring(ses.payload);
92
93 num_prompts = buf_getint(ses.payload);
94
95 if (num_prompts >= DROPBEAR_MAX_CLI_INTERACT_PROMPTS) {
96 dropbear_exit("Too many prompts received for keyboard-interactive");
97 }
98
99 /* we'll build the response as we go */
100 CHECKCLEARTOWRITE();
101 buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_INFO_RESPONSE);
102 buf_putint(ses.writepayload, num_prompts);
103
104 if (strlen(name) > 0) {
105 cleantext(name);
106 fprintf(stderr, "%s", name);
107 }
108 m_free(name);
109
110 if (strlen(instruction) > 0) {
111 cleantext(instruction);
112 fprintf(stderr, "%s", instruction);
113 }
114 m_free(instruction);
115
116 for (i = 0; i < num_prompts; i++) {
117 unsigned int response_len = 0;
118 prompt = buf_getstring(ses.payload, NULL);
119 cleantext(prompt);
120
121 echo = buf_getbool(ses.payload);
122
123 if (!echo) {
124 unsigned char* p = getpass_or_cancel(prompt);
125 response = m_strdup(p);
126 m_burn(p, strlen(p));
127 } else {
128 response = get_response(prompt);
129 }
130
131 response_len = strlen(response);
132 buf_putstring(ses.writepayload, response, response_len);
133 m_burn(response, response_len);
134 m_free(response);
135 }
136
137 encrypt_packet();
138
139
140 TRACE(("leave recv_msg_recv_userauth_info_request"))
141}
142
143void cli_auth_interactive() {
144
145 TRACE(("enter cli_auth_interactive"))
146 CHECKCLEARTOWRITE();
147
148 buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_REQUEST);
149
150 /* username */
151 buf_putstring(ses.writepayload, cli_opts.username,
152 strlen(cli_opts.username));
153
154 /* service name */
155 buf_putstring(ses.writepayload, SSH_SERVICE_CONNECTION,
156 SSH_SERVICE_CONNECTION_LEN);
157
158 /* method */
159 buf_putstring(ses.writepayload, AUTH_METHOD_INTERACT,
160 AUTH_METHOD_INTERACT_LEN);
161
162 /* empty language tag */
163 buf_putstring(ses.writepayload, "", 0);
164
165 /* empty submethods */
166 buf_putstring(ses.writepayload, "", 0);
167
168 encrypt_packet();
169 cli_ses.interact_request_received = 0;
170
171 TRACE(("leave cli_auth_interactive"))
172
173}
174#endif /* ENABLE_CLI_INTERACT_AUTH */