/[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.29 by sysadm, Tue May 13 02:20:52 2025 UTC Revision 1.56 by sysadm, Sat Jun 7 02:38:28 2025 UTC
# Line 20  Line 20 
20    
21  #include "net_server.h"  #include "net_server.h"
22  #include "common.h"  #include "common.h"
23    #include "bbs_main.h"
24    #include "bbs.h"
25  #include "log.h"  #include "log.h"
26  #include "io.h"  #include "io.h"
27  #include "fork.h"  #include "init.h"
28  #include "menu.h"  #include "menu.h"
29    #include "database.h"
30    #include "login.h"
31    #include "file_loader.h"
32    #include "section_list_loader.h"
33  #include <errno.h>  #include <errno.h>
34  #include <fcntl.h>  #include <fcntl.h>
35  #include <string.h>  #include <string.h>
# Line 35  Line 41 
41  #include <sys/wait.h>  #include <sys/wait.h>
42  #include <sys/epoll.h>  #include <sys/epoll.h>
43  #include <arpa/inet.h>  #include <arpa/inet.h>
44    #include <netinet/in.h>
45    #include <systemd/sd-daemon.h>
46    #include <libssh/libssh.h>
47    #include <libssh/server.h>
48    #include <libssh/callbacks.h>
49    
50  int net_server(const char *hostaddr, in_port_t port)  struct process_sockaddr_t
51  {  {
52          unsigned int namelen;          pid_t pid;
53            in_addr_t s_addr;
54    };
55    typedef struct process_sockaddr_t PROCESS_SOCKADDR;
56    
57    static PROCESS_SOCKADDR process_sockaddr_pool[MAX_CLIENT_LIMIT];
58    
59    #define SSH_AUTH_MAX_DURATION 60 // seconds
60    
61    struct ssl_server_cb_data_t
62    {
63            int tries;
64            int error;
65    };
66    
67    static int auth_password(ssh_session session, const char *user,
68                                                     const char *password, void *userdata)
69    {
70            MYSQL *db;
71            struct ssl_server_cb_data_t *p_data = userdata;
72          int ret;          int ret;
         int flags;  
         struct sockaddr_in sin;  
         struct epoll_event ev, events[MAX_EVENTS];  
         int nfds, epollfd;  
         sigset_t nsigset;  
         sigset_t osigset;  
         siginfo_t siginfo;  
73    
74          socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);          if ((db = db_open()) == NULL)
75            {
76                    return SSH_AUTH_ERROR;
77            }
78    
79          if (socket_server < 0)          if (strcmp(user, "guest") == 0)
80          {          {
81                  log_error("Create socket failed\n");                  ret = load_guest_info(db);
82                  return -1;          }
83            else
84            {
85                    ret = check_user(db, user, password);
86          }          }
87    
88          sin.sin_family = AF_INET;          mysql_close(db);
         sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY);  
         sin.sin_port = htons(port);  
89    
90          // Reuse address and port          if (ret == 0)
         flags = 1;  
         if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags)) < 0)  
