This file is indexed.

/usr/sbin/gdm-emulatord is in kdm-gdmcompat 0.13-2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/perl
#
# Copyright (C) 2007, 2008, 2009 Fabian Knittel
# Copyright (C) 2008 Philipp Kern
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use warnings;
use strict;
use POSIX qw(setsid setuid setgid);
use POSIX ":sys_wait_h";
use IO::Socket;
use Sys::Syslog;

# Declare functions
#

sub main();
sub logerror($);
sub loginfo($);
sub logdebug($);
sub daemonize($);
sub my_die($);
sub run_kdmctl($);
sub run_xauth($$);
sub handle_connection($$);
sub handle_abort($);
sub handle_list_servers($$);
sub handle_flexi_xserver($);
sub search_auth_key($$);
sub handle_set_vt($$);
sub handle_get_config($$$);
sub open_socket($);



# Main program
#
main();

# Functions.
#

sub main()
{
	my %CONFIG = (
		'PID_FILE' => '/var/run/gdm-emulatord.pid',
		'GDM_SOCKET_FILE' => '/var/run/gdm_socket',
		'GOTO_BACKGROUND' => 1,
		'DEFAULT_MIN_UID' => 1000,
		'DEFAULT_EXCLUDE_USERS' => ''
	);

	openlog("gdm-emulatord", 'pid', 'user');

	$SIG{'PIPE'} = 'IGNORE';
	$SIG{'QUIT'} = \&handle_abort;
	$SIG{'INT'} = \&handle_abort;
	$SIG{'TERM'} = \&handle_abort;


	my $gdm_socket = open_socket($CONFIG{'GDM_SOCKET_FILE'});

	if ($CONFIG{'GOTO_BACKGROUND'}) {
		daemonize($CONFIG{'PID_FILE'});
	}

	loginfo "Started gdm-emulatord. Will listen on ".
			$CONFIG{'GDM_SOCKET_FILE'};
	while (1) {
		my $conn = $gdm_socket->accept;
		if (!$conn) {
			logerror "Failed to accept connection: $!";
			sleep 5;
			next;
		}

		# Connections are short-lived, so we simply handle them in a
		# blocked fashion.
		handle_connection($conn, \%CONFIG);
	}

	# Never reached.
	exit(0);
}

sub open_socket($)
{
	my $gdm_socket_fn = $_[0];

	unlink $gdm_socket_fn;
	my $gdm_socket;
	defined($gdm_socket = IO::Socket::UNIX->new(Type => SOCK_STREAM,
		                                    Local => $gdm_socket_fn,
						    Listen => 1)) or
		my_die "could not open gdm socket: $!";
	chmod 0666, $gdm_socket_fn or
		my_die "could not set permissions on socket: $!";
	return $gdm_socket;
}

sub handle_connection($$)
{
	my ($conn, $config) = @_;

	my $curr_auth;

	my $line;
	while (defined($line = $conn->getline)) {
		chomp $line;

		if ($line eq 'VERSION') {
			$conn->print("GDM 2.16.0\n");
		} elsif ($line eq 'CONSOLE_SERVERS') {
			handle_list_servers($conn, 'alllocal');
		} elsif ($line eq 'ALL_SERVERS') {
			handle_list_servers($conn, 'all');
		} elsif ($line eq 'CLOSE') {
			last;
		} elsif ($line =~ /^AUTH_LOCAL (.+)/) {
			$curr_auth = search_auth_key($conn, $1);
		} elsif ($line =~ /^SET_SAFE_LOGOUT_ACTION (.+)/) {
			if (!defined($curr_auth)) {
				logerror "Attempted SET_SAFE_LOGOUT_ACTION ".
					"without prior authentication";
				$conn->print("ERROR 100 Not authenticated\n");
			} else {
				# TODO: use this stuff somehow
				$conn->print("OK\n");
			}
		} elsif ($line =~ /^SET_VT (\d+)/) {
			my $vtnum = $1;
			if (!defined($curr_auth)) {
				logerror "Attempted SET_VT without ".
					"prior authentication";
				$conn->print("ERROR 100 Not authenticated\n");
			} else {
				handle_set_vt($conn, $vtnum);
			}
		} elsif ($line =~ /^FLEXI_XSERVER/) {
			if (!defined($curr_auth)) {
				logerror "Attempted FLEXI_XSERVER without ".
					"prior authentication";
				$conn->print("ERROR 100 Not authenticated\n");
			} else {
				handle_flexi_xserver($conn);
			}
		} elsif ($line eq 'QUERY_LOGOUT_ACTION') {
			if (!defined($curr_auth)) {
				logerror "Attempted QUERY_LOGOUT_ACTION ".
					"without prior authentication";
				$conn->print("ERROR 100 Not authenticated\n");
			} else {
				# TODO: Implement.
				$conn->print("OK \n");
			}
		} elsif ($line =~ /^GET_CONFIG (.+)/) {
			handle_get_config($conn, $config, $1);
		} else {
			logerror "Received unknown command: '$line'";
			$conn->print("ERROR 0 Not implemented\n");
		}
	}

	$conn->close;
}

