/[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.56 by sysadm, Sat Jun 7 02:38:28 2025 UTC Revision 1.73 by sysadm, Sun Oct 19 01:30:38 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #define _XOPEN_SOURCE 500  
 #define _POSIX_C_SOURCE 200809L  
 #define _GNU_SOURCE  
   
 #include "net_server.h"  
 #include "common.h"  
 #include "bbs_main.h"  
17  #include "bbs.h"  #include "bbs.h"
18  #include "log.h"  #include "bbs_main.h"
19    #include "common.h"
20    #include "database.h"
21    #include "file_loader.h"
22  #include "io.h"  #include "io.h"
23  #include "init.h"  #include "init.h"
24  #include "menu.h"  #include "log.h"
 #include "database.h"  
25  #include "login.h"  #include "login.h"
26  #include "file_loader.h"  #include "menu.h"
27    #include "net_server.h"
28    #include "section_list.h"
29  #include "section_list_loader.h"  #include "section_list_loader.h"
30  #include <errno.h>  #include <errno.h>
31  #include <fcntl.h>  #include <fcntl.h>
 #include <string.h>  
32  #include <signal.h>  #include <signal.h>
33  #include <stdlib.h>  #include <stdlib.h>
34    #include <string.h>
35  #include <unistd.h>  #include <unistd.h>
 #include <sys/syscall.h>  
 #include <sys/socket.h>  
 #include <sys/wait.h>  
 #include <sys/epoll.h>  
36  #include <arpa/inet.h>  #include <arpa/inet.h>
37  #include <netinet/in.h>  #include <libssh/callbacks.h>
 #include <systemd/sd-daemon.h>  
38  #include <libssh/libssh.h>  #include <libssh/libssh.h>
39  #include <libssh/server.h>  #include <libssh/server.h>
40  #include <libssh/callbacks.h>  #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  struct process_sockaddr_t
52  {  {
# Line 56  typedef struct process_sockaddr_t PROCES Line 57  typedef struct process_sockaddr_t PROCES
57    
58  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];
59    
60  #define SSH_AUTH_MAX_DURATION 60 // seconds  #define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds
61    
62  struct ssl_server_cb_data_t  struct ssl_server_cb_data_t
63  {  {
# Line 67  struct ssl_server_cb_data_t Line 68  struct ssl_server_cb_data_t
68  static int auth_password(ssh_session session, const char *user,  static int auth_password(ssh_session session, const char *user,
69                                                   const char *password, void *userdata)                                                   const char *password, void *userdata)
70  {  {
         MYSQL *db;  
71          struct ssl_server_cb_data_t *p_data = userdata;          struct ssl_server_cb_data_t *p_data = userdata;
72          int ret;          int ret;
73    
         if ((db = db_open()) == NULL)  
         {  
                 return SSH_AUTH_ERROR;  
         }  
   
74          if (strcmp(user, "guest") == 0)          if (strcmp(user, "guest") == 0)
75          {          {
76                  ret = load_guest_info(db);                  ret = load_guest_info();
77          }          }
78          else          else
79          {          {
80                  ret = check_user(db, user, password);                  ret = check_user(user, password);
81          }          }
82    
         mysql_close(db);  
   
83          if (ret == 0)          if (ret == 0)
84          {          {
85                  return SSH_AUTH_SUCCESS;                  return SSH_AUTH_SUCCESS;
# Line 132  static ssh_channel new_session_channel(s Line 125  static ssh_channel new_session_channel(s
125  static int fork_server(void)  static int fork_server(void)
126  {  {
127          ssh_event event;          ssh_event event;
128            long int ssh_timeout = 0;
129          int pid;          int pid;
130          int i;          int i;
131          int ret;          int ret;
# Line 183  static int fork_server(void) Line 177  static int fork_server(void)
177                  ssh_callbacks_init(&cb);                  ssh_callbacks_init(&cb);
178                  ssh_set_server_callbacks(SSH_session, &cb);                  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))                  if (ssh_handle_key_exchange(SSH_session))
188                  {                  {
189                          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));
# Line 193  static int fork_server(void) Line 194  static int fork_server(void)
194                  event = ssh_event_new();                  event = ssh_event_new();
195                  ssh_event_add_session(event, SSH_session);                  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++)                  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, 1000); // 1 second                          ret = ssh_event_dopoll(event, 100); // 0.1 second
200                          if (ret == SSH_ERROR)                          if (ret == SSH_ERROR)
201                          {                          {
202    #ifdef _DEBUG
203                                  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));
204    #endif
205                                  goto cleanup;                                  goto cleanup;
206                          }                          }
207                  }                  }
# Line 208  static int fork_server(void) Line 211  static int fork_server(void)
211                          log_error("SSH auth error, tried %d times\n", cb_data.tries);                          log_error("SSH auth error, tried %d times\n", cb_data.tries);
212                          goto cleanup;                          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          // Redirect Input
# Line 268  int net_server(const char *hostaddr, in_ Line 278  int net_server(const char *hostaddr, in_
278          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
279          int nfds, epollfd;          int nfds, epollfd;
280          siginfo_t siginfo;          siginfo_t siginfo;
281            int notify_child_exit = 0;
282            time_t tm_notify_child_exit = time(NULL);
283          int sd_notify_stopping = 0;          int sd_notify_stopping = 0;
284          MENU_SET *p_bbs_menu_new;          MENU_SET bbs_menu_new;
285            MENU_SET top10_menu_new;
286          int i, j;          int i, j;
287          pid_t pid;          pid_t pid;
288          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
# Line 281  int net_server(const char *hostaddr, in_ Line 294  int net_server(const char *hostaddr, in_
294          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
295                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||                  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 ||                  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)                  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));                  log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));
# Line 409  int net_server(const char *hostaddr, in_ Line 423  int net_server(const char *hostaddr, in_
423    
424                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
425                  {                  {
426                          log_common("Notify %d child process to exit\n", SYS_child_process_count);                          if (notify_child_exit == 0)
                         if (kill(0, SIGTERM) < 0)  
427                          {                          {
428                                  log_error("Send SIGTERM signal failed (%d)\n", errno);                                  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                          sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count);                                  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)                  if (SYS_conf_reload && !SYS_server_exit)
# Line 429  int net_server(const char *hostaddr, in_ Line 473  int net_server(const char *hostaddr, in_
473                                  log_error("Reload conf failed\n");                                  log_error("Reload conf failed\n");
474                          }                          }
475    
476                          p_bbs_menu_new = calloc(1, sizeof(MENU_SET));                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
                         if (p_bbs_menu_new == NULL)  
