/[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.97 by sysadm, Fri Nov 28 03:23:58 2025 UTC Revision 1.110 by sysadm, Thu Dec 18 07:58:23 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 507  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 521  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);
         int i, j;  
531          pid_t pid;          pid_t pid;
532          int ssh_key_valid = 0;          int ssh_key_valid = 0;
533          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
# Line 536  int net_server(const char *hostaddr, in_ Line 542  int net_server(const char *hostaddr, in_
542    
543          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_RSA_KEY_FILE) < 0)          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_RSA_KEY_FILE) < 0)
544          {          {
545                  log_error("Error setting SSH RSA key: %s\n", SSH_HOST_RSA_KEY_FILE);                  log_error("Error loading SSH RSA key: %s\n", SSH_HOST_RSA_KEY_FILE);
546          }          }
547          else          else
548          {          {
# Line 544  int net_server(const char *hostaddr, in_ Line 550  int net_server(const char *hostaddr, in_
550          }          }
551          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ED25519_KEY_FILE) < 0)          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ED25519_KEY_FILE) < 0)
552          {          {
553                  log_error("Error setting SSH ED25519 key: %s\n", SSH_HOST_ED25519_KEY_FILE);                  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          else
564          {          {
# Line 560  int net_server(const char *hostaddr, in_ Line 574  int net_server(const char *hostaddr, in_
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_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256,ssh-ed25519") < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "+ssh-ed25519,ecdsa-sha2-nistp256,ssh-rsa") < 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 578  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 638  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 694  int net_server(const char *hostaddr, in_ Line 710  int net_server(const char *hostaddr, in_
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 791  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                            // Get EULA modification tm
811                            if (stat(DATA_EULA, &file_stat) == -1)
812                            {
813                                    log_error("stat(%s) error\n", DATA_EULA, errno);
814                            }
815                            else
816                            {
817                                    BBS_eula_tm = file_stat.st_mtim.tv_sec;
818                            }
819    
820                          if (detach_menu_shm(&bbs_menu) < 0)                          if (detach_menu_shm(&bbs_menu) < 0)
821                          {                          {
822                                  log_error("detach_menu_shm(bbs_menu) error\n");                                  log_error("detach_menu_shm(bbs_menu) error\n");
# Line 810  int net_server(const char *hostaddr, in_ Line 836  int net_server(const char *hostaddr, in_
836                                  log_error("load_menu(top10_menu) error\n");                                  log_error("load_menu(top10_menu) error\n");
837                                  unload_menu(&top10_menu);                                  unload_menu(&top10_menu);
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 907  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 929  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 965  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