File: scp_sync.c

package info (click to toggle)
xrdp 0.10.5-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 15,104 kB
  • sloc: ansic: 107,426; sh: 5,852; asm: 4,742; cpp: 2,555; makefile: 1,474
file content (210 lines) | stat: -rw-r--r-- 5,671 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
 * xrdp: A Remote Desktop Protocol server.
 *
 * Copyright (C) Jay Sorg 2004-2022
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 *
 * @file scp_sync.c
 * @brief scp definitions (synchronous calls)
 * @author Matt Burt
 *
 */

#if defined(HAVE_CONFIG_H)
#include "config_ac.h"
#endif

//nclude "tools_common.h"
//nclude "trans.h"
#include "os_calls.h"
#include "log.h"
#include "scp.h"
#include "scp_sync.h"

/*****************************************************************************/
int
scp_sync_wait_specific(struct trans *t, enum scp_msg_code wait_msgno)
{

    int rv = 0;
    int available = 0;

    while (rv == 0 && !available)
    {
        if ((rv = scp_msg_in_wait_available(t)) != 0)
        {
            LOG(LOG_LEVEL_ERROR, "Error waiting on sesman transport");
        }
        else
        {
            enum scp_msg_code reply_msgno = scp_msg_in_get_msgno(t);

            available = 1;
            if (reply_msgno != wait_msgno)
            {
                char buff[64];
                scp_msgno_to_str(reply_msgno, buff, sizeof(buff));

                LOG(LOG_LEVEL_WARNING,
                    "Ignoring unexpected message %s", buff);
                scp_msg_in_reset(t);
                available = 0;
            }
        }
    }

    return rv;
}

/*****************************************************************************/
int
scp_sync_uds_login_request(struct trans *t)
{
    int rv = scp_send_uds_login_request(t);
    if (rv == 0)
    {
        if ((rv = scp_sync_wait_specific(t, E_SCP_LOGIN_RESPONSE)) == 0)
        {
            enum scp_login_status login_result;
            int server_closed;
            rv = scp_get_login_response(t, &login_result, &server_closed, NULL);
            if (rv == 0 && login_result != E_SCP_LOGIN_OK)
            {
                char msg[256];
                scp_login_status_to_str(login_result, msg, sizeof(msg));
                g_printf("Login failed; %s\n", msg);
                rv = 1;
                if (!server_closed)
                {
                    (void)scp_send_close_connection_request(t);
                }
            }
            scp_msg_in_reset(t); // Done with this message
        }

    }
    return rv;
}

/*****************************************************************************/
struct list *
scp_sync_list_sessions_request(struct trans *t)
{
    struct list *sessions = list_create();
    if (sessions == NULL)
    {
        LOG(LOG_LEVEL_ERROR, "Out of memory for sessions list");
    }
    else
    {
        int end_of_list = 0;

        enum scp_list_sessions_status status;
        struct scp_session_info *p;

        int rv = scp_send_list_sessions_request(t);

        sessions->auto_free = 1;

        while (rv == 0 && !end_of_list)
        {
            rv = scp_sync_wait_specific(t, E_SCP_LIST_SESSIONS_RESPONSE);
            if (rv != 0)
            {
                break;
            }

            rv = scp_get_list_sessions_response(t, &status, &p);
            if (rv != 0)
            {
                break;
            }

            switch (status)
            {
                case E_SCP_LS_SESSION_INFO:
                    if (!list_add_item(sessions, (tintptr)p))
                    {
                        g_free(p);
                        LOG(LOG_LEVEL_ERROR, "Out of memory for session item");
                        rv = 1;
                    }
                    break;

                case E_SCP_LS_END_OF_LIST:
                    end_of_list = 1;
                    break;

                default:
                    LOG(LOG_LEVEL_ERROR,
                        "Unexpected return code %d for session item", status);
                    rv = 1;
            }
            scp_msg_in_reset(t);
        }

        if (rv != 0)
        {
            list_delete(sessions);
            sessions = NULL;
        }
    }

    return sessions;
}

/*****************************************************************************/
int
scp_sync_create_sockdir_request(struct trans *t)
{
    int rv = scp_send_create_sockdir_request(t);
    if (rv == 0)
    {
        rv = scp_sync_wait_specific(t, E_SCP_CREATE_SOCKDIR_RESPONSE);
        if (rv == 0)
        {
            enum scp_create_sockdir_status status;
            rv = scp_get_create_sockdir_response(t, &status);
            if (rv == 0)
            {
                switch (status)
                {
                    case E_SCP_CS_OK:
                        break;

                    case E_SCP_CS_NOT_LOGGED_IN:
                        LOG(LOG_LEVEL_ERROR, "sesman reported not-logged-in");
                        rv = 1;
                        break;

                    case E_SCP_CS_OTHER_ERROR:
                        LOG(LOG_LEVEL_ERROR,
                            "sesman reported fail on create directory");
                        rv = 1;
                        break;
                }
            }
            scp_msg_in_reset(t); // Done with this message
            if (!rv)
            {
                (void)scp_send_close_connection_request(t);
            }
        }

    }
    return rv;
}