/[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.52 by sysadm, Thu Jun 5 08:36:02 2025 UTC Revision 1.68 by sysadm, Thu Jun 26 12:17:02 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.h"
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>
 #include <string.h>  
32  #include <signal.h>  #include <signal.h>
33  #include <stdlib.h>  #include <stdlib.h>
34    #include <string.h>
35  #include <unistd.h>  #include <unistd.h>
 #include <sys/syscall.h>  
 #include <sys/socket.h>  
 #include <sys/wait.h>  
 #include <sys/epoll.h>  
36  #include <arpa/inet.h>  #include <arpa/inet.h>
37    #include <libssh/callbacks.h>
38    #include <libssh/libssh.h>
39    #include <libssh/server.h>
40  #include <netinet/in.h>  #include <netinet/in.h>
41    #include <sys/epoll.h>
42    #include <sys/socket.h>
43    #include <sys/syscall.h>
44    #include <sys/types.h>
45    #include <sys/wait.h>
46  #include <systemd/sd-daemon.h>  #include <systemd/sd-daemon.h>
47    
48  struct process_sockaddr_t  struct process_sockaddr_t
# Line 50  typedef struct process_sockaddr_t PROCES Line 54  typedef struct process_sockaddr_t PROCES
54    
55  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];  static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];
56    
57    #define SSH_AUTH_MAX_DURATION (60 * 1000) // milliseconds
58    
59    struct ssl_server_cb_data_t
60    {
61            int tries;
62            int error;
63    };
64    
65    static int auth_password(ssh_session session, const char *user,
66                                                     const char *password, void *userdata)
67    {
68            struct ssl_server_cb_data_t *p_data = userdata;
69            int ret;
70    
71            if (strcmp(user, "guest") == 0)
72            {
73                    ret = load_guest_info();
74            }
75            else
76            {
77                    ret = check_user(user, password);
78            }
79    
80            if (ret == 0)
81            {
82                    return SSH_AUTH_SUCCESS;
83            }
84    
85            if ((++(p_data->tries)) >= BBS_login_retry_times)
86            {
87                    p_data->error = 1;
88            }
89    
90            return SSH_AUTH_DENIED;
91    }
92    
93    static int pty_request(ssh_session session, ssh_channel channel, const char *term,
94                                               int x, int y, int px, int py, void *userdata)
95    {
96            return 0;
97    }
98    
99    static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
100    {
101            return 0;
102    }
103    
104    static struct ssh_channel_callbacks_struct channel_cb = {
105            .channel_pty_request_function = pty_request,
106            .channel_shell_request_function = shell_request};
107    
108    static ssh_channel new_session_channel(ssh_session session, void *userdata)
109    {
110            if (SSH_channel != NULL)
111            {
112                    return NULL;
113            }
114    
115            SSH_channel = ssh_channel_new(session);
116            ssh_callbacks_init(&channel_cb);
117            ssh_set_channel_callbacks(SSH_channel, &channel_cb);
118    
119            return SSH_channel;
120    }
121    
122    static int fork_server(void)
123    {
124            ssh_event event;
125            int pid;
126            int i;
127            int ret;
128    
129            struct ssl_server_cb_data_t cb_data = {
130                    .tries = 0,
131                    .error = 0,
132            };
133    
134            struct ssh_server_callbacks_struct cb = {
135                    .userdata = &cb_data,
136                    .auth_password_function = auth_password,
137                    .channel_open_request_session_function = new_session_channel,
138            };
139    
140            pid = fork();
141    
142            if (pid > 0) // Parent process
143            {
144                    SYS_child_process_count++;
145                    log_common("Child process (%d) start\n", pid);
146                    return pid;
147            }
148            else if (pid < 0) // Error
149            {
150                    log_error("fork() error (%d)\n", errno);
151                    return -1;
152            }
153    
154            // Child process
155    
156            if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1)
157            {
158                    log_error("Close server socket failed\n");
159            }
160    
161            SSH_session = ssh_new();
162    
163            if (SSH_v2)
164            {
165                    if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK)
166                    {
167                            log_error("ssh_bind_accept_fd() error: %s\n", ssh_get_error(SSH_session));
168                            goto cleanup;
169                    }
170    
171                    ssh_bind_free(sshbind);
172    
173                    ssh_callbacks_init(&cb);
174                    ssh_set_server_callbacks(SSH_session, &cb);
175    
176                    if (ssh_handle_key_exchange(SSH_session))
177                    {
178                            log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));
179                            goto cleanup;
180                    }
181                    ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);
182    
183                    event = ssh_event_new();
184                    ssh_event_add_session(event, SSH_session);
185    
186                    for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i += 100)
187                    {
188                            ret = ssh_event_dopoll(event, 100); // 0.1 second
189                            if (ret == SSH_ERROR)
190                            {
191                                    log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));
192                                    goto cleanup;
193                            }
194                    }
195    
196                    if (cb_data.error)
197                    {
198                            log_error("SSH auth error, tried %d times\n", cb_data.tries);
199                            goto cleanup;
200                    }
201            }
202    
203            // Redirect Input
204            close(STDIN_FILENO);
205            if (dup2(socket_client, STDIN_FILENO) == -1)
206            {
207                    log_error("Redirect stdin to client socket failed\n");
208                    goto cleanup;
209            }
210    
211            // Redirect Output
212            close(STDOUT_FILENO);
213            if (dup2(socket_client, STDOUT_FILENO) == -1)
214            {
215                    log_error("Redirect stdout to client socket failed\n");
216                    goto cleanup;
217            }
218    
219            SYS_child_process_count = 0;
220    
221            bbs_main();
222    
223    cleanup:
224            // Child process exit
225            SYS_server_exit = 1;
226    
227            if (SSH_v2)
228            {
229                    ssh_channel_free(SSH_channel);
230                    ssh_disconnect(SSH_session);
231            }
232            else if (close(socket_client) == -1)
233            {
234                    log_error("Close client socket failed\n");
235            }
236    
237            ssh_free(SSH_session);
238            ssh_finalize();
239    
240            // Close Input and Output for client
241            close(STDIN_FILENO);
242            close(STDOUT_FILENO);
243    
244            log_common("Process exit normally\n");
245            log_end();
246    
247            _exit(0);
248    
249            return 0;
250    }
251    
252  int net_server(const char *hostaddr, in_port_t port[])  int net_server(const char *hostaddr, in_port_t port[])
253  {  {
254          unsigned int addrlen;          unsigned int addrlen;
# Line 60  int net_server(const char *hostaddr, in_ Line 259  int net_server(const char *hostaddr, in_
259          int nfds, epollfd;          int nfds, epollfd;
260          siginfo_t siginfo;          siginfo_t siginfo;
261          int sd_notify_stopping = 0;          int sd_notify_stopping = 0;
262          MENU_SET *p_bbs_menu_new;          MENU_SET bbs_menu_new;
263          int i, j;          int i, j;
264          pid_t pid;          pid_t pid;
265          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
# Line 72  int net_server(const char *hostaddr, in_ Line 271  int net_server(const char *hostaddr, in_
271          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
272                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
273                  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 ||
274                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 ||
275                  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)
276          {          {
277                  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 420  int net_server(const char *hostaddr, in_
420                                  log_error("Reload conf failed\n");                                  log_error("Reload conf failed\n");
421                          }                          }
422    
423                          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)  
424                          {                          {
425                                  unload_menu(p_bbs_menu_new);                                  unload_menu(&bbs_menu_new);
                                 free(p_bbs_menu_new);  
                                 p_bbs_menu_new = NULL;  
   
426                                  log_error("Reload menu failed\n");                                  log_error("Reload menu failed\n");
427                          }                          }
428                          else                          else
429                          {                          {
430                                  unload_menu(p_bbs_menu);                                  unload_menu(&bbs_menu);
431                                  free(p_bbs_menu);                                  memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
   
                                 p_bbs_menu = p_bbs_menu_new;  
                                 p_bbs_menu_new = NULL;  
   
432                                  log_common("Reload menu successfully\n");                                  log_common("Reload menu successfully\n");
433                          }                          }
434    
                         sd_notify(0, "READY=1");  
                 }  
   
                 if (SYS_data_file_reload && !SYS_server_exit)  
                 {  
                         SYS_data_file_reload = 0;  
                         sd_notify(0, "RELOADING=1");  
   
435                          for (int i = 0; i < data_files_load_startup_count; i++)                          for (int i = 0; i < data_files_load_startup_count; i++)
436                          {                          {
437                                  if (load_file(data_files_load_startup[i]) < 0)                                  if (load_file(data_files_load_startup[i]) < 0)
# Line 259  int net_server(const char *hostaddr, in_ Line 439  int net_server(const char *hostaddr, in_
439                                          log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);                                          log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);
440                                  }                                  }
441                          }                          }
   
