/[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.91 by sysadm, Mon Nov 17 09:46:47 2025 UTC Revision 1.109 by sysadm, Thu Dec 18 02:56:00 2025 UTC
# Line 40  Line 40 
40  #include <netinet/in.h>  #include <netinet/in.h>
41  #include <sys/ioctl.h>  #include <sys/ioctl.h>
42  #include <sys/socket.h>  #include <sys/socket.h>
43    #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 511  int net_server(const char *hostaddr, in_ Line 528  int net_server(const char *hostaddr, in_
528          int nfds;          int nfds;
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 525  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-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 546  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 606  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 641  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                          pid = waitpid(-1, NULL, WNOHANG);                          pid = waitpid(-1, &ret, WNOHANG);
693                          if (pid > 0)                          if (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--;
697                                  log_common("Child process (%d) exited\n", pid);  
698                                    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)                                  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)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", 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)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", pid);                                                          log_error("hash_dict_del(hash_dict_pid_sockaddr, %lu) error\n", (uint64_t)pid);
731                                                  }                                                  }
732                                          }                                          }
733                                  }                                  }
# 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 748  int net_server(const char *hostaddr, in_ Line 807  int net_server(const char *hostaddr, in_
807                                  log_error("Reload BWF conf failed\n");                                  log_error("Reload BWF conf failed\n");
808                          }                          }
809    
810                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)                          // Get EULA modification tm
811                            if (stat(DATA_EULA, &file_stat) == -1)
812                          {                          {
813                                  unload_menu(&bbs_menu_new);                                  log_error("stat(%s) error\n", DATA_EULA, errno);
                                 log_error("Reload bbs menu failed\n");  
814                          }                          }
815                          else                          else
816                          {                          {
817                                    BBS_eula_tm = file_stat.st_mtim.tv_sec;
818                            }
819    
820                            if (detach_menu_shm(&bbs_menu) < 0)
821                            {
822                                    log_error("detach_menu_shm(bbs_menu) error\n");
823                            }
824                            if (load_menu(&bbs_menu, CONF_MENU) < 0)
825                            {
826                                    log_error("load_menu(bbs_menu) error\n");
827                                  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");  
828                          }                          }
829    
830                          if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)                          if (detach_menu_shm(&top10_menu) < 0)
831                          {                          {
832                                  unload_menu(&top10_menu_new);                                  log_error("detach_menu_shm(top10_menu) error\n");
                                 log_error("Reload top10 menu failed\n");  
833                          }                          }
834                          else                          if (load_menu(&top10_menu, CONF_TOP10_MENU) < 0)
835                          {                          {
836                                    log_error("load_menu(top10_menu) error\n");
837                                  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");  
838                          }                          }
839                            top10_menu.allow_exit = 1;
840    
841                          for (int i = 0; i < data_files_load_startup_count; i++)                          for (int i = 0; i < data_files_load_startup_count; i++)
842                          {                          {
# Line 780  int net_server(const char *hostaddr, in_ Line 845  int net_server(const char *hostaddr, in_
845                                          log_error("load_file(%s) error\n", data_files_load_startup[i]);                                          log_error("load_file(%s) error\n", data_files_load_startup[i]);
846                                  }                                  }
847                          }                          }
                         log_common("Reload data files successfully\n");  
848    
849                          // Load section config and gen_ex                          // Load section config and gen_ex
850                          if (load_section_config_from_db(1) < 0)                          if (load_section_config_from_db(1) < 0)
851                          {                          {
852                                  log_error("load_section_config_from_db(1) error\n");                                  log_error("load_section_config_from_db(1) error\n");
853                          }                          }
                         else  
                         {  
                                 log_common("Reload section config and gen_ex successfully\n");  
                         }  
854    
855                          // Notify child processes to reload configuration                          // Notify child processes to reload configuration
856                          if (kill(-getpid(), SIGUSR1) < 0)                          if (kill(0, SIGUSR1) < 0)
857                          {                          {
858                                  log_error("Send SIGUSR1 signal failed (%d)\n", errno);                                  log_error("Send SIGUSR1 signal failed (%d)\n", errno);
859                          }                          }
# Line 874  int net_server(const char *hostaddr, in_ Line 934  int net_server(const char *hostaddr, in_
934    
935                                          port_client = ntohs(sin.sin_port);                                          port_client = ntohs(sin.sin_port);
936    
                                         log_common("Accept %s connection from %s:%d\n", (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client);  
   
937                                          if (SYS_child_process_count - 1 < BBS_max_client)                                          if (SYS_child_process_count - 1 < BBS_max_client)
938                                          {                                          {
939                                                  j = 0;                                                  int64_t j = 0;
940                                                  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);
941                                                  if (ret < 0)                                                  if (ret < 0)
942                                                  {                                                  {
943                                                          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 954  int net_server(const char *hostaddr, in_
954                                                                  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);
955                                                                  if (ret < 0)                                                                  if (ret < 0)
956                                                                  {                                                                  {
957                                                                          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);
958                                                                  }                                                                  }
959    
960                                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);                                                                  if (j == 0)
                                                                 if (ret < 0)  
961                                                                  {                                                                  {
962                                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, 1);                                                                          // First connection from this IP
963                                                                            log_common("Accept %s connection from %s:%d\n",
964                                                                                               (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client);
965    
966                                                                            ret = hash_dict_set(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);
967                                                                            if (ret < 0)
968                                                                            {
969                                                                                    log_error("hash_dict_set(hash_dict_sockaddr_count, %s, 1) error\n", hostaddr_client);
970                                                                            }
971                                                                    }
972                                                                    else
973                                                                    {
974                                                                            // Increase connection count from this IP
975                                                                            log_common("Accept %s connection from %s:%d, already have %d connections\n",
976                                                                                               (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, j);
977    
978                                                                            ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);
979                                                                            if (ret <= 0)
980                                                                            {
981                                                                                    log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, 1) error: %d\n", hostaddr_client, ret);
982                                                                            }
983                                                                  }                                                                  }
984                                                          }                                                          }
985                                                  }                                                  }
986                                                  else                                                  else
987                                                  {                                                  {
988                                                          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",
989                                                                              (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, j, BBS_max_client_per_ip);
990                                                  }                                                  }
991                                          }                                          }
992                                          else                                          else
993                                          {                                          {
994                                                  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",
995                                                                      (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, SYS_child_process_count - 1, BBS_max_client);
996                                          }                                          }
997    
998                                          if (close(socket_client) == -1)                                          if (close(socket_client) == -1)
# Line 932  int net_server(const char *hostaddr, in_ Line 1011  int net_server(const char *hostaddr, in_
1011          }          }
1012  #endif  #endif
1013    
1014          for (i = 0; i < 2; i++)          for (int i = 0; i < 2; i++)
1015          {          {
1016                  if (close(socket_server[i]) == -1)                  if (close(socket_server[i]) == -1)
1017                  {                  {


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

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