/[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.73 by sysadm, Sun Oct 19 01:30:38 2025 UTC Revision 1.79 by sysadm, Wed Nov 5 04:19:21 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  enum _net_server_constant_t
43  #define WAIT_CHILD_PROCESS_KILL_TIMEOUT 1 // second  {
44            WAIT_CHILD_PROCESS_EXIT_TIMEOUT = 5, // second
45            WAIT_CHILD_PROCESS_KILL_TIMEOUT = 1, // second
46    
47            SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds
48    };
49    
50  struct process_sockaddr_t  struct process_sockaddr_t
51  {  {
# Line 57  typedef struct process_sockaddr_t PROCES Line 56  typedef struct process_sockaddr_t PROCES
56    
57  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];
58    
59  #define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds  static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server";
60    
61  struct ssl_server_cb_data_t  /* A userdata struct for session. */
62    struct session_data_struct
63  {  {
64          int tries;          int tries;
65          int error;          int error;
66  };  };
67    
68    /* A userdata struct for channel. */
69    struct channel_data_struct
70    {
71            /* pid of the child process the channel will spawn. */
72            pid_t pid;
73            /* For PTY allocation */
74            socket_t pty_master;
75            socket_t pty_slave;
76            /* For communication with the child process. */
77            socket_t child_stdin;
78            socket_t child_stdout;
79            /* Only used for subsystem and exec requests. */
80            socket_t child_stderr;
81            /* Event which is used to poll the above descriptors. */
82            ssh_event event;
83            /* Terminal size struct. */
84            struct winsize *winsize;
85    };
86    
87  static int auth_password(ssh_session session, const char *user,  static int auth_password(ssh_session session, const char *user,
88                                                   const char *password, void *userdata)                                                   const char *password, void *userdata)
89  {  {
90          struct ssl_server_cb_data_t *p_data = userdata;          struct session_data_struct *sdata = (struct session_data_struct *)userdata;
91          int ret;          int ret;
92    
93          if (strcmp(user, "guest") == 0)          if (strcmp(user, "guest") == 0)
# Line 85  static int auth_password(ssh_session ses Line 104  static int auth_password(ssh_session ses
104                  return SSH_AUTH_SUCCESS;                  return SSH_AUTH_SUCCESS;
105          }          }
106    
107          if ((++(p_data->tries)) >= BBS_login_retry_times)          if ((++(sdata->tries)) >= BBS_login_retry_times)
108          {          {
109                  p_data->error = 1;                  sdata->error = 1;
110          }          }
111    
112          return SSH_AUTH_DENIED;          return SSH_AUTH_DENIED;
113  }  }
114    
115  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,
116                                             int x, int y, int px, int py, void *userdata)                                             int cols, int rows, int px, int py, void *userdata)
117  {  {
118          return 0;          struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
119            int rc;
120    
121            (void)session;
122            (void)channel;
123            (void)term;
124    
125            cdata->winsize->ws_row = (unsigned short int)rows;
126            cdata->winsize->ws_col = (unsigned short int)cols;
127            cdata->winsize->ws_xpixel = (unsigned short int)px;
128            cdata->winsize->ws_ypixel = (unsigned short int)py;
129    
130            rc = openpty(&cdata->pty_master, &cdata->pty_slave, NULL, NULL, cdata->winsize);
131            if (rc != 0)
132            {
133                    log_error("Failed to open pty\n");
134                    return SSH_ERROR;
135            }
136    
137            return SSH_OK;
138    }
139    
140    static int pty_resize(ssh_session session, ssh_channel channel, int cols, int rows,
141                                              int py, int px, void *userdata)
142    {
143            struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
144    
145            (void)session;
146            (void)channel;
147    
148            cdata->winsize->ws_row = (unsigned short int)rows;
149            cdata->winsize->ws_col = (unsigned short int)cols;
150            cdata->winsize->ws_xpixel = (unsigned short int)px;
151            cdata->winsize->ws_ypixel = (unsigned short int)py;
152    
153            if (cdata->pty_master != -1)
154            {
155                    return ioctl(cdata->pty_master, TIOCSWINSZ, cdata->winsize);
156            }
157    
158            return SSH_ERROR;
159    }
160    
161    static int exec_pty(const char *mode, const char *command, struct channel_data_struct *cdata)
162    {
163            (void)cdata;
164    
165            if (command != NULL)
166            {
167                    log_error("Forbid exec /bin/sh %s %s)\n", mode, command);
168            }
169    
170            return SSH_OK;
171    }
172    
173    static int exec_nopty(const char *command, struct channel_data_struct *cdata)
174    {
175            (void)cdata;
176    
177            if (command != NULL)
178            {
179                    log_error("Forbid exec /bin/sh -c %s)\n", command);
180            }
181    
182            return SSH_OK;
183    }
184    
185    static int exec_request(ssh_session session, ssh_channel channel, const char *command, void *userdata)
186    {
187            struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
188    
189            (void)session;
190            (void)channel;
191    
192            if (cdata->pid > 0)
193            {
194                    return SSH_ERROR;
195            }
196    
197            if (cdata->pty_master != -1 && cdata->pty_slave != -1)
198            {
199                    return exec_pty("-c", command, cdata);
200            }
201            return exec_nopty(command, cdata);
202  }  }
203    
204  static int shell_request(ssh_session session, ssh_channel channel, void *userdata)  static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
205  {  {
206          return 0;          struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
207    
208            (void)session;
209            (void)channel;
210    
211            if (cdata->pid > 0)
212            {
213                    return SSH_ERROR;
214            }
215    
216            if (cdata->pty_master != -1 && cdata->pty_slave != -1)
217            {
218                    return exec_pty("-l", NULL, cdata);
219            }
220            /* Client requested a shell without a pty, let's pretend we allow that */
221            return SSH_OK;
222  }  }
223    
224  static struct ssh_channel_callbacks_struct channel_cb = {  static int subsystem_request(ssh_session session, ssh_channel channel, const char *subsystem, void *userdata)
225          .channel_pty_request_function = pty_request,  {
226          .channel_shell_request_function = shell_request};          (void)session;
227            (void)channel;
228    
229            log_error("subsystem_request(subsystem=%s)\n", subsystem);
230    
231  static ssh_channel new_session_channel(ssh_session session, void *userdata)          /* subsystem requests behave similarly to exec requests. */
232            if (strcmp(subsystem, "sftp") == 0)
233            {
234                    return exec_request(session, channel, SFTP_SERVER_PATH, userdata);
235            }
236            return SSH_ERROR;
237    }
238    
239    static ssh_channel channel_open(ssh_session session, void *userdata)
240  {  {
241            (void)userdata;
242    
243          if (SSH_channel != NULL)          if (SSH_channel != NULL)
244          {          {
245                  return NULL;                  return NULL;
246          }          }
247    
248          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);  
249    
250          return SSH_channel;          return SSH_channel;
251  }  }
# Line 130  static int fork_server(void) Line 258  static int fork_server(void)
258          int i;          int i;
259          int ret;          int ret;
260    
261          struct ssl_server_cb_data_t cb_data = {          /* Structure for storing the pty size. */
262            struct winsize wsize = {
263                    .ws_row = 0,
264                    .ws_col = 0,
265                    .ws_xpixel = 0,
266                    .ws_ypixel = 0};
267    
268            /* Our struct holding information about the channel. */
269            struct channel_data_struct cdata = {
270                    .pid = 0,
271                    .pty_master = -1,
272                    .pty_slave = -1,
273                    .child_stdin = -1,
274                    .child_stdout = -1,
275                    .child_stderr = -1,
276                    .event = NULL,
277                    .winsize = &wsize};
278    
279            struct session_data_struct cb_data = {
280                  .tries = 0,                  .tries = 0,
281                  .error = 0,                  .error = 0,
282          };          };
283    
284          struct ssh_server_callbacks_struct cb = {          struct ssh_channel_callbacks_struct channel_cb = {
285                    .userdata = &cdata,
286                    .channel_pty_request_function = pty_request,
287                    .channel_pty_window_change_function = pty_resize,
288                    .channel_shell_request_function = shell_request,
289                    .channel_exec_request_function = exec_request,
290                    .channel_subsystem_request_function = subsystem_request};
291    
292            struct ssh_server_callbacks_struct server_cb = {
293                  .userdata = &cb_data,                  .userdata = &cb_data,
294                  .auth_password_function = auth_password,                  .auth_password_function = auth_password,
295                  .channel_open_request_session_function = new_session_channel,                  .channel_open_request_session_function = channel_open,
296          };          };
297    
298          pid = fork();          pid = fork();
# Line 174  static int fork_server(void) Line 328  static int fork_server(void)
328    
329                  ssh_bind_free(sshbind);                  ssh_bind_free(sshbind);
330    
                 ssh_callbacks_init(&cb);  
                 ssh_set_server_callbacks(SSH_session, &cb);  
   
