/[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.4 by sysadm, Tue Oct 19 17:11:39 2004 UTC Revision 1.83 by sysadm, Sun Nov 16 00:19:42 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                            net_server.c  -  description  /*
3                               -------------------   * net_server
4      begin                : Mon Oct 11 2004   *   - network server with SSH support
5      copyright            : (C) 2004 by Leaflet   *
6      email                : leaflet@leafok.com   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7   ***************************************************************************/   */
8    
9  /***************************************************************************  #ifdef HAVE_CONFIG_H
10   *                                                                         *  #include "config.h"
11   *   This program is free software; you can redistribute it and/or modify  *  #endif
12   *   it under the terms of the GNU General Public License as published by  *  
13   *   the Free Software Foundation; either version 2 of the License, or     *  #include "bbs.h"
14   *   (at your option) any later version.                                   *  #include "bbs_main.h"
15   *                                                                         *  #include "bwf.h"
  ***************************************************************************/  
   
16  #include "common.h"  #include "common.h"
17    #include "database.h"
18    #include "file_loader.h"
19    #include "hash_dict.h"
20    #include "io.h"
21    #include "init.h"
22    #include "log.h"
23    #include "login.h"
24    #include "menu.h"
25    #include "net_server.h"
26    #include "section_list.h"
27    #include "section_list_loader.h"
28    #include <errno.h>
29    #include <fcntl.h>
30    #include <pty.h>
31    #include <signal.h>
32    #include <stdlib.h>
33    #include <string.h>
34    #include <unistd.h>
35    #include <utmp.h>
36    #include <arpa/inet.h>
37    #include <libssh/callbacks.h>
38    #include <libssh/libssh.h>
39    #include <libssh/server.h>
40    #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>
47    
48    enum _net_server_constant_t
49    {
50            WAIT_CHILD_PROCESS_EXIT_TIMEOUT = 5, // second
51            WAIT_CHILD_PROCESS_KILL_TIMEOUT = 1, // second
52    
53            SSH_AUTH_MAX_DURATION = 60 * 1000, // milliseconds
54    };
55    
56    static const char SFTP_SERVER_PATH[] = "/usr/lib/sftp-server";
57    
58    /* A userdata struct for session. */
59    struct session_data_struct
60    {
61            int tries;
62            int error;
63    };
64    
65    /* A userdata struct for channel. */
66    struct channel_data_struct
67    {
68            /* pid of the child process the channel will spawn. */
69            pid_t pid;
70            /* For PTY allocation */
71            socket_t pty_master;
72            socket_t pty_slave;
73            /* For communication with the child process. */
74            socket_t child_stdin;
75            socket_t child_stdout;
76            /* Only used for subsystem and exec requests. */
77            socket_t child_stderr;
78            /* Event which is used to poll the above descriptors. */
79            ssh_event event;
80            /* Terminal size struct. */
81            struct winsize *winsize;
82    };
83    
84  int  static int auth_password(ssh_session session, const char *user,
85  net_server (const char *hostaddr, unsigned int port)                                                   const char *password, void *userdata)
86  {  {
87    int sock_server, sock_client, namelen, seq, netint;          struct session_data_struct *sdata = (struct session_data_struct *)userdata;
88    struct sockaddr_in sin;          int ret;
89    char temp[256];  
90            if (strcmp(user, "guest") == 0)
91    sock_server = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);          {
92                    ret = load_guest_info();
93    if (sock_server < 0)          }
94      {          else
95        log_error ("Create socket failed\n");          {
96        exit (1);                  ret = check_user(user, password);
97      }          }
98    
99    sin.sin_family = AF_INET;          if (ret == 0)
100    sin.sin_addr.s_addr =          {
101      (strlen (hostaddr) > 0 ? inet_addr (hostaddr) : INADDR_ANY);                  return SSH_AUTH_SUCCESS;
102    sin.sin_port = htons (port);          }
103    
104    if (bind (sock_server, (struct sockaddr *)&sin, sizeof(sin)) < 0)          if ((++(sdata->tries)) >= BBS_login_retry_times)
105      {          {
106        log_error ("Bind address failed\n");                  sdata->error = 1;
107        exit (2);          }
108      }  
109            return SSH_AUTH_DENIED;
110    if (listen (sock_server, 10) < 0)  }
111      {  
112        log_error ("Socket listen failed\n");  static int pty_request(ssh_session session, ssh_channel channel, const char *term,
113        exit (3);                                             int cols, int rows, int px, int py, void *userdata)
114      }  {
115            struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
116    socket_server = sock_server;          int rc;
117    strcpy(hostaddr_server,inet_ntoa(sin.sin_addr));  
118    port_server = ntohs(sin.sin_port);          (void)session;
119              (void)channel;
120    sprintf(temp, "Listening at %s:%d\n",          (void)term;
121      hostaddr_server, port_server);  
122    log_std (temp);          cdata->winsize->ws_row = (unsigned short int)rows;
123            cdata->winsize->ws_col = (unsigned short int)cols;
124    namelen = sizeof (sin);          cdata->winsize->ws_xpixel = (unsigned short int)px;
125    while(1)          cdata->winsize->ws_ypixel = (unsigned short int)py;
126      {  
127        if ((sock_client = accept (sock_server, (struct sockaddr *) &sin, &namelen)) < 0)          rc = openpty(&cdata->pty_master, &cdata->pty_slave, NULL, NULL, cdata->winsize);
128          {          if (rc != 0)
129            log_error ("Accept connection error\n");          {
130          }                  log_error("Failed to open pty\n");
131        else                  return SSH_ERROR;
132          {          }
133            sprintf(temp, "Accept connection from %s:%d\n",  
134              inet_ntoa(sin.sin_addr),ntohs(sin.sin_port));          return SSH_OK;
135            log_std (temp);  }
136          }  
137          static int pty_resize(ssh_session session, ssh_channel channel, int cols, int rows,
138        if (fork_server(sock_server, sock_client, &sin)<0)                                            int py, int px, void *userdata)
139          {  {
140            log_error ("Fork error\n");          struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
141          }  
142                  (void)session;
143        if (close(sock_client) == -1)          (void)channel;
144          {  
145            log_error("Close client socket failed\n");          cdata->winsize->ws_row = (unsigned short int)rows;
146          }          cdata->winsize->ws_col = (unsigned short int)cols;
147      }          cdata->winsize->ws_xpixel = (unsigned short int)px;
148            cdata->winsize->ws_ypixel = (unsigned short int)py;
149    
150            if (cdata->pty_master != -1)
151            {
152                    return ioctl(cdata->pty_master, TIOCSWINSZ, cdata->winsize);
153            }
154    
155            return SSH_ERROR;
156    }
157    
158    static int exec_pty(const char *mode, const char *command, struct channel_data_struct *cdata)
159    {
160            (void)cdata;
161    
162            if (command != NULL)
163            {
164                    log_error("Forbid exec /bin/sh %s %s)\n", mode, command);
165            }
166    
167            return SSH_OK;
168    }
169    
170    static int exec_nopty(const char *command, struct channel_data_struct *cdata)
171    {
172            (void)cdata;
173    
174            if (command != NULL)
175            {
176                    log_error("Forbid exec /bin/sh -c %s)\n", command);
177            }
178    
179            return SSH_OK;
180    }
181    
182    static int exec_request(ssh_session session, ssh_channel channel, const char *command, void *userdata)
183    {
184            struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
185    
186            (void)session;
187            (void)channel;
188    
189            if (cdata->pid > 0)
190            {
191                    return SSH_ERROR;
192            }
193    
194            if (cdata->pty_master != -1 && cdata->pty_slave != -1)
195            {
196                    return exec_pty("-c", command, cdata);
197            }
198            return exec_nopty(command, cdata);
199    }
200    
201    static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
202    {
203            struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
204    
205            (void)session;
206            (void)channel;
207    
208            if (cdata->pid > 0)
209            {
210                    return SSH_ERROR;
211            }
212    
213            if (cdata->pty_master != -1 && cdata->pty_slave != -1)
214            {
215                    return exec_pty("-l", NULL, cdata);
216            }
217            /* Client requested a shell without a pty, let's pretend we allow that */
218            return SSH_OK;
219    }
220    
221    static int subsystem_request(ssh_session session, ssh_channel channel, const char *subsystem, void *userdata)
222    {
223            (void)session;
224            (void)channel;
225    
226            log_error("subsystem_request(subsystem=%s)\n", subsystem);
227    
228            /* subsystem requests behave similarly to exec requests. */
229            if (strcmp(subsystem, "sftp") == 0)
230            {
231                    return exec_request(session, channel, SFTP_SERVER_PATH, userdata);
232            }
233            return SSH_ERROR;
234    }
235    
236    static ssh_channel channel_open(ssh_session session, void *userdata)
237    {
238            (void)userdata;
239    
240            if (SSH_channel != NULL)
241            {
242                    return NULL;
243            }
244    
245            SSH_channel = ssh_channel_new(session);
246    
247            return SSH_channel;
248    }
249    
250    static int fork_server(void)
251    {
252            ssh_event event;
253            long int ssh_timeout = 0;
254            int pid;
255            int i;
256            int ret;
257    
258            /* Structure for storing the pty size. */
259            struct winsize wsize = {
260                    .ws_row = 0,
261                    .ws_col = 0,
262                    .ws_xpixel = 0,
263                    .ws_ypixel = 0};
264    
265            /* Our struct holding information about the channel. */
266            struct channel_data_struct cdata = {
267                    .pid = 0,
268                    .pty_master = -1,
269                    .pty_slave = -1,
270                    .child_stdin = -1,
271                    .child_stdout = -1,
272                    .child_stderr = -1,
273                    .event = NULL,
274                    .winsize = &wsize};
275    
276            struct session_data_struct cb_data = {
277                    .tries = 0,
278                    .error = 0,
279            };
280    
281            struct ssh_channel_callbacks_struct channel_cb = {
282                    .userdata = &cdata,
283                    .channel_pty_request_function = pty_request,
284                    .channel_pty_window_change_function = pty_resize,
285                    .channel_shell_request_function = shell_request,
286                    .channel_exec_request_function = exec_request,
287                    .channel_subsystem_request_function = subsystem_request};
288    
289            struct ssh_server_callbacks_struct server_cb = {
290                    .userdata = &cb_data,
291                    .auth_password_function = auth_password,
292                    .channel_open_request_session_function = channel_open,
293            };
294    
295            pid = fork();
296    
297            if (pid > 0) // Parent process
298            {
299                    SYS_child_process_count++;
300                    log_common("Child process (%d) start\n", pid);
301                    return pid;
302            }
303            else if (pid < 0) // Error
304            {
305                    log_error("fork() error (%d)\n", errno);
306                    return -1;
307            }
308    
309            // Child process
310    
311            if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1)
312            {
313                    log_error("Close server socket failed\n");
314            }
315    
316            SSH_session = ssh_new();
317    
318            if (SSH_v2)
319            {
320                    if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK)
321                    {
322                            log_error("ssh_bind_accept_fd() error: %s\n", ssh_get_error(SSH_session));
323                            goto cleanup;
324                    }
325    
326                    ssh_bind_free(sshbind);
327    
328                    ssh_timeout = 60; // second
329                    if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
330                    {
331                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
332                            goto cleanup;
333                    }
334    
335                    ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);
336    
337                    ssh_callbacks_init(&server_cb);
338                    ssh_callbacks_init(&channel_cb);
339    
340                    ssh_set_server_callbacks(SSH_session, &server_cb);
341    
342                    if (ssh_handle_key_exchange(SSH_session))
343                    {
344                            log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));
345                            goto cleanup;
346                    }
347    
348                    event = ssh_event_new();
349                    ssh_event_add_session(event, SSH_session);
350    
351                    for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i += 100)
352                    {
353                            ret = ssh_event_dopoll(event, 100); // 0.1 second
354                            if (ret == SSH_ERROR)
355                            {
356    #ifdef _DEBUG
357                                    log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));
358    #endif
359                                    goto cleanup;
360                            }
361                    }
362    
363                    if (cb_data.error)
364                    {
365                            log_error("SSH auth error, tried %d times\n", cb_data.tries);
366                            goto cleanup;
367                    }
368    
369                    ssh_set_channel_callbacks(SSH_channel, &channel_cb);
370    
371                    do
372                    {
373                            ret = ssh_event_dopoll(event, 100); // 0.1 second
374                            if (ret == SSH_ERROR)
375                            {
376                                    ssh_channel_close(SSH_channel);
377                            }
378    
379                            if (ret == SSH_AGAIN) // loop until SSH connection is fully established
380                            {
381                                    /* Executed only once, once the child process starts. */
382                                    cdata.event = event;
383                                    break;
384                            }
385                    } while (ssh_channel_is_open(SSH_channel));
386    
387                    ssh_timeout = 0;
388                    if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
389                    {
390                            log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));
391                            goto cleanup;
392                    }
393            }
394    
395            // Redirect Input
396            if (dup2(socket_client, STDIN_FILENO) == -1)
397            {
398                    log_error("Redirect stdin to client socket failed\n");
399                    goto cleanup;
400            }
401    
402            // Redirect Output
403            if (dup2(socket_client, STDOUT_FILENO) == -1)
404            {
405                    log_error("Redirect stdout to client socket failed\n");
406                    goto cleanup;
407            }
408    
409            SYS_child_process_count = 0;
410    
411            bbs_main();
412    
413    cleanup:
414            // Child process exit
415            SYS_server_exit = 1;
416    
417            if (SSH_v2)
418            {
419                    close(cdata.pty_master);
420                    close(cdata.child_stdin);
421                    close(cdata.child_stdout);
422                    close(cdata.child_stderr);
423    
424                    ssh_channel_free(SSH_channel);
425                    ssh_disconnect(SSH_session);
426            }
427            else if (close(socket_client) == -1)
428            {
429                    log_error("Close client socket failed\n");
430            }
431    
432            ssh_free(SSH_session);
433            ssh_finalize();
434    
435            // Close Input and Output for client
436            close(STDIN_FILENO);
437            close(STDOUT_FILENO);
438    
439            log_common("Process exit normally\n");
440            log_end();
441    
442            _exit(0);
443    
444            return 0;
445    }
446    
447    int net_server(const char *hostaddr, in_port_t port[])
448    {
449            HASH_DICT *hash_dict_pid_sockaddr = NULL;
450            HASH_DICT *hash_dict_sockaddr_count = NULL;
451    
452            unsigned int addrlen;
453            int ret;
454            int flags[2];
455            struct sockaddr_in sin;
456            struct epoll_event ev, events[MAX_EVENTS];
457            int nfds, epollfd;
458            siginfo_t siginfo;
459            int notify_child_exit = 0;
460            time_t tm_notify_child_exit = time(NULL);
461            int sd_notify_stopping = 0;
462            MENU_SET bbs_menu_new;
463            MENU_SET top10_menu_new;
464            int i, j;
465            pid_t pid;
466            int ssh_log_level = SSH_LOG_NOLOG;
467    
468            ssh_init();
469    
470            sshbind = ssh_bind_new();
471    
472            if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
473                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
474                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_KEYFILE) < 0 ||
475                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "ssh-rsa,rsa-sha2-512,rsa-sha2-256") < 0 ||
476                    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &ssh_log_level) < 0)
477            {
478                    log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));
479                    ssh_bind_free(sshbind);
480                    return -1;
481            }
482    
483            epollfd = epoll_create1(0);
484            if (epollfd < 0)
485            {
486                    log_error("epoll_create1() error (%d)\n", errno);
487                    return -1;
488            }
489    
490            // Server socket
491            for (i = 0; i < 2; i++)
492            {
493                    socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
494    
495                    if (socket_server[i] < 0)
496                    {
497                            log_error("Create socket_server error (%d)\n", errno);
498                            return -1;
499                    }
500    
501                    sin.sin_family = AF_INET;
502                    sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY);
503                    sin.sin_port = htons(port[i]);
504    
505                    // Reuse address and port
506                    flags[i] = 1;
507                    if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags[i], sizeof(flags[i])) < 0)
508                    {
509                            log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);
510                    }
511                    if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags[i], sizeof(flags[i])) < 0)
512                    {
513                            log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);
514                    }
515    
516                    if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0)
517                    {
518                            log_error("Bind address %s:%u error (%d)\n",
519                                              inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);
520                            return -1;
521                    }
522    
523                    if (listen(socket_server[i], 10) < 0)
524                    {
525                            log_error("Telnet socket listen error (%d)\n", errno);
526                            return -1;
527                    }
528    
529                    log_common("Listening at %s:%u\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
530    
531                    ev.events = EPOLLIN;
532                    ev.data.fd = socket_server[i];
533                    if (epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_server[i], &ev) == -1)
534                    {
535                            log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno);
536                            if (close(epollfd) < 0)
537                            {
538                                    log_error("close(epoll) error (%d)\n");
539                            }
540                            return -1;
541                    }
542    
543                    flags[i] = fcntl(socket_server[i], F_GETFL, 0);
544                    fcntl(socket_server[i], F_SETFL, flags[i] | O_NONBLOCK);
545            }
546    
547            hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);
548            if (hash_dict_pid_sockaddr == NULL)
549            {
550                    log_error("hash_dict_create(hash_dict_pid_sockaddr) error\n");
551                    return -1;
552            }
553            hash_dict_sockaddr_count = hash_dict_create(MAX_CLIENT_LIMIT);
554            if (hash_dict_sockaddr_count == NULL)
555            {
556                    log_error("hash_dict_create(hash_dict_sockaddr_count) error\n");
557                    return -1;
558            }
559    
560            // Startup complete
561            sd_notifyf(0, "READY=1\n"
562                                      "STATUS=Listening at %s:%d (Telnet) and %s:%d (SSH2)\n"
563                                      "MAINPID=%d",
564                               hostaddr, port[0], hostaddr, port[1], getpid());
565    
566            while (!SYS_server_exit || SYS_child_process_count > 0)
567            {
568                    if (SYS_server_exit && !sd_notify_stopping)
569                    {
570                            sd_notify(0, "STOPPING=1");
571                            sd_notify_stopping = 1;
572                    }
573    
574                    while ((SYS_child_exit || SYS_server_exit) && SYS_child_process_count > 0)
575                    {
576                            SYS_child_exit = 0;
577    
578                            siginfo.si_pid = 0;
579                            ret = waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG);
580                            if (ret == 0 && siginfo.si_pid > 0)
581                            {
582                                    SYS_child_exit = 1; // Retry waitid
583    
584                                    SYS_child_process_count--;
585                                    log_common("Child process (%d) exited\n", siginfo.si_pid);
586    
587                                    if (siginfo.si_pid != section_list_loader_pid)
588                                    {
589                                            j = 0;
590                                            ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid, (int64_t *)&j);
591                                            if (ret < 0)
592                                            {
593                                                    log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);
594                                            }
595                                            else
596                                            {
597                                                    sin.sin_addr.s_addr = (uint32_t)j;
598                                                    j = 0;
599                                                    ret = hash_dict_get(hash_dict_sockaddr_count, sin.sin_addr.s_addr, (int64_t *)&j);
600                                                    if (ret < 0)
601                                                    {
602                                                            log_error("hash_dict_get(hash_dict_sockaddr_count, %d) error\n", sin.sin_addr.s_addr);
603                                                    }
604                                                    else if (ret == 0)
605                                                    {
606                                                            log_error("hash_dict_get(hash_dict_sockaddr_count, %d) not found\n", sin.sin_addr.s_addr);
607                                                            j = 1;
608                                                    }
609    
610                                                    j--;
611                                                    ret = hash_dict_set(hash_dict_sockaddr_count, sin.sin_addr.s_addr, j);
612                                                    if (ret < 0)
613                                                    {
614                                                            log_error("hash_dict_set(hash_dict_sockaddr_count, %d, %d) error\n", sin.sin_addr.s_addr, j);
615                                                    }
616    
617                                                    ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)siginfo.si_pid);
618                                                    if (ret < 0)
619                                                    {
620                                                            log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", siginfo.si_pid);
621                                                    }
622                                            }
623                                    }
624                            }
625                            else if (ret == 0)
626                            {
627                                    break;
628                            }
629                            else if (ret < 0)
630                            {
631                                    log_error("Error in waitid: %d\n", errno);
632                                    break;
633                            }
634                    }
635    
636                    if (SYS_server_exit && !SYS_child_exit && SYS_child_process_count > 0)
637                    {
638                            if (notify_child_exit == 0)
639                            {
640                                    sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
641                                    log_common("Notify %d child process to exit\n", SYS_child_process_count);
642    
643                                    if (kill(-getpid(), SIGTERM) < 0)
644                                    {
645                                            log_error("Send SIGTERM signal failed (%d)\n", errno);
646                                    }
647    
648                                    notify_child_exit = 1;
649                                    tm_notify_child_exit = time(NULL);
650                            }
651                            else if (notify_child_exit == 1 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_EXIT_TIMEOUT)
652                            {
653                                    sd_notifyf(0, "STATUS=Kill %d child process", SYS_child_process_count);
654    
655                                    if (kill(-getpid(), SIGKILL) < 0)
656                                    {
657                                            log_error("Send SIGKILL signal failed (%d)\n", errno);
658                                    }
659    
660                                    notify_child_exit = 2;
661                                    tm_notify_child_exit = time(NULL);
662                            }
663                            else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT)
664                            {
665                                    log_error("Main process prepare to exit without waiting for %d child process any longer\n", SYS_child_process_count);
666                                    SYS_child_process_count = 0;
667                            }
668                    }
669    
670                    if (SYS_conf_reload && !SYS_server_exit)
671                    {
672                            SYS_conf_reload = 0;
673                            sd_notify(0, "RELOADING=1");
674    
675                            // Reload configuration
676                            if (load_conf(CONF_BBSD) < 0)
677                            {
678                                    log_error("Reload conf failed\n");
679                            }
680    
681                            // Reload BWF config
682                            if (bwf_load(CONF_BWF) < 0)
683                            {
684                                    log_error("Reload BWF conf failed\n");
685                            }
686    
687                            if (load_menu(&bbs_menu_new, CONF_MENU) < 0)
688                            {
689                                    unload_menu(&bbs_menu_new);
690                                    log_error("Reload bbs menu failed\n");
691                            }
692                            else
693                            {
694                                    unload_menu(&bbs_menu);
695                                    memcpy(&bbs_menu, &bbs_menu_new, sizeof(bbs_menu_new));
696                                    log_common("Reload bbs menu successfully\n");
697                            }
698    
699                            if (load_menu(&top10_menu_new, CONF_TOP10_MENU) < 0)
700                            {
701                                    unload_menu(&top10_menu_new);
702                                    log_error("Reload top10 menu failed\n");
703                            }
704                            else
705                            {
706                                    unload_menu(&top10_menu);
707                                    top10_menu_new.allow_exit = 1;
708                                    memcpy(&top10_menu, &top10_menu_new, sizeof(top10_menu_new));
709                                    log_common("Reload top10 menu successfully\n");
710                            }
711    
712                            for (int i = 0; i < data_files_load_startup_count; i++)
713                            {
714                                    if (load_file(data_files_load_startup[i]) < 0)
715                                    {
716                                            log_error("load_file(%s) error\n", data_files_load_startup[i]);
717                                    }
718                            }
719                            log_common("Reload data files successfully\n");
720    
721                            // Load section config and gen_ex
722                            if (load_section_config_from_db(1) < 0)
723                            {
724                                    log_error("load_section_config_from_db(1) error\n");
725                            }
726                            else
727                            {
728                                    log_common("Reload section config and gen_ex successfully\n");
729                            }
730    
731                            sd_notify(0, "READY=1");
732                    }
733    
734                    nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second
735    
736                    if (nfds < 0)
737                    {
738                            if (errno != EINTR)
739                            {
740                                    log_error("epoll_wait() error (%d)\n", errno);
741                                    break;
742                            }
743                            continue;
744                    }
745    
746                    // Stop accept new connection on exit
747                    if (SYS_server_exit)
748                    {
749                            continue;
750                    }
751    
752                    for (int i = 0; i < nfds; i++)
753                    {
754                            if (events[i].data.fd == socket_server[0] || events[i].data.fd == socket_server[1])
755                            {
756                                    SSH_v2 = (events[i].data.fd == socket_server[1] ? 1 : 0);
757    
758                                    while (!SYS_server_exit) // Accept all incoming connections until error
759                                    {
760                                            addrlen = sizeof(sin);
761                                            socket_client = accept(socket_server[SSH_v2], (struct sockaddr *)&sin, &addrlen);
762                                            if (socket_client < 0)
763                                            {
764                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
765                                                    {
766                                                            break;
767                                                    }
768                                                    else if (errno == EINTR)
769                                                    {
770                                                            continue;
771                                                    }
772                                                    else
773                                                    {
774                                                            log_error("accept(socket_server) error (%d)\n", errno);
775                                                            break;
776                                                    }
777                                            }
778    
779                                            strncpy(hostaddr_client, inet_ntoa(sin.sin_addr), sizeof(hostaddr_client) - 1);
780                                            hostaddr_client[sizeof(hostaddr_client) - 1] = '\0';
781    
782                                            port_client = ntohs(sin.sin_port);
783    
784                                            log_common("Accept %s connection from %s:%d\n", (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client);
785    
786                                            if (SYS_child_process_count - 1 < BBS_max_client)
787                                            {
788                                                    j = 0;
789                                                    ret = hash_dict_get(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, (int64_t *)&j);
790                                                    if (ret < 0)
791                                                    {
792                                                            log_error("hash_dict_get(hash_dict_sockaddr_count, %s) error\n", hostaddr_client);
793                                                    }
794    
795                                                    if (j < BBS_max_client_per_ip)
796                                                    {
797                                                            if ((pid = fork_server()) < 0)
798                                                            {
799                                                                    log_error("fork_server() error\n");
800                                                            }
801                                                            else if (pid > 0)
802                                                            {
803                                                                    ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr);
804                                                                    if (ret < 0)
805                                                                    {
806                                                                            log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client);
807                                                                    }
808    
809                                                                    ret = hash_dict_set(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, j + 1);
810                                                                    if (ret < 0)
811                                                                    {
812                                                                            log_error("hash_dict_set(hash_dict_sockaddr_count, %s, %d) error\n", hostaddr_client, j + 1);
813                                                                    }
814                                                            }
815                                                    }
816                                                    else
817                                                    {
818                                                            log_error("Rejected client connection from %s over limit per IP (%d)\n", hostaddr_client, BBS_max_client_per_ip);
819                                                    }
820                                            }
821                                            else
822                                            {
823                                                    log_error("Rejected client connection over limit (%d)\n", SYS_child_process_count - 1);
824                                            }
825    
826                                            if (close(socket_client) == -1)
827                                            {
828                                                    log_error("close(socket_lient) error (%d)\n", errno);
829                                            }
830                                    }
831                            }
832                    }
833            }
834    
835            if (close(epollfd) < 0)
836            {
837                    log_error("close(epoll) error (%d)\n");
838            }
839    
840            for (i = 0; i < 2; i++)
841            {
842                    fcntl(socket_server[i], F_SETFL, flags[i]);
843    
844                    if (close(socket_server[i]) == -1)
845                    {
846                            log_error("Close server socket failed\n");
847                    }
848            }
849    
850            hash_dict_destroy(hash_dict_pid_sockaddr);
851            hash_dict_destroy(hash_dict_sockaddr_count);
852    
853            ssh_bind_free(sshbind);
854            ssh_finalize();
855    
856    return 0;          return 0;
857  }  }


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

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