1 /**
2 * xrdp: A Remote Desktop Protocol server.
3 *
4 * Copyright (C) Jay Sorg 2004-2012
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 /**
20 *
21 * @file libscp_session.c
22 * @brief SCP_SESSION handling code
23 * @author Simone Fedele
24 *
25 */
26
27 #if defined(HAVE_CONFIG_H)
28 #include <config_ac.h>
29 #endif
30
31 #include "libscp_session.h"
32 #include "string_calls.h"
33
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <arpa/inet.h>
37
38 //extern struct log_config* s_log;
39
40 /*******************************************************************/
41 struct SCP_SESSION *
scp_session_create(void)42 scp_session_create(void)
43 {
44 struct SCP_SESSION *s;
45
46 s = (struct SCP_SESSION *)g_malloc(sizeof(struct SCP_SESSION), 1);
47
48 if (0 == s)
49 {
50 LOG(LOG_LEVEL_ERROR, "[session:%d] session create: malloc error", __LINE__);
51 return 0;
52 }
53
54 return s;
55 }
56
57 /*******************************************************************/
58 int
scp_session_set_type(struct SCP_SESSION * s,tui8 type)59 scp_session_set_type(struct SCP_SESSION *s, tui8 type)
60 {
61 switch (type)
62 {
63 case SCP_SESSION_TYPE_XVNC:
64 s->type = SCP_SESSION_TYPE_XVNC;
65 break;
66
67 case SCP_SESSION_TYPE_XRDP:
68 s->type = SCP_SESSION_TYPE_XRDP;
69 break;
70
71 case SCP_SESSION_TYPE_XORG:
72 s->type = SCP_SESSION_TYPE_XORG;
73 break;
74
75 case SCP_GW_AUTHENTICATION:
76 s->type = SCP_GW_AUTHENTICATION;
77 break;
78
79 case SCP_SESSION_TYPE_MANAGE:
80 s->type = SCP_SESSION_TYPE_MANAGE;
81 s->mng = (struct SCP_MNG_DATA *)g_malloc(sizeof(struct SCP_MNG_DATA), 1);
82
83 if (NULL == s->mng)
84 {
85 LOG(LOG_LEVEL_ERROR, "[session:%d] set_type: internal error", __LINE__);
86 return 1;
87 }
88
89 break;
90
91 default:
92 LOG(LOG_LEVEL_WARNING, "[session:%d] set_type: unknown type", __LINE__);
93 return 1;
94 }
95
96 return 0;
97 }
98
99 /*******************************************************************/
100 int
scp_session_set_version(struct SCP_SESSION * s,tui32 version)101 scp_session_set_version(struct SCP_SESSION *s, tui32 version)
102 {
103 switch (version)
104 {
105 case 0:
106 s->version = 0;
107 break;
108 case 1:
109 s->version = 1;
110 break;
111 default:
112 LOG(LOG_LEVEL_WARNING, "[session:%d] set_version: unknown version", __LINE__);
113 return 1;
114 }
115
116 return 0;
117 }
118
119 /*******************************************************************/
120 int
scp_session_set_height(struct SCP_SESSION * s,tui16 h)121 scp_session_set_height(struct SCP_SESSION *s, tui16 h)
122 {
123 s->height = h;
124 return 0;
125 }
126
127 /*******************************************************************/
128 int
scp_session_set_width(struct SCP_SESSION * s,tui16 w)129 scp_session_set_width(struct SCP_SESSION *s, tui16 w)
130 {
131 s->width = w;
132 return 0;
133 }
134
135 /*******************************************************************/
136 int
scp_session_set_bpp(struct SCP_SESSION * s,tui8 bpp)137 scp_session_set_bpp(struct SCP_SESSION *s, tui8 bpp)
138 {
139 switch (bpp)
140 {
141 case 8:
142 case 15:
143 case 16:
144 case 24:
145 case 32:
146 s->bpp = bpp;
147 break;
148 default:
149 return 1;
150 }
151 return 0;
152 }
153
154 /*******************************************************************/
155 int
scp_session_set_rsr(struct SCP_SESSION * s,tui8 rsr)156 scp_session_set_rsr(struct SCP_SESSION *s, tui8 rsr)
157 {
158 if (s->rsr)
159 {
160 s->rsr = 1;
161 }
162 else
163 {
164 s->rsr = 0;
165 }
166
167 return 0;
168 }
169
170 /*******************************************************************/
171 int
scp_session_set_locale(struct SCP_SESSION * s,const char * str)172 scp_session_set_locale(struct SCP_SESSION *s, const char *str)
173 {
174 if (0 == str)
175 {
176 LOG(LOG_LEVEL_WARNING, "[session:%d] set_locale: null locale", __LINE__);
177 s->locale[0] = '\0';
178 return 1;
179 }
180
181 g_strncpy(s->locale, str, 17);
182 s->locale[17] = '\0';
183 return 0;
184 }
185
186 /*******************************************************************/
187 int
scp_session_set_username(struct SCP_SESSION * s,const char * str)188 scp_session_set_username(struct SCP_SESSION *s, const char *str)
189 {
190 if (0 == str)
191 {
192 LOG(LOG_LEVEL_WARNING, "[session:%d] set_username: null username", __LINE__);
193 return 1;
194 }
195
196 if (0 != s->username)
197 {
198 g_free(s->username);
199 }
200
201 s->username = g_strdup(str);
202
203 if (0 == s->username)
204 {
205 LOG(LOG_LEVEL_WARNING, "[session:%d] set_username: strdup error", __LINE__);
206 return 1;
207 }
208
209 return 0;
210 }
211
212 /*******************************************************************/
213 int
scp_session_set_password(struct SCP_SESSION * s,const char * str)214 scp_session_set_password(struct SCP_SESSION *s, const char *str)
215 {
216 if (0 == str)
217 {
218 LOG(LOG_LEVEL_WARNING, "[session:%d] set_password: null password", __LINE__);
219 return 1;
220 }
221
222 if (0 != s->password)
223 {
224 g_free(s->password);
225 }
226
227 s->password = g_strdup(str);
228
229 if (0 == s->password)
230 {
231 LOG(LOG_LEVEL_WARNING, "[session:%d] set_password: strdup error", __LINE__);
232 return 1;
233 }
234
235 return 0;
236 }
237
238 /*******************************************************************/
239 int
scp_session_set_domain(struct SCP_SESSION * s,const char * str)240 scp_session_set_domain(struct SCP_SESSION *s, const char *str)
241 {
242 if (0 == str)
243 {
244 LOG(LOG_LEVEL_WARNING, "[session:%d] set_domain: null domain", __LINE__);
245 return 1;
246 }
247
248 if (0 != s->domain)
249 {
250 g_free(s->domain);
251 }
252
253 s->domain = g_strdup(str);
254
255 if (0 == s->domain)
256 {
257 LOG(LOG_LEVEL_WARNING, "[session:%d] set_domain: strdup error", __LINE__);
258 return 1;
259 }
260
261 return 0;
262 }
263
264 /*******************************************************************/
265 int
scp_session_set_program(struct SCP_SESSION * s,const char * str)266 scp_session_set_program(struct SCP_SESSION *s, const char *str)
267 {
268 if (0 == str)
269 {
270 LOG(LOG_LEVEL_WARNING, "[session:%d] set_program: null program", __LINE__);
271 return 1;
272 }
273
274 if (0 != s->program)
275 {
276 g_free(s->program);
277 }
278
279 s->program = g_strdup(str);
280
281 if (0 == s->program)
282 {
283 LOG(LOG_LEVEL_WARNING, "[session:%d] set_program: strdup error", __LINE__);
284 return 1;
285 }
286
287 return 0;
288 }
289
290 /*******************************************************************/
291 int
scp_session_set_directory(struct SCP_SESSION * s,const char * str)292 scp_session_set_directory(struct SCP_SESSION *s, const char *str)
293 {
294 if (0 == str)
295 {
296 LOG(LOG_LEVEL_WARNING, "[session:%d] set_directory: null directory", __LINE__);
297 return 1;
298 }
299
300 if (0 != s->directory)
301 {
302 g_free(s->directory);
303 }
304
305 s->directory = g_strdup(str);
306
307 if (0 == s->directory)
308 {
309 LOG(LOG_LEVEL_WARNING, "[session:%d] set_directory: strdup error", __LINE__);
310 return 1;
311 }
312
313 return 0;
314 }
315
316 /*******************************************************************/
317 int
scp_session_set_client_ip(struct SCP_SESSION * s,const char * str)318 scp_session_set_client_ip(struct SCP_SESSION *s, const char *str)
319 {
320 if (0 == str)
321 {
322 LOG(LOG_LEVEL_WARNING, "[session:%d] set_client_ip: null ip", __LINE__);
323 return 1;
324 }
325
326 if (0 != s->client_ip)
327 {
328 g_free(s->client_ip);
329 }
330
331 s->client_ip = g_strdup(str);
332
333 if (0 == s->client_ip)
334 {
335 LOG(LOG_LEVEL_WARNING, "[session:%d] set_client_ip: strdup error", __LINE__);
336 return 1;
337 }
338
339 return 0;
340 }
341
342 /*******************************************************************/
343 int
scp_session_set_hostname(struct SCP_SESSION * s,const char * str)344 scp_session_set_hostname(struct SCP_SESSION *s, const char *str)
345 {
346 if (0 == str)
347 {
348 LOG(LOG_LEVEL_WARNING, "[session:%d] set_hostname: null hostname", __LINE__);
349 return 1;
350 }
351
352 if (0 != s->hostname)
353 {
354 g_free(s->hostname);
355 }
356
357 s->hostname = g_strdup(str);
358
359 if (0 == s->hostname)
360 {
361 LOG(LOG_LEVEL_WARNING, "[session:%d] set_hostname: strdup error", __LINE__);
362 return 1;
363 }
364
365 return 0;
366 }
367
368 /*******************************************************************/
369 int
scp_session_set_errstr(struct SCP_SESSION * s,const char * str)370 scp_session_set_errstr(struct SCP_SESSION *s, const char *str)
371 {
372 if (0 == str)
373 {
374 LOG(LOG_LEVEL_WARNING, "[session:%d] set_errstr: null string", __LINE__);
375 return 1;
376 }
377
378 if (0 != s->errstr)
379 {
380 g_free(s->errstr);
381 }
382
383 s->errstr = g_strdup(str);
384
385 if (0 == s->errstr)
386 {
387 LOG(LOG_LEVEL_WARNING, "[session:%d] set_errstr: strdup error", __LINE__);
388 return 1;
389 }
390
391 return 0;
392 }
393
394 /*******************************************************************/
395 int
scp_session_set_display(struct SCP_SESSION * s,SCP_DISPLAY display)396 scp_session_set_display(struct SCP_SESSION *s, SCP_DISPLAY display)
397 {
398 s->display = display;
399 return 0;
400 }
401
402 /*******************************************************************/
403 int
scp_session_set_addr(struct SCP_SESSION * s,int type,const void * addr)404 scp_session_set_addr(struct SCP_SESSION *s, int type, const void *addr)
405 {
406 switch (type)
407 {
408 case SCP_ADDRESS_TYPE_IPV4:
409 g_memcpy(&(s->ipv4addr), addr, 4);
410 break;
411 #ifdef IN6ADDR_ANY_INIT
412 case SCP_ADDRESS_TYPE_IPV6:
413 g_memcpy(s->ipv6addr, addr, 16);
414 break;
415 #endif
416 default:
417 return 1;
418 }
419
420 return 0;
421 }
422
423 /*******************************************************************/
424 int
scp_session_set_guid(struct SCP_SESSION * s,const tui8 * guid)425 scp_session_set_guid(struct SCP_SESSION *s, const tui8 *guid)
426 {
427 if (0 == guid)
428 {
429 LOG(LOG_LEVEL_WARNING, "[session:%d] set_guid: null guid", __LINE__);
430 return 1;
431 }
432
433 g_memcpy(s->guid, guid, 16);
434
435 return 0;
436 }
437
438 /*******************************************************************/
439 void
scp_session_destroy(struct SCP_SESSION * s)440 scp_session_destroy(struct SCP_SESSION *s)
441 {
442 g_free(s->username);
443 g_free(s->password);
444 g_free(s->hostname);
445 g_free(s->domain);
446 g_free(s->program);
447 g_free(s->directory);
448 g_free(s->client_ip);
449 g_free(s->errstr);
450 g_free(s->mng);
451 g_free(s);
452 }
453