/[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.107 by sysadm, Wed Dec 17 04:22:40 2025 UTC Revision 1.113 by sysadm, Thu Dec 25 05:20:09 2025 UTC
# Line 153  static int pty_request(ssh_session sessi Line 153  static int pty_request(ssh_session sessi
153          rc = openpty(&cdata->pty_master, &cdata->pty_slave, NULL, NULL, cdata->winsize);          rc = openpty(&cdata->pty_master, &cdata->pty_slave, NULL, NULL, cdata->winsize);
154          if (rc != 0)          if (rc != 0)
155          {          {
156                  log_error("Failed to open pty\n");                  log_error("Failed to open pty");
157                  return SSH_ERROR;                  return SSH_ERROR;
158          }          }
159    
# Line 187  static int exec_pty(const char *mode, co Line 187  static int exec_pty(const char *mode, co
187    
188          if (command != NULL)          if (command != NULL)
189          {          {
190                  log_error("Forbid exec /bin/sh %s %s)\n", mode, command);                  log_error("Forbid exec /bin/sh %s %s)", mode, command);
191          }          }
192    
193          return SSH_OK;          return SSH_OK;
# Line 199  static int exec_nopty(const char *comman Line 199  static int exec_nopty(const char *comman
199    
200          if (command != NULL)          if (command != NULL)
201          {          {
202                  log_error("Forbid exec /bin/sh -c %s)\n", command);                  log_error("Forbid exec /bin/sh -c %s)", command);
203          }          }
204    
205          return SSH_OK;          return SSH_OK;
# Line 249  static int subsystem_request(ssh_session Line 249  static int subsystem_request(ssh_session
249          (void)session;          (void)session;
250          (void)channel;          (void)channel;
251    
252          log_error("subsystem_request(subsystem=%s)\n", subsystem);          log_error("subsystem_request(subsystem=%s)", subsystem);
253    
254          /* subsystem requests behave similarly to exec requests. */          /* subsystem requests behave similarly to exec requests. */
255          if (strcmp(subsystem, "sftp") == 0)          if (strcmp(subsystem, "sftp") == 0)
# Line 323  static int fork_server(void) Line 323  static int fork_server(void)
323          if (pid > 0) // Parent process          if (pid > 0) // Parent process
324          {          {
325                  SYS_child_process_count++;                  SYS_child_process_count++;
326                  log_common("Child process (%d) start\n", pid);                  log_common("Child process (%d) start", pid);
327                  return pid;                  return pid;
328          }          }
329          else if (pid < 0) // Error          else if (pid < 0) // Error
330          {          {
331                  log_error("fork() error (%d)\n", errno);                  log_error("fork() error (%d)", errno);
332                  return -1;                  return -1;
333          }          }
334    
# Line 336  static int fork_server(void) Line 336  static int fork_server(void)
336  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
337          if (close(epollfd_server) < 0)          if (close(epollfd_server) < 0)
338          {          {
339                  log_error("close(epollfd_server) error (%d)\n");                  log_error("close(epollfd_server) error (%d)");
340          }          }
341  #endif  #endif
342    
# Line 344  static int fork_server(void) Line 344  static int fork_server(void)
344          {          {
345                  if (close(socket_server[i]) == -1)                  if (close(socket_server[i]) == -1)
346                  {                  {
347                          log_error("Close server socket failed\n");                          log_error("Close server socket failed");
348                  }                  }
349          }          }
350    
# Line 357  static int fork_server(void) Line 357  static int fork_server(void)
357          {          {
358                  if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK)                  if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK)
359                  {                  {
360                          log_error("ssh_bind_accept_fd() error: %s\n", ssh_get_error(SSH_session));                          log_error("ssh_bind_accept_fd() error: %s", ssh_get_error(SSH_session));
361                          goto cleanup;                          goto cleanup;
362                  }                  }
363    
# Line 366  static int fork_server(void) Line 366  static int fork_server(void)
366                  ssh_timeout = 60; // second                  ssh_timeout = 60; // second
367                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
368                  {                  {
369                          log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));                          log_error("Error setting SSH options: %s", ssh_get_error(SSH_session));
370                          goto cleanup;                          goto cleanup;
371                  }                  }
372    
# Line 379  static int fork_server(void) Line 379  static int fork_server(void)
379    
380                  if (ssh_handle_key_exchange(SSH_session))                  if (ssh_handle_key_exchange(SSH_session))
381                  {                  {
382                          log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));                          log_error("ssh_handle_key_exchange() error: %s", ssh_get_error(SSH_session));
383                          goto cleanup;                          goto cleanup;
384                  }                  }
385    
# Line 391  static int fork_server(void) Line 391  static int fork_server(void)
391                          ret = ssh_event_dopoll(event, 100); // 0.1 second                          ret = ssh_event_dopoll(event, 100); // 0.1 second
392                          if (ret == SSH_ERROR)                          if (ret == SSH_ERROR)
393                          {                          {
394  #ifdef _DEBUG                                  log_debug("ssh_event_dopoll() error: %s", ssh_get_error(SSH_session));
                                 log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));  
 #endif  
