/[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.58 by sysadm, Sat Jun 7 11:28:29 2025 UTC Revision 1.74 by sysadm, Sat Oct 25 03:11:11 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>
32  #include <string.h>  #include <pty.h>
33  #include <signal.h>  #include <signal.h>
34  #include <stdlib.h>  #include <stdlib.h>
35    #include <string.h>
36  #include <unistd.h>  #include <unistd.h>
37  #include <sys/syscall.h>  #include <utmp.h>
 #include <sys/socket.h>  
 #include <sys/wait.h>  
 #include <sys/epoll.h>  
38  #include <arpa/inet.h>  #include <arpa/inet.h>
39  #include <netinet/in.h>  #include <libssh/callbacks.h>
 #include <systemd/sd-daemon.h>  
40  #include <libssh/libssh.h>  #include <libssh/libssh.h>
41  #include <libssh/server.h>  #include <libssh/server.h>
42  #include <libssh/callbacks.h>  #include <netinet/in.h>
43    #include <sys/epoll.h>
44    #include <sys/socket.h>
45    #include <sys/syscall.h>
46    #include <sys/types.h>
47    #include <sys/wait.h>
48    #include <systemd/sd-daemon.h>
49    
50    #define WAIT_CHILD_PROCESS_EXIT_TIMEOUT 5 // second
51    #define WAIT_CHILD_PROCESS_KILL_TIMEOUT 1 // second
52    
53  struct process_sockaddr_t  struct process_sockaddr_t
54  {  {
# Line 58  static PROCESS_SOCKADDR process_sockaddr Line 61  static PROCESS_SOCKADDR process_sockaddr
61    
62  #define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds  #define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds
63    
64  struct ssl_server_cb_data_t  #define SFTP_SERVER_PATH "/usr/lib/sftp-server"
65    
66    /* A userdata struct for session. */
67    struct session_data_struct
68  {  {
69          int tries;          int tries;
70          int error;          int error;
71  };  };
72    
73    /* A userdata struct for channel. */
74    struct channel_data_struct
75    {
76            /* pid of the child process the channel will spawn. */
77            pid_t pid;
78            /* For PTY allocation */
79            socket_t pty_master;
80            socket_t pty_slave;
81            /* For communication with the child process. */
82            socket_t child_stdin;
83            socket_t child_stdout;
84            /* Only used for subsystem and exec requests. */
85            socket_t child_stderr;
86            /* Event which is used to poll the above descriptors. */
87            ssh_event event;
88            /* Terminal size struct. */
89            struct winsize *winsize;
90    };
91    
92  static int auth_password(ssh_session session, const char *user,  static int auth_password(ssh_session session, const char *user,
93                                                   const char *password, void *userdata)                                                   const char *password, void *userdata)
94  {  {
95          struct ssl_server_cb_data_t *p_data = userdata;          struct session_data_struct *sdata = (struct session_data_struct *)userdata;
96          int ret;          int ret;
97    
98          if (strcmp(user, "guest") == 0)          if (strcmp(user, "guest") == 0)
# Line 84  static int auth_password(ssh_session ses Line 109  static int auth_password(ssh_session ses
109                  return SSH_AUTH_SUCCESS;                  return SSH_AUTH_SUCCESS;
110          }          }
111    
112          if ((++(p_data->tries)) >= BBS_login_retry_times)          if ((++(sdata->tries)) >= BBS_login_retry_times)
113          {          {
114                  p_data->error = 1;                  sdata->error = 1;
115          }          }
116    
117          return SSH_AUTH_DENIED;          return SSH_AUTH_DENIED;
118  }  }
119    
120  static int pty_request(ssh_session session, ssh_channel channel, const char *term,  static int pty_request(ssh_session session, ssh_channel channel, const char *term,
121                                             int x, int y, int px, int py, void *userdata)                                             int cols, int rows, int px, int py, void *userdata)
122  {  {
123          return 0;          struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
124            int rc;
125    
126            (void)session;
127            (void)channel;
128            (void)term;
129    
130            cdata->winsize->ws_row = (unsigned short int)rows;
131            cdata->winsize->ws_col = (unsigned short int)cols;
132            cdata->winsize->ws_xpixel = (unsigned short int)px;
133            cdata->winsize->ws_ypixel = (unsigned short int)py;
134    
135            rc = openpty(&cdata->pty_master, &cdata->pty_slave, NULL, NULL, cdata->winsize);
136            if (rc != 0)
137            {
138                    log_error("Failed to open pty\n");
139                    return SSH_ERROR;
140            }
141    
142            return SSH_OK;
143    }
144    
145    static int pty_resize(ssh_session session, ssh_channel channel, int cols, int rows,
146                                              int py, int px, void *userdata)
147    {
148            struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
149    
150            (void)session;
151            (void)channel;
152    
153            cdata->winsize->ws_row = (unsigned short int)rows;
154            cdata->winsize->ws_col = (unsigned short int)cols;
155            cdata->winsize->ws_xpixel = (unsigned short int)px;
156            cdata->winsize->ws_ypixel = (unsigned short int)py;
157    
158            if (cdata->pty_master != -1)
159            {
160                    return ioctl(cdata->pty_master, TIOCSWINSZ, cdata->winsize);
161            }
162    
163            return SSH_ERROR;
164    }
165    
166    static int exec_pty(const char *mode, const char *command, struct channel_data_struct *cdata)
167    {
168            (void)cdata;
169    
170            if (command != NULL)
171            {
172                    log_error("Forbid exec /bin/sh %s %s)\n", mode, command);
173            }
174    
175            return SSH_OK;
176    }
177    
178    static int exec_nopty(const char *command, struct channel_data_struct *cdata)
179    {
180            (void)cdata;
181    
182            if (command != NULL)
183            {
184                    log_error("Forbid exec /bin/sh -c %s)\n", command);
185            }
186    
187            return SSH_OK;
188    }
189    
190    static int exec_request(ssh_session session, ssh_channel channel, const char *command, void *userdata)
191    {
192            struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
193    
194            (void)session;
195            (void)channel;
196    
197            if (cdata->pid > 0)
198            {
199                    return SSH_ERROR;
200            }
201    
202            if (cdata->pty_master != -1 && cdata->pty_slave != -1)
203            {
204                    return exec_pty("-c", command, cdata);
205            }
206            return exec_nopty(command, cdata);
207  }  }
208    
209  static int shell_request(ssh_session session, ssh_channel channel, void *userdata)  static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
210  {  {
211          return 0;          struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
212    
213            (void)session;
214            (void)channel;
215    
216            if (cdata->pid > 0)
217            {
218                    return SSH_ERROR;
219            }
220    
221            if (cdata->pty_master != -1 && cdata->pty_slave != -1)
222            {
223                    return exec_pty("-l", NULL, cdata);
224            }
225            /* Client requested a shell without a pty, let's pretend we allow that */
226            return SSH_OK;
227  }  }
228    
229  static struct ssh_channel_callbacks_struct channel_cb = {  static int subsystem_request(ssh_session session, ssh_channel channel, const char *subsystem, void *userdata)
230          .channel_pty_request_function = pty_request,  {
231          .channel_shell_request_function = shell_request};          (void)session;
232            (void)channel;
233    
234            log_error("subsystem_request(subsystem=%s)\n", subsystem);
235    
236            /* subsystem requests behave similarly to exec requests. */
237            if (strcmp(subsystem, "sftp") == 0)
238            {
239                    return exec_request(session, channel, SFTP_SERVER_PATH, userdata);
240            }
241            return SSH_ERROR;
242    }
243    
244  static ssh_channel new_session_channel(ssh_session session, void *userdata)  static ssh_channel channel_open(ssh_session session, void *userdata)
245  {  {
246            (void)userdata;
247    
248          if (SSH_channel != NULL)          if (SSH_channel != NULL)
249          {          {
250                  return NULL;                  return NULL;
251          }          }
252    
253          SSH_channel = ssh_channel_new(session);          SSH_channel = ssh_channel_new(session);
         ssh_callbacks_init(&channel_cb);  
         ssh_set_channel_callbacks(SSH_channel, &channel_cb);  
254    
255          return SSH_channel;          return SSH_channel;
256  }  }
# Line 124  static ssh_channel new_session_channel(s Line 258  static ssh_channel new_session_channel(s
258  static int fork_server(void)  static int fork_server(void)
259  {  {
260          ssh_event event;          ssh_event event;
261            long int ssh_timeout = 0;
262          int pid;          int pid;
263          int i;          int i;
264          int ret;          int ret;
265    
266          struct ssl_server_cb_data_t cb_data = {          /* Structure for storing the pty size. */
267            struct winsize wsize = {
268                    .ws_row = 0,
269                    .ws_col = 0,
270                    .ws_xpixel = 0,
271                    .ws_ypixel = 0};
272    
273            /* Our struct holding information about the channel. */
274            struct channel_data_struct cdata = {
275                    .pid = 0,
276                    .pty_master = -1,
277                    .pty_slave = -1,
278                    .child_stdin = -1,
279                    .child_stdout = -1,
280                    .child_stderr = -1,
281                    .event = NULL,
282                    .winsize = &wsize};
283    
284            struct session_data_struct cb_data = {
285                  .tries = 0,                  .tries = 0,
286                  .error = 0,                  .error = 0,
287          };          };
288    
289          struct ssh_server_callbacks_struct cb = {          struct ssh_channel_callbacks_struct channel_cb = {
290                    .userdata = &cdata,
291                    .channel_pty_request_function = pty_request,
292                    .channel_pty_window_change_function = pty_resize,
293                    .channel_shell_request_function = shell_request,
294                    .channel_exec_request_function = exec_request,
295                    .channel_subsystem_request_function = subsystem_request};
296    
297            struct ssh_server_callbacks_struct server_cb = {
298                  .userdata = &cb_data,                  .userdata = &cb_data,
299                  .auth_password_function = auth_password,                  .auth_password_function = auth_password,
300                  .channel_open_request_session_function = new_session_channel,                  .channel_open_request_session_function = channel_open,
301          };          };
302    
303          pid = fork();          pid = fork();
# Line 172  static int fork_server(void) Line 333  static int fork_server(void)
333    
334                  ssh_bind_free(sshbind);                  ssh_bind_free(sshbind);
335    
336                  ssh_callbacks_init(&cb);                  ssh_callbacks_init(&channel_cb);
337                  ssh_set_server_callbacks(SSH_session, &cb);                  ssh_callbacks_init(&server_cb);
338    
339                    ssh_set_server_callbacks(SSH_session, &server_cb);
340    
341                    ssh_timeout = 60; // second
342                    if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
343                    {
344                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
345                            goto cleanup;
346                    }
347    
348                  if (ssh_handle_key_exchange(SSH_session))                  if (ssh_handle_key_exchange(SSH_session))
349                  {                  {
# Line 190  static int fork_server(void) Line 360  static int fork_server(void)
360                          ret = ssh_event_dopoll(event, 100); // 0.1 second                          ret = ssh_event_dopoll(event, 100); // 0.1 second
361                          if (ret == SSH_ERROR)                          if (ret == SSH_ERROR)
362                          {                          {
363    #ifdef _DEBUG
364                                  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));
365    #endif
366                                  goto cleanup;                                  goto cleanup;
367                          }                          }
368                  }                  }
# Line 200  static int fork_server(void) Line 372  static int fork_server(void)
372                          log_error("SSH auth error, tried %d times\n", cb_data.tries);                          log_error("SSH auth error, tried %d times\n", cb_data.tries);
373                          goto cleanup;                          goto cleanup;
374                  }                  }
375    
376                    ssh_set_channel_callbacks(SSH_channel, &channel_cb);
377    
378                    ssh_timeout = 0;
379                    if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
380                    {
381                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
382                            goto cleanup;
383                    }
384          }          }
385    
386          // Redirect Input          // Redirect Input
# Line 228  cleanup: Line 409  cleanup:
409    
410          if (SSH_v2)          if (SSH_v2)
411          {          {
412                    close(cdata.pty_master);
413                    close(cdata.child_stdin);
414                    close(cdata.child_stdout);
415                    close(cdata.child_stderr);
416    
417                  ssh_channel_free(SSH_channel);                  ssh_channel_free(SSH_channel);
418                  ssh_disconnect(SSH_session);                  ssh_disconnect(SSH_session);
419          }          }
# Line 260  int net_server(const char *hostaddr, in_ Line 446  int net_server(const char *hostaddr, in_
446          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
447          int nfds, epollfd;          int nfds, epollfd;
448          siginfo_t siginfo;          siginfo_t siginfo;
449            int notify_child_exit = 0;
450            time_t tm_notify_child_exit = time(NULL);
451          int sd_notify_stopping = 0;          int sd_notify_stopping = 0;
452          MENU_SET *p_bbs_menu_new;          MENU_SET bbs_menu_new;
453            MENU_SET top10_menu_new;
454          int i, j;          int i, j;
455          pid_t pid;          pid_t pid;
456          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
# Line 273  int net_server(const char *hostaddr, in_ Line 462  int net_server(const char *hostaddr, in_
462          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
463                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
464                  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 ||
465                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 ||
466                  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)
467          {          {
468                  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 401  int net_server(const char *hostaddr, in_ Line 591  int net_server(const char *hostaddr, in_
591    
592                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
593                  {                  {
594                          log_common("Notify %d child process to exit\n", SYS_child_process_count);                          if (notify_child_exit == 0)
                         if (kill(0, SIGTERM) < 0)  
595                          {                          {
596                                  log_error("Send SIGTERM signal failed (%d)\n", errno);                                  sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
597                                    log_common("Notify %d child process to exit\n", SYS_child_process_count);
598    
599                                    if (kill(0, SIGTERM) < 0)
600                                    {
601                                            log_error("Send SIGTERM signal failed (%d)\n", errno);
602                                    }
603    
604                                    notify_child_exit = 1;
605                                    tm_notify_child_exit = time(NULL);
606                          }                          }
607                            else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)
608                            {
609                                    sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
610    
611                                    for (i = 0; i < BBS_max_client; i++)
612                                    {
613                                            if (process_sockaddr_pool[i].pid != 0)
614                                            {
615                                                    log_error("Kill child process (pid=%d)\n", process_sockaddr_pool[i].pid);
616                                                    if (kill(process_sockaddr_pool[i].pid, SIGKILL) < 0)
617                                                    {
618                                                            log_error("Send SIGKILL signal failed (%d)\n", errno);
619                                                    }
620                                            }
621                                    }
622    
623                          sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count);                                  notify_child_exit = 2;
624                                    tm_notify_child_exit = time(NULL);
625                            }
626                            else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT)
627                            {
628                                    log_error("Main process prepare to exit without waiting for %d child process any longer\n", SYS_child_process_count);
629                                    SYS_child_process_count = 0;
630                            }
631                  }                  }
632    
633                  if (SYS_conf_reload && !SYS_server_exit)                  if (SYS_conf_reload && !SYS_server_exit)
# Line 421  int net_server(const char *hostaddr, in_ Line 641  int net_server(const char *hostaddr, in_
641                                  log_error("Reload conf failed\n");                                  log_error("Reload conf failed\n");
642                          }                          }
643    
644                          p_bbs_menu_new = calloc(1, sizeof(MENU_SET));                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
                         if (p_bbs_menu_new == NULL)  
