/[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.88 by sysadm, Mon Nov 17 06:41:18 2025 UTC Revision 1.111 by sysadm, Thu Dec 18 14:47:00 2025 UTC
# Line 38  Line 38 
38  #include <libssh/libssh.h>  #include <libssh/libssh.h>
39  #include <libssh/server.h>  #include <libssh/server.h>
40  #include <netinet/in.h>  #include <netinet/in.h>
41    #include <sys/ioctl.h>
42  #include <sys/socket.h>  #include <sys/socket.h>
43  #include <sys/syscall.h>  #include <sys/stat.h>
44  #include <sys/types.h>  #include <sys/types.h>
45  #include <sys/wait.h>  #include <sys/wait.h>
46    
# Line 114  static int auth_password(ssh_session ses Line 115  static int auth_password(ssh_session ses
115          else          else
116          {          {
117                  ret = check_user(user, password);                  ret = check_user(user, password);
118                    if (ret == 2) // Enforce update user agreement
119                    {
120                            BBS_update_eula = 1;
121                            ret = 0;
122                    }
123          }          }
124    
125          if (ret == 0)          if (ret == 0)
# Line 385  static int fork_server(void) Line 391  static int fork_server(void)
391                          ret = ssh_event_dopoll(event, 100); // 0.1 second                          ret = ssh_event_dopoll(event, 100); // 0.1 second
392                          if (ret == SSH_ERROR)                          if (ret == SSH_ERROR)
393                          {                          {
394  #ifdef _DEBUG                                  log_debug("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));
                                 log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));  
 #endif  
395                                  goto cleanup;                                  goto cleanup;
396                          }                          }
397                  }                  }
# Line 422  static int fork_server(void) Line 426  static int fork_server(void)
426                          log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));                          log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
427                          goto cleanup;                          goto cleanup;
428                  }                  }
429    
430                    ssh_set_blocking(SSH_session, 0);
431          }          }
432    
433          // Redirect Input          // Redirect Input
# Line 446  static int fork_server(void) Line 452  static int fork_server(void)
452    
453          SYS_child_process_count = 0;          SYS_child_process_count = 0;
454    
455            // BWF compile
456            if (bwf_compile() < 0)
457            {
458                    log_error("bwf_compile() error\n");
459                    goto cleanup;
460            }
461    
462          bbs_main();          bbs_main();
463    
464  cleanup:  cleanup:
# Line 482  cleanup: Line 495  cleanup:
495          ssh_free(SSH_session);          ssh_free(SSH_session);
496          ssh_finalize();          ssh_finalize();
497    
498            // BWF cleanup
499            bwf_cleanup();
500    
501          // Close Input and Output for client          // Close Input and Output for client
502          io_cleanup();          io_cleanup();
503          close(STDIN_FILENO);          close(STDIN_FILENO);
# Line 497  cleanup: Line 513  cleanup:
513    
514  int net_server(const char *hostaddr, in_port_t port[])  int net_server(const char *hostaddr, in_port_t port[])
515  {  {
516            struct stat file_stat;
517          unsigned int addrlen;          unsigned int addrlen;
518          int ret;          int ret;
519          int flags_server[2];          int flags_server[2];
# Line 509  int net_server(const char *hostaddr, in_ Line 526  int net_server(const char *hostaddr, in_
526  #endif  #endif
527    
528          int nfds;          int nfds;
         siginfo_t siginfo;  
529          int notify_child_exit = 0;          int notify_child_exit = 0;
530          time_t tm_notify_child_exit = time(NULL);          time_t tm_notify_child_exit = time(NULL);
         MENU_SET bbs_menu_new;  
         MENU_SET top10_menu_new;  
         int i, j;  
531          pid_t pid;          pid_t pid;
532            int ssh_key_valid = 0;
533          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
534    
535  #ifdef HAVE_SYSTEMD_SD_DAEMON_H  #ifdef HAVE_SYSTEMD_SD_DAEMON_H
# Line 526  int net_server(const char *hostaddr, in_ Line 540  int net_server(const char *hostaddr, in_
540    
541          sshbind = ssh_bind_new();          sshbind = ssh_bind_new();
542    
543            if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_RSA_KEY_FILE) < 0)
544            {
545                    log_error("Error loading SSH RSA key: %s\n", SSH_HOST_RSA_KEY_FILE);
546            }
547            else
548            {
549                    ssh_key_valid = 1;
550            }
551            if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ED25519_KEY_FILE) < 0)
552            {
553                    log_error("Error loading SSH ED25519 key: %s\n", SSH_HOST_ED25519_KEY_FILE);
554            }
555            else
556            {
557                    ssh_key_valid = 1;
558            }
559            if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ECDSA_KEY_FILE) < 0)
560            {
561                    log_error("Error loading SSH ECDSA key: %s\n", SSH_HOST_ECDSA_KEY_FILE);
562            }
563            else
564            {
565                    ssh_key_valid = 1;
566            }
567    
568            if (!ssh_key_valid)
569            {
570                    log_error("Error: no valid SSH host key\n");
571                    ssh_bind_free(sshbind);
572                    return -1;
573            }
574    
575          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
576                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
577                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "+ssh-ed25519,ecdsa-sha2-nistp256,ssh-rsa") < 0 ||
                 ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 ||  
