Allow IPv6 address entry in tools>ping - Loosens valid character check
[tomato/davidwu.git] / release / src / router / dropbear / cli-authinteract.c
bloba06c9ca8e1a0a65df053c0bf3e7d9d5144612905
1 /*
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:
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
23 * SOFTWARE. */
25 #include "includes.h"
26 #include "buffer.h"
27 #include "dbutil.h"
28 #include "session.h"
29 #include "ssh.h"
30 #include "runopts.h"
32 #ifdef ENABLE_CLI_INTERACT_AUTH
34 static unsigned char* get_response(unsigned char* prompt)
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;
42 fprintf(stderr, "%s", prompt);
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);
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);
62 m_burn(buf, sizeof(buf));
64 return response;
67 void recv_msg_userauth_info_request() {
69 unsigned char *name = NULL;
70 unsigned char *instruction = NULL;
71 unsigned int num_prompts = 0;
72 unsigned int i;
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,
83 cli_opts.remotehost);
85 cli_ses.interact_request_received = 1;
87 name = buf_getstring(ses.payload, NULL);
88 instruction = buf_getstring(ses.payload, NULL);
90 /* language tag */
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 */
100 CHECKCLEARTOWRITE();
101 buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_INFO_RESPONSE);
102 buf_putint(ses.writepayload, num_prompts);
104 if (strlen(name) > 0) {
105 cleantext(name);
106 fprintf(stderr, "%s", name);
108 m_free(name);
110 if (strlen(instruction) > 0) {
111 cleantext(instruction);
112 fprintf(stderr, "%s", instruction);
114 m_free(instruction);
116 for (i = 0; i < num_prompts; i++) {
117 unsigned int response_len = 0;
118 prompt = buf_getstring(ses.payload, NULL);
119 cleantext(prompt);
121 echo = buf_getbool(ses.payload);
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);
131 response_len = strlen(response);
132 buf_putstring(ses.writepayload, response, response_len);
133 m_burn(response, response_len);
134 m_free(prompt);
135 m_free(response);
138 encrypt_packet();
141 TRACE(("leave recv_msg_recv_userauth_info_request"))
144 void cli_auth_interactive() {
146 TRACE(("enter cli_auth_interactive"))
147 CHECKCLEARTOWRITE();
149 buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_REQUEST);
151 /* username */
152 buf_putstring(ses.writepayload, cli_opts.username,
153 strlen(cli_opts.username));
155 /* service name */
156 buf_putstring(ses.writepayload, SSH_SERVICE_CONNECTION,
157 SSH_SERVICE_CONNECTION_LEN);
159 /* method */
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);
169 encrypt_packet();
170 cli_ses.interact_request_received = 0;
172 TRACE(("leave cli_auth_interactive"))
175 #endif /* ENABLE_CLI_INTERACT_AUTH */