395                                  goto cleanup;                                  goto cleanup;
396                          }                          }
397                  }                  }
398    
399                  if (cb_data.error)                  if (cb_data.error)
400                  {                  {
401                          log_error("SSH auth error, tried %d times\n", cb_data.tries);                          log_error("SSH auth error, tried %d times", cb_data.tries);
402                          goto cleanup;                          goto cleanup;
403                  }                  }
404    
# Line 425  static int fork_server(void) Line 423  static int fork_server(void)
423                  ssh_timeout = 0;                  ssh_timeout = 0;
424                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)                  if (ssh_options_set(SSH_session, SSH_OPTIONS_TIMEOUT, &ssh_timeout) < 0)
425                  {                  {
426                          log_error("Error setting SSH options: %s\n", ssh_get_error(SSH_session));                          log_error("Error setting SSH options: %s", ssh_get_error(SSH_session));
427                          goto cleanup;                          goto cleanup;
428                  }                  }
429    
# Line 435  static int fork_server(void) Line 433  static int fork_server(void)
433          // Redirect Input          // Redirect Input
434          if (dup2(socket_client, STDIN_FILENO) == -1)          if (dup2(socket_client, STDIN_FILENO) == -1)
435          {          {
436                  log_error("Redirect stdin to client socket failed\n");                  log_error("Redirect stdin to client socket failed");
437                  goto cleanup;                  goto cleanup;
438          }          }
439    
440          // Redirect Output          // Redirect Output
441          if (dup2(socket_client, STDOUT_FILENO) == -1)          if (dup2(socket_client, STDOUT_FILENO) == -1)
442          {          {
443                  log_error("Redirect stdout to client socket failed\n");                  log_error("Redirect stdout to client socket failed");
444                  goto cleanup;                  goto cleanup;
445          }          }
446    
447          if (io_init() < 0)          if (io_init() < 0)
448          {          {
449                  log_error("io_init() error\n");                  log_error("io_init() error");
450                  goto cleanup;                  goto cleanup;
451          }          }
452    
# Line 457  static int fork_server(void) Line 455  static int fork_server(void)
455          // BWF compile          // BWF compile
456          if (bwf_compile() < 0)          if (bwf_compile() < 0)
457          {          {
458                  log_error("bwf_compile() error\n");                  log_error("bwf_compile() error");
459                  goto cleanup;                  goto cleanup;
460          }          }
461    
# Line 491  cleanup: Line 489  cleanup:
489          }          }
490          else if (close(socket_client) == -1)          else if (close(socket_client) == -1)
491          {          {
492                  log_error("Close client socket failed\n");                  log_error("Close client socket failed");
493          }          }
494    
495          ssh_free(SSH_session);          ssh_free(SSH_session);
# Line 505  cleanup: Line 503  cleanup:
503          close(STDIN_FILENO);          close(STDIN_FILENO);
504          close(STDOUT_FILENO);          close(STDOUT_FILENO);
505    
506          log_common("Process exit normally\n");          log_common("Process exit normally");
507          log_end();          log_end();
508    
509          _exit(0);          _exit(0);
# Line 520  int net_server(const char *hostaddr, in_ Line 518  int net_server(const char *hostaddr, in_
518          int ret;          int ret;
519          int flags_server[2];          int flags_server[2];
520          struct sockaddr_in sin;          struct sockaddr_in sin;
521            char local_addr[INET_ADDRSTRLEN];
522    
523  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
524          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
# Line 530  int net_server(const char *hostaddr, in_ Line 529  int net_server(const char *hostaddr, in_
529          int nfds;          int nfds;
530          int notify_child_exit = 0;          int notify_child_exit = 0;
531          time_t tm_notify_child_exit = time(NULL);          time_t tm_notify_child_exit = time(NULL);
         int i, j;  
