/[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.69 by sysadm, Sun Jun 29 02:28:02 2025 UTC Revision 1.78 by sysadm, Tue Nov 4 14:58:56 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    net_server.c  -  description  /*
3                                                           -------------------   * net_server
4          Copyright            : (C) 2004-2025 by Leaflet   *   - network server with SSH support
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "bbs.h"  #include "bbs.h"
10  #include "bbs_main.h"  #include "bbs_main.h"
# Line 29  Line 21 
21  #include "section_list_loader.h"  #include "section_list_loader.h"
22  #include <errno.h>  #include <errno.h>
23  #include <fcntl.h>  #include <fcntl.h>
24    #include <pty.h>
25  #include <signal.h>  #include <signal.h>
26  #include <stdlib.h>  #include <stdlib.h>
27  #include <string.h>  #include <string.h>
28  #include <unistd.h>  #include <unistd.h>
29    #include <utmp.h>
30  #include <arpa/inet.h>  #include <arpa/inet.h>
31  #include <libssh/callbacks.h>  #include <libssh/callbacks.h>
32  #include <libssh/libssh.h>  #include <libssh/libssh.h>
# Line 45  Line 39 
39  #include <sys/wait.h>  #include <sys/wait.h>
40  #include <systemd/sd-daemon.h>  #include <systemd/sd-daemon.h>
41    
42    #define WAIT_CHILD_PROCESS_EXIT_TIMEOUT 5 // second
43    #define WAIT_CHILD_PROCESS_KILL_TIMEOUT 1 // second
44    
45  struct process_sockaddr_t  struct process_sockaddr_t
46  {  {
47          pid_t pid;          pid_t pid;
# Line 56  static PROCESS_SOCKADDR process_sockaddr Line 53  static PROCESS_SOCKADDR process_sockaddr
53    
54  #define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds  #define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds
55    
56  struct ssl_server_cb_data_t  #define SFTP_SERVER_PATH "/usr/lib/sftp-server"
57    
58    /* A userdata struct for session. */
59    struct session_data_struct
60  {  {
61          int tries;          int tries;
62          int error;          int error;
63  };  };
64    
65    /* A userdata struct for channel. */
66    struct channel_data_struct
67    {
68            /* pid of the child process the channel will spawn. */
69            pid_t pid;
70            /* For PTY allocation */
71            socket_t pty_master;
72            socket_t pty_slave;
73            /* For communication with the child process. */
74            socket_t child_stdin;
75            socket_t child_stdout;
76            /* Only used for subsystem and exec requests. */
77            socket_t child_stderr;
78            /* Event which is used to poll the above descriptors. */
79            ssh_event event;
80            /* Terminal size struct. */
81            struct winsize *winsize;
82    };
83    
84  static int auth_password(ssh_session session, const char *user,  static int auth_password(ssh_session session, const char *user,
85                                                   const char *password, void *userdata)                                                   const char *password, void *userdata)
86  {  {
87          struct ssl_server_cb_data_t *p_data = userdata;          struct session_data_struct *sdata = (struct session_data_struct *)userdata;
88          int ret;          int ret;
89    
90          if (strcmp(user, "guest") == 0)          if (strcmp(user, "guest") == 0)
# Line 82  static int auth_password(ssh_session ses Line 101  static int auth_password(ssh_session ses
101                  return SSH_AUTH_SUCCESS;                  return SSH_AUTH_SUCCESS;
102          }          }
103    
104          if ((++(p_data->tries)) >= BBS_login_retry_times)          if ((++(sdata->tries)) >= BBS_login_retry_times)
105          {          {
106                  p_data->error = 1;                  sdata->error = 1;
107          }          }
108    
109          return SSH_AUTH_DENIED;          return SSH_AUTH_DENIED;
110  }  }
111    
112  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,
113                                             int x, int y, int px, int py, void *userdata)                                             int cols, int rows, int px, int py, void *userdata)
114  {  {
115          return 0;          struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
116            int rc;
117    
118            (void)session;
119            (void)channel;
120            (void)term;
121    
122            cdata->winsize->ws_row = (unsigned short int)rows;
123            cdata->winsize->ws_col = (unsigned short int)cols;
124            cdata->winsize->ws_xpixel = (unsigned short int)px;
125            cdata->winsize->ws_ypixel = (unsigned short int)py;
126    
127            rc = openpty(&cdata->pty_master, &cdata->pty_slave, NULL, NULL, cdata->winsize);
128            if (rc != 0)
129            {
130                    log_error("Failed to open pty\n");
131                    return SSH_ERROR;
132            }
133    
134            return SSH_OK;
135    }
136    
137    static int pty_resize(ssh_session session, ssh_channel channel, int cols, int rows,
138                                              int py, int px, void *userdata)
139    {
140            struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
141    
142            (void)session;
143            (void)channel;
144    
145            cdata->winsize->ws_row = (unsigned short int)rows;
146            cdata->winsize->ws_col = (unsigned short int)cols;
147            cdata->winsize->ws_xpixel = (unsigned short int)px;
148            cdata->winsize->ws_ypixel = (unsigned short int)py;
149    
150            if (cdata->pty_master != -1)
151            {
152                    return ioctl(cdata->pty_master, TIOCSWINSZ, cdata->winsize);
153            }
154    
155            return SSH_ERROR;
156    }
157    
158    static int exec_pty(const char *mode, const char *command, struct channel_data_struct *cdata)
159    {
160            (void)cdata;
161    
162            if (command != NULL)
163            {
164                    log_error("Forbid exec /bin/sh %s %s)\n", mode, command);
165            }
166    
167            return SSH_OK;
168    }
169    
170    static int exec_nopty(const char *command, struct channel_data_struct *cdata)
171    {
172            (void)cdata;
173    
174            if (command != NULL)
175            {
176                    log_error("Forbid exec /bin/sh -c %s)\n", command);
177            }
178    
179            return SSH_OK;
180    }
181    
182    static int exec_request(ssh_session session, ssh_channel channel, const char *command, void *userdata)
183    {
184            struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
185    
186            (void)session;
187            (void)channel;
188    
189            if (cdata->pid > 0)
190            {
191                    return SSH_ERROR;
192            }
193    
194            if (cdata->pty_master != -1 && cdata->pty_slave != -1)
195            {
196                    return exec_pty("-c", command, cdata);
197            }
198            return exec_nopty(command, cdata);
199  }  }
200    
201  static int shell_request(ssh_session session, ssh_channel channel, void *userdata)  static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
202  {  {
203          return 0;          struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
204    
205            (void)session;
206            (void)channel;
207    
208            if (cdata->pid > 0)
209            {
210                    return SSH_ERROR;
211            }
212    
213            if (cdata->pty_master != -1 && cdata->pty_slave != -1)
214            {
215                    return exec_pty("-l", NULL, cdata);
216            }
217            /* Client requested a shell without a pty, let's pretend we allow that */
218            return SSH_OK;
219  }  }
220    
221  static struct ssh_channel_callbacks_struct channel_cb = {  static int subsystem_request(ssh_session session, ssh_channel channel, const char *subsystem, void *userdata)
222          .channel_pty_request_function = pty_request,  {
223          .channel_shell_request_function = shell_request};          (void)session;
224            (void)channel;
225    
226            log_error("subsystem_request(subsystem=%s)\n", subsystem);
227    
228            /* subsystem requests behave similarly to exec requests. */
229            if (strcmp(subsystem, "sftp") == 0)
230            {
231                    return exec_request(session, channel, SFTP_SERVER_PATH, userdata);
232            }
233            return SSH_ERROR;
234    }
235    
236  static ssh_channel new_session_channel(ssh_session session, void *userdata)  static ssh_channel channel_open(ssh_session session, void *userdata)
237  {  {
238            (void)userdata;
239    
240          if (SSH_channel != NULL)          if (SSH_channel != NULL)
241          {          {
242                  return NULL;                  return NULL;
243          }          }
244    
245          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);  
246    
247          return SSH_channel;          return SSH_channel;
248  }  }
# Line 122  static ssh_channel new_session_channel(s Line 250  static ssh_channel new_session_channel(s
250  static int fork_server(void)  static int fork_server(void)
251  {  {
252          ssh_event event;          ssh_event event;
253            long int ssh_timeout = 0;
254          int pid;          int pid;
255          int i;          int i;
256          int ret;          int ret;
257    
258          struct ssl_server_cb_data_t cb_data = {          /* Structure for storing the pty size. */
259            struct winsize wsize = {
260                    .ws_row = 0,
261                    .ws_col = 0,
262                    .ws_xpixel = 0,
263                    .ws_ypixel = 0};
264    
265            /* Our struct holding information about the channel. */
266            struct channel_data_struct cdata = {
267                    .pid = 0,
268                    .pty_master = -1,
269                    .pty_slave = -1,
270                    .child_stdin = -1,
271                    .child_stdout = -1,
272                    .child_stderr = -1,
273                    .event = NULL,
274                    .winsize = &wsize};
275    
276            struct session_data_struct cb_data = {
277                  .tries = 0,                  .tries = 0,
278                  .error = 0,                  .error = 0,
279          };          };
280    
281          struct ssh_server_callbacks_struct cb = {          struct ssh_channel_callbacks_struct channel_cb = {
282                    .userdata = &cdata,
283                    .channel_pty_request_function = pty_request,
284                    .channel_pty_window_change_function = pty_resize,
285                    .channel_shell_request_function = shell_request,
286                    .channel_exec_request_function = exec_request,
287                    .channel_subsystem_request_function = subsystem_request};
288    
289            struct ssh_server_callbacks_struct server_cb = {
290                  .userdata = &cb_data,                  .userdata = &cb_data,
291                  .auth_password_function = auth_password,                  .auth_password_function = auth_password,
292                  .channel_open_request_session_function = new_session_channel,                  .channel_open_request_session_function = channel_open,
293          };          };
294    
295          pid = fork();          pid = fork();
# Line 170  static int fork_server(void) Line 325  static int fork_server(void)
325    
326                  ssh_bind_free(sshbind);                  ssh_bind_free(sshbind);
327    
328                  ssh_callbacks_init(&cb);                  ssh_timeout = 60; // second
329                  ssh_set_server_callbacks(SSH_session, &cb);                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
330                    {
331                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
332                            goto cleanup;
333                    }
334    
335                    ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);
336    
337                    ssh_callbacks_init(&server_cb);
338                    ssh_callbacks_init(&channel_cb);
339    
340                    ssh_set_server_callbacks(SSH_session, &server_cb);
341    
342                  if (ssh_handle_key_exchange(SSH_session))                  if (ssh_handle_key_exchange(SSH_session))
343                  {                  {
344                          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));
345                          goto cleanup;                          goto cleanup;
346                  }                  }
                 ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);  
