/[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.76 by sysadm, Wed Oct 29 07:35:05 2025 UTC Revision 1.82 by sysadm, Fri Nov 14 08:45:25 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 47  Line 45 
45  #include <sys/wait.h>  #include <sys/wait.h>
46  #include <systemd/sd-daemon.h>  #include <systemd/sd-daemon.h>
47    
48  #define WAIT_CHILD_PROCESS_EXIT_TIMEOUT 5 // second  enum _net_server_constant_t
 #define WAIT_CHILD_PROCESS_KILL_TIMEOUT 1 // second  
   
 struct process_sockaddr_t  
49  {  {
50          pid_t pid;          WAIT_CHILD_PROCESS_EXIT_TIMEOUT = 5, // second
51          in_addr_t s_addr;          WAIT_CHILD_PROCESS_KILL_TIMEOUT = 1, // second
 };  
 typedef struct process_sockaddr_t PROCESS_SOCKADDR;  
   
 static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];  
52    
53  #define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds          SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds
54    };
55    
56  #define SFTP_SERVER_PATH "/usr/lib/sftp-server"  static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server";
57    
58  /* A userdata struct for session. */  /* A userdata struct for session. */
59  struct session_data_struct  struct session_data_struct
# Line 456  cleanup: Line 448  cleanup:
448    
449  int net_server(const char *hostaddr, in_port_t port[])  int net_server(const char *hostaddr, in_port_t port[])
450  {  {
451            HASH_DICT *hash_dict_pid_sockaddr = NULL;
452            HASH_DICT *hash_dict_sockaddr_count = NULL;
453    
454          unsigned int addrlen;          unsigned int addrlen;
455          int ret;          int ret;
456          int flags[2];          int flags[2];
# Line 551  int net_server(const char *hostaddr, in_ Line 546  int net_server(const char *hostaddr, in_
546                  fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);                  fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);
547          }          }
548    
549            hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);
550            if (hash_dict_pid_sockaddr == NULL)
551            {
552                    log_error("hash_dict_create(hash_dict_pid_sockaddr) error\n");
553                    return -1;
554            }
555            hash_dict_sockaddr_count = hash_dict_create(MAX_CLIENT_LIMIT);
556            if (hash_dict_sockaddr_count == NULL)
557            {
558                    log_error("hash_dict_create(hash_dict_sockaddr_count) error\n");
559                    return -1;
560            }
561    
562          // Startup complete          // Startup complete
563          sd_notifyf(0, "READY=1\n"          sd_notifyf(0, "READY=1\n"
564                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"
# Line 580  int net_server(const char *hostaddr, in_ Line 588  int net_server(const char *hostaddr, in_
588    
589                                  if (siginfo.si_pid != section_list_loader_pid)                                  if (siginfo.si_pid != section_list_loader_pid)
590                                  {                                  {
591                                          i = 0;                                          j = 0;
592                                          for (; i < BBS_max_client; i++)                                          ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid, (int64_t *)&j);
593                                            if (ret < 0)
594                                          {                                          {
595                                                  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;  
                                                 }  
596                                          }                                          }
597                                          if (i >= BBS_max_client)                                          else
598                                          {                                          {
599                                                  log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid);                                                  sin.sin_addr.s_addr = (uint32_t)j;
600                                                    j = 0;
601                                                    ret = hash_dict_get(hash_dict_sockaddr_count, sin.sin_addr.s_addr, (int64_t *)&j);
602                                                    if (ret < 0)
603                                                    {
604                                                            log_error("hash_dict_get(hash_dict_sockaddr_count, %d) error\n", sin.sin_addr.s_addr);
605                                                    }
606                                                    else if (ret == 0)
607                                                    {
608                                                            log_error("hash_dict_get(hash_dict_sockaddr_count, %d) not found\n", sin.sin_addr.s_addr);
609                                                            j = 1;
610                                                    }
611    
612                                                    j--;
613                                                    ret = hash_dict_set(hash_dict_sockaddr_count, sin.sin_addr.s_addr, j);
614                                                    if (ret < 0)
615                                                    {
616                                                            log_error("hash_dict_set(hash_dict_sockaddr_count, %d, %d) error\n", sin.sin_addr.s_addr, j);
617                                                    }
618    
619                                                    ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid);
620                                                    if (ret < 0)
621                                                    {
622                                                            log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);
623                                                    }
624                                          }                                          }
625                                  }                                  }
626                          }                          }
# Line 613  int net_server(const char *hostaddr, in_ Line 642  int net_server(const char *hostaddr, in_
642                                  sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);                                  sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
643                                  log_common("Notify %d child process to exit\n", SYS_child_process_count);                                  log_common("Notify %d child process to exit\n", SYS_child_process_count);
644    
645                                  if (kill(0, SIGTERM) < 0)                                  if (kill(-getpid(), SIGTERM) < 0)
646                                  {                                  {
647                                          log_error("Send SIGTERM signal failed (%d)\n", errno);                                          log_error("Send SIGTERM signal failed (%d)\n", errno);
648                                  }                                  }
# Line 625  int net_server(const char *hostaddr, in_ Line 654  int net_server(const char *hostaddr, in_
654                          {                          {
655                                  sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);                                  sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
656    
657                                  for (i = 0; i < BBS_max_client; i++)                                  if (kill(-getpid(), SIGKILL) < 0)
658                                  {                                  {
659                                          if (process_sockaddr_pool[i].pid != 0)                                          log_error("Send SIGKILL signal failed (%d)\n", errno);
                                         {  
                                                 log_error("Kill child process (pid=%d)\n", process_sockaddr_pool[i].pid);  
                                                 if (kill(process_sockaddr_pool[i].pid, SIGKILL) < 0)  
                                                 {  
                                                         log_error("Send SIGKILL signal failed (%d)\n", errno);  
                                                 }  
                                         }  
660                                  }                                  }
661    
662                                  notify_child_exit = 2;                                  notify_child_exit = 2;
# Line 658  int net_server(const char *hostaddr, in_ Line 680  int net_server(const char *hostaddr, in_
680                                  log_error("Reload conf failed\n");                                  log_error("Reload conf failed\n");
681                          }                          }
682    
683                            // Reload BWF config
684                            if (bwf_load(CONF_BWF) < 0)
685                            {
686                                    log_error("Reload BWF conf failed\n");
687                            }
688    
689                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
690                          {                          {
691                                  unload_menu(&bbs_menu_new);                                  unload_menu(&bbs_menu_new);
# Line 760  int net_server(const char *hostaddr, in_ Line 788  int net_server(const char *hostaddr, in_
788                                          if (SYS_child_process_count - 1 < BBS_max_client)                                          if (SYS_child_process_count - 1 < BBS_max_client)
789                                          {                                          {
790                                                  j = 0;                                                  j = 0;
791                                                  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);
792                                                    if (ret < 0)
793                                                  {                                                  {
794                                                          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;  
                                                                 }  
                                                         }  
795                                                  }                                                  }
796    
797                                                  if (j < BBS_max_client_per_ip)                                                  if (j < BBS_max_client_per_ip)
# Line 781  int net_server(const char *hostaddr, in_ Line 802  int net_server(const char *hostaddr, in_
802                                                          }                                                          }
803                                                          else if (pid > 0)                                                          else if (pid > 0)
804                                                          {                                                          {
805                                                                  i = 0;                                                                  ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr);
806                                                                  for (; i < BBS_max_client; i++)                                                                  if (ret < 0)
807                                                                  {                                                                  {
808                                                                          if (process_sockaddr_pool[i].pid == 0)                                                                          log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client);
                                                                         {  
                                                                                 break;  
                                                                         }  
809                                                                  }                                                                  }
810    
811                                                                  if (i >= BBS_max_client)                                                                  ret = hash_dict_set(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, j + 1);
812                                                                    if (ret < 0)
813                                                                  {                                                                  {
814                                                                          log_error("Process sockaddr pool depleted\n");                                                                          log_error("hash_dict_set(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, j + 1);
                                                                 }  
                                                                 else  
                                                                 {  
                                                                         process_sockaddr_pool[i].pid = pid;  
                                                                         process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr;  
815                                                                  }                                                                  }
816                                                          }                                                          }
817                                                  }                                                  }
818                                                    else
819                                                    {
820                                                            log_error("Rejected client connection from %s over limit per IP (%d)\n", hostaddr_client, BBS_max_client_per_ip);
821                                                    }
822                                          }                                          }
823                                          else                                          else
824                                          {                                          {
# Line 831  int net_server(const char *hostaddr, in_ Line 849  int net_server(const char *hostaddr, in_
849                  }                  }
850          }          }
851    
852            hash_dict_destroy(hash_dict_pid_sockaddr);
853            hash_dict_destroy(hash_dict_sockaddr_count);
854    
855          ssh_bind_free(sshbind);          ssh_bind_free(sshbind);
856          ssh_finalize();          ssh_finalize();
857    


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

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