summaryrefslogtreecommitdiff
path: root/daemon/gdm-server.c
blob: 11701c7d26320b0157ad6a25c30f34c555045b5d (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <ctype.h>
#include <pwd.h>
#include <grp.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>

#include <X11/Xlib.h> /* for Display */

#include "gdm-common.h"

#include "gdm-server.h"

extern char **environ;

#define GDM_SERVER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDM_TYPE_SERVER, GdmServerPrivate))

/* These are the servstat values, also used as server
 * process exit codes */
#define SERVER_TIMEOUT 2	/* Server didn't start */
#define SERVER_DEAD 250		/* Server stopped */
#define SERVER_PENDING 251	/* Server started but not ready for connections yet */
#define SERVER_RUNNING 252	/* Server running and ready for connections */
#define SERVER_ABORT 253	/* Server failed badly. Suspending display. */

struct GdmServerPrivate
{
	char    *command;
	GPid     pid;

	gboolean disable_tcp;
	int      priority;
	uid_t    uid;
	char    *session_args;

	char    *log_dir;
	char    *display_name;
	char    *auth_file;

	gboolean is_parented;
	char    *parent_display_name;
	char    *parent_auth_file;
	char    *chosen_hostname;
};

enum {
	PROP_0,
	PROP_DISPLAY_NAME,
	PROP_AUTH_FILE,
	PROP_IS_PARENTED,
	PROP_PARENT_DISPLAY_NAME,
	PROP_PARENT_AUTH_FILE,
	PROP_CHOSEN_HOSTNAME,
	PROP_COMMAND,
	PROP_PRIORITY,
	PROP_UID,
	PROP_SESSION_ARGS,
	PROP_LOG_DIR,
	PROP_DISABLE_TCP,
};

static void	gdm_server_class_init	(GdmServerClass *klass);
static void	gdm_server_init	        (GdmServer      *server);
static void	gdm_server_finalize	(GObject        *object);

G_DEFINE_TYPE (GdmServer, gdm_server, G_TYPE_OBJECT)

gboolean
gdm_server_stop (GdmServer *server)
{
	g_debug ("Stopping server");

	return TRUE;
}

/* We keep a connection (parent_dsp) open with the parent X server
 * before running a proxy on it to prevent the X server resetting
 * as we open and close other connections.
 * Note that XDMCP servers, by default, reset when the seed X
 * connection closes whereas usually the X server only quits when
 * all X connections have closed.
 */
#if 0
static gboolean
connect_to_parent (GdmServer *server)
{
	int maxtries;
	int openretries;

	g_debug ("gdm_server_start: Connecting to parent display \'%s\'",
		   d->parent_disp);

	d->parent_dsp = NULL;

	maxtries = SERVER_IS_XDMCP (d) ? 10 : 2;

	openretries = 0;
	while (openretries < maxtries &&
	       d->parent_dsp == NULL) {
		d->parent_dsp = XOpenDisplay (d->parent_disp);

		if G_UNLIKELY (d->parent_dsp == NULL) {
			g_debug ("gdm_server_start: Sleeping %d on a retry", 1+openretries*2);
			gdm_sleep_no_signal (1+openretries*2);
			openretries++;
		}
	}

	if (d->parent_dsp == NULL)
		gdm_error (_("%s: failed to connect to parent display \'%s\'"),
			   "gdm_server_start", d->parent_disp);

	return d->parent_dsp != NULL;
}
#endif

static gboolean
gdm_server_resolve_command_line (GdmServer  *server,
				 const char *vtarg,
				 int        *argcp,
				 char     ***argvp)
{
	int      argc;
	char   **argv;
	int      len;
	int      i;
	gboolean gotvtarg = FALSE;
	gboolean query_in_arglist = FALSE;

	g_shell_parse_argv (server->priv->command, &argc, &argv, NULL);

	for (len = 0; argv != NULL && argv[len] != NULL; len++) {
		char *arg = argv[len];

		/* HACK! Not to add vt argument to servers that already force
		 * allocation.  Mostly for backwards compat only */
		if (strncmp (arg, "vt", 2) == 0 &&
		    isdigit (arg[2]) &&
		    (arg[3] == '\0' ||
		     (isdigit (arg[3]) && arg[4] == '\0')))
			gotvtarg = TRUE;
		if (strcmp (arg, "-query") == 0 ||
		    strcmp (arg, "-indirect") == 0)
			query_in_arglist = TRUE;
	}

	argv = g_renew (char *, argv, len + 10);
	/* shift args down one */
	for (i = len - 1; i >= 1; i--) {
		argv[i+1] = argv[i];
	}

	/* server number is the FIRST argument, before any others */
	argv[1] = g_strdup (server->priv->display_name);
	len++;

	if (server->priv->auth_file != NULL) {
		argv[len++] = g_strdup ("-auth");
		argv[len++] = g_strdup (server->priv->auth_file);
	}

	if (server->priv->chosen_hostname) {
		/* run just one session */
		argv[len++] = g_strdup ("-terminate");
		argv[len++] = g_strdup ("-query");
		argv[len++] = g_strdup (server->priv->chosen_hostname);
		query_in_arglist = TRUE;
	}

	if (server->priv->disable_tcp && ! query_in_arglist) {
		argv[len++] = g_strdup ("-nolisten");
		argv[len++] = g_strdup ("tcp");
	}

	if (vtarg != NULL && ! gotvtarg) {
		argv[len++] = g_strdup (vtarg);
	}

	argv[len++] = NULL;

	*argvp = argv;
	*argcp = len;

	return TRUE;
}

/* somewhat safer rename (safer if the log dir is unsafe), may in fact
   lose the file though, it guarantees that a is gone, but not that
   b exists */
static void
safer_rename (const char *a, const char *b)
{
	errno = 0;
	if (link (a, b) < 0) {
		if (errno == EEXIST) {
			VE_IGNORE_EINTR (g_unlink (a));
			return;
		}
		VE_IGNORE_EINTR (g_unlink (b));
		/* likely this system doesn't support hard links */
		g_rename (a, b);
		VE_IGNORE_EINTR (g_unlink (a));
		return;
	}
	VE_IGNORE_EINTR (g_unlink (a));
}

static void
rotate_logs (GdmServer *server)
{
	const char *dname;
	const char *logdir;

	dname = server->priv->display_name;
	logdir = server->priv->log_dir;

	/* I'm too lazy to write a loop */
	char *fname4 = gdm_make_filename (logdir, dname, ".log.4");
	char *fname3 = gdm_make_filename (logdir, dname, ".log.3");
	char *fname2 = gdm_make_filename (logdir, dname, ".log.2");
	char *fname1 = gdm_make_filename (logdir, dname, ".log.1");
	char *fname = gdm_make_filename (logdir, dname, ".log");

	/* Rotate the logs (keep 4 last) */
	VE_IGNORE_EINTR (g_unlink (fname4));
	safer_rename (fname3, fname4);
	safer_rename (fname2, fname3);
	safer_rename (fname1, fname2);
	safer_rename (fname, fname1);

	g_free (fname4);
	g_free (fname3);
	g_free (fname2);
	g_free (fname1);
	g_free (fname);
}

static void
change_user (GdmServer *server)
{
	if (server->priv->uid != 0) {
		struct passwd *pwent;

		pwent = getpwuid (server->priv->uid);
		if (pwent == NULL) {
			g_warning (_("%s: Server was to be spawned by uid %d but "
				     "that user doesn't exist"),
				   "gdm_server_spawn",
				   (int)server->priv->uid);
			_exit (SERVER_ABORT);
		}

		if (setgid (pwent->pw_gid) < 0)  {
			g_warning (_("%s: Couldn't set groupid to %d"), 
				   "gdm_server_spawn", (int)pwent->pw_gid);
			_exit (SERVER_ABORT);
		}

		if (initgroups (pwent->pw_name, pwent->pw_gid) < 0) {
			g_warning (_("%s: initgroups () failed for %s"),
				   "gdm_server_spawn", pwent->pw_name);
			_exit (SERVER_ABORT);
		}

		if (setuid (server->priv->uid) < 0)  {
			g_warning (_("%s: Couldn't set userid to %d"),
				   "gdm_server_spawn", (int)server->priv->uid);
			_exit (SERVER_ABORT);
		}
	} else {
		gid_t groups[1] = { 0 };

		if (setgid (0) < 0)  {
			g_warning (_("%s: Couldn't set groupid to 0"), 
				   "gdm_server_spawn");
			/* Don't error out, it's not fatal, if it fails we'll
			 * just still be */
		}
		/* this will get rid of any suplementary groups etc... */
		setgroups (1, groups);
	}
}

static void
server_child_setup (GdmServer *server)
{
	char            *logfile;
	int              logfd;
	struct sigaction ign_signal;
	sigset_t         mask;

	/* Rotate the X server logs */
	rotate_logs (server);

	/* Log all output from spawned programs to a file */
	logfile = gdm_make_filename (server->priv->log_dir,
				     server->priv->display_name,
				     ".log");
	VE_IGNORE_EINTR (g_unlink (logfile));
	VE_IGNORE_EINTR (logfd = open (logfile, O_CREAT|O_TRUNC|O_WRONLY|O_EXCL, 0644));

	if (logfd != -1) {
		VE_IGNORE_EINTR (dup2 (logfd, 1));
		VE_IGNORE_EINTR (dup2 (logfd, 2));
		close (logfd);
	} else {
		g_warning (_("%s: Could not open logfile for display %s!"),
			   "gdm_server_spawn",
			   server->priv->display_name);
	}

	/* The X server expects USR1/TTIN/TTOU to be SIG_IGN */
	ign_signal.sa_handler = SIG_IGN;
	ign_signal.sa_flags = SA_RESTART;
	sigemptyset (&ign_signal.sa_mask);

	if (sigaction (SIGUSR1, &ign_signal, NULL) < 0) {
		g_warning (_("%s: Error setting %s to %s"),
			   "gdm_server_spawn", "USR1", "SIG_IGN");
		_exit (SERVER_ABORT);
	}

	if (sigaction (SIGTTIN, &ign_signal, NULL) < 0) {
		g_warning (_("%s: Error setting %s to %s"),
			   "gdm_server_spawn", "TTIN", "SIG_IGN");
		_exit (SERVER_ABORT);
	}

	if (sigaction (SIGTTOU, &ign_signal, NULL) < 0) {
		g_warning (_("%s: Error setting %s to %s"),
			   "gdm_server_spawn", "TTOU", "SIG_IGN");
		_exit (SERVER_ABORT);
	}

	/* And HUP and TERM are at SIG_DFL from gdm_unset_signals,
	   we also have an empty mask and all that fun stuff */

	/* unblock signals (especially HUP/TERM/USR1) so that we
	 * can control the X server */
	sigemptyset (&mask);
	sigprocmask (SIG_SETMASK, &mask, NULL);

	if (server->priv->priority != 0) {
		if (setpriority (PRIO_PROCESS, 0, server->priv->priority)) {
			g_warning (_("%s: Server priority couldn't be set to %d: %s"),
				   "gdm_server_spawn",
				   server->priv->priority,
				   g_strerror (errno));
		}
	}

	setpgid (0, 0);

	change_user (server);

#if sun
	{
		/* Remove old communication pipe, if present */
		char old_pipe[MAXPATHLEN];

		sprintf (old_pipe, "%s/%d", SDTLOGIN_DIR, server->priv->display_name);
		g_unlink (old_pipe);
	}
#endif
}

static void
listify_hash (const char *key,
	      const char *value,
	      GPtrArray  *env)
{
	char *str;
	str = g_strdup_printf ("%s=%s", key, value);
	g_ptr_array_add (env, str);
}

static GPtrArray *
get_server_environment (GdmServer *server)
{
	GPtrArray  *env;
	char      **l;
	GHashTable *hash;

	env = g_ptr_array_new ();

	/* create a hash table of current environment, then update keys has necessary */
	hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
	for (l = environ; *l != NULL; l++) {
		char **str;
		str = g_strsplit (*l, "=", 2);
		g_hash_table_insert (hash, str[0], str[1]);
	}

	/* modify environment here */
	if (server->priv->is_parented) {
		if (server->priv->parent_auth_file != NULL) {
			g_hash_table_insert (hash, g_strdup ("XAUTHORITY"), g_strdup (server->priv->parent_auth_file));
		}

		if (server->priv->parent_display_name != NULL) {
			g_hash_table_insert (hash, g_strdup ("DISPLAY"), g_strdup (server->priv->parent_display_name));
		}
	} else {
		g_hash_table_insert (hash, g_strdup ("DISPLAY="), g_strdup (server->priv->display_name));
	}

	if (server->priv->uid != 0) {
		struct passwd *pwent;

		pwent = getpwuid (server->priv->uid);

		if (pwent->pw_dir != NULL
		    && g_file_test (pwent->pw_dir, G_FILE_TEST_EXISTS)) {
			g_hash_table_insert (hash, g_strdup ("HOME"), g_strdup (pwent->pw_dir));
		} else {
			/* Hack */
			g_hash_table_insert (hash, g_strdup ("HOME"), g_strdup ("/"));
		}
		g_hash_table_insert (hash, g_strdup ("SHELL"), g_strdup (pwent->pw_shell));
		g_hash_table_remove (hash, "MAIL");
	}

	g_hash_table_foreach (hash, (GHFunc)listify_hash, env);
	g_hash_table_destroy (hash);

	g_ptr_array_add (env, NULL);

	return env;
}

static void
server_add_xserver_args (GdmServer *server,
			 int       *argc,
			 char    ***argv)
{
	int    count;
	char **args;
	int    len;
	int    i;

	len = *argc;
	g_shell_parse_argv (server->priv->session_args, &count, &args, NULL);
	*argv = g_renew (char *, *argv, len + count + 1);

	for (i=0; i < count;i++) {
		*argv[len++] = g_strdup (args[i]);
	}

	*argc += count;

	argv[len] = NULL;
	g_strfreev (args);
}

static gboolean
gdm_server_spawn (GdmServer  *server,
		  const char *vtarg)
{
	int              argc;
	gchar          **argv = NULL;
	GError          *error;
	GPtrArray       *env;
	gboolean         ret;

	ret = FALSE;

	/* Figure out the server command */
	argv = NULL;
	argc = 0;
	gdm_server_resolve_command_line (server,
					 vtarg,
					 &argc,
					 &argv);

	if (server->priv->session_args) {
		server_add_xserver_args (server, &argc, &argv);
	}

	if (argv[0] == NULL) {
		g_warning (_("%s: Empty server command for display %s"),
			   "gdm_server_spawn",
			   server->priv->display_name);
		_exit (SERVER_ABORT);
	}

	env = get_server_environment (server);

	g_debug ("Starting X server process");
	error = NULL;
	ret = g_spawn_async_with_pipes (NULL,
					argv,
					(char **)env->pdata,
					G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
					(GSpawnChildSetupFunc)server_child_setup,
					server,
					&server->priv->pid,
					NULL,
					NULL,
					NULL,
					&error);

	if (! ret) {
		g_warning ("Could not start command '%s': %s",
			   server->priv->command,
			   error->message);
		g_error_free (error);
	}

	g_strfreev (argv);
	g_ptr_array_foreach (env, (GFunc)g_free, NULL);
        g_ptr_array_free (env, TRUE);

	return ret;
}

/**
 * gdm_server_start:
 * @disp: Pointer to a GdmDisplay structure
 *
 * Starts a local X server. Handles retries and fatal errors properly.
 */

gboolean
gdm_server_start (GdmServer *server)
{
	char *vtarg = NULL;
	int vtfd = -1;
	int vt = -1;
	gboolean res;

#if 0
	if (d->type == TYPE_XDMCP_PROXY &&
	    ! connect_to_parent (d))
		return FALSE;
#endif

#if 0
	if (d->type == TYPE_STATIC ||
	    d->type == TYPE_FLEXI) {
		vtarg = gdm_get_empty_vt_argument (&vtfd, &vt);
	}
#endif

	/* fork X server process */
	res = gdm_server_spawn (server, vtarg);

#if 0
	/* If we were holding a vt open for the server, close it now as it has
	 * already taken the bait. */
	if (vtfd > 0) {
		VE_IGNORE_EINTR (close (vtfd));
	}
#endif

	return res;
}

static void
_gdm_server_set_display_name (GdmServer  *server,
			      const char *name)
{
        g_free (server->priv->display_name);
        server->priv->display_name = g_strdup (name);
}

static void
gdm_server_set_property (GObject      *object,
			guint	       prop_id,
			const GValue *value,
			GParamSpec   *pspec)
{
	GdmServer *self;

	self = GDM_SERVER (object);

	switch (prop_id) {
	case PROP_DISPLAY_NAME:
		_gdm_server_set_display_name (self, g_value_get_string (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
gdm_server_get_property (GObject    *object,
				 guint       prop_id,
				 GValue	    *value,
				 GParamSpec *pspec)
{
	GdmServer *self;

	self = GDM_SERVER (object);

	switch (prop_id) {
	case PROP_DISPLAY_NAME:
		g_value_set_string (value, self->priv->display_name);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static GObject *
gdm_server_constructor (GType                  type,
		       guint                  n_construct_properties,
		       GObjectConstructParam *construct_properties)
{
        GdmServer      *server;
        GdmServerClass *klass;

        klass = GDM_SERVER_CLASS (g_type_class_peek (GDM_TYPE_SERVER));

        server = GDM_SERVER (G_OBJECT_CLASS (gdm_server_parent_class)->constructor (type,
										    n_construct_properties,
										    construct_properties));
        return G_OBJECT (server);
}

static void
gdm_server_class_init (GdmServerClass *klass)
{
	GObjectClass    *object_class = G_OBJECT_CLASS (klass);

	object_class->get_property = gdm_server_get_property;
	object_class->set_property = gdm_server_set_property;
        object_class->constructor = gdm_server_constructor;
	object_class->finalize = gdm_server_finalize;

	g_type_class_add_private (klass, sizeof (GdmServerPrivate));

	g_object_class_install_property (object_class,
					 PROP_DISPLAY_NAME,
					 g_param_spec_string ("display-name",
							      "name",
							      "name",
							      NULL,
							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}

static void
gdm_server_init (GdmServer *server)
{

	server->priv = GDM_SERVER_GET_PRIVATE (server);

	server->priv->pid = -1;
}

static void
gdm_server_finalize (GObject *object)
{
	GdmServer *server;

	g_return_if_fail (object != NULL);
	g_return_if_fail (GDM_IS_SERVER (object));

	server = GDM_SERVER (object);

	g_return_if_fail (server->priv != NULL);

	G_OBJECT_CLASS (gdm_server_parent_class)->finalize (object);
}

GdmServer *
gdm_server_new (const char *display_name)
{
	GObject *object;

	object = g_object_new (GDM_TYPE_SERVER,
			       "display-name", display_name,
			       NULL);

	return GDM_SERVER (object);
}