/[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.66 by sysadm, Wed Jun 25 01:50:14 2025 UTC Revision 1.75 by sysadm, Sat Oct 25 07:58:10 2025 UTC
# Line 25  Line 25 
25  #include "login.h"  #include "login.h"
26  #include "menu.h"  #include "menu.h"
27  #include "net_server.h"  #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 <pty.h>
33  #include <signal.h>  #include <signal.h>
34  #include <stdlib.h>  #include <stdlib.h>
35  #include <string.h>  #include <string.h>
36  #include <unistd.h>  #include <unistd.h>
37    #include <utmp.h>
38  #include <arpa/inet.h>  #include <arpa/inet.h>
39  #include <libssh/callbacks.h>  #include <libssh/callbacks.h>
40  #include <libssh/libssh.h>  #include <libssh/libssh.h>
# Line 44  Line 47 
47  #include <sys/wait.h>  #include <sys/wait.h>
48  #include <systemd/sd-daemon.h>  #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  {  {
55          pid_t pid;          pid_t pid;
# Line 55  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 81  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  static ssh_channel new_session_channel(ssh_session session, void *userdata)          /* 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 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 121  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 169  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_timeout = 60; // second
337                  ssh_set_server_callbacks(SSH_session, &cb);                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
338                    {
339                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
340                            goto cleanup;
341                    }
342    
343                    ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);
344    
345                    ssh_callbacks_init(&server_cb);
346                    ssh_callbacks_init(&channel_cb);
347    
348                    ssh_set_server_callbacks(SSH_session, &server_cb);
349    
350                  if (ssh_handle_key_exchange(SSH_session))                  if (ssh_handle_key_exchange(SSH_session))
351                  {                  {
352                          log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));                          log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));
353                          goto cleanup;                          goto cleanup;
354                  }                  }
                 ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);  
