/[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.1 by sysadm, Mon Oct 11 11:15:33 2004 UTC Revision 1.25 by sysadm, Thu Jun 5 05:24:56 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                            fork.c  -  description                                                    fork.c  -  description
3                               -------------------                                                           -------------------
4      begin                : Mon Oct 11 2004          Copyright            : (C) 2004-2025 by Leaflet
5      copyright            : (C) 2004 by Leaf          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"
22    #include "fork.h"
23    #include "menu.h"
24    #include "database.h"
25    #include "login.h"
26    #include <stdio.h>
27    #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    #define SSH_AUTH_MAX_DURATION 60 // seconds
37    
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            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            return SSH_channel;
107    }
108    
109    int fork_server(ssh_bind sshbind)
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            if (close(socket_server) == -1)
143            {
144                    log_error("Close server socket failed\n");
145            }
146    
147            SSH_session = ssh_new();
148    
149            if (SSH_v2)
150            {
151                    if (ssh_bind_accept_fd(sshbind, SSH_session, socket_client) != SSH_OK)
152                    {
153                            log_error("ssh_bind_accept_fd() error: %s\n", ssh_get_error(SSH_session));
154                            goto cleanup;
155                    }
156    
157                    ssh_bind_free(sshbind);
158    
159                    ssh_callbacks_init(&cb);
160                    ssh_set_server_callbacks(SSH_session, &cb);
161    
162                    if (ssh_handle_key_exchange(SSH_session))
163                    {
164                            log_error("ssh_handle_key_exchange() error: %s\n", ssh_get_error(SSH_session));
165                            goto cleanup;
166                    }
167                    ssh_set_auth_methods(SSH_session, SSH_AUTH_METHOD_PASSWORD);
168    
169                    event = ssh_event_new();
170                    ssh_event_add_session(event, SSH_session);
171    
172                    for (i = 0; i < SSH_AUTH_MAX_DURATION && !SYS_server_exit && !cb_data.error && SSH_channel == NULL; i++)
173                    {
174                            ret = ssh_event_dopoll(event, 1000); // 1 second
175                            if (ret == SSH_ERROR)
176                            {
177                                    log_error("ssh_event_dopoll() error: %s\n", ssh_get_error(SSH_session));
178                                    goto cleanup;
179                            }
180                    }
181    
182                    if (cb_data.error)
183                    {
184                            log_error("SSH auth error, tried %d times\n", cb_data.tries);
185                            goto cleanup;
186                    }
187            }
188    
189            // Redirect Input
190            close(STDIN_FILENO);
191            if (dup2(socket_client, STDIN_FILENO) == -1)
192            {
193                    log_error("Redirect stdin to client socket failed\n");
194                    goto cleanup;
195            }
196    
197            // Redirect Output
198            close(STDOUT_FILENO);
199            if (dup2(socket_client, STDOUT_FILENO) == -1)
200            {
201                    log_error("Redirect stdout to client socket failed\n");
202                    goto cleanup;
203            }
204    
205            SYS_child_process_count = 0;
206    
207            bbs_main();
208    
209    cleanup:
210            // Child process exit
211            SYS_server_exit = 1;
212    
213            if (SSH_v2)
214            {
215                    ssh_channel_free(SSH_channel);
216                    ssh_disconnect(SSH_session);
217            }
218            else if (close(socket_client) == -1)
219            {
220                    log_error("Close client socket failed\n");
221            }
222    
223            ssh_free(SSH_session);
224            ssh_finalize();
225    
226            // Close Input and Output for client
227            close(STDIN_FILENO);
228            close(STDOUT_FILENO);
229    
230            log_common("Process exit normally\n");
231            log_end();
232    
233            _exit(0);
234    
235            return 0;
236    }


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

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