331                  ssh_timeout = 60; // second                  ssh_timeout = 60; // second
332                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
333                  {                  {
# Line 184  static int fork_server(void) Line 335  static int fork_server(void)
335                          goto cleanup;                          goto cleanup;
336                  }                  }
337    
338                    ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);
339    
340                    ssh_callbacks_init(&server_cb);
341                    ssh_callbacks_init(&channel_cb);
342    
343                    ssh_set_server_callbacks(SSH_session, &server_cb);
344    
345                  if (ssh_handle_key_exchange(SSH_session))                  if (ssh_handle_key_exchange(SSH_session))
346                  {                  {
347                          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));
348                          goto cleanup;                          goto cleanup;
349                  }                  }
                 ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);  
350    
351                  event = ssh_event_new();                  event = ssh_event_new();
352                  ssh_event_add_session(event, SSH_session);                  ssh_event_add_session(event, SSH_session);
# Line 212  static int fork_server(void) Line 369  static int fork_server(void)
369                          goto cleanup;                          goto cleanup;
370                  }                  }
371    
372                    ssh_set_channel_callbacks(SSH_channel, &channel_cb);
373    
374                    do
375                    {
376                            ret = ssh_event_dopoll(event, 100); // 0.1 second
377                            if (ret == SSH_ERROR)
378                            {
379                                    ssh_channel_close(SSH_channel);
380                            }
381    
382                            if (ret == SSH_AGAIN) // loop until SSH connection is fully established
383                            {
384                                    /* Executed only once, once the child process starts. */
385                                    cdata.event = event;
386                                    break;
387                            }
388                    } while (ssh_channel_is_open(SSH_channel));
389    
390                  ssh_timeout = 0;                  ssh_timeout = 0;
391                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
392                  {                  {
# Line 246  cleanup: Line 421  cleanup:
421    
422          if (SSH_v2)          if (SSH_v2)
423          {          {
424                    close(cdata.pty_master);
425                    close(cdata.child_stdin);
426                    close(cdata.child_stdout);
427                    close(cdata.child_stderr);
428    
429                  ssh_channel_free(SSH_channel);                  ssh_channel_free(SSH_channel);
430                  ssh_disconnect(SSH_session);                  ssh_disconnect(SSH_session);
431          }          }
# Line 502  int net_server(const char *hostaddr, in_ Line 682  int net_server(const char *hostaddr, in_
682                          {                          {
683                                  if (load_file(data_files_load_startup[i]) < 0)                                  if (load_file(data_files_load_startup[i]) < 0)
684                                  {                                  {
685                                          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]);
686                                  }                                  }
687                          }                          }
688                          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