645                          {                          {
646                                  log_error("OOM: calloc(MENU_SET)\n");                                  unload_menu(&bbs_menu_new);
647                                    log_error("Reload bbs menu failed\n");
648                          }                          }
649                          else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0)                          else
650                          {                          {
651                                  unload_menu(p_bbs_menu_new);                                  unload_menu(&bbs_menu);
652                                  free(p_bbs_menu_new);                                  memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
653                                  p_bbs_menu_new = NULL;                                  log_common("Reload bbs menu successfully\n");
654                            }
655    
656                                  log_error("Reload menu failed\n");                          if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)
657                            {
658                                    unload_menu(&top10_menu_new);
659                                    log_error("Reload top10 menu failed\n");
660                          }                          }
661                          else                          else
662                          {                          {
663                                  unload_menu(p_bbs_menu);                                  unload_menu(&top10_menu);
664                                  free(p_bbs_menu);                                  top10_menu_new.allow_exit = 1;
665                                    memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new));
666                                  p_bbs_menu = p_bbs_menu_new;                                  log_common("Reload top10 menu successfully\n");
                                 p_bbs_menu_new = NULL;  
   
                                 log_common("Reload menu successfully\n");  
667                          }                          }
668    
                         sd_notify(0, "READY=1");  
                 }  
   
                 if (SYS_data_file_reload && !SYS_server_exit)  
                 {  
                         SYS_data_file_reload = 0;  
                         sd_notify(0, "RELOADING=1");  
   
669                          for (int i = 0; i < data_files_load_startup_count; i++)                          for (int i = 0; i < data_files_load_startup_count; i++)
670                          {                          {
671                                  if (load_file(data_files_load_startup[i]) < 0)                                  if (load_file(data_files_load_startup[i]) < 0)
# Line 460  int net_server(const char *hostaddr, in_ Line 673  int net_server(const char *hostaddr, in_
673                                          log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);                                          log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);
674                                  }                                  }
675                          }                          }
   
676                          log_common("Reload data files successfully\n");                          log_common("Reload data files successfully\n");
                         sd_notify(0, "READY=1");  
                 }  
   
                 if (SYS_section_list_reload && !SYS_server_exit)  
                 {  
                         SYS_section_list_reload = 0;  
677    
678                          if (section_list_loader_reload() < 0)                          // Load section config and gen_ex
679                            if (load_section_config_from_db(1) < 0)
680                            {
681                                    log_error("load_section_config_from_db(1) error\n");
682                            }
683                            else
684                          {                          {
685                                  log_error("ksection_list_loader_reload() failed\n");                                  log_common("Reload section config and gen_ex successfully\n");
686                          }                          }
687    
688                            sd_notify(0, "READY=1");
689                  }                  }
690    
691                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
# Line 525  int net_server(const char *hostaddr, in_ Line 738  int net_server(const char *hostaddr, in_
738    
739                                          port_client = ntohs(sin.sin_port);                                          port_client = ntohs(sin.sin_port);
740    
741                                          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);
742    
743                                          if (SYS_child_process_count - 1 < BBS_max_client)                                          if (SYS_child_process_count - 1 < BBS_max_client)
744                                          {                                          {


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

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