/[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.82 by sysadm, Fri Nov 14 08:45:25 2025 UTC Revision 1.87 by sysadm, Mon Nov 17 02:32:42 2025 UTC
# Line 43  Line 43 
43  #include <sys/syscall.h>  #include <sys/syscall.h>
44  #include <sys/types.h>  #include <sys/types.h>
45  #include <sys/wait.h>  #include <sys/wait.h>
46    
47    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
48  #include <systemd/sd-daemon.h>  #include <systemd/sd-daemon.h>
49    #endif
50    
51  enum _net_server_constant_t  enum _net_server_constant_t
52  {  {
# Line 53  enum _net_server_constant_t Line 56  enum _net_server_constant_t
56          SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds          SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds
57  };  };
58    
 static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server";  
   
59  /* A userdata struct for session. */  /* A userdata struct for session. */
60  struct session_data_struct  struct session_data_struct
61  {  {
# Line 81  struct channel_data_struct Line 82  struct channel_data_struct
82          struct winsize *winsize;          struct winsize *winsize;
83  };  };
84    
85    static int socket_server[2];
86    static int socket_client;
87    static int epollfd_server = -1;
88    static ssh_bind sshbind;
89    
90    static HASH_DICT *hash_dict_pid_sockaddr = NULL;
91    static HASH_DICT *hash_dict_sockaddr_count = NULL;
92    
93    static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server";
94    
95  static int auth_password(ssh_session session, const char *user,  static int auth_password(ssh_session session, const char *user,
96                                                   const char *password, void *userdata)                                                   const char *password, void *userdata)
97  {  {
# Line 307  static int fork_server(void) Line 318  static int fork_server(void)
318          }          }
319    
320          // Child process          // Child process
321            if (close(epollfd_server) < 0)
322            {
323                    log_error("close(epollfd_server) error (%d)\n");
324            }
325    
326          if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1)          for (i = 0; i < 2; i++)
327          {          {
328                  log_error("Close server socket failed\n");                  if (close(socket_server[i]) == -1)
329                    {
330                            log_error("Close server socket failed\n");
331                    }
332          }          }
333    
334            hash_dict_destroy(hash_dict_pid_sockaddr);
335            hash_dict_destroy(hash_dict_sockaddr_count);
336    
337          SSH_session = ssh_new();          SSH_session = ssh_new();
338    
339          if (SSH_v2)          if (SSH_v2)
# Line 393  static int fork_server(void) Line 414  static int fork_server(void)
414          }          }
415    
416          // Redirect Input          // Redirect Input
         close(STDIN_FILENO);  
417          if (dup2(socket_client, STDIN_FILENO) == -1)          if (dup2(socket_client, STDIN_FILENO) == -1)
418          {          {
419                  log_error("Redirect stdin to client socket failed\n");                  log_error("Redirect stdin to client socket failed\n");
# Line 401  static int fork_server(void) Line 421  static int fork_server(void)
421          }          }
422    
423          // Redirect Output          // Redirect Output
         close(STDOUT_FILENO);  
