/[LeafOK_CVS]/lbbs/src/fork.c
ViewVC logotype

Diff of /lbbs/src/fork.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.10 by sysadm, Sun Mar 20 17:37:14 2005 UTC Revision 1.26 by sysadm, Thu Jun 5 08:36:02 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                            fork.c  -  description                                                    fork.c  -  description
3                               -------------------                                                           -------------------
4      begin                : Mon Oct 18 2004          Copyright            : (C) 2004-2025 by Leaflet
5      copyright            : (C) 2004 by Leaflet          Email                : leaflet@leafok.com
     email                : leaflet@leafok.com  
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17  #include "common.h"  #include "common.h"
18    #include "bbs_main.h"
19    #include "bbs.h"
20    #include "log.h"
21  #include "io.h"  #include "io.h"
22    #include "fork.h"
23    #include "menu.h"
24    #include "database.h"
25    #include "login.h"
26    #include <stdio.h>
27  #include <string.h>  #include <string.h>
28    #include <signal.h>
29    #include <unistd.h>
30    #include <stdlib.h>
31    #include <errno.h>
32    #include <libssh/libssh.h>
33    #include <libssh/server.h>
34    #include <libssh/callbacks.h>
35    
36  int  #define SSH_AUTH_MAX_DURATION 60 // seconds
37  fork_server ()  
38    struct ssl_server_cb_data_t
39    {
40            int tries;
41            int error;
42    };
43    
44    static int auth_password(ssh_session session, const char *user,
45                                                     const char *password, void *userdata)
46  {  {
47    int pid;          MYSQL *db;
48            struct ssl_server_cb_data_t *p_data = userdata;
49            int ret;
50    
51            if ((db = db_open()) == NULL)
52            {
53                    return SSH_AUTH_ERROR;
54            }
55    
56            if (strcmp(user, "guest") == 0)
57            {
58                    ret = load_guest_info(db);
59            }
60            else
61            {
62                    ret = check_user(db, user, password);
63            }
64    
65            mysql_close(db);
66    
67            if (ret == 0)
68            {
69                    return SSH_AUTH_SUCCESS;
70            }
71    
72            if ((++(p_data->tries)) >= BBS_login_retry_times)
73            {
74                    p_data->error = 1;
75            }
76    
77            return SSH_AUTH_DENIED;
78    }
79    
80    if (pid = fork ())  static int pty_request(ssh_session session, ssh_channel channel, const char *term,
81      return 0;                                             int x, int y, int px, int py, void *userdata)
82    else if (pid < 0)  {
83      return -1;          return 0;
84    }
85    log_std ("Child process start\n");  
86    static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
87    if (close (socket_server) == -1)  {
88      {          return 0;
89        log_error ("Close server socket failed\n");  }
90        return -2;  
91      }  static struct ssh_channel_callbacks_struct channel_cb = {
92            .channel_pty_request_function = pty_request,
93    //Redirect Input          .channel_shell_request_function = shell_request};
94    close (0);  
95    if (dup2 (socket_client, 0) == -1)  static ssh_channel new_session_channel(ssh_session session, void *userdata)
96      {  {
97        log_error ("Redirect stdin to client socket failed\n");          if (SSH_channel != NULL)
98        return -3;          {
99      }                  return NULL;
100            }
101    //Redirect Output  
102    close (1);          SSH_channel = ssh_channel_new(session);
103    if (dup2 (socket_client, 1) == -1)          ssh_callbacks_init(&channel_cb);
104      {          ssh_set_channel_callbacks(SSH_channel, &channel_cb);
105        log_error ("Redirect stdout to client socket failed\n");  
106        return -4;          return SSH_channel;
107      }  }
108    
109    bbs_main ();  int fork_server(void)
110    {
111    if (close (socket_client) == -1)          ssh_event event;
112      {          int pid;
113        log_error ("Close client socket failed\n");          int i;
114      }          int ret;
115    
116    log_std ("Child process exit\n");          struct ssl_server_cb_data_t cb_data = {
117                    .tries = 0,
118    //Close Input and Output for client                  .error = 0,
119    close (0);          };
120    close (1);  
121            struct ssh_server_callbacks_struct cb = {
122                    .userdata = &cb_data,
123                    .auth_password_function = auth_password,
124                    .channel_open_request_session_function = new_session_channel,
125            };
126    
127            pid = fork();
128    
129            if (pid > 0) // Parent process
130            {
131                    SYS_child_process_count++;
132                    log_common("Child process (%d) start\n", pid);
133                    return pid;
134            }
135            else if (pid < 0) // Error
136            {
137                    log_error("fork() error (%d)\n", errno);
138                    return -1;
139            }
140    
141            // Child process
142    
143            if (close(socket_server[0]) == -1 || close(socket_server[1]) == -1)
144            {
145                    log_error("Close server socket failed\n");
146            }
147    
148            SSH_session = ssh_new();
149    
150            if (SSH_v2)
151            {
152                    if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK)
153                    {
154                            log_error("ssh_bind_accept_fd() error: %s\n", ssh_get_error(SSH_session));
155                            goto cleanup;
156                    }
157    
158                    ssh_bind_free(sshbind);
159    
160                    ssh_callbacks_init(&cb);
161                    ssh_set_server_callbacks(SSH_session, &cb);
162    
163                    if (ssh_handle_key_exchange(SSH_session))
164                    {
165                            log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));
166                            goto cleanup;
167                    }
168                    ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);
169    
170                    event = ssh_event_new();
171                    ssh_event_add_session(event, SSH_session);
172    
173                    for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i++)
174                    {
175                            ret = ssh_event_dopoll(event, 1000); // 1 second
176                            if (ret == SSH_ERROR)
177                            {
178                                    log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));
179                                    goto cleanup;
180                            }
181                    }
182    
183                    if (cb_data.error)
184                    {
185                            log_error("SSH auth error, tried %d times\n", cb_data.tries);
186                            goto cleanup;
187                    }
188            }
189    
190            // Redirect Input
191            close(STDIN_FILENO);
192            if (dup2(socket_client, STDIN_FILENO) == -1)
193            {
194                    log_error("Redirect stdin to client socket failed\n");
195                    goto cleanup;
196            }
197    
198            // Redirect Output
199            close(STDOUT_FILENO);
200            if (dup2(socket_client, STDOUT_FILENO) == -1)
201            {
202                    log_error("Redirect stdout to client socket failed\n");
203                    goto cleanup;
204            }
205    
206            SYS_child_process_count = 0;
207    
208            bbs_main();
209    
210    cleanup:
211            // Child process exit
212            SYS_server_exit = 1;
213    
214            if (SSH_v2)
215            {
216                    ssh_channel_free(SSH_channel);
217                    ssh_disconnect(SSH_session);
218            }
219            else if (close(socket_client) == -1)
220            {
221                    log_error("Close client socket failed\n");
222            }
223    
224            ssh_free(SSH_session);
225            ssh_finalize();
226    
227            // Close Input and Output for client
228            close(STDIN_FILENO);
229            close(STDOUT_FILENO);
230    
231    log_end ();          log_common("Process exit normally\n");
232            log_end();
233    
234    //Exit child process normally          _exit(0);
   exit (0);  
235    
236    return 0;          return 0;
237  }  }


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

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