comparison cli-authpasswd.c @ 285:1b9e69c058d2

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3) to branch 'au.asn.ucc.matt.dropbear' (head fdf4a7a3b97ae5046139915de7e40399cceb2c01)
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 13:23:58 +0000
parents 475a818dd6e7
children 8f3ec7c104d9
comparison
equal deleted inserted replaced
281:997e6f7dc01e 285:1b9e69c058d2
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
32 #ifdef ENABLE_CLI_PASSWORD_AUTH
33
34 #ifdef ENABLE_CLI_ASKPASS_HELPER
35 /* Returns 1 if we want to use the askpass program, 0 otherwise */
36 static int want_askpass()
37 {
38 char* askpass_prog = NULL;
39
40 askpass_prog = getenv("SSH_ASKPASS");
41 return askpass_prog && !isatty(STDIN_FILENO) && getenv("DISPLAY");
42 }
43
44 /* returns a statically allocated password from a helper app, or NULL
45 * on failure */
46 static char *gui_getpass(const char *prompt) {
47
48 pid_t pid;
49 int p[2], maxlen, len, status;
50 static char buf[DROPBEAR_MAX_CLI_PASS + 1];
51 char* helper = NULL;
52
53 TRACE(("enter gui_getpass"))
54
55 helper = getenv("SSH_ASKPASS");
56 if (!helper)
57 {
58 TRACE(("leave gui_getpass: no askpass program"))
59 return NULL;
60 }
61
62 if (pipe(p) < 0) {
63 TRACE(("error creating child pipe"))
64 return NULL;
65 }
66
67 pid = fork();
68
69 if (pid < 0) {
70 TRACE(("fork error"))
71 return NULL;
72 }
73
74 if (!pid) {
75 /* child */
76 close(p[0]);
77 if (dup2(p[1], STDOUT_FILENO) < 0) {
78 TRACE(("error redirecting stdout"))
79 exit(1);
80 }
81 close(p[1]);
82 execlp(helper, helper, prompt, (char *)0);
83 TRACE(("execlp error"))
84 exit(1);
85 }
86
87 close(p[1]);
88 maxlen = sizeof(buf);
89 while (maxlen > 0) {
90 len = read(p[0], buf + sizeof(buf) - maxlen, maxlen);
91 if (len > 0) {
92 maxlen -= len;
93 } else {
94 if (errno != EINTR)
95 break;
96 }
97 }
98
99 close(p[0]);
100
101 while (waitpid(pid, &status, 0) < 0 && errno == EINTR)
102 ;
103 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
104 return(NULL);
105
106 len = sizeof(buf) - maxlen;
107 buf[len] = '\0';
108 if (len > 0 && buf[len - 1] == '\n')
109 buf[len - 1] = '\0';
110
111 TRACE(("leave gui_getpass"))
112 return(buf);
113 }
114 #endif /* ENABLE_CLI_ASKPASS_HELPER */
115
116 void cli_auth_password() {
117
118 char* password = NULL;
119
120 TRACE(("enter cli_auth_password"))
121 CHECKCLEARTOWRITE();
122
123 #ifdef ENABLE_CLI_ASKPASS_HELPER
124 if (want_askpass())
125 password = gui_getpass("Password: ");
126 else
127 #endif
128 password = getpass_or_cancel("Password: ");
129
130 buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_REQUEST);
131
132 buf_putstring(ses.writepayload, cli_opts.username,
133 strlen(cli_opts.username));
134
135 buf_putstring(ses.writepayload, SSH_SERVICE_CONNECTION,
136 SSH_SERVICE_CONNECTION_LEN);
137
138 buf_putstring(ses.writepayload, AUTH_METHOD_PASSWORD,
139 AUTH_METHOD_PASSWORD_LEN);
140
141 buf_putbyte(ses.writepayload, 0); /* FALSE - so says the spec */
142
143 buf_putstring(ses.writepayload, password, strlen(password));
144
145 encrypt_packet();
146 m_burn(password, strlen(password));
147
148 TRACE(("leave cli_auth_password"))
149 }
150 #endif /* ENABLE_CLI_PASSWORD_AUTH */