424          if (dup2(socket_client, STDOUT_FILENO) == -1)          if (dup2(socket_client, STDOUT_FILENO) == -1)
425          {          {
426                  log_error("Redirect stdout to client socket failed\n");                  log_error("Redirect stdout to client socket failed\n");
427                  goto cleanup;                  goto cleanup;
428          }          }
429    
430            if (io_init() < 0)
431            {
432                    log_error("io_init() error\n");
433                    goto cleanup;
434            }
435    
436          SYS_child_process_count = 0;          SYS_child_process_count = 0;
437    
438          bbs_main();          bbs_main();
# Line 418  cleanup: Line 443  cleanup:
443    
444          if (SSH_v2)          if (SSH_v2)
445          {          {
446                  close(cdata.pty_master);                  if (cdata.pty_master != -1)
447                  close(cdata.child_stdin);                  {
448                  close(cdata.child_stdout);                          close(cdata.pty_master);
449                  close(cdata.child_stderr);                  }
450                    if (cdata.child_stdin != -1)
451                    {
452                            close(cdata.child_stdin);
453                    }
454                    if (cdata.child_stdout != -1)
455                    {
456                            close(cdata.child_stdout);
457                    }
458                    if (cdata.child_stderr != -1)
459                    {
460                            close(cdata.child_stderr);
461                    }
462    
463                  ssh_channel_free(SSH_channel);                  ssh_channel_free(SSH_channel);
464                  ssh_disconnect(SSH_session);                  ssh_disconnect(SSH_session);
# Line 435  cleanup: Line 472  cleanup:
472          ssh_finalize();          ssh_finalize();
473    
474          // Close Input and Output for client          // Close Input and Output for client
475            io_cleanup();
476          close(STDIN_FILENO);          close(STDIN_FILENO);
477          close(STDOUT_FILENO);          close(STDOUT_FILENO);
478    
# Line 448  cleanup: Line 486  cleanup:
486    
487  int net_server(const char *hostaddr, in_port_t port[])  int net_server(const char *hostaddr, in_port_t port[])
488  {  {
         HASH_DICT *hash_dict_pid_sockaddr = NULL;  
         HASH_DICT *hash_dict_sockaddr_count = NULL;  
   
489          unsigned int addrlen;          unsigned int addrlen;
490          int ret;          int ret;
491          int flags[2];          int flags_server[2];
492          struct sockaddr_in sin;          struct sockaddr_in sin;
493          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
494          int nfds, epollfd;          int nfds;
495          siginfo_t siginfo;          siginfo_t siginfo;
496          int notify_child_exit = 0;          int notify_child_exit = 0;
497          time_t tm_notify_child_exit = time(NULL);          time_t tm_notify_child_exit = time(NULL);
         int sd_notify_stopping = 0;  
498          MENU_SET bbs_menu_new;          MENU_SET bbs_menu_new;
499          MENU_SET top10_menu_new;          MENU_SET top10_menu_new;
500          int i, j;          int i, j;
501          pid_t pid;          pid_t pid;
502          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
503    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
504            int sd_notify_stopping = 0;
505    #endif
506    
507          ssh_init();          ssh_init();
508    
# Line 482  int net_server(const char *hostaddr, in_ Line 519  int net_server(const char *hostaddr, in_
519                  return -1;                  return -1;
520          }          }
521    
522          epollfd = epoll_create1(0);          epollfd_server = epoll_create1(0);
523          if (epollfd < 0)          if (epollfd_server == -1)
524          {          {
525                  log_error("epoll_create1() error (%d)\n", errno);                  log_error("epoll_create1() error (%d)\n", errno);
526                  return -1;                  return -1;
# Line 505  int net_server(const char *hostaddr, in_ Line 542  int net_server(const char *hostaddr, in_
542                  sin.sin_port = htons(port[i]);                  sin.sin_port = htons(port[i]);
543    
544                  // Reuse address and port                  // Reuse address and port
545                  flags[i] = 1;                  flags_server[i] = 1;
546                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags[i], sizeof(flags[i])) < 0)                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags_server[i], sizeof(flags_server[i])) < 0)
547                  {                  {
548                          log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);                          log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);
549                  }                  }
550                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags[i], sizeof(flags[i])) < 0)                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0)
551                  {                  {
552                          log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);                          log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);
553                  }                  }
# Line 532  int net_server(const char *hostaddr, in_ Line 569  int net_server(const char *hostaddr, in_
569    
570                  ev.events = EPOLLIN;                  ev.events = EPOLLIN;
571                  ev.data.fd = socket_server[i];                  ev.data.fd = socket_server[i];
572                  if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server[i], &ev) == -1)                  if (epoll_ctl(epollfd_server, EPOLL_CTL_ADD, socket_server[i], &ev) == -1)
573                  {                  {
574                          log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno);                          log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno);
575                          if (close(epollfd) < 0)                          if (close(epollfd_server) < 0)
576                          {                          {
577                                  log_error("close(epoll) error (%d)\n");                                  log_error("close(epoll) error (%d)\n");
578                          }                          }
579                          return -1;                          return -1;
580                  }                  }
581    
582                  flags[i] = fcntl(socket_server[i], F_GETFL, 0);                  flags_server[i] = fcntl(socket_server[i], F_GETFL, 0);
583                  fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);                  fcntl(socket_server[i], F_SETFL, flags_server[i] | O_NONBLOCK);
584          }          }
585    
586          hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);          hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);
# Line 560  int net_server(const char *hostaddr, in_ Line 597  int net_server(const char *hostaddr, in_
597          }          }
598    
599          // Startup complete          // Startup complete
600    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
601          sd_notifyf(0, "READY=1\n"          sd_notifyf(0, "READY=1\n"
602                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"                                    "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"
603                                    "MAINPID=%d",                                    "MAINPID=%d",
604                             hostaddr, port[0], hostaddr, port[1], getpid());                             hostaddr, port[0], hostaddr, port[1], getpid());
605    #endif
606    
607          while (!SYS_server_exit || SYS_child_process_count > 0)          while (!SYS_server_exit || SYS_child_process_count > 0)
608          {          {
609    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
610                  if (SYS_server_exit && !sd_notify_stopping)                  if (SYS_server_exit && !sd_notify_stopping)
611                  {                  {
612                          sd_notify(0, "STOPPING=1");                          sd_notify(0, "STOPPING=1");
613                          sd_notify_stopping = 1;                          sd_notify_stopping = 1;
614                  }                  }
615    #endif
616    
617                  while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)                  while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)
618                  {                  {
# Line 596  int net_server(const char *hostaddr, in_ Line 637  int net_server(const char *hostaddr, in_
637                                          }                                          }
638                                          else                                          else
639                                          {                                          {
640                                                  sin.sin_addr.s_addr = (uint32_t)j;                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)j, -1);
                                                 j = 0;  
                                                 ret = hash_dict_get(hash_dict_sockaddr_count, sin.sin_addr.s_addr, (int64_t *)&j);  
