summaryrefslogtreecommitdiffstats
path: root/tdm/kfrontend/tdm_greet.c
blob: c90ebc1c965c0734a1bd927cce36075b7439e43c (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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/*

TDE Greeter module for xdm

Copyright (C) 2001-2003 Oswald Buddenhagen <ossi@kde.org>

This file contains code from the old xdm core,
Copyright 1988, 1998  Keith Packard, MIT X Consortium/The Open Group

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.

*/

#include <config.h>

#include "tdm_greet.h"
#include "tdmconfig.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>
#include <setjmp.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifdef _POSIX_PRIORITY_SCHEDULING
# include <sched.h>
#endif

# include <X11/Xlib.h>
#if defined(HAVE_XTEST) || defined(HAVE_XKB)
# include <X11/keysym.h>
#endif

#ifdef HAVE_XTEST
# include <X11/extensions/XTest.h>
#endif

#ifdef HAVE_XKB
# include <X11/XKBlib.h>
#endif

extern void LogOutOfMem( void );

static void *
Realloc( void *ptr, size_t size )
{
	void *ret;

	if (!(ret = realloc( ptr, size )) && size)
		LogOutOfMem();
	return ret;
}

#define PRINT_QUOTES
#define PRINT_ARRAYS
#define LOG_NAME "tdm_greet"
#define LOG_DEBUG_MASK DEBUG_GREET
#define LOG_PANIC_EXIT 1
#define STATIC
#include <printf.c>

static void
GDebug( const char *fmt, ... )
{
	va_list args;

	if (debugLevel & DEBUG_HLPCON) {
		va_start( args, fmt );
		Logger( DM_DEBUG, fmt, args );
		va_end( args );
	}
}


char *dname;

int rfd;
static int wfd, mrfd, mwfd, srfd, swfd;
static const char *who;

void
GSet( int master )
{
	if (master)
		rfd = mrfd, wfd = mwfd, who = "core (master)";
	else
		rfd = srfd, wfd = swfd, who = "core";

}

static int
Reader( void *buf, int count )
{
	int ret, rlen;

	for (rlen = 0; rlen < count; ) {
	  dord:
		ret = read( rfd, (void *)((char *)buf + rlen), count - rlen );
		if (ret < 0) {
			if (errno == EINTR)
				goto dord;
			if (errno == EAGAIN)
				break;
			return -1;
		}
		if (!ret)
			break;
		rlen += ret;
	}
	return rlen;
}

static void
GRead( void *buf, int count )
{
	if (Reader( buf, count ) != count)
		LogPanic( "Can't read from %s\n", who );
}

static void
GWrite( const void *buf, int count )
{
	if (write( wfd, buf, count ) != count)
		LogPanic( "Can't write to %s\n", who );
#ifdef _POSIX_PRIORITY_SCHEDULING
	if ((debugLevel & DEBUG_HLPCON))
		sched_yield();
#endif
}

void
GSendInt( int val )
{
	GDebug( "Sending int %d (%#x) to %s\n", val, val, who );
	GWrite( &val, sizeof(val) );
}

void
GSendStr( const char *buf )
{
	int len = buf ? strlen( buf ) + 1 : 0;
	GDebug( "Sending string %'s to %s\n", buf, who );
	GWrite( &len, sizeof(len) );
	GWrite( buf, len );
}

/*
static void
GSendNStr( const char *buf, int len )
{
	int tlen = len + 1;
	GDebug( "Sending string %'.*s to %s\n", len, buf, who );
	GWrite( &tlen, sizeof(tlen) );
	GWrite( buf, len );
	GWrite( "", 1 );
}
*/

void
GSendArr( int len, const char *buf )
{
	GDebug( "Sending array %02[:*hhx to %s\n", len, buf, who );
	GWrite( &len, sizeof(len) );
	GWrite( buf, len );
}

int
GRecvInt()
{
	int val;

	GDebug( "Receiving int from %s ...\n", who );
	GRead( &val, sizeof(val) );
	GDebug( " -> %d (%#x)\n", val, val );
	return val;
}

static char *
iGRecvArr( int *rlen )
{
	int len;
	char *buf;

	GRead( &len, sizeof(len) );
	*rlen = len;
	GDebug( " -> %d bytes\n", len );
	if (!len)
		return (char *)0;
	if (!(buf = malloc( len )))
		LogPanic( "No memory for read buffer\n" );
	GRead( buf, len );
	return buf;
}

char *
GRecvStr()
{
	int len;
	char *buf;

	GDebug( "Receiving string from %s ...\n", who );
	buf = iGRecvArr( &len );
	GDebug( " -> %'.*s\n", len, buf );
	return buf;
}

char **
GRecvStrArr( int *rnum )
{
	int num;
	char **argv, **cargv;

	GDebug( "Receiving string array from %s ...\n", who );
	GRead( &num, sizeof(num) );
	GDebug( " -> %d strings\n", num );
	if (rnum)
		*rnum = num;
	if (!num)
		return (char **)0;
	if (!(argv = malloc( num * sizeof(char *))))
		LogPanic( "No memory for read buffer\n" );
	for (cargv = argv; --num >= 0; cargv++)
		*cargv = GRecvStr();
	return argv;
}

char *
GRecvArr( int *num )
{
	char *arr;

	GDebug( "Receiving array from %s ...\n", who );
	GRead( num, sizeof(*num) );
	GDebug( " -> %d bytes\n", *num );
	if (!*num)
		return (char *)0;
	if (!(arr = malloc( *num )))
		LogPanic( "No memory for read buffer\n" );
	GRead( arr, *num );
	GDebug( " -> %02[*hhx\n", *num, arr );
	return arr;
}

static void
ReqCfg( int id )
{
	GSendInt( G_GetCfg );
	GSendInt( id );
	switch (GRecvInt()) {
	case GE_NoEnt:
		LogPanic( "Config value %#x not available\n", id );
	case GE_BadType:
		LogPanic( "Core does not know type of config value %#x\n", id );
	}
}

int
GetCfgInt( int id )
{
	ReqCfg( id );
	return GRecvInt();
}

char *
GetCfgStr( int id )
{
	ReqCfg( id );
	return GRecvStr();
}

char **
GetCfgStrArr( int id, int *len )
{
	ReqCfg( id );
	return GRecvStrArr( len );
}

static void
disposeSession( dpySpec *sess )
{
	free( sess->display );
	free( sess->from );
	if (sess->user)
		free( sess->user );
	if (sess->session)
		free( sess->session );
}

dpySpec *
fetchSessions( int flags )
{
	dpySpec *sess, *sessions = 0, tsess;

	GSet( 1 );
	GSendInt( G_List );
	GSendInt( flags );
  next:
	while ((tsess.display = GRecvStr())) {
		tsess.from = GRecvStr();
#ifdef HAVE_VTS
		tsess.vt = GRecvInt();
#endif
		tsess.user = GRecvStr();
		tsess.session = GRecvStr();
		tsess.flags = GRecvInt();
		if ((tsess.flags & isTTY) && *tsess.from)
			for (sess = sessions; sess; sess = sess->next)
				if (sess->user && !strcmp( sess->user, tsess.user ) &&
				    !strcmp( sess->from, tsess.from ))
				{
					sess->count++;
					disposeSession( &tsess );
					goto next;
				}
		if (!(sess = malloc( sizeof(*sess) )))
			LogPanic( "Out of memory\n" );
		tsess.count = 1;
		tsess.next = sessions;
		*sess = tsess;
		sessions = sess;
	}
	GSet( 0 );
	return sessions;
}

void
disposeSessions( dpySpec *sess )
{
	while (sess) {
		dpySpec *nsess = sess->next;
		disposeSession( sess );
		free( sess );
		sess = nsess;
	}
}

void
freeStrArr( char **arr )
{
	char **tarr;

	if (arr) {
		for (tarr = arr; *tarr; tarr++)
			free( *tarr );
		free( arr );
	}
}


static int
ignoreErrors( Display *dpy ATTR_UNUSED, XErrorEvent *event ATTR_UNUSED )
{
	Debug( "ignoring X error\n" );
	return 0;
}

/*
 * this is mostly bogus -- but quite useful.  I wish the protocol
 * had some way of enumerating and identifying clients, that way
 * this code wouldn't have to be this kludgy.
 */

static void
killWindows( Display *dpy, Window window )
{
	Window root, parent, *children;
	unsigned child, nchildren = 0;

	while (XQueryTree( dpy, window, &root, &parent, &children, &nchildren )
	       && nchildren > 0)
	{
		for (child = 0; child < nchildren; child++) {
			Debug( "XKillClient 0x%lx\n", (unsigned long)children[child] );
			XKillClient( dpy, children[child] );
		}
		XFree( (char *)children );
	}
}

static jmp_buf resetJmp;

static void
abortReset( int n ATTR_UNUSED )
{
	longjmp (resetJmp, 1);
}

/*
 * this display connection better not have any windows...
 */

static void
pseudoReset( Display *dpy )
{
	int screen;

	if (setjmp( resetJmp )) {
		LogError( "pseudoReset timeout\n" );
	} else {
		(void)signal( SIGALRM, abortReset );
		(void)alarm( 30 );
		XSetErrorHandler( ignoreErrors );
		for (screen = 0; screen < ScreenCount( dpy ); screen++) {
			Debug( "pseudoReset screen %d\n", screen );
			killWindows( dpy, RootWindow( dpy, screen ) );
		}
		Debug( "before XSync\n" );
		XSync( dpy, False );
		(void)alarm( 0 );
	}
	signal( SIGALRM, SIG_DFL );
	XSetErrorHandler( (XErrorHandler)0 );
	Debug( "pseudoReset done\n" );
}


static jmp_buf syncJump;

static void
syncTimeout( int n ATTR_UNUSED )
{
	longjmp( syncJump, 1 );
}

void
SecureDisplay( Display *dpy )
{
	Debug( "SecureDisplay %s\n", dname );
	(void)signal( SIGALRM, syncTimeout );
	if (setjmp( syncJump )) {
		LogError( "Display %s could not be secured\n", dname );
		exit( EX_RESERVER_DPY );
	}
	(void)alarm( (unsigned)_grabTimeout );
	Debug( "Before XGrabServer %s\n", dname );
	XGrabServer( dpy );
	Debug( "XGrabServer succeeded %s\n", dname );
	if (XGrabKeyboard( dpy, DefaultRootWindow( dpy ), True, GrabModeAsync,
	                   GrabModeAsync, CurrentTime ) != GrabSuccess)
	{
		(void)alarm( 0 );
		(void)signal( SIGALRM, SIG_DFL );
		LogError( "Keyboard on display %s could not be secured\n", dname );
		sleep( 10 );
		exit( EX_RESERVER_DPY );
	}
	(void)alarm( 0 );
	(void)signal( SIGALRM, SIG_DFL );
	pseudoReset( dpy );
	if (!_grabServer)
	{
		XUngrabServer( dpy );
		XSync( dpy, 0 );
	}
	Debug( "done secure %s\n", dname );
#ifdef HAVE_XKBSETPERCLIENTCONTROLS
	/*
	 * Activate the correct mapping for modifiers in XKB extension as
	 * grabbed keyboard has its own mapping by default
	 */
	{
		int opcode, evbase, errbase, majret, minret;
		unsigned int value = XkbPCF_GrabsUseXKBStateMask;
		if (XkbQueryExtension( dpy, &opcode, &evbase,
		                       &errbase, &majret, &minret ))
			XkbSetPerClientControls( dpy, value, &value );
	}
#endif
}

void
UnsecureDisplay( Display *dpy )
{
	Debug( "Unsecure display %s\n", dname );
	if (_grabServer) {
		XUngrabServer( dpy );
		XSync( dpy, 0 );
	}
}

static jmp_buf pingTime;

static int
PingLostIOErr( Display *dpy ATTR_UNUSED )
{
	longjmp( pingTime, 1 );
}

static void
PingLostSig( int n ATTR_UNUSED )
{
	longjmp( pingTime, 1 );
}

int
PingServer( Display *dpy )
{
	int (*oldError)( Display * );
	void (*oldSig)( int );
	int oldAlarm;

	oldError = XSetIOErrorHandler( PingLostIOErr );
	oldAlarm = alarm( 0 );
	oldSig = signal( SIGALRM, PingLostSig );
	(void)alarm( _pingTimeout * 60 );
	if (!setjmp( pingTime )) {
		Debug( "Ping server\n" );
		XSync( dpy, 0 );
	} else {
		Debug( "Server dead\n" );
		(void)alarm( 0 );
		(void)signal( SIGALRM, SIG_DFL );
		XSetIOErrorHandler( oldError );
		return 0;
	}
	(void)alarm( 0 );
	(void)signal( SIGALRM, oldSig );
	(void)alarm( oldAlarm );
	Debug( "Server alive\n" );
	XSetIOErrorHandler( oldError );
	return 1;
}

/*
 * Modifier changing code based on tdebase/kxkb/kcmmisc.cpp
 *
 * XTest part: Copyright (C) 2000-2001 Lubos Lunak <l.lunak@kde.org>
 * XKB part:   Copyright (C) 2001-2002 Oswald Buddenhagen <ossi@kde.org>
 *
 */

#ifdef HAVE_XKB
static int
xkb_init( Display *dpy )
{
	int xkb_opcode, xkb_event, xkb_error;
	int xkb_lmaj = XkbMajorVersion;
	int xkb_lmin = XkbMinorVersion;
	return XkbLibraryVersion( &xkb_lmaj, &xkb_lmin ) &&
	       XkbQueryExtension( dpy, &xkb_opcode, &xkb_event,
	                          &xkb_error, &xkb_lmaj, &xkb_lmin );
}

static unsigned int
xkb_modifier_mask_work( XkbDescPtr xkb, const char *name )
{
	int i;

	if (!xkb->names)
		return 0;
	for (i = 0; i < XkbNumVirtualMods; i++) {
		char *modStr = XGetAtomName( xkb->dpy, xkb->names->vmods[i] );
		if (modStr != NULL && strcmp( name, modStr ) == 0) {
			unsigned int mask;
			XkbVirtualModsToReal( xkb, 1 << i, &mask );
			return mask;
		}
	}
	return 0;
}

static unsigned int
xkb_modifier_mask( Display *dpy, const char *name )
{
	XkbDescPtr xkb;

	if ((xkb = XkbGetKeyboard( dpy, XkbAllComponentsMask, XkbUseCoreKbd ))) {
		unsigned int mask = xkb_modifier_mask_work( xkb, name );
		XkbFreeKeyboard( xkb, 0, True );
		return mask;
	}
	return 0;
}

static int
xkb_get_modifier_state( Display *dpy, const char *name )
{
	unsigned int mask;
	XkbStateRec state;

	if (!(mask = xkb_modifier_mask( dpy, name )))
		return 0;
	XkbGetState( dpy, XkbUseCoreKbd, &state );
	return (mask & state.locked_mods) != 0;
}

static int
xkb_set_modifier( Display *dpy, const char *name, int sts )
{
	unsigned int mask;

	if (!(mask = xkb_modifier_mask( dpy, name )))
		return 0;
	XkbLockModifiers( dpy, XkbUseCoreKbd, mask, sts ? mask : 0 );
	return 1;
}
#endif /* HAVE_XKB */

#ifdef HAVE_XTEST
static int
xtest_get_modifier_state( Display *dpy, int key )
{
	XModifierKeymap *map;
	KeyCode modifier_keycode;
	unsigned int i, mask;
	Window dummy1, dummy2;
	int dummy3, dummy4, dummy5, dummy6;

	if ((modifier_keycode = XKeysymToKeycode( dpy, key )) == NoSymbol)
		return 0;
	map = XGetModifierMapping( dpy );
	for (i = 0; i < 8; ++i)
		if (map->modifiermap[map->max_keypermod * i] == modifier_keycode) {
			XFreeModifiermap( map );
			XQueryPointer( dpy, DefaultRootWindow( dpy ),
			               &dummy1, &dummy2, &dummy3, &dummy4, &dummy5, &dummy6,
			               &mask );
			return (mask & (1 << i)) != 0;
		}
	XFreeModifiermap( map );
	return 0;
}

static void
xtest_fake_keypress( Display *dpy, int key )
{
	XTestFakeKeyEvent( dpy, XKeysymToKeycode( dpy, key ), True, CurrentTime );
	XTestFakeKeyEvent( dpy, XKeysymToKeycode( dpy, key ), False, CurrentTime );
}
#endif /* HAVE_XTEST */

#ifdef HAVE_XKB
static int havexkb;
#endif
static int nummodified, oldnumstate, newnumstate;
static Display *dpy;

void
setup_modifiers( Display *mdpy, int numlock )
{
	if (numlock == 2)
		return;
	newnumstate = numlock;
	nummodified = 1;
	dpy = mdpy;
#ifdef HAVE_XKB
	if (xkb_init( mdpy )) {
		havexkb = 1;
		oldnumstate = xkb_get_modifier_state( mdpy, "NumLock" );
		xkb_set_modifier( mdpy, "NumLock", numlock );
		return;
	}
#endif
#ifdef HAVE_XTEST
	oldnumstate = xtest_get_modifier_state( mdpy, XK_Num_Lock );
	if (oldnumstate != numlock)
		xtest_fake_keypress( mdpy, XK_Num_Lock );
#endif
}

void
restore_modifiers( void )
{
#ifdef HAVE_XTEST
	int numstat;
#endif

	if (!nummodified)
		return;
#ifdef HAVE_XKB
	if (havexkb) {
		if (xkb_get_modifier_state( dpy, "NumLock" ) == newnumstate)
			xkb_set_modifier( dpy, "NumLock", oldnumstate );
		return;
	}
#endif
#ifdef HAVE_XTEST
	numstat = xtest_get_modifier_state( dpy, XK_Num_Lock );
	if (numstat == newnumstate && newnumstate != oldnumstate)
		xtest_fake_keypress( dpy, XK_Num_Lock );
#endif
}

void
setCursor( Display *mdpy, int window, int shape )
{
	Cursor xcursor;

	if ((xcursor = XCreateFontCursor( mdpy, shape ))) {
		XDefineCursor( mdpy, window, xcursor );
		XFreeCursor( mdpy, xcursor );
		XFlush( mdpy );
	}
}

static void
sigterm( int n ATTR_UNUSED )
{
	exit( EX_NORMAL );
}

static char *savhome;

static void
cleanup( void )
{
	char buf[128];

	if (strcmp( savhome, getenv( "HOME" ) ) || memcmp( savhome, "/tmp/", 5 ))
		LogError( "Internal error: memory corruption detected\n" ); /* no panic: recursion */
	else {
		sprintf( buf, "rm -rf %s", savhome );
		system( buf );
	}
}

extern void kg_main( const char *argv0 );

int
main( int argc ATTR_UNUSED, char **argv )
{
	char *ci;
	int i;
	char qtrc[40];

	if (!(ci = getenv( "CONINFO" ))) {
		fprintf( stderr, "This program is part of tdm and should not be run manually.\n" );
		return 1;
	}
	if (sscanf( ci, "%d %d %d %d", &srfd, &swfd, &mrfd, &mwfd ) != 4)
		return 1;
	fcntl( srfd, F_SETFD, FD_CLOEXEC );
	fcntl( swfd, F_SETFD, FD_CLOEXEC );
	fcntl( mrfd, F_SETFD, FD_CLOEXEC );
	fcntl( mwfd, F_SETFD, FD_CLOEXEC );
	GSet( 0 );

	InitLog();

	if ((debugLevel = GRecvInt()) & DEBUG_WGREET)
		sleep( 100 );

	signal( SIGTERM, sigterm );

	dname = getenv( "DISPLAY" );

	init_config();

	/* for TQSettings */
	srand( time( 0 ) );
	for (i = 0; i < 10000; i++) {
		sprintf( qtrc, "/tmp/%010d", rand() );
		if (!mkdir( qtrc, 0700 ))
			goto okay;
	}
	LogPanic( "Cannot create $HOME\n" );
  okay:
	if (setenv( "HOME", qtrc, 1 ))
		LogPanic( "Cannot set $HOME\n" );
	if (!(savhome = strdup( qtrc )))
		LogPanic( "Cannot save $HOME\n" );
	atexit( cleanup );

	setenv( "LC_ALL", _language, 1 );

	kg_main( argv[0] );

	return EX_NORMAL;
}