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             break;
82 
83         default:
84             LOG(LOG_LEVEL_WARNING, "[session:%d] set_type: unknown type", __LINE__);
85             return 1;
86     }
87 
88     return 0;
89 }
90 
91 /*******************************************************************/
92 int
scp_session_set_version(struct SCP_SESSION * s,tui32 version)93 scp_session_set_version(struct SCP_SESSION *s, tui32 version)
94 {
95     switch (version)
96     {
97         case 0:
98             s->version = 0;
99             break;
100         case 1:
101             s->version = 1;
102             break;
103         default:
104             LOG(LOG_LEVEL_WARNING, "[session:%d] set_version: unknown version", __LINE__);
105             return 1;
106     }
107 
108     return 0;
109 }
110 
111 /*******************************************************************/
112 int
scp_session_set_height(struct SCP_SESSION * s,tui16 h)113 scp_session_set_height(struct SCP_SESSION *s, tui16 h)
114 {
115     s->height = h;
116     return 0;
117 }
118 
119 /*******************************************************************/
120 int
scp_session_set_width(struct SCP_SESSION * s,tui16 w)121 scp_session_set_width(struct SCP_SESSION *s, tui16 w)
122 {
123     s->width = w;
124     return 0;
125 }
126 
127 /*******************************************************************/
128 int
scp_session_set_bpp(struct SCP_SESSION * s,tui8 bpp)129 scp_session_set_bpp(struct SCP_SESSION *s, tui8 bpp)
130 {
131     switch (bpp)
132     {
133         case 8:
134         case 15:
135         case 16:
136         case 24:
137         case 32:
138             s->bpp = bpp;
139             break;
140         default:
141             return 1;
142     }
143     return 0;
144 }
145 
146 /*******************************************************************/
147 int
scp_session_set_rsr(struct SCP_SESSION * s,tui8 rsr)148 scp_session_set_rsr(struct SCP_SESSION *s, tui8 rsr)
149 {
150     if (s->rsr)
151     {
152         s->rsr = 1;
153     }
154     else
155     {
156         s->rsr = 0;
157     }
158 
159     return 0;
160 }
161 
162 /*******************************************************************/
163 int
scp_session_set_locale(struct SCP_SESSION * s,const char * str)164 scp_session_set_locale(struct SCP_SESSION *s, const char *str)
165 {
166     if (0 == str)
167     {
168         LOG(LOG_LEVEL_WARNING, "[session:%d] set_locale: null locale", __LINE__);
169         s->locale[0] = '\0';
170         return 1;
171     }
172 
173     g_strncpy(s->locale, str, 17);
174     s->locale[17] = '\0';
175     return 0;
176 }
177 
178 /*******************************************************************/
179 int
scp_session_set_username(struct SCP_SESSION * s,const char * str)180 scp_session_set_username(struct SCP_SESSION *s, const char *str)
181 {
182     if (0 == str)
183     {
184         LOG(LOG_LEVEL_WARNING, "[session:%d] set_username: null username", __LINE__);
185         return 1;
186     }
187 
188     if (0 != s->username)
189     {
190         g_free(s->username);
191     }
192 
193     s->username = g_strdup(str);
194 
195     if (0 == s->username)
196     {
197         LOG(LOG_LEVEL_WARNING, "[session:%d] set_username: strdup error", __LINE__);
198         return 1;
199     }
200 
201     return 0;
202 }
203 
204 /*******************************************************************/
205 int
scp_session_set_password(struct SCP_SESSION * s,const char * str)206 scp_session_set_password(struct SCP_SESSION *s, const char *str)
207 {
208     if (0 == str)
209     {
210         LOG(LOG_LEVEL_WARNING, "[session:%d] set_password: null password", __LINE__);
211         return 1;
212     }
213 
214     if (0 != s->password)
215     {
216         g_free(s->password);
217     }
218 
219     s->password = g_strdup(str);
220 
221     if (0 == s->password)
222     {
223         LOG(LOG_LEVEL_WARNING, "[session:%d] set_password: strdup error", __LINE__);
224         return 1;
225     }
226 
227     return 0;
228 }
229 
230 /*******************************************************************/
231 int
scp_session_set_domain(struct SCP_SESSION * s,const char * str)232 scp_session_set_domain(struct SCP_SESSION *s, const char *str)
233 {
234     if (0 == str)
235     {
236         LOG(LOG_LEVEL_WARNING, "[session:%d] set_domain: null domain", __LINE__);
237         return 1;
238     }
239 
240     if (0 != s->domain)
241     {
242         g_free(s->domain);
243     }
244 
245     s->domain = g_strdup(str);
246 
247     if (0 == s->domain)
248     {
249         LOG(LOG_LEVEL_WARNING, "[session:%d] set_domain: strdup error", __LINE__);
250         return 1;
251     }
252 
253     return 0;
254 }
255 
256 /*******************************************************************/
257 int
scp_session_set_program(struct SCP_SESSION * s,const char * str)258 scp_session_set_program(struct SCP_SESSION *s, const char *str)
259 {
260     if (0 == str)
261     {
262         LOG(LOG_LEVEL_WARNING, "[session:%d] set_program: null program", __LINE__);
263         return 1;
264     }
265 
266     if (0 != s->program)
267     {
268         g_free(s->program);
269     }
270 
271     s->program = g_strdup(str);
272 
273     if (0 == s->program)
274     {
275         LOG(LOG_LEVEL_WARNING, "[session:%d] set_program: strdup error", __LINE__);
276         return 1;
277     }
278 
279     return 0;
280 }
281 
282 /*******************************************************************/
283 int
scp_session_set_directory(struct SCP_SESSION * s,const char * str)284 scp_session_set_directory(struct SCP_SESSION *s, const char *str)
285 {
286     if (0 == str)
287     {
288         LOG(LOG_LEVEL_WARNING, "[session:%d] set_directory: null directory", __LINE__);
289         return 1;
290     }
291 
292     if (0 != s->directory)
293     {
294         g_free(s->directory);
295     }
296 
297     s->directory = g_strdup(str);
298 
299     if (0 == s->directory)
300     {
301         LOG(LOG_LEVEL_WARNING, "[session:%d] set_directory: strdup error", __LINE__);
302         return 1;
303     }
304 
305     return 0;
306 }
307 
308 /*******************************************************************/
309 int
scp_session_set_client_ip(struct SCP_SESSION * s,const char * str)310 scp_session_set_client_ip(struct SCP_SESSION *s, const char *str)
311 {
312     if (0 == str)
313     {
314         LOG(LOG_LEVEL_WARNING, "[session:%d] set_client_ip: null ip", __LINE__);
315         return 1;
316     }
317 
318     if (0 != s->client_ip)
319     {
320         g_free(s->client_ip);
321     }
322 
323     s->client_ip = g_strdup(str);
324 
325     if (0 == s->client_ip)
326     {
327         LOG(LOG_LEVEL_WARNING, "[session:%d] set_client_ip: strdup error", __LINE__);
328         return 1;
329     }
330 
331     return 0;
332 }
333 
334 /*******************************************************************/
335 int
scp_session_set_hostname(struct SCP_SESSION * s,const char * str)336 scp_session_set_hostname(struct SCP_SESSION *s, const char *str)
337 {
338     if (0 == str)
339     {
340         LOG(LOG_LEVEL_WARNING, "[session:%d] set_hostname: null hostname", __LINE__);
341         return 1;
342     }
343 
344     if (0 != s->hostname)
345     {
346         g_free(s->hostname);
347     }
348 
349     s->hostname = g_strdup(str);
350 
351     if (0 == s->hostname)
352     {
353         LOG(LOG_LEVEL_WARNING, "[session:%d] set_hostname: strdup error", __LINE__);
354         return 1;
355     }
356 
357     return 0;
358 }
359 
360 /*******************************************************************/
361 int
scp_session_set_errstr(struct SCP_SESSION * s,const char * str)362 scp_session_set_errstr(struct SCP_SESSION *s, const char *str)
363 {
364     if (0 == str)
365     {
366         LOG(LOG_LEVEL_WARNING, "[session:%d] set_errstr: null string", __LINE__);
367         return 1;
368     }
369 
370     if (0 != s->errstr)
371     {
372         g_free(s->errstr);
373     }
374 
375     s->errstr = g_strdup(str);
376 
377     if (0 == s->errstr)
378     {
379         LOG(LOG_LEVEL_WARNING, "[session:%d] set_errstr: strdup error", __LINE__);
380         return 1;
381     }
382 
383     return 0;
384 }
385 
386 /*******************************************************************/
387 int
scp_session_set_display(struct SCP_SESSION * s,SCP_DISPLAY display)388 scp_session_set_display(struct SCP_SESSION *s, SCP_DISPLAY display)
389 {
390     s->display = display;
391     return 0;
392 }
393 
394 /*******************************************************************/
395 int
scp_session_set_addr(struct SCP_SESSION * s,int type,const void * addr)396 scp_session_set_addr(struct SCP_SESSION *s, int type, const void *addr)
397 {
398     switch (type)
399     {
400         case SCP_ADDRESS_TYPE_IPV4:
401             g_memcpy(&(s->ipv4addr), addr, 4);
402             break;
403 #ifdef IN6ADDR_ANY_INIT
404         case SCP_ADDRESS_TYPE_IPV6:
405             g_memcpy(s->ipv6addr, addr, 16);
406             break;
407 #endif
408         default:
409             return 1;
410     }
411 
412     return 0;
413 }
414 
415 /*******************************************************************/
416 int
scp_session_set_guid(struct SCP_SESSION * s,const tui8 * guid)417 scp_session_set_guid(struct SCP_SESSION *s, const tui8 *guid)
418 {
419     if (0 == guid)
420     {
421         LOG(LOG_LEVEL_WARNING, "[session:%d] set_guid: null guid", __LINE__);
422         return 1;
423     }
424 
425     g_memcpy(s->guid, guid, 16);
426 
427     return 0;
428 }
429 
430 /*******************************************************************/
431 void
scp_session_destroy(struct SCP_SESSION * s)432 scp_session_destroy(struct SCP_SESSION *s)
433 {
434     if (s != NULL)
435     {
436         g_free(s->username);
437         g_free(s->password);
438         g_free(s->hostname);
439         g_free(s->domain);
440         g_free(s->program);
441         g_free(s->directory);
442         g_free(s->client_ip);
443         g_free(s->errstr);
444         g_free(s);
445     }
446 }
447 
448 /*******************************************************************/
449 struct SCP_SESSION *
scp_session_clone(const struct SCP_SESSION * s)450 scp_session_clone(const struct SCP_SESSION *s)
451 {
452     struct SCP_SESSION *result = NULL;
453 
454     if (s != NULL && (result = g_new(struct SCP_SESSION, 1)) != NULL)
455     {
456         /* Duplicate all the scalar variables */
457         g_memcpy(result, s, sizeof(*s));
458 
459         /* Now duplicate all the strings */
460         result->username = g_strdup(s->username);
461         result->password = g_strdup(s->password);
462         result->hostname = g_strdup(s->hostname);
463         result->errstr = g_strdup(s->errstr);
464         result->domain = g_strdup(s->domain);
465         result->program = g_strdup(s->program);
466         result->directory = g_strdup(s->directory);
467         result->client_ip = g_strdup(s->client_ip);
468 
469         /* Did all the string copies succeed? */
470         if ((s->username != NULL && result->username == NULL) ||
471                 (s->password != NULL && result->password == NULL) ||
472                 (s->hostname != NULL && result->hostname == NULL) ||
473                 (s->errstr != NULL && result->errstr == NULL) ||
474                 (s->domain != NULL && result->domain == NULL) ||
475                 (s->program != NULL && result->program == NULL) ||
476                 (s->directory != NULL && result->directory == NULL) ||
477                 (s->client_ip != NULL && result->client_ip == NULL))
478         {
479             scp_session_destroy(result);
480             result = NULL;
481         }
482     }
483 
484     return result;
485 }
486