annotate dbutil.c @ 1025:02baa0b334e8 fastopen

async connections working
author Matt Johnston <matt@ucc.asn.au>
date Wed, 18 Feb 2015 22:46:15 +0800
parents aac0095dc3b4
children daf21fd50abf
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 * strlcat() is copyright as follows:
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 * Copyright (c) 1998 Todd C. Miller <[email protected]>
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 * All rights reserved.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 * Redistribution and use in source and binary forms, with or without
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 * modification, are permitted provided that the following conditions
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 * are met:
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 * 1. Redistributions of source code must retain the above copyright
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 * notice, this list of conditions and the following disclaimer.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 * 2. Redistributions in binary form must reproduce the above copyright
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 * notice, this list of conditions and the following disclaimer in the
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 * documentation and/or other materials provided with the distribution.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 * 3. The name of the author may not be used to endorse or promote products
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 * derived from this software without specific prior written permission.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50
928
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
51 #include "config.h"
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
52
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
53 #ifdef __linux__
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
54 #define _GNU_SOURCE
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
55 /* To call clock_gettime() directly */
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
56 #include <sys/syscall.h>
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
57 #endif /* __linux */
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
58
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
59 #ifdef HAVE_MACH_MACH_TIME_H
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
60 #include <mach/mach_time.h>
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
61 #include <mach/mach.h>
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
62 #endif
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
63
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 #include "includes.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 #include "dbutil.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 #include "buffer.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 #include "session.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 #include "atomicio.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 #define MAX_FMT 100
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71
73
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
72 static void generic_dropbear_exit(int exitcode, const char* format,
614
00eca37e47e8 Add noreturn and format attribute hints for some functions.
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
73 va_list param) ATTRIB_NORETURN;
73
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
74 static void generic_dropbear_log(int priority, const char* format,
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
75 va_list param);
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
76
614
00eca37e47e8 Add noreturn and format attribute hints for some functions.
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
77 void (*_dropbear_exit)(int exitcode, const char* format, va_list param) ATTRIB_NORETURN
73
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
78 = generic_dropbear_exit;
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
79 void (*_dropbear_log)(int priority, const char* format, va_list param)
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
80 = generic_dropbear_log;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81
94
c85c88500ea6 DEBUG_TRACE now only triggers with -v on the cmdline
Matt Johnston <matt@ucc.asn.au>
parents: 73
diff changeset
82 #ifdef DEBUG_TRACE
c85c88500ea6 DEBUG_TRACE now only triggers with -v on the cmdline
Matt Johnston <matt@ucc.asn.au>
parents: 73
diff changeset
83 int debug_trace = 0;
c85c88500ea6 DEBUG_TRACE now only triggers with -v on the cmdline
Matt Johnston <matt@ucc.asn.au>
parents: 73
diff changeset
84 #endif
c85c88500ea6 DEBUG_TRACE now only triggers with -v on the cmdline
Matt Johnston <matt@ucc.asn.au>
parents: 73
diff changeset
85
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 #ifndef DISABLE_SYSLOG
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 void startsyslog() {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 openlog(PROGNAME, LOG_PID, LOG_AUTHPRIV);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 #endif /* DISABLE_SYSLOG */
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 /* the "format" string must be <= 100 characters */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 void dropbear_close(const char* format, ...) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 va_list param;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 va_start(param, format);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 _dropbear_exit(EXIT_SUCCESS, format, param);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 va_end(param);
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 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 void dropbear_exit(const char* format, ...) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 va_list param;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 va_start(param, format);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 _dropbear_exit(EXIT_FAILURE, format, param);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 va_end(param);
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
73
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
114 static void generic_dropbear_exit(int exitcode, const char* format,
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
115 va_list param) {
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
116
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
117 char fmtbuf[300];
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
118
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
119 snprintf(fmtbuf, sizeof(fmtbuf), "Exited: %s", format);
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
120
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
121 _dropbear_log(LOG_INFO, fmtbuf, param);
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
122
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
123 exit(exitcode);
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
124 }
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
125
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
126 void fail_assert(const char* expr, const char* file, int line) {
594
a98a2138364a Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
127 dropbear_exit("Failed assertion (%s:%d): `%s'", file, line, expr);
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
128 }
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
129
108
10f4d3319780 - added circular buffering for channels
Matt Johnston <matt@ucc.asn.au>
parents: 107
diff changeset
130 static void generic_dropbear_log(int UNUSED(priority), const char* format,
73
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
131 va_list param) {
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
132
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
133 char printbuf[1024];
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
134
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
135 vsnprintf(printbuf, sizeof(printbuf), format, param);
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
136
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
137 fprintf(stderr, "%s\n", printbuf);
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
138
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
139 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 /* this is what can be called to write arbitrary log messages */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 void dropbear_log(int priority, const char* format, ...) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 va_list param;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 va_start(param, format);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 _dropbear_log(priority, format, param);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 va_end(param);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 #ifdef DEBUG_TRACE
1016
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
153
1021
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
154 static double debug_start_time = -1;
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
155
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
156 void debug_start_net()
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
157 {
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
158 if (getenv("DROPBEAR_DEBUG_NET_TIMESTAMP"))
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
159 {
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
160 /* Timestamps start from first network activity */
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
161 struct timeval tv;
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
162 gettimeofday(&tv, NULL);
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
163 debug_start_time = tv.tv_sec + (tv.tv_usec / 1000000.0);
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
164 TRACE(("Resetting Dropbear TRACE timestamps"))
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
165 }
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
166 }
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
167
1016
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
168 static double time_since_start()
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
169 {
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
170 double nowf;
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
171 struct timeval tv;
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
172 gettimeofday(&tv, NULL);
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
173 nowf = tv.tv_sec + (tv.tv_usec / 1000000.0);
1021
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
174 if (debug_start_time < 0)
1016
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
175 {
1021
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
176 debug_start_time = nowf;
1016
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
177 return 0;
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
178 }
1021
24135c8e1d46 Add envirnonment variable for debug timestamps to roughly match
Matt Johnston <matt@ucc.asn.au>
parents: 1018
diff changeset
179 return nowf - debug_start_time;
1016
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
180 }
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
181
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 void dropbear_trace(const char* format, ...) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 va_list param;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184
94
c85c88500ea6 DEBUG_TRACE now only triggers with -v on the cmdline
Matt Johnston <matt@ucc.asn.au>
parents: 73
diff changeset
185 if (!debug_trace) {
c85c88500ea6 DEBUG_TRACE now only triggers with -v on the cmdline
Matt Johnston <matt@ucc.asn.au>
parents: 73
diff changeset
186 return;
c85c88500ea6 DEBUG_TRACE now only triggers with -v on the cmdline
Matt Johnston <matt@ucc.asn.au>
parents: 73
diff changeset
187 }
c85c88500ea6 DEBUG_TRACE now only triggers with -v on the cmdline
Matt Johnston <matt@ucc.asn.au>
parents: 73
diff changeset
188
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 va_start(param, format);
1016
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
190 fprintf(stderr, "TRACE (%d) %f: ", getpid(), time_since_start());
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 vfprintf(stderr, format, param);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 fprintf(stderr, "\n");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 va_end(param);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194 }
753
d63ef1e211ea Take transmit and receive keys into use separately
Matt Johnston <matt@ucc.asn.au>
parents: 731
diff changeset
195
731
9a5438271556 Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
196 void dropbear_trace2(const char* format, ...) {
753
d63ef1e211ea Take transmit and receive keys into use separately
Matt Johnston <matt@ucc.asn.au>
parents: 731
diff changeset
197 static int trace_env = -1;
731
9a5438271556 Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
198 va_list param;
9a5438271556 Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
199
753
d63ef1e211ea Take transmit and receive keys into use separately
Matt Johnston <matt@ucc.asn.au>
parents: 731
diff changeset
200 if (trace_env == -1) {
d63ef1e211ea Take transmit and receive keys into use separately
Matt Johnston <matt@ucc.asn.au>
parents: 731
diff changeset
201 trace_env = getenv("DROPBEAR_TRACE2") ? 1 : 0;
d63ef1e211ea Take transmit and receive keys into use separately
Matt Johnston <matt@ucc.asn.au>
parents: 731
diff changeset
202 }
d63ef1e211ea Take transmit and receive keys into use separately
Matt Johnston <matt@ucc.asn.au>
parents: 731
diff changeset
203
d63ef1e211ea Take transmit and receive keys into use separately
Matt Johnston <matt@ucc.asn.au>
parents: 731
diff changeset
204 if (!(debug_trace && trace_env)) {
731
9a5438271556 Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
205 return;
9a5438271556 Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
206 }
9a5438271556 Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
207
9a5438271556 Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
208 va_start(param, format);
1016
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
209 fprintf(stderr, "TRACE2 (%d) %f: ", getpid(), time_since_start());
731
9a5438271556 Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
210 vfprintf(stderr, format, param);
9a5438271556 Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
211 fprintf(stderr, "\n");
9a5438271556 Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
212 va_end(param);
9a5438271556 Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
213 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 #endif /* DEBUG_TRACE */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215
870
80af450dae76 Set IPTOS_LOWDELAY on PTY sessions only
Catalin Patulea <cat@vv.carleton.ca>
parents: 864
diff changeset
216 void set_sock_nodelay(int sock) {
251
b02e8eef3c3a - new function to set "low delay" for a packet, set the ip TOS bit
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
217 int val;
b02e8eef3c3a - new function to set "low delay" for a packet, set the ip TOS bit
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
218
b02e8eef3c3a - new function to set "low delay" for a packet, set the ip TOS bit
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
219 /* disable nagle */
b02e8eef3c3a - new function to set "low delay" for a packet, set the ip TOS bit
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
220 val = 1;
b02e8eef3c3a - new function to set "low delay" for a packet, set the ip TOS bit
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
221 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&val, sizeof(val));
870
80af450dae76 Set IPTOS_LOWDELAY on PTY sessions only
Catalin Patulea <cat@vv.carleton.ca>
parents: 864
diff changeset
222 }
80af450dae76 Set IPTOS_LOWDELAY on PTY sessions only
Catalin Patulea <cat@vv.carleton.ca>
parents: 864
diff changeset
223
1023
a00303a7d247 tcp fastopen for the server
Matt Johnston <matt@ucc.asn.au>
parents: 1022
diff changeset
224 #ifdef DROPBEAR_TCP_FAST_OPEN
a00303a7d247 tcp fastopen for the server
Matt Johnston <matt@ucc.asn.au>
parents: 1022
diff changeset
225 void set_listen_fast_open(int sock) {
a00303a7d247 tcp fastopen for the server
Matt Johnston <matt@ucc.asn.au>
parents: 1022
diff changeset
226 int qlen = MAX(MAX_UNAUTH_PER_IP, 5);
a00303a7d247 tcp fastopen for the server
Matt Johnston <matt@ucc.asn.au>
parents: 1022
diff changeset
227 if (setsockopt(sock, SOL_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) != 0) {
a00303a7d247 tcp fastopen for the server
Matt Johnston <matt@ucc.asn.au>
parents: 1022
diff changeset
228 TRACE(("set_listen_fast_open failed for socket %d: %s", sock, strerror(errno)))
a00303a7d247 tcp fastopen for the server
Matt Johnston <matt@ucc.asn.au>
parents: 1022
diff changeset
229 }
a00303a7d247 tcp fastopen for the server
Matt Johnston <matt@ucc.asn.au>
parents: 1022
diff changeset
230 }
a00303a7d247 tcp fastopen for the server
Matt Johnston <matt@ucc.asn.au>
parents: 1022
diff changeset
231
a00303a7d247 tcp fastopen for the server
Matt Johnston <matt@ucc.asn.au>
parents: 1022
diff changeset
232 #endif
a00303a7d247 tcp fastopen for the server
Matt Johnston <matt@ucc.asn.au>
parents: 1022
diff changeset
233
871
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
234 void set_sock_priority(int sock, enum dropbear_prio prio) {
870
80af450dae76 Set IPTOS_LOWDELAY on PTY sessions only
Catalin Patulea <cat@vv.carleton.ca>
parents: 864
diff changeset
235
871
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
236 int iptos_val = 0, so_prio_val = 0, rc;
251
b02e8eef3c3a - new function to set "low delay" for a packet, set the ip TOS bit
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
237
962
db9fa5971d24 Don't warn about ENOTSOCK when setting priority
Matt Johnston <matt@ucc.asn.au>
parents: 953
diff changeset
238 /* Don't log ENOTSOCK errors so that this can harmlessly be called
db9fa5971d24 Don't warn about ENOTSOCK when setting priority
Matt Johnston <matt@ucc.asn.au>
parents: 953
diff changeset
239 * on a client '-J' proxy pipe */
db9fa5971d24 Don't warn about ENOTSOCK when setting priority
Matt Johnston <matt@ucc.asn.au>
parents: 953
diff changeset
240
629
bdadc7f4b97d Set IPTOS_LOWDELAY for IPv6 too
Matt Johnston <matt@ucc.asn.au>
parents: 627
diff changeset
241 /* set the TOS bit for either ipv4 or ipv6 */
251
b02e8eef3c3a - new function to set "low delay" for a packet, set the ip TOS bit
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
242 #ifdef IPTOS_LOWDELAY
871
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
243 if (prio == DROPBEAR_PRIO_LOWDELAY) {
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
244 iptos_val = IPTOS_LOWDELAY;
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
245 } else if (prio == DROPBEAR_PRIO_BULK) {
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
246 iptos_val = IPTOS_THROUGHPUT;
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
247 }
639
452bcf810e44 Put better #if guards around IPv6 socket options for IPV6_TCLASS and
Matt Johnston <matt@ucc.asn.au>
parents: 629
diff changeset
248 #if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS)
871
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
249 rc = setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, (void*)&iptos_val, sizeof(iptos_val));
962
db9fa5971d24 Don't warn about ENOTSOCK when setting priority
Matt Johnston <matt@ucc.asn.au>
parents: 953
diff changeset
250 if (rc < 0 && errno != ENOTSOCK) {
871
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
251 TRACE(("Couldn't set IPV6_TCLASS (%s)", strerror(errno)));
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
252 }
629
bdadc7f4b97d Set IPTOS_LOWDELAY for IPv6 too
Matt Johnston <matt@ucc.asn.au>
parents: 627
diff changeset
253 #endif
871
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
254 rc = setsockopt(sock, IPPROTO_IP, IP_TOS, (void*)&iptos_val, sizeof(iptos_val));
962
db9fa5971d24 Don't warn about ENOTSOCK when setting priority
Matt Johnston <matt@ucc.asn.au>
parents: 953
diff changeset
255 if (rc < 0 && errno != ENOTSOCK) {
871
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
256 TRACE(("Couldn't set IP_TOS (%s)", strerror(errno)));
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
257 }
251
b02e8eef3c3a - new function to set "low delay" for a packet, set the ip TOS bit
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
258 #endif
b02e8eef3c3a - new function to set "low delay" for a packet, set the ip TOS bit
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
259
b02e8eef3c3a - new function to set "low delay" for a packet, set the ip TOS bit
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
260 #ifdef SO_PRIORITY
871
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
261 if (prio == DROPBEAR_PRIO_LOWDELAY) {
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
262 so_prio_val = TC_PRIO_INTERACTIVE;
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
263 } else if (prio == DROPBEAR_PRIO_BULK) {
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
264 so_prio_val = TC_PRIO_BULK;
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
265 }
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
266 /* linux specific, sets QoS class. see tc-prio(8) */
aa689d140928 - Sockets are set to lowdelay priority initially to improve conneciton setup
Matt Johnston <matt@ucc.asn.au>
parents: 870
diff changeset
267 rc = setsockopt(sock, SOL_SOCKET, SO_PRIORITY, (void*) &so_prio_val, sizeof(so_prio_val));
962
db9fa5971d24 Don't warn about ENOTSOCK when setting priority
Matt Johnston <matt@ucc.asn.au>
parents: 953
diff changeset
268 if (rc < 0 && errno != ENOTSOCK)
870
80af450dae76 Set IPTOS_LOWDELAY on PTY sessions only
Catalin Patulea <cat@vv.carleton.ca>
parents: 864
diff changeset
269 dropbear_log(LOG_WARNING, "Couldn't set SO_PRIORITY (%s)",
80af450dae76 Set IPTOS_LOWDELAY on PTY sessions only
Catalin Patulea <cat@vv.carleton.ca>
parents: 864
diff changeset
270 strerror(errno));
251
b02e8eef3c3a - new function to set "low delay" for a packet, set the ip TOS bit
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
271 #endif
b02e8eef3c3a - new function to set "low delay" for a packet, set the ip TOS bit
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
272
b02e8eef3c3a - new function to set "low delay" for a packet, set the ip TOS bit
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
273 }
b02e8eef3c3a - new function to set "low delay" for a packet, set the ip TOS bit
Matt Johnston <matt@ucc.asn.au>
parents: 242
diff changeset
274
258
306499676384 * add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents: 251
diff changeset
275 /* Listen on address:port.
306499676384 * add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents: 251
diff changeset
276 * Special cases are address of "" listening on everything,
306499676384 * add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents: 251
diff changeset
277 * and address of NULL listening on localhost only.
63
dcc43965928f - A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents: 62
diff changeset
278 * Returns the number of sockets bound on success, or -1 on failure. On
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
279 * failure, if errstring wasn't NULL, it'll be a newly malloced error
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
280 * string.*/
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
281 int dropbear_listen(const char* address, const char* port,
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
282 int *socks, unsigned int sockcount, char **errstring, int *maxfd) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
283
70
b0316ce64e4b Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents: 68
diff changeset
284 struct addrinfo hints, *res = NULL, *res0 = NULL;
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
285 int err;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
286 unsigned int nsock;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
287 struct linger linger;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
288 int val;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
289 int sock;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
290
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 161
diff changeset
291 TRACE(("enter dropbear_listen"))
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
292
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
293 memset(&hints, 0, sizeof(hints));
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
294 hints.ai_family = AF_UNSPEC; /* TODO: let them flag v4 only etc */
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
295 hints.ai_socktype = SOCK_STREAM;
63
dcc43965928f - A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents: 62
diff changeset
296
433
c216212001fc Fix for -pedantic -ansi compilation, change // to /**/, plus some signedness
Matt Johnston <matt@ucc.asn.au>
parents: 425
diff changeset
297 /* for calling getaddrinfo:
c216212001fc Fix for -pedantic -ansi compilation, change // to /**/, plus some signedness
Matt Johnston <matt@ucc.asn.au>
parents: 425
diff changeset
298 address == NULL and !AI_PASSIVE: local loopback
c216212001fc Fix for -pedantic -ansi compilation, change // to /**/, plus some signedness
Matt Johnston <matt@ucc.asn.au>
parents: 425
diff changeset
299 address == NULL and AI_PASSIVE: all interfaces
c216212001fc Fix for -pedantic -ansi compilation, change // to /**/, plus some signedness
Matt Johnston <matt@ucc.asn.au>
parents: 425
diff changeset
300 address != NULL: whatever the address says */
258
306499676384 * add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents: 251
diff changeset
301 if (!address) {
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 161
diff changeset
302 TRACE(("dropbear_listen: local loopback"))
63
dcc43965928f - A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents: 62
diff changeset
303 } else {
258
306499676384 * add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents: 251
diff changeset
304 if (address[0] == '\0') {
306499676384 * add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents: 251
diff changeset
305 TRACE(("dropbear_listen: all interfaces"))
306499676384 * add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents: 251
diff changeset
306 address = NULL;
306499676384 * add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents: 251
diff changeset
307 }
63
dcc43965928f - A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents: 62
diff changeset
308 hints.ai_flags = AI_PASSIVE;
dcc43965928f - A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents: 62
diff changeset
309 }
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
310 err = getaddrinfo(address, port, &hints, &res0);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
311
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
312 if (err) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
313 if (errstring != NULL && *errstring == NULL) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
314 int len;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
315 len = 20 + strlen(gai_strerror(err));
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
316 *errstring = (char*)m_malloc(len);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
317 snprintf(*errstring, len, "Error resolving: %s", gai_strerror(err));
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
318 }
239
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
319 if (res0) {
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
320 freeaddrinfo(res0);
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
321 res0 = NULL;
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
322 }
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 161
diff changeset
323 TRACE(("leave dropbear_listen: failed resolving"))
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
324 return -1;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
325 }
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
326
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
327
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
328 nsock = 0;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
329 for (res = res0; res != NULL && nsock < sockcount;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
330 res = res->ai_next) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
331
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
332 /* Get a socket */
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
333 socks[nsock] = socket(res->ai_family, res->ai_socktype,
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
334 res->ai_protocol);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
335
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
336 sock = socks[nsock]; /* For clarity */
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
337
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
338 if (sock < 0) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
339 err = errno;
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 161
diff changeset
340 TRACE(("socket() failed"))
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
341 continue;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
342 }
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
343
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
344 /* Various useful socket options */
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
345 val = 1;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
346 /* set to reuse, quick timeout */
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
347 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &val, sizeof(val));
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
348 linger.l_onoff = 1;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
349 linger.l_linger = 5;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
350 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void*)&linger, sizeof(linger));
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
351
639
452bcf810e44 Put better #if guards around IPv6 socket options for IPV6_TCLASS and
Matt Johnston <matt@ucc.asn.au>
parents: 629
diff changeset
352 #if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY)
627
7cc34a52feb8 Try bind IPV6_V6ONLY
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
353 if (res->ai_family == AF_INET6) {
7cc34a52feb8 Try bind IPV6_V6ONLY
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
354 int on = 1;
7cc34a52feb8 Try bind IPV6_V6ONLY
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
355 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
7cc34a52feb8 Try bind IPV6_V6ONLY
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
356 &on, sizeof(on)) == -1) {
7cc34a52feb8 Try bind IPV6_V6ONLY
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
357 dropbear_log(LOG_WARNING, "Couldn't set IPV6_V6ONLY");
7cc34a52feb8 Try bind IPV6_V6ONLY
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
358 }
7cc34a52feb8 Try bind IPV6_V6ONLY
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
359 }
7cc34a52feb8 Try bind IPV6_V6ONLY
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
360 #endif
7cc34a52feb8 Try bind IPV6_V6ONLY
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
361
870
80af450dae76 Set IPTOS_LOWDELAY on PTY sessions only
Catalin Patulea <cat@vv.carleton.ca>
parents: 864
diff changeset
362 set_sock_nodelay(sock);
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
363
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
364 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
365 err = errno;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
366 close(sock);
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 161
diff changeset
367 TRACE(("bind(%s) failed", port))
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
368 continue;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
369 }
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
370
936
d93a6bcf616f Improve handling lots of concurrent forwarded connections. Increase
Matt Johnston <matt@ucc.asn.au>
parents: 930
diff changeset
371 if (listen(sock, DROPBEAR_LISTEN_BACKLOG) < 0) {
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
372 err = errno;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
373 close(sock);
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 161
diff changeset
374 TRACE(("listen() failed"))
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
375 continue;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
376 }
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
377
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
378 *maxfd = MAX(*maxfd, sock);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
379
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
380 nsock++;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
381 }
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
382
239
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
383 if (res0) {
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
384 freeaddrinfo(res0);
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
385 res0 = NULL;
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
386 }
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
387
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
388 if (nsock == 0) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
389 if (errstring != NULL && *errstring == NULL) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
390 int len;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
391 len = 20 + strlen(strerror(err));
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
392 *errstring = (char*)m_malloc(len);
64
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
393 snprintf(*errstring, len, "Error listening: %s", strerror(err));
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
394 }
408
28b10e93685c Fix failure-handling in dropbear_listen() when errstring is unset
Matt Johnston <matt@ucc.asn.au>
parents: 277
diff changeset
395 TRACE(("leave dropbear_listen: failure, %s", strerror(err)))
28b10e93685c Fix failure-handling in dropbear_listen() when errstring is unset
Matt Johnston <matt@ucc.asn.au>
parents: 277
diff changeset
396 return -1;
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
397 }
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
398
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 161
diff changeset
399 TRACE(("leave dropbear_listen: success, %d socks bound", nsock))
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
400 return nsock;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
401 }
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
402
547
cf376c696dfc Make it compile, update for changes in channel structure.
Matt Johnston <matt@ucc.asn.au>
parents: 500
diff changeset
403 /* Connect to a given unix socket. The socket is blocking */
225
ca7e76d981d9 - progress towards client agent forwarding
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
404 #ifdef ENABLE_CONNECT_UNIX
550
61c3513825b0 Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents: 547
diff changeset
405 int connect_unix(const char* path) {
61c3513825b0 Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents: 547
diff changeset
406 struct sockaddr_un addr;
225
ca7e76d981d9 - progress towards client agent forwarding
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
407 int fd = -1;
ca7e76d981d9 - progress towards client agent forwarding
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
408
550
61c3513825b0 Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents: 547
diff changeset
409 memset((void*)&addr, 0x0, sizeof(addr));
61c3513825b0 Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents: 547
diff changeset
410 addr.sun_family = AF_UNIX;
61c3513825b0 Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents: 547
diff changeset
411 strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
225
ca7e76d981d9 - progress towards client agent forwarding
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
412 fd = socket(PF_UNIX, SOCK_STREAM, 0);
550
61c3513825b0 Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents: 547
diff changeset
413 if (fd < 0) {
61c3513825b0 Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents: 547
diff changeset
414 TRACE(("Failed to open unix socket"))
61c3513825b0 Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents: 547
diff changeset
415 return -1;
61c3513825b0 Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents: 547
diff changeset
416 }
61c3513825b0 Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents: 547
diff changeset
417 if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
61c3513825b0 Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents: 547
diff changeset
418 TRACE(("Failed to connect to '%s' socket", path))
615
e3ac0a426bd0 Fix FD leak if connect() fails, found by Klocwork
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
419 m_close(fd);
550
61c3513825b0 Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents: 547
diff changeset
420 return -1;
61c3513825b0 Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents: 547
diff changeset
421 }
225
ca7e76d981d9 - progress towards client agent forwarding
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
422 return fd;
ca7e76d981d9 - progress towards client agent forwarding
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
423 }
ca7e76d981d9 - progress towards client agent forwarding
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
424 #endif
ca7e76d981d9 - progress towards client agent forwarding
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
425
1016
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
426 #if defined(__linux__) && defined(TCP_DEFER_ACCEPT)
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
427 static void set_piggyback_ack(int sock) {
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
428 /* Undocumented Linux feature - set TCP_DEFER_ACCEPT and data will be piggybacked
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
429 on the 3rd packet (ack) of the TCP handshake. Saves a IP packet.
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
430 http://thread.gmane.org/gmane.linux.network/224627/focus=224727
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
431 "Piggyback the final ACK of the three way TCP connection establishment with the data" */
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
432 int val = 1;
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
433 /* No error checking, this is opportunistic */
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
434 int err = setsockopt(sock, IPPROTO_TCP, TCP_DEFER_ACCEPT, (void*)&val, sizeof(val));
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
435 if (err)
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
436 {
1018
cb148f8d3d22 Make it compile
Matt Johnston <matt@ucc.asn.au>
parents: 1016
diff changeset
437 TRACE(("Failed setsockopt TCP_DEFER_ACCEPT: %s", strerror(errno)))
1016
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
438 }
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
439 }
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
440 #endif
257f7d5fca97 piggyback data on acks when making connections on linux
Matt Johnston <matt@ucc.asn.au>
parents: 995
diff changeset
441
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
442 /* Sets up a pipe for a, returning three non-blocking file descriptors
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
443 * and the pid. exec_fn is the function that will actually execute the child process,
484
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
444 * it will be run after the child has fork()ed, and is passed exec_data.
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
445 * If ret_errfd == NULL then stderr will not be captured.
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
446 * ret_pid can be passed as NULL to discard the pid. */
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
447 int spawn_command(void(*exec_fn)(void *user_data), void *exec_data,
482
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
448 int *ret_writefd, int *ret_readfd, int *ret_errfd, pid_t *ret_pid) {
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
449 int infds[2];
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
450 int outfds[2];
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
451 int errfds[2];
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
452 pid_t pid;
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
453
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
454 const int FDIN = 0;
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
455 const int FDOUT = 1;
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
456
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
457 /* redirect stdin/stdout/stderr */
484
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
458 if (pipe(infds) != 0) {
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
459 return DROPBEAR_FAILURE;
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
460 }
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
461 if (pipe(outfds) != 0) {
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
462 return DROPBEAR_FAILURE;
484
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
463 }
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
464 if (ret_errfd && pipe(errfds) != 0) {
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
465 return DROPBEAR_FAILURE;
484
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
466 }
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
467
667
fc7ae88e63b3 Rename HAVE_FORK to USE_VFORK
Matt Johnston <matt@ucc.asn.au>
parents: 666
diff changeset
468 #ifdef USE_VFORK
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
469 pid = vfork();
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
470 #else
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
471 pid = fork();
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
472 #endif
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
473
484
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
474 if (pid < 0) {
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
475 return DROPBEAR_FAILURE;
484
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
476 }
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
477
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
478 if (!pid) {
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
479 /* child */
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
480
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
481 TRACE(("back to normal sigchld"))
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
482 /* Revert to normal sigchld handling */
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
483 if (signal(SIGCHLD, SIG_DFL) == SIG_ERR) {
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
484 dropbear_exit("signal() error");
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
485 }
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
486
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
487 /* redirect stdin/stdout */
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
488
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
489 if ((dup2(infds[FDIN], STDIN_FILENO) < 0) ||
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
490 (dup2(outfds[FDOUT], STDOUT_FILENO) < 0) ||
484
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
491 (ret_errfd && dup2(errfds[FDOUT], STDERR_FILENO) < 0)) {
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
492 TRACE(("leave noptycommand: error redirecting FDs"))
594
a98a2138364a Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
493 dropbear_exit("Child dup2() failure");
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
494 }
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
495
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
496 close(infds[FDOUT]);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
497 close(infds[FDIN]);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
498 close(outfds[FDIN]);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
499 close(outfds[FDOUT]);
484
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
500 if (ret_errfd)
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
501 {
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
502 close(errfds[FDIN]);
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
503 close(errfds[FDOUT]);
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
504 }
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
505
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
506 exec_fn(exec_data);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
507 /* not reached */
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
508 return DROPBEAR_FAILURE;
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
509 } else {
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
510 /* parent */
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
511 close(infds[FDIN]);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
512 close(outfds[FDOUT]);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
513
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
514 setnonblocking(outfds[FDIN]);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
515 setnonblocking(infds[FDOUT]);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
516
484
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
517 if (ret_errfd) {
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
518 close(errfds[FDOUT]);
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
519 setnonblocking(errfds[FDIN]);
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
520 }
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
521
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
522 if (ret_pid) {
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
523 *ret_pid = pid;
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
524 }
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
525
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
526 *ret_writefd = infds[FDOUT];
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
527 *ret_readfd = outfds[FDIN];
484
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
528 if (ret_errfd) {
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
529 *ret_errfd = errfds[FDIN];
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
530 }
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
531 return DROPBEAR_SUCCESS;
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
532 }
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
533 }
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
534
482
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
535 /* Runs a command with "sh -c". Will close FDs (except stdin/stdout/stderr) and
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
536 * re-enabled SIGPIPE. If cmd is NULL, will run a login shell.
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
537 */
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
538 void run_shell_command(const char* cmd, unsigned int maxfd, char* usershell) {
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
539 char * argv[4];
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
540 char * baseshell = NULL;
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
541 unsigned int i;
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
542
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
543 baseshell = basename(usershell);
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
544
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
545 if (cmd != NULL) {
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
546 argv[0] = baseshell;
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
547 } else {
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
548 /* a login shell should be "-bash" for "/bin/bash" etc */
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
549 int len = strlen(baseshell) + 2; /* 2 for "-" */
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
550 argv[0] = (char*)m_malloc(len);
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
551 snprintf(argv[0], len, "-%s", baseshell);
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
552 }
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
553
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
554 if (cmd != NULL) {
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
555 argv[1] = "-c";
492
b956d6151600 Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
556 argv[2] = (char*)cmd;
482
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
557 argv[3] = NULL;
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
558 } else {
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
559 /* construct a shell of the form "-bash" etc */
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
560 argv[1] = NULL;
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
561 }
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
562
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
563 /* Re-enable SIGPIPE for the executed process */
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
564 if (signal(SIGPIPE, SIG_DFL) == SIG_ERR) {
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
565 dropbear_exit("signal() error");
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
566 }
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
567
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
568 /* close file descriptors except stdin/stdout/stderr
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
569 * Need to be sure FDs are closed here to avoid reading files as root */
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
570 for (i = 3; i <= maxfd; i++) {
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
571 m_close(i);
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
572 }
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
573
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
574 execv(usershell, argv);
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
575 }
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
576
568
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
577 void get_socket_address(int fd, char **local_host, char **local_port,
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
578 char **remote_host, char **remote_port, int host_lookup)
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
579 {
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
580 struct sockaddr_storage addr;
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
581 socklen_t addrlen;
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
582
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
583 if (local_host || local_port) {
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
584 addrlen = sizeof(addr);
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
585 if (getsockname(fd, (struct sockaddr*)&addr, &addrlen) < 0) {
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
586 dropbear_exit("Failed socket address: %s", strerror(errno));
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
587 }
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
588 getaddrstring(&addr, local_host, local_port, host_lookup);
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
589 }
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
590 if (remote_host || remote_port) {
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
591 addrlen = sizeof(addr);
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
592 if (getpeername(fd, (struct sockaddr*)&addr, &addrlen) < 0) {
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
593 dropbear_exit("Failed socket address: %s", strerror(errno));
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
594 }
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
595 getaddrstring(&addr, remote_host, remote_port, host_lookup);
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
596 }
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
597 }
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
598
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
599 /* Return a string representation of the socket address passed. The return
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
600 * value is allocated with malloc() */
568
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
601 void getaddrstring(struct sockaddr_storage* addr,
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
602 char **ret_host, char **ret_port,
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
603 int host_lookup) {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
604
568
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
605 char host[NI_MAXHOST+1], serv[NI_MAXSERV+1];
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
606 unsigned int len;
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
607 int ret;
568
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
608
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
609 int flags = NI_NUMERICSERV | NI_NUMERICHOST;
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
610
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
611 #ifndef DO_HOST_LOOKUP
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
612 host_lookup = 0;
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
613 #endif
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
614
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
615 if (host_lookup) {
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
616 flags = NI_NUMERICSERV;
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
617 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
618
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
619 len = sizeof(struct sockaddr_storage);
160
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
620 /* Some platforms such as Solaris 8 require that len is the length
335
e17f0333c21e Another stab at getting ss_family happy on older glibc
Matt Johnston <matt@ucc.asn.au>
parents: 277
diff changeset
621 * of the specific structure. Some older linux systems (glibc 2.1.3
e17f0333c21e Another stab at getting ss_family happy on older glibc
Matt Johnston <matt@ucc.asn.au>
parents: 277
diff changeset
622 * such as debian potato) have sockaddr_storage.__ss_family instead
e17f0333c21e Another stab at getting ss_family happy on older glibc
Matt Johnston <matt@ucc.asn.au>
parents: 277
diff changeset
623 * but we'll ignore them */
e17f0333c21e Another stab at getting ss_family happy on older glibc
Matt Johnston <matt@ucc.asn.au>
parents: 277
diff changeset
624 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY
160
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
625 if (addr->ss_family == AF_INET) {
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
626 len = sizeof(struct sockaddr_in);
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
627 }
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
628 #ifdef AF_INET6
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
629 if (addr->ss_family == AF_INET6) {
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
630 len = sizeof(struct sockaddr_in6);
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
631 }
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
632 #endif
335
e17f0333c21e Another stab at getting ss_family happy on older glibc
Matt Johnston <matt@ucc.asn.au>
parents: 277
diff changeset
633 #endif
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
634
568
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
635 ret = getnameinfo((struct sockaddr*)addr, len, host, sizeof(host)-1,
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
636 serv, sizeof(serv)-1, flags);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
637
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
638 if (ret != 0) {
568
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
639 if (host_lookup) {
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
640 /* On some systems (Darwin does it) we get EINTR from getnameinfo
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
641 * somehow. Eew. So we'll just return the IP, since that doesn't seem
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
642 * to exhibit that behaviour. */
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
643 getaddrstring(addr, ret_host, ret_port, 0);
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
644 return;
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
645 } else {
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
646 /* if we can't do a numeric lookup, something's gone terribly wrong */
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
647 dropbear_exit("Failed lookup: %s", gai_strerror(ret));
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
648 }
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
649 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
650
568
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
651 if (ret_host) {
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
652 *ret_host = m_strdup(host);
160
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
653 }
568
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
654 if (ret_port) {
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 550
diff changeset
655 *ret_port = m_strdup(serv);
160
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
656 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
657 }
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
658
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
659 #ifdef DEBUG_TRACE
198
65585699d980 * add a "label" argument to printhex()
Matt Johnston <matt@ucc.asn.au>
parents: 173
diff changeset
660 void printhex(const char * label, const unsigned char * buf, int len) {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
661
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
662 int i;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
663
198
65585699d980 * add a "label" argument to printhex()
Matt Johnston <matt@ucc.asn.au>
parents: 173
diff changeset
664 fprintf(stderr, "%s\n", label);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
665 for (i = 0; i < len; i++) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
666 fprintf(stderr, "%02x", buf[i]);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
667 if (i % 16 == 15) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
668 fprintf(stderr, "\n");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
669 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
670 else if (i % 2 == 1) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
671 fprintf(stderr, " ");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
672 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
673 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
674 fprintf(stderr, "\n");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
675 }
764
2202e854d187 add printmpint() for debugging
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
676
2202e854d187 add printmpint() for debugging
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
677 void printmpint(const char *label, mp_int *mp) {
2202e854d187 add printmpint() for debugging
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
678 buffer *buf = buf_new(1000);
2202e854d187 add printmpint() for debugging
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
679 buf_putmpint(buf, mp);
2202e854d187 add printmpint() for debugging
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
680 printhex(label, buf->data, buf->len);
2202e854d187 add printmpint() for debugging
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
681 buf_free(buf);
2202e854d187 add printmpint() for debugging
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
682
2202e854d187 add printmpint() for debugging
Matt Johnston <matt@ucc.asn.au>
parents: 667
diff changeset
683 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
684 #endif
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
685
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
686 /* Strip all control characters from text (a null-terminated string), except
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
687 * for '\n', '\r' and '\t'.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
688 * The result returned is a newly allocated string, this must be free()d after
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
689 * use */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
690 char * stripcontrol(const char * text) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
691
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
692 char * ret;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
693 int len, pos;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
694 int i;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
695
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
696 len = strlen(text);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
697 ret = m_malloc(len+1);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
698
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
699 pos = 0;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
700 for (i = 0; i < len; i++) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
701 if ((text[i] <= '~' && text[i] >= ' ') /* normal printable range */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
702 || text[i] == '\n' || text[i] == '\r' || text[i] == '\t') {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
703 ret[pos] = text[i];
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
704 pos++;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
705 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
706 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
707 ret[pos] = 0x0;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
708 return ret;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
709 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
710
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
711
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
712 /* reads the contents of filename into the buffer buf, from the current
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
713 * position, either to the end of the file, or the buffer being full.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
714 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
715 int buf_readfile(buffer* buf, const char* filename) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
716
357
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
717 int fd = -1;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
718 int len;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
719 int maxlen;
358
e81d3bc1dc78 Forgot variable declaration.
Matt Johnston <matt@ucc.asn.au>
parents: 357
diff changeset
720 int ret = DROPBEAR_FAILURE;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
721
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
722 fd = open(filename, O_RDONLY);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
723
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
724 if (fd < 0) {
357
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
725 goto out;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
726 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
727
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
728 do {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
729 maxlen = buf->size - buf->pos;
357
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
730 len = read(fd, buf_getwriteptr(buf, maxlen), maxlen);
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
731 if (len < 0) {
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
732 if (errno == EINTR || errno == EAGAIN) {
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
733 continue;
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
734 }
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
735 goto out;
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
736 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
737 buf_incrwritepos(buf, len);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
738 } while (len < maxlen && len > 0);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
739
357
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
740 ret = DROPBEAR_SUCCESS;
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
741
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
742 out:
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
743 if (fd >= 0) {
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
744 m_close(fd);
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
745 }
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
746 return ret;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
747 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
748
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
749 /* get a line from the file into buffer in the style expected for an
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
750 * authkeys file.
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
751 * Will return DROPBEAR_SUCCESS if data is read, or DROPBEAR_FAILURE on EOF.*/
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
752 /* Only used for ~/.ssh/known_hosts and ~/.ssh/authorized_keys */
68
eee77ac31ccc cleaning up the pubkey defines
Matt Johnston <matt@ucc.asn.au>
parents: 64
diff changeset
753 #if defined(DROPBEAR_CLIENT) || defined(ENABLE_SVR_PUBKEY_AUTH)
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
754 int buf_getline(buffer * line, FILE * authfile) {
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
755
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
756 int c = EOF;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
757
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
758 buf_setpos(line, 0);
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
759 buf_setlen(line, 0);
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
760
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
761 while (line->pos < line->size) {
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
762
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
763 c = fgetc(authfile); /*getc() is weird with some uClibc systems*/
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
764 if (c == EOF || c == '\n' || c == '\r') {
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
765 goto out;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
766 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
767
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
768 buf_putbyte(line, (unsigned char)c);
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
769 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
770
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 161
diff changeset
771 TRACE(("leave getauthline: line too long"))
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
772 /* We return success, but the line length will be zeroed - ie we just
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
773 * ignore that line */
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
774 buf_setlen(line, 0);
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
775
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
776 out:
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
777
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
778
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
779 /* if we didn't read anything before EOF or error, exit */
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
780 if (c == EOF && line->pos == 0) {
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
781 return DROPBEAR_FAILURE;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
782 } else {
117
e0acad552a92 Read the last line of a file without a finishing '\n' correctly
Matt Johnston <matt@ucc.asn.au>
parents: 109
diff changeset
783 buf_setpos(line, 0);
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
784 return DROPBEAR_SUCCESS;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
785 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
786
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
787 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
788 #endif
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
789
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
790 /* make sure that the socket closes */
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
791 void m_close(int fd) {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
792
883
ff597bf2cfb0 DROPBEAR_CLI_AUTH_IMMEDIATE fixed, now enabled by default
Matt Johnston <matt@ucc.asn.au>
parents: 871
diff changeset
793 if (fd == -1) {
ff597bf2cfb0 DROPBEAR_CLI_AUTH_IMMEDIATE fixed, now enabled by default
Matt Johnston <matt@ucc.asn.au>
parents: 871
diff changeset
794 return;
ff597bf2cfb0 DROPBEAR_CLI_AUTH_IMMEDIATE fixed, now enabled by default
Matt Johnston <matt@ucc.asn.au>
parents: 871
diff changeset
795 }
ff597bf2cfb0 DROPBEAR_CLI_AUTH_IMMEDIATE fixed, now enabled by default
Matt Johnston <matt@ucc.asn.au>
parents: 871
diff changeset
796
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
797 int val;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
798 do {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
799 val = close(fd);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
800 } while (val < 0 && errno == EINTR);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
801
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
802 if (val < 0 && errno != EBADF) {
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
803 /* Linux says EIO can happen */
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
804 dropbear_exit("Error closing fd %d, %s", fd, strerror(errno));
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
805 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
806 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
807
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
808 void * m_malloc(size_t size) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
809
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
810 void* ret;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
811
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
812 if (size == 0) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
813 dropbear_exit("m_malloc failed");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
814 }
123
a0db9a23f6d4 calloc memory rather than mallocing it - can't hurt too much, and is
Matt Johnston <matt@ucc.asn.au>
parents: 117
diff changeset
815 ret = calloc(1, size);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
816 if (ret == NULL) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
817 dropbear_exit("m_malloc failed");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
818 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
819 return ret;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
820
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
821 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
822
11
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
823 void * m_strdup(const char * str) {
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
824 char* ret;
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
825
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
826 ret = strdup(str);
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
827 if (ret == NULL) {
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
828 dropbear_exit("m_strdup failed");
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
829 }
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
830 return ret;
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
831 }
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
832
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
833 void * m_realloc(void* ptr, size_t size) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
834
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
835 void *ret;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
836
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
837 if (size == 0) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
838 dropbear_exit("m_realloc failed");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
839 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
840 ret = realloc(ptr, size);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
841 if (ret == NULL) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
842 dropbear_exit("m_realloc failed");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
843 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
844 return ret;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
845 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
846
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
847 /* Clear the data, based on the method in David Wheeler's
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
848 * "Secure Programming for Linux and Unix HOWTO" */
161
b9d3f725e00b 0.44 release changes
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
849 /* Beware of calling this from within dbutil.c - things might get
b9d3f725e00b 0.44 release changes
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
850 * optimised away */
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
851 void m_burn(void *data, unsigned int len) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
852 volatile char *p = data;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
853
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
854 if (data == NULL)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
855 return;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
856 while (len--) {
655
76e780c74a5e - Burn buffers to 0x00 instead
Matt Johnston <matt@ucc.asn.au>
parents: 639
diff changeset
857 *p++ = 0x0;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
858 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
859 }
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
860
109
2e9d1f29c50f merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
861
2e9d1f29c50f merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
862 void setnonblocking(int fd) {
2e9d1f29c50f merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
863
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 161
diff changeset
864 TRACE(("setnonblocking: %d", fd))
109
2e9d1f29c50f merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
865
2e9d1f29c50f merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
866 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
173
257f09a63dab * add SSH_ASKPASS support (based on patch from Paul Whittaker
Matt Johnston <matt@ucc.asn.au>
parents: 172
diff changeset
867 if (errno == ENODEV) {
257f09a63dab * add SSH_ASKPASS support (based on patch from Paul Whittaker
Matt Johnston <matt@ucc.asn.au>
parents: 172
diff changeset
868 /* Some devices (like /dev/null redirected in)
257f09a63dab * add SSH_ASKPASS support (based on patch from Paul Whittaker
Matt Johnston <matt@ucc.asn.au>
parents: 172
diff changeset
869 * can't be set to non-blocking */
257f09a63dab * add SSH_ASKPASS support (based on patch from Paul Whittaker
Matt Johnston <matt@ucc.asn.au>
parents: 172
diff changeset
870 TRACE(("ignoring ENODEV for setnonblocking"))
257f09a63dab * add SSH_ASKPASS support (based on patch from Paul Whittaker
Matt Johnston <matt@ucc.asn.au>
parents: 172
diff changeset
871 } else {
257f09a63dab * add SSH_ASKPASS support (based on patch from Paul Whittaker
Matt Johnston <matt@ucc.asn.au>
parents: 172
diff changeset
872 dropbear_exit("Couldn't set nonblocking");
257f09a63dab * add SSH_ASKPASS support (based on patch from Paul Whittaker
Matt Johnston <matt@ucc.asn.au>
parents: 172
diff changeset
873 }
109
2e9d1f29c50f merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
874 }
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 161
diff changeset
875 TRACE(("leave setnonblocking"))
109
2e9d1f29c50f merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
876 }
425
1afa503e33f5 Disable core dumps
Matt Johnston <matt@ucc.asn.au>
parents: 410
diff changeset
877
1afa503e33f5 Disable core dumps
Matt Johnston <matt@ucc.asn.au>
parents: 410
diff changeset
878 void disallow_core() {
1afa503e33f5 Disable core dumps
Matt Johnston <matt@ucc.asn.au>
parents: 410
diff changeset
879 struct rlimit lim;
1afa503e33f5 Disable core dumps
Matt Johnston <matt@ucc.asn.au>
parents: 410
diff changeset
880 lim.rlim_cur = lim.rlim_max = 0;
1afa503e33f5 Disable core dumps
Matt Johnston <matt@ucc.asn.au>
parents: 410
diff changeset
881 setrlimit(RLIMIT_CORE, &lim);
1afa503e33f5 Disable core dumps
Matt Johnston <matt@ucc.asn.au>
parents: 410
diff changeset
882 }
492
b956d6151600 Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
883
b956d6151600 Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
884 /* Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE, with the result in *val */
b956d6151600 Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
885 int m_str_to_uint(const char* str, unsigned int *val) {
864
30ab30e46452 Fix some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 835
diff changeset
886 unsigned long l;
492
b956d6151600 Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
887 errno = 0;
864
30ab30e46452 Fix some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 835
diff changeset
888 l = strtoul(str, NULL, 10);
492
b956d6151600 Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
889 /* The c99 spec doesn't actually seem to define EINVAL, but most platforms
b956d6151600 Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
890 * I've looked at mention it in their manpage */
864
30ab30e46452 Fix some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 835
diff changeset
891 if ((l == 0 && errno == EINVAL)
30ab30e46452 Fix some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 835
diff changeset
892 || (l == ULONG_MAX && errno == ERANGE)
30ab30e46452 Fix some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 835
diff changeset
893 || (l > UINT_MAX)) {
492
b956d6151600 Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
894 return DROPBEAR_FAILURE;
b956d6151600 Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
895 } else {
864
30ab30e46452 Fix some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 835
diff changeset
896 *val = l;
492
b956d6151600 Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
897 return DROPBEAR_SUCCESS;
b956d6151600 Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
898 }
b956d6151600 Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
899 }
817
a625f9e135a4 Constant time memcmp for the hmac and password crypt
Matt Johnston <matt@ucc.asn.au>
parents: 753
diff changeset
900
995
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
901 /* Returns malloced path. Only expands ~ in first character */
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
902 char * expand_tilde(const char *inpath) {
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
903 struct passwd *pw = NULL;
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
904 if (inpath[0] == '~') {
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
905 pw = getpwuid(getuid());
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
906 if (pw && pw->pw_dir) {
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
907 int len = strlen(inpath) + strlen(pw->pw_dir) + 1;
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
908 char *buf = m_malloc(len);
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
909 snprintf(buf, len, "%s/%s", pw->pw_dir, &inpath[1]);
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
910 return buf;
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
911 }
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
912 }
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
913
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
914 /* Fallback */
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
915 return m_strdup(inpath);
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
916 }
6fb4c010c448 Default client key path ~/.ssh/id_dropbear
Matt Johnston <matt@ucc.asn.au>
parents: 962
diff changeset
917
817
a625f9e135a4 Constant time memcmp for the hmac and password crypt
Matt Johnston <matt@ucc.asn.au>
parents: 753
diff changeset
918 int constant_time_memcmp(const void* a, const void *b, size_t n)
a625f9e135a4 Constant time memcmp for the hmac and password crypt
Matt Johnston <matt@ucc.asn.au>
parents: 753
diff changeset
919 {
a625f9e135a4 Constant time memcmp for the hmac and password crypt
Matt Johnston <matt@ucc.asn.au>
parents: 753
diff changeset
920 const char *xa = a, *xb = b;
a625f9e135a4 Constant time memcmp for the hmac and password crypt
Matt Johnston <matt@ucc.asn.au>
parents: 753
diff changeset
921 uint8_t c = 0;
a625f9e135a4 Constant time memcmp for the hmac and password crypt
Matt Johnston <matt@ucc.asn.au>
parents: 753
diff changeset
922 size_t i;
a625f9e135a4 Constant time memcmp for the hmac and password crypt
Matt Johnston <matt@ucc.asn.au>
parents: 753
diff changeset
923 for (i = 0; i < n; i++)
a625f9e135a4 Constant time memcmp for the hmac and password crypt
Matt Johnston <matt@ucc.asn.au>
parents: 753
diff changeset
924 {
a625f9e135a4 Constant time memcmp for the hmac and password crypt
Matt Johnston <matt@ucc.asn.au>
parents: 753
diff changeset
925 c |= (xa[i] ^ xb[i]);
a625f9e135a4 Constant time memcmp for the hmac and password crypt
Matt Johnston <matt@ucc.asn.au>
parents: 753
diff changeset
926 }
a625f9e135a4 Constant time memcmp for the hmac and password crypt
Matt Johnston <matt@ucc.asn.au>
parents: 753
diff changeset
927 return c;
a625f9e135a4 Constant time memcmp for the hmac and password crypt
Matt Johnston <matt@ucc.asn.au>
parents: 753
diff changeset
928 }
a625f9e135a4 Constant time memcmp for the hmac and password crypt
Matt Johnston <matt@ucc.asn.au>
parents: 753
diff changeset
929
928
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
930 #if defined(__linux__) && defined(SYS_clock_gettime)
952
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
931 /* CLOCK_MONOTONIC_COARSE was added in Linux 2.6.32 but took a while to
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
932 reach userspace include headers */
928
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
933 #ifndef CLOCK_MONOTONIC_COARSE
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
934 #define CLOCK_MONOTONIC_COARSE 6
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
935 #endif
952
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
936 static clockid_t get_linux_clock_source() {
928
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
937 struct timespec ts;
952
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
938 if (syscall(SYS_clock_gettime, CLOCK_MONOTONIC_COARSE, &ts) == 0) {
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
939 return CLOCK_MONOTONIC_COARSE;
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
940 }
953
356a25a108a3 Fix some format string warnings
Matt Johnston <matt@ucc.asn.au>
parents: 952
diff changeset
941
952
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
942 if (syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &ts) == 0) {
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
943 return CLOCK_MONOTONIC;
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
944 }
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
945 return -1;
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
946 }
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
947 #endif
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
948
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
949 time_t monotonic_now() {
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
950 #if defined(__linux__) && defined(SYS_clock_gettime)
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
951 static clockid_t clock_source = -2;
928
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
952
952
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
953 if (clock_source == -2) {
953
356a25a108a3 Fix some format string warnings
Matt Johnston <matt@ucc.asn.au>
parents: 952
diff changeset
954 /* First run, find out which one works.
952
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
955 -1 will fall back to time() */
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
956 clock_source = get_linux_clock_source();
928
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
957 }
952
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
958
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
959 if (clock_source >= 0) {
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
960 struct timespec ts;
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
961 if (syscall(SYS_clock_gettime, clock_source, &ts) != 0) {
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
962 /* Intermittent clock failures should not happen */
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
963 dropbear_exit("Clock broke");
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
964 }
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
965 return ts.tv_sec;
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
966 }
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
967 #endif /* linux clock_gettime */
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
968
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
969 #if defined(HAVE_MACH_ABSOLUTE_TIME)
928
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
970 /* OS X, see https://developer.apple.com/library/mac/qa/qa1398/_index.html */
930
8f04e36622c0 Fix monotonic_now() on OS X
Matt Johnston <matt@ucc.asn.au>
parents: 928
diff changeset
971 static mach_timebase_info_data_t timebase_info;
928
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
972 if (timebase_info.denom == 0) {
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
973 mach_timebase_info(&timebase_info);
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
974 }
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
975 return mach_absolute_time() * timebase_info.numer / timebase_info.denom
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
976 / 1e9;
952
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
977 #endif /* osx mach_absolute_time */
ef8d939de3dd Fix clock_gettime handling
Matt Johnston <matt@ucc.asn.au>
parents: 936
diff changeset
978
928
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
979 /* Fallback for everything else - this will sometimes go backwards */
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
980 return time(NULL);
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
981 }
7cd89d4e0335 Add new monotonic_now() wrapper so that timeouts are unaffected by
Matt Johnston <matt@ucc.asn.au>
parents: 883
diff changeset
982
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
983
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
984 struct dropbear_progress_connection
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
985 {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
986 struct addrinfo *res;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
987 struct addrinfo *res_iter;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
988
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
989 char *remotehost, *remoteport; /* For error reporting */
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
990
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
991 connect_callback cb;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
992 void *cb_data;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
993
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
994 struct Queue *writequeue; /* A queue of encrypted packets to send with TCP fastopen,
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
995 or NULL. */
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
996
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
997 int sock;
1025
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
998
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
999 char* errstring;
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1000 };
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1001
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1002 /* Deallocate a progress connection. Removes from the pending list if iter!=NULL.
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1003 Does not close sockets */
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1004 static void remove_connect(struct dropbear_progress_connection *c, m_list_elem *iter) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1005 if (c->res) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1006 freeaddrinfo(c->res);
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1007 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1008 m_free(c->remotehost);
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1009 m_free(c->remoteport);
1025
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1010 m_free(c->errstring);
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1011 m_free(c);
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1012
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1013 if (iter) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1014 list_remove(iter);
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1015 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1016 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1017
1025
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1018 static void cancel_callback(int result, int sock, void* UNUSED(data), const char* UNUSED(errstring)) {
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1019 if (result == DROPBEAR_SUCCESS)
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1020 {
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1021 m_close(sock);
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1022 }
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1023 }
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1024
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1025 void cancel_connect(struct dropbear_progress_connection *c) {
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1026 c->cb = cancel_callback;
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1027 c->cb_data = NULL;
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1028 }
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1029
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1030 static void connect_try_next(struct dropbear_progress_connection *c) {
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1031 int err = EADDRNOTAVAIL;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1032 struct addrinfo *r;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1033
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1034 if (!c->res_iter) {
1025
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1035 return;
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1036 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1037
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1038 for (r = c->res_iter; r; r = r->ai_next)
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1039 {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1040 assert(c->sock == -1);
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1041
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1042 c->sock = socket(c->res_iter->ai_family, c->res_iter->ai_socktype, c->res_iter->ai_protocol);
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1043 if (c->sock < 0) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1044 err = errno;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1045 continue;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1046 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1047
1025
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1048 ses.maxfd = MAX(ses.maxfd, c->sock);
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1049 setnonblocking(c->sock);
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1050
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1051 #if defined(__linux__) && defined(TCP_DEFER_ACCEPT)
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1052 set_piggyback_ack(sock);
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1053 #endif
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1054
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1055 if (connect(c->sock, r->ai_addr, r->ai_addrlen) < 0) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1056 if (errno == EINPROGRESS) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1057 TRACE(("Connect in progress"))
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1058 break;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1059 } else {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1060 err = errno;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1061 close(c->sock);
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1062 c->sock = -1;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1063 continue;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1064 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1065 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1066
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1067 break; /* Success. Treated the same as EINPROGRESS */
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1068 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1069
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1070 if (r) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1071 c->res_iter = r->ai_next;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1072 } else {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1073 c->res_iter = NULL;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1074 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1075
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1076 if (c->sock >= 0 || (errno == EINPROGRESS)) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1077 /* Success */
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1078 set_sock_nodelay(c->sock);
1025
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1079 return;
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1080 } else {
1025
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1081 if (!c->res_iter)
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1082 {
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1083
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1084 }
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1085 /* XXX - returning error message through */
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1086 #if 0
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1087 /* Failed */
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1088 if (errstring != NULL && *errstring == NULL) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1089 int len;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1090 len = 20 + strlen(strerror(err));
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1091 *errstring = (char*)m_malloc(len);
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1092 snprintf(*errstring, len, "Error connecting: %s", strerror(err));
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1093 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1094 TRACE(("Error connecting: %s", strerror(err)))
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1095 #endif
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1096 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1097 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1098
1025
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1099 /* Connect via TCP to a host. */
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1100 struct dropbear_progress_connection *connect_remote(const char* remotehost, const char* remoteport,
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1101 connect_callback cb, void* cb_data)
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1102 {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1103 struct dropbear_progress_connection *c = NULL;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1104 int err;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1105 struct addrinfo hints;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1106
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1107 c = m_malloc(sizeof(*c));
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1108 c->remotehost = m_strdup(remotehost);
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1109 c->remoteport = m_strdup(remoteport);
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1110 c->sock = -1;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1111 c->cb = cb;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1112 c->cb_data = cb_data;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1113
1025
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1114 list_append(&ses.conn_pending, c);
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1115
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1116 memset(&hints, 0, sizeof(hints));
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1117 hints.ai_socktype = SOCK_STREAM;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1118 hints.ai_family = PF_UNSPEC;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1119
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1120 err = getaddrinfo(remotehost, remoteport, &hints, &c->res);
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1121 if (err) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1122 int len;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1123 len = 100 + strlen(gai_strerror(err));
1025
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1124 c->errstring = (char*)m_malloc(len);
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1125 snprintf(c->errstring, len, "Error resolving '%s' port '%s'. %s",
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1126 remotehost, remoteport, gai_strerror(err));
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1127 TRACE(("Error resolving: %s", gai_strerror(err)))
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1128 return NULL;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1129 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1130
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1131 c->res_iter = c->res;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1132
1025
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1133 /* Set one going */
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1134 connect_try_next(c);
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1135
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1136 return c;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1137 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1138
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1139
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1140 void set_connect_fds(fd_set *writefd) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1141 m_list_elem *iter;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1142 TRACE(("enter handle_connect_fds"))
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1143 for (iter = ses.conn_pending.first; iter; iter = iter->next) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1144 struct dropbear_progress_connection *c = iter->item;
1025
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1145 /* Set one going */
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1146 while (c->res_iter && c->sock < 0)
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1147 {
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1148 connect_try_next(c);
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1149 }
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1150 if (c->sock >= 0) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1151 FD_SET(c->sock, writefd);
1025
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1152 } else {
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1153 m_list_elem *remove_iter;
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1154 /* Final failure */
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1155 if (!c->errstring) {
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1156 c->errstring = m_strdup("unexpected failure");
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1157 }
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1158 c->cb(DROPBEAR_FAILURE, -1, c->cb_data, c->errstring);
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1159 /* Safely remove without invalidating iter */
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1160 remove_iter = iter;
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1161 iter = iter->prev;
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1162 remove_connect(c, remove_iter);
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1163 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1164 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1165 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1166
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1167 void handle_connect_fds(fd_set *writefd) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1168 m_list_elem *iter;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1169 TRACE(("enter handle_connect_fds"))
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1170 for (iter = ses.conn_pending.first; iter; iter = iter->next) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1171 int val;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1172 socklen_t vallen = sizeof(val);
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1173 struct dropbear_progress_connection *c = iter->item;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1174
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1175 if (!FD_ISSET(c->sock, writefd)) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1176 continue;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1177 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1178
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1179 TRACE(("handling %s port %s socket %d", c->remotehost, c->remoteport, c->sock));
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1180
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1181 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &val, &vallen) != 0) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1182 TRACE(("handle_connect_fds getsockopt(%d) SO_ERROR failed: %s", c->sock, strerror(errno)))
1025
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1183 /* This isn't expected to happen - Unix has surprises though, continue gracefully. */
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1184 m_close(c->sock);
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1185 c->sock = -1;
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1186 } else if (val != 0) {
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1187 /* Connect failed */
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1188 TRACE(("connect to %s port %s failed.", c->remotehost, c->remoteport))
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1189 m_close(c->sock);
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1190 c->sock = -1;
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1191
1025
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1192 m_free(c->errstring);
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1193 c->errstring = strerror(val);
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1194 } else {
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1195 /* New connection has been established */
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1196 c->cb(DROPBEAR_SUCCESS, c->sock, c->cb_data, NULL);
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1197 remove_connect(c, iter);
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1198 TRACE(("leave handle_connect_fds - success"))
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1199 /* Must return here - remove_connect() invalidates iter */
02baa0b334e8 async connections working
Matt Johnston <matt@ucc.asn.au>
parents: 1024
diff changeset
1200 return;
1024
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1201 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1202 }
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1203 TRACE(("leave handle_connect_fds - end iter"))
aac0095dc3b4 work in progress for async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1023
diff changeset
1204 }