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

Diff of /lbbs/src/net_server.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.68 by sysadm, Thu Jun 26 12:17:02 2025 UTC Revision 1.86 by sysadm, Sun Nov 16 13:18:30 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    net_server.c  -  description  /*
3                                                           -------------------   * net_server
4          Copyright            : (C) 2004-2025 by Leaflet   *   - network server with SSH support
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "bbs_main.h"  #include "bbs_main.h"
15    #include "bwf.h"
16  #include "common.h"  #include "common.h"
17  #include "database.h"  #include "database.h"
18  #include "file_loader.h"  #include "file_loader.h"
19    #include "hash_dict.h"
20  #include "io.h"  #include "io.h"
21  #include "init.h"  #include "init.h"
22  #include "log.h"  #include "log.h"
# Line 29  Line 27 
27  #include "section_list_loader.h"  #include "section_list_loader.h"
28  #include <errno.h>  #include <errno.h>
29  #include <fcntl.h>  #include <fcntl.h>
30    #include <pty.h>
31  #include <signal.h>  #include <signal.h>
32  #include <stdlib.h>  #include <stdlib.h>
33  #include <string.h>  #include <string.h>
34  #include <unistd.h>  #include <unistd.h>
35    #include <utmp.h>
36  #include <arpa/inet.h>  #include <arpa/inet.h>
37  #include <libssh/callbacks.h>  #include <libssh/callbacks.h>
38  #include <libssh/libssh.h>  #include <libssh/libssh.h>
# Line 43  Line 43 
43  #include <sys/syscall.h>  #include <sys/syscall.h>
44  #include <sys/types.h>  #include <sys/types.h>
45  #include <sys/wait.h>  #include <sys/wait.h>
46    
47    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
48  #include <systemd/sd-daemon.h>  #include <systemd/sd-daemon.h>
49    #endif
50    
51  struct process_sockaddr_t  enum _net_server_constant_t
52  {  {
53          pid_t pid;          WAIT_CHILD_PROCESS_EXIT_TIMEOUT = 5, // second
54          in_addr_t s_addr;          WAIT_CHILD_PROCESS_KILL_TIMEOUT = 1, // second
 };  
 typedef struct process_sockaddr_t PROCESS_SOCKADDR;  
55    
56  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];          SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds
57    };
58    
59  #define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds  static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server";
60    
61  struct ssl_server_cb_data_t  /* A userdata struct for session. */
62    struct session_data_struct
63  {  {
64          int tries;          int tries;
65          int error;          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,  static int auth_password(ssh_session session, const char *user,
88                                                   const char *password, void *userdata)                                                   const char *password, void *userdata)
89  {  {
90          struct ssl_server_cb_data_t *p_data = userdata;          struct session_data_struct *sdata = (struct session_data_struct *)userdata;
91          int ret;          int ret;
92    
93          if (strcmp(user, "guest") == 0)          if (strcmp(user, "guest") == 0)
# Line 82  static int auth_password(ssh_session ses Line 104  static int auth_password(ssh_session ses
104                  return SSH_AUTH_SUCCESS;                  return SSH_AUTH_SUCCESS;
105          }          }
106    
107          if ((++(p_data->tries)) >= BBS_login_retry_times)          if ((++(sdata->tries)) >= BBS_login_retry_times)
108          {          {
109                  p_data->error = 1;                  sdata->error = 1;
110          }          }
111    
112          return SSH_AUTH_DENIED;          return SSH_AUTH_DENIED;
113  }  }
114    
115  static int pty_request(ssh_session session, ssh_channel channel, const char *term,  static int pty_request(ssh_session session, ssh_channel channel, const char *term,
116                                             int x, int y, int px, int py, void *userdata)                                             int cols, int rows, int px, int py, void *userdata)
117  {  {
118          return 0;          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)  static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
205  {  {
206          return 0;          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 struct ssh_channel_callbacks_struct channel_cb = {  static int subsystem_request(ssh_session session, ssh_channel channel, const char *subsystem, void *userdata)
225          .channel_pty_request_function = pty_request,  {
226          .channel_shell_request_function = shell_request};          (void)session;
227            (void)channel;
228    
229            log_error("subsystem_request(subsystem=%s)\n", subsystem);
230    
231  static ssh_channel new_session_channel(ssh_session session, void *userdata)          /* 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)          if (SSH_channel != NULL)
244          {          {
245                  return NULL;                  return NULL;
246          }          }
247    
248          SSH_channel = ssh_channel_new(session);          SSH_channel = ssh_channel_new(session);
         ssh_callbacks_init(&channel_cb);  
         ssh_set_channel_callbacks(SSH_channel, &channel_cb);  
249    
250          return SSH_channel;          return SSH_channel;
251  }  }
# Line 122  static ssh_channel new_session_channel(s Line 253  static ssh_channel new_session_channel(s
253  static int fork_server(void)  static int fork_server(void)
254  {  {
255          ssh_event event;          ssh_event event;
256            long int ssh_timeout = 0;
257          int pid;          int pid;
258          int i;          int i;
259          int ret;          int ret;
260    
261          struct ssl_server_cb_data_t cb_data = {          /* 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,                  .tries = 0,
281                  .error = 0,                  .error = 0,
282          };          };
283    
284          struct ssh_server_callbacks_struct cb = {          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,                  .userdata = &cb_data,
294                  .auth_password_function = auth_password,                  .auth_password_function = auth_password,
295                  .channel_open_request_session_function = new_session_channel,                  .channel_open_request_session_function = channel_open,
296          };          };
297    
298          pid = fork();          pid = fork();
# Line 170  static int fork_server(void) Line 328  static int fork_server(void)
328    
329                  ssh_bind_free(sshbind);                  ssh_bind_free(sshbind);
330    
331                  ssh_callbacks_init(&cb);                  ssh_timeout = 60; // second
332                  ssh_set_server_callbacks(SSH_session, &cb);                  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))                  if (ssh_handle_key_exchange(SSH_session))
346                  {                  {
347                          log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));                          log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));
348                          goto cleanup;                          goto cleanup;
349                  }                  }
                 ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);  
350    
351                  event = ssh_event_new();                  event = ssh_event_new();
352                  ssh_event_add_session(event, SSH_session);                  ssh_event_add_session(event, SSH_session);
# Line 188  static int fork_server(void) Line 356  static int fork_server(void)
356                          ret = ssh_event_dopoll(event, 100); // 0.1 second                          ret = ssh_event_dopoll(event, 100); // 0.1 second
357                          if (ret == SSH_ERROR)                          if (ret == SSH_ERROR)
358                          {                          {
359    #ifdef _DEBUG
360                                  log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));                                  log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));
361    #endif
362                                  goto cleanup;                                  goto cleanup;
363                          }                          }
364                  }                  }
# Line 198  static int fork_server(void) Line 368  static int fork_server(void)
368                          log_error("SSH auth error, tried %d times\n", cb_data.tries);                          log_error("SSH auth error, tried %d times\n", cb_data.tries);
369                          goto cleanup;                          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          // Redirect Input
         close(STDIN_FILENO);  
399          if (dup2(socket_client, STDIN_FILENO) == -1)          if (dup2(socket_client, STDIN_FILENO) == -1)
400          {          {
401                  log_error("Redirect stdin to client socket failed\n");                  log_error("Redirect stdin to client socket failed\n");
# Line 209  static int fork_server(void) Line 403  static int fork_server(void)
403          }          }
404    
405          // Redirect Output          // Redirect Output
         close(STDOUT_FILENO);  
406          if (dup2(socket_client, STDOUT_FILENO) == -1)          if (dup2(socket_client, STDOUT_FILENO) == -1)
407          {          {
408                  log_error("Redirect stdout to client socket failed\n");                  log_error("Redirect stdout to client socket failed\n");
# Line 226  cleanup: Line 419  cleanup:
419    
420          if (SSH_v2)          if (SSH_v2)
421          {          {
422                    close(cdata.pty_master);
423                    close(cdata.child_stdin);
424                    close(cdata.child_stdout);
425                    close(cdata.child_stderr);
426    
427                  ssh_channel_free(SSH_channel);                  ssh_channel_free(SSH_channel);
428                  ssh_disconnect(SSH_session);                  ssh_disconnect(SSH_session);
429          }          }
# Line 251  cleanup: Line 449  cleanup:
449    
450  int net_server(const char *hostaddr, in_port_t port[])  int net_server(const char *hostaddr, in_port_t port[])
451  {  {
452            HASH_DICT *hash_dict_pid_sockaddr = NULL;
453            HASH_DICT *hash_dict_sockaddr_count = NULL;
454    
455          unsigned int addrlen;          unsigned int addrlen;
456          int ret;          int ret;
457          int flags[2];          int flags[2];
# Line 258  int net_server(const char *hostaddr, in_ Line 459  int net_server(const char *hostaddr, in_
459          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
460          int nfds, epollfd;          int nfds, epollfd;
461          siginfo_t siginfo;          siginfo_t siginfo;
462          int sd_notify_stopping = 0;          int notify_child_exit = 0;
463            time_t tm_notify_child_exit = time(NULL);
464          MENU_SET bbs_menu_new;          MENU_SET bbs_menu_new;
465            MENU_SET top10_menu_new;
466          int i, j;          int i, j;
467          pid_t pid;          pid_t pid;
468          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
469    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
470            int sd_notify_stopping = 0;
471    #endif
472    
473          ssh_init();          ssh_init();
474    
# Line 343  int net_server(const char *hostaddr, in_ Line 549  int net_server(const char *hostaddr, in_
549                  fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);                  fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);
550          }          }
551    
552            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          // Startup complete          // Startup complete
566    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
567          sd_notifyf(0, "READY=1\n"          sd_notifyf(0, "READY=1\n"
568                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"
569                                    "MAINPID=%d",                                    "MAINPID=%d",
570                             hostaddr, port[0], hostaddr, port[1], getpid());                             hostaddr, port[0], hostaddr, port[1], getpid());
571    #endif
572    
573          while (!SYS_server_exit || SYS_child_process_count > 0)          while (!SYS_server_exit || SYS_child_process_count > 0)
574          {          {
575    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
576                  if (SYS_server_exit && !sd_notify_stopping)                  if (SYS_server_exit && !sd_notify_stopping)
577                  {                  {
578                          sd_notify(0, "STOPPING=1");                          sd_notify(0, "STOPPING=1");
579                          sd_notify_stopping = 1;                          sd_notify_stopping = 1;
580                  }                  }
581    #endif
582    
583                  while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)                  while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)
584                  {                  {
# Line 372  int net_server(const char *hostaddr, in_ Line 595  int net_server(const char *hostaddr, in_
595    
596                                  if (siginfo.si_pid != section_list_loader_pid)                                  if (siginfo.si_pid != section_list_loader_pid)
597                                  {                                  {
598                                          i = 0;                                          j = 0;
599                                          for (; i < BBS_max_client; i++)                                          ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid, (int64_t *)&j);
600                                            if (ret < 0)
601                                          {                                          {
602                                                  if (process_sockaddr_pool[i].pid == siginfo.si_pid)                                                  log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);
                                                 {  
                                                         process_sockaddr_pool[i].pid = 0;  
                                                         break;  
                                                 }  
603                                          }                                          }
604                                          if (i >= BBS_max_client)                                          else
605                                          {                                          {
606                                                  log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid);                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)j, -1);
607                                                    if (ret < 0)
608                                                    {
609                                                            log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error\n", j);
610                                                    }
611    
612                                                    ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid);
613                                                    if (ret < 0)
614                                                    {
615                                                            log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);
616                                                    }
617                                          }                                          }
618                                  }                                  }
619                          }                          }
# Line 400  int net_server(const char *hostaddr, in_ Line 630  int net_server(const char *hostaddr, in_
630    
631                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
632                  {                  {
633                          log_common("Notify %d child process to exit\n", SYS_child_process_count);                          if (notify_child_exit == 0)
                         if (kill(0, SIGTERM) < 0)  
634                          {                          {
635                                  log_error("Send SIGTERM signal failed (%d)\n", errno);  #ifdef HAVE_SYSTEMD_SD_DAEMON_H
636                                    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    #endif
639    
640                                    if (kill(-getpid(), SIGTERM) < 0)
641                                    {
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                          }                          }
648                            else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)
649                            {
650    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
651                                    sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
652    #endif
653    
654                          sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count);                                  if (kill(-getpid(), SIGKILL) < 0)
655                                    {
656                                            log_error("Send SIGKILL signal failed (%d)\n", errno);
657                                    }
658    
659                                    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                  }                  }
668    
669                  if (SYS_conf_reload && !SYS_server_exit)                  if (SYS_conf_reload && !SYS_server_exit)
670                  {                  {
671                          SYS_conf_reload = 0;                          SYS_conf_reload = 0;
672    
673    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
674                          sd_notify(0, "RELOADING=1");                          sd_notify(0, "RELOADING=1");
675    #endif
676    
677                            // Restart log
678                            if (log_restart() < 0)
679                            {
680                                    log_error("Restart logging failed\n");
681                            }
682    
683                          // Reload configuration                          // Reload configuration
684                          if (load_conf(CONF_BBSD) < 0)                          if (load_conf(CONF_BBSD) < 0)
# Line 420  int net_server(const char *hostaddr, in_ Line 686  int net_server(const char *hostaddr, in_
686                                  log_error("Reload conf failed\n");                                  log_error("Reload conf failed\n");
687                          }                          }
688    
689                            // Reload BWF config
690                            if (bwf_load(CONF_BWF) < 0)
691                            {
692                                    log_error("Reload BWF conf failed\n");
693                            }
694    
695                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
696                          {                          {
697                                  unload_menu(&bbs_menu_new);                                  unload_menu(&bbs_menu_new);
698                                  log_error("Reload menu failed\n");                                  log_error("Reload bbs menu failed\n");
699                          }                          }
700                          else                          else
701                          {                          {
702                                  unload_menu(&bbs_menu);                                  unload_menu(&bbs_menu);
703                                  memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));                                  memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
704                                  log_common("Reload menu successfully\n");                                  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                          }                          }
719    
720                          for (int i = 0; i < data_files_load_startup_count; i++)                          for (int i = 0; i < data_files_load_startup_count; i++)
721                          {                          {
722                                  if (load_file(data_files_load_startup[i]) < 0)                                  if (load_file(data_files_load_startup[i]) < 0)
723                                  {                                  {
724                                          log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);                                          log_error("load_file(%s) error\n", data_files_load_startup[i]);
725                                  }                                  }
726                          }                          }
727                          log_common("Reload data files successfully\n");                          log_common("Reload data files successfully\n");
# Line 451  int net_server(const char *hostaddr, in_ Line 736  int net_server(const char *hostaddr, in_
736                                  log_common("Reload section config and gen_ex successfully\n");                                  log_common("Reload section config and gen_ex successfully\n");
737                          }                          }
738    
739                            // 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    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
746                          sd_notify(0, "READY=1");                          sd_notify(0, "READY=1");
747    #endif
748                  }                  }
749    
750                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
# Line 509  int net_server(const char *hostaddr, in_ Line 802  int net_server(const char *hostaddr, in_
802                                          if (SYS_child_process_count - 1 < BBS_max_client)                                          if (SYS_child_process_count - 1 < BBS_max_client)
803                                          {                                          {
804                                                  j = 0;                                                  j = 0;
805                                                  for (i = 0; i < BBS_max_client; i++)                                                  ret = hash_dict_get(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, (int64_t *)&j);
806                                                    if (ret < 0)
807                                                  {                                                  {
808                                                          if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr)                                                          log_error("hash_dict_get(hash_dict_sockaddr_count, %s) error\n", hostaddr_client);
                                                         {  
                                                                 j++;  
                                                                 if (j >= BBS_max_client_per_ip)  
                                                                 {  
                                                                         log_common("Too many client connections (%d) from %s\n", j, hostaddr_client);  
                                                                         break;  
                                                                 }  
                                                         }  
809                                                  }                                                  }
810    
811                                                  if (j < BBS_max_client_per_ip)                                                  if (j < BBS_max_client_per_ip)
# Line 530  int net_server(const char *hostaddr, in_ Line 816  int net_server(const char *hostaddr, in_
816                                                          }                                                          }
817                                                          else if (pid > 0)                                                          else if (pid > 0)
818                                                          {                                                          {
819                                                                  i = 0;                                                                  ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr);
820                                                                  for (; i < BBS_max_client; i++)                                                                  if (ret < 0)
821                                                                  {                                                                  {
822                                                                          if (process_sockaddr_pool[i].pid == 0)                                                                          log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client);
                                                                         {  
                                                                                 break;  
                                                                         }  
823                                                                  }                                                                  }
824    
825                                                                  if (i >= BBS_max_client)                                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);
826                                                                  {                                                                  if (ret < 0)
                                                                         log_error("Process sockaddr pool depleted\n");  
                                                                 }  
                                                                 else  
827                                                                  {                                                                  {
828                                                                          process_sockaddr_pool[i].pid = pid;                                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, 1);
                                                                         process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr;  
829                                                                  }                                                                  }
830                                                          }                                                          }
831                                                  }                                                  }
832                                                    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                                          }                                          }
837                                          else                                          else
838                                          {                                          {
# Line 580  int net_server(const char *hostaddr, in_ Line 863  int net_server(const char *hostaddr, in_
863                  }                  }
864          }          }
865    
866            hash_dict_destroy(hash_dict_pid_sockaddr);
867            hash_dict_destroy(hash_dict_sockaddr_count);
868    
869          ssh_bind_free(sshbind);          ssh_bind_free(sshbind);
870          ssh_finalize();          ssh_finalize();
871    


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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