summaryrefslogtreecommitdiffstats
path: root/sesman/session.c
blob: 9b2965e13ad6142b048fdd3286fd95ee2293113d (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   xrdp: A Remote Desktop Protocol server.
   Copyright (C) Jay Sorg 2005

   session manager
   linux only

*/

#include <stdlib.h>

#include "sesman.h"

extern unsigned char g_fixedkey[8];
extern struct config_sesman g_cfg; /* config.h */
extern int g_server_type;
#ifdef OLDSESSION
extern struct session_item g_session_items[100]; /* sesman.h */
#else
struct session_chain* g_sessions;
#endif
int g_session_count;

/******************************************************************************/
struct session_item* DEFAULT_CC
session_get_bydata(char* name, int width, int height, int bpp)
{
#ifdef OLDSESSION
  int i;

  for (i = 0; i < 100; i++)
  {
    if (g_strncmp(name, g_session_items[i].name, 255) == 0 &&
        g_session_items[i].width == width &&
        g_session_items[i].height == height &&
        g_session_items[i].bpp == bpp)
    {
      return g_session_items + i;
    }
  }
#else
  struct session_chain* tmp;

  /*THREAD-FIX require chain lock */
  tmp=g_sessions;

  while (tmp != 0)
  {
    if (g_strncmp(name, tmp->item->name, 255) == 0 &&
        tmp->item->width == width &&
        tmp->item->height == height &&
        tmp->item->bpp == bpp)
    {
      /*THREAD-FIX release chain lock */
      return tmp->item;
    }
    tmp=tmp->next;   
  }
  
  /*THREAD-FIX release chain lock */
#endif
  return 0;
}

/******************************************************************************/
/* returns non zero if there is an xserver running on this display */
static int DEFAULT_CC
x_server_running(int display)
{
  char text[256];

  g_sprintf(text, "/tmp/.X11-unix/X%d", display);
  return g_file_exist(text);
}

/******************************************************************************/
/* returns 0 if error else the display number the session was started on */
int DEFAULT_CC
session_start(int width, int height, int bpp, char* username, char* password,
              long data)
{
  int display;
  int pid;
  int wmpid;
  int xpid;
  char geometry[32];
  char depth[32];
  char screen[32];
  char cur_dir[256];
  char text[256];
  char passwd_file[256];
#ifndef OLDSESSION
  struct session_chain* temp;
#endif

  /*THREAD-FIX lock to control g_session_count*/
  /* check to limit concurrent sessions */
  if (g_session_count >= g_cfg.sess.max_sessions)
  {
    log_message(LOG_LEVEL_INFO, "max concurrent session limit exceeded. login for user %s denied", username);
    return 0;
  }

#ifndef OLDSESSION
  temp=malloc(sizeof(struct session_chain));
  if (temp == 0)
  {
    log_message(LOG_LEVEL_ERROR, "cannot create new chain element - user %s", username);
    return 0;
  }
  temp->item = malloc(sizeof(struct session_item));
  if (temp->item == 0)
  {
    free(temp);
    log_message(LOG_LEVEL_ERROR, "cannot create new session item - user %s", username);
    return 0;
  }
#endif

  g_get_current_dir(cur_dir, 255);
  display = 10;
  /*while (x_server_running(display) && display < 50)*/
  /* we search for a free display up to max_sessions */
  /* we should need no more displays than this       */
  while (x_server_running(display))
  {
    display++;
    if ((display - 10) > g_cfg.sess.max_sessions || display >= 50)
    {
      return 0;
    }
  }
  wmpid = 0;
  pid = g_fork();
  if (pid == -1)
  {
  }
  else if (pid == 0) /* child */
  {
    g_unset_signals();
    auth_start_session(data, display);
    g_sprintf(geometry, "%dx%d", width, height);
    g_sprintf(depth, "%d", bpp);
    g_sprintf(screen, ":%d", display);
    wmpid = g_fork();
    if (wmpid == -1)
    {
    }
    else if (wmpid == 0) /* child */
    {
      /* give X a bit to start */
      g_sleep(1000);
      env_set_user(username, 0, display);
      if (x_server_running(display))
      {
        auth_set_env(data);
        /* try to execute user window manager if enabled */
        if (g_cfg.enable_user_wm)
        {
          g_sprintf(text,"%s/%s", g_getenv("HOME"), g_cfg.user_wm);
          if (g_file_exist(text))
          {
            g_execlp3(text, g_cfg.user_wm, 0);
          }
        }
        /* if we're here something happened to g_execlp3
           so we try running the default window manager */
        g_sprintf(text, "%s/%s", cur_dir, g_cfg.default_wm);
        g_execlp3(text, g_cfg.default_wm, 0);
	
        /* still a problem starting window manager just start xterm */
        g_execlp3("xterm", "xterm", 0);
        /* should not get here */
      }
      log_message(LOG_LEVEL_ALWAYS,"error starting window manager %s - pid %d", username, g_getpid());
      g_exit(0);
    }
    else /* parent */
    {
      xpid = g_fork();
      if (xpid == -1)
      {
      }
      else if (xpid == 0) /* child */
      {
        env_set_user(username, passwd_file, display);
        env_check_password_file(passwd_file, password);
        if (g_server_type == 0)
        {
          g_execlp11("Xvnc", "Xvnc", screen, "-geometry", geometry,
                     "-depth", depth, "-bs", "-rfbauth", passwd_file, 0);
        }
        else if (g_server_type == 10)
        {
          g_execlp11("Xrdp", "Xrdp", screen, "-geometry", geometry,
                     "-depth", depth, "-bs", 0, 0, 0);
        }
        /* should not get here */
        log_message(LOG_LEVEL_ALWAYS,"error doing execve for user %s - pid %d",username,g_getpid());
        g_exit(0);
      }
      else /* parent */
      {
        g_waitpid(wmpid);
        g_sigterm(xpid);
        g_sigterm(wmpid);
        g_sleep(1000);
        auth_end(data);
        g_exit(0);
      }
    }
  }
  else /* parent */
  {
#ifdef OLDSESSION
    g_session_items[display].pid = pid;
    g_strcpy(g_session_items[display].name, username);
    g_session_items[display].display = display;
    g_session_items[display].width = width;
    g_session_items[display].height = height;
    g_session_items[display].bpp = bpp;
    g_session_items[display].data = data;

    g_session_items[display].connect_time=g_time1();
    g_session_items[display].disconnect_time=(time_t) 0;
    g_session_items[display].idle_time=(time_t) 0;
    
    g_session_items[display].type=SESMAN_SESSION_TYPE_XVNC;
    g_session_items[display].status=SESMAN_SESSION_STATUS_ACTIVE;
    
    g_session_count++;
#else
    temp->item->pid=pid;
    temp->item->display=display;
    temp->item->width=width;
    temp->item->height=height;
    temp->item->bpp=bpp;
    temp->item->data=data;
    g_strncpy(temp->item->name, username, 255);

    temp->item->connect_time=g_time1();
    temp->item->disconnect_time=(time_t) 0;
    temp->item->idle_time=(time_t) 0;

    temp->item->type=SESMAN_SESSION_TYPE_XVNC;
    temp->item->status=SESMAN_SESSION_STATUS_ACTIVE;
    
    /*THREAD-FIX lock the chain*/
    temp->next=g_sessions;
    g_sessions=temp;
    g_session_count++;
    /*THERAD-FIX free the chain*/
#endif
    g_sleep(5000);
  }
  return display;
}

/*
SESMAN_SESSION_TYPE_XRDP  1
SESMAN_SESSION_TYPE_XVNC  2

SESMAN_SESSION_STATUS_ACTIVE        1
SESMAN_SESSION_STATUS_IDLE          2
SESMAN_SESSION_STATUS_DISCONNECTED  3

struct session_item
{
  char name[256];
  int pid; 
  int display;
  int width;
  int height;
  int bpp;
  long data;

  / *
  unsigned char status;
  unsigned char type;
  * / 
  
  / *
  time_t connect_time;
  time_t disconnect_time;
  time_t idle_time;
  * /
};

struct session_chain
{
  struct session_chain* next;
  struct session_item* item;
};
*/

#ifndef OLDSESSION

/******************************************************************************/
int DEFAULT_CC
session_kill(int pid)
{
  struct session_chain* tmp;
  struct session_chain* prev;
  
  /*THREAD-FIX require chain lock */
  tmp=g_sessions;
  prev=0;
  
  while (tmp != 0)
  {
    if (tmp->item == 0)
    {
      log_message(LOG_LEVEL_ERROR, "session descriptor for pid %d is null!", pid);
      if (prev == 0)
      {
        /* prev does no exist, so it's the first element - so we set g_sessions */
	g_sessions = tmp->next;
      }
      else
      {
        prev->next = tmp->next;
      }
      /*THREAD-FIX release chain lock */
      return SESMAN_SESSION_KILL_NULLITEM;
    }

    if (tmp->item->pid == pid)
    {
      /* deleting the session */   
      log_message(LOG_LEVEL_INFO, "session %d - user %s - terminated", tmp->item->pid, tmp->item->name);
      free(tmp->item);
      if (prev == 0)
      {
        /* prev does no exist, so it's the first element - so we set g_sessions */
	g_sessions = tmp->next;
      }
      else
      {
        prev->next = tmp->next;
      }
      free(tmp);
      g_session_count--;
      /*THREAD-FIX release chain lock */
      return SESMAN_SESSION_KILL_OK;
    }
     
    /* go on */
    prev = tmp;
    tmp=tmp->next;
  }
  
  /*THREAD-FIX release chain lock */
  return SESMAN_SESSION_KILL_NOTFOUND;
}

/******************************************************************************/
struct session_item* DEFAULT_CC
session_get_bypid(int pid)
{
  struct session_chain* tmp;
  
  /*THREAD-FIX  require chain lock */
  tmp=g_sessions;
  while (tmp != 0)
  {
    if (tmp->item == 0)
    {
      log_message(LOG_LEVEL_ERROR, "session descriptor for pid %d is null!", pid);
      /*THREAD-FIX release chain lock */
      return 0;
    }
    
    if (tmp->item->pid == pid)
    {
      /*THREAD-FIX release chain lock */
      return tmp->item;
    }

    /* go on */
    tmp=tmp->next;
  }
  
  /*THREAD-FIX release chain lock */
  return 0;
}

#endif