annotate common-session.c @ 482:7ad49f34a122

- Add run_shell_command() function to run a "sh -c" command, handling lots of the work that exechild did (and can be shared by client -J option)
author Matt Johnston <matt@ucc.asn.au>
date Mon, 15 Sep 2008 14:04:55 +0000
parents e3db1f7a2e43
children 738313e73b1c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 * Dropbear - a SSH2 server
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * Copyright (c) 2002,2003 Matt Johnston
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 * All rights reserved.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 * of this software and associated documentation files (the "Software"), to deal
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * in the Software without restriction, including without limitation the rights
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 * copies of the Software, and to permit persons to whom the Software is
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 * furnished to do so, subject to the following conditions:
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 * The above copyright notice and this permission notice shall be included in
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 * all copies or substantial portions of the Software.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 * SOFTWARE. */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 #include "includes.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 #include "session.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 #include "dbutil.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 #include "packet.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 #include "algo.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 #include "buffer.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 #include "dss.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 #include "ssh.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 #include "random.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 #include "kex.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 #include "channel.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 #include "atomicio.h"
454
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
37 #include "runopts.h"
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
108
10f4d3319780 - added circular buffering for channels
Matt Johnston <matt@ucc.asn.au>
parents: 107
diff changeset
39 static void checktimeouts();
454
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
40 static long select_timeout();
108
10f4d3319780 - added circular buffering for channels
Matt Johnston <matt@ucc.asn.au>
parents: 107
diff changeset
41 static int ident_readln(int fd, char* buf, int count);
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
42
24
469950e86d0f switching to global vars
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
43 struct sshsession ses; /* GLOBAL */
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 /* need to know if the session struct has been initialised, this way isn't the
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 * cleanest, but works OK */
24
469950e86d0f switching to global vars
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
47 int sessinitdone = 0; /* GLOBAL */
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 /* this is set when we get SIGINT or SIGTERM, the handler is in main.c */
24
469950e86d0f switching to global vars
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
50 int exitflag = 0; /* GLOBAL */
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
53
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 /* called only at the start of a session, set up initial state */
479
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
55 void common_session_init(int sock_in, int sock_out, char* remotehost) {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 135
diff changeset
57 TRACE(("enter session_init"))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
59 ses.remotehost = remotehost;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60
479
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
61 ses.sock_in = sock_in;
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
62 ses.sock_out = sock_out;
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
63 ses.maxfd = MAX(sock_in, sock_out);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64
454
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
65 ses.connect_time = 0;
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
66 ses.last_packet_time = 0;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67
416
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
68 if (pipe(ses.signal_pipe) < 0) {
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
69 dropbear_exit("signal pipe failed");
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
70 }
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
71 setnonblocking(ses.signal_pipe[0]);
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
72 setnonblocking(ses.signal_pipe[1]);
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
73
33
f789045062e6 Progressing client support
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
74 kexfirstinitialise(); /* initialise the kex state */
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75
448
9c61e7af0156 Rearrange the channel buffer sizes into three neat use-editable values in
Matt Johnston <matt@ucc.asn.au>
parents: 416
diff changeset
76 ses.writepayload = buf_new(TRANS_MAX_PAYLOAD_LEN);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 ses.transseq = 0;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 ses.readbuf = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 ses.decryptreadbuf = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 ses.payload = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 ses.recvseq = 0;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83
135
d663445c1533 Initialise the outgoing packet queue
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
84 initqueue(&ses.writequeue);
d663445c1533 Initialise the outgoing packet queue
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
85
22
c1e5d9195402 merge of abac2150ee4f4031a98016241fbd136d24fed127
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
86 ses.requirenext = SSH_MSG_KEXINIT;
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
87 ses.dataallowed = 1; /* we can send data until we actually
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
88 send the SSH_MSG_KEXINIT */
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 ses.ignorenext = 0;
34
e2a1eaa19f22 Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents: 33
diff changeset
90 ses.lastpacket = 0;
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
91 ses.reply_queue_head = NULL;
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
92 ses.reply_queue_tail = NULL;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 /* set all the algos to none */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 ses.keys = (struct key_context*)m_malloc(sizeof(struct key_context));
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 ses.newkeys = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 ses.keys->recv_algo_crypt = &dropbear_nocipher;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 ses.keys->trans_algo_crypt = &dropbear_nocipher;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 ses.keys->recv_algo_mac = &dropbear_nohash;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 ses.keys->trans_algo_mac = &dropbear_nohash;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 ses.keys->algo_kex = -1;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 ses.keys->algo_hostkey = -1;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 ses.keys->recv_algo_comp = DROPBEAR_COMP_NONE;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 ses.keys->trans_algo_comp = DROPBEAR_COMP_NONE;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 #ifndef DISABLE_ZLIB
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 ses.keys->recv_zstream = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 ses.keys->trans_zstream = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 #endif
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 /* key exchange buffers */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 ses.session_id = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 ses.kexhashbuf = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 ses.transkexinit = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 ses.dh_K = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 ses.remoteident = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119
6
ab00ef513e97 Sorted out the first channel init issues.
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
120 ses.chantypes = NULL;
ab00ef513e97 Sorted out the first channel init issues.
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
121
21
d7cc5b484a2e - Port restriction code back in
Matt Johnston <matt@ucc.asn.au>
parents: 6
diff changeset
122 ses.allowprivport = 0;
d7cc5b484a2e - Port restriction code back in
Matt Johnston <matt@ucc.asn.au>
parents: 6
diff changeset
123
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 135
diff changeset
124 TRACE(("leave session_init"))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
127 void session_loop(void(*loophandler)()) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
128
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
129 fd_set readfd, writefd;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
130 struct timeval timeout;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
131 int val;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
132
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
133 /* main loop, select()s for all sockets in use */
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
134 for(;;) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
135
454
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
136 timeout.tv_sec = select_timeout();
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
137 timeout.tv_usec = 0;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
138 FD_ZERO(&writefd);
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
139 FD_ZERO(&readfd);
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
140 dropbear_assert(ses.payload == NULL);
479
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
141 if (ses.sock_in != -1) {
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
142 FD_SET(ses.sock_in, &readfd);
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
143 }
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
144 if (ses.sock_out != -1 && !isempty(&ses.writequeue)) {
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
145 FD_SET(ses.sock_out, &writefd);
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
146 }
416
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
147
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
148 /* We get woken up when signal handlers write to this pipe.
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
149 SIGCHLD in svr-chansession is the only one currently. */
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
150 FD_SET(ses.signal_pipe[0], &readfd);
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
151
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
152 /* set up for channels which require reading/writing */
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
153 if (ses.dataallowed) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
154 setchannelfds(&readfd, &writefd);
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
155 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
156 val = select(ses.maxfd+1, &readfd, &writefd, NULL, &timeout);
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
157
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
158 if (exitflag) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
159 dropbear_exit("Terminated by signal");
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
160 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
161
373
70caa99bfe3a Tidy up behaviour when select() is interrupted. We follow normal
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
162 if (val < 0 && errno != EINTR) {
70caa99bfe3a Tidy up behaviour when select() is interrupted. We follow normal
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
163 dropbear_exit("Error in select");
70caa99bfe3a Tidy up behaviour when select() is interrupted. We follow normal
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
164 }
70caa99bfe3a Tidy up behaviour when select() is interrupted. We follow normal
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
165
70caa99bfe3a Tidy up behaviour when select() is interrupted. We follow normal
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
166 if (val <= 0) {
70caa99bfe3a Tidy up behaviour when select() is interrupted. We follow normal
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
167 /* If we were interrupted or the select timed out, we still
70caa99bfe3a Tidy up behaviour when select() is interrupted. We follow normal
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
168 * want to iterate over channels etc for reading, to handle
70caa99bfe3a Tidy up behaviour when select() is interrupted. We follow normal
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
169 * server processes exiting etc.
70caa99bfe3a Tidy up behaviour when select() is interrupted. We follow normal
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
170 * We don't want to read/write FDs. */
70caa99bfe3a Tidy up behaviour when select() is interrupted. We follow normal
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
171 FD_ZERO(&writefd);
70caa99bfe3a Tidy up behaviour when select() is interrupted. We follow normal
Matt Johnston <matt@ucc.asn.au>
parents: 344
diff changeset
172 FD_ZERO(&readfd);
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
173 }
416
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
174
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
175 /* We'll just empty out the pipe if required. We don't do
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
176 any thing with the data, since the pipe's purpose is purely to
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
177 wake up the select() above. */
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
178 if (FD_ISSET(ses.signal_pipe[0], &readfd)) {
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
179 char x;
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
180 while (read(ses.signal_pipe[0], &x, 1) > 0) {}
a01c0c8e543a Improve behaviour when flushing out after a process has exited.
Matt Johnston <matt@ucc.asn.au>
parents: 373
diff changeset
181 }
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
182
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
183 /* check for auth timeout, rekeying required etc */
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
184 checktimeouts();
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
185
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
186 /* process session socket's incoming/outgoing data */
479
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
187 if (ses.sock_out != -1) {
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
188 if (FD_ISSET(ses.sock_out, &writefd) && !isempty(&ses.writequeue)) {
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
189 write_packet();
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
190 }
479
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
191 }
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
192
479
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
193 if (ses.sock_in != -1) {
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
194 if (FD_ISSET(ses.sock_in, &readfd)) {
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
195 read_packet();
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
196 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
197
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
198 /* Process the decrypted packet. After this, the read buffer
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
199 * will be ready for a new packet */
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
200 if (ses.payload != NULL) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
201 process_packet();
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
202 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
203 }
452
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
204
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
205 /* if required, flush out any queued reply packets that
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
206 were being held up during a KEX */
4cab61369879 Prevent invalid packets being sent during key-exchange, instead queue
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
207 maybe_flush_reply_queue();
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
208
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
209 /* process pipes etc for the channels, ses.dataallowed == 0
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
210 * during rekeying ) */
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
211 if (ses.dataallowed) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
212 channelio(&readfd, &writefd);
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
213 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
214
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
215 if (loophandler) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
216 loophandler();
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
217 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
218
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
219 } /* for(;;) */
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
220
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
221 /* Not reached */
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
222 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
223
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 /* clean up a session on exit */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 void common_session_cleanup() {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 135
diff changeset
227 TRACE(("enter session_cleanup"))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 /* we can't cleanup if we don't know the session state */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 if (!sessinitdone) {
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 135
diff changeset
231 TRACE(("leave session_cleanup: !sessinitdone"))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 return;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 m_free(ses.session_id);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 m_burn(ses.keys, sizeof(struct key_context));
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 m_free(ses.keys);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 chancleanup();
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 135
diff changeset
241 TRACE(("leave session_cleanup"))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 void session_identification() {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 /* max length of 255 chars */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248 char linebuf[256];
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 int len = 0;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 char done = 0;
103
8aeac62a968f Allow leading lines before the ident banner when connecting
Matt Johnston <matt@ucc.asn.au>
parents: 41
diff changeset
251 int i;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253 /* write our version string, this blocks */
479
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
254 if (atomicio(write, ses.sock_out, LOCAL_IDENT "\r\n",
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 strlen(LOCAL_IDENT "\r\n")) == DROPBEAR_FAILURE) {
344
bf29e6659fb9 Just use the normal "remote closed" handler when reading ident stings
Matt Johnston <matt@ucc.asn.au>
parents: 275
diff changeset
256 ses.remoteclosed();
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258
275
7f9adaf85fca Exit with a message if the ssh protocol version is incompatible
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
259 /* If they send more than 50 lines, something is wrong */
7f9adaf85fca Exit with a message if the ssh protocol version is incompatible
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
260 for (i = 0; i < 50; i++) {
479
e3db1f7a2e43 - Split main socket var into ses.sock_in/ses.sock_out in preparation
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
261 len = ident_readln(ses.sock_in, linebuf, sizeof(linebuf));
103
8aeac62a968f Allow leading lines before the ident banner when connecting
Matt Johnston <matt@ucc.asn.au>
parents: 41
diff changeset
262
8aeac62a968f Allow leading lines before the ident banner when connecting
Matt Johnston <matt@ucc.asn.au>
parents: 41
diff changeset
263 if (len < 0 && errno != EINTR) {
8aeac62a968f Allow leading lines before the ident banner when connecting
Matt Johnston <matt@ucc.asn.au>
parents: 41
diff changeset
264 /* It failed */
8aeac62a968f Allow leading lines before the ident banner when connecting
Matt Johnston <matt@ucc.asn.au>
parents: 41
diff changeset
265 break;
8aeac62a968f Allow leading lines before the ident banner when connecting
Matt Johnston <matt@ucc.asn.au>
parents: 41
diff changeset
266 }
8aeac62a968f Allow leading lines before the ident banner when connecting
Matt Johnston <matt@ucc.asn.au>
parents: 41
diff changeset
267
8aeac62a968f Allow leading lines before the ident banner when connecting
Matt Johnston <matt@ucc.asn.au>
parents: 41
diff changeset
268 if (len >= 4 && memcmp(linebuf, "SSH-", 4) == 0) {
8aeac62a968f Allow leading lines before the ident banner when connecting
Matt Johnston <matt@ucc.asn.au>
parents: 41
diff changeset
269 /* start of line matches */
8aeac62a968f Allow leading lines before the ident banner when connecting
Matt Johnston <matt@ucc.asn.au>
parents: 41
diff changeset
270 done = 1;
8aeac62a968f Allow leading lines before the ident banner when connecting
Matt Johnston <matt@ucc.asn.au>
parents: 41
diff changeset
271 break;
8aeac62a968f Allow leading lines before the ident banner when connecting
Matt Johnston <matt@ucc.asn.au>
parents: 41
diff changeset
272 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 if (!done) {
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 135
diff changeset
276 TRACE(("err: %s for '%s'\n", strerror(errno), linebuf))
344
bf29e6659fb9 Just use the normal "remote closed" handler when reading ident stings
Matt Johnston <matt@ucc.asn.au>
parents: 275
diff changeset
277 ses.remoteclosed();
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278 } else {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 /* linebuf is already null terminated */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280 ses.remoteident = m_malloc(len);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 memcpy(ses.remoteident, linebuf, len);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283
275
7f9adaf85fca Exit with a message if the ssh protocol version is incompatible
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
284 /* Shall assume that 2.x will be backwards compatible. */
7f9adaf85fca Exit with a message if the ssh protocol version is incompatible
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
285 if (strncmp(ses.remoteident, "SSH-2.", 6) != 0
7f9adaf85fca Exit with a message if the ssh protocol version is incompatible
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
286 && strncmp(ses.remoteident, "SSH-1.99-", 9) != 0) {
7f9adaf85fca Exit with a message if the ssh protocol version is incompatible
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
287 dropbear_exit("Incompatible remote version '%s'", ses.remoteident);
7f9adaf85fca Exit with a message if the ssh protocol version is incompatible
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
288 }
7f9adaf85fca Exit with a message if the ssh protocol version is incompatible
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
289
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 135
diff changeset
290 TRACE(("remoteident: %s", ses.remoteident))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
291
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
292 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
293
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
294 /* returns the length including null-terminating zero on success,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
295 * or -1 on failure */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
296 static int ident_readln(int fd, char* buf, int count) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
297
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298 char in;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
299 int pos = 0;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300 int num = 0;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301 fd_set fds;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302 struct timeval timeout;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
303
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 135
diff changeset
304 TRACE(("enter ident_readln"))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
305
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
306 if (count < 1) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
307 return -1;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
308 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
309
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
310 FD_ZERO(&fds);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
311
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
312 /* select since it's a non-blocking fd */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
313
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
314 /* leave space to null-terminate */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
315 while (pos < count-1) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
316
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
317 FD_SET(fd, &fds);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
318
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
319 timeout.tv_sec = 1;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
320 timeout.tv_usec = 0;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
321 if (select(fd+1, &fds, NULL, NULL, &timeout) < 0) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
322 if (errno == EINTR) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
323 continue;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324 }
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 135
diff changeset
325 TRACE(("leave ident_readln: select error"))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326 return -1;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
327 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
328
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
329 checktimeouts();
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
330
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
331 /* Have to go one byte at a time, since we don't want to read past
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
332 * the end, and have to somehow shove bytes back into the normal
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
333 * packet reader */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
334 if (FD_ISSET(fd, &fds)) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
335 num = read(fd, &in, 1);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
336 /* a "\n" is a newline, "\r" we want to read in and keep going
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
337 * so that it won't be read as part of the next line */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
338 if (num < 0) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
339 /* error */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
340 if (errno == EINTR) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
341 continue; /* not a real error */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
342 }
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 135
diff changeset
343 TRACE(("leave ident_readln: read error"))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
344 return -1;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
345 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
346 if (num == 0) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
347 /* EOF */
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 135
diff changeset
348 TRACE(("leave ident_readln: EOF"))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
349 return -1;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
350 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
351 if (in == '\n') {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
352 /* end of ident string */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
353 break;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
354 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
355 /* we don't want to include '\r's */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
356 if (in != '\r') {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
357 buf[pos] = in;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
358 pos++;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
359 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
360 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
361 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
362
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
363 buf[pos] = '\0';
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 135
diff changeset
364 TRACE(("leave ident_readln: return %d", pos+1))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
365 return pos+1;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
366 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
367
454
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
368 void send_msg_ignore() {
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
369 CHECKCLEARTOWRITE();
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
370 buf_putbyte(ses.writepayload, SSH_MSG_IGNORE);
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
371 buf_putstring(ses.writepayload, "", 0);
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
372 encrypt_packet();
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
373 }
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
374
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
375 /* Check all timeouts which are required. Currently these are the time for
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
376 * user authentication, and the automatic rekeying. */
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
377 static void checktimeouts() {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
378
454
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
379 time_t now;
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
380
454
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
381 now = time(NULL);
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
382
454
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
383 if (ses.connect_time != 0 && now - ses.connect_time >= AUTH_TIMEOUT) {
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
384 dropbear_close("Timeout before auth");
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
385 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
386
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
387 /* we can't rekey if we haven't done remote ident exchange yet */
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
388 if (ses.remoteident == NULL) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
389 return;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
390 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
391
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
392 if (!ses.kexstate.sentkexinit
454
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
393 && (now - ses.kexstate.lastkextime >= KEX_REKEY_TIMEOUT
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
394 || ses.kexstate.datarecv+ses.kexstate.datatrans >= KEX_REKEY_DATA)) {
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 135
diff changeset
395 TRACE(("rekeying after timeout or max data reached"))
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
396 send_msg_kexinit();
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
397 }
454
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
398
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
399 if (opts.keepalive_secs > 0
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
400 && now - ses.last_packet_time >= opts.keepalive_secs) {
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
401 send_msg_ignore();
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
402 }
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
403 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
404
454
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
405 static long select_timeout() {
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
406 /* determine the minimum timeout that might be required, so
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
407 as to avoid waking when unneccessary */
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
408 long ret = LONG_MAX;
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
409 if (KEX_REKEY_TIMEOUT > 0)
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
410 ret = MIN(KEX_REKEY_TIMEOUT, ret);
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
411 if (AUTH_TIMEOUT > 0)
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
412 ret = MIN(AUTH_TIMEOUT, ret);
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
413 if (opts.keepalive_secs > 0)
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
414 ret = MIN(opts.keepalive_secs, ret);
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
415 return ret;
7e43f5e473b9 - Add -K keepalive flag for dropbear and dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 452
diff changeset
416 }
482
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
417
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
418 const char* get_user_shell() {
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
419 /* an empty shell should be interpreted as "/bin/sh" */
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
420 if (ses.authstate.pw_shell[0] == '\0') {
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
421 return "/bin/sh";
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
422 } else {
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
423 return ses.authstate.pw_shell;
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
424 }
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 479
diff changeset
425 }