578                  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)
579          {          {
580                  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 547  int net_server(const char *hostaddr, in_ Line 592  int net_server(const char *hostaddr, in_
592  #endif  #endif
593    
594          // Server socket          // Server socket
595          for (i = 0; i < 2; i++)          for (int i = 0; i < 2; i++)
596          {          {
597                  socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);                  socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
598    
# Line 567  int net_server(const char *hostaddr, in_ Line 612  int net_server(const char *hostaddr, in_
612                  {                  {
613                          log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);                          log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);
614                  }                  }
615    #if defined(SO_REUSEPORT)
616                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0)                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0)
617                  {                  {
618                          log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);                          log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);
619                  }                  }
620    #endif
621    
622                  if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0)                  if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0)
623                  {                  {
# Line 605  int net_server(const char *hostaddr, in_ Line 652  int net_server(const char *hostaddr, in_
652                  fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK);                  fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK);
653          }          }
654    
655            ssh_bind_set_blocking(sshbind, 0);
656    
657          hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);          hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);
658          if (hash_dict_pid_sockaddr == NULL)          if (hash_dict_pid_sockaddr == NULL)
659          {          {
# Line 640  int net_server(const char *hostaddr, in_ Line 689  int net_server(const char *hostaddr, in_
689                  {                  {
690                          SYS_child_exit = 0;                          SYS_child_exit = 0;
691    
692                          siginfo.si_pid = 0;                          pid = waitpid(-1, &ret, WNOHANG);
693                          ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG);                          if (pid > 0)
                         if (ret == 0 && siginfo.si_pid > 0)  
694                          {                          {
695                                  SYS_child_exit = 1; // Retry waitid                                  SYS_child_exit = 1; // Retry waitid
   
696                                  SYS_child_process_count--;                                  SYS_child_process_count--;
                                 log_common("Child process (%d) exited\n", siginfo.si_pid);  
697    
698                                  if (siginfo.si_pid != section_list_loader_pid)                                  if (WIFEXITED(ret))
699                                    {
700                                            log_common("Child process (%d) exited, status=%d\n", pid, WEXITSTATUS(ret));
701                                    }
702                                    else if (WIFSIGNALED(ret))
703                                    {
704                                            log_common("Child process (%d) is killed, status=%d\n", pid, WTERMSIG(ret));
705                                    }
706                                    else
707                                    {
708                                            log_common("Child process (%d) exited abnormally, status=%d\n", pid, ret);
709                                    }
710    
711                                    if (pid != section_list_loader_pid)
712                                  {                                  {
713                                          j = 0;                                          int64_t j = 0;
714                                          ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid, (int64_t *)&j);                                          ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)pid, &j);
715                                          if (ret < 0)                                          if (ret < 0)
716                                          {                                          {
717                                                  log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);                                                  log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", pid);
718                                          }                                          }
719                                          else                                          else
720                                          {                                          {
721                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)j, -1);                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (in_addr_t)j, -1);
722                                                  if (ret < 0)                                                  if (ret <= 0)
723                                                  {                                                  {
724                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error\n", j);                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %lu, -1) error: %d\n", (in_addr_t)j, ret);
725                                                  }                                                  }
726    
727                                                  ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid);                                                  ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)pid);
728                                                  if (ret < 0)                                                  if (ret < 0)
729                                                  {                                                  {
730                                                          log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);                                                          log_error("hash_dict_del(hash_dict_pid_sockaddr, %lu) error\n", (uint64_t)pid);
731                                                  }                                                  }
732                                          }                                          }
733                                  }                                  }
734                          }                          }
735                          else if (ret == 0)                          else if (pid == 0)
736                          {                          {
737                                  break;                                  break;
738                          }                          }
739                          else if (ret < 0)                          else if (pid < 0)
740                          {                          {
741                                  log_error("Error in waitid: %d\n", errno);                                  log_error("Error in waitpid(): %d\n", errno);
742                                  break;                                  break;
743                          }                          }
744                  }                  }
# Line 693  int net_server(const char *hostaddr, in_ Line 752  int net_server(const char *hostaddr, in_
752                                  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);
753  #endif  #endif
754    
755                                  if (kill(-getpid(), SIGTERM) < 0)                                  if (kill(0, SIGTERM) < 0)
756                                  {                                  {
757                                          log_error("Send SIGTERM signal failed (%d)\n", errno);                                          log_error("Send SIGTERM signal failed (%d)\n", errno);
758                                  }                                  }
# Line 707  int net_server(const char *hostaddr, in_ Line 766  int net_server(const char *hostaddr, in_
766                                  sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);                                  sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
767  #endif  #endif
768    
769                                  if (kill(-getpid(), SIGKILL) < 0)                                  if (kill(0, SIGKILL) < 0)
770                                  {                                  {
771                                          log_error("Send SIGKILL signal failed (%d)\n", errno);                                          log_error("Send SIGKILL signal failed (%d)\n", errno);
772                                  }                                  }
# Line 730  int net_server(const char *hostaddr, in_ Line 789  int net_server(const char *hostaddr, in_
789                          sd_notify(0, "RELOADING=1");                          sd_notify(0, "RELOADING=1");
790  #endif  #endif
791    
792                            log_common("Reload configuration\n");
793                            
794                          // Restart log                          // Restart log
795                          if (log_restart() < 0)                          if (log_restart() < 0)
796                          {                          {
# Line 748  int net_server(const char *hostaddr, in_ Line 809  int net_server(const char *hostaddr, in_
809                                  log_error("Reload BWF conf failed\n");                                  log_error("Reload BWF conf failed\n");
810                          }                          }
811    
812                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)                          // Get EULA modification tm
813                            if (stat(DATA_EULA, &file_stat) == -1)
814                          {                          {
815                                  unload_menu(&bbs_menu_new);                                  log_error("stat(%s) error\n", DATA_EULA, errno);
                                 log_error("Reload bbs menu failed\n");  
816                          }                          }
817                          else                          else
818                          {                          {
819                                    BBS_eula_tm = file_stat.st_mtim.tv_sec;
820                            }
821    
822                            if (detach_menu_shm(&bbs_menu) < 0)
823                            {
824                                    log_error("detach_menu_shm(bbs_menu) error\n");
825                            }
826                            if (load_menu(&bbs_menu, CONF_MENU) < 0)
827                            {
828                                    log_error("load_menu(bbs_menu) error\n");
829                                  unload_menu(&bbs_menu);                                  unload_menu(&bbs_menu);
                                 memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));  
                                 log_common("Reload bbs menu successfully\n");  