347    
348                  event = ssh_event_new();                  event = ssh_event_new();
349                  ssh_event_add_session(event, SSH_session);                  ssh_event_add_session(event, SSH_session);
# Line 188  static int fork_server(void) Line 353  static int fork_server(void)
353                          ret = ssh_event_dopoll(event, 100); // 0.1 second                          ret = ssh_event_dopoll(event, 100); // 0.1 second
354                          if (ret == SSH_ERROR)                          if (ret == SSH_ERROR)
355                          {                          {
356    #ifdef _DEBUG
357                                  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));
358    #endif
359                                  goto cleanup;                                  goto cleanup;
360                          }                          }
361                  }                  }
# Line 198  static int fork_server(void) Line 365  static int fork_server(void)
365                          log_error("SSH auth error, tried %d times\n", cb_data.tries);                          log_error("SSH auth error, tried %d times\n", cb_data.tries);
366                          goto cleanup;                          goto cleanup;
367                  }                  }
368    
369                    ssh_set_channel_callbacks(SSH_channel, &channel_cb);
370    
371                    do
372                    {
373                            ret = ssh_event_dopoll(event, 100); // 0.1 second
374                            if (ret == SSH_ERROR)
375                            {
376                                    ssh_channel_close(SSH_channel);
377                            }
378    
379                            if (ret == SSH_AGAIN) // loop until SSH connection is fully established
380                            {
381                                    /* Executed only once, once the child process starts. */
382                                    cdata.event = event;
383                                    break;
384                            }
385                    } while (ssh_channel_is_open(SSH_channel));
386    
387                    ssh_timeout = 0;
388                    if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
389                    {
390                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
391                            goto cleanup;
392                    }
393          }          }
394    
395          // Redirect Input          // Redirect Input
# Line 226  cleanup: Line 418  cleanup:
418    
419          if (SSH_v2)          if (SSH_v2)
420          {          {
421                    close(cdata.pty_master);
422                    close(cdata.child_stdin);
423                    close(cdata.child_stdout);
424                    close(cdata.child_stderr);
425    
426                  ssh_channel_free(SSH_channel);                  ssh_channel_free(SSH_channel);
427                  ssh_disconnect(SSH_session);                  ssh_disconnect(SSH_session);
428          }          }
# Line 258  int net_server(const char *hostaddr, in_ Line 455  int net_server(const char *hostaddr, in_
455          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
456          int nfds, epollfd;          int nfds, epollfd;
457          siginfo_t siginfo;          siginfo_t siginfo;
458            int notify_child_exit = 0;
459            time_t tm_notify_child_exit = time(NULL);
460          int sd_notify_stopping = 0;          int sd_notify_stopping = 0;
461          MENU_SET bbs_menu_new;          MENU_SET bbs_menu_new;
462            MENU_SET top10_menu_new;
463          int i, j;          int i, j;
464          pid_t pid;          pid_t pid;
465          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
# Line 400  int net_server(const char *hostaddr, in_ Line 600  int net_server(const char *hostaddr, in_
600    
601                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
602                  {                  {
603                          log_common("Notify %d child process to exit\n", SYS_child_process_count);                          if (notify_child_exit == 0)
                         if (kill(0, SIGTERM) < 0)  
604                          {                          {
605                                  log_error("Send SIGTERM signal failed (%d)\n", errno);                                  sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
606                                    log_common("Notify %d child process to exit\n", SYS_child_process_count);
607    
608                                    if (kill(0, SIGTERM) < 0)
609                                    {
610                                            log_error("Send SIGTERM signal failed (%d)\n", errno);
611                                    }
612    
613                                    notify_child_exit = 1;
614                                    tm_notify_child_exit = time(NULL);
615                          }                          }
616                            else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)
617                            {
618                                    sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
619    
620                          sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count);                                  for (i = 0; i < BBS_max_client; i++)
621                                    {
622                                            if (process_sockaddr_pool[i].pid != 0)
623                                            {
624                                                    log_error("Kill child process (pid=%d)\n", process_sockaddr_pool[i].pid);
625                                                    if (kill(process_sockaddr_pool[i].pid, SIGKILL) < 0)
626                                                    {
627                                                            log_error("Send SIGKILL signal failed (%d)\n", errno);
628                                                    }
629                                            }
630                                    }
631    
632                          sleep(1); // Sleep for a while to avoid notifying child processes too frequently                                  notify_child_exit = 2;
633                                    tm_notify_child_exit = time(NULL);
634                            }
635                            else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT)
636                            {
637                                    log_error("Main process prepare to exit without waiting for %d child process any longer\n", SYS_child_process_count);
638                                    SYS_child_process_count = 0;
639                            }
640                  }                  }
641    
642                  if (SYS_conf_reload && !SYS_server_exit)                  if (SYS_conf_reload && !SYS_server_exit)
# Line 425  int net_server(const char *hostaddr, in_ Line 653  int net_server(const char *hostaddr, in_
653                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
654                          {                          {
655                                  unload_menu(&bbs_menu_new);                                  unload_menu(&bbs_menu_new);
656                                  log_error("Reload menu failed\n");                                  log_error("Reload bbs menu failed\n");
657                          }                          }
658                          else                          else
659                          {                          {
660                                  unload_menu(&bbs_menu);                                  unload_menu(&bbs_menu);
661                                  memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));                                  memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
662                                  log_common("Reload menu successfully\n");                                  log_common("Reload bbs menu successfully\n");
663                            }
664    
665                            if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)
666                            {
667                                    unload_menu(&top10_menu_new);
668                                    log_error("Reload top10 menu failed\n");
669                            }
670                            else
671                            {
672                                    unload_menu(&top10_menu);
673                                    top10_menu_new.allow_exit = 1;
674                                    memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new));
675                                    log_common("Reload top10 menu successfully\n");
676                          }                          }
677    
678                          for (int i = 0; i < data_files_load_startup_count; i++)                          for (int i = 0; i < data_files_load_startup_count; i++)
679                          {                          {
680                                  if (load_file(data_files_load_startup[i]) < 0)                                  if (load_file(data_files_load_startup[i]) < 0)
681                                  {                                  {
682                                          log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);                                          log_error("load_file(%s) error\n", data_files_load_startup[i]);
683                                  }                                  }
684                          }                          }
685                          log_common("Reload data files successfully\n");                          log_common("Reload data files successfully\n");


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

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