summaryrefslogtreecommitdiffstats
path: root/kdmlib/kgreet_pam.cpp
blob: b439524881d29e1409e225b89f09c8c5a8f4c0b4 (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
/*

Conversation widget for kdm greeter

Copyright (C) 2008 Dirk Mueller <mueller@kde.org>

based on classic kdm greeter:

  Copyright (C) 1997, 1998, 2000 Steffen Hansen <hansen@kde.org>
  Copyright (C) 2000-2003 Oswald Buddenhagen <ossi@kde.org>


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 "kgreet_pam.h"
#include "themer/kdmthemer.h"
#include "themer/kdmlabel.h"

#include <klocale.h>
#include <klineedit.h>
#include <kpassdlg.h>
#include <kuser.h>

#include <tqregexp.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqtimer.h>

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <syslog.h>

//#define PAM_GREETER_DEBUG

class KDMPasswordEdit : public KPasswordEdit {
public:
	KDMPasswordEdit( TQWidget *parent ) : KPasswordEdit( parent, 0 ) {}
	KDMPasswordEdit( KPasswordEdit::EchoModes echoMode, TQWidget *parent ) : KPasswordEdit( echoMode, parent, 0 ) {}
protected:
	virtual void contextMenuEvent( TQContextMenuEvent * ) {}
};

static FILE* log;
static void debug(const char* fmt, ...)
{
    va_list lst;
    va_start(lst, fmt);

#ifdef PAM_GREETER_DEBUG
#if 0
    vfprintf(log, fmt, lst);
    fflush(log);
#else
    char buf[6000];
    sprintf(buf, "*** %s\n", fmt);
    vsyslog(LOG_WARNING, buf, lst);
#endif
#endif
    va_end(lst);
}

static KPasswordEdit::EchoModes echoMode;

KPamGreeter::KPamGreeter( KGreeterPluginHandler *_handler,
                                  KdmThemer *themer,
                                  TQWidget *parent, TQWidget *pred,
                                  const TQString &_fixedEntity,
                                  Function _func, Context _ctx ) :
	TQObject(),
	KGreeterPlugin( _handler ),
	fixedUser( _fixedEntity ),
	func( _func ),
	ctx( _ctx ),
	exp( -1 ),
	pExp( -1 ),
	running( false )
{
        ctx = Login;

        debug("KPamGreeter constructed\n");

	m_parentWidget = parent;

	KdmItem *user_entry = 0, *pw_entry = 0;
	int line = 0;

	layoutItem = 0;

	if (themer &&
	    (!(user_entry = themer->findNode( "user-entry" )) ||
	     !(pw_entry = themer->findNode( "pw-entry" ))))
		themer = 0;

        m_themer = themer;

	if (!themer)
		layoutItem = new TQGridLayout( 0, 0, 10 );

        loginLabel = 0;
        authLabel.clear();
        authEdit.clear();
	loginLabel = 0;
	loginEdit = 0;
	if (ctx == ExUnlock || ctx == ExChangeTok)
		fixedUser = KUser().loginName();
	if (func != ChAuthTok) {
		debug("func != ChAuthTok\n");
		debug("fixedUser: *%s*\n", fixedUser.latin1());

		if (fixedUser.isEmpty()) {
			loginEdit = new KLineEdit( parent );
			loginEdit->setContextMenuEnabled( false );
			connect( loginEdit, TQT_SIGNAL(lostFocus()), TQT_SLOT(slotLoginLostFocus()) );
			connect( loginEdit, TQT_SIGNAL(lostFocus()), TQT_SLOT(slotActivity()) );
			connect( loginEdit, TQT_SIGNAL(textChanged( const TQString & )), TQT_SLOT(slotActivity()) );
			connect( loginEdit, TQT_SIGNAL(selectionChanged()), TQT_SLOT(slotActivity()) );
			if (pred) {
				parent->setTabOrder( pred, loginEdit );
				pred = loginEdit;
			}
			if (!getLayoutItem()) {
				loginEdit->adjustSize();
				user_entry->setWidget( loginEdit );
			} else {
				loginLabel = new TQLabel( loginEdit, i18n("Username:"), parent );
				getLayoutItem()->addWidget( loginLabel, line, 0 );
				getLayoutItem()->addWidget( loginEdit, line++, 1 );
			}
		} else if (ctx != Login && ctx != Shutdown && getLayoutItem()) {
			loginLabel = new TQLabel( i18n("Username:"), parent );
			getLayoutItem()->addWidget( loginLabel, line, 0 );
			getLayoutItem()->addWidget( new TQLabel( fixedUser, parent ), line++, 1 );
		}
#if 0
		if (echoMode == -1)
			passwdEdit = new KDMPasswordEdit( parent );
		else
			passwdEdit = new KDMPasswordEdit( echoMode,
			                                  parent );
		connect( passwdEdit, TQT_SIGNAL(textChanged( const TQString & )),
		         TQT_SLOT(slotActivity()) );
		connect( passwdEdit, TQT_SIGNAL(lostFocus()), TQT_SLOT(slotActivity()) );
		if (pred) {
			parent->setTabOrder( pred, passwdEdit );
			pred = passwdEdit;
		}
		if (!getLayoutItem()) {
			passwdEdit->adjustSize();
			pw_entry->setWidget( passwdEdit );
		} else {
			passwdLabel = new TQLabel( passwdEdit,
			                          func == Authenticate ?
			                            i18n("hello &Password:") :
			                            i18n("Current &password:"),
			                          parent );
			getLayoutItem()->addWidget( passwdLabel, line, 0 );
			getLayoutItem()->addWidget( passwdEdit, line++, 1 );
		}
#endif
		if (loginEdit)
			loginEdit->setFocus();
	}
	if (func != Authenticate) {
		if (echoMode == -1) {
                        authEdit << new KDMPasswordEdit( echoMode, parent );
			authEdit << new KDMPasswordEdit( echoMode, parent );
		} else {
			authEdit << new KDMPasswordEdit( parent );
			authEdit << new KDMPasswordEdit( parent );
		}
		authLabel << new TQLabel( authEdit[0], i18n("&New password:"), parent );
		authLabel << new TQLabel( authEdit[1], i18n("Con&firm password:"), parent );
		if (pred) {
			parent->setTabOrder( pred, authEdit[0] );
			parent->setTabOrder( authEdit[0], authEdit[1] );
		}
		if (getLayoutItem()) {
			getLayoutItem()->addWidget( authLabel[0], line, 0 );
			getLayoutItem()->addWidget( authEdit[0], line++, 1 );
			getLayoutItem()->addWidget( authLabel[1], line, 0 );
			getLayoutItem()->addWidget( authEdit[1], line, 1 );
		}
		if (authEdit.size() >= 2)
                        authEdit[1]->setFocus();
	}
}

// virtual
KPamGreeter::~KPamGreeter()
{
        debug("KPamGreeter::~KPamGreeter");
	abort();
	if (!layoutItem) {
		delete loginEdit;
		return;
	}
	TQLayoutIterator it = static_cast<TQLayout *>(layoutItem)->iterator();
	for (TQLayoutItem *itm = it.current(); itm; itm = ++it)
		 delete itm->widget();
	delete layoutItem;
        debug("destructor finished, good bye");
}

void // virtual
KPamGreeter::loadUsers( const TQStringList &users )
{
	KCompletion *userNamesCompletion = new KCompletion;
	userNamesCompletion->setItems( users );
	loginEdit->setCompletionObject( userNamesCompletion );
	loginEdit->setAutoDeleteCompletionObject( true );
	loginEdit->setCompletionMode( KGlobalSettings::CompletionAuto );
}

void // virtual
KPamGreeter::presetEntity( const TQString &entity, int field )
{
        debug("presetEntity(%s,%d) called!\n", entity.latin1(), field);
	loginEdit->setText( entity );
	if (field == 1 && authEdit.size() >= 1)
		authEdit[0]->setFocus();
	else {
		loginEdit->setFocus();
		loginEdit->selectAll();
		if (field == -1 && authEdit.size() >= 1) {
			authEdit[0]->setText( "     " );
			authEdit[0]->setEnabled( false );
			authTok = false;
		}
	}
	curUser = entity;
}

TQString // virtual
KPamGreeter::getEntity() const
{
	return fixedUser.isEmpty() ? loginEdit->text() : fixedUser;
}

void // virtual
KPamGreeter::setUser( const TQString &user )
{
	// assert( fixedUser.isEmpty() );
	curUser = user;
	loginEdit->setText( user );
        if (authEdit.size() >= 1) {
	    authEdit[0]->setFocus();
 	    authEdit[0]->selectAll();
        }
}

void // virtual
KPamGreeter::setEnabled(bool enable)
{
	// assert( !passwd1Label );
	// assert( func == Authenticate && ctx == Shutdown );
//	if (loginLabel)
//		loginLabel->setEnabled( enable );
		authEdit[0]->setEnabled( enable );
		setActive( enable );
		if (enable)
			authEdit[0]->setFocus();
	}

void // private
KPamGreeter::returnData()
{
       debug("*************** returnData called with exp %d\n", exp);


	switch (exp) {
	case 0:
		handler->gplugReturnText( (loginEdit ? loginEdit->text() :
						       fixedUser).local8Bit(),
					  KGreeterPluginHandler::IsUser );
		break;
	case 1:
		handler->gplugReturnText( authEdit[0]->password(),
					  KGreeterPluginHandler::IsPassword |
					  KGreeterPluginHandler::IsSecret );
		break;
	case 2:
		handler->gplugReturnText( authEdit[1]->password(),
					  KGreeterPluginHandler::IsSecret );
		break;
	default: // case 3:
		handler->gplugReturnText( authEdit[2]->password(),
					  KGreeterPluginHandler::IsNewPassword |
					  KGreeterPluginHandler::IsSecret );
		break;
	}
}

bool // virtual
KPamGreeter::textMessage( const char *text, bool err )
{
    debug(" ************** textMessage(%s, %d)\n", text, err);

    if (!authEdit.size())
	    return false;

    if (getLayoutItem()) {
      TQLabel* label = new TQLabel(TQString::fromUtf8(text), m_parentWidget);
      getLayoutItem()->addWidget(label, state+1, 0, 0);
    }

    return true;
}

void // virtual
KPamGreeter::textPrompt( const char *prompt, bool echo, bool nonBlocking )
{
    debug("textPrompt called with prompt %s echo %d nonBlocking %d", prompt, echo, nonBlocking);
    debug("state is %d, authEdit.size is %d\n", state, authEdit.size());

    if (state == 0 && echo) {
        if (loginLabel)
	    loginLabel->setText(TQString::fromUtf8(prompt));
        else if (m_themer) {
            KdmLabel *kdmlabel = static_cast<KdmLabel*>(m_themer->findNode("user-label"));
            if (kdmlabel) {
                //userLabel->setText(TQString::fromUtf8(prompt));
                kdmlabel->label.text = TQString::fromUtf8(prompt);
                TQTimer::singleShot(0, kdmlabel, TQT_SLOT(update()));
            }
        }
    }
    else if (state >= authEdit.size()) {
	if (getLayoutItem()) {
   	    TQLabel* label = new TQLabel(TQString::fromUtf8(prompt), m_parentWidget);
	    getLayoutItem()->addWidget(label, state+1, 0, 0);
            debug("added label widget to layout");
        }
        else if (m_themer) {
            debug("themer found!");
	    KdmItem *pw_label = 0;

            KdmLabel *kdmlabel = static_cast<KdmLabel*>(m_themer->findNode("pw-label"));
            if (kdmlabel) {
                //userLabel->setText(TQString::fromUtf8(prompt));
                TQString str = TQString::fromUtf8(prompt);
                kdmlabel->label.text = str;
                TQTimer::singleShot(0, kdmlabel, TQT_SLOT(update()));
            }
        }

	KDMPasswordEdit* passwdEdit;

	if (echoMode == -1)
	    passwdEdit = new KDMPasswordEdit( m_parentWidget );
	else
	    passwdEdit = new KDMPasswordEdit( echoMode, m_parentWidget);
	connect( passwdEdit, TQT_SIGNAL(textChanged( const TQString & )),
		TQT_SLOT(slotActivity()) );
	connect( passwdEdit, TQT_SIGNAL(lostFocus()), TQT_SLOT(slotActivity()) );
	authEdit << passwdEdit;

#if 1
       for(TQValueList<KPasswordEdit*>::iterator it = authEdit.begin();
        it != authEdit.end();
        ++it) {
       if ((*it)->isEnabled() && (*it)->text().isEmpty()) {
          (*it)->setFocus();
          break;
        }
       }
#endif
       if (getLayoutItem())
	   getLayoutItem()->addWidget(passwdEdit, state+1, 1, 0);

       if (m_themer) {
           debug("themer found!");
	   KdmItem *pw_entry = 0;

	   pw_entry = m_themer->findNode("pw-entry");

	   if (pw_entry && passwdEdit)
	       pw_entry->setWidget(passwdEdit);

           if (0) {
                //userLabel->setText(TQString::fromUtf8(prompt));
                //kdmlabel->label.text = TQString::fromUtf8(prompt);
                //TQTimer::singleShot(0, kdmlabel, TQT_SLOT(update()));
           }
       } 
       else
           debug("no themer found!");
    }
    ++state;
    pExp = exp;

    exp = authEdit.size();
    debug("state %d exp: %d, has %d\n", state, exp, has);

    if (has >= exp || nonBlocking)
	returnData();
}

bool // virtual
KPamGreeter::binaryPrompt( const char *, bool )
{
	// this simply cannot happen ... :}
	return true;
}

void // virtual
KPamGreeter::start()
{
   debug("******* start() called\n");

   while(authEdit.begin() != authEdit.end()) {
       KPasswordEdit* item = *authEdit.remove(authEdit.begin());
       delete item;
   }

   while(authLabel.begin() != authLabel.end()) {
       TQLabel* item = *authLabel.remove(authLabel.begin());
       delete item;
   }

   authTok = !(authEdit.size() >= 2 && authEdit[1]->isEnabled());
   exp = has = -1;
   state = 0;
   running = true;
   handler->gplugStart();
}

void // virtual
KPamGreeter::suspend()
{
}

void // virtual
KPamGreeter::resume()
{
}

void // virtual
KPamGreeter::next()
{
        debug("********* next() called state %d\n", state);

	if (state == 0 && running && handler) {
                debug(" **** returned text!\n");
		handler->gplugReturnText( (loginEdit ? loginEdit->text() :
		                  fixedUser).local8Bit(),
		                          KGreeterPluginHandler::IsUser );
                setActive(false);
        }
	
        has = 0;

    for(TQValueList<KPasswordEdit*>::iterator it = authEdit.begin();
        it != authEdit.end();
        ++it) {

          has++;
        if ((*it)->hasFocus()) {
          ++it;
          if (it != authEdit.end())
              (*it)->setFocus();
          break;
        }
        if (it == authEdit.end())
           has = -1;
   }

   debug(" has %d and exp %d\n", has, exp);

#if 0
	// assert( running );
	if (loginEdit && loginEdit->hasFocus()) {
		passwdEdit->setFocus(); // will cancel running login if necessary
		has = 0;
	} else if (passwdEdit && passwdEdit->hasFocus()) {
		if (passwd1Edit)
			passwd1Edit->setFocus();
		has = 1;
	} else if (passwd1Edit) {
		if (passwd1Edit->hasFocus()) {
			passwd2Edit->setFocus();
			has = 1; // sic!
		} else
			has = 3;
	} else
		has = 1;
	if (exp < 0)
		handler->gplugStart();
#endif
	if (has >= exp)
		returnData();
}

void // virtual
KPamGreeter::abort()
{
	debug("***** abort() called\n");

	running = false;
	if (exp >= 0) {
		exp = -1;
		handler->gplugReturnText( 0, 0 );
	}
}

void // virtual
KPamGreeter::succeeded()
{
        debug("**** succeeded() called\n");

	// assert( running || timed_login );
	if (!authTok)
		setActive( false );
	else
		setAllActive( false );
	exp = -1;
	running = false;
}

void // virtual
KPamGreeter::failed()
{
	// assert( running || timed_login );
	setActive( false );
	setAllActive( false );
	exp = -1;
	running = false;
}

#include<assert.h>
void // virtual
KPamGreeter::revive()
{
	// assert( !running );
	setAllActive( true );

#if 1
        if (authEdit.size()  < 1)
                return;
#endif

        assert(authEdit.size() >= 1);
	if (authTok) {
		authEdit[0]->erase();
                if(authEdit.size() >= 2)
			authEdit[1]->erase();
		authEdit[0]->setFocus();
	} else {
		authEdit[0]->erase();
		if (loginEdit && loginEdit->isEnabled())
			authEdit[0]->setEnabled( true );
		else {
			setActive( true );
			if (loginEdit && loginEdit->text().isEmpty())
				loginEdit->setFocus();
			else
				authEdit[0]->setFocus();
		}
	}
}

void // virtual
KPamGreeter::clear()
{
	// assert( !running && !passwd1Edit );
	authEdit[0]->erase();
	if (loginEdit) {
		loginEdit->clear();
		loginEdit->setFocus();
		curUser = TQString::null;
	} else
		authEdit[0]->setFocus();
}


// private

void
KPamGreeter::setActive( bool enable )
{
	if (loginEdit)
		loginEdit->setEnabled( enable );
}

void
KPamGreeter::setAllActive( bool enable )
{
    for(TQValueList<KPasswordEdit*>::iterator it = authEdit.begin();
        it != authEdit.end();
        ++it)
        (*it)->setEnabled( enable );
}

void
KPamGreeter::slotLoginLostFocus()
{
	if (!running)
		return;
	if (exp > 0) {
		if (curUser == loginEdit->text())
			return;
		exp = -1;
		handler->gplugReturnText( 0, 0 );
	}
	curUser = loginEdit->text();
        debug("curUser is %s", curUser.latin1());
	handler->gplugSetUser( curUser );
}

void
KPamGreeter::slotActivity()
{  
        debug("slotActivity");

	if (running)
		handler->gplugActivity();
}

// factory

static bool init( const TQString &,
                  TQVariant (*getConf)( void *, const char *, const TQVariant & ),
                  void *ctx )
{
	echoMode = (KPasswordEdit::EchoModes) getConf( ctx, "EchoMode", TQVariant( -1 ) ).toInt();
	KGlobal::locale()->insertCatalogue( "kgreet_pam" );
	return true;
}

static void done( void )
{
	KGlobal::locale()->removeCatalogue( "kgreet_pam" );
        if (log && log != stderr)
	    fclose(log);
        log = 0;
}

static KGreeterPlugin *
create( KGreeterPluginHandler *handler, KdmThemer *themer,
        TQWidget *parent, TQWidget *predecessor,
        const TQString &fixedEntity,
        KGreeterPlugin::Function func,
        KGreeterPlugin::Context ctx )
{
	return new KPamGreeter( handler, themer, parent, predecessor, fixedEntity, func, ctx );
}

KDE_EXPORT kgreeterplugin_info kgreeterplugin_info = {
	I18N_NOOP("Pam conversation plugin"), "pam",
	kgreeterplugin_info::Local | kgreeterplugin_info::Presettable,
	init, done, create
};

#include "kgreet_pam.moc"