91          {          {
92                  log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);                  return SSH_AUTH_SUCCESS;
93          }          }
94          if (setsockopt(socket_server, SOL_SOCKET, SO_REUSEPORT, &flags, sizeof(flags)) < 0)  
95            if ((++(p_data->tries)) >= BBS_login_retry_times)
96          {          {
97                  log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);                  p_data->error = 1;
98          }          }
99    
100          if (bind(socket_server, (struct sockaddr *)&sin, sizeof(sin)) < 0)          return SSH_AUTH_DENIED;
101    }
102    
103    static int pty_request(ssh_session session, ssh_channel channel, const char *term,
104                                               int x, int y, int px, int py, void *userdata)
105    {
106            return 0;
107    }
108    
109    static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
110    {
111            return 0;
112    }
113    
114    static struct ssh_channel_callbacks_struct channel_cb = {
115            .channel_pty_request_function = pty_request,
116            .channel_shell_request_function = shell_request};
117    
118    static ssh_channel new_session_channel(ssh_session session, void *userdata)
119    {
120            if (SSH_channel != NULL)
121          {          {
122                  log_error("Bind address %s:%u failed (%d)\n",                  return NULL;
                                   inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);  
                 return -1;  
123          }          }
124    
125          if (listen(socket_server, 10) < 0)          SSH_channel = ssh_channel_new(session);
126            ssh_callbacks_init(&channel_cb);
127            ssh_set_channel_callbacks(SSH_channel, &channel_cb);
128    
129            return SSH_channel;
130    }
131    
132    static int fork_server(void)
133    {
134            ssh_event event;
135            int pid;
136            int i;
137            int ret;
138    
139            struct ssl_server_cb_data_t cb_data = {
140                    .tries = 0,
141                    .error = 0,
142            };
143    
144            struct ssh_server_callbacks_struct cb = {
145                    .userdata = &cb_data,
146                    .auth_password_function = auth_password,
147                    .channel_open_request_session_function = new_session_channel,
148            };
149    
150            pid = fork();
151    
152            if (pid > 0) // Parent process
153          {          {
154                  log_error("Socket listen failed (%d)\n", errno);                  SYS_child_process_count++;
155                    log_common("Child process (%d) start\n", pid);
156                    return pid;
157            }
158            else if (pid < 0) // Error
159            {
160                    log_error("fork() error (%d)\n", errno);
161                  return -1;                  return -1;
162          }          }
163    
164          strncpy(hostaddr_server, inet_ntoa(sin.sin_addr), sizeof(hostaddr_server) - 1);          // Child process
165          hostaddr_server[sizeof(hostaddr_server) - 1] = '\0';  
166            if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1)
167            {
168                    log_error("Close server socket failed\n");
169            }
170    
171            SSH_session = ssh_new();
172    
173            if (SSH_v2)
174            {
175                    if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK)
176                    {
177                            log_error("ssh_bind_accept_fd() error: %s\n", ssh_get_error(SSH_session));
178                            goto cleanup;
179                    }
180    
181                    ssh_bind_free(sshbind);
182    
183                    ssh_callbacks_init(&cb);
184                    ssh_set_server_callbacks(SSH_session, &cb);
185    
186                    if (ssh_handle_key_exchange(SSH_session))
187                    {
188                            log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));
189                            goto cleanup;
190                    }
191                    ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);
192    
193                    event = ssh_event_new();
194                    ssh_event_add_session(event, SSH_session);
195    
196                    for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i++)
197                    {
198                            ret = ssh_event_dopoll(event, 1000); // 1 second
199                            if (ret == SSH_ERROR)
200                            {
201                                    log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));
202                                    goto cleanup;
203                            }
204                    }
205    
206                    if (cb_data.error)
207                    {
208                            log_error("SSH auth error, tried %d times\n", cb_data.tries);
209                            goto cleanup;
210                    }
211            }
212    
213            // Redirect Input
214            close(STDIN_FILENO);
215            if (dup2(socket_client, STDIN_FILENO) == -1)
216            {
217                    log_error("Redirect stdin to client socket failed\n");
218                    goto cleanup;
219            }
220    
221            // Redirect Output
222            close(STDOUT_FILENO);
223            if (dup2(socket_client, STDOUT_FILENO) == -1)
224            {
225                    log_error("Redirect stdout to client socket failed\n");
226                    goto cleanup;
227            }
228    
229          port_server = ntohs(sin.sin_port);          SYS_child_process_count = 0;
         namelen = sizeof(sin);  
