Mercurial > dropbear
annotate svr-main.c @ 63:dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
- still a checkpoint-ish commit
- sorted out listening on localhost only
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 11 Aug 2004 17:26:47 +0000 |
parents | 20563735e8b5 |
children | ac96bc733e71 |
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 * |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
4 * Copyright (c) 2002,2003 Matt Johnston |
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" |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
31 |
62 | 32 static int listensockets(int *sock, int sockcount, int *maxfd); |
30
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
33 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
|
34 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
|
35 static void sigintterm_handler(int fish); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
36 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
37 static int childpipes[MAX_UNAUTH_CLIENTS]; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
38 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
39 #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
|
40 #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
|
41 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
|
42 #else |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
43 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
|
44 #endif |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
45 { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
46 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
47 fd_set fds; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
48 struct timeval seltimeout; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
49 unsigned int i, j; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
50 int val; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
51 int maxsock = -1; |
62 | 52 struct sockaddr_storage remoteaddr; |
30
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
53 int remoteaddrlen; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
54 int listensocks[MAX_LISTEN_ADDR]; |
62 | 55 int listensockcount = 0; |
30
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
56 FILE * pidfile; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
57 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
58 int childsock; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
59 pid_t childpid; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
60 int childpipe[2]; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
61 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
62 struct sigaction sa_chld; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
63 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
64 _dropbear_exit = svr_dropbear_exit; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
65 _dropbear_log = svr_dropbear_log; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
66 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
67 /* get commandline options */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
68 svr_getopts(argc, argv); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
69 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
70 /* fork */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
71 if (svr_opts.forkbg) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
72 int closefds = 0; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
73 #ifndef DEBUG_TRACE |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
74 if (!svr_opts.usingsyslog) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
75 closefds = 1; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
76 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
77 #endif |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
78 if (daemon(0, closefds) < 0) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
79 dropbear_exit("Failed to create background process: %s", |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
80 strerror(errno)); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
81 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
82 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
83 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
84 #ifndef DISABLE_SYSLOG |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
85 if (svr_opts.usingsyslog) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
86 startsyslog(); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
87 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
88 #endif |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
89 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
90 /* 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
|
91 if (svr_opts.forkbg) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
92 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
|
93 } else { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
94 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
|
95 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
96 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
97 /* 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
|
98 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
|
99 if (pidfile) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
100 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
|
101 fclose(pidfile); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
102 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
103 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
104 /* set up cleanup handler */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
105 if (signal(SIGINT, sigintterm_handler) == SIG_ERR || |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
106 #ifndef DEBUG_VALGRIND |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
107 signal(SIGTERM, sigintterm_handler) == SIG_ERR || |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
108 #endif |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
109 signal(SIGPIPE, SIG_IGN) == SIG_ERR) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
110 dropbear_exit("signal() error"); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
111 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
112 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
113 /* catch and reap zombie children */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
114 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
|
115 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
|
116 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
|
117 dropbear_exit("signal() error"); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
118 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
119 if (signal(SIGSEGV, sigsegv_handler) == SIG_ERR) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
120 dropbear_exit("signal() error"); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
121 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
122 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
123 /* 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
|
124 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
|
125 childpipes[i] = -1; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
126 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
127 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
128 /* Set up the listening sockets */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
129 /* XXX XXX ports */ |
62 | 130 listensockcount = listensockets(listensocks, MAX_LISTEN_ADDR, &maxsock); |
131 if (listensockcount < 0) { | |
132 dropbear_exit("No listening ports available."); | |
133 } | |
30
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
134 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
135 /* incoming connection select loop */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
136 for(;;) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
137 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
138 FD_ZERO(&fds); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
139 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
140 seltimeout.tv_sec = 60; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
141 seltimeout.tv_usec = 0; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
142 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
143 /* listening sockets */ |
62 | 144 for (i = 0; i < (unsigned int)listensockcount; i++) { |
30
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
145 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
|
146 } |
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 /* pre-authentication clients */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
149 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
|
150 if (childpipes[i] >= 0) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
151 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
|
152 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
|
153 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
154 } |
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 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
|
157 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
158 if (exitflag) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
159 unlink(DROPBEAR_PIDFILE); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
160 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
|
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 if (val == 0) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
164 /* timeout reached */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
165 continue; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
166 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
167 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
168 if (val < 0) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
169 if (errno == EINTR) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
170 continue; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
171 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
172 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
|
173 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
174 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
175 /* close fds which have been authed or closed - auth.c handles |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
176 * 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
|
177 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
|
178 if (childpipes[i] >= 0 && FD_ISSET(childpipes[i], &fds)) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
179 close(childpipes[i]); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
180 childpipes[i] = -1; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
181 } |
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 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
184 /* handle each socket which has something to say */ |
62 | 185 for (i = 0; i < (unsigned int)listensockcount; i++) { |
30
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
186 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
|
187 continue; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
188 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
189 /* child connection XXX - ip6 stuff here */ |
62 | 190 remoteaddrlen = sizeof(remoteaddr); |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
191 childsock = accept(listensocks[i], |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
192 (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
|
193 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
194 if (childsock < 0) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
195 /* accept failed */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
196 continue; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
197 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
198 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
199 /* check for max number of connections not authorised */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
200 for (j = 0; j < MAX_UNAUTH_CLIENTS; j++) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
201 if (childpipes[j] < 0) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
202 break; |
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 } |
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 (j == MAX_UNAUTH_CLIENTS) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
207 /* no free connections */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
208 /* TODO - possibly log, though this would be an easy way |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
209 * to fill logs/disk */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
210 close(childsock); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
211 continue; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
212 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
213 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
214 if (pipe(childpipe) < 0) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
215 TRACE(("error creating child pipe")); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
216 close(childsock); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
217 continue; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
218 } |
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 if ((childpid = fork()) == 0) { |
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 /* child */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
223 char * addrstring = NULL; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
224 #ifdef DEBUG_FORKGPROF |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
225 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
|
226 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
|
227 #endif /* DEBUG_FORKGPROF */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
228 |
62 | 229 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
|
230 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
|
231 m_free(addrstring); |
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 if (setsid() < 0) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
234 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
|
235 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
236 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
237 /* make sure we close sockets */ |
62 | 238 for (i = 0; i < (unsigned int)listensockcount; i++) { |
30
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
239 if (m_close(listensocks[i]) == DROPBEAR_FAILURE) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
240 dropbear_exit("Couldn't close socket"); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
241 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
242 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
243 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
244 if (m_close(childpipe[0]) == DROPBEAR_FAILURE) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
245 dropbear_exit("Couldn't close socket"); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
246 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
247 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
248 /* start the session */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
249 svr_session(childsock, childpipe[1], |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
250 getaddrhostname(&remoteaddr)); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
251 /* don't return */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
252 assert(0); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
253 } |
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 /* parent */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
256 childpipes[j] = childpipe[0]; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
257 if (m_close(childpipe[1]) == DROPBEAR_FAILURE |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
258 || m_close(childsock) == DROPBEAR_FAILURE) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
259 dropbear_exit("Couldn't close socket"); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
260 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
261 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
262 } /* for(;;) loop */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
263 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
264 /* don't reach here */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
265 return -1; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
266 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
267 #endif |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
268 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
269 /* catch + reap zombie children */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
270 static void sigchld_handler(int fish) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
271 struct sigaction sa_chld; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
272 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
273 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
|
274 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
275 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
|
276 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
|
277 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
|
278 dropbear_exit("signal() error"); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
279 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
280 } |
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 /* catch any segvs */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
283 static void sigsegv_handler(int fish) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
284 fprintf(stderr, "Aiee, segfault! You should probably report " |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
285 "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
|
286 exit(EXIT_FAILURE); |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
287 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
288 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
289 /* catch ctrl-c or sigterm */ |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
290 static void sigintterm_handler(int fish) { |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
291 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
292 exitflag = 1; |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
293 } |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
294 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
295 /* Set up listening sockets for all the requested ports */ |
62 | 296 static int listensockets(int *sock, int sockcount, int *maxfd) { |
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 unsigned int i; |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
299 char portstring[NI_MAXSERV]; |
62 | 300 char* errstring = NULL; |
301 unsigned int sockpos = 0; | |
302 int nsock; | |
30
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
303 |
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
304 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
|
305 |
62 | 306 snprintf(portstring, sizeof(portstring), "%d", svr_opts.ports[i]); |
307 nsock = dropbear_listen(NULL, portstring, &sock[sockpos], | |
308 sockcount - sockpos, | |
309 &errstring, maxfd); | |
310 | |
311 if (nsock < 0) { | |
312 dropbear_log(LOG_WARNING, "Failed listening on port %s: %s", | |
313 portstring, errstring); | |
314 m_free(errstring); | |
315 continue; | |
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 |
62 | 318 sockpos += nsock; |
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 } |
62 | 321 return sockpos; |
30
223b0f5f8dce
Switching to the magical new Makefile, and new dbmulti style
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
322 } |