355    
356                  event = ssh_event_new();                  event = ssh_event_new();
357                  ssh_event_add_session(event, SSH_session);                  ssh_event_add_session(event, SSH_session);
# Line 187  static int fork_server(void) Line 361  static int fork_server(void)
361                          ret = ssh_event_dopoll(event, 100); // 0.1 second                          ret = ssh_event_dopoll(event, 100); // 0.1 second
362                          if (ret == SSH_ERROR)                          if (ret == SSH_ERROR)
363                          {                          {
364    #ifdef _DEBUG
365                                  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));
366    #endif
367                                  goto cleanup;                                  goto cleanup;
368                          }                          }
369                  }                  }
# Line 197  static int fork_server(void) Line 373  static int fork_server(void)
373                          log_error("SSH auth error, tried %d times\n", cb_data.tries);                          log_error("SSH auth error, tried %d times\n", cb_data.tries);
374                          goto cleanup;                          goto cleanup;
375                  }                  }
376    
377                    ssh_set_channel_callbacks(SSH_channel, &channel_cb);
378    
379                    do
380                    {
381                            ret = ssh_event_dopoll(event, 100); // 0.1 second
382                            if (ret == SSH_ERROR)
383                            {
384                                    ssh_channel_close(SSH_channel);
385                            }
386    
387                            if (ret == SSH_AGAIN) // loop until SSH connection is fully established
388                            {
389                                    /* Executed only once, once the child process starts. */
390                                    cdata.event = event;
391                                    break;
392                            }
393                    } while (ssh_channel_is_open(SSH_channel));
394    
395                    ssh_timeout = 0;
396                    if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
397                    {
398                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
399                            goto cleanup;
400                    }
401          }          }
402    
403          // Redirect Input          // Redirect Input
# Line 225  cleanup: Line 426  cleanup:
426    
427          if (SSH_v2)          if (SSH_v2)
428          {          {
429                    close(cdata.pty_master);
430                    close(cdata.child_stdin);
431                    close(cdata.child_stdout);
432                    close(cdata.child_stderr);
433    
434                  ssh_channel_free(SSH_channel);                  ssh_channel_free(SSH_channel);
435                  ssh_disconnect(SSH_session);                  ssh_disconnect(SSH_session);
436          }          }
# Line 257  int net_server(const char *hostaddr, in_ Line 463  int net_server(const char *hostaddr, in_
463          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
464          int nfds, epollfd;          int nfds, epollfd;
465          siginfo_t siginfo;          siginfo_t siginfo;
466            int notify_child_exit = 0;
467            time_t tm_notify_child_exit = time(NULL);
468          int sd_notify_stopping = 0;          int sd_notify_stopping = 0;
469          MENU_SET bbs_menu_new;          MENU_SET bbs_menu_new;
470            MENU_SET top10_menu_new;
471          int i, j;          int i, j;
472          pid_t pid;          pid_t pid;
473          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
# Line 399  int net_server(const char *hostaddr, in_ Line 608  int net_server(const char *hostaddr, in_
608    
609                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
610                  {                  {
611                          log_common("Notify %d child process to exit\n", SYS_child_process_count);                          if (notify_child_exit == 0)
                         if (kill(0, SIGTERM) < 0)  
612                          {                          {
613                                  log_error("Send SIGTERM signal failed (%d)\n", errno);                                  sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
614                                    log_common("Notify %d child process to exit\n", SYS_child_process_count);
615    
616                                    if (kill(0, SIGTERM) < 0)
617                                    {
618                                            log_error("Send SIGTERM signal failed (%d)\n", errno);
619                                    }
620    
621                                    notify_child_exit = 1;
622                                    tm_notify_child_exit = time(NULL);
623                          }                          }
624                            else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)
625                            {
626                                    sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
627    
628                          sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count);                                  for (i = 0; i < BBS_max_client; i++)
629                                    {
630                                            if (process_sockaddr_pool[i].pid != 0)
631                                            {
632                                                    log_error("Kill child process (pid=%d)\n", process_sockaddr_pool[i].pid);
633                                                    if (kill(process_sockaddr_pool[i].pid, SIGKILL) < 0)
634                                                    {
635                                                            log_error("Send SIGKILL signal failed (%d)\n", errno);
636                                                    }
637                                            }
638                                    }
639    
640                                    notify_child_exit = 2;
641                                    tm_notify_child_exit = time(NULL);
642                            }
643                            else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT)
644                            {
645                                    log_error("Main process prepare to exit without waiting for %d child process any longer\n", SYS_child_process_count);
646                                    SYS_child_process_count = 0;
647                            }
648                  }                  }
649    
650                  if (SYS_conf_reload && !SYS_server_exit)                  if (SYS_conf_reload && !SYS_server_exit)
# Line 422  int net_server(const char *hostaddr, in_ Line 661  int net_server(const char *hostaddr, in_
661                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
662                          {                          {
663                                  unload_menu(&bbs_menu_new);                                  unload_menu(&bbs_menu_new);
664                                  log_error("Reload menu failed\n");                                  log_error("Reload bbs menu failed\n");
665                          }                          }
666                          else                          else
667                          {                          {
668                                    unload_menu(&bbs_menu);
669                                  memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));                                  memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
670                                  log_common("Reload menu successfully\n");                                  log_common("Reload bbs menu successfully\n");
671                          }                          }
672    
673                          sd_notify(0, "READY=1");                          if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)
674                  }                          {
675                                    unload_menu(&top10_menu_new);
676                  if (SYS_data_file_reload && !SYS_server_exit)                                  log_error("Reload top10 menu failed\n");
677                  {                          }
678                          SYS_data_file_reload = 0;                          else
679                          sd_notify(0, "RELOADING=1");                          {
680                                    unload_menu(&top10_menu);
681                                    top10_menu_new.allow_exit = 1;
682                                    memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new));
683                                    log_common("Reload top10 menu successfully\n");
684                            }
685    
686                          for (int i = 0; i < data_files_load_startup_count; i++)                          for (int i = 0; i < data_files_load_startup_count; i++)
687                          {                          {
# Line 445  int net_server(const char *hostaddr, in_ Line 690  int net_server(const char *hostaddr, in_
690                                          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]);
691                                  }                                  }
692                          }                          }
   
693                          log_common("Reload data files successfully\n");                          log_common("Reload data files successfully\n");
                         sd_notify(0, "READY=1");  
                 }  
694    
695                  if (SYS_section_list_reload && !SYS_server_exit)                          // Load section config and gen_ex
696                  {                          if (load_section_config_from_db(1) < 0)
                         SYS_section_list_reload = 0;  
   
                         if (section_list_loader_reload() < 0)  
697                          {                          {
698                                  log_error("section_list_loader_reload() failed\n");                                  log_error("load_section_config_from_db(1) error\n");
699                          }                          }
700                            else
701                            {
702                                    log_common("Reload section config and gen_ex successfully\n");
703                            }
704    
705                            sd_notify(0, "READY=1");
706                  }                  }
707    
708                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second


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

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