230    
231          log_std("Listening at %s:%d\n", hostaddr_server, port_server);          bbs_main();
232    
233          sigemptyset(&nsigset);  cleanup:
234          sigaddset(&nsigset, SIGHUP);          // Child process exit
235          sigaddset(&nsigset, SIGCHLD);          SYS_server_exit = 1;
236          sigaddset(&nsigset, SIGTERM);  
237            if (SSH_v2)
238            {
239                    ssh_channel_free(SSH_channel);
240                    ssh_disconnect(SSH_session);
241            }
242            else if (close(socket_client) == -1)
243            {
244                    log_error("Close client socket failed\n");
245            }
246    
247            ssh_free(SSH_session);
248            ssh_finalize();
249    
250            // Close Input and Output for client
251            close(STDIN_FILENO);
252            close(STDOUT_FILENO);
253    
254            log_common("Process exit normally\n");
255            log_end();
256    
257            _exit(0);
258    
259            return 0;
260    }
261    
262    int net_server(const char *hostaddr, in_port_t port[])
263    {
264            unsigned int addrlen;
265            int ret;
266            int flags[2];
267            struct sockaddr_in sin;
268            struct epoll_event ev, events[MAX_EVENTS];
269            int nfds, epollfd;
270            siginfo_t siginfo;
271            int sd_notify_stopping = 0;
272            MENU_SET *p_bbs_menu_new;
273            int i, j;
274            pid_t pid;
275            int ssh_log_level = SSH_LOG_NOLOG;
276    
277            ssh_init();
278    
279            sshbind = ssh_bind_new();
280    
281            if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
282                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
283                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||
284                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
285            {
286                    log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));
287                    ssh_bind_free(sshbind);
288                    return -1;
289            }
290    
291          epollfd = epoll_create1(0);          epollfd = epoll_create1(0);
292          if (epollfd < 0)          if (epollfd < 0)
# Line 104  int net_server(const char *hostaddr, in_ Line 295  int net_server(const char *hostaddr, in_
295                  return -1;                  return -1;
296          }          }
297    
298          ev.events = EPOLLIN;          // Server socket
299          ev.data.fd = socket_server;          for (i = 0; i < 2; i++)
         if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server, &ev) == -1)  
