Mercurial > dropbear
annotate dbutil.c @ 735:73b6e5d8801b
Fix segfault when /dev/urandom isn't writable
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 02 Apr 2013 18:53:18 +0800 |
parents | 9a5438271556 |
children | d63ef1e211ea |
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 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
51 #include "includes.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
52 #include "dbutil.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
53 #include "buffer.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
54 #include "session.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
55 #include "atomicio.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
56 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
57 #define MAX_FMT 100 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
58 |
73
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
59 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
|
60 va_list param) ATTRIB_NORETURN; |
73
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
61 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
|
62 va_list param); |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
63 |
614
00eca37e47e8
Add noreturn and format attribute hints for some functions.
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
64 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
|
65 = generic_dropbear_exit; |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
66 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
|
67 = generic_dropbear_log; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
68 |
94
c85c88500ea6
DEBUG_TRACE now only triggers with -v on the cmdline
Matt Johnston <matt@ucc.asn.au>
parents:
73
diff
changeset
|
69 #ifdef DEBUG_TRACE |
c85c88500ea6
DEBUG_TRACE now only triggers with -v on the cmdline
Matt Johnston <matt@ucc.asn.au>
parents:
73
diff
changeset
|
70 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
|
71 #endif |
c85c88500ea6
DEBUG_TRACE now only triggers with -v on the cmdline
Matt Johnston <matt@ucc.asn.au>
parents:
73
diff
changeset
|
72 |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
73 #ifndef DISABLE_SYSLOG |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
74 void startsyslog() { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
75 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
76 openlog(PROGNAME, LOG_PID, LOG_AUTHPRIV); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
77 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
78 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
79 #endif /* DISABLE_SYSLOG */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
80 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
81 /* the "format" string must be <= 100 characters */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
82 void dropbear_close(const char* format, ...) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
83 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
84 va_list param; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
85 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
86 va_start(param, format); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
87 _dropbear_exit(EXIT_SUCCESS, format, param); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
88 va_end(param); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
89 |
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 void dropbear_exit(const char* format, ...) { |
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 va_list param; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
95 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
96 va_start(param, format); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
97 _dropbear_exit(EXIT_FAILURE, format, param); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
98 va_end(param); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
99 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
100 |
73
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
101 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
|
102 va_list param) { |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
103 |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
104 char fmtbuf[300]; |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
105 |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
106 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
|
107 |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
108 _dropbear_log(LOG_INFO, fmtbuf, param); |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
109 |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
110 exit(exitcode); |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
111 } |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
112 |
241
c5d3ef11155f
* use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
113 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
|
114 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
|
115 } |
c5d3ef11155f
* use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
116 |
108
10f4d3319780
- added circular buffering for channels
Matt Johnston <matt@ucc.asn.au>
parents:
107
diff
changeset
|
117 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
|
118 va_list param) { |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
119 |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
120 char printbuf[1024]; |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
121 |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
122 vsnprintf(printbuf, sizeof(printbuf), format, param); |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
123 |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
124 fprintf(stderr, "%s\n", printbuf); |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
125 |
0bf5cebe622c
Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
126 } |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
127 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
128 /* 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
|
129 void dropbear_log(int priority, const char* format, ...) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
130 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
131 va_list param; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
132 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
133 va_start(param, format); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
134 _dropbear_log(priority, format, param); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
135 va_end(param); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
136 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
137 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
138 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
139 #ifdef DEBUG_TRACE |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
140 void dropbear_trace(const char* format, ...) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
141 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
142 va_list param; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
143 |
94
c85c88500ea6
DEBUG_TRACE now only triggers with -v on the cmdline
Matt Johnston <matt@ucc.asn.au>
parents:
73
diff
changeset
|
144 if (!debug_trace) { |
c85c88500ea6
DEBUG_TRACE now only triggers with -v on the cmdline
Matt Johnston <matt@ucc.asn.au>
parents:
73
diff
changeset
|
145 return; |
c85c88500ea6
DEBUG_TRACE now only triggers with -v on the cmdline
Matt Johnston <matt@ucc.asn.au>
parents:
73
diff
changeset
|
146 } |
c85c88500ea6
DEBUG_TRACE now only triggers with -v on the cmdline
Matt Johnston <matt@ucc.asn.au>
parents:
73
diff
changeset
|
147 |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
148 va_start(param, format); |
485
12d845ab7b5f
Rework netcat-alike to be a proper mode, with -B argument.
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
149 fprintf(stderr, "TRACE (%d): ", getpid()); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
150 vfprintf(stderr, format, param); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
151 fprintf(stderr, "\n"); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
152 va_end(param); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
153 } |
731
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
154 void dropbear_trace2(const char* format, ...) { |
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
155 |
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
156 va_list param; |
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
157 |
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
158 if (!(debug_trace && getenv("DROPBEAR_TRACE2"))) { |
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
159 return; |
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
160 } |
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
161 |
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
162 va_start(param, format); |
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
163 fprintf(stderr, "TRACE2 (%d): ", getpid()); |
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
164 vfprintf(stderr, format, param); |
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
165 fprintf(stderr, "\n"); |
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
166 va_end(param); |
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
167 } |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
168 #endif /* DEBUG_TRACE */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
169 |
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
|
170 static void set_sock_priority(int sock) { |
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
|
171 |
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
|
172 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
|
173 |
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
|
174 /* 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
|
175 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
|
176 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&val, sizeof(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
|
177 |
629
bdadc7f4b97d
Set IPTOS_LOWDELAY for IPv6 too
Matt Johnston <matt@ucc.asn.au>
parents:
627
diff
changeset
|
178 /* 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
|
179 #ifdef IPTOS_LOWDELAY |
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
|
180 val = IPTOS_LOWDELAY; |
639
452bcf810e44
Put better #if guards around IPv6 socket options for IPV6_TCLASS and
Matt Johnston <matt@ucc.asn.au>
parents:
629
diff
changeset
|
181 #if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS) |
629
bdadc7f4b97d
Set IPTOS_LOWDELAY for IPv6 too
Matt Johnston <matt@ucc.asn.au>
parents:
627
diff
changeset
|
182 setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, (void*)&val, sizeof(val)); |
bdadc7f4b97d
Set IPTOS_LOWDELAY for IPv6 too
Matt Johnston <matt@ucc.asn.au>
parents:
627
diff
changeset
|
183 #endif |
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
|
184 setsockopt(sock, IPPROTO_IP, IP_TOS, (void*)&val, sizeof(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
|
185 #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
|
186 |
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
|
187 #ifdef SO_PRIORITY |
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
|
188 /* linux specific, sets QoS class. |
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
|
189 * 6 looks to be optimal for interactive traffic (see tc-prio(8) ). */ |
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
|
190 val = 6; |
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
|
191 setsockopt(sock, SOL_SOCKET, SO_PRIORITY, (void*) &val, sizeof(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
|
192 #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
|
193 |
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
|
194 } |
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
|
195 |
258
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
251
diff
changeset
|
196 /* 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
|
197 * 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
|
198 * 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
|
199 * Returns the number of sockets bound on success, or -1 on failure. On |
62 | 200 * failure, if errstring wasn't NULL, it'll be a newly malloced error |
201 * string.*/ | |
202 int dropbear_listen(const char* address, const char* port, | |
203 int *socks, unsigned int sockcount, char **errstring, int *maxfd) { | |
204 | |
70
b0316ce64e4b
Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents:
68
diff
changeset
|
205 struct addrinfo hints, *res = NULL, *res0 = NULL; |
62 | 206 int err; |
207 unsigned int nsock; | |
208 struct linger linger; | |
209 int val; | |
210 int sock; | |
211 | |
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
|
212 TRACE(("enter dropbear_listen")) |
62 | 213 |
214 memset(&hints, 0, sizeof(hints)); | |
215 hints.ai_family = AF_UNSPEC; /* TODO: let them flag v4 only etc */ | |
216 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
|
217 |
433
c216212001fc
Fix for -pedantic -ansi compilation, change // to /**/, plus some signedness
Matt Johnston <matt@ucc.asn.au>
parents:
425
diff
changeset
|
218 /* for calling getaddrinfo: |
c216212001fc
Fix for -pedantic -ansi compilation, change // to /**/, plus some signedness
Matt Johnston <matt@ucc.asn.au>
parents:
425
diff
changeset
|
219 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
|
220 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
|
221 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
|
222 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
|
223 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
|
224 } else { |
258
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
251
diff
changeset
|
225 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
|
226 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
|
227 address = NULL; |
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
251
diff
changeset
|
228 } |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
229 hints.ai_flags = AI_PASSIVE; |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
230 } |
62 | 231 err = getaddrinfo(address, port, &hints, &res0); |
232 | |
233 if (err) { | |
234 if (errstring != NULL && *errstring == NULL) { | |
235 int len; | |
236 len = 20 + strlen(gai_strerror(err)); | |
237 *errstring = (char*)m_malloc(len); | |
238 snprintf(*errstring, len, "Error resolving: %s", gai_strerror(err)); | |
239 } | |
239
e5ad9fa8b1fa
add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
240 if (res0) { |
e5ad9fa8b1fa
add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
241 freeaddrinfo(res0); |
e5ad9fa8b1fa
add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
242 res0 = NULL; |
e5ad9fa8b1fa
add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
243 } |
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
|
244 TRACE(("leave dropbear_listen: failed resolving")) |
62 | 245 return -1; |
246 } | |
247 | |
248 | |
249 nsock = 0; | |
250 for (res = res0; res != NULL && nsock < sockcount; | |
251 res = res->ai_next) { | |
252 | |
253 /* Get a socket */ | |
254 socks[nsock] = socket(res->ai_family, res->ai_socktype, | |
255 res->ai_protocol); | |
256 | |
257 sock = socks[nsock]; /* For clarity */ | |
258 | |
259 if (sock < 0) { | |
260 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
|
261 TRACE(("socket() failed")) |
62 | 262 continue; |
263 } | |
264 | |
265 /* Various useful socket options */ | |
266 val = 1; | |
267 /* set to reuse, quick timeout */ | |
268 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &val, sizeof(val)); | |
269 linger.l_onoff = 1; | |
270 linger.l_linger = 5; | |
271 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void*)&linger, sizeof(linger)); | |
272 | |
639
452bcf810e44
Put better #if guards around IPv6 socket options for IPV6_TCLASS and
Matt Johnston <matt@ucc.asn.au>
parents:
629
diff
changeset
|
273 #if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY) |
627 | 274 if (res->ai_family == AF_INET6) { |
275 int on = 1; | |
276 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, | |
277 &on, sizeof(on)) == -1) { | |
278 dropbear_log(LOG_WARNING, "Couldn't set IPV6_V6ONLY"); | |
279 } | |
280 } | |
281 #endif | |
282 | |
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
|
283 set_sock_priority(sock); |
62 | 284 |
285 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) { | |
286 err = errno; | |
287 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
|
288 TRACE(("bind(%s) failed", port)) |
62 | 289 continue; |
290 } | |
291 | |
292 if (listen(sock, 20) < 0) { | |
293 err = errno; | |
294 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
|
295 TRACE(("listen() failed")) |
62 | 296 continue; |
297 } | |
298 | |
299 *maxfd = MAX(*maxfd, sock); | |
300 | |
301 nsock++; | |
302 } | |
303 | |
239
e5ad9fa8b1fa
add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
304 if (res0) { |
e5ad9fa8b1fa
add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
305 freeaddrinfo(res0); |
e5ad9fa8b1fa
add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
306 res0 = NULL; |
e5ad9fa8b1fa
add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
307 } |
e5ad9fa8b1fa
add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
308 |
62 | 309 if (nsock == 0) { |
310 if (errstring != NULL && *errstring == NULL) { | |
311 int len; | |
312 len = 20 + strlen(strerror(err)); | |
313 *errstring = (char*)m_malloc(len); | |
64 | 314 snprintf(*errstring, len, "Error listening: %s", strerror(err)); |
62 | 315 } |
408
28b10e93685c
Fix failure-handling in dropbear_listen() when errstring is unset
Matt Johnston <matt@ucc.asn.au>
parents:
277
diff
changeset
|
316 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
|
317 return -1; |
62 | 318 } |
319 | |
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
|
320 TRACE(("leave dropbear_listen: success, %d socks bound", nsock)) |
62 | 321 return nsock; |
322 } | |
323 | |
547
cf376c696dfc
Make it compile, update for changes in channel structure.
Matt Johnston <matt@ucc.asn.au>
parents:
500
diff
changeset
|
324 /* 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
|
325 #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
|
326 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
|
327 struct sockaddr_un addr; |
225
ca7e76d981d9
- progress towards client agent forwarding
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
328 int fd = -1; |
ca7e76d981d9
- progress towards client agent forwarding
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
329 |
550
61c3513825b0
Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents:
547
diff
changeset
|
330 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
|
331 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
|
332 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
|
333 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
|
334 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
|
335 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
|
336 return -1; |
61c3513825b0
Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents:
547
diff
changeset
|
337 } |
61c3513825b0
Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents:
547
diff
changeset
|
338 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
|
339 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
|
340 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
|
341 return -1; |
61c3513825b0
Talking to the agent works now. Can't interpret the pubkeys.
Matt Johnston <matt@ucc.asn.au>
parents:
547
diff
changeset
|
342 } |
225
ca7e76d981d9
- progress towards client agent forwarding
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
343 return fd; |
ca7e76d981d9
- progress towards client agent forwarding
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
344 } |
ca7e76d981d9
- progress towards client agent forwarding
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
345 #endif |
ca7e76d981d9
- progress towards client agent forwarding
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
346 |
26 | 347 /* Connect via TCP to a host. Connection will try ipv4 or ipv6, will |
62 | 348 * return immediately if nonblocking is set. On failure, if errstring |
349 * wasn't null, it will be a newly malloced error message */ | |
350 | |
351 /* TODO: maxfd */ | |
26 | 352 int connect_remote(const char* remotehost, const char* remoteport, |
353 int nonblocking, char ** errstring) { | |
354 | |
355 struct addrinfo *res0 = NULL, *res = NULL, hints; | |
356 int sock; | |
357 int err; | |
358 | |
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
|
359 TRACE(("enter connect_remote")) |
26 | 360 |
361 if (errstring != NULL) { | |
362 *errstring = NULL; | |
363 } | |
364 | |
365 memset(&hints, 0, sizeof(hints)); | |
366 hints.ai_socktype = SOCK_STREAM; | |
367 hints.ai_family = PF_UNSPEC; | |
368 | |
369 err = getaddrinfo(remotehost, remoteport, &hints, &res0); | |
370 if (err) { | |
371 if (errstring != NULL && *errstring == NULL) { | |
372 int len; | |
490
bd2634b03b12
- Improve DNS failure message to include lookup host
Matt Johnston <matt@ucc.asn.au>
parents:
486
diff
changeset
|
373 len = 100 + strlen(gai_strerror(err)); |
26 | 374 *errstring = (char*)m_malloc(len); |
490
bd2634b03b12
- Improve DNS failure message to include lookup host
Matt Johnston <matt@ucc.asn.au>
parents:
486
diff
changeset
|
375 snprintf(*errstring, len, "Error resolving '%s' port '%s'. %s", |
bd2634b03b12
- Improve DNS failure message to include lookup host
Matt Johnston <matt@ucc.asn.au>
parents:
486
diff
changeset
|
376 remotehost, remoteport, gai_strerror(err)); |
26 | 377 } |
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
|
378 TRACE(("Error resolving: %s", gai_strerror(err))) |
26 | 379 return -1; |
380 } | |
381 | |
382 sock = -1; | |
383 err = EADDRNOTAVAIL; | |
384 for (res = res0; res; res = res->ai_next) { | |
385 | |
386 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | |
387 if (sock < 0) { | |
388 err = errno; | |
389 continue; | |
390 } | |
391 | |
392 if (nonblocking) { | |
225
ca7e76d981d9
- progress towards client agent forwarding
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
393 setnonblocking(sock); |
26 | 394 } |
395 | |
396 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) { | |
70
b0316ce64e4b
Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents:
68
diff
changeset
|
397 if (errno == EINPROGRESS && nonblocking) { |
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
|
398 TRACE(("Connect in progress")) |
26 | 399 break; |
400 } else { | |
401 err = errno; | |
402 close(sock); | |
403 sock = -1; | |
404 continue; | |
405 } | |
406 } | |
407 | |
408 break; /* Success */ | |
409 } | |
410 | |
70
b0316ce64e4b
Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents:
68
diff
changeset
|
411 if (sock < 0 && !(errno == EINPROGRESS && nonblocking)) { |
26 | 412 /* Failed */ |
413 if (errstring != NULL && *errstring == NULL) { | |
414 int len; | |
415 len = 20 + strlen(strerror(err)); | |
416 *errstring = (char*)m_malloc(len); | |
417 snprintf(*errstring, len, "Error connecting: %s", strerror(err)); | |
418 } | |
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
|
419 TRACE(("Error connecting: %s", strerror(err))) |
26 | 420 } else { |
421 /* Success */ | |
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
|
422 set_sock_priority(sock); |
26 | 423 } |
424 | |
425 freeaddrinfo(res0); | |
62 | 426 if (sock > 0 && errstring != NULL && *errstring != NULL) { |
427 m_free(*errstring); | |
428 } | |
26 | 429 |
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
|
430 TRACE(("leave connect_remote: sock %d\n", sock)) |
26 | 431 return sock; |
432 } | |
433 | |
481
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
434 /* 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
|
435 * 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
|
436 * 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
|
437 * 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
|
438 * 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
|
439 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
|
440 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
|
441 int infds[2]; |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
442 int outfds[2]; |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
443 int errfds[2]; |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
444 pid_t pid; |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
445 |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
446 const int FDIN = 0; |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
447 const int FDOUT = 1; |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
448 |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
449 /* 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
|
450 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
|
451 return DROPBEAR_FAILURE; |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
452 } |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
453 if (pipe(outfds) != 0) { |
481
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
454 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
|
455 } |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
456 if (ret_errfd && pipe(errfds) != 0) { |
481
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
457 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
|
458 } |
481
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
459 |
667
fc7ae88e63b3
Rename HAVE_FORK to USE_VFORK
Matt Johnston <matt@ucc.asn.au>
parents:
666
diff
changeset
|
460 #ifdef USE_VFORK |
481
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
461 pid = vfork(); |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
462 #else |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
463 pid = fork(); |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
464 #endif |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
465 |
484
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
466 if (pid < 0) { |
481
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
467 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
|
468 } |
481
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
469 |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
470 if (!pid) { |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
471 /* child */ |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
472 |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
473 TRACE(("back to normal sigchld")) |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
474 /* Revert to normal sigchld handling */ |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
475 if (signal(SIGCHLD, SIG_DFL) == SIG_ERR) { |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
476 dropbear_exit("signal() error"); |
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 |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
479 /* redirect stdin/stdout */ |
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 if ((dup2(infds[FDIN], STDIN_FILENO) < 0) || |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
482 (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
|
483 (ret_errfd && dup2(errfds[FDOUT], STDERR_FILENO) < 0)) { |
481
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
484 TRACE(("leave noptycommand: error redirecting FDs")) |
594
a98a2138364a
Improve capitalisation for all logged strings
Matt Johnston <matt@ucc.asn.au>
parents:
568
diff
changeset
|
485 dropbear_exit("Child dup2() failure"); |
481
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 |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
488 close(infds[FDOUT]); |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
489 close(infds[FDIN]); |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
490 close(outfds[FDIN]); |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
491 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
|
492 if (ret_errfd) |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
493 { |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
494 close(errfds[FDIN]); |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
495 close(errfds[FDOUT]); |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
496 } |
481
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
497 |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
498 exec_fn(exec_data); |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
499 /* not reached */ |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
500 return DROPBEAR_FAILURE; |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
501 } else { |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
502 /* parent */ |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
503 close(infds[FDIN]); |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
504 close(outfds[FDOUT]); |
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 setnonblocking(outfds[FDIN]); |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
507 setnonblocking(infds[FDOUT]); |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
508 |
484
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
509 if (ret_errfd) { |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
510 close(errfds[FDOUT]); |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
511 setnonblocking(errfds[FDIN]); |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
512 } |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
513 |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
514 if (ret_pid) { |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
515 *ret_pid = pid; |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
516 } |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
517 |
481
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
518 *ret_writefd = infds[FDOUT]; |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
519 *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
|
520 if (ret_errfd) { |
effb4a25b1ae
Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents:
482
diff
changeset
|
521 *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
|
522 } |
481
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
523 return DROPBEAR_SUCCESS; |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
524 } |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
525 } |
357a2e2e9bcc
- Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents:
433
diff
changeset
|
526 |
482
7ad49f34a122
- Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents:
481
diff
changeset
|
527 /* 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
|
528 * 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
|
529 */ |
7ad49f34a122
- Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents:
481
diff
changeset
|
530 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
|
531 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
|
532 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
|
533 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
|
534 |
7ad49f34a122
- Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents:
481
diff
changeset
|
535 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
|
536 |
7ad49f34a122
- Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents:
481
diff
changeset
|
537 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
|
538 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
|
539 } else { |
7ad49f34a122
- Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents:
481
diff
changeset
|
540 /* 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
|
541 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
|
542 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
|
543 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
|
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 |
7ad49f34a122
- Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents:
481
diff
changeset
|
546 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
|
547 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
|
548 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
|
549 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
|
550 } else { |
7ad49f34a122
- Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents:
481
diff
changeset
|
551 /* 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
|
552 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
|
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 |
7ad49f34a122
- Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents:
481
diff
changeset
|
555 /* 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
|
556 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
|
557 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
|
558 } |
7ad49f34a122
- Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents:
481
diff
changeset
|
559 |
7ad49f34a122
- Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents:
481
diff
changeset
|
560 /* 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
|
561 * 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
|
562 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
|
563 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
|
564 } |
7ad49f34a122
- Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents:
481
diff
changeset
|
565 |
7ad49f34a122
- Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents:
481
diff
changeset
|
566 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
|
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 |
568
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
569 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
|
570 char **remote_host, char **remote_port, int host_lookup) |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
571 { |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
572 struct sockaddr_storage addr; |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
573 socklen_t addrlen; |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
574 |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
575 if (local_host || local_port) { |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
576 addrlen = sizeof(addr); |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
577 if (getsockname(fd, (struct sockaddr*)&addr, &addrlen) < 0) { |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
578 dropbear_exit("Failed socket address: %s", strerror(errno)); |
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 getaddrstring(&addr, local_host, local_port, host_lookup); |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
581 } |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
582 if (remote_host || remote_port) { |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
583 addrlen = sizeof(addr); |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
584 if (getpeername(fd, (struct sockaddr*)&addr, &addrlen) < 0) { |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
585 dropbear_exit("Failed socket address: %s", strerror(errno)); |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
586 } |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
587 getaddrstring(&addr, remote_host, remote_port, host_lookup); |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
588 } |
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 |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
591 /* 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
|
592 * value is allocated with malloc() */ |
568
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
593 void getaddrstring(struct sockaddr_storage* addr, |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
594 char **ret_host, char **ret_port, |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
595 int host_lookup) { |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
596 |
568
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
597 char host[NI_MAXHOST+1], serv[NI_MAXSERV+1]; |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
598 unsigned int len; |
62 | 599 int ret; |
568
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
600 |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
601 int flags = NI_NUMERICSERV | NI_NUMERICHOST; |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
602 |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
603 #ifndef DO_HOST_LOOKUP |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
604 host_lookup = 0; |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
605 #endif |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
606 |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
607 if (host_lookup) { |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
608 flags = NI_NUMERICSERV; |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
609 } |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
610 |
62 | 611 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
|
612 /* 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
|
613 * 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
|
614 * 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
|
615 * 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
|
616 #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
|
617 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
|
618 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
|
619 } |
7ceceb46d655
Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents:
150
diff
changeset
|
620 #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
|
621 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
|
622 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
|
623 } |
7ceceb46d655
Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents:
150
diff
changeset
|
624 #endif |
335
e17f0333c21e
Another stab at getting ss_family happy on older glibc
Matt Johnston <matt@ucc.asn.au>
parents:
277
diff
changeset
|
625 #endif |
62 | 626 |
568
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
627 ret = getnameinfo((struct sockaddr*)addr, len, host, sizeof(host)-1, |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
628 serv, sizeof(serv)-1, flags); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
629 |
62 | 630 if (ret != 0) { |
568
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
631 if (host_lookup) { |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
632 /* 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
|
633 * 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
|
634 * to exhibit that behaviour. */ |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
635 getaddrstring(addr, ret_host, ret_port, 0); |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
636 return; |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
637 } else { |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
638 /* 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
|
639 dropbear_exit("Failed lookup: %s", gai_strerror(ret)); |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
640 } |
62 | 641 } |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
642 |
568
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
643 if (ret_host) { |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
644 *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
|
645 } |
568
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
646 if (ret_port) { |
005530560594
Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents:
550
diff
changeset
|
647 *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
|
648 } |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
649 } |
62 | 650 |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
651 #ifdef DEBUG_TRACE |
198
65585699d980
* add a "label" argument to printhex()
Matt Johnston <matt@ucc.asn.au>
parents:
173
diff
changeset
|
652 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
|
653 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
654 int i; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
655 |
198
65585699d980
* add a "label" argument to printhex()
Matt Johnston <matt@ucc.asn.au>
parents:
173
diff
changeset
|
656 fprintf(stderr, "%s\n", label); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
657 for (i = 0; i < len; i++) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
658 fprintf(stderr, "%02x", buf[i]); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
659 if (i % 16 == 15) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
660 fprintf(stderr, "\n"); |
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 else if (i % 2 == 1) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
663 fprintf(stderr, " "); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
664 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
665 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
666 fprintf(stderr, "\n"); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
667 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
668 #endif |
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 /* 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
|
671 * for '\n', '\r' and '\t'. |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
672 * 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
|
673 * use */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
674 char * stripcontrol(const char * text) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
675 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
676 char * ret; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
677 int len, pos; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
678 int i; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
679 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
680 len = strlen(text); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
681 ret = m_malloc(len+1); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
682 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
683 pos = 0; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
684 for (i = 0; i < len; i++) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
685 if ((text[i] <= '~' && text[i] >= ' ') /* normal printable range */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
686 || 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
|
687 ret[pos] = text[i]; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
688 pos++; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
689 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
690 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
691 ret[pos] = 0x0; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
692 return ret; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
693 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
694 |
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 /* 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
|
697 * 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
|
698 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
699 int buf_readfile(buffer* buf, const char* filename) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
700 |
357
9e2ad1023978
Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
701 int fd = -1; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
702 int len; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
703 int maxlen; |
358
e81d3bc1dc78
Forgot variable declaration.
Matt Johnston <matt@ucc.asn.au>
parents:
357
diff
changeset
|
704 int ret = DROPBEAR_FAILURE; |
4
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 fd = open(filename, O_RDONLY); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
707 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
708 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
|
709 goto out; |
4
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 do { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
713 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
|
714 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
|
715 if (len < 0) { |
9e2ad1023978
Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
716 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
|
717 continue; |
9e2ad1023978
Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
718 } |
9e2ad1023978
Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
719 goto out; |
9e2ad1023978
Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
720 } |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
721 buf_incrwritepos(buf, len); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
722 } while (len < maxlen && len > 0); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
723 |
357
9e2ad1023978
Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
724 ret = DROPBEAR_SUCCESS; |
9e2ad1023978
Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
725 |
9e2ad1023978
Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
726 out: |
9e2ad1023978
Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
727 if (fd >= 0) { |
9e2ad1023978
Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
728 m_close(fd); |
9e2ad1023978
Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
729 } |
9e2ad1023978
Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents:
335
diff
changeset
|
730 return ret; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
731 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
732 |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
733 /* 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
|
734 * authkeys file. |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
735 * 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
|
736 /* 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
|
737 #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
|
738 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
|
739 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
740 int c = EOF; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
741 |
731
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
742 TRACE2(("enter buf_getline")) |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
743 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
744 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
|
745 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
|
746 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
747 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
|
748 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
749 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
|
750 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
|
751 goto out; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
752 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
753 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
754 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
|
755 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
756 |
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
|
757 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
|
758 /* 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
|
759 * ignore that line */ |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
760 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
|
761 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
762 out: |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
763 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
764 |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
765 /* 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
|
766 if (c == EOF && line->pos == 0) { |
731
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
767 TRACE2(("leave buf_getline: failure")) |
51
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
768 return DROPBEAR_FAILURE; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
769 } else { |
731
9a5438271556
Move the more verbose TRACE() statements into TRACE2()
Matt Johnston <matt@ucc.asn.au>
parents:
667
diff
changeset
|
770 TRACE2(("leave buf_getline: success")) |
117
e0acad552a92
Read the last line of a file without a finishing '\n' correctly
Matt Johnston <matt@ucc.asn.au>
parents:
109
diff
changeset
|
771 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
|
772 return DROPBEAR_SUCCESS; |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
773 } |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
774 |
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 #endif |
095d689fed16
- Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
777 |
277
044bc108b9b3
* Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
778 /* make sure that the socket closes */ |
044bc108b9b3
* Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
779 void m_close(int fd) { |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
780 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
781 int val; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
782 do { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
783 val = close(fd); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
784 } while (val < 0 && errno == EINTR); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
785 |
277
044bc108b9b3
* Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
786 if (val < 0 && errno != EBADF) { |
044bc108b9b3
* Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
787 /* Linux says EIO can happen */ |
044bc108b9b3
* Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
788 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
|
789 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
790 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
791 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
792 void * m_malloc(size_t size) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
793 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
794 void* ret; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
795 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
796 if (size == 0) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
797 dropbear_exit("m_malloc failed"); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
798 } |
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
|
799 ret = calloc(1, size); |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
800 if (ret == NULL) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
801 dropbear_exit("m_malloc failed"); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
802 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
803 return ret; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
804 |
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 |
11
f76c9389e9e0
Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents:
4
diff
changeset
|
807 void * m_strdup(const char * str) { |
f76c9389e9e0
Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents:
4
diff
changeset
|
808 char* ret; |
f76c9389e9e0
Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents:
4
diff
changeset
|
809 |
f76c9389e9e0
Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents:
4
diff
changeset
|
810 ret = strdup(str); |
f76c9389e9e0
Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents:
4
diff
changeset
|
811 if (ret == NULL) { |
f76c9389e9e0
Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents:
4
diff
changeset
|
812 dropbear_exit("m_strdup failed"); |
f76c9389e9e0
Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents:
4
diff
changeset
|
813 } |
f76c9389e9e0
Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents:
4
diff
changeset
|
814 return ret; |
f76c9389e9e0
Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents:
4
diff
changeset
|
815 } |
f76c9389e9e0
Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents:
4
diff
changeset
|
816 |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
817 void * m_realloc(void* ptr, size_t size) { |
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 void *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 if (size == 0) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
822 dropbear_exit("m_realloc failed"); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
823 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
824 ret = realloc(ptr, size); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
825 if (ret == NULL) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
826 dropbear_exit("m_realloc failed"); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
827 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
828 return ret; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
829 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
830 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
831 /* 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
|
832 * "Secure Programming for Linux and Unix HOWTO" */ |
161 | 833 /* Beware of calling this from within dbutil.c - things might get |
834 * optimised away */ | |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
835 void m_burn(void *data, unsigned int len) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
836 volatile char *p = data; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
837 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
838 if (data == NULL) |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
839 return; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
840 while (len--) { |
655
76e780c74a5e
- Burn buffers to 0x00 instead
Matt Johnston <matt@ucc.asn.au>
parents:
639
diff
changeset
|
841 *p++ = 0x0; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
842 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
843 } |
26 | 844 |
109
2e9d1f29c50f
merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
845 |
2e9d1f29c50f
merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
846 void setnonblocking(int fd) { |
2e9d1f29c50f
merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
847 |
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
|
848 TRACE(("setnonblocking: %d", fd)) |
109
2e9d1f29c50f
merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
849 |
2e9d1f29c50f
merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
850 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
|
851 if (errno == ENODEV) { |
257f09a63dab
* add SSH_ASKPASS support (based on patch from Paul Whittaker
Matt Johnston <matt@ucc.asn.au>
parents:
172
diff
changeset
|
852 /* 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
|
853 * 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
|
854 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
|
855 } else { |
257f09a63dab
* add SSH_ASKPASS support (based on patch from Paul Whittaker
Matt Johnston <matt@ucc.asn.au>
parents:
172
diff
changeset
|
856 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
|
857 } |
109
2e9d1f29c50f
merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
858 } |
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
|
859 TRACE(("leave setnonblocking")) |
109
2e9d1f29c50f
merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
860 } |
425 | 861 |
862 void disallow_core() { | |
863 struct rlimit lim; | |
864 lim.rlim_cur = lim.rlim_max = 0; | |
865 setrlimit(RLIMIT_CORE, &lim); | |
866 } | |
492
b956d6151600
Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents:
490
diff
changeset
|
867 |
b956d6151600
Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents:
490
diff
changeset
|
868 /* 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
|
869 int m_str_to_uint(const char* str, unsigned int *val) { |
b956d6151600
Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents:
490
diff
changeset
|
870 errno = 0; |
b956d6151600
Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents:
490
diff
changeset
|
871 *val = strtoul(str, NULL, 10); |
b956d6151600
Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents:
490
diff
changeset
|
872 /* 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
|
873 * I've looked at mention it in their manpage */ |
b956d6151600
Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents:
490
diff
changeset
|
874 if ((*val == 0 && errno == EINVAL) |
b956d6151600
Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents:
490
diff
changeset
|
875 || (*val == ULONG_MAX && errno == ERANGE)) { |
b956d6151600
Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents:
490
diff
changeset
|
876 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
|
877 } else { |
b956d6151600
Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents:
490
diff
changeset
|
878 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
|
879 } |
b956d6151600
Replace calls to strtoul() with a helper m_str_to_uint()
Matt Johnston <matt@ucc.asn.au>
parents:
490
diff
changeset
|
880 } |