442                          log_common("Reload data files successfully\n");                          log_common("Reload data files successfully\n");
                         sd_notify(0, "READY=1");  
                 }  
443    
444                  if (SYS_section_list_reload && !SYS_server_exit)                          // Load section config and gen_ex
445                  {                          if (load_section_config_from_db(1) < 0)
                         SYS_section_list_reload = 0;  
   
                         if (section_list_loader_reload() < 0)  
446                          {                          {
447                                  log_error("ksection_list_loader_reload() failed\n");                                  log_error("load_section_config_from_db(1) error\n");
448                          }                          }
449                            else
450                            {
451                                    log_common("Reload section config and gen_ex successfully\n");
452                            }
453    
454                            sd_notify(0, "READY=1");
455                  }                  }
456    
457                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
# Line 296  int net_server(const char *hostaddr, in_ Line 476  int net_server(const char *hostaddr, in_
476                  {                  {
477                          if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1])                          if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1])
478                          {                          {
479                                  SSH_v2 = (events[i].data.fd == socket_server[1]);                                  SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0);
480    
481                                  while (!SYS_server_exit) // Accept all incoming connections until error                                  while (!SYS_server_exit) // Accept all incoming connections until error
482                                  {                                  {
483                                          addrlen = sizeof(sin);                                          addrlen = sizeof(sin);
484                                          socket_client = accept(events[i].data.fd, (struct sockaddr *)&sin, &addrlen);                                          socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, &addrlen);
485                                          if (socket_client < 0)                                          if (socket_client < 0)
486                                          {                                          {
487                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 324  int net_server(const char *hostaddr, in_ Line 504  int net_server(const char *hostaddr, in_
504    
505                                          port_client = ntohs(sin.sin_port);                                          port_client = ntohs(sin.sin_port);
506    
507                                          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);
508    
509                                          if (SYS_child_process_count - 1 < BBS_max_client)                                          if (SYS_child_process_count - 1 < BBS_max_client)
510                                          {                                          {


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

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