300          {          {
301                  log_error("epoll_ctl(socket_server) error (%d)\n", errno);                  socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
302                  return -1;  
303                    if (socket_server[i] < 0)
304                    {
305                            log_error("Create socket_server error (%d)\n", errno);
306                            return -1;
307                    }
308    
309                    sin.sin_family = AF_INET;
310                    sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY);
311                    sin.sin_port = htons(port[i]);
312    
313                    // Reuse address and port
314                    flags[i] = 1;
315                    if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags[i], sizeof(flags[i])) < 0)
316                    {
317                            log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);
318                    }
319                    if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags[i], sizeof(flags[i])) < 0)
320                    {
321                            log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);
322                    }
323    
324                    if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0)
325                    {
326                            log_error("Bind address %s:%u error (%d)\n",
327                                              inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);
328                            return -1;
329                    }
330    
331                    if (listen(socket_server[i], 10) < 0)
332                    {
333                            log_error("Telnet socket listen error (%d)\n", errno);
334                            return -1;
335                    }
336    
337                    log_common("Listening at %s:%u\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
338    
339                    ev.events = EPOLLIN;
340                    ev.data.fd = socket_server[i];
341                    if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server[i], &ev) == -1)
342                    {
343                            log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno);
344                            if (close(epollfd) < 0)
345                            {
346                                    log_error("close(epoll) error (%d)\n");
347                            }
348                            return -1;
349                    }
350    
351                    flags[i] = fcntl(socket_server[i], F_GETFL, 0);
352                    fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);
353          }          }
354    
355          flags = fcntl(socket_server, F_GETFL, 0);          // Startup complete
356          fcntl(socket_server, F_SETFL, flags | O_NONBLOCK);          sd_notifyf(0, "READY=1\n"
357                                      "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"
358                                      "MAINPID=%d",
359                               hostaddr, port[0], hostaddr, port[1], getpid());
360    
361          while (!SYS_server_exit || SYS_child_process_count > 0)          while (!SYS_server_exit || SYS_child_process_count > 0)
362          {          {
363                  sigprocmask(SIG_BLOCK, &nsigset, &osigset);                  if (SYS_server_exit && !sd_notify_stopping)
364                    {
365                            sd_notify(0, "STOPPING=1");
366                            sd_notify_stopping = 1;
367                    }
368    
369                  while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)                  while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)
370                  {                  {
371                            SYS_child_exit = 0;
372    
373                          siginfo.si_pid = 0;                          siginfo.si_pid = 0;
374                          ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG);                          ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG);
375                          if (ret == 0 && siginfo.si_pid > 0)                          if (ret == 0 && siginfo.si_pid > 0)
376                          {                          {
377                                    SYS_child_exit = 1; // Retry waitid
378    
379                                  SYS_child_process_count--;                                  SYS_child_process_count--;
380                                  log_std("Child process (%d) exited\n", siginfo.si_pid);                                  log_common("Child process (%d) exited\n", siginfo.si_pid);
381    
382                                    if (siginfo.si_pid != section_list_loader_pid)
383                                    {
384                                            i = 0;
385                                            for (; i < BBS_max_client; i++)
386                                            {
387                                                    if (process_sockaddr_pool[i].pid == siginfo.si_pid)
388                                                    {
389                                                            process_sockaddr_pool[i].pid = 0;
390                                                            break;
391                                                    }
392                                            }
393                                            if (i >= BBS_max_client)
394                                            {
395                                                    log_error("Child process (%d) not found in process sockaddr pool\n", siginfo.si_pid);
396                                            }
397                                    }
398                          }                          }
399                          else if (ret == 0)                          else if (ret == 0)
400                          {                          {
                                 SYS_child_exit = 0;  
401                                  break;                                  break;
402                          }                          }
403                          else if (ret < 0)                          else if (ret < 0)
# Line 142  int net_server(const char *hostaddr, in_ Line 409  int net_server(const char *hostaddr, in_
409    
410                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)                  if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
411                  {                  {
412                          log_std("Notify %d child process to exit\n", SYS_child_process_count);                          log_common("Notify %d child process to exit\n", SYS_child_process_count);
413                          if (kill(0, SIGTERM) < 0)                          if (kill(0, SIGTERM) < 0)
414                          {                          {
415                                  log_error("Send SIGTERM signal failed (%d)\n", errno);                                  log_error("Send SIGTERM signal failed (%d)\n", errno);
416                          }                          }
417    
418                            sd_notifyf(0, "STATUS=Waiting for %d child process to exit", SYS_child_process_count);
419                  }                  }
420    
421                  if (SYS_menu_reload && !SYS_server_exit)                  if (SYS_conf_reload && !SYS_server_exit)
422                  {                  {
423                          if (reload_menu(&bbs_menu) < 0)                          SYS_conf_reload = 0;
424                            sd_notify(0, "RELOADING=1");
425    
426                            // Reload configuration
427                            if (load_conf(CONF_BBSD) < 0)
428                            {
429                                    log_error("Reload conf failed\n");
430                            }
431    
432                            p_bbs_menu_new = calloc(1, sizeof(MENU_SET));
433                            if (p_bbs_menu_new == NULL)
434                          {                          {
435                                    log_error("OOM: calloc(MENU_SET)\n");
436                            }
437                            else if (load_menu(p_bbs_menu_new, CONF_MENU) < 0)
438                            {
439                                    unload_menu(p_bbs_menu_new);
440                                    free(p_bbs_menu_new);
441                                    p_bbs_menu_new = NULL;
442    
443                                  log_error("Reload menu failed\n");                                  log_error("Reload menu failed\n");
444                          }                          }
445                          else                          else
446                          {                          {
447                                  log_std("Reload menu successfully\n");                                  unload_menu(p_bbs_menu);
448                                    free(p_bbs_menu);
449    
450                                    p_bbs_menu = p_bbs_menu_new;
451                                    p_bbs_menu_new = NULL;
452    
453                                    log_common("Reload menu successfully\n");
454                          }                          }
455                          SYS_menu_reload = 0;  
456                            sd_notify(0, "READY=1");
457                  }                  }
458    
459                  sigprocmask(SIG_SETMASK, &osigset, NULL);                  if (SYS_data_file_reload && !SYS_server_exit)
460                    {
461                            SYS_data_file_reload = 0;
462                            sd_notify(0, "RELOADING=1");
463    
464                            for (int i = 0; i < data_files_load_startup_count; i++)
465                            {
466                                    if (load_file(data_files_load_startup[i]) < 0)
467                                    {
468                                            log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]);
469                                    }
470                            }
471    
472                            log_common("Reload data files successfully\n");
473                            sd_notify(0, "READY=1");
474                    }
475    
476                    if (SYS_section_list_reload && !SYS_server_exit)
477                    {
478                            SYS_section_list_reload = 0;
479    
480                            if (section_list_loader_reload() < 0)
481                            {
482                                    log_error("ksection_list_loader_reload() failed\n");
483                            }
484                    }
485    
486                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
487    
# Line 184  int net_server(const char *hostaddr, in_ Line 503  int net_server(const char *hostaddr, in_
503    
504                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
505                  {                  {
506                          if (events[i].data.fd == socket_server)                          if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1])
507                          {                          {
508                                    SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0);
509    
510                                  while (!SYS_server_exit) // Accept all incoming connections until error                                  while (!SYS_server_exit) // Accept all incoming connections until error
511                                  {                                  {
512                                          socket_client = accept(socket_server, (struct sockaddr *)&sin, &namelen);                                          addrlen = sizeof(sin);
513                                            socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, &addrlen);
514                                          if (socket_client < 0)                                          if (socket_client < 0)
515                                          {                                          {
516                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 211  int net_server(const char *hostaddr, in_ Line 533  int net_server(const char *hostaddr, in_
533    
534                                          port_client = ntohs(sin.sin_port);                                          port_client = ntohs(sin.sin_port);
535    
536                                          log_std("Accept connection from %s:%d\n", hostaddr_client, port_client);                                          log_common("Accept %sconnection from %s:%d\n", (SSH_v2 ? "" : "SSH2 "), hostaddr_client, port_client);
537    
538                                            if (SYS_child_process_count - 1 < BBS_max_client)
539                                            {
540                                                    j = 0;
541                                                    for (i = 0; i < BBS_max_client; i++)
542                                                    {
543                                                            if (process_sockaddr_pool[i].pid != 0 && process_sockaddr_pool[i].s_addr == sin.sin_addr.s_addr)
544                                                            {
545                                                                    j++;
546                                                                    if (j >= BBS_max_client_per_ip)
547                                                                    {
548                                                                            log_common("Too many client connections (%d) from %s\n", j, hostaddr_client);
549                                                                            break;
550                                                                    }
551                                                            }
552                                                    }
553    
554                                          if (fork_server() < 0)                                                  if (j < BBS_max_client_per_ip)
555                                                    {
556                                                            if ((pid = fork_server()) < 0)
557                                                            {
558                                                                    log_error("fork_server() error\n");
559                                                            }
560                                                            else if (pid > 0)
561                                                            {
562                                                                    i = 0;
563                                                                    for (; i < BBS_max_client; i++)
564                                                                    {
565                                                                            if (process_sockaddr_pool[i].pid == 0)
566                                                                            {
567                                                                                    break;
568                                                                            }
569                                                                    }
570    
571                                                                    if (i >= BBS_max_client)
572                                                                    {
573                                                                            log_error("Process sockaddr pool depleted\n");
574                                                                    }
575                                                                    else
576                                                                    {
577                                                                            process_sockaddr_pool[i].pid = pid;
578                                                                            process_sockaddr_pool[i].s_addr = sin.sin_addr.s_addr;
579                                                                    }
580                                                            }
581                                                    }
582                                            }
583                                            else
584                                          {                                          {
585                                                  log_error("fork_server() error\n");                                                  log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1);
586                                          }                                          }
587    
588                                          if (close(socket_client) == -1)                                          if (close(socket_client) == -1)
# Line 227  int net_server(const char *hostaddr, in_ Line 594  int net_server(const char *hostaddr, in_
594                  }                  }
595          }          }
596    
597          fcntl(socket_server, F_SETFL, flags);          if (close(epollfd) < 0)
598            {
599                    log_error("close(epoll) error (%d)\n");
600            }
601    
602          if (close(socket_server) == -1)          for (i = 0; i < 2; i++)
603          {          {
604                  log_error("Close server socket failed\n");                  fcntl(socket_server[i], F_SETFL, flags[i]);
605    
606                    if (close(socket_server[i]) == -1)
607                    {
608                            log_error("Close server socket failed\n");
609                    }
610          }          }
611    
612            ssh_bind_free(sshbind);
613            ssh_finalize();
614    
615          return 0;          return 0;
616  }  }


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

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