/[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.11 by sysadm, Sat May 7 12:08:28 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            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    static int pty_request(ssh_session session, ssh_channel channel, const char *term,
81                                               int x, int y, int px, int py, void *userdata)
82    {
83            return 0;
84    }
85    
86    static int shell_request(ssh_session session, ssh_channel channel, void *userdata)
87    {
88            return 0;
89    }
90    
91    static struct ssh_channel_callbacks_struct channel_cb = {
92            .channel_pty_request_function = pty_request,
93            .channel_shell_request_function = shell_request};
94    
95    static ssh_channel new_session_channel(ssh_session session, void *userdata)
96  {  {
97    int pid;          if (SSH_channel != NULL)
98            {
99                    return NULL;
100            }
101    
102            SSH_channel = ssh_channel_new(session);
103            ssh_callbacks_init(&channel_cb);
104            ssh_set_channel_callbacks(SSH_channel, &channel_cb);
105    
106    if (pid = fork ())          return SSH_channel;
107    {  }
     SYS_child_process_count ++;  
     log_std ("Child process (%d) start\n", pid);  
     return 0;  
   }  
   else if (pid < 0)  
     return -1;  
   
   
   if (close (socket_server) == -1)  
     {  
       log_error ("Close server socket failed\n");  
       return -2;  
     }  
   
   //Redirect Input  
   close (0);  
   if (dup2 (socket_client, 0) == -1)  
     {  
       log_error ("Redirect stdin to client socket failed\n");  
       return -3;  
     }  
   
   //Redirect Output  
   close (1);  
   if (dup2 (socket_client, 1) == -1)  
     {  
       log_error ("Redirect stdout to client socket failed\n");  
       return -4;  
     }  
   
   bbs_main ();  
   
   if (close (socket_client) == -1)  
     {  
       log_error ("Close client socket failed\n");  
     }  
   
   //Close Input and Output for client  
   close (0);  
   close (1);  
108    
109    log_std ("Process exit normally\n");  int fork_server(void)
110    {
111            ssh_event event;
112            int pid;
113            int i;
114            int ret;
115    
116            struct ssl_server_cb_data_t cb_data = {
117                    .tries = 0,
118                    .error = 0,
119            };
120    
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