/
lolock.c
256 lines (205 loc) · 7 KB
/
lolock.c
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
/************************************************************************
* *
* Copyright (c) 2010 Sylvain `Magicking' Laurent <syllaur@gmail.com> *
* *
* lolock 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 3 of the License, or *
* (at your option) any later version. *
* *
* lolock 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 lolock. If not, see <http://www.gnu.org/licenses/>. *
* *
************************************************************************/
#include <SDL.h>
#include <SDL_ttf.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#include <security/pam_appl.h>
#include "font.h"
#define ERROR(msg,type) {\
fprintf(stderr, "\nError in %s:%d\n%s: %s\n", \
__FILE__, __LINE__, msg, type##_GetError()); \
exit(1); \
}
#define FONT_SIZE 42
#define PASS_MAX_SIZE 24
#define REPLACEMENT_CHAR '#'
static int cursor_state;
static SDL_Surface *sdlsScreen = NULL;
static TTF_Font *ttffFont = NULL;
static char *login_str;
void release_ressource()
{
free(login_str);
SDL_ShowCursor(cursor_state);
TTF_Quit();
SDL_FreeSurface(sdlsScreen);
SDL_Quit();
}
static TTF_Font * Load_Font(int ptsize)
{
SDL_RWops *sdlrwmem = NULL;
sdlrwmem = SDL_RWFromConstMem(&FontDump, FontSize);
return TTF_OpenFontRW(sdlrwmem, 1, ptsize);
}
int conv_func(int num_msg, const struct pam_message **msg,
struct pam_response **resp, void *appdata_ptr)
{
int i;
if (num_msg <= 0 || num_msg > PAM_MAX_NUM_MSG)
return PAM_CONV_ERR;
if (!(*resp = calloc(num_msg, sizeof (**resp))))
return PAM_BUF_ERR;
for (i = 0; i < num_msg; i++) //Should never be > 1
{
(*resp[i]).resp_retcode = 0;
if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF)
(*resp[i]).resp = strdup((const char *)appdata_ptr);
else
(*resp[i]).resp = NULL;
}
return PAM_SUCCESS;
}
static int check_pw(const char *login, const char *pwd)
{
struct pam_conv pam_c;
pam_handle_t *phth;
int ret;
pam_c.conv = conv_func;
pam_c.appdata_ptr = (void *)pwd;
assert(pam_start("lolock", login, &pam_c, &phth) == PAM_SUCCESS);
ret = pam_authenticate(phth, PAM_SILENT);
pam_end(phth, 0);
return ret == PAM_SUCCESS;
}
static void init(SDL_Surface ** sdlsScreen, TTF_Font ** ttffFont)
{
SDL_putenv("SDL_VIDEO_ALLOW_SCREENSAVER=1");
if (SDL_Init(SDL_INIT_VIDEO) < 0)
ERROR ("Could not initialize SDL",SDL);
if (TTF_Init() < 0)
ERROR ("Could not initialize TTF",TTF);
atexit(release_ressource);
if ((*ttffFont = Load_Font(FONT_SIZE)) == NULL)
ERROR ("Could not open font", TTF);
if ((*sdlsScreen = SDL_SetVideoMode(0, 0, 32,
SDL_SWSURFACE | SDL_FULLSCREEN )) == NULL)
ERROR ("Could not initialize video mode",SDL);
cursor_state = SDL_ShowCursor(SDL_QUERY);
SDL_ShowCursor(SDL_DISABLE);
SDL_EnableUNICODE(1);
}
static int ProcessPassword(char * str, int size)
{
(void)size;
return check_pw(login_str, str);
}
static void ProcessPending(char * str, int size)
{
SDL_Rect sdlrDest;
SDL_Surface *sdlsText = NULL;
SDL_Color sdlcWhite = {255, 255, 255, 0};
char *buffer = NULL;
int height;
int i;
(void)str;
if (size > 0)
{
buffer = (char*)malloc(size + 1);
for (i = 0 ; i < size; i++)
buffer[i] = REPLACEMENT_CHAR;
buffer[i] = 0;
sdlsText = TTF_RenderUTF8_Blended(ttffFont, buffer, sdlcWhite);
free(buffer);
}
height = TTF_FontHeight(ttffFont);
sdlrDest.x = 0;
sdlrDest.y = (sdlsScreen->h / 2) - (height / 2) + (height + 8);
sdlrDest.w = sdlsScreen->w;
sdlrDest.h = height;
SDL_FillRect(sdlsScreen, &sdlrDest, 0);
if (size > 0)
{
sdlrDest.x = (sdlsScreen->w / 2) - (sdlsText->w / 2);
sdlrDest.w = sdlsText->w;
SDL_BlitSurface(sdlsText, NULL, sdlsScreen, &sdlrDest);
SDL_FreeSurface(sdlsText);
}
sdlrDest.x = 0;
sdlrDest.w = sdlsScreen->w;
SDL_UpdateRects(sdlsScreen, 1, &sdlrDest);
}
int main()
{
SDL_Surface *sdlsText = NULL;
SDL_Color sdlcWhite = {255, 255, 255, 0};
SDL_Rect sdlrDest;
SDL_Event sdleEvt;
char *login_str_up;
int login_len;
char *msg_print;
const char *msg = "THIS SCREEN IS LOCKED BY ";
int password_len = 0;
char password[PASS_MAX_SIZE + 1];
int i;
init(&sdlsScreen, &ttffFont);
login_str = strdup(getpwuid(getuid())->pw_name);
login_len = strlen(login_str);
login_str_up = (char*)malloc(login_len + 1);
for (i = 0; i <= login_len; i++)
login_str_up[i] = toupper(login_str[i]);
msg_print = (char*)malloc(strlen(msg) + login_len + 1);
sprintf(msg_print, "%s%s", msg, login_str_up);
sdlsText = TTF_RenderUTF8_Blended(ttffFont, msg_print, sdlcWhite);
free(msg_print);
free(login_str_up);
sdlrDest.x = (sdlsScreen->w / 2) - (sdlsText->w / 2);
sdlrDest.y = (sdlsScreen->h / 2) - (sdlsText->h / 2) - (FONT_SIZE + 8);
sdlrDest.w = sdlsText->w;
sdlrDest.h = sdlsText->h;
SDL_BlitSurface(sdlsText, NULL, sdlsScreen, &sdlrDest);
SDL_UpdateRects(sdlsScreen, 1, &sdlrDest);
SDL_FreeSurface(sdlsText);
SDL_WM_GrabInput(SDL_GRAB_ON);
while (SDL_WaitEvent(&sdleEvt))
if (sdleEvt.type == SDL_KEYDOWN && sdleEvt.key.keysym.unicode & 0x07F)
switch (sdleEvt.key.keysym.unicode & 0x07F)
{
case 0x0D: //Enter
password[password_len] = 0;
if (ProcessPassword(password, password_len))
exit(0);
else
ProcessPending(NULL, 0);
password_len = 0;
while (SDL_PollEvent(&sdleEvt));
break;
case 0x01b:
password_len = 0;
case 0x08: //backspace
if (password_len)
password_len--;
ProcessPending(password, password_len);
break;
default:
if (password_len >= PASS_MAX_SIZE)
break;
password[password_len] = sdleEvt.key.keysym.unicode & 0x07F;
password_len++;
ProcessPending(password, password_len);
break;
}
return 0;
}