/[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.72 by sysadm, Mon Oct 13 07:13:39 2025 UTC Revision 1.74 by sysadm, Sat Oct 25 03:11:11 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            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          return 0;          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    
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                  ssh_timeout = 60; // second
342                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
# Line 199  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 210  static int fork_server(void) Line 373  static int fork_server(void)
373                          goto cleanup;                          goto cleanup;
374                  }                  }
375    
376                    ssh_set_channel_callbacks(SSH_channel, &channel_cb);
377    
378                  ssh_timeout = 0;                  ssh_timeout = 0;
379                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
380                  {                  {
# Line 244  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          }          }


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

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