/[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.53 by sysadm, Thu Jun 5 09:04:55 2025 UTC Revision 1.66 by sysadm, Wed Jun 25 01:50:14 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17  #define _XOPEN_SOURCE 500  #include "bbs.h"
18  #define _POSIX_C_SOURCE 200809L  #include "bbs_main.h"
 #define _GNU_SOURCE  
   
 #include "net_server.h"  
19  #include "common.h"  #include "common.h"
20  #include "log.h"  #include "database.h"
21    #include "file_loader.h"
22  #include "io.h"  #include "io.h"
23  #include "init.h"  #include "init.h"
24  #include "fork.h"  #include "log.h"
25    #include "login.h"
26  #include "menu.h"  #include "menu.h"
27  #include "file_loader.h"  #include "net_server.h"
28  #include "section_list_loader.h"  #include "section_list_loader.h"
29  #include <errno.h>  #include <errno.h>
30  #include <fcntl.h>  #include <fcntl.h>
 #include <string.h>  
31  #include <signal.h>  #include <signal.h>
32  #include <stdlib.h>  #include <stdlib.h>
33    #include <string.h>
34  #include <unistd.h>  #include <unistd.h>
 #include <sys/syscall.h>  
 #include <sys/socket.h>  
 #include <sys/wait.h>  
 #include <sys/epoll.h>  
35  #include <arpa/inet.h>  #include <arpa/inet.h>
36    #include <libssh/callbacks.h>
37    #include <libssh/libssh.h>
38    #include <libssh/server.h>
39  #include <netinet/in.h>  #include <netinet/in.h>
40    #include <sys/epoll.h>
41    #include <sys/socket.h>
42    #include <sys/syscall.h>
43    #include <sys/types.h>
44    #include <sys/wait.h>
45  #include <systemd/sd-daemon.h>  #include <systemd/sd-daemon.h>
46    
47  struct process_sockaddr_t  struct process_sockaddr_t
# Line 50  typedef struct process_sockaddr_t PROCES Line 53  typedef struct process_sockaddr_t PROCES
53    
54  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];
55    
56    #define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds
57    
58    struct ssl_server_cb_data_t
59    {
60            int tries;
61            int error;
62    };
63    
64    static int auth_password(ssh_session session, const char *user,
65                                                     const char *password, void *userdata)
66    {
67            struct ssl_server_cb_data_t *p_data = userdata;
68            int ret;
69    
70            if (strcmp(user, "guest") == 0)
71            {
72                    ret = load_guest_info();
73            }
74            else
75            {
76                    ret = check_user(user, password);
77            }
78    
79            if (ret == 0)
80            {
81                    return SSH_AUTH_SUCCESS;
82            }
83    
84            if ((++(p_data->tries)) >= BBS_login_retry_times)
85            {
86                    p_data->error = 1;
87            }
88    
89            return SSH_AUTH_DENIED;
90    }
91    
92    static int pty_request(ssh_session session, ssh_channel channel, const char *term,
93                                               int x, int y, int px, int py, void *userdata)
94    {
95            return 0;
96    }
97    
98    static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
99    {
100            return 0;
101    }
102    
103    static struct ssh_channel_callbacks_struct channel_cb = {
104            .channel_pty_request_function = pty_request,
105            .channel_shell_request_function = shell_request};
106    
107    static ssh_channel new_session_channel(ssh_session session, void *userdata)
108    {
109            if (SSH_channel != NULL)
110            {
111                    return NULL;
112            }
113    
114            SSH_channel = ssh_channel_new(session);
115            ssh_callbacks_init(&channel_cb);
116            ssh_set_channel_callbacks(SSH_channel, &channel_cb);
117    
118            return SSH_channel;
119    }
120    
121    static int fork_server(void)
122    {
123            ssh_event event;
124            int pid;
125            int i;
126            int ret;
127    
128            struct ssl_server_cb_data_t cb_data = {
129                    .tries = 0,
130                    .error = 0,
131            };
132    
133            struct ssh_server_callbacks_struct cb = {
134                    .userdata = &cb_data,
135                    .auth_password_function = auth_password,
136                    .channel_open_request_session_function = new_session_channel,
137            };
138    
139            pid = fork();
140    
141            if (pid > 0) // Parent process
142            {
143                    SYS_child_process_count++;
144                    log_common("Child process (%d) start\n", pid);
145                    return pid;
146            }
147            else if (pid < 0) // Error
148            {
149                    log_error("fork() error (%d)\n", errno);
150                    return -1;
151            }
152    
153            // Child process
154    
155            if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1)
156            {
157                    log_error("Close server socket failed\n");
158            }
159    
160            SSH_session = ssh_new();
161    
162            if (SSH_v2)
163            {
164                    if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK)
165                    {
166                            log_error("ssh_bind_accept_fd() error: %s\n", ssh_get_error(SSH_session));
167                            goto cleanup;
168                    }
169    
170                    ssh_bind_free(sshbind);
171    
172                    ssh_callbacks_init(&cb);
173                    ssh_set_server_callbacks(SSH_session, &cb);
174    
175                    if (ssh_handle_key_exchange(SSH_session))
176                    {
177                            log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));
178                            goto cleanup;
179                    }
180                    ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);
181    
182                    event = ssh_event_new();
183                    ssh_event_add_session(event, SSH_session);
184    
185                    for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i += 100)
186                    {
187                            ret = ssh_event_dopoll(event, 100); // 0.1 second
188                            if (ret == SSH_ERROR)
189                            {
190                                    log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));
191                                    goto cleanup;
192                            }
193                    }
194    
195                    if (cb_data.error)
196                    {
197                            log_error("SSH auth error, tried %d times\n", cb_data.tries);
198                            goto cleanup;
199                    }
200            }
201    
202            // Redirect Input
203            close(STDIN_FILENO);
204            if (dup2(socket_client, STDIN_FILENO) == -1)
205            {
206                    log_error("Redirect stdin to client socket failed\n");
207                    goto cleanup;
208            }
209    
210            // Redirect Output
211            close(STDOUT_FILENO);
212            if (dup2(socket_client, STDOUT_FILENO) == -1)
213            {
214                    log_error("Redirect stdout to client socket failed\n");
215                    goto cleanup;
216            }
217    
218            SYS_child_process_count = 0;
219    
220            bbs_main();
221    
222    cleanup:
223            // Child process exit
224            SYS_server_exit = 1;
225    
226            if (SSH_v2)
227            {
228                    ssh_channel_free(SSH_channel);
229                    ssh_disconnect(SSH_session);
230            }
231            else if (close(socket_client) == -1)
232            {
233                    log_error("Close client socket failed\n");
234            }
235    
236            ssh_free(SSH_session);
237            ssh_finalize();
238    
239            // Close Input and Output for client
240            close(STDIN_FILENO);
241            close(STDOUT_FILENO);
242    
243            log_common("Process exit normally\n");
244            log_end();
245    
246            _exit(0);
247    
248            return 0;
249    }
250    
251  int net_server(const char *hostaddr, in_port_t port[])  int net_server(const char *hostaddr, in_port_t port[])
252  {  {
253          unsigned int addrlen;          unsigned int addrlen;
# Line 60  int net_server(const char *hostaddr, in_ Line 258  int net_server(const char *hostaddr, in_
258          int nfds, epollfd;          int nfds, epollfd;
259          siginfo_t siginfo;          siginfo_t siginfo;
260          int sd_notify_stopping = 0;          int sd_notify_stopping = 0;
261          MENU_SET *p_bbs_menu_new;          MENU_SET bbs_menu_new;
262          int i, j;          int i, j;
263          pid_t pid;          pid_t pid;
264          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
# Line 72  int net_server(const char *hostaddr, in_ Line 270  int net_server(const char *hostaddr, in_
270          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
271                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
272                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||
273                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 ||
274                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
275          {          {
276                  log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));                  log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));
# Line 220  int net_server(const char *hostaddr, in_ Line 419  int net_server(const char *hostaddr, in_
419                                  log_error("Reload conf failed\n");                                  log_error("Reload conf failed\n");
420                          }                          }
421    
422                          p_bbs_menu_new = calloc(1, sizeof(MENU_SET));                          if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
                         if (p_bbs_menu_new == NULL)  
                         {  
                                 log_error("OOM: calloc(MENU_SET)\n");  
                         }  
                         else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0)  
