/[LeafOK_CVS]/lbbs/src/net_server.c
ViewVC logotype

Contents of /lbbs/src/net_server.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.79 - (show annotations)
Wed Nov 5 04:19:21 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.78: +8 -5 lines
Content type: text/x-csrc
Use enum / const int instead of macro define constant integers
Use const char * instead of macro define for constant strings

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * net_server
4 * - network server with SSH support
5 *
6 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 */
8
9 #include "bbs.h"
10 #include "bbs_main.h"
11 #include "common.h"
12 #include "database.h"
13 #include "file_loader.h"
14 #include "io.h"
15 #include "init.h"
16 #include "log.h"
17 #include "login.h"
18 #include "menu.h"
19 #include "net_server.h"
20 #include "section_list.h"
21 #include "section_list_loader.h"
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <pty.h>
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <utmp.h>
30 #include <arpa/inet.h>
31 #include <libssh/callbacks.h>
32 #include <libssh/libssh.h>
33 #include <libssh/server.h>
34 #include <netinet/in.h>
35 #include <sys/epoll.h>
36 #include <sys/socket.h>
37 #include <sys/syscall.h>
38 #include <sys/types.h>
39 #include <sys/wait.h>
40 #include <systemd/sd-daemon.h>
41
42 enum _net_server_constant_t
43 {
44 WAIT_CHILD_PROCESS_EXIT_TIMEOUT = 5, // second
45 WAIT_CHILD_PROCESS_KILL_TIMEOUT = 1, // second
46
47 SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds
48 };
49
50 struct process_sockaddr_t
51 {
52 pid_t pid;
53 in_addr_t s_addr;
54 };
55 typedef struct process_sockaddr_t PROCESS_SOCKADDR;
56
57 static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];
58
59 static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server";
60
61 /* A userdata struct for session. */
62 struct session_data_struct
63 {
64 int tries;
65 int error;
66 };
67
68 /* A userdata struct for channel. */
69 struct channel_data_struct
70 {
71 /* pid of the child process the channel will spawn. */
72 pid_t pid;
73 /* For PTY allocation */
74 socket_t pty_master;
75 socket_t pty_slave;
76 /* For communication with the child process. */
77 socket_t child_stdin;
78 socket_t child_stdout;
79 /* Only used for subsystem and exec requests. */
80 socket_t child_stderr;
81 /* Event which is used to poll the above descriptors. */
82 ssh_event event;
83 /* Terminal size struct. */
84 struct winsize *winsize;
85 };
86
87 static int auth_password(ssh_session session, const char *user,
88 const char *password, void *userdata)
89 {
90 struct session_data_struct *sdata = (struct session_data_struct *)userdata;
91 int ret;
92
93 if (strcmp(user, "guest") == 0)
94 {
95 ret = load_guest_info();
96 }
97 else
98 {
99 ret = check_user(user, password);
100 }
101
102 if (ret == 0)
103 {
104 return SSH_AUTH_SUCCESS;
105 }
106
107 if ((++(sdata->tries)) >= BBS_login_retry_times)
108 {
109 sdata->error = 1;
110 }
111
112 return SSH_AUTH_DENIED;
113 }
114
115 static int pty_request(ssh_session session, ssh_channel channel, const char *term,
116 int cols, int rows, int px, int py, void *userdata)
117 {
118 struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
119 int rc;
120
121 (void)session;
122 (void)channel;
123 (void)term;
124
125 cdata->winsize->ws_row = (unsigned short int)rows;
126 cdata->winsize->ws_col = (unsigned short int)cols;
127 cdata->winsize->ws_xpixel = (unsigned short int)px;
128 cdata->winsize->ws_ypixel = (unsigned short int)py;
129
130 rc = openpty(&cdata->pty_master, &cdata->pty_slave, NULL, NULL, cdata->winsize);
131 if (rc != 0)
132 {
133 log_error("Failed to open pty\n");
134 return SSH_ERROR;
135 }
136
137 return SSH_OK;
138 }
139
140 static int pty_resize(ssh_session session, ssh_channel channel, int cols, int rows,
141 int py, int px, void *userdata)
142 {
143 struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
144
145 (void)session;
146 (void)channel;
147
148 cdata->winsize->ws_row = (unsigned short int)rows;
149 cdata->winsize->ws_col = (unsigned short int)cols;
150 cdata->winsize->ws_xpixel = (unsigned short int)px;
151 cdata->winsize->ws_ypixel = (unsigned short int)py;
152
153 if (cdata->pty_master != -1)
154 {
155 return ioctl(cdata->pty_master, TIOCSWINSZ, cdata->winsize);
156 }
157
158 return SSH_ERROR;
159 }
160
161 static int exec_pty(const char *mode, const char *command, struct channel_data_struct *cdata)
162 {
163 (void)cdata;
164
165 if (command != NULL)
166 {
167 log_error("Forbid exec /bin/sh %s %s)\n", mode, command);
168 }
169
170 return SSH_OK;
171 }
172
173 static int exec_nopty(const char *command, struct channel_data_struct *cdata)
174 {
175 (void)cdata;
176
177 if (command != NULL)
178 {
179 log_error("Forbid exec /bin/sh -c %s)\n", command);
180 }
181
182 return SSH_OK;
183 }
184
185 static int exec_request(ssh_session session, ssh_channel channel, const char *command, void *userdata)
186 {
187 struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
188
189 (void)session;
190 (void)channel;
191
192 if (cdata->pid > 0)
193 {
194 return SSH_ERROR;
195 }
196
197 if (cdata->pty_master != -1 && cdata->pty_slave != -1)
198 {
199 return exec_pty("-c", command, cdata);
200 }
201 return exec_nopty(command, cdata);
202 }
203
204 static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
205 {
206 struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
207
208 (void)session;
209 (void)channel;
210
211 if (cdata->pid > 0)
212 {
213 return SSH_ERROR;
214 }
215
216 if (cdata->pty_master != -1 && cdata->pty_slave != -1)
217 {
218 return exec_pty("-l", NULL, cdata);
219 }
220 /* Client requested a shell without a pty, let's pretend we allow that */
221 return SSH_OK;
222 }
223
224 static int subsystem_request(ssh_session session, ssh_channel channel, const char *subsystem, void *userdata)
225 {
226 (void)session;
227 (void)channel;
228
229 log_error("subsystem_request(subsystem=%s)\n", subsystem);
230
231 /* subsystem requests behave similarly to exec requests. */
232 if (strcmp(subsystem, "sftp") == 0)
233 {
234 return exec_request(session, channel, SFTP_SERVER_PATH, userdata);
235 }
236 return SSH_ERROR;
237 }
238
239 static ssh_channel channel_open(ssh_session session, void *userdata)
240 {
241 (void)userdata;
242
243 if (SSH_channel != NULL)
244 {
245 return NULL;
246 }
247
248 SSH_channel = ssh_channel_new(session);
249
250 return SSH_channel;
251 }
252
253 static int fork_server(void)
254 {
255 ssh_event event;
256 long int ssh_timeout = 0;
257 int pid;
258 int i;
259 int ret;
260
261 /* Structure for storing the pty size. */
262 struct winsize wsize = {
263 .ws_row = 0,
264 .ws_col = 0,
265 .ws_xpixel = 0,
266 .ws_ypixel = 0};
267
268 /* Our struct holding information about the channel. */
269 struct channel_data_struct cdata = {
270 .pid = 0,
271 .pty_master = -1,
272 .pty_slave = -1,
273 .child_stdin = -1,
274 .child_stdout = -1,
275 .child_stderr = -1,
276 .event = NULL,
277 .winsize = &wsize};
278
279 struct session_data_struct cb_data = {
280 .tries = 0,
281 .error = 0,
282 };
283
284 struct ssh_channel_callbacks_struct channel_cb = {
285 .userdata = &cdata,
286 .channel_pty_request_function = pty_request,
287 .channel_pty_window_change_function = pty_resize,
288 .channel_shell_request_function = shell_request,
289 .channel_exec_request_function = exec_request,
290 .channel_subsystem_request_function = subsystem_request};
291
292 struct ssh_server_callbacks_struct server_cb = {
293 .userdata = &cb_data,
294 .auth_password_function = auth_password,
295 .channel_open_request_session_function = channel_open,
296 };
297
298 pid = fork();
299
300 if (pid > 0) // Parent process
301 {
302 SYS_child_process_count++;
303 log_common("Child process (%d) start\n", pid);
304 return pid;
305 }
306 else if (pid < 0) // Error
307 {
308 log_error("fork() error (%d)\n", errno);
309 return -1;
310 }
311
312 // Child process
313
314 if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1)
315 {
316 log_error("Close server socket failed\n");
317 }
318
319 SSH_session = ssh_new();
320
321 if (SSH_v2)
322 {
323 if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK)
324 {
325 log_error("ssh_bind_accept_fd() error: %s\n", ssh_get_error(SSH_session));
326 goto cleanup;
327 }
328
329 ssh_bind_free(sshbind);
330
331 ssh_timeout = 60; // second
332 if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
333 {
334 log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
335 goto cleanup;
336 }
337
338 ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);
339
340 ssh_callbacks_init(&server_cb);
341 ssh_callbacks_init(&channel_cb);
342
343 ssh_set_server_callbacks(SSH_session, &server_cb);
344
345 if (ssh_handle_key_exchange(SSH_session))
346 {
347 log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));
348 goto cleanup;
349 }
350
351 event = ssh_event_new();
352 ssh_event_add_session(event, SSH_session);
353
354 for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i += 100)
355 {
356 ret = ssh_event_dopoll(event, 100); // 0.1 second
357 if (ret == SSH_ERROR)
358 {
359 #ifdef _DEBUG
360 log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));
361 #endif
362 goto cleanup;
363 }
364 }
365
366 if (cb_data.error)
367 {
368 log_error("SSH auth error, tried %d times\n", cb_data.tries);
369 goto cleanup;
370 }
371
372 ssh_set_channel_callbacks(SSH_channel, &channel_cb);
373
374 do
375 {
376 ret = ssh_event_dopoll(event, 100); // 0.1 second
377 if (ret == SSH_ERROR)
378 {
379 ssh_channel_close(SSH_channel);
380 }
381
382 if (ret == SSH_AGAIN) // loop until SSH connection is fully established
383 {
384 /* Executed only once, once the child process starts. */
385 cdata.event = event;
386 break;
387 }
388 } while (ssh_channel_is_open(SSH_channel));
389
390 ssh_timeout = 0;
391 if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
392 {
393 log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
394 goto cleanup;
395 }
396 }
397
398 // Redirect Input
399 close(STDIN_FILENO);
400 if (dup2(socket_client, STDIN_FILENO) == -1)
401 {
402 log_error("Redirect stdin to client socket failed\n");
403 goto cleanup;
404 }
405
406 // Redirect Output
407 close(STDOUT_FILENO);
408 if (dup2(socket_client, STDOUT_FILENO) == -1)
409 {
410 log_error("Redirect stdout to client socket failed\n");
411 goto cleanup;
412 }
413
414 SYS_child_process_count = 0;
415
416 bbs_main();
417
418 cleanup:
419 // Child process exit
420 SYS_server_exit = 1;
421
422 if (SSH_v2)
423 {
424 close(cdata.pty_master);
425 close(cdata.child_stdin);
426 close(cdata.child_stdout);
427 close(cdata.child_stderr);
428
429 ssh_channel_free(SSH_channel);
430 ssh_disconnect(SSH_session);
431 }
432 else if (close(socket_client) == -1)
433 {
434 log_error("Close client socket failed\n");
435 }
436
437 ssh_free(SSH_session);
438 ssh_finalize();
439
440 // Close Input and Output for client
441 close(STDIN_FILENO);
442 close(STDOUT_FILENO);
443
444 log_common("Process exit normally\n");
445 log_end();
446
447 _exit(0);
448
449 return 0;
450 }
451
452 int net_server(const char *hostaddr, in_port_t port[])
453 {
454 unsigned int addrlen;
455 int ret;
456 int flags[2];
457 struct sockaddr_in sin;
458 struct epoll_event ev, events[MAX_EVENTS];
459 int nfds, epollfd;
460 siginfo_t siginfo;
461 int notify_child_exit = 0;
462 time_t tm_notify_child_exit = time(NULL);
463 int sd_notify_stopping = 0;
464 MENU_SET bbs_menu_new;
465 MENU_SET top10_menu_new;
466 int i, j;
467 pid_t pid;
468 int ssh_log_level = SSH_LOG_NOLOG;
469
470 ssh_init();
471
472 sshbind = ssh_bind_new();
473
474 if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
475 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
476 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||
477 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 ||
478 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
479 {
480 log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));
481 ssh_bind_free(sshbind);
482 return -1;
483 }
484
485 epollfd = epoll_create1(0);
486 if (epollfd < 0)
487 {
488 log_error("epoll_create1() error (%d)\n", errno);
489 return -1;
490 }
491
492 // Server socket
493 for (i = 0; i < 2; i++)
494 {
495 socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
496
497 if (socket_server[i] < 0)
498 {
499 log_error("Create socket_server error (%d)\n", errno);
500 return -1;
501 }
502
503 sin.sin_family = AF_INET;
504 sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY);
505 sin.sin_port = htons(port[i]);
506
507 // Reuse address and port
508 flags[i] = 1;
509 if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags[i], sizeof(flags[i])) < 0)
510 {
511 log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);
512 }
513 if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags[i], sizeof(flags[i])) < 0)
514 {
515 log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);
516 }
517
518 if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0)
519 {
520 log_error("Bind address %s:%u error (%d)\n",
521 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);
522 return -1;
523 }
524
525 if (listen(socket_server[i], 10) < 0)
526 {
527 log_error("Telnet socket listen error (%d)\n", errno);
528 return -1;
529 }
530
531 log_common("Listening at %s:%u\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
532
533 ev.events = EPOLLIN;
534 ev.data.fd = socket_server[i];
535 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server[i], &ev) == -1)
536 {
537 log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno);
538 if (close(epollfd) < 0)
539 {
540 log_error("close(epoll) error (%d)\n");
541 }
542 return -1;
543 }
544
545 flags[i] = fcntl(socket_server[i], F_GETFL, 0);
546 fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);
547 }
548
549 // Startup complete
550 sd_notifyf(0, "READY=1\n"
551 "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"
552 "MAINPID=%d",
553 hostaddr, port[0], hostaddr, port[1], getpid());
554
555 while (!SYS_server_exit || SYS_child_process_count > 0)
556 {
557 if (SYS_server_exit && !sd_notify_stopping)
558 {
559 sd_notify(0, "STOPPING=1");
560 sd_notify_stopping = 1;
561 }
562
563 while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)
564 {
565 SYS_child_exit = 0;
566
567 siginfo.si_pid = 0;
568 ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG);
569 if (ret == 0 && siginfo.si_pid > 0)
570 {
571 SYS_child_exit = 1; // Retry waitid
572
573 SYS_child_process_count--;
574 log_common("Child process (%d) exited\n", siginfo.si_pid);
575
576 if (siginfo.si_pid != section_list_loader_pid)
577 {
578 i = 0;
579 for (; i < BBS_max_client; i++)
580 {
581 if (process_sockaddr_pool[i].pid == siginfo.si_pid)
582 {
583 process_sockaddr_pool[i].pid = 0;
584 break;
585 }
586 }
587 if (i >= BBS_max_client)
588 {
589 log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid);
590 }
591 }
592 }
593 else if (ret == 0)
594 {
595 break;
596 }
597 else if (ret < 0)
598 {
599 log_error("Error in waitid: %d\n", errno);
600 break;
601 }
602 }
603
604 if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
605 {
606 if (notify_child_exit == 0)
607 {
608 sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
609 log_common("Notify %d child process to exit\n", SYS_child_process_count);
610
611 if (kill(0, SIGTERM) < 0)
612 {
613 log_error("Send SIGTERM signal failed (%d)\n", errno);
614 }
615
616 notify_child_exit = 1;
617 tm_notify_child_exit = time(NULL);
618 }
619 else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)
620 {
621 sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
622
623 for (i = 0; i < BBS_max_client; i++)
624 {
625 if (process_sockaddr_pool[i].pid != 0)
626 {
627 log_error("Kill child process (pid=%d)\n", process_sockaddr_pool[i].pid);
628 if (kill(process_sockaddr_pool[i].pid, SIGKILL) < 0)
629 {
630 log_error("Send SIGKILL signal failed (%d)\n", errno);
631 }
632 }
633 }
634
635 notify_child_exit = 2;
636 tm_notify_child_exit = time(NULL);
637 }
638 else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT)
639 {
640 log_error("Main process prepare to exit without waiting for %d child process any longer\n", SYS_child_process_count);
641 SYS_child_process_count = 0;
642 }
643 }
644
645 if (SYS_conf_reload && !SYS_server_exit)
646 {
647 SYS_conf_reload = 0;
648 sd_notify(0, "RELOADING=1");
649
650 // Reload configuration
651 if (load_conf(CONF_BBSD) < 0)
652 {
653 log_error("Reload conf failed\n");
654 }
655
656 if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
657 {
658 unload_menu(&bbs_menu_new);
659 log_error("Reload bbs menu failed\n");
660 }
661 else
662 {
663 unload_menu(&bbs_menu);
664 memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
665 log_common("Reload bbs menu successfully\n");
666 }
667
668 if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)
669 {
670 unload_menu(&top10_menu_new);
671 log_error("Reload top10 menu failed\n");
672 }
673 else
674 {
675 unload_menu(&top10_menu);
676 top10_menu_new.allow_exit = 1;
677 memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new));
678 log_common("Reload top10 menu successfully\n");
679 }
680
681 for (int i = 0; i < data_files_load_startup_count; i++)
682 {
683 if (load_file(data_files_load_startup[i]) < 0)
684 {
685 log_error("load_file(%s) error\n", data_files_load_startup[i]);
686 }
687 }
688 log_common("Reload data files successfully\n");
689
690 // Load section config and gen_ex
691 if (load_section_config_from_db(1) < 0)
692 {
693 log_error("load_section_config_from_db(1) error\n");
694 }
695 else
696 {
697 log_common("Reload section config and gen_ex successfully\n");
698 }
699
700 sd_notify(0, "READY=1");
701 }
702
703 nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
704
705 if (nfds < 0)
706 {
707 if (errno != EINTR)
708 {
709 log_error("epoll_wait() error (%d)\n", errno);
710 break;
711 }
712 continue;
713 }
714
715 // Stop accept new connection on exit
716 if (SYS_server_exit)
717 {
718 continue;
719 }
720
721 for (int i = 0; i < nfds; i++)
722 {
723 if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1])
724 {
725 SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0);
726
727 while (!SYS_server_exit) // Accept all incoming connections until error
728 {
729 addrlen = sizeof(sin);
730 socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, &addrlen);
731 if (socket_client < 0)
732 {
733 if (errno == EAGAIN || errno == EWOULDBLOCK)
734 {
735 break;
736 }
737 else if (errno == EINTR)
738 {
739 continue;
740 }
741 else
742 {
743 log_error("accept(socket_server) error (%d)\n", errno);
744 break;
745 }
746 }
747
748 strncpy(hostaddr_client, inet_ntoa(sin.sin_addr), sizeof(hostaddr_client) - 1);
749 hostaddr_client[sizeof(hostaddr_client) - 1] = '\0';
750
751 port_client = ntohs(sin.sin_port);
752
753 log_common("Accept %s connection from %s:%d\n", (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client);
754
755 if (SYS_child_process_count - 1 < BBS_max_client)
756 {
757 j = 0;
758 for (i = 0; i < BBS_max_client; i++)
759 {
760 if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr)
761 {
762 j++;
763 if (j >= BBS_max_client_per_ip)
764 {
765 log_common("Too many client connections (%d) from %s\n", j, hostaddr_client);
766 break;
767 }
768 }
769 }
770
771 if (j < BBS_max_client_per_ip)
772 {
773 if ((pid = fork_server()) < 0)
774 {
775 log_error("fork_server() error\n");
776 }
777 else if (pid > 0)
778 {
779 i = 0;
780 for (; i < BBS_max_client; i++)
781 {
782 if (process_sockaddr_pool[i].pid == 0)
783 {
784 break;
785 }
786 }
787
788 if (i >= BBS_max_client)
789 {
790 log_error("Process sockaddr pool depleted\n");
791 }
792 else
793 {
794 process_sockaddr_pool[i].pid = pid;
795 process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr;
796 }
797 }
798 }
799 }
800 else
801 {
802 log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1);
803 }
804
805 if (close(socket_client) == -1)
806 {
807 log_error("close(socket_lient) error (%d)\n", errno);
808 }
809 }
810 }
811 }
812 }
813
814 if (close(epollfd) < 0)
815 {
816 log_error("close(epoll) error (%d)\n");
817 }
818
819 for (i = 0; i < 2; i++)
820 {
821 fcntl(socket_server[i], F_SETFL, flags[i]);
822
823 if (close(socket_server[i]) == -1)
824 {
825 log_error("Close server socket failed\n");
826 }
827 }
828
829 ssh_bind_free(sshbind);
830 ssh_finalize();
831
832 return 0;
833 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1