477                          {                          {
478                                  log_error("OOM: calloc(MENU_SET)\n");                                  unload_menu(&bbs_menu_new);
479                                    log_error("Reload bbs menu failed\n");
480                          }                          }
481                          else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0)                          else
482                          {                          {
483                                  unload_menu(p_bbs_menu_new);                                  unload_menu(&bbs_menu);
484                                  free(p_bbs_menu_new);                                  memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
485                                  p_bbs_menu_new = NULL;                                  log_common("Reload bbs menu successfully\n");
486                            }
487    
488                                  log_error("Reload menu failed\n");                          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                          else
494                          {                          {
495                                  unload_menu(p_bbs_menu);                                  unload_menu(&top10_menu);
496                                  free(p_bbs_menu);                                  top10_menu_new.allow_exit = 1;
497                                    memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new));
498                                  p_bbs_menu = p_bbs_menu_new;                                  log_common("Reload top10 menu successfully\n");
                                 p_bbs_menu_new = NULL;  
   
                                 log_common("Reload menu successfully\n");  
499                          }                          }
500    
                         sd_notify(0, "READY=1");  
                 }  
   
                 if (SYS_data_file_reload && !SYS_server_exit)  
                 {  
                         SYS_data_file_reload = 0;  
                         sd_notify(0, "RELOADING=1");  
   
501                          for (int i = 0; i < data_files_load_startup_count; i++)                          for (int i = 0; i < data_files_load_startup_count; i++)
502                          {                          {
503                                  if (load_file(data_files_load_startup[i]) < 0)                                  if (load_file(data_files_load_startup[i]) < 0)
# Line 468  int net_server(const char *hostaddr, in_ Line 505  int net_server(const char *hostaddr, in_
505                                          log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);                                          log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);
506                                  }                                  }
507                          }                          }
   
508                          log_common("Reload data files successfully\n");                          log_common("Reload data files successfully\n");
                         sd_notify(0, "READY=1");  
                 }  
   
                 if (SYS_section_list_reload && !SYS_server_exit)  
                 {  
                         SYS_section_list_reload = 0;  
509    
510                          if (section_list_loader_reload() < 0)                          // Load section config and gen_ex
511                            if (load_section_config_from_db(1) < 0)
512                          {                          {
513                                  log_error("ksection_list_loader_reload() failed\n");                                  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                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
# Line 533  int net_server(const char *hostaddr, in_ Line 570  int net_server(const char *hostaddr, in_
570    
571                                          port_client = ntohs(sin.sin_port);                                          port_client = ntohs(sin.sin_port);
572    
573                                          log_common("Accept %sconnection from %s:%d\n", (SSH_v2 ? "" : "SSH2 "), hostaddr_client, port_client);                                          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)                                          if (SYS_child_process_count - 1 < BBS_max_client)
576                                          {                                          {


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

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