423                          {                          {
424                                  unload_menu(p_bbs_menu_new);                                  unload_menu(&bbs_menu_new);
                                 free(p_bbs_menu_new);  
                                 p_bbs_menu_new = NULL;  
   
425                                  log_error("Reload menu failed\n");                                  log_error("Reload menu failed\n");
426                          }                          }
427                          else                          else
428                          {                          {
429                                  unload_menu(p_bbs_menu);                                  memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
                                 free(p_bbs_menu);  
   
                                 p_bbs_menu = p_bbs_menu_new;  
                                 p_bbs_menu_new = NULL;  
   
430                                  log_common("Reload menu successfully\n");                                  log_common("Reload menu successfully\n");
431                          }                          }
432    
# Line 270  int net_server(const char *hostaddr, in_ Line 456  int net_server(const char *hostaddr, in_
456    
457                          if (section_list_loader_reload() < 0)                          if (section_list_loader_reload() < 0)
458                          {                          {
459                                  log_error("ksection_list_loader_reload() failed\n");                                  log_error("section_list_loader_reload() failed\n");
460                          }                          }
461                  }                  }
462    
# Line 324  int net_server(const char *hostaddr, in_ Line 510  int net_server(const char *hostaddr, in_
510    
511                                          port_client = ntohs(sin.sin_port);                                          port_client = ntohs(sin.sin_port);
512    
513                                          log_common("Accept connection from %s:%d\n", hostaddr_client, port_client);                                          log_common("Accept %s connection from %s:%d\n", (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client);
514    
515                                          if (SYS_child_process_count - 1 < BBS_max_client)                                          if (SYS_child_process_count - 1 < BBS_max_client)
516                                          {                                          {


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

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