532          pid_t pid;          pid_t pid;
533          int ssh_key_valid = 0;          int ssh_key_valid = 0;
534          int ssh_log_level = SSH_LOG_NOLOG;          int ssh_log_level = SSH_LOG_NOLOG;
# Line 545  int net_server(const char *hostaddr, in_ Line 543  int net_server(const char *hostaddr, in_
543    
544          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_RSA_KEY_FILE) < 0)          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_RSA_KEY_FILE) < 0)
545          {          {
546                  log_error("Error loading SSH RSA key: %s\n", SSH_HOST_RSA_KEY_FILE);                  log_error("Error loading SSH RSA key: %s", SSH_HOST_RSA_KEY_FILE);
547          }          }
548          else          else
549          {          {
# Line 553  int net_server(const char *hostaddr, in_ Line 551  int net_server(const char *hostaddr, in_
551          }          }
552          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ED25519_KEY_FILE) < 0)          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ED25519_KEY_FILE) < 0)
553          {          {
554                  log_error("Error loading SSH ED25519 key: %s\n", SSH_HOST_ED25519_KEY_FILE);                  log_error("Error loading SSH ED25519 key: %s", SSH_HOST_ED25519_KEY_FILE);
555          }          }
556          else          else
557          {          {
# Line 561  int net_server(const char *hostaddr, in_ Line 559  int net_server(const char *hostaddr, in_
559          }          }
560          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ECDSA_KEY_FILE) < 0)          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, SSH_HOST_ECDSA_KEY_FILE) < 0)
561          {          {
562                  log_error("Error loading SSH ECDSA key: %s\n", SSH_HOST_ECDSA_KEY_FILE);                  log_error("Error loading SSH ECDSA key: %s", SSH_HOST_ECDSA_KEY_FILE);
563          }          }
564          else          else
565          {          {
# Line 570  int net_server(const char *hostaddr, in_ Line 568  int net_server(const char *hostaddr, in_
568    
569          if (!ssh_key_valid)          if (!ssh_key_valid)
570          {          {
571                  log_error("Error: no valid SSH host key\n");                  log_error("Error: no valid SSH host key");
572                  ssh_bind_free(sshbind);                  ssh_bind_free(sshbind);
573                  return -1;                  return -1;
574          }          }
575    
576          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||          if (ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, hostaddr) < 0 ||
577                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port) < 0 ||
578                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "+ssh-rsa") < 0 ||                  ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, "+ssh-ed25519,ecdsa-sha2-nistp256,ssh-rsa") < 0 ||
579                  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)
580          {          {
581                  log_error("Error setting SSH bind options: %s\n", ssh_get_error(sshbind));                  log_error("Error setting SSH bind options: %s", ssh_get_error(sshbind));
582                  ssh_bind_free(sshbind);                  ssh_bind_free(sshbind);
583                  return -1;                  return -1;
584          }          }
# Line 589  int net_server(const char *hostaddr, in_ Line 587  int net_server(const char *hostaddr, in_
587          epollfd_server = epoll_create1(0);          epollfd_server = epoll_create1(0);
588          if (epollfd_server == -1)          if (epollfd_server == -1)
589          {          {
590                  log_error("epoll_create1() error (%d)\n", errno);                  log_error("epoll_create1() error (%d)", errno);
591                  return -1;                  return -1;
592          }          }
593  #endif  #endif
594    
595          // Server socket          // Server socket
596          for (i = 0; i < 2; i++)          for (int i = 0; i < 2; i++)
597          {          {
598                  socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);                  socket_server[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
599    
600                  if (socket_server[i] < 0)                  if (socket_server[i] < 0)
601                  {                  {
602                          log_error("Create socket_server error (%d)\n", errno);                          log_error("Create socket_server error (%d)", errno);
603                          return -1;                          return -1;
604                  }                  }
605    
# Line 609  int net_server(const char *hostaddr, in_ Line 607  int net_server(const char *hostaddr, in_
607                  sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY);                  sin.sin_addr.s_addr = (hostaddr[0] != '\0' ? inet_addr(hostaddr) : INADDR_ANY);
608                  sin.sin_port = htons(port[i]);                  sin.sin_port = htons(port[i]);
609    
610                    if (inet_ntop(AF_INET, &(sin.sin_addr), local_addr, sizeof(local_addr)) == NULL)
611                    {
612                            log_error("inet_ntop() error (%d)", errno);
613                            return -1;
614                    }
615    
616                  // Reuse address and port                  // Reuse address and port
617                  flags_server[i] = 1;                  flags_server[i] = 1;
618                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags_server[i], sizeof(flags_server[i])) < 0)                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEADDR, &flags_server[i], sizeof(flags_server[i])) < 0)
619                  {                  {
620                          log_error("setsockopt SO_REUSEADDR error (%d)\n", errno);                          log_error("setsockopt SO_REUSEADDR error (%d)", errno);
621                  }                  }
622  #if defined(SO_REUSEPORT)  #if defined(SO_REUSEPORT)
623                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0)                  if (setsockopt(socket_server[i], SOL_SOCKET, SO_REUSEPORT, &flags_server[i], sizeof(flags_server[i])) < 0)
624                  {                  {
625                          log_error("setsockopt SO_REUSEPORT error (%d)\n", errno);                          log_error("setsockopt SO_REUSEPORT error (%d)", errno);
626                  }                  }
627  #endif  #endif
628    
629                  if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0)                  if (bind(socket_server[i], (struct sockaddr *)&sin, sizeof(sin)) < 0)
630                  {                  {
631                          log_error("Bind address %s:%u error (%d)\n",                          log_error("Bind address %s:%u error (%d)",
632                                            inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno);                                            local_addr, port[i], errno);
633                          return -1;                          return -1;
634                  }                  }
635    
636                  if (listen(socket_server[i], 10) < 0)                  if (listen(socket_server[i], 10) < 0)
637                  {                  {
638                          log_error("Telnet socket listen error (%d)\n", errno);                          log_error("Telnet socket listen error (%d)", errno);
639                          return -1;                          return -1;
640                  }                  }
641    
642                  log_common("Listening at %s:%u\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));                  log_common("Listening at %s:%u", local_addr, port[i]);
643    
644  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
645                  ev.events = EPOLLIN;                  ev.events = EPOLLIN;
646                  ev.data.fd = socket_server[i];                  ev.data.fd = socket_server[i];
647                  if (epoll_ctl(epollfd_server, EPOLL_CTL_ADD, socket_server[i], &ev) == -1)                  if (epoll_ctl(epollfd_server, EPOLL_CTL_ADD, socket_server[i], &ev) == -1)
648                  {                  {
649                          log_error("epoll_ctl(socket_server[%d]) error (%d)\n", i, errno);                          log_error("epoll_ctl(socket_server[%d]) error (%d)", i, errno);
650                          if (close(epollfd_server) < 0)                          if (close(epollfd_server) < 0)
651                          {                          {
652                                  log_error("close(epoll) error (%d)\n");                                  log_error("close(epoll) error (%d)");
653                          }                          }
654                          return -1;                          return -1;
655                  }                  }
# Line 660  int net_server(const char *hostaddr, in_ Line 664  int net_server(const char *hostaddr, in_
664          hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);          hash_dict_pid_sockaddr = hash_dict_create(MAX_CLIENT_LIMIT);
665          if (hash_dict_pid_sockaddr == NULL)          if (hash_dict_pid_sockaddr == NULL)
666          {          {
667                  log_error("hash_dict_create(hash_dict_pid_sockaddr) error\n");                  log_error("hash_dict_create(hash_dict_pid_sockaddr) error");
668                  return -1;                  return -1;
669          }          }
670          hash_dict_sockaddr_count = hash_dict_create(MAX_CLIENT_LIMIT);          hash_dict_sockaddr_count = hash_dict_create(MAX_CLIENT_LIMIT);
671          if (hash_dict_sockaddr_count == NULL)          if (hash_dict_sockaddr_count == NULL)
672          {          {
673                  log_error("hash_dict_create(hash_dict_sockaddr_count) error\n");                  log_error("hash_dict_create(hash_dict_sockaddr_count) error");
674                  return -1;                  return -1;
675          }          }
676    
# Line 700  int net_server(const char *hostaddr, in_ Line 704  int net_server(const char *hostaddr, in_
704    
705                                  if (WIFEXITED(ret))                                  if (WIFEXITED(ret))
706                                  {                                  {
707                                          log_common("Child process (%d) exited, status=%d\n", pid, WEXITSTATUS(ret));                                          log_common("Child process (%d) exited, status=%d", pid, WEXITSTATUS(ret));
708                                  }                                  }
709                                  else if (WIFSIGNALED(ret))                                  else if (WIFSIGNALED(ret))
710                                  {                                  {
711                                          log_common("Child process (%d) is killed, status=%d\n", pid, WTERMSIG(ret));                                          log_common("Child process (%d) is killed, status=%d", pid, WTERMSIG(ret));
712                                  }                                  }
713                                  else                                  else
714                                  {                                  {
715                                          log_common("Child process (%d) exited abnormally, status=%d\n", pid, ret);                                          log_common("Child process (%d) exited abnormally, status=%d", pid, ret);
716                                  }                                  }
717    
718                                  if (pid != section_list_loader_pid)                                  if (pid != section_list_loader_pid)
719                                  {                                  {
720                                          j = 0;                                          int64_t j = 0;
721                                          ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)pid, (int64_t *)&j);                                          ret = hash_dict_get(hash_dict_pid_sockaddr, (uint64_t)pid, &j);
722                                          if (ret < 0)                                          if (ret < 0)
723                                          {                                          {
724                                                  log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error\n", pid);                                                  log_error("hash_dict_get(hash_dict_pid_sockaddr, %d) error", pid);
725                                          }                                          }
726                                          else                                          else
727                                          {                                          {
728                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (in_addr_t)j, -1);                                                  ret = hash_dict_inc(hash_dict_sockaddr_count, (in_addr_t)j, -1);
729                                                  if (ret <= 0)                                                  if (ret <= 0)
730                                                  {                                                  {
731                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %d, -1) error: %d\n", (in_addr_t)j, ret);                                                          log_error("hash_dict_inc(hash_dict_sockaddr_count, %lu, -1) error: %d", (in_addr_t)j, ret);
732                                                  }                                                  }
733    
734                                                  ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)pid);                                                  ret = hash_dict_del(hash_dict_pid_sockaddr, (uint64_t)pid);
735                                                  if (ret < 0)                                                  if (ret < 0)
736                                                  {                                                  {
737                                                          log_error("hash_dict_del(hash_dict_pid_sockaddr, %d) error\n", pid);                                                          log_error("hash_dict_del(hash_dict_pid_sockaddr, %lu) error", (uint64_t)pid);
738                                                  }                                                  }
739                                          }                                          }
740                                  }                                  }
# Line 741  int net_server(const char *hostaddr, in_ Line 745  int net_server(const char *hostaddr, in_
745                          }                          }
746                          else if (pid < 0)                          else if (pid < 0)
747                          {                          {
748                                  log_error("Error in waitpid(): %d\n", errno);                                  log_error("Error in waitpid(): %d", errno);
749                                  break;                                  break;
750                          }                          }
751                  }                  }
# Line 752  int net_server(const char *hostaddr, in_ Line 756  int net_server(const char *hostaddr, in_
756                          {                          {
757  #ifdef HAVE_SYSTEMD_SD_DAEMON_H  #ifdef HAVE_SYSTEMD_SD_DAEMON_H
758                                  sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);                                  sd_notifyf(0, "STATUS=Notify %d child process to exit", SYS_child_process_count);
759                                  log_common("Notify %d child process to exit\n", SYS_child_process_count);                                  log_common("Notify %d child process to exit", SYS_child_process_count);
760  #endif  #endif
761    
762                                  if (kill(0, SIGTERM) < 0)                                  if (kill(0, SIGTERM) < 0)
763                                  {                                  {
764                                          log_error("Send SIGTERM signal failed (%d)\n", errno);                                          log_error("Send SIGTERM signal failed (%d)", errno);
765                                  }                                  }
766    
767                                  notify_child_exit = 1;                                  notify_child_exit = 1;
# Line 771  int net_server(const char *hostaddr, in_ Line 775  int net_server(const char *hostaddr, in_
775    
776                                  if (kill(0, SIGKILL) < 0)                                  if (kill(0, SIGKILL) < 0)
777                                  {                                  {
778                                          log_error("Send SIGKILL signal failed (%d)\n", errno);                                          log_error("Send SIGKILL signal failed (%d)", errno);
779                                  }                                  }
780    
781                                  notify_child_exit = 2;                                  notify_child_exit = 2;
# Line 779  int net_server(const char *hostaddr, in_ Line 783  int net_server(const char *hostaddr, in_
783                          }                          }
784                          else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT)                          else if (notify_child_exit == 2 && time(NULL) - tm_notify_child_exit >= WAIT_CHILD_PROCESS_KILL_TIMEOUT)
785                          {                          {
786                                  log_error("Main process prepare to exit without waiting for %d child process any longer\n", SYS_child_process_count);                                  log_error("Main process prepare to exit without waiting for %d child process any longer", SYS_child_process_count);
787                                  SYS_child_process_count = 0;                                  SYS_child_process_count = 0;
788                          }                          }
789                  }                  }
# Line 792  int net_server(const char *hostaddr, in_ Line 796  int net_server(const char *hostaddr, in_
796                          sd_notify(0, "RELOADING=1");                          sd_notify(0, "RELOADING=1");
797  #endif  #endif
798    
799                            log_common("Reload configuration");
800    
801                          // Restart log                          // Restart log
802                          if (log_restart() < 0)                          if (log_restart() < 0)
803                          {                          {
804                                  log_error("Restart logging failed\n");                                  log_error("Restart logging failed");
805                          }                          }
806    
807                          // Reload configuration                          // Reload configuration
808                          if (load_conf(CONF_BBSD) < 0)                          if (load_conf(CONF_BBSD) < 0)
809                          {                          {
810                                  log_error("Reload conf failed\n");                                  log_error("Reload conf failed");
811                          }                          }
812    
813                          // Reload BWF config                          // Reload BWF config
814                          if (bwf_load(CONF_BWF) < 0)                          if (bwf_load(CONF_BWF) < 0)
815                          {                          {
816                                  log_error("Reload BWF conf failed\n");                                  log_error("Reload BWF conf failed");
817                          }                          }
818    
819                          // Get EULA modification tm                          // Get EULA modification tm
820                          if (stat(DATA_EULA, &file_stat) == -1)                          if (stat(DATA_EULA, &file_stat) == -1)
821                          {                          {
822                                  log_error("stat(%s) error\n", DATA_EULA, errno);                                  log_error("stat(%s) error", DATA_EULA, errno);
823                          }                          }
824                          else                          else
825                          {                          {
# Line 822  int net_server(const char *hostaddr, in_ Line 828  int net_server(const char *hostaddr, in_
828    
829                          if (detach_menu_shm(&bbs_menu) < 0)                          if (detach_menu_shm(&bbs_menu) < 0)
830                          {                          {
831                                  log_error("detach_menu_shm(bbs_menu) error\n");                                  log_error("detach_menu_shm(bbs_menu) error");
832                          }                          }
833                          if (load_menu(&bbs_menu, CONF_MENU) < 0)                          if (load_menu(&bbs_menu, CONF_MENU) < 0)
834                          {                          {
835                                  log_error("load_menu(bbs_menu) error\n");                                  log_error("load_menu(bbs_menu) error");
836                                  unload_menu(&bbs_menu);                                  unload_menu(&bbs_menu);
837                          }                          }
838    
839                          if (detach_menu_shm(&top10_menu) < 0)                          if (detach_menu_shm(&top10_menu) < 0)
840                          {                          {
841                                  log_error("detach_menu_shm(top10_menu) error\n");                                  log_error("detach_menu_shm(top10_menu) error");
842                          }                          }
843                          if (load_menu(&top10_menu, CONF_TOP10_MENU) < 0)                          if (load_menu(&top10_menu, CONF_TOP10_MENU) < 0)
844                          {                          {
845                                  log_error("load_menu(top10_menu) error\n");                                  log_error("load_menu(top10_menu) error");
846                                  unload_menu(&top10_menu);                                  unload_menu(&top10_menu);
847                          }                          }
848                          top10_menu.allow_exit = 1;                          top10_menu.allow_exit = 1;
# Line 845  int net_server(const char *hostaddr, in_ Line 851  int net_server(const char *hostaddr, in_
851                          {                          {
852                                  if (load_file(data_files_load_startup[i]) < 0)                                  if (load_file(data_files_load_startup[i]) < 0)
853                                  {                                  {
854                                          log_error("load_file(%s) error\n", data_files_load_startup[i]);                                          log_error("load_file(%s) error", data_files_load_startup[i]);
855                                  }                                  }
856                          }                          }
857    
858                          // Load section config and gen_ex                          // Load section config and gen_ex
859                          if (load_section_config_from_db(1) < 0)                          if (load_section_config_from_db(1) < 0)
860                          {                          {
861                                  log_error("load_section_config_from_db(1) error\n");                                  log_error("load_section_config_from_db(1) error");
862                          }                          }
863    
864                          // Notify child processes to reload configuration                          // Notify child processes to reload configuration
865                          if (kill(0, SIGUSR1) < 0)                          if (kill(0, SIGUSR1) < 0)
866                          {                          {
867                                  log_error("Send SIGUSR1 signal failed (%d)\n", errno);                                  log_error("Send SIGUSR1 signal failed (%d)", errno);
868                          }                          }
869    
870  #ifdef HAVE_SYSTEMD_SD_DAEMON_H  #ifdef HAVE_SYSTEMD_SD_DAEMON_H
# Line 882  int net_server(const char *hostaddr, in_ Line 888  int net_server(const char *hostaddr, in_
888                          if (errno != EINTR)                          if (errno != EINTR)
889                          {                          {
890  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
891                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)", errno);
892  #else  #else
893                                  log_error("poll() error (%d)\n", errno);                                  log_error("poll() error (%d)", errno);
894  #endif  #endif
895                                  break;                                  break;
896                          }                          }
# Line 927  int net_server(const char *hostaddr, in_ Line 933  int net_server(const char *hostaddr, in_
933                                                  }                                                  }
934                                                  else                                                  else
935                                                  {                                                  {
936                                                          log_error("accept(socket_server) error (%d)\n", errno);                                                          log_error("accept(socket_server) error (%d)", errno);
937                                                          break;                                                          break;
938                                                  }                                                  }
939                                          }                                          }
940    
941                                          strncpy(hostaddr_client, inet_ntoa(sin.sin_addr), sizeof(hostaddr_client) - 1);                                          if (inet_ntop(AF_INET, &(sin.sin_addr), hostaddr_client, sizeof(hostaddr_client)) == NULL)
942                                          hostaddr_client[sizeof(hostaddr_client) - 1] = '\0';                                          {
943                                                    log_error("inet_ntop() error (%d)", errno);
944                                                    close(socket_client);
945                                                    break;
946                                            }
947                                          port_client = ntohs(sin.sin_port);                                          port_client = ntohs(sin.sin_port);
948    
949                                          if (SYS_child_process_count - 1 < BBS_max_client)                                          if (SYS_child_process_count - 1 < BBS_max_client)
950                                          {                                          {
951                                                  j = 0;                                                  int64_t j = 0;
952                                                  ret = hash_dict_get(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, (int64_t *)&j);                                                  ret = hash_dict_get(hash_dict_sockaddr_count, sin.sin_addr.s_addr, &j);
953                                                  if (ret < 0)                                                  if (ret < 0)
954                                                  {                                                  {
955                                                          log_error("hash_dict_get(hash_dict_sockaddr_count, %s) error\n", hostaddr_client);                                                          log_error("hash_dict_get(hash_dict_sockaddr_count, %s) error", hostaddr_client);
956                                                  }                                                  }
957    
958                                                  if (j < BBS_max_client_per_ip)                                                  if (j < BBS_max_client_per_ip)
959                                                  {                                                  {
960                                                          if ((pid = fork_server()) < 0)                                                          if ((pid = fork_server()) < 0)
961                                                          {                                                          {
962                                                                  log_error("fork_server() error\n");                                                                  log_error("fork_server() error");
963                                                          }                                                          }
964                                                          else if (pid > 0)                                                          else if (pid > 0)
965                                                          {                                                          {
966                                                                  ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr);                                                                  ret = hash_dict_set(hash_dict_pid_sockaddr, (uint64_t)pid, sin.sin_addr.s_addr);
967                                                                  if (ret < 0)                                                                  if (ret < 0)
968                                                                  {                                                                  {
969                                                                          log_error("hash_dict_set(hash_dict_pid_sockaddr, %d, %s) error\n", pid, hostaddr_client);                                                                          log_error("hash_dict_set(hash_dict_pid_sockaddr, %lu, %s) error", (uint64_t)pid, hostaddr_client);
970                                                                  }                                                                  }
971    
972                                                                  if (j == 0)                                                                  if (j == 0)
973                                                                  {                                                                  {
974                                                                          // First connection from this IP                                                                          // First connection from this IP
975                                                                          log_common("Accept %s connection from %s:%d\n",                                                                          log_common("Accept %s connection from %s:%d",
976                                                                                             (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client);                                                                                             (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client);
977    
978                                                                          ret = hash_dict_set(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);                                                                          ret = hash_dict_set(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);
979                                                                          if (ret < 0)                                                                          if (ret < 0)
980                                                                          {                                                                          {
981                                                                                  log_error("hash_dict_set(hash_dict_sockaddr_count, %s, 1) error\n", hostaddr_client);                                                                                  log_error("hash_dict_set(hash_dict_sockaddr_count, %s, 1) error", hostaddr_client);
982                                                                          }                                                                          }
983                                                                  }                                                                  }
984                                                                  else                                                                  else
985                                                                  {                                                                  {
986                                                                          // Increase connection count from this IP                                                                          // Increase connection count from this IP
987                                                                          log_common("Accept %s connection from %s:%d, already have %d connections\n",                                                                          log_common("Accept %s connection from %s:%d, already have %d connections",
988                                                                                             (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, j);                                                                                             (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, j);
989    
990                                                                          ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);                                                                          ret = hash_dict_inc(hash_dict_sockaddr_count, (uint64_t)sin.sin_addr.s_addr, 1);
991                                                                          if (ret <= 0)                                                                          if (ret <= 0)
992                                                                          {                                                                          {
993                                                                                  log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, 1) error: %d\n", hostaddr_client, ret);                                                                                  log_error("hash_dict_inc(hash_dict_sockaddr_count, %s, 1) error: %d", hostaddr_client, ret);
994                                                                          }                                                                          }
995                                                                  }                                                                  }
996                                                          }                                                          }
997                                                  }                                                  }
998                                                  else                                                  else
999                                                  {                                                  {
1000                                                          log_error("Rejected %s connection from %s:%d over limit per IP (%d >= %d)\n",                                                          log_error("Rejected %s connection from %s:%d over limit per IP (%d >= %d)",
1001                                                                            (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, j, BBS_max_client_per_ip);                                                                            (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, j, BBS_max_client_per_ip);
1002                                                  }                                                  }
1003                                          }                                          }
1004                                          else                                          else
1005                                          {                                          {
1006                                                  log_error("Rejected %s connection from %s:%d over limit (%d >= %d)\n",                                                  log_error("Rejected %s connection from %s:%d over limit (%d >= %d)",
1007                                                                    (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, SYS_child_process_count - 1, BBS_max_client);                                                                    (SSH_v2 ? "SSH" : "telnet"), hostaddr_client, port_client, SYS_child_process_count - 1, BBS_max_client);
1008                                          }                                          }
1009    
1010                                          if (close(socket_client) == -1)                                          if (close(socket_client) == -1)
1011                                          {                                          {
1012                                                  log_error("close(socket_lient) error (%d)\n", errno);                                                  log_error("close(socket_lient) error (%d)", errno);
1013                                          }                                          }
1014                                  }                                  }
1015                          }                          }
# Line 1010  int net_server(const char *hostaddr, in_ Line 1019  int net_server(const char *hostaddr, in_
1019  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
1020          if (close(epollfd_server) < 0)          if (close(epollfd_server) < 0)
1021          {          {
1022                  log_error("close(epollfd_server) error (%d)\n");                  log_error("close(epollfd_server) error (%d)");
1023          }          }
1024  #endif  #endif
1025    
1026          for (i = 0; i < 2; i++)          for (int i = 0; i < 2; i++)
1027          {          {
1028                  if (close(socket_server[i]) == -1)                  if (close(socket_server[i]) == -1)
1029                  {                  {
1030                          log_error("Close server socket failed\n");                          log_error("Close server socket failed");
1031                  }                  }
1032          }          }
1033    


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

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