sub run_kdmctl($)
{
	my $param = $_[0];

	if (open(CTL, "/usr/bin/kdmctl $param|")) {
		my $line = <CTL>;
		chomp($line);
		close(CTL);

		my @TOKENS = split(/\t/, $line);
		return @TOKENS;
	} else {
		return;
	}
}

sub run_xauth($$)
{
	my ($xauth_file, $xdisp) = @_;

	if (open(XAUTH, "/usr/bin/xauth -f $xauth_file list $xdisp|")) {
		my $line = <XAUTH>;
		chomp($line);
		close(XAUTH);

		if ($line !~ /^.+$xdisp +MIT-MAGIC-COOKIE-1 +([a-f0-9]+)$/) {
			logerror "xauth output does not match expected output";
			return;
		}
		my $xauthcookie = $1;
		return $xauthcookie;
	} else {
		return;
	}
}

sub handle_set_vt($$)
{
	my $conn = $_[0];
	my $vtnum = $_[1];

	loginfo "Received request to activate vt$vtnum";
	my @TOKENS = run_kdmctl('activate vt'.$vtnum);
	if (@TOKENS) {
		my $result = shift @TOKENS;
		if ($result eq 'ok') {
			$conn->print("OK\n");
			# Avoid DoS by waiting for a moment.
			sleep 7;
		} else {
			logerror "kdmctl failed to provide switch to vt $vtnum";
			$conn->print("ERROR 2 kdmctl failed to switch to ".
			             "vt $vtnum\n");
		}
	} else {
		logerror "Failed to call kdmctl";
		$conn->print("ERROR 2 Failed to call kdmctl\n");
	}
}

sub handle_flexi_xserver($)
{
	my $conn = $_[0];

	loginfo "Received request for new xsession";
	my @TOKENS = run_kdmctl('reserve');
	if (@TOKENS) {
		my $result = shift @TOKENS;
		if ($result eq 'ok') {
			$conn->print("OK\n");
			# Avoid DoS by waiting for a moment.
			sleep 7;
		} else {
			logerror "kdmctl failed to provide ".
				 "new session";
			$conn->print("ERROR 2 kdmctl failed to".
				     " provide new session\n");
		}
	} else {
		logerror "Failed to call kdmctl";
		$conn->print("ERROR 2 Failed to call kdmctl\n");
	}
}