641                                                  if (ret < 0)                                                  if (ret < 0)
642                                                  {                                                  {
643                                                          log_error("hash_dict_get(hash_dict_sockaddr_count, %d) error\n", sin.sin_addr.s_addr);                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error\n", j);
                                                 }  
                                                 else if (ret == 0)  
                                                 {  
                                                         log_error("hash_dict_get(hash_dict_sockaddr_count, %d) not found\n", sin.sin_addr.s_addr);  
                                                         j = 1;  
                                                 }  
   
                                                 j--;  
                                                 ret = hash_dict_set(hash_dict_sockaddr_count, sin.sin_addr.s_addr, j);  
                                                 if (ret < 0)  
                                                 {  
                                                         log_error("hash_dict_set(hash_dict_sockaddr_count, %d, %d) error\n", sin.sin_addr.s_addr, j);  
644                                                  }                                                  }
645    
646                                                  ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid);                                                  ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid);
# Line 639  int net_server(const char *hostaddr, in_ Line 666  int net_server(const char *hostaddr, in_
666                  {                  {
667                          if (notify_child_exit == 0)                          if (notify_child_exit == 0)
668                          {                          {
669    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
670                                  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);
671                                  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);
672    #endif
673    
674                                  if (kill(-getpid(), SIGTERM) < 0)                                  if (kill(-getpid(), SIGTERM) < 0)
675                                  {                                  {
# Line 652  int net_server(const char *hostaddr, in_ Line 681  int net_server(const char *hostaddr, in_
681                          }                          }
682                          else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)                          else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)
683                          {                          {
684    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
685                                  sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);                                  sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
686    #endif
687    
688                                  if (kill(-getpid(), SIGKILL) < 0)                                  if (kill(-getpid(), SIGKILL) < 0)
689                                  {                                  {
# Line 672  int net_server(const char *hostaddr, in_ Line 703  int net_server(const char *hostaddr, in_
703                  if (SYS_conf_reload && !SYS_server_exit)                  if (SYS_conf_reload && !SYS_server_exit)
704                  {                  {
705                          SYS_conf_reload = 0;                          SYS_conf_reload = 0;
706    
707    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
708                          sd_notify(0, "RELOADING=1");                          sd_notify(0, "RELOADING=1");
709    #endif
710    
711                            // Restart log
712                            if (log_restart() < 0)
713                            {
714                                    log_error("Restart logging failed\n");
715                            }
716    
717                          // Reload configuration                          // Reload configuration
718                          if (load_conf(CONF_BBSD) < 0)                          if (load_conf(CONF_BBSD) < 0)
# Line 730  int net_server(const char *hostaddr, in_ Line 770  int net_server(const char *hostaddr, in_
770                                  log_common("Reload section config and gen_ex successfully\n");                                  log_common("Reload section config and gen_ex successfully\n");
771                          }                          }
772    
773                            // Notify child processes to reload configuration
774                            if (kill(-getpid(), SIGUSR1) < 0)
775                            {
776                                    log_error("Send SIGUSR1 signal failed (%d)\n", errno);
777                            }
778    
779    #ifdef HAVE_SYSTEMD_SD_DAEMON_H
780                          sd_notify(0, "READY=1");                          sd_notify(0, "READY=1");
781    #endif
782                  }                  }
783    
784                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd_server, events, MAX_EVENTS, 100); // 0.1 second
785    
786                  if (nfds < 0)                  if (nfds < 0)
787                  {                  {
# Line 808  int net_server(const char *hostaddr, in_ Line 856  int net_server(const char *hostaddr, in_
856                                                                          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, %d, %s) error\n", pid, hostaddr_client);
857                                                                  }                                                                  }
858    
859                                                                  ret = hash_dict_set(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, j + 1);                                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);
860                                                                  if (ret < 0)                                                                  if (ret < 0)
861                                                                  {                                                                  {
862                                                                          log_error("hash_dict_set(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, j + 1);                                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, 1);
863                                                                  }                                                                  }
864                                                          }                                                          }
865                                                  }                                                  }
# Line 834  int net_server(const char *hostaddr, in_ Line 882  int net_server(const char *hostaddr, in_
882                  }                  }
883          }          }
884    
885          if (close(epollfd) < 0)          if (close(epollfd_server) < 0)
886          {          {
887                  log_error("close(epoll) error (%d)\n");                  log_error("close(epollfd_server) error (%d)\n");
888          }          }
889    
890          for (i = 0; i < 2; i++)          for (i = 0; i < 2; i++)
891          {          {
                 fcntl(socket_server[i], F_SETFL, flags[i]);  
   
892                  if (close(socket_server[i]) == -1)                  if (close(socket_server[i]) == -1)
893                  {                  {
894                          log_error("Close server socket failed\n");                          log_error("Close server socket failed\n");


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

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