/[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.56 by sysadm, Sat Jun 7 02:38:28 2025 UTC Revision 1.67 by sysadm, Wed Jun 25 02:49:20 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #define _XOPEN_SOURCE 500  
 #define _POSIX_C_SOURCE 200809L  
 #define _GNU_SOURCE  
   
 #include "net_server.h"  
 #include "common.h"  
 #include "bbs_main.h"  
17  #include "bbs.h"  #include "bbs.h"
18  #include "log.h"  #include "bbs_main.h"
19    #include "common.h"
20    #include "database.h"
21    #include "file_loader.h"
22  #include "io.h"  #include "io.h"
23  #include "init.h"  #include "init.h"
24  #include "menu.h"  #include "log.h"
 #include "database.h"  
25  #include "login.h"  #include "login.h"
26  #include "file_loader.h"  #include "menu.h"
27    #include "net_server.h"
28    #include "section_list.h"
29  #include "section_list_loader.h"  #include "section_list_loader.h"
30  #include <errno.h>  #include <errno.h>
31  #include <fcntl.h>  #include <fcntl.h>
 #include <string.h>  
32  #include <signal.h>  #include <signal.h>
33  #include <stdlib.h>  #include <stdlib.h>
34    #include <string.h>
35  #include <unistd.h>  #include <unistd.h>
 #include <sys/syscall.h>  
 #include <sys/socket.h>  
 #include <sys/wait.h>  
 #include <sys/epoll.h>  
36  #include <arpa/inet.h>  #include <arpa/inet.h>
37  #include <netinet/in.h>  #include <libssh/callbacks.h>
 #include <systemd/sd-daemon.h>  
38  #include <libssh/libssh.h>  #include <libssh/libssh.h>
39  #include <libssh/server.h>  #include <libssh/server.h>
40  #include <libssh/callbacks.h>  #include <netinet/in.h>
41    #include <sys/epoll.h>
42    #include <sys/socket.h>
43    #include <sys/syscall.h>
44    #include <sys/types.h>
45    #include <sys/wait.h>
46    #include <systemd/sd-daemon.h>
47    
48  struct process_sockaddr_t  struct process_sockaddr_t
49  {  {
# Line 56  typedef struct process_sockaddr_t PROCES Line 54  typedef struct process_sockaddr_t PROCES
54    
55  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];
56    
57  #define SSH_AUTH_MAX_DURATION 60 // seconds  #define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds
58    
59  struct ssl_server_cb_data_t  struct ssl_server_cb_data_t
60  {  {
# Line 67  struct ssl_server_cb_data_t Line 65  struct ssl_server_cb_data_t
65  static int auth_password(ssh_session session, const char *user,  static int auth_password(ssh_session session, const char *user,
66                                                   const char *password, void *userdata)                                                   const char *password, void *userdata)
67  {  {
         MYSQL *db;  
68          struct ssl_server_cb_data_t *p_data = userdata;          struct ssl_server_cb_data_t *p_data = userdata;
69          int ret;          int ret;
70    
         if ((db = db_open()) == NULL)  
         {  
                 return SSH_AUTH_ERROR;  
         }  
   
71          if (strcmp(user, "guest") == 0)          if (strcmp(user, "guest") == 0)
72          {          {
73                  ret = load_guest_info(db);                  ret = load_guest_info();
74          }          }
75          else          else
76          {          {
77                  ret = check_user(db, user, password);                  ret = check_user(user, password);
78          }          }
79    
         mysql_close(db);  
   
80          if (ret == 0)          if (ret == 0)
81          {          {
82                  return SSH_AUTH_SUCCESS;                  return SSH_AUTH_SUCCESS;
# Line 193  static int fork_server(void) Line 183  static int fork_server(void)
183                  event = ssh_event_new();                  event = ssh_event_new();
184                  ssh_event_add_session(event, SSH_session);                  ssh_event_add_session(event, SSH_session);
185    
186                  for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i++)                  for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i += 100)
187                  {                  {
188                          ret = ssh_event_dopoll(event, 1000); // 1 second                          ret = ssh_event_dopoll(event, 100); // 0.1 second
189                          if (ret == SSH_ERROR)                          if (ret == SSH_ERROR)
190                          {                          {
191                                  log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));                                  log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));
# Line 269  int net_server(const char *hostaddr, in_ Line 259  int net_server(const char *hostaddr, in_
259          int nfds, epollfd;          int nfds, epollfd;
260          siginfo_t siginfo;          siginfo_t siginfo;
261          int sd_notify_stopping = 0;          int sd_notify_stopping = 0;
262          MENU_SET *p_bbs_menu_new;          MENU_SET bbs_menu_new;
263          int i, j;          int i, j;
264          pid_t pid;          pid_t pid;
265          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
# Line 281  int net_server(const char *hostaddr, in_ Line 271  int net_server(const char *hostaddr, in_
271          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
272                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
273                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||
274                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 ||
275                  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)
276          {          {
277                  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 429  int net_server(const char *hostaddr, in_ Line 420  int net_server(const char *hostaddr, in_
420                                  log_error("Reload conf failed\n");                                  log_error("Reload conf failed\n");
421                          }                          }
422    
423                          p_bbs_menu_new = calloc(1, sizeof(MENU_SET));                          // acquire rw lock of all sections to avoid conflict with menu reload in data loader process
424                          if (p_bbs_menu_new == NULL)                          ret = section_list_rw_lock(NULL);
425                          {                          if (ret < 0)
                                 log_error("OOM: calloc(MENU_SET)\n");  
                         }  
                         else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0)  
426                          {                          {
427                                  unload_menu(p_bbs_menu_new);                                  log_error("section_list_rw_lock(NULL) error\n");
                                 free(p_bbs_menu_new);  
                                 p_bbs_menu_new = NULL;  
   
                                 log_error("Reload menu failed\n");  
428                          }                          }
429                          else                          else
430                          {                          {
431                                  unload_menu(p_bbs_menu);                                  if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
432                                  free(p_bbs_menu);                                  {
433                                            unload_menu(&bbs_menu_new);
434                                  p_bbs_menu = p_bbs_menu_new;                                          log_error("Reload menu failed\n");
435                                  p_bbs_menu_new = NULL;                                  }
436                                    else
437                                    {
438                                            unload_menu(&bbs_menu);
439                                            memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
440                                            log_common("Reload menu successfully\n");
441                                    }
442    
443                                  log_common("Reload menu successfully\n");                                  // release rw lock of all sections
444                                    ret = section_list_rw_unlock(NULL);
445                                    if (ret < 0)
446                                    {
447                                            log_error("section_list_rw_unlock(NULL) error\n");
448                                    }
449                          }                          }
450    
451                          sd_notify(0, "READY=1");                          sd_notify(0, "READY=1");
# Line 479  int net_server(const char *hostaddr, in_ Line 474  int net_server(const char *hostaddr, in_
474    
475                          if (section_list_loader_reload() < 0)                          if (section_list_loader_reload() < 0)
476                          {                          {
477                                  log_error("ksection_list_loader_reload() failed\n");                                  log_error("section_list_loader_reload() failed\n");
478                          }                          }
479                  }                  }
480    
# Line 533  int net_server(const char *hostaddr, in_ Line 528  int net_server(const char *hostaddr, in_
528    
529                                          port_client = ntohs(sin.sin_port);                                          port_client = ntohs(sin.sin_port);
530    
531                                          log_common("Accept %sconnection from %s:%d\n", (SSH_v2 ? "" : "SSH2 "), hostaddr_client, port_client);                                          log_common("Accept %s connection from %s:%d\n", (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client);
532    
533                                          if (SYS_child_process_count - 1 < BBS_max_client)                                          if (SYS_child_process_count - 1 < BBS_max_client)
534                                          {                                          {


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

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