sub search_auth_key($$)
{
	my ($conn, $xauthcookie) = @_;

	my @TOKENS = run_kdmctl("list alllocal");
	if (@TOKENS) {
		my $result = shift @TOKENS;
		if ($result eq 'ok') {
			foreach my $token (@TOKENS) {
				my @ELS = split(/,/, $token);
				my $disp = $ELS[0];
				if ($disp !~ /^:\d+/) {
					# We only care for X displays.
					next;
				}
				if (!defined($ELS[2])) {
					# We need a logged-in user.
					next;
				}
				my $user = $ELS[2];

				my @PW = getpwnam($user);
				if (!@PW) {
					# Somehow, the user has no entry in the
					# system's password database. Skip.
					logdebug "Skipping invalid user $user";
					next;
				}
				my $homedir = $PW[7];

				my $xauth_file = $homedir."/.Xauthority";
				if (!-e $xauth_file) {
					# User has no xauth file, so we can't
					# recover the xauth cookie. Skip.
					logdebug "Skipping user $user without ".
						".Xauthority file";
					next;
				}

				my $user_xauthcookie = run_xauth($xauth_file,
					$disp);
				if (!defined($user_xauthcookie)) {
					# Cookie could not be read
					logerror "X auth cookie reading failed";
					next;
				}
				if ($user_xauthcookie ne $xauthcookie) {
					# Cookie does not match.
					next;
				}

				logdebug "User $user authenticated for ".
					"disp $disp";
				$conn->print("OK\n");
				return { 'user' => $user,
					'disp' => $disp,
					'cookie' => $xauthcookie };
			}
		} else {
			logerror "kdmctl list output could ".
				 "not be parsed";
			$conn->print("ERROR 999 kdmctl list ".
				"output could not be parsed\n");
		}
	} else {
		logerror "Failed to call kdmctl";
		$conn->print("ERROR 999 Failed to call kdmctl\n");
	}

	logerror "Failed to find a user matching the authentication";
	$conn->print("ERROR 100 Not authenticated\n");
	return;
}

sub handle_list_servers($$)
{
	my $conn = $_[0];
	my $type = $_[1];

	my @TOKENS = run_kdmctl("list $type");
	if (@TOKENS) {
		my $result = shift @TOKENS;
		if ($result eq 'ok') {
			my @SERVERS;

			foreach my $server (@TOKENS) {
				my @ELS = split(/,/, $server);
				my $disp = $ELS[0];
				my $vt = $ELS[1];
				if ($vt !~ /^vt(\d+)$/) {
					logerror "could not parse vt info $vt";
					next;
				}
				my $vtnum = $1;
				my $user = defined($ELS[2]) ? $ELS[2] : '';
				push @SERVERS, $disp.",".$user.",".$vtnum;
			}

			my $server_list = join(';', @SERVERS);
			logdebug "Sending consoles $server_list";
			$conn->print("OK $server_list\n");
		} else {
			logerror "kdmctl list output could ".
				 "not be parsed";
			$conn->print("ERROR 999 kdmctl list ".
				"output could not be parsed\n");
		}
	} else {
		logerror "Failed to call kdmctl";
		$conn->print("ERROR 999 Failed to call kdmctl\n");
	}
}

sub handle_get_config($$$)
{
	my ($conn, $config, $var) = @_;

	logdebug "Request for variable $var";
	if ($var eq 'greeter/MinimalUID') {
		my $min_uid = ${$config}{'DEFAULT_MIN_UID'};

		logdebug "Sending minimal UID $min_uid";
		$conn->print("OK $min_uid\n");
	} elsif ($var eq 'greeter/Exclude') {
		my $excl_users = ${$config}{'DEFAULT_EXCLUDE_USERS'};

		logdebug "Sending excluded users \"$excl_users\"";
		$conn->print("OK $excl_users\n");
	} else {
		logdebug "Declining request for variable $var";
		$conn->print("ERROR 50 Unsupported key <$var>\n");
	}
}

sub daemonize($)
{
	my $pid_file = $_[0];

	chdir('/') or my_die "Can't chdir to /: $!";

	open(STDIN, '/dev/null') or my_die "Can't read /dev/null: $!";
	open(STDOUT, '>>/dev/null') or my_die "Can't write to /dev/null: $!";
	open(STDERR, '>>/dev/null') or my_die "Can't write to /dev/null: $!";

	my $pid;
	defined($pid = fork()) or my_die "Can't fork: $!";

	if ($pid > 0) {
		# We're the parent.
		open(PID, ">$pid_file") or
			my_die "Can't write pid file $pid_file: $!\n";
		print PID $pid."\n";
		close(PID);
		exit 0;
	}

	setsid() or my_die "Can't start a new session: $!";
}

sub my_die($)
{
	logerror "fatal: ".$_[0];
	closelog();
	exit 1;
}

sub logerror($)
{
	syslog('err', $_[0]);
}

sub loginfo($)
{
	syslog('info', $_[0]);
}

sub logdebug($)
{
	syslog('debug', $_[0]);
}

sub handle_abort($)
{
	my $sig = $_[0];

	loginfo("gdm-emulatord received signal $sig.");
	exit(1);
}