annotate svr-main.c @ 317:063cf9e8efea ucc-axis-hack

dropbear_log to stderr since it works now
author Matt Johnston <matt@ucc.asn.au>
date Sat, 01 Apr 2006 12:09:46 +0000
parents 474c1a700b67
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 * Dropbear - a SSH2 server
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 *
290
94ee16f5b8a8 0.48 progress
Matt Johnston <matt@ucc.asn.au>
parents: 279
diff changeset
4 * Copyright (c) 2002-2006 Matt Johnston
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 * All rights reserved.
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 *
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 * of this software and associated documentation files (the "Software"), to deal
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * in the Software without restriction, including without limitation the rights
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 * copies of the Software, and to permit persons to whom the Software is
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 * furnished to do so, subject to the following conditions:
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 *
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 * The above copyright notice and this permission notice shall be included in
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 * all copies or substantial portions of the Software.
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 *
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
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
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 * SOFTWARE. */
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 #include "includes.h"
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 #include "dbutil.h"
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 #include "session.h"
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 #include "buffer.h"
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 #include "signkey.h"
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 #include "runopts.h"
298
7dad470ad4aa minor cleanups for some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 290
diff changeset
31 #include "random.h"
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
33 static size_t listensockets(int *sock, size_t sockcount, int *maxfd);
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 static void sigchld_handler(int dummy);
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 static void sigsegv_handler(int);
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 static void sigintterm_handler(int fish);
156
8c2b3506f112 Rearrange preprocessor parts so that compilation with various options
Matt Johnston <matt@ucc.asn.au>
parents: 113
diff changeset
37 #ifdef INETD_MODE
71
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
38 static void main_inetd();
156
8c2b3506f112 Rearrange preprocessor parts so that compilation with various options
Matt Johnston <matt@ucc.asn.au>
parents: 113
diff changeset
39 #endif
8c2b3506f112 Rearrange preprocessor parts so that compilation with various options
Matt Johnston <matt@ucc.asn.au>
parents: 113
diff changeset
40 #ifdef NON_INETD_MODE
71
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
41 static void main_noinetd();
156
8c2b3506f112 Rearrange preprocessor parts so that compilation with various options
Matt Johnston <matt@ucc.asn.au>
parents: 113
diff changeset
42 #endif
309
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
43 #ifdef INETD_SERVER_MODE
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
44 static void main_inetd_server();
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
45 #endif
71
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
46 static void commonsetup();
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47
309
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
48 static void blank_dropbear_log(int priority, const char* format, va_list param)
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
49 {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
50 return;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
51 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
52
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 #if defined(DBMULTI_dropbear) || !defined(DROPBEAR_MULTI)
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 #if defined(DBMULTI_dropbear) && defined(DROPBEAR_MULTI)
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 int dropbear_main(int argc, char ** argv)
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 #else
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 int main(int argc, char ** argv)
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 #endif
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 {
71
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
60 _dropbear_exit = svr_dropbear_exit;
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
61 _dropbear_log = svr_dropbear_log;
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
62
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
63 /* get commandline options */
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
64 svr_getopts(argc, argv);
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
65
309
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
66 #ifdef INETD_SERVER_MODE
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
67 if (svr_opts.inetd_dropbear_path) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
68 main_inetd_server();
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
69 /* notreached */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
70 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
71 #endif
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
72
71
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
73 #ifdef INETD_MODE
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
74 /* service program mode */
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
75 if (svr_opts.inetdmode) {
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
76 main_inetd();
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
77 /* notreached */
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
78 }
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
79 #endif
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
80
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
81 #ifdef NON_INETD_MODE
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
82 main_noinetd();
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
83 /* notreached */
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
84 #endif
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
85
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
86 dropbear_exit("Compiled without normal mode, can't run without -i\n");
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
87 return -1;
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
88 }
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
89 #endif
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
90
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
91 #ifdef INETD_MODE
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
92 static void main_inetd() {
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
93
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
94 struct sockaddr_storage remoteaddr;
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
95 socklen_t remoteaddrlen;
71
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
96 char * addrstring = NULL;
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
97
272
3be7ae2e8dfa Only read /dev/random once when the program starts
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
98 /* Set up handlers, syslog, seed random */
71
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
99 commonsetup();
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
100
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
101 remoteaddrlen = sizeof(remoteaddr);
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
102 if (getpeername(0, (struct sockaddr*)&remoteaddr, &remoteaddrlen) < 0) {
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
103 dropbear_exit("Unable to getpeername: %s", strerror(errno));
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
104 }
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
105
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
106 /* In case our inetd was lax in logging source addresses */
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
107 addrstring = getaddrstring(&remoteaddr, 1);
309
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
108 dropbear_log(LOG_INFO, "Child inetd connection from %s", addrstring);
71
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
109
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
110 /* Don't check the return value - it may just fail since inetd has
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
111 * already done setsid() after forking (xinetd on Darwin appears to do
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
112 * this */
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
113 setsid();
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
114
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
115 /* Start service program
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
116 * -1 is a dummy childpipe, just something we can close() without
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
117 * mattering. */
158
364a75cfebab Log the IP along with auth success/fail attempts
Matt Johnston <matt@ucc.asn.au>
parents: 156
diff changeset
118 svr_session(0, -1, getaddrhostname(&remoteaddr), addrstring);
71
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
119
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
120 /* notreached */
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
121 }
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
122 #endif /* INETD_MODE */
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
123
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
124 #ifdef NON_INETD_MODE
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
125 void main_noinetd() {
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 fd_set fds;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 struct timeval seltimeout;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 unsigned int i, j;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 int val;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 int maxsock = -1;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 int listensocks[MAX_LISTEN_ADDR];
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
132 size_t listensockcount = 0;
71
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
133 FILE *pidfile = NULL;
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
135 int childpipes[MAX_UNAUTH_CLIENTS];
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
136 char * preauth_addrs[MAX_UNAUTH_CLIENTS];
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
137
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 int childsock;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 int childpipe[2];
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 /* fork */
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 if (svr_opts.forkbg) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 int closefds = 0;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 #ifndef DEBUG_TRACE
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 if (!svr_opts.usingsyslog) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 closefds = 1;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 #endif
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 if (daemon(0, closefds) < 0) {
71
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
150 dropbear_exit("Failed to daemonize: %s", strerror(errno));
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153
71
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
154 commonsetup();
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 /* should be done after syslog is working */
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 if (svr_opts.forkbg) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 dropbear_log(LOG_INFO, "Running in background");
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 } else {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 dropbear_log(LOG_INFO, "Not forking");
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 /* create a PID file so that we can be killed easily */
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 pidfile = fopen(DROPBEAR_PIDFILE, "w");
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 if (pidfile) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 fprintf(pidfile, "%d\n", getpid());
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 fclose(pidfile);
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 /* sockets to identify pre-authenticated clients */
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 for (i = 0; i < MAX_UNAUTH_CLIENTS; i++) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 childpipes[i] = -1;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 }
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
174 bzero(preauth_addrs, sizeof(preauth_addrs));
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 /* Set up the listening sockets */
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
177 listensockcount = listensockets(listensocks, MAX_LISTEN_ADDR, &maxsock);
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
178 if (listensockcount == 0)
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
179 {
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
180 dropbear_exit("No listening ports available.");
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
181 }
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 /* incoming connection select loop */
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 for(;;) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 FD_ZERO(&fds);
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 seltimeout.tv_sec = 60;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 seltimeout.tv_usec = 0;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 /* listening sockets */
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
192 for (i = 0; i < listensockcount; i++) {
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 FD_SET(listensocks[i], &fds);
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196 /* pre-authentication clients */
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 for (i = 0; i < MAX_UNAUTH_CLIENTS; i++) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 if (childpipes[i] >= 0) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199 FD_SET(childpipes[i], &fds);
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200 maxsock = MAX(maxsock, childpipes[i]);
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 val = select(maxsock+1, &fds, NULL, NULL, &seltimeout);
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206 if (exitflag) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 unlink(DROPBEAR_PIDFILE);
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208 dropbear_exit("Terminated by signal");
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 if (val == 0) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212 /* timeout reached */
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 continue;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 if (val < 0) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217 if (errno == EINTR) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218 continue;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 dropbear_exit("Listening socket error");
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
223 /* close fds which have been authed or closed - svr-auth.c handles
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 * closing the auth sockets on success */
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 for (i = 0; i < MAX_UNAUTH_CLIENTS; i++) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 if (childpipes[i] >= 0 && FD_ISSET(childpipes[i], &fds)) {
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
227 m_close(childpipes[i]);
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 childpipes[i] = -1;
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
229 m_free(preauth_addrs[i]);
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 /* handle each socket which has something to say */
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
234 for (i = 0; i < listensockcount; i++) {
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
235
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
236 struct sockaddr_storage remoteaddr;
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
237 socklen_t remoteaddrlen = 0;
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
238 size_t num_unauthed_for_addr = 0;
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
239 size_t num_unauthed_total = 0;
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
240 char * remote_addr_str = NULL;
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
241 pid_t fork_ret = 0;
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
242 size_t conn_idx = 0;
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
243
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 if (!FD_ISSET(listensocks[i], &fds))
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 continue;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
247 remoteaddrlen = sizeof(remoteaddr);
63
dcc43965928f - A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents: 62
diff changeset
248 childsock = accept(listensocks[i],
dcc43965928f - A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents: 62
diff changeset
249 (struct sockaddr*)&remoteaddr, &remoteaddrlen);
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 if (childsock < 0) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252 /* accept failed */
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253 continue;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
256 /* Limit the number of unauthenticated connections per IP */
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
257 remote_addr_str = getaddrstring(&remoteaddr, 0);
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
258
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
259 num_unauthed_for_addr = 0;
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
260 num_unauthed_total = 0;
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 for (j = 0; j < MAX_UNAUTH_CLIENTS; j++) {
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
262 if (childpipes[j] >= 0) {
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
263 num_unauthed_total++;
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
264 if (strcmp(remote_addr_str, preauth_addrs[j]) == 0) {
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
265 num_unauthed_for_addr++;
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
266 }
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
267 } else {
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
268 /* a free slot */
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
269 conn_idx = j;
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
273 if (num_unauthed_total >= MAX_UNAUTH_CLIENTS
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
274 || num_unauthed_for_addr >= MAX_UNAUTH_PER_IP) {
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
275 goto out;
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278 if (pipe(childpipe) < 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: 158
diff changeset
279 TRACE(("error creating child pipe"))
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
280 goto out;
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
283 fork_ret = fork();
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
284 if (fork_ret < 0) {
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
285 dropbear_log(LOG_WARNING, "error forking: %s", strerror(errno));
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
286 goto out;
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
287
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
288 } else if (fork_ret > 0) {
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
289
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
290 /* parent */
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
291 childpipes[conn_idx] = childpipe[0];
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
292 m_close(childpipe[1]);
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
293 preauth_addrs[conn_idx] = remote_addr_str;
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
294 remote_addr_str = NULL;
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
295
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
296 } else {
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
297
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298 /* child */
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
299 char * addrstring = NULL;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300 #ifdef DEBUG_FORKGPROF
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301 extern void _start(void), etext(void);
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302 monstartup((u_long)&_start, (u_long)&etext);
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
303 #endif /* DEBUG_FORKGPROF */
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
304
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
305 m_free(remote_addr_str);
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
306 addrstring = getaddrstring(&remoteaddr, 1);
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
307 dropbear_log(LOG_INFO, "Child connection from %s", addrstring);
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
308
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
309 if (setsid() < 0) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
310 dropbear_exit("setsid: %s", strerror(errno));
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
311 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
312
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
313 /* make sure we close sockets */
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
314 for (i = 0; i < listensockcount; i++) {
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
315 m_close(listensocks[i]);
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
316 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
317
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
318 m_close(childpipe[0]);
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
319
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
320 /* start the session */
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
321 svr_session(childsock, childpipe[1],
158
364a75cfebab Log the IP along with auth success/fail attempts
Matt Johnston <matt@ucc.asn.au>
parents: 156
diff changeset
322 getaddrhostname(&remoteaddr),
364a75cfebab Log the IP along with auth success/fail attempts
Matt Johnston <matt@ucc.asn.au>
parents: 156
diff changeset
323 addrstring);
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324 /* don't return */
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
325 dropbear_assert(0);
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326 }
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
327
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
328 out:
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
329 /* This section is important for the parent too */
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
330 m_close(childsock);
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
331 if (remote_addr_str) {
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
332 m_free(remote_addr_str);
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
333 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
334 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
335 } /* for(;;) loop */
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
336
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
337 /* don't reach here */
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
338 }
71
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
339 #endif /* NON_INETD_MODE */
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
340
309
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
341 #ifdef INETD_SERVER_MODE
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
342 void main_inetd_server() {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
343 fd_set fds;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
344 struct timeval seltimeout;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
345 unsigned int i;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
346 int val;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
347 int maxsock = -1;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
348 int listensocks[MAX_LISTEN_ADDR];
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
349 size_t listensockcount = 0;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
350 FILE *pidfile = NULL;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
351
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
352 int childsock;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
353
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
354 /* fork */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
355 if (svr_opts.forkbg) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
356 int closefds = 0;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
357 #ifndef DEBUG_TRACE
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
358 if (!svr_opts.usingsyslog) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
359 closefds = 1;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
360 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
361 #endif
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
362 if (daemon(0, closefds) < 0) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
363 dropbear_exit("Failed to daemonize: %s", strerror(errno));
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
364 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
365 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
366
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
367 commonsetup();
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
368
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
369 /* should be done after syslog is working */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
370 if (svr_opts.forkbg) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
371 dropbear_log(LOG_INFO, "Running in background");
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
372 } else {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
373 dropbear_log(LOG_INFO, "Not forking");
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
374 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
375
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
376 /* create a PID file so that we can be killed easily */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
377 pidfile = fopen(DROPBEAR_PIDFILE, "w");
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
378 if (pidfile) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
379 fprintf(pidfile, "%d\n", getpid());
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
380 fclose(pidfile);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
381 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
382
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
383 /* Set up the listening sockets */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
384 listensockcount = listensockets(listensocks, MAX_LISTEN_ADDR, &maxsock);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
385 if (listensockcount == 0)
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
386 {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
387 dropbear_exit("No listening ports available.");
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
388 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
389
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
390 /* incoming connection select loop */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
391 for(;;) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
392
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
393 FD_ZERO(&fds);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
394
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
395 seltimeout.tv_sec = 60;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
396 seltimeout.tv_usec = 0;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
397
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
398 /* listening sockets */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
399 for (i = 0; i < listensockcount; i++) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
400 FD_SET(listensocks[i], &fds);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
401 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
402
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
403 val = select(maxsock+1, &fds, NULL, NULL, &seltimeout);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
404
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
405 if (exitflag) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
406 unlink(DROPBEAR_PIDFILE);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
407 dropbear_exit("Terminated by signal");
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
408 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
409
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
410 if (val == 0) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
411 /* timeout reached */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
412 continue;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
413 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
414
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
415 if (val < 0) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
416 if (errno == EINTR) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
417 continue;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
418 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
419 dropbear_exit("Listening socket error");
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
420 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
421
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
422 /* handle each socket which has something to say */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
423 for (i = 0; i < listensockcount; i++) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
424
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
425 struct sockaddr_storage remoteaddr;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
426 socklen_t remoteaddrlen = 0;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
427 pid_t fork_ret = 0;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
428
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
429 if (!FD_ISSET(listensocks[i], &fds))
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
430 continue;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
431
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
432 remoteaddrlen = sizeof(remoteaddr);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
433 childsock = accept(listensocks[i],
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
434 (struct sockaddr*)&remoteaddr, &remoteaddrlen);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
435
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
436 if (childsock < 0) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
437 /* accept failed */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
438 continue;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
439 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
440
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
441 fork_ret = fork();
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
442 if (fork_ret < 0) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
443 dropbear_log(LOG_WARNING, "error forking: %s", strerror(errno));
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
444 goto out;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
445
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
446 } else if (fork_ret > 0) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
447 /* parent */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
448 } else {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
449
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
450 char * argv[3];
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
451
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
452 /* child */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
453
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
454 if (setsid() < 0) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
455 dropbear_exit("setsid: %s", strerror(errno));
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
456 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
457
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
458 /* make sure we close sockets */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
459 for (i = 0; i < listensockcount; i++) {
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
460 m_close(listensocks[i]);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
461 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
462
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
463 close(STDIN_FILENO);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
464 close(STDOUT_FILENO);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
465 //close(STDERR_FILENO);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
466
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
467 dup2(childsock, STDIN_FILENO);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
468 dup2(childsock, STDOUT_FILENO);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
469
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
470 argv[0] = "dropbear";
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
471 argv[1] = "-i";
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
472 argv[2] = NULL;
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
473
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
474 execv(svr_opts.inetd_dropbear_path, argv);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
475 dropbear_exit("failed to fork inetd dropbear");
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
476
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
477 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
478 out:
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
479 /* This section is important for the parent too */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
480 m_close(childsock);
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
481 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
482
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
483 } /* for(;;) loop */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
484
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
485 /* don't reach here */
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
486 }
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
487 #endif /* INETD_SERVER_MODE */
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
488
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
489 /* catch + reap zombie children */
108
10f4d3319780 - added circular buffering for channels
Matt Johnston <matt@ucc.asn.au>
parents: 101
diff changeset
490 static void sigchld_handler(int UNUSED(unused)) {
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
491 struct sigaction sa_chld;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
492
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
493 while(waitpid(-1, NULL, WNOHANG) > 0);
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
494
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
495 sa_chld.sa_handler = sigchld_handler;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
496 sa_chld.sa_flags = SA_NOCLDSTOP;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
497 if (sigaction(SIGCHLD, &sa_chld, NULL) < 0) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
498 dropbear_exit("signal() error");
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
499 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
500 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
501
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
502 /* catch any segvs */
108
10f4d3319780 - added circular buffering for channels
Matt Johnston <matt@ucc.asn.au>
parents: 101
diff changeset
503 static void sigsegv_handler(int UNUSED(unused)) {
299
740e782679be Various changes to compile+kind of run on UCC's axis board.
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
504 printf( "Aiee, segfault! You should probably report "
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
505 "this as a bug to the developer\n");
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
506 exit(EXIT_FAILURE);
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
507 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
508
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
509 /* catch ctrl-c or sigterm */
108
10f4d3319780 - added circular buffering for channels
Matt Johnston <matt@ucc.asn.au>
parents: 101
diff changeset
510 static void sigintterm_handler(int UNUSED(unused)) {
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
511
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
512 exitflag = 1;
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
513 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
514
71
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
515 /* Things used by inetd and non-inetd modes */
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
516 static void commonsetup() {
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
517
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
518 struct sigaction sa_chld;
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
519 #ifndef DISABLE_SYSLOG
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
520 if (svr_opts.usingsyslog) {
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
521 startsyslog();
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
522 }
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
523 #endif
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
524
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
525 /* set up cleanup handler */
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
526 if (signal(SIGINT, sigintterm_handler) == SIG_ERR ||
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
527 #ifndef DEBUG_VALGRIND
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
528 signal(SIGTERM, sigintterm_handler) == SIG_ERR ||
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
529 #endif
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
530 signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
531 dropbear_exit("signal() error");
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
532 }
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
533
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
534 /* catch and reap zombie children */
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
535 sa_chld.sa_handler = sigchld_handler;
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
536 sa_chld.sa_flags = SA_NOCLDSTOP;
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
537 if (sigaction(SIGCHLD, &sa_chld, NULL) < 0) {
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
538 dropbear_exit("signal() error");
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
539 }
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
540 if (signal(SIGSEGV, sigsegv_handler) == SIG_ERR) {
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
541 dropbear_exit("signal() error");
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
542 }
113
775c6cbfe995 Load the hostkeys for inetd too - oops
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
543
775c6cbfe995 Load the hostkeys for inetd too - oops
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
544 /* Now we can setup the hostkeys - needs to be after logging is on,
775c6cbfe995 Load the hostkeys for inetd too - oops
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
545 * otherwise we might end up blatting error messages to the socket */
775c6cbfe995 Load the hostkeys for inetd too - oops
Matt Johnston <matt@ucc.asn.au>
parents: 108
diff changeset
546 loadhostkeys();
272
3be7ae2e8dfa Only read /dev/random once when the program starts
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
547
309
474c1a700b67 add inetd server mode
Matt Johnston <matt@ucc.asn.au>
parents: 302
diff changeset
548 seedrandom();
71
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
549 }
ac96bc733e71 adding inetd mode back from 0.43
Matt Johnston <matt@ucc.asn.au>
parents: 63
diff changeset
550
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
551 /* Set up listening sockets for all the requested ports */
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
552 static size_t listensockets(int *sock, size_t sockcount, int *maxfd) {
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
553
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554 unsigned int i;
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
555 char* errstring = NULL;
277
044bc108b9b3 * Per-IP connection unauthed connection limits
Matt Johnston <matt@ucc.asn.au>
parents: 258
diff changeset
556 size_t sockpos = 0;
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
557 int nsock;
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
558
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 158
diff changeset
559 TRACE(("listensockets: %d to try\n", svr_opts.portcount))
101
72dc22f56858 Change the way we load keys/ports so we don't print error messages into our
Matt Johnston <matt@ucc.asn.au>
parents: 71
diff changeset
560
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
561 for (i = 0; i < svr_opts.portcount; i++) {
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
562
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 158
diff changeset
563 TRACE(("listening on '%s'", svr_opts.ports[i]))
101
72dc22f56858 Change the way we load keys/ports so we don't print error messages into our
Matt Johnston <matt@ucc.asn.au>
parents: 71
diff changeset
564
258
306499676384 * add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents: 241
diff changeset
565 nsock = dropbear_listen("", svr_opts.ports[i], &sock[sockpos],
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
566 sockcount - sockpos,
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
567 &errstring, maxfd);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
568
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
569 if (nsock < 0) {
101
72dc22f56858 Change the way we load keys/ports so we don't print error messages into our
Matt Johnston <matt@ucc.asn.au>
parents: 71
diff changeset
570 dropbear_log(LOG_WARNING, "Failed listening on '%s': %s",
72dc22f56858 Change the way we load keys/ports so we don't print error messages into our
Matt Johnston <matt@ucc.asn.au>
parents: 71
diff changeset
571 svr_opts.ports[i], errstring);
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
572 m_free(errstring);
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
573 continue;
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
574 }
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
575
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
576 sockpos += nsock;
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
577
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
578 }
62
20563735e8b5 just checkpointing
Matt Johnston <matt@ucc.asn.au>
parents: 30
diff changeset
579 return sockpos;
30
223b0f5f8dce Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
580 }