/[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.71 by sysadm, Mon Sep 22 05:07:22 2025 UTC Revision 1.75 by sysadm, Sat Oct 25 07:58:10 2025 UTC
# Line 29  Line 29 
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 59  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 85  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 130  static int fork_server(void) Line 263  static int fork_server(void)
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 174  static int fork_server(void) Line 333  static int fork_server(void)
333    
334                  ssh_bind_free(sshbind);                  ssh_bind_free(sshbind);
335    
                 ssh_callbacks_init(&cb);  
                 ssh_set_server_callbacks(SSH_session, &cb);  
   
336                  ssh_timeout = 60; // second                  ssh_timeout = 60; // second
337                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
338                  {                  {
# Line 184  static int fork_server(void) Line 340  static int fork_server(void)
340                          goto cleanup;                          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 199  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 210  static int fork_server(void) Line 374  static int fork_server(void)
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;                  ssh_timeout = 0;
396                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
397                  {                  {
# Line 244  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 280  int net_server(const char *hostaddr, in_ Line 467  int net_server(const char *hostaddr, in_
467          time_t tm_notify_child_exit = time(NULL);          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 473  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);                                  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                            if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)
674                            {
675                                    unload_menu(&top10_menu_new);
676                                    log_error("Reload top10 menu failed\n");
677                            }
678                            else
679                            {
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++)


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

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