Mercurial > dropbear
annotate cli-main.c @ 902:2351b2da8e0d DROPBEAR_2014.63
2014.63
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 19 Feb 2014 22:04:35 +0800 |
parents | aa689d140928 |
children | c88dce72f6d2 |
rev | line source |
---|---|
74
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
1 /* |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
2 * Dropbear - a SSH2 server |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
3 * SSH client implementation |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
4 * |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
5 * Copyright (c) 2002,2003 Matt Johnston |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
6 * Copyright (c) 2004 by Mihnea Stoenescu |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
7 * All rights reserved. |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
8 * |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
9 * Permission is hereby granted, free of charge, to any person obtaining a copy |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
10 * of this software and associated documentation files (the "Software"), to deal |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
11 * in the Software without restriction, including without limitation the rights |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
13 * copies of the Software, and to permit persons to whom the Software is |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
14 * furnished to do so, subject to the following conditions: |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
15 * |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
16 * The above copyright notice and this permission notice shall be included in |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
17 * all copies or substantial portions of the Software. |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
18 * |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
25 * SOFTWARE. */ |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
26 |
33 | 27 #include "includes.h" |
28 #include "dbutil.h" | |
29 #include "runopts.h" | |
30 #include "session.h" | |
858
220f55d540ae
rename random.h to dbrandom.h since some OSes have a system random.h
Matt Johnston <matt@ucc.asn.au>
parents:
857
diff
changeset
|
31 #include "dbrandom.h" |
795 | 32 #include "crypto_desc.h" |
26 | 33 |
614
00eca37e47e8
Add noreturn and format attribute hints for some functions.
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
34 static void cli_dropbear_exit(int exitcode, const char* format, va_list param) ATTRIB_NORETURN; |
33 | 35 static void cli_dropbear_log(int priority, const char* format, va_list param); |
36 | |
542
c9128994a2d6
Wrap proxycmd function in ENABLE_CLI_PROXYCMD #ifdef
Matt Johnston <matt@ucc.asn.au>
parents:
484
diff
changeset
|
37 #ifdef ENABLE_CLI_PROXYCMD |
483
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
38 static void cli_proxy_cmd(int *sock_in, int *sock_out); |
542
c9128994a2d6
Wrap proxycmd function in ENABLE_CLI_PROXYCMD #ifdef
Matt Johnston <matt@ucc.asn.au>
parents:
484
diff
changeset
|
39 #endif |
483
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
40 |
33 | 41 #if defined(DBMULTI_dbclient) || !defined(DROPBEAR_MULTI) |
42 #if defined(DBMULTI_dbclient) && defined(DROPBEAR_MULTI) | |
43 int cli_main(int argc, char ** argv) { | |
44 #else | |
26 | 45 int main(int argc, char ** argv) { |
33 | 46 #endif |
26 | 47 |
479
e3db1f7a2e43
- Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents:
425
diff
changeset
|
48 int sock_in, sock_out; |
26 | 49 char* error = NULL; |
50 | |
51 _dropbear_exit = cli_dropbear_exit; | |
52 _dropbear_log = cli_dropbear_log; | |
53 | |
425 | 54 disallow_core(); |
55 | |
795 | 56 seedrandom(); |
57 crypto_init(); | |
58 | |
26 | 59 cli_getopts(argc, argv); |
60 | |
33 | 61 TRACE(("user='%s' host='%s' port='%s'", cli_opts.username, |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
109
diff
changeset
|
62 cli_opts.remotehost, cli_opts.remoteport)) |
33 | 63 |
109
2e9d1f29c50f
merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
64 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { |
2e9d1f29c50f
merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
65 dropbear_exit("signal() error"); |
2e9d1f29c50f
merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
66 } |
2e9d1f29c50f
merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
67 |
483
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
68 #ifdef ENABLE_CLI_PROXYCMD |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
69 if (cli_opts.proxycmd) { |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
70 cli_proxy_cmd(&sock_in, &sock_out); |
544
9e51707cd6f2
- Make -i and -W pass through multihop arguments
Matt Johnston <matt@ucc.asn.au>
parents:
542
diff
changeset
|
71 m_free(cli_opts.proxycmd); |
479
e3db1f7a2e43
- Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents:
425
diff
changeset
|
72 } else |
e3db1f7a2e43
- Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents:
425
diff
changeset
|
73 #endif |
e3db1f7a2e43
- Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents:
425
diff
changeset
|
74 { |
e3db1f7a2e43
- Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents:
425
diff
changeset
|
75 int sock = connect_remote(cli_opts.remotehost, cli_opts.remoteport, |
e3db1f7a2e43
- Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents:
425
diff
changeset
|
76 0, &error); |
e3db1f7a2e43
- Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents:
425
diff
changeset
|
77 sock_in = sock_out = sock; |
871
aa689d140928
- Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents:
858
diff
changeset
|
78 if (cli_opts.wantpty) { |
aa689d140928
- Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents:
858
diff
changeset
|
79 set_sock_priority(sock, DROPBEAR_PRIO_LOWDELAY); |
aa689d140928
- Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents:
858
diff
changeset
|
80 } |
479
e3db1f7a2e43
- Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents:
425
diff
changeset
|
81 } |
e3db1f7a2e43
- Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents:
425
diff
changeset
|
82 |
e3db1f7a2e43
- Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents:
425
diff
changeset
|
83 if (sock_in < 0) { |
26 | 84 dropbear_exit("%s", error); |
85 } | |
86 | |
568
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
544
diff
changeset
|
87 cli_session(sock_in, sock_out); |
26 | 88 |
89 /* not reached */ | |
90 return -1; | |
91 } | |
33 | 92 #endif /* DBMULTI stuff */ |
93 | |
94 static void cli_dropbear_exit(int exitcode, const char* format, va_list param) { | |
95 | |
96 char fmtbuf[300]; | |
97 | |
98 if (!sessinitdone) { | |
594
a98a2138364a
Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents:
568
diff
changeset
|
99 snprintf(fmtbuf, sizeof(fmtbuf), "Exited: %s", |
33 | 100 format); |
101 } else { | |
102 snprintf(fmtbuf, sizeof(fmtbuf), | |
594
a98a2138364a
Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents:
568
diff
changeset
|
103 "Connection to %s@%s:%s exited: %s", |
33 | 104 cli_opts.username, cli_opts.remotehost, |
105 cli_opts.remoteport, format); | |
106 } | |
107 | |
40
b4874d772210
- Added terminal mode handling etc for the client, and window change
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
108 /* Do the cleanup first, since then the terminal will be reset */ |
733
70811267715c
Run the cleanup handler also when we close due to TCP connection being closed
Matt Johnston <matt@ucc.asn.au>
parents:
614
diff
changeset
|
109 session_cleanup(); |
40
b4874d772210
- Added terminal mode handling etc for the client, and window change
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
110 |
33 | 111 _dropbear_log(LOG_INFO, fmtbuf, param); |
112 | |
113 exit(exitcode); | |
114 } | |
115 | |
108
10f4d3319780
- added circular buffering for channels
Matt Johnston <matt@ucc.asn.au>
parents:
74
diff
changeset
|
116 static void cli_dropbear_log(int UNUSED(priority), |
10f4d3319780
- added circular buffering for channels
Matt Johnston <matt@ucc.asn.au>
parents:
74
diff
changeset
|
117 const char* format, va_list param) { |
33 | 118 |
119 char printbuf[1024]; | |
120 | |
121 vsnprintf(printbuf, sizeof(printbuf), format, param); | |
122 | |
40
b4874d772210
- Added terminal mode handling etc for the client, and window change
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
123 fprintf(stderr, "%s: %s\n", cli_opts.progname, printbuf); |
33 | 124 |
125 } | |
483
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
126 |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
127 static void exec_proxy_cmd(void *user_data_cmd) { |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
128 const char *cmd = user_data_cmd; |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
129 char *usershell; |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
130 |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
131 usershell = m_strdup(get_user_shell()); |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
132 run_shell_command(cmd, ses.maxfd, usershell); |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
133 dropbear_exit("Failed to run '%s'\n", cmd); |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
134 } |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
135 |
542
c9128994a2d6
Wrap proxycmd function in ENABLE_CLI_PROXYCMD #ifdef
Matt Johnston <matt@ucc.asn.au>
parents:
484
diff
changeset
|
136 #ifdef ENABLE_CLI_PROXYCMD |
483
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
137 static void cli_proxy_cmd(int *sock_in, int *sock_out) { |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
138 int ret; |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
139 |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
140 fill_passwd(cli_opts.own_user); |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
141 |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
142 ret = spawn_command(exec_proxy_cmd, cli_opts.proxycmd, |
484
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
483
diff
changeset
|
143 sock_out, sock_in, NULL, NULL); |
483
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
144 if (ret == DROPBEAR_FAILURE) { |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
145 dropbear_exit("Failed running proxy command"); |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
146 *sock_in = *sock_out = -1; |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
147 } |
738313e73b1c
- "-J 'nc localhost 22'" kind of works, needs fixing hostkeys, ptys etc.
Matt Johnston <matt@ucc.asn.au>
parents:
479
diff
changeset
|
148 } |
857 | 149 #endif /* ENABLE_CLI_PROXYCMD */ |