/[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.73 - (show annotations)
Sun Oct 19 01:30:38 2025 UTC (4 months, 4 weeks ago) by sysadm
Branch: MAIN
Changes since 1.72: +2 -0 lines
Content type: text/x-csrc
Copy terminal control sequence directly without invoking iconv()

1 /***************************************************************************
2 net_server.c - description
3 -------------------
4 Copyright : (C) 2004-2025 by Leaflet
5 Email : leaflet@leafok.com
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 3 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17 #include "bbs.h"
18 #include "bbs_main.h"
19 #include "common.h"
20 #include "database.h"
21 #include "file_loader.h"
22 #include "io.h"
23 #include "init.h"
24 #include "log.h"
25 #include "login.h"
26 #include "menu.h"
27 #include "net_server.h"
28 #include "section_list.h"
29 #include "section_list_loader.h"
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <signal.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <arpa/inet.h>
37 #include <libssh/callbacks.h>
38 #include <libssh/libssh.h>
39 #include <libssh/server.h>
40 #include <netinet/in.h>
41 #include <sys/epoll.h>
42 #include <sys/socket.h>
43 #include <sys/syscall.h>
44 #include <sys/types.h>
45 #include <sys/wait.h>
46 #include <systemd/sd-daemon.h>
47
48 #define WAIT_CHILD_PROCESS_EXIT_TIMEOUT 5 // second
49 #define WAIT_CHILD_PROCESS_KILL_TIMEOUT 1 // second
50
51 struct process_sockaddr_t
52 {
53 pid_t pid;
54 in_addr_t s_addr;
55 };
56 typedef struct process_sockaddr_t PROCESS_SOCKADDR;
57
58 static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];
59
60 #define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds
61
62 struct ssl_server_cb_data_t
63 {
64 int tries;
65 int error;
66 };
67
68 static int auth_password(ssh_session session, const char *user,
69 const char *password, void *userdata)
70 {
71 struct ssl_server_cb_data_t *p_data = userdata;
72 int ret;
73
74 if (strcmp(user, "guest") == 0)
75 {
76 ret = load_guest_info();
77 }
78 else
79 {
80 ret = check_user(user, password);
81 }
82
83 if (ret == 0)
84 {
85 return SSH_AUTH_SUCCESS;
86 }
87
88 if ((++(p_data->tries)) >= BBS_login_retry_times)
89 {
90 p_data->error = 1;
91 }
92
93 return SSH_AUTH_DENIED;
94 }
95
96 static int pty_request(ssh_session session, ssh_channel channel, const char *term,
97 int x, int y, int px, int py, void *userdata)
98 {
99 return 0;
100 }
101
102 static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
103 {
104 return 0;
105 }
106
107 static struct ssh_channel_callbacks_struct channel_cb = {
108 .channel_pty_request_function = pty_request,
109 .channel_shell_request_function = shell_request};
110
111 static ssh_channel new_session_channel(ssh_session session, void *userdata)
112 {
113 if (SSH_channel != NULL)
114 {
115 return NULL;
116 }
117
118 SSH_channel = ssh_channel_new(session);
119 ssh_callbacks_init(&channel_cb);
120 ssh_set_channel_callbacks(SSH_channel, &channel_cb);
121
122 return SSH_channel;
123 }
124
125 static int fork_server(void)
126 {
127 ssh_event event;
128 long int ssh_timeout = 0;
129 int pid;
130 int i;
131 int ret;
132
133 struct ssl_server_cb_data_t cb_data = {
134 .tries = 0,
135 .error = 0,
136 };
137
138 struct ssh_server_callbacks_struct cb = {
139 .userdata = &cb_data,
140 .auth_password_function = auth_password,
141 .channel_open_request_session_function = new_session_channel,
142 };
143
144 pid = fork();
145
146 if (pid > 0) // Parent process
147 {
148 SYS_child_process_count++;
149 log_common("Child process (%d) start\n", pid);
150 return pid;
151 }
152 else if (pid < 0) // Error
153 {
154 log_error("fork() error (%d)\n", errno);
155 return -1;
156 }
157
158 // Child process
159
160 if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1)
161 {
162 log_error("Close server socket failed\n");
163 }
164
165 SSH_session = ssh_new();
166
167 if (SSH_v2)
168 {
169 if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK)
170 {
171 log_error("ssh_bind_accept_fd() error: %s\n", ssh_get_error(SSH_session));
172 goto cleanup;
173 }
174
175 ssh_bind_free(sshbind);
176
177 ssh_callbacks_init(&cb);
178 ssh_set_server_callbacks(SSH_session, &cb);
179
180 ssh_timeout = 60; // second
181 if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
182 {
183 log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
184 goto cleanup;
185 }
186
187 if (ssh_handle_key_exchange(SSH_session))
188 {
189 log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));
190 goto cleanup;
191 }
192 ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);
193
194 event = ssh_event_new();
195 ssh_event_add_session(event, SSH_session);
196
197 for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i += 100)
198 {
199 ret = ssh_event_dopoll(event, 100); // 0.1 second
200 if (ret == SSH_ERROR)
201 {
202 #ifdef _DEBUG
203 log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));
204 #endif
205 goto cleanup;
206 }
207 }
208
209 if (cb_data.error)
210 {
211 log_error("SSH auth error, tried %d times\n", cb_data.tries);
212 goto cleanup;
213 }
214
215 ssh_timeout = 0;
216 if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
217 {
218 log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
219 goto cleanup;
220 }
221 }
222
223 // Redirect Input
224 close(STDIN_FILENO);
225 if (dup2(socket_client, STDIN_FILENO) == -1)
226 {
227 log_error("Redirect stdin to client socket failed\n");
228 goto cleanup;
229 }
230
231 // Redirect Output
232 close(STDOUT_FILENO);
233 if (dup2(socket_client, STDOUT_FILENO) == -1)
234 {
235 log_error("Redirect stdout to client socket failed\n");
236 goto cleanup;
237 }
238
239 SYS_child_process_count = 0;
240
241 bbs_main();
242
243 cleanup:
244 // Child process exit
245 SYS_server_exit = 1;
246
247 if (SSH_v2)
248 {
249 ssh_channel_free(SSH_channel);
250 ssh_disconnect(SSH_session);
251 }
252 else if (close(socket_client) == -1)
253 {
254 log_error("Close client socket failed\n");
255 }
256
257 ssh_free(SSH_session);
258 ssh_finalize();
259
260 // Close Input and Output for client
261 close(STDIN_FILENO);
262 close(STDOUT_FILENO);
263
264 log_common("Process exit normally\n");
265 log_end();
266
267 _exit(0);
268
269 return 0;
270 }
271
272 int net_server(const char *hostaddr, in_port_t port[])
273 {
274 unsigned int addrlen;
275 int ret;
276 int flags[2];
277 struct sockaddr_in sin;
278 struct epoll_event ev, events[MAX_EVENTS];
279 int nfds, epollfd;
280 siginfo_t siginfo;
281 int notify_child_exit = 0;
282 time_t tm_notify_child_exit = time(NULL);
283 int sd_notify_stopping = 0;
284 MENU_SET bbs_menu_new;
285 MENU_SET top10_menu_new;
286 int i, j;
287 pid_t pid;
288 int ssh_log_level = SSH_LOG_NOLOG;
289
290 ssh_init();
291
292 sshbind = ssh_bind_new();
293
294 if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
295 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
296 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||
297 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 ||
298 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
299 {
300 log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));
301 ssh_bind_free(sshbind);
302 return -1;
303 }
304
305 epollfd = epoll_create1(0);
306 if (epollfd < 0)
307 {
308 log_error("epoll_create1() error (%d)\n", errno);
309 return -1;
310 }
311
312 // Server socket
313 for (i = 0; i < 2; i++)
314 {
315 socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
316
317 if (socket_server[i] < 0)
318 {
319 log_error("Create socket_server error (%d)\n", errno);
320 return -1;
321 }
322
323 sin.sin_family = AF_INET;
324 sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY);
325 sin.sin_port = htons(port[i]);
326
327 // Reuse address and port
328 flags[i] = 1;
329 if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags[i], sizeof(flags[i])) < 0)
330 {
331 log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);
332 }
333 if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags[i], sizeof(flags[i])) < 0)
334 {
335 log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);
336 }
337
338 if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0)
339 {
340 log_error("Bind address %s:%u error (%d)\n",
341 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);
342 return -1;
343 }
344
345 if (listen(socket_server[i], 10) < 0)
346 {
347 log_error("Telnet socket listen error (%d)\n", errno);
348 return -1;
349 }
350
351 log_common("Listening at %s:%u\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
352
353 ev.events = EPOLLIN;
354 ev.data.fd = socket_server[i];
355 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server[i], &ev) == -1)
356 {
357 log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno);
358 if (close(epollfd) < 0)
359 {
360 log_error("close(epoll) error (%d)\n");
361 }
362 return -1;
363 }
364
365 flags[i] = fcntl(socket_server[i], F_GETFL, 0);
366 fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);
367 }
368
369 // Startup complete
370 sd_notifyf(0, "READY=1\n"
371 "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"
372 "MAINPID=%d",
373 hostaddr, port[0], hostaddr, port[1], getpid());
374
375 while (!SYS_server_exit || SYS_child_process_count > 0)
376 {
377 if (SYS_server_exit && !sd_notify_stopping)
378 {
379 sd_notify(0, "STOPPING=1");
380 sd_notify_stopping = 1;
381 }
382
383 while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)
384 {
385 SYS_child_exit = 0;
386
387 siginfo.si_pid = 0;
388 ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG);
389 if (ret == 0 && siginfo.si_pid > 0)
390 {
391 SYS_child_exit = 1; // Retry waitid
392
393 SYS_child_process_count--;
394 log_common("Child process (%d) exited\n", siginfo.si_pid);
395
396 if (siginfo.si_pid != section_list_loader_pid)
397 {
398 i = 0;
399 for (; i < BBS_max_client; i++)
400 {
401 if (process_sockaddr_pool[i].pid == siginfo.si_pid)
402 {
403 process_sockaddr_pool[i].pid = 0;
404 break;
405 }
406 }
407 if (i >= BBS_max_client)
408 {
409 log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid);
410 }
411 }
412 }
413 else if (ret == 0)
414 {
415 break;
416 }
417 else if (ret < 0)
418 {
419 log_error("Error in waitid: %d\n", errno);
420 break;
421 }
422 }
423
424 if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
425 {
426 if (notify_child_exit == 0)
427 {
428 sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
429 log_common("Notify %d child process to exit\n", SYS_child_process_count);
430
431 if (kill(0, SIGTERM) < 0)
432 {
433 log_error("Send SIGTERM signal failed (%d)\n", errno);
434 }
435
436 notify_child_exit = 1;
437 tm_notify_child_exit = time(NULL);
438 }
439 else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)
440 {
441 sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
442
443 for (i = 0; i < BBS_max_client; i++)
444 {
445 if (process_sockaddr_pool[i].pid != 0)
446 {
447 log_error("Kill child process (pid=%d)\n", process_sockaddr_pool[i].pid);
448 if (kill(process_sockaddr_pool[i].pid, SIGKILL) < 0)
449 {
450 log_error("Send SIGKILL signal failed (%d)\n", errno);
451 }
452 }
453 }
454
455 notify_child_exit = 2;
456 tm_notify_child_exit = time(NULL);
457 }
458 else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT)
459 {
460 log_error("Main process prepare to exit without waiting for %d child process any longer\n", SYS_child_process_count);
461 SYS_child_process_count = 0;
462 }
463 }
464
465 if (SYS_conf_reload && !SYS_server_exit)
466 {
467 SYS_conf_reload = 0;
468 sd_notify(0, "RELOADING=1");
469
470 // Reload configuration
471 if (load_conf(CONF_BBSD) < 0)
472 {
473 log_error("Reload conf failed\n");
474 }
475
476 if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
477 {
478 unload_menu(&bbs_menu_new);
479 log_error("Reload bbs menu failed\n");
480 }
481 else
482 {
483 unload_menu(&bbs_menu);
484 memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
485 log_common("Reload bbs menu successfully\n");
486 }
487
488 if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)
489 {
490 unload_menu(&top10_menu_new);
491 log_error("Reload top10 menu failed\n");
492 }
493 else
494 {
495 unload_menu(&top10_menu);
496 top10_menu_new.allow_exit = 1;
497 memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new));
498 log_common("Reload top10 menu successfully\n");
499 }
500
501 for (int i = 0; i < data_files_load_startup_count; i++)
502 {
503 if (load_file(data_files_load_startup[i]) < 0)
504 {
505 log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);
506 }
507 }
508 log_common("Reload data files successfully\n");
509
510 // Load section config and gen_ex
511 if (load_section_config_from_db(1) < 0)
512 {
513 log_error("load_section_config_from_db(1) error\n");
514 }
515 else
516 {
517 log_common("Reload section config and gen_ex successfully\n");
518 }
519
520 sd_notify(0, "READY=1");
521 }
522
523 nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
524
525 if (nfds < 0)
526 {
527 if (errno != EINTR)
528 {
529 log_error("epoll_wait() error (%d)\n", errno);
530 break;
531 }
532 continue;
533 }
534
535 // Stop accept new connection on exit
536 if (SYS_server_exit)
537 {
538 continue;
539 }
540
541 for (int i = 0; i < nfds; i++)
542 {
543 if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1])
544 {
545 SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0);
546
547 while (!SYS_server_exit) // Accept all incoming connections until error
548 {
549 addrlen = sizeof(sin);
550 socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, &addrlen);
551 if (socket_client < 0)
552 {
553 if (errno == EAGAIN || errno == EWOULDBLOCK)
554 {
555 break;
556 }
557 else if (errno == EINTR)
558 {
559 continue;
560 }
561 else
562 {
563 log_error("accept(socket_server) error (%d)\n", errno);
564 break;
565 }
566 }
567
568 strncpy(hostaddr_client, inet_ntoa(sin.sin_addr), sizeof(hostaddr_client) - 1);
569 hostaddr_client[sizeof(hostaddr_client) - 1] = '\0';
570
571 port_client = ntohs(sin.sin_port);
572
573 log_common("Accept %s connection from %s:%d\n", (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client);
574
575 if (SYS_child_process_count - 1 < BBS_max_client)
576 {
577 j = 0;
578 for (i = 0; i < BBS_max_client; i++)
579 {
580 if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr)
581 {
582 j++;
583 if (j >= BBS_max_client_per_ip)
584 {
585 log_common("Too many client connections (%d) from %s\n", j, hostaddr_client);
586 break;
587 }
588 }
589 }
590
591 if (j < BBS_max_client_per_ip)
592 {
593 if ((pid = fork_server()) < 0)
594 {
595 log_error("fork_server() error\n");
596 }
597 else if (pid > 0)
598 {
599 i = 0;
600 for (; i < BBS_max_client; i++)
601 {
602 if (process_sockaddr_pool[i].pid == 0)
603 {
604 break;
605 }
606 }
607
608 if (i >= BBS_max_client)
609 {
610 log_error("Process sockaddr pool depleted\n");
611 }
612 else
613 {
614 process_sockaddr_pool[i].pid = pid;
615 process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr;
616 }
617 }
618 }
619 }
620 else
621 {
622 log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1);
623 }
624
625 if (close(socket_client) == -1)
626 {
627 log_error("close(socket_lient) error (%d)\n", errno);
628 }
629 }
630 }
631 }
632 }
633
634 if (close(epollfd) < 0)
635 {
636 log_error("close(epoll) error (%d)\n");
637 }
638
639 for (i = 0; i < 2; i++)
640 {
641 fcntl(socket_server[i], F_SETFL, flags[i]);
642
643 if (close(socket_server[i]) == -1)
644 {
645 log_error("Close server socket failed\n");
646 }
647 }
648
649 ssh_bind_free(sshbind);
650 ssh_finalize();
651
652 return 0;
653 }

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