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

Annotation of /lbbs/src/net_server.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.86 - (hide annotations)
Sun Nov 16 13:18:30 2025 UTC (3 months, 4 weeks ago) by sysadm
Branch: MAIN
Changes since 1.85: +19 -1 lines
Content type: text/x-csrc
Add options to ./configure
  --with-mariadb
  --enable-systemd

1 sysadm 1.77 /* SPDX-License-Identifier: GPL-3.0-or-later */
2     /*
3     * net_server
4     * - network server with SSH support
5     *
6 sysadm 1.78 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 sysadm 1.77 */
8 sysadm 1.1
9 sysadm 1.81 #ifdef HAVE_CONFIG_H
10     #include "config.h"
11     #endif
12    
13 sysadm 1.62 #include "bbs.h"
14     #include "bbs_main.h"
15 sysadm 1.80 #include "bwf.h"
16 sysadm 1.1 #include "common.h"
17 sysadm 1.62 #include "database.h"
18     #include "file_loader.h"
19 sysadm 1.82 #include "hash_dict.h"
20 sysadm 1.7 #include "io.h"
21 sysadm 1.44 #include "init.h"
22 sysadm 1.62 #include "log.h"
23     #include "login.h"
24 sysadm 1.19 #include "menu.h"
25 sysadm 1.62 #include "net_server.h"
26 sysadm 1.67 #include "section_list.h"
27 sysadm 1.42 #include "section_list_loader.h"
28 sysadm 1.20 #include <errno.h>
29     #include <fcntl.h>
30 sysadm 1.74 #include <pty.h>
31 sysadm 1.19 #include <signal.h>
32 sysadm 1.20 #include <stdlib.h>
33 sysadm 1.62 #include <string.h>
34 sysadm 1.19 #include <unistd.h>
35 sysadm 1.74 #include <utmp.h>
36 sysadm 1.9 #include <arpa/inet.h>
37 sysadm 1.62 #include <libssh/callbacks.h>
38 sysadm 1.55 #include <libssh/libssh.h>
39     #include <libssh/server.h>
40 sysadm 1.62 #include <netinet/in.h>
41 sysadm 1.61 #include <sys/epoll.h>
42 sysadm 1.62 #include <sys/socket.h>
43 sysadm 1.61 #include <sys/syscall.h>
44     #include <sys/types.h>
45     #include <sys/wait.h>
46 sysadm 1.86
47     #ifdef HAVE_SYSTEMD_SD_DAEMON_H
48 sysadm 1.61 #include <systemd/sd-daemon.h>
49 sysadm 1.86 #endif
50 sysadm 1.3
51 sysadm 1.79 enum _net_server_constant_t
52     {
53     WAIT_CHILD_PROCESS_EXIT_TIMEOUT = 5, // second
54     WAIT_CHILD_PROCESS_KILL_TIMEOUT = 1, // second
55    
56     SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds
57     };
58 sysadm 1.70
59 sysadm 1.79 static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server";
60 sysadm 1.74
61     /* A userdata struct for session. */
62     struct session_data_struct
63 sysadm 1.55 {
64     int tries;
65     int error;
66     };
67    
68 sysadm 1.74 /* 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 sysadm 1.55 static int auth_password(ssh_session session, const char *user,
88     const char *password, void *userdata)
89     {
90 sysadm 1.74 struct session_data_struct *sdata = (struct session_data_struct *)userdata;
91 sysadm 1.55 int ret;
92    
93     if (strcmp(user, "guest") == 0)
94     {
95 sysadm 1.57 ret = load_guest_info();
96 sysadm 1.55 }
97     else
98     {
99 sysadm 1.57 ret = check_user(user, password);
100 sysadm 1.55 }
101    
102     if (ret == 0)
103     {
104     return SSH_AUTH_SUCCESS;
105     }
106    
107 sysadm 1.74 if ((++(sdata->tries)) >= BBS_login_retry_times)
108 sysadm 1.55 {
109 sysadm 1.74 sdata->error = 1;
110 sysadm 1.55 }
111    
112     return SSH_AUTH_DENIED;
113     }
114    
115     static int pty_request(ssh_session session, ssh_channel channel, const char *term,
116 sysadm 1.74 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 sysadm 1.55 {
187 sysadm 1.74 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 sysadm 1.55 }
203    
204     static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
205     {
206 sysadm 1.74 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 sysadm 1.55 }
223    
224 sysadm 1.74 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 sysadm 1.55
239 sysadm 1.74 static ssh_channel channel_open(ssh_session session, void *userdata)
240 sysadm 1.55 {
241 sysadm 1.74 (void)userdata;
242    
243 sysadm 1.55 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 sysadm 1.56 static int fork_server(void)
254 sysadm 1.55 {
255     ssh_event event;
256 sysadm 1.71 long int ssh_timeout = 0;
257 sysadm 1.55 int pid;
258     int i;
259     int ret;
260    
261 sysadm 1.74 /* 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 sysadm 1.55 .tries = 0,
281     .error = 0,
282     };
283    
284 sysadm 1.74 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 sysadm 1.55 .userdata = &cb_data,
294     .auth_password_function = auth_password,
295 sysadm 1.74 .channel_open_request_session_function = channel_open,
296 sysadm 1.55 };
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 sysadm 1.71 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 sysadm 1.75 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 sysadm 1.55 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 sysadm 1.58 for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i += 100)
355 sysadm 1.55 {
356 sysadm 1.58 ret = ssh_event_dopoll(event, 100); // 0.1 second
357 sysadm 1.55 if (ret == SSH_ERROR)
358     {
359 sysadm 1.73 #ifdef _DEBUG
360 sysadm 1.55 log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));
361 sysadm 1.73 #endif
362 sysadm 1.55 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 sysadm 1.71
372 sysadm 1.74 ssh_set_channel_callbacks(SSH_channel, &channel_cb);
373    
374 sysadm 1.75 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 sysadm 1.71 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 sysadm 1.55 }
397    
398     // Redirect Input
399     if (dup2(socket_client, STDIN_FILENO) == -1)
400     {
401     log_error("Redirect stdin to client socket failed\n");
402     goto cleanup;
403     }
404    
405     // Redirect Output
406     if (dup2(socket_client, STDOUT_FILENO) == -1)
407     {
408     log_error("Redirect stdout to client socket failed\n");
409     goto cleanup;
410     }
411    
412     SYS_child_process_count = 0;
413    
414     bbs_main();
415    
416     cleanup:
417     // Child process exit
418     SYS_server_exit = 1;
419    
420     if (SSH_v2)
421     {
422 sysadm 1.74 close(cdata.pty_master);
423     close(cdata.child_stdin);
424     close(cdata.child_stdout);
425     close(cdata.child_stderr);
426    
427 sysadm 1.55 ssh_channel_free(SSH_channel);
428     ssh_disconnect(SSH_session);
429     }
430     else if (close(socket_client) == -1)
431     {
432     log_error("Close client socket failed\n");
433     }
434    
435     ssh_free(SSH_session);
436     ssh_finalize();
437    
438     // Close Input and Output for client
439     close(STDIN_FILENO);
440     close(STDOUT_FILENO);
441    
442     log_common("Process exit normally\n");
443     log_end();
444    
445     _exit(0);
446    
447     return 0;
448     }
449    
450 sysadm 1.52 int net_server(const char *hostaddr, in_port_t port[])
451 sysadm 1.1 {
452 sysadm 1.82 HASH_DICT *hash_dict_pid_sockaddr = NULL;
453     HASH_DICT *hash_dict_sockaddr_count = NULL;
454    
455 sysadm 1.52 unsigned int addrlen;
456 sysadm 1.19 int ret;
457 sysadm 1.52 int flags[2];
458 sysadm 1.13 struct sockaddr_in sin;
459 sysadm 1.25 struct epoll_event ev, events[MAX_EVENTS];
460     int nfds, epollfd;
461 sysadm 1.19 siginfo_t siginfo;
462 sysadm 1.70 int notify_child_exit = 0;
463     time_t tm_notify_child_exit = time(NULL);
464 sysadm 1.66 MENU_SET bbs_menu_new;
465 sysadm 1.72 MENU_SET top10_menu_new;
466 sysadm 1.44 int i, j;
467     pid_t pid;
468 sysadm 1.51 int ssh_log_level = SSH_LOG_NOLOG;
469 sysadm 1.86 #ifdef HAVE_SYSTEMD_SD_DAEMON_H
470     int sd_notify_stopping = 0;
471     #endif
472 sysadm 1.10
473 sysadm 1.51 ssh_init();
474    
475     sshbind = ssh_bind_new();
476    
477     if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
478     ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
479     ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||
480 sysadm 1.63 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 ||
481 sysadm 1.51 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
482     {
483     log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));
484     ssh_bind_free(sshbind);
485     return -1;
486     }
487 sysadm 1.10
488 sysadm 1.52 epollfd = epoll_create1(0);
489     if (epollfd < 0)
490 sysadm 1.13 {
491 sysadm 1.52 log_error("epoll_create1() error (%d)\n", errno);
492 sysadm 1.27 return -1;
493 sysadm 1.13 }
494 sysadm 1.10
495 sysadm 1.52 // Server socket
496     for (i = 0; i < 2; i++)
497 sysadm 1.23 {
498 sysadm 1.52 socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
499 sysadm 1.23
500 sysadm 1.52 if (socket_server[i] < 0)
501     {
502     log_error("Create socket_server error (%d)\n", errno);
503     return -1;
504     }
505 sysadm 1.10
506 sysadm 1.52 sin.sin_family = AF_INET;
507     sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY);
508     sin.sin_port = htons(port[i]);
509    
510     // Reuse address and port
511     flags[i] = 1;
512     if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags[i], sizeof(flags[i])) < 0)
513     {
514     log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);
515     }
516     if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags[i], sizeof(flags[i])) < 0)
517     {
518     log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);
519     }
520 sysadm 1.1
521 sysadm 1.52 if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0)
522     {
523     log_error("Bind address %s:%u error (%d)\n",
524     inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);
525     return -1;
526     }
527 sysadm 1.16
528 sysadm 1.52 if (listen(socket_server[i], 10) < 0)
529     {
530     log_error("Telnet socket listen error (%d)\n", errno);
531     return -1;
532     }
533 sysadm 1.13
534 sysadm 1.52 log_common("Listening at %s:%u\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
535 sysadm 1.25
536 sysadm 1.52 ev.events = EPOLLIN;
537     ev.data.fd = socket_server[i];
538     if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server[i], &ev) == -1)
539 sysadm 1.30 {
540 sysadm 1.52 log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno);
541     if (close(epollfd) < 0)
542     {
543     log_error("close(epoll) error (%d)\n");
544     }
545     return -1;
546 sysadm 1.30 }
547 sysadm 1.52
548     flags[i] = fcntl(socket_server[i], F_GETFL, 0);
549     fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);
550 sysadm 1.25 }
551    
552 sysadm 1.82 hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);
553     if (hash_dict_pid_sockaddr == NULL)
554     {
555     log_error("hash_dict_create(hash_dict_pid_sockaddr) error\n");
556     return -1;
557     }
558     hash_dict_sockaddr_count = hash_dict_create(MAX_CLIENT_LIMIT);
559     if (hash_dict_sockaddr_count == NULL)
560     {
561     log_error("hash_dict_create(hash_dict_sockaddr_count) error\n");
562     return -1;
563     }
564    
565 sysadm 1.31 // Startup complete
566 sysadm 1.86 #ifdef HAVE_SYSTEMD_SD_DAEMON_H
567 sysadm 1.31 sd_notifyf(0, "READY=1\n"
568 sysadm 1.52 "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"
569 sysadm 1.31 "MAINPID=%d",
570 sysadm 1.52 hostaddr, port[0], hostaddr, port[1], getpid());
571 sysadm 1.86 #endif
572 sysadm 1.31
573 sysadm 1.19 while (!SYS_server_exit || SYS_child_process_count > 0)
574 sysadm 1.13 {
575 sysadm 1.86 #ifdef HAVE_SYSTEMD_SD_DAEMON_H
576 sysadm 1.31 if (SYS_server_exit && !sd_notify_stopping)
577     {
578     sd_notify(0, "STOPPING=1");
579     sd_notify_stopping = 1;
580     }
581 sysadm 1.86 #endif
582 sysadm 1.37
583 sysadm 1.22 while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)
584 sysadm 1.19 {
585 sysadm 1.30 SYS_child_exit = 0;
586    
587 sysadm 1.19 siginfo.si_pid = 0;
588     ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG);
589     if (ret == 0 && siginfo.si_pid > 0)
590     {
591 sysadm 1.30 SYS_child_exit = 1; // Retry waitid
592    
593 sysadm 1.19 SYS_child_process_count--;
594 sysadm 1.49 log_common("Child process (%d) exited\n", siginfo.si_pid);
595 sysadm 1.44
596 sysadm 1.45 if (siginfo.si_pid != section_list_loader_pid)
597 sysadm 1.44 {
598 sysadm 1.82 j = 0;
599     ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid, (int64_t *)&j);
600     if (ret < 0)
601     {
602     log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);
603     }
604     else
605 sysadm 1.44 {
606 sysadm 1.85 ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)j, -1);
607 sysadm 1.82 if (ret < 0)
608     {
609 sysadm 1.85 log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error\n", j);
610 sysadm 1.82 }
611    
612     ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid);
613     if (ret < 0)
614 sysadm 1.45 {
615 sysadm 1.82 log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);
616 sysadm 1.45 }
617     }
618 sysadm 1.44 }
619 sysadm 1.19 }
620     else if (ret == 0)
621     {
622     break;
623     }
624     else if (ret < 0)
625     {
626     log_error("Error in waitid: %d\n", errno);
627     break;
628     }
629     }
630    
631 sysadm 1.22 if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
632 sysadm 1.21 {
633 sysadm 1.70 if (notify_child_exit == 0)
634 sysadm 1.21 {
635 sysadm 1.86 #ifdef HAVE_SYSTEMD_SD_DAEMON_H
636 sysadm 1.70 sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
637     log_common("Notify %d child process to exit\n", SYS_child_process_count);
638 sysadm 1.86 #endif
639 sysadm 1.70
640 sysadm 1.82 if (kill(-getpid(), SIGTERM) < 0)
641 sysadm 1.70 {
642     log_error("Send SIGTERM signal failed (%d)\n", errno);
643     }
644    
645     notify_child_exit = 1;
646     tm_notify_child_exit = time(NULL);
647 sysadm 1.21 }
648 sysadm 1.70 else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)
649     {
650 sysadm 1.86 #ifdef HAVE_SYSTEMD_SD_DAEMON_H
651 sysadm 1.70 sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
652 sysadm 1.86 #endif
653 sysadm 1.31
654 sysadm 1.82 if (kill(-getpid(), SIGKILL) < 0)
655 sysadm 1.70 {
656 sysadm 1.82 log_error("Send SIGKILL signal failed (%d)\n", errno);
657 sysadm 1.70 }
658 sysadm 1.69
659 sysadm 1.70 notify_child_exit = 2;
660     tm_notify_child_exit = time(NULL);
661     }
662     else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT)
663     {
664     log_error("Main process prepare to exit without waiting for %d child process any longer\n", SYS_child_process_count);
665     SYS_child_process_count = 0;
666     }
667 sysadm 1.21 }
668    
669 sysadm 1.44 if (SYS_conf_reload && !SYS_server_exit)
670 sysadm 1.19 {
671 sysadm 1.44 SYS_conf_reload = 0;
672 sysadm 1.86
673     #ifdef HAVE_SYSTEMD_SD_DAEMON_H
674 sysadm 1.37 sd_notify(0, "RELOADING=1");
675 sysadm 1.86 #endif
676 sysadm 1.30
677 sysadm 1.84 // Restart log
678     if (log_restart() < 0)
679     {
680     log_error("Restart logging failed\n");
681     }
682    
683 sysadm 1.44 // Reload configuration
684     if (load_conf(CONF_BBSD) < 0)
685     {
686     log_error("Reload conf failed\n");
687     }
688    
689 sysadm 1.80 // Reload BWF config
690     if (bwf_load(CONF_BWF) < 0)
691     {
692     log_error("Reload BWF conf failed\n");
693     }
694    
695 sysadm 1.68 if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
696 sysadm 1.34 {
697 sysadm 1.68 unload_menu(&bbs_menu_new);
698 sysadm 1.72 log_error("Reload bbs menu failed\n");
699 sysadm 1.19 }
700     else
701     {
702 sysadm 1.68 unload_menu(&bbs_menu);
703     memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
704 sysadm 1.72 log_common("Reload bbs menu successfully\n");
705     }
706    
707     if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)
708     {
709     unload_menu(&top10_menu_new);
710     log_error("Reload top10 menu failed\n");
711     }
712     else
713     {
714     unload_menu(&top10_menu);
715     top10_menu_new.allow_exit = 1;
716     memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new));
717     log_common("Reload top10 menu successfully\n");
718 sysadm 1.19 }
719 sysadm 1.37
720 sysadm 1.35 for (int i = 0; i < data_files_load_startup_count; i++)
721     {
722 sysadm 1.39 if (load_file(data_files_load_startup[i]) < 0)
723 sysadm 1.35 {
724 sysadm 1.76 log_error("load_file(%s) error\n", data_files_load_startup[i]);
725 sysadm 1.35 }
726     }
727 sysadm 1.49 log_common("Reload data files successfully\n");
728 sysadm 1.35
729 sysadm 1.68 // Load section config and gen_ex
730     if (load_section_config_from_db(1) < 0)
731     {
732     log_error("load_section_config_from_db(1) error\n");
733     }
734     else
735 sysadm 1.42 {
736 sysadm 1.68 log_common("Reload section config and gen_ex successfully\n");
737 sysadm 1.42 }
738 sysadm 1.68
739 sysadm 1.84 // Notify child processes to reload configuration
740     if (kill(-getpid(), SIGUSR1) < 0)
741     {
742     log_error("Send SIGUSR1 signal failed (%d)\n", errno);
743     }
744    
745 sysadm 1.86 #ifdef HAVE_SYSTEMD_SD_DAEMON_H
746 sysadm 1.68 sd_notify(0, "READY=1");
747 sysadm 1.86 #endif
748 sysadm 1.42 }
749    
750 sysadm 1.25 nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
751 sysadm 1.22
752 sysadm 1.25 if (nfds < 0)
753 sysadm 1.19 {
754     if (errno != EINTR)
755     {
756 sysadm 1.25 log_error("epoll_wait() error (%d)\n", errno);
757     break;
758 sysadm 1.19 }
759     continue;
760     }
761 sysadm 1.13
762 sysadm 1.19 // Stop accept new connection on exit
763     if (SYS_server_exit)
764 sysadm 1.13 {
765     continue;
766     }
767    
768 sysadm 1.25 for (int i = 0; i < nfds; i++)
769 sysadm 1.13 {
770 sysadm 1.52 if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1])
771 sysadm 1.13 {
772 sysadm 1.53 SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0);
773 sysadm 1.52
774 sysadm 1.29 while (!SYS_server_exit) // Accept all incoming connections until error
775 sysadm 1.13 {
776 sysadm 1.52 addrlen = sizeof(sin);
777 sysadm 1.53 socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, &addrlen);
778 sysadm 1.25 if (socket_client < 0)
779     {
780 sysadm 1.28 if (errno == EAGAIN || errno == EWOULDBLOCK)
781     {
782     break;
783     }
784     else if (errno == EINTR)
785     {
786     continue;
787     }
788     else
789 sysadm 1.25 {
790     log_error("accept(socket_server) error (%d)\n", errno);
791 sysadm 1.28 break;
792 sysadm 1.25 }
793     }
794    
795     strncpy(hostaddr_client, inet_ntoa(sin.sin_addr), sizeof(hostaddr_client) - 1);
796     hostaddr_client[sizeof(hostaddr_client) - 1] = '\0';
797    
798     port_client = ntohs(sin.sin_port);
799    
800 sysadm 1.64 log_common("Accept %s connection from %s:%d\n", (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client);
801 sysadm 1.25
802 sysadm 1.43 if (SYS_child_process_count - 1 < BBS_max_client)
803 sysadm 1.25 {
804 sysadm 1.44 j = 0;
805 sysadm 1.82 ret = hash_dict_get(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, (int64_t *)&j);
806     if (ret < 0)
807 sysadm 1.44 {
808 sysadm 1.82 log_error("hash_dict_get(hash_dict_sockaddr_count, %s) error\n", hostaddr_client);
809 sysadm 1.44 }
810    
811     if (j < BBS_max_client_per_ip)
812 sysadm 1.43 {
813 sysadm 1.52 if ((pid = fork_server()) < 0)
814 sysadm 1.44 {
815     log_error("fork_server() error\n");
816     }
817     else if (pid > 0)
818     {
819 sysadm 1.82 ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr);
820     if (ret < 0)
821 sysadm 1.44 {
822 sysadm 1.82 log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client);
823 sysadm 1.44 }
824    
825 sysadm 1.85 ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);
826 sysadm 1.82 if (ret < 0)
827 sysadm 1.44 {
828 sysadm 1.85 log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, 1);
829 sysadm 1.44 }
830     }
831 sysadm 1.43 }
832 sysadm 1.82 else
833     {
834     log_error("Rejected client connection from %s over limit per IP (%d)\n", hostaddr_client, BBS_max_client_per_ip);
835     }
836 sysadm 1.43 }
837     else
838     {
839     log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1);
840 sysadm 1.25 }
841    
842     if (close(socket_client) == -1)
843     {
844     log_error("close(socket_lient) error (%d)\n", errno);
845     }
846 sysadm 1.13 }
847     }
848     }
849 sysadm 1.25 }
850 sysadm 1.13
851 sysadm 1.30 if (close(epollfd) < 0)
852     {
853     log_error("close(epoll) error (%d)\n");
854     }
855    
856 sysadm 1.52 for (i = 0; i < 2; i++)
857     {
858     fcntl(socket_server[i], F_SETFL, flags[i]);
859 sysadm 1.13
860 sysadm 1.52 if (close(socket_server[i]) == -1)
861     {
862     log_error("Close server socket failed\n");
863     }
864 sysadm 1.13 }
865 sysadm 1.7
866 sysadm 1.82 hash_dict_destroy(hash_dict_pid_sockaddr);
867     hash_dict_destroy(hash_dict_sockaddr_count);
868    
869 sysadm 1.51 ssh_bind_free(sshbind);
870     ssh_finalize();
871    
872 sysadm 1.13 return 0;
873 sysadm 1.1 }

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