Mercurial > dropbear
comparison main.c @ 24:469950e86d0f
switching to global vars
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 20 Jul 2004 12:05:00 +0000 |
parents | bc6477a6c393 |
children | 0969767bca0d |
comparison
equal
deleted
inserted
replaced
23:c896a4dd65da | 24:469950e86d0f |
---|---|
27 #include "session.h" | 27 #include "session.h" |
28 #include "buffer.h" | 28 #include "buffer.h" |
29 #include "signkey.h" | 29 #include "signkey.h" |
30 #include "runopts.h" | 30 #include "runopts.h" |
31 | 31 |
32 static int listensockets(int *sock, runopts * opts, int *maxfd); | 32 static int listensockets(int *sock, int *maxfd); |
33 static void sigchld_handler(int dummy); | 33 static void sigchld_handler(int dummy); |
34 static void sigsegv_handler(int); | 34 static void sigsegv_handler(int); |
35 static void sigintterm_handler(int fish); | 35 static void sigintterm_handler(int fish); |
36 | 36 |
37 static int childpipes[MAX_UNAUTH_CLIENTS]; | 37 static int childpipes[MAX_UNAUTH_CLIENTS]; |
51 int maxsock = -1; | 51 int maxsock = -1; |
52 struct sockaddr remoteaddr; | 52 struct sockaddr remoteaddr; |
53 int remoteaddrlen; | 53 int remoteaddrlen; |
54 int listensocks[MAX_LISTEN_ADDR]; | 54 int listensocks[MAX_LISTEN_ADDR]; |
55 unsigned int listensockcount = 0; | 55 unsigned int listensockcount = 0; |
56 runopts * opts; | |
57 FILE * pidfile; | 56 FILE * pidfile; |
58 | 57 |
59 int childsock; | 58 int childsock; |
60 pid_t childpid; | 59 pid_t childpid; |
61 int childpipe[2]; | 60 int childpipe[2]; |
64 | 63 |
65 _dropbear_exit = svr_dropbear_exit; | 64 _dropbear_exit = svr_dropbear_exit; |
66 _dropbear_log = svr_dropbear_log; | 65 _dropbear_log = svr_dropbear_log; |
67 | 66 |
68 /* get commandline options */ | 67 /* get commandline options */ |
69 opts = svr_getopts(argc, argv); | 68 svr_getopts(argc, argv); |
70 | 69 |
71 /* fork */ | 70 /* fork */ |
72 if (opts->forkbg) { | 71 if (svr_opts.forkbg) { |
73 int closefds = 0; | 72 int closefds = 0; |
74 #ifndef DEBUG_TRACE | 73 #ifndef DEBUG_TRACE |
75 if (!usingsyslog) { | 74 if (!svr_opts.usingsyslog) { |
76 closefds = 1; | 75 closefds = 1; |
77 } | 76 } |
78 #endif | 77 #endif |
79 if (daemon(0, closefds) < 0) { | 78 if (daemon(0, closefds) < 0) { |
80 dropbear_exit("Failed to create background process: %s", | 79 dropbear_exit("Failed to create background process: %s", |
81 strerror(errno)); | 80 strerror(errno)); |
82 } | 81 } |
83 } | 82 } |
84 | 83 |
85 #ifndef DISABLE_SYSLOG | 84 #ifndef DISABLE_SYSLOG |
86 if (usingsyslog) { | 85 if (svr_opts.usingsyslog) { |
87 startsyslog(); | 86 startsyslog(); |
88 } | 87 } |
89 #endif | 88 #endif |
90 | 89 |
91 /* should be done after syslog is working */ | 90 /* should be done after syslog is working */ |
92 if (opts->forkbg) { | 91 if (svr_opts.forkbg) { |
93 dropbear_log(LOG_INFO, "Running in background"); | 92 dropbear_log(LOG_INFO, "Running in background"); |
94 } else { | 93 } else { |
95 dropbear_log(LOG_INFO, "Not forking"); | 94 dropbear_log(LOG_INFO, "Not forking"); |
96 } | 95 } |
97 | 96 |
126 childpipes[i] = -1; | 125 childpipes[i] = -1; |
127 } | 126 } |
128 | 127 |
129 /* Set up the listening sockets */ | 128 /* Set up the listening sockets */ |
130 /* XXX XXX ports */ | 129 /* XXX XXX ports */ |
131 listensockcount = listensockets(listensocks, opts, &maxsock); | 130 listensockcount = listensockets(listensocks, &maxsock); |
132 | 131 |
133 /* incoming connection select loop */ | 132 /* incoming connection select loop */ |
134 for(;;) { | 133 for(;;) { |
135 | 134 |
136 FD_ZERO(&fds); | 135 FD_ZERO(&fds); |
240 | 239 |
241 if (m_close(childpipe[0]) == DROPBEAR_FAILURE) { | 240 if (m_close(childpipe[0]) == DROPBEAR_FAILURE) { |
242 dropbear_exit("Couldn't close socket"); | 241 dropbear_exit("Couldn't close socket"); |
243 } | 242 } |
244 /* start the session */ | 243 /* start the session */ |
245 svr_session(childsock, opts, childpipe[1], &remoteaddr); | 244 svr_session(childsock, childpipe[1], &remoteaddr); |
246 /* don't return */ | 245 /* don't return */ |
247 assert(0); | 246 assert(0); |
248 } | 247 } |
249 | 248 |
250 /* parent */ | 249 /* parent */ |
286 | 285 |
287 exitflag = 1; | 286 exitflag = 1; |
288 } | 287 } |
289 | 288 |
290 /* Set up listening sockets for all the requested ports */ | 289 /* Set up listening sockets for all the requested ports */ |
291 static int listensockets(int *sock, runopts * opts, int *maxfd) { | 290 static int listensockets(int *sock, int *maxfd) { |
292 | 291 |
293 int listensock; /* listening fd */ | 292 int listensock; /* listening fd */ |
294 struct sockaddr_in listen_addr; | 293 struct sockaddr_in listen_addr; |
295 struct linger linger; | 294 struct linger linger; |
296 unsigned int i; | 295 unsigned int i; |
297 int val; | 296 int val; |
298 | 297 |
299 for (i = 0; i < opts->portcount; i++) { | 298 for (i = 0; i < svr_opts.portcount; i++) { |
300 | 299 |
301 /* iterate through all the sockets to listen on */ | 300 /* iterate through all the sockets to listen on */ |
302 listensock = socket(PF_INET, SOCK_STREAM, 0); | 301 listensock = socket(PF_INET, SOCK_STREAM, 0); |
303 if (listensock < 0) { | 302 if (listensock < 0) { |
304 dropbear_exit("Failed to create socket"); | 303 dropbear_exit("Failed to create socket"); |
317 setsockopt(listensock, IPPROTO_TCP, TCP_NODELAY, | 316 setsockopt(listensock, IPPROTO_TCP, TCP_NODELAY, |
318 (void*)&val, sizeof(val)); | 317 (void*)&val, sizeof(val)); |
319 | 318 |
320 memset((void*)&listen_addr, 0x0, sizeof(listen_addr)); | 319 memset((void*)&listen_addr, 0x0, sizeof(listen_addr)); |
321 listen_addr.sin_family = AF_INET; | 320 listen_addr.sin_family = AF_INET; |
322 listen_addr.sin_port = htons(opts->ports[i]); | 321 listen_addr.sin_port = htons(svr_opts.ports[i]); |
323 listen_addr.sin_addr.s_addr = htonl(INADDR_ANY); | 322 listen_addr.sin_addr.s_addr = htonl(INADDR_ANY); |
324 memset(&(listen_addr.sin_zero), '\0', 8); | 323 memset(&(listen_addr.sin_zero), '\0', 8); |
325 | 324 |
326 if (bind(listensock, (struct sockaddr *)&listen_addr, | 325 if (bind(listensock, (struct sockaddr *)&listen_addr, |
327 sizeof(listen_addr)) < 0) { | 326 sizeof(listen_addr)) < 0) { |
328 dropbear_exit("Bind failed port %d", opts->ports[i]); | 327 dropbear_exit("Bind failed port %d", svr_opts.ports[i]); |
329 } | 328 } |
330 | 329 |
331 /* listen */ | 330 /* listen */ |
332 if (listen(listensock, 20) < 0) { /* TODO set listen count */ | 331 if (listen(listensock, 20) < 0) { /* TODO set listen count */ |
333 dropbear_exit("Listen failed"); | 332 dropbear_exit("Listen failed"); |
340 | 339 |
341 sock[i] = listensock; | 340 sock[i] = listensock; |
342 *maxfd = MAX(listensock, *maxfd); | 341 *maxfd = MAX(listensock, *maxfd); |
343 } | 342 } |
344 | 343 |
345 return opts->portcount; | 344 return svr_opts.portcount; |
346 } | 345 } |