annotate dbutil.c @ 489:79c657a673ec

- Allow specifying a port with host/port. - Rearrange multihop hostname parsing
author Matt Johnston <matt@ucc.asn.au>
date Thu, 18 Sep 2008 16:01:59 +0000
parents d59f628e7baa
children bd2634b03b12
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 * Dropbear - a SSH2 server
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * Copyright (c) 2002,2003 Matt Johnston
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 * All rights reserved.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 * of this software and associated documentation files (the "Software"), to deal
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * in the Software without restriction, including without limitation the rights
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 * copies of the Software, and to permit persons to whom the Software is
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 * furnished to do so, subject to the following conditions:
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 * The above copyright notice and this permission notice shall be included in
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 * all copies or substantial portions of the Software.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 * SOFTWARE.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 * strlcat() is copyright as follows:
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 * Copyright (c) 1998 Todd C. Miller <[email protected]>
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 * All rights reserved.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 * Redistribution and use in source and binary forms, with or without
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 * modification, are permitted provided that the following conditions
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 * are met:
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 * 1. Redistributions of source code must retain the above copyright
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 * notice, this list of conditions and the following disclaimer.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 * 2. Redistributions in binary form must reproduce the above copyright
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 * notice, this list of conditions and the following disclaimer in the
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 * documentation and/or other materials provided with the distribution.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 * 3. The name of the author may not be used to endorse or promote products
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 * derived from this software without specific prior written permission.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50
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,
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
60 va_list param);
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
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
64 void (*_dropbear_exit)(int exitcode, const char* format, va_list param)
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) {
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
114 dropbear_exit("failed assertion (%s:%d): `%s'", file, line, expr);
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 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 #endif /* DEBUG_TRACE */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155
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
156 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
157
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
158 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
159
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
160 /* 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
161 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
162 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
163
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
164 /* set the TOS bit. note that this will fail for ipv6, I can't find any
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
165 * equivalent. */
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
166 #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
167 val = 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
168 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
169 #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
170
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 #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
172 /* 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
173 * 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
174 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
175 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
176 #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
177
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
178 }
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
258
306499676384 * add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents: 251
diff changeset
180 /* 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
181 * 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
182 * 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
183 * Returns the number of sockets bound on success, or -1 on failure. On
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
184 * failure, if errstring wasn't NULL, it'll be a newly malloced error
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
185 * string.*/
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
186 int dropbear_listen(const char* address, const char* port,
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
187 int *socks, unsigned int sockcount, char **errstring, int *maxfd) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
188
70
b0316ce64e4b Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents: 68
diff changeset
189 struct addrinfo hints, *res = NULL, *res0 = NULL;
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
190 int err;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
191 unsigned int nsock;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
192 struct linger linger;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
193 int val;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
194 int sock;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
195
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
196 TRACE(("enter dropbear_listen"))
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
197
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
198 memset(&hints, 0, sizeof(hints));
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
199 hints.ai_family = AF_UNSPEC; /* TODO: let them flag v4 only etc */
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
200 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
201
433
c216212001fc Fix for -pedantic -ansi compilation, change // to /**/, plus some signedness
Matt Johnston <matt@ucc.asn.au>
parents: 425
diff changeset
202 /* for calling getaddrinfo:
c216212001fc Fix for -pedantic -ansi compilation, change // to /**/, plus some signedness
Matt Johnston <matt@ucc.asn.au>
parents: 425
diff changeset
203 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
204 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
205 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
206 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
207 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
208 } else {
258
306499676384 * add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents: 251
diff changeset
209 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
210 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
211 address = NULL;
306499676384 * add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents: 251
diff changeset
212 }
63
dcc43965928f - A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents: 62
diff changeset
213 hints.ai_flags = AI_PASSIVE;
dcc43965928f - A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents: 62
diff changeset
214 }
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
215 err = getaddrinfo(address, port, &hints, &res0);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
216
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
217 if (err) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
218 if (errstring != NULL && *errstring == NULL) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
219 int len;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
220 len = 20 + strlen(gai_strerror(err));
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
221 *errstring = (char*)m_malloc(len);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
222 snprintf(*errstring, len, "Error resolving: %s", gai_strerror(err));
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
223 }
239
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
224 if (res0) {
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
225 freeaddrinfo(res0);
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
226 res0 = NULL;
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
227 }
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
228 TRACE(("leave dropbear_listen: failed resolving"))
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
229 return -1;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
230 }
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
231
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
232
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
233 nsock = 0;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
234 for (res = res0; res != NULL && nsock < sockcount;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
235 res = res->ai_next) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
236
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
237 /* Get a socket */
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
238 socks[nsock] = socket(res->ai_family, res->ai_socktype,
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
239 res->ai_protocol);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
240
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
241 sock = socks[nsock]; /* For clarity */
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
242
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
243 if (sock < 0) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
244 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
245 TRACE(("socket() failed"))
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
246 continue;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
247 }
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
248
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
249 /* Various useful socket options */
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
250 val = 1;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
251 /* set to reuse, quick timeout */
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
252 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &val, sizeof(val));
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
253 linger.l_onoff = 1;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
254 linger.l_linger = 5;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
255 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void*)&linger, sizeof(linger));
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
256
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
257 set_sock_priority(sock);
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
258
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
259 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
260 err = errno;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
261 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
262 TRACE(("bind(%s) failed", port))
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
263 continue;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
264 }
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
265
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
266 if (listen(sock, 20) < 0) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
267 err = errno;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
268 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
269 TRACE(("listen() failed"))
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
270 continue;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
271 }
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
272
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
273 *maxfd = MAX(*maxfd, sock);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
274
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
275 nsock++;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
276 }
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
277
239
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
278 if (res0) {
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
279 freeaddrinfo(res0);
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
280 res0 = NULL;
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
281 }
e5ad9fa8b1fa add a missing freeaddrinfo()
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
282
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
283 if (nsock == 0) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
284 if (errstring != NULL && *errstring == NULL) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
285 int len;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
286 len = 20 + strlen(strerror(err));
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
287 *errstring = (char*)m_malloc(len);
64
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
288 snprintf(*errstring, len, "Error listening: %s", strerror(err));
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
289 }
408
28b10e93685c Fix failure-handling in dropbear_listen() when errstring is unset
Matt Johnston <matt@ucc.asn.au>
parents: 277
diff changeset
290 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
291 return -1;
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
292 }
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
293
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
294 TRACE(("leave dropbear_listen: success, %d socks bound", nsock))
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
295 return nsock;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
296 }
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
297
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
298 /* Connect via TCP to a host. Connection will try ipv4 or ipv6, will
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
299 * return immediately if nonblocking is set. On failure, if errstring
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
300 * wasn't null, it will be a newly malloced error message */
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
301
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
302 /* TODO: maxfd */
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
303 int connect_remote(const char* remotehost, const char* remoteport,
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
304 int nonblocking, char ** errstring) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
305
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
306 struct addrinfo *res0 = NULL, *res = NULL, hints;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
307 int sock;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
308 int err;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
309
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
310 TRACE(("enter connect_remote"))
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
311
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
312 if (errstring != NULL) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
313 *errstring = NULL;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
314 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
315
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
316 memset(&hints, 0, sizeof(hints));
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
317 hints.ai_socktype = SOCK_STREAM;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
318 hints.ai_family = PF_UNSPEC;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
319
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
320 err = getaddrinfo(remotehost, remoteport, &hints, &res0);
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
321 if (err) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
322 if (errstring != NULL && *errstring == NULL) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
323 int len;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
324 len = 20 + strlen(gai_strerror(err));
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
325 *errstring = (char*)m_malloc(len);
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
326 snprintf(*errstring, len, "Error resolving: %s", gai_strerror(err));
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
327 }
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
328 TRACE(("Error resolving: %s", gai_strerror(err)))
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
329 return -1;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
330 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
331
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
332 sock = -1;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
333 err = EADDRNOTAVAIL;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
334 for (res = res0; res; res = res->ai_next) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
335
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
336 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
337 if (sock < 0) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
338 err = errno;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
339 continue;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
340 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
341
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
342 if (nonblocking) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
343 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
344 close(sock);
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
345 sock = -1;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
346 if (errstring != NULL && *errstring == NULL) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
347 *errstring = m_strdup("Failed non-blocking");
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
348 }
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
349 TRACE(("Failed non-blocking: %s", strerror(errno)))
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
350 continue;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
351 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
352 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
353
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
354 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
355 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
356 TRACE(("Connect in progress"))
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
357 break;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
358 } else {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
359 err = errno;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
360 close(sock);
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
361 sock = -1;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
362 continue;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
363 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
364 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
365
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
366 break; /* Success */
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
367 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
368
70
b0316ce64e4b Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents: 68
diff changeset
369 if (sock < 0 && !(errno == EINPROGRESS && nonblocking)) {
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
370 /* Failed */
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
371 if (errstring != NULL && *errstring == NULL) {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
372 int len;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
373 len = 20 + strlen(strerror(err));
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
374 *errstring = (char*)m_malloc(len);
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
375 snprintf(*errstring, len, "Error connecting: %s", strerror(err));
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
376 }
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
377 TRACE(("Error connecting: %s", strerror(err)))
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
378 } else {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
379 /* 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
380 set_sock_priority(sock);
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
381 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
382
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
383 freeaddrinfo(res0);
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
384 if (sock > 0 && errstring != NULL && *errstring != NULL) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
385 m_free(*errstring);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
386 }
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
387
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
388 TRACE(("leave connect_remote: sock %d\n", sock))
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
389 return sock;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
390 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
391
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
392 /* 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
393 * 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
394 * 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
395 * 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
396 * 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
397 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
398 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
399 int infds[2];
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
400 int outfds[2];
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
401 int errfds[2];
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
402 pid_t pid;
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
403
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
404 const int FDIN = 0;
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
405 const int FDOUT = 1;
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
406
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
407 /* 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
408 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
409 return DROPBEAR_FAILURE;
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
410 }
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
411 if (pipe(outfds) != 0) {
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
412 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
413 }
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
414 if (ret_errfd && pipe(errfds) != 0) {
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
415 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
416 }
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
417
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
418 #ifdef __uClinux__
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
419 pid = vfork();
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
420 #else
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
421 pid = fork();
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
422 #endif
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
423
484
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
424 if (pid < 0) {
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
425 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
426 }
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
427
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
428 if (!pid) {
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
429 /* child */
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
430
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
431 TRACE(("back to normal sigchld"))
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
432 /* Revert to normal sigchld handling */
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
433 if (signal(SIGCHLD, SIG_DFL) == SIG_ERR) {
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
434 dropbear_exit("signal() error");
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
435 }
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
436
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
437 /* redirect stdin/stdout */
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
438
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
439 if ((dup2(infds[FDIN], STDIN_FILENO) < 0) ||
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
440 (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
441 (ret_errfd && dup2(errfds[FDOUT], STDERR_FILENO) < 0)) {
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
442 TRACE(("leave noptycommand: error redirecting FDs"))
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
443 dropbear_exit("child dup2() failure");
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
444 }
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 close(infds[FDOUT]);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
447 close(infds[FDIN]);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
448 close(outfds[FDIN]);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
449 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
450 if (ret_errfd)
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
451 {
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
452 close(errfds[FDIN]);
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
453 close(errfds[FDOUT]);
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
454 }
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
455
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
456 exec_fn(exec_data);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
457 /* not reached */
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
458 return DROPBEAR_FAILURE;
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
459 } else {
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
460 /* parent */
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
461 close(infds[FDIN]);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
462 close(outfds[FDOUT]);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
463
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
464 setnonblocking(outfds[FDIN]);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
465 setnonblocking(infds[FDOUT]);
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
466
484
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
467 if (ret_errfd) {
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
468 close(errfds[FDOUT]);
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
469 setnonblocking(errfds[FDIN]);
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
470 }
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
471
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
472 if (ret_pid) {
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
473 *ret_pid = pid;
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
474 }
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
475
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
476 *ret_writefd = infds[FDOUT];
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
477 *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
478 if (ret_errfd) {
effb4a25b1ae Don't capture stderr from spawned processes in proxycommand mode
Matt Johnston <matt@ucc.asn.au>
parents: 482
diff changeset
479 *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
480 }
481
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
481 return DROPBEAR_SUCCESS;
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
482 }
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
483 }
357a2e2e9bcc - Generalise spawn_command function
Matt Johnston <matt@ucc.asn.au>
parents: 433
diff changeset
484
482
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
485 /* 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
486 * 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
487 */
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
488 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
489 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
490 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
491 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
492
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
493 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
494
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
495 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
496 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
497 } else {
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
498 /* 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
499 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
500 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
501 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
502 }
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
503
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
504 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
505 argv[1] = "-c";
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
506 argv[2] = cmd;
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
507 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
508 } else {
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
509 /* 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
510 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
511 }
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
512
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
513 /* 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
514 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
515 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
516 }
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
517
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
518 /* 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
519 * 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
520 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
521 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
522 }
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
523
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
524 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
525 }
7ad49f34a122 - Add run_shell_command() function to run a "sh -c" command, handling
Matt Johnston <matt@ucc.asn.au>
parents: 481
diff changeset
526
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
527 /* 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
528 * value is allocated with malloc() */
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
529 unsigned char * getaddrstring(struct sockaddr_storage* addr, int withport) {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
530
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
531 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
532 char *retstring = NULL;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
533 int ret;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
534 unsigned int len;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
535
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
536 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
537 /* 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
538 * 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
539 * 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
540 * 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
541 #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
542 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
543 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
544 }
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
545 #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
546 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
547 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
548 }
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
549 #endif
335
e17f0333c21e Another stab at getting ss_family happy on older glibc
Matt Johnston <matt@ucc.asn.au>
parents: 277
diff changeset
550 #endif
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
551
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
552 ret = getnameinfo((struct sockaddr*)addr, len, hbuf, sizeof(hbuf),
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
553 sbuf, sizeof(sbuf), NI_NUMERICSERV | NI_NUMERICHOST);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
555 if (ret != 0) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
556 /* This is a fairly bad failure - it'll fallback to IP if it
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
557 * just can't resolve */
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
558 dropbear_exit("failed lookup (%d, %d)", ret, errno);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
559 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
560
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
561 if (withport) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
562 len = strlen(hbuf) + 2 + strlen(sbuf);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
563 retstring = (char*)m_malloc(len);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
564 snprintf(retstring, len, "%s:%s", hbuf, sbuf);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
565 } else {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
566 retstring = m_strdup(hbuf);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
567 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
568
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
569 return retstring;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
570
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
571 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
572
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
573 /* Get the hostname corresponding to the address addr. On failure, the IP
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
574 * address is returned. The return value is allocated with strdup() */
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
575 char* getaddrhostname(struct sockaddr_storage * addr) {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
576
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
577 char hbuf[NI_MAXHOST];
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
578 char sbuf[NI_MAXSERV];
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
579 int ret;
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
580 unsigned int len;
172
3e49d8d18005 * respect DO_HOST_LOOKUP config option
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
581 #ifdef DO_HOST_LOOKUP
3e49d8d18005 * respect DO_HOST_LOOKUP config option
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
582 const int flags = NI_NUMERICSERV;
3e49d8d18005 * respect DO_HOST_LOOKUP config option
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
583 #else
3e49d8d18005 * respect DO_HOST_LOOKUP config option
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
584 const int flags = NI_NUMERICHOST | NI_NUMERICSERV;
3e49d8d18005 * respect DO_HOST_LOOKUP config option
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
585 #endif
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
586
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
587 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
588 /* Some platforms such as Solaris 8 require that len is the length
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
589 * of the specific structure. */
335
e17f0333c21e Another stab at getting ss_family happy on older glibc
Matt Johnston <matt@ucc.asn.au>
parents: 277
diff changeset
590 #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
591 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
592 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
593 }
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
594 #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
595 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
596 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
597 }
7ceceb46d655 Fix so that getnameinfo() is passed the address-specific structure size. This
Matt Johnston <matt@ucc.asn.au>
parents: 150
diff changeset
598 #endif
335
e17f0333c21e Another stab at getting ss_family happy on older glibc
Matt Johnston <matt@ucc.asn.au>
parents: 277
diff changeset
599 #endif
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
600
172
3e49d8d18005 * respect DO_HOST_LOOKUP config option
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
601
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
602 ret = getnameinfo((struct sockaddr*)addr, len, hbuf, sizeof(hbuf),
172
3e49d8d18005 * respect DO_HOST_LOOKUP config option
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
603 sbuf, sizeof(sbuf), flags);
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
604
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
605 if (ret != 0) {
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
606 /* On some systems (Darwin does it) we get EINTR from getnameinfo
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
607 * somehow. Eew. So we'll just return the IP, since that doesn't seem
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
608 * to exhibit that behaviour. */
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
609 return getaddrstring(addr, 0);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
610 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
611
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
612 return m_strdup(hbuf);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
613 }
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
614
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
615 #ifdef DEBUG_TRACE
198
65585699d980 * add a "label" argument to printhex()
Matt Johnston <matt@ucc.asn.au>
parents: 173
diff changeset
616 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
617
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
618 int i;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
619
198
65585699d980 * add a "label" argument to printhex()
Matt Johnston <matt@ucc.asn.au>
parents: 173
diff changeset
620 fprintf(stderr, "%s\n", label);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
621 for (i = 0; i < len; i++) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
622 fprintf(stderr, "%02x", buf[i]);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
623 if (i % 16 == 15) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
624 fprintf(stderr, "\n");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
625 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
626 else if (i % 2 == 1) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
627 fprintf(stderr, " ");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
628 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
629 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
630 fprintf(stderr, "\n");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
631 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
632 #endif
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
633
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
634 /* 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
635 * for '\n', '\r' and '\t'.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
636 * 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
637 * use */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
638 char * stripcontrol(const char * text) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
639
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
640 char * ret;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
641 int len, pos;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
642 int i;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
643
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
644 len = strlen(text);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
645 ret = m_malloc(len+1);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
646
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
647 pos = 0;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
648 for (i = 0; i < len; i++) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
649 if ((text[i] <= '~' && text[i] >= ' ') /* normal printable range */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
650 || 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
651 ret[pos] = text[i];
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
652 pos++;
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 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
655 ret[pos] = 0x0;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
656 return ret;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
657 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
658
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
659
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
660 /* 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
661 * 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
662 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
663 int buf_readfile(buffer* buf, const char* filename) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
664
357
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
665 int fd = -1;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
666 int len;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
667 int maxlen;
358
e81d3bc1dc78 Forgot variable declaration.
Matt Johnston <matt@ucc.asn.au>
parents: 357
diff changeset
668 int ret = DROPBEAR_FAILURE;
4
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 fd = open(filename, O_RDONLY);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
671
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
672 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
673 goto out;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
674 }
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 do {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
677 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
678 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
679 if (len < 0) {
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
680 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
681 continue;
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
682 }
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
683 goto out;
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
684 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
685 buf_incrwritepos(buf, len);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
686 } while (len < maxlen && len > 0);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
687
357
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
688 ret = DROPBEAR_SUCCESS;
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
689
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
690 out:
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
691 if (fd >= 0) {
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
692 m_close(fd);
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
693 }
9e2ad1023978 Handle failure reading a file (such as a key file)
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
694 return ret;
4
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
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
697 /* 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
698 * authkeys file.
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
699 * 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
700 /* 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
701 #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
702 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
703
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
704 int c = EOF;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
705
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
706 TRACE(("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
707
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
708 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
709 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
710
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
711 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
712
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
713 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
714 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
715 goto out;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
716 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
717
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
718 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
719 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
720
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
721 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
722 /* 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
723 * ignore that line */
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
724 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
725
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
726 out:
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
727
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
728
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
729 /* 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
730 if (c == EOF && line->pos == 0) {
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 161
diff changeset
731 TRACE(("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
732 return DROPBEAR_FAILURE;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
733 } else {
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
734 TRACE(("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
735 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
736 return DROPBEAR_SUCCESS;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
737 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
738
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 #endif
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 45
diff changeset
741
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
742 /* make sure that the socket closes */
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
743 void m_close(int fd) {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
744
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
745 int val;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
746 do {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
747 val = close(fd);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
748 } while (val < 0 && errno == EINTR);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
749
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
750 if (val < 0 && errno != EBADF) {
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
751 /* Linux says EIO can happen */
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
752 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
753 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
754 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
755
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
756 void * m_malloc(size_t size) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
757
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
758 void* ret;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
759
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
760 if (size == 0) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
761 dropbear_exit("m_malloc failed");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
762 }
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
763 ret = calloc(1, size);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
764 if (ret == NULL) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
765 dropbear_exit("m_malloc failed");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
766 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
767 return ret;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
768
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
769 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
770
11
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
771 void * m_strdup(const char * str) {
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
772 char* ret;
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
773
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
774 ret = strdup(str);
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
775 if (ret == NULL) {
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
776 dropbear_exit("m_strdup failed");
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
777 }
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
778 return ret;
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
779 }
f76c9389e9e0 Mostly done with the listener changeover
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
780
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
781 void __m_free(void* ptr) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
782 if (ptr != NULL) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
783 free(ptr);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
784 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
785 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
786
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
787 void * m_realloc(void* ptr, size_t size) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
788
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
789 void *ret;
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 if (size == 0) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
792 dropbear_exit("m_realloc failed");
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 ret = realloc(ptr, size);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
795 if (ret == NULL) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
796 dropbear_exit("m_realloc failed");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
797 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
798 return ret;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
799 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
800
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
801 /* 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
802 * "Secure Programming for Linux and Unix HOWTO" */
161
b9d3f725e00b 0.44 release changes
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
803 /* Beware of calling this from within dbutil.c - things might get
b9d3f725e00b 0.44 release changes
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
804 * optimised away */
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
805 void m_burn(void *data, unsigned int len) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
806 volatile char *p = data;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
807
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
808 if (data == NULL)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
809 return;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
810 while (len--) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
811 *p++ = 0x66;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
812 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
813 }
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 11
diff changeset
814
109
2e9d1f29c50f merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
815
2e9d1f29c50f merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
816 void setnonblocking(int fd) {
2e9d1f29c50f merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
817
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
818 TRACE(("setnonblocking: %d", fd))
109
2e9d1f29c50f merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
819
2e9d1f29c50f merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
820 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
821 if (errno == ENODEV) {
257f09a63dab * add SSH_ASKPASS support (based on patch from Paul Whittaker
Matt Johnston <matt@ucc.asn.au>
parents: 172
diff changeset
822 /* 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
823 * 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
824 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
825 } else {
257f09a63dab * add SSH_ASKPASS support (based on patch from Paul Whittaker
Matt Johnston <matt@ucc.asn.au>
parents: 172
diff changeset
826 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
827 }
109
2e9d1f29c50f merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
828 }
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
829 TRACE(("leave setnonblocking"))
109
2e9d1f29c50f merge of 50be59810e462f9f44f55e421227d6aa0b31982b
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
830 }
425
1afa503e33f5 Disable core dumps
Matt Johnston <matt@ucc.asn.au>
parents: 410
diff changeset
831
1afa503e33f5 Disable core dumps
Matt Johnston <matt@ucc.asn.au>
parents: 410
diff changeset
832 void disallow_core() {
1afa503e33f5 Disable core dumps
Matt Johnston <matt@ucc.asn.au>
parents: 410
diff changeset
833 struct rlimit lim;
1afa503e33f5 Disable core dumps
Matt Johnston <matt@ucc.asn.au>
parents: 410
diff changeset
834 lim.rlim_cur = lim.rlim_max = 0;
1afa503e33f5 Disable core dumps
Matt Johnston <matt@ucc.asn.au>
parents: 410
diff changeset
835 setrlimit(RLIMIT_CORE, &lim);
1afa503e33f5 Disable core dumps
Matt Johnston <matt@ucc.asn.au>
parents: 410
diff changeset
836 }