/[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.55 - (show annotations)
Sat Jun 7 01:51:54 2025 UTC (9 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.54: +210 -1 lines
Content type: text/x-csrc
Merge fork.c(.h) to net_server.c(.h)

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

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