File: lock.c

package info (click to toggle)
splitvt 1.6.6-7
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 340 kB
  • ctags: 393
  • sloc: ansic: 4,811; sh: 99; makefile: 19; perl: 15
file content (102 lines) | stat: -rw-r--r-- 2,304 bytes parent folder | download | duplicates (8)
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

/* The screen locking module for splitvt */

#include	<unistd.h>
#include	<sys/types.h>
#include	<string.h>
#include	<stdio.h>
#include	<pwd.h>
#include	"vt100.h"
#include	"splitvt.h"

#define UNLOCKED	0
#define ENTERED1	1
#define ENTERED2	2
#define LOCKED		3

static int lock_state=UNLOCKED;

/* The number of characters entered (prevent overflow) */
static int cnt=0;

static char entered1[BUFSIZ], entered2[BUFSIZ];
static char *nextbuf=entered1;
static struct passwd *pw=NULL;

/* Used to release our input when the screen is unlocked */
extern void (*do_input)(), normal_input();

/* Since this function is called as an input function, we need to handle
   all state here. 
*/
void lock_screen(c)
char c;
{
	char message[512];

	if ( c == '\r' ) {
		switch (lock_state) {
			case UNLOCKED:	/* No password entered, cancel lock */
					vt_info("Lock cancelled.");
					reset_bar(1);
					do_input=normal_input;
					break;
			case ENTERED1:	/* First password has been entered */
					*nextbuf='\0';
					nextbuf=entered2;
					cnt=0;
					vt_info("Re-enter password: ");
					lock_state=ENTERED2;
					break;
			case ENTERED2:	/* Second password has been entered */
					*nextbuf='\0';
					nextbuf=entered1;
					cnt=0;
					if (strcmp(entered1, entered2) == 0) {
						sprintf(message,
				"Screen locked by %s.  Enter password: ", 
							pw?pw->pw_name:"user");
						vt_info(message);
						lock_state=LOCKED;
					} else {
						sprintf(message,
				"Passwords do not match.  Try again: ");
						vt_info(message);
						lock_state=UNLOCKED;
					}
					break;
			case LOCKED:	/* Verify password  */
					*nextbuf='\0';
					nextbuf=entered1;
					cnt=0;
					if (strcmp(entered1, entered2) == 0) {
						vt_info(NULL);
						do_input=normal_input;
						lock_state=UNLOCKED;
					} else {
						sprintf(message,
					"Password incorrect.  Try again: ");
						vt_info(message);
					}
					break;
			default:	/* What?? */
					break;
		}
	} else {
		switch (lock_state) {
			case UNLOCKED:	/* No password entered, start lock */
					pw=getpwuid(getuid()); /* Can fail */
					nextbuf=entered1;
					lock_state=ENTERED1;
					/* Fall through */
			case ENTERED1:
			case ENTERED2:
			case LOCKED:
					++cnt;
					if (cnt < BUFSIZ-1) { *(nextbuf++) = c; cnt++; }
					break;
		}
	}
	return;
}