830                          }                          }
831    
832                          if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)                          if (detach_menu_shm(&top10_menu) < 0)
833                          {                          {
834                                  unload_menu(&top10_menu_new);                                  log_error("detach_menu_shm(top10_menu) error\n");
                                 log_error("Reload top10 menu failed\n");  
835                          }                          }
836                          else                          if (load_menu(&top10_menu, CONF_TOP10_MENU) < 0)
837                          {                          {
838                                    log_error("load_menu(top10_menu) error\n");
839                                  unload_menu(&top10_menu);                                  unload_menu(&top10_menu);
                                 top10_menu_new.allow_exit = 1;  
                                 memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new));  
                                 log_common("Reload top10 menu successfully\n");  
840                          }                          }
841                            top10_menu.allow_exit = 1;
842    
843                          for (int i = 0; i < data_files_load_startup_count; i++)                          for (int i = 0; i < data_files_load_startup_count; i++)
844                          {                          {
# Line 780  int net_server(const char *hostaddr, in_ Line 847  int net_server(const char *hostaddr, in_
847                                          log_error("load_file(%s) error\n", data_files_load_startup[i]);                                          log_error("load_file(%s) error\n", data_files_load_startup[i]);
848                                  }                                  }
849                          }                          }
                         log_common("Reload data files successfully\n");  
850    
851                          // Load section config and gen_ex                          // Load section config and gen_ex
852                          if (load_section_config_from_db(1) < 0)                          if (load_section_config_from_db(1) < 0)
853                          {                          {
854                                  log_error("load_section_config_from_db(1) error\n");                                  log_error("load_section_config_from_db(1) error\n");
855                          }                          }
                         else  
                         {  
                                 log_common("Reload section config and gen_ex successfully\n");  
                         }  
856    
857                          // Notify child processes to reload configuration                          // Notify child processes to reload configuration
858                          if (kill(-getpid(), SIGUSR1) < 0)                          if (kill(0, SIGUSR1) < 0)
859                          {                          {
860                                  log_error("Send SIGUSR1 signal failed (%d)\n", errno);                                  log_error("Send SIGUSR1 signal failed (%d)\n", errno);
861                          }                          }
# Line 851  int net_server(const char *hostaddr, in_ Line 913  int net_server(const char *hostaddr, in_
913                                  while (!SYS_server_exit) // Accept all incoming connections until error                                  while (!SYS_server_exit) // Accept all incoming connections until error
914                                  {                                  {
915                                          addrlen = sizeof(sin);                                          addrlen = sizeof(sin);
916                                          socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, &addrlen);                                          socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, (socklen_t *)&addrlen);
917                                          if (socket_client < 0)                                          if (socket_client < 0)
918                                          {                                          {
919                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 874  int net_server(const char *hostaddr, in_ Line 936  int net_server(const char *hostaddr, in_
936    
937                                          port_client = ntohs(sin.sin_port);                                          port_client = ntohs(sin.sin_port);
938    
                                         log_common("Accept %s connection from %s:%d\n", (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client);  
   
939                                          if (SYS_child_process_count - 1 < BBS_max_client)                                          if (SYS_child_process_count - 1 < BBS_max_client)
940                                          {                                          {
941                                                  j = 0;                                                  int64_t j = 0;
942                                                  ret = hash_dict_get(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, (int64_t *)&j);                                                  ret = hash_dict_get(hash_dict_sockaddr_count, sin.sin_addr.s_addr, &j);
943                                                  if (ret < 0)                                                  if (ret < 0)
944                                                  {                                                  {
945                                                          log_error("hash_dict_get(hash_dict_sockaddr_count, %s) error\n", hostaddr_client);                                                          log_error("hash_dict_get(hash_dict_sockaddr_count, %s) error\n", hostaddr_client);
# Line 896  int net_server(const char *hostaddr, in_ Line 956  int net_server(const char *hostaddr, in_
956                                                                  ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr);                                                                  ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr);
957                                                                  if (ret < 0)                                                                  if (ret < 0)
958                                                                  {                                                                  {
959                                                                          log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client);                                                                          log_error("hash_dict_set(hash_dict_pid_sockaddr, %lu, %s) error\n", (uint64_t)pid, hostaddr_client);
960                                                                  }                                                                  }
961    
962                                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);                                                                  if (j == 0)
                                                                 if (ret < 0)  
963                                                                  {                                                                  {
964                                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, 1);                                                                          // First connection from this IP
965                                                                            log_common("Accept %s connection from %s:%d\n",
966                                                                                               (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client);
967    
968                                                                            ret = hash_dict_set(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);
969                                                                            if (ret < 0)
970                                                                            {
971                                                                                    log_error("hash_dict_set(hash_dict_sockaddr_count, %s, 1) error\n", hostaddr_client);
972                                                                            }
973                                                                    }
974                                                                    else
975                                                                    {
976                                                                            // Increase connection count from this IP
977                                                                            log_common("Accept %s connection from %s:%d, already have %d connections\n",
978                                                                                               (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, j);
979    
980                                                                            ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);
981                                                                            if (ret <= 0)
982                                                                            {
983                                                                                    log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, 1) error: %d\n", hostaddr_client, ret);
984                                                                            }
985                                                                  }                                                                  }
986                                                          }                                                          }
987                                                  }                                                  }
988                                                  else                                                  else
989                                                  {                                                  {
990                                                          log_error("Rejected client connection from %s over limit per IP (%d)\n", hostaddr_client, BBS_max_client_per_ip);                                                          log_error("Rejected %s connection from %s:%d over limit per IP (%d >= %d)\n",
991                                                                              (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, j, BBS_max_client_per_ip);
992                                                  }                                                  }
993                                          }                                          }
994                                          else                                          else
995                                          {                                          {
996                                                  log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1);                                                  log_error("Rejected %s connection from %s:%d over limit (%d >= %d)\n",
997                                                                      (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, SYS_child_process_count - 1, BBS_max_client);
998                                          }                                          }
999    
1000                                          if (close(socket_client) == -1)                                          if (close(socket_client) == -1)
# Line 932  int net_server(const char *hostaddr, in_ Line 1013  int net_server(const char *hostaddr, in_
1013          }          }
1014  #endif  #endif
1015    
1016          for (i = 0; i < 2; i++)          for (int i = 0; i < 2; i++)
1017          {          {
1018                  if (close(socket_server[i]) == -1)                  if (close(socket_server[i]) == -1)
1019                  {                  {


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

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