-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
396 lines (326 loc) · 14.3 KB
/
main.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
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
/*
* XL-more version 1.01
*
* github.com/alexyuriev/xl-more
*
* Derived from XL by Danny Dulai <github.com/dannydulai/xl>
* PAM support, state signaling and resource management by Alex Yuriev <github.com/alexyriev>
*
* Copyright (c) 1997 Danni Dulai
* Copyright (c) 2018 Alexander O. Yuriev
* Copyright (c) 2002 Networks Associates Technology, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <pwd.h>
#include <security/pam_appl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sys/types.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xresource.h>
#include <X11/Xutil.h>
#define MAX_PASSWORD_LEN 1024
#define DEFAULT_LOCKED_IGNORE_STR "#000000"
#define DEFAULT_LOCKED_STORE_STR "#0000ff"
#define XRESOURCE_LOAD_STRING(NAME, DST) \
XrmGetResource(db, NAME, "String", &type, &ret); \
if (ret.addr != NULL && !strncmp("String", type, 64)) \
DST = ret.addr; \
else \
DST = NULL;
#define XL_LOCK_SCREEN_COLOR(NAME) \
XUnmapWindow( display, w); \
XSetWindowBackground( display, w, NAME ); \
XMapWindow( display, w); \
XFlush( display );
static int pam_converse(int n, const struct pam_message **msg, struct pam_response **resp, void *data)
{
struct pam_response *responses;
char *password = data;
unsigned i;
if (n <= 0 || n > PAM_MAX_NUM_MSG) {
syslog(LOG_ERR, "Internal PAM error: num_msg <= 0 or great than %d", PAM_MAX_NUM_MSG);
return (PAM_CONV_ERR);
}
if ((responses = calloc(n, sizeof *responses)) == NULL) {
syslog(LOG_ERR, "Internal PAM error: out of memory");
return (PAM_BUF_ERR);
}
for (i = 0; i < n; ++i) {
responses[i].resp_retcode = 0;
responses[i].resp = NULL;
char *m_style = NULL;
switch (msg[i]->msg_style) {
case PAM_PROMPT_ECHO_OFF: m_style = "PAM_PROMPT_ECHO_OFF"; break;
case PAM_PROMPT_ECHO_ON: m_style = "PAM_PROMPT_ECHO_ON"; break;
case PAM_ERROR_MSG: m_style = "PAM_ERROR_MSG"; break;
case PAM_TEXT_INFO: m_style = "PAM_TEXT_INFO"; break;
default: syslog(LOG_ERR, "Internal PAM error: uknown message type %d", msg[i]->msg_style);
goto fail;
}
switch (msg[i]->msg_style) {
case PAM_PROMPT_ECHO_OFF: if ((responses[i].resp = strdup(password)) == NULL) {
goto fail;
}
break;
default: syslog(LOG_ERR, "Internal error: message style %s not implemented", m_style);
goto fail;
}
}
*resp = responses;
return (PAM_SUCCESS);
fail:
for (i = 0; i < n; ++i) {
if (responses[i].resp != NULL) {
memset(responses[i].resp, 0, strlen(responses[i].resp));
free(responses[i].resp);
}
}
memset(responses, 0, n * sizeof *responses);
*resp = NULL;
return (PAM_CONV_ERR);
}
static int authenticate_using_pam (const char* service_name, const char* username, char* password)
{
struct pam_conv pam_conversation = { pam_converse, password };
pam_handle_t *pamh;
int ret;
ret = pam_start(service_name, username, &pam_conversation, &pamh);
if (ret != PAM_SUCCESS) {
syslog(LOG_ERR, "PAM Initialization failed: %s", pam_strerror(pamh, ret));
return 1;
}
ret = pam_authenticate(pamh, 0);
if (ret != PAM_SUCCESS) {
return 1;
}
ret = pam_end(pamh, ret);
if (ret != PAM_SUCCESS) {
syslog(LOG_ERR, "PAM termination failed after successful valdation: %s", pam_strerror(pamh, ret));
return 1;
}
return 0;
}
static char *return_null_or_dup(char *check) {
if (!check || !strlen(check)) {
return NULL;
}
return strdup(check);
}
static char *return_dup_if_null_or_self(char *check, char *dup)
{
if (!check) {
return strdup(dup);
}
return check;
}
static int load_resources( Display *display, char **resourceStrPAMService, char **resourceStrColorIgnore, char **resourceStrColorStore )
{
XrmDatabase db; /* Xresources database */
XrmValue ret; /* structure that holds pointer to string */
char *resource_manager;
char *type; /* class of returned variable */
char *var; /* pointer to the resource */
XrmInitialize();
if ( !(resource_manager = XResourceManagerString( display )) ) {
return -1;
}
if ( !(db = XrmGetStringDatabase(resource_manager)) ) {
return -2;
}
XRESOURCE_LOAD_STRING("XL-more.color.ignore", var);
*resourceStrColorIgnore = return_null_or_dup(var);
XRESOURCE_LOAD_STRING("XL-more.color.store", var);
*resourceStrColorStore = return_null_or_dup(var);
XRESOURCE_LOAD_STRING("XL-more.pam_service", var);
*resourceStrPAMService = return_null_or_dup(var);
return 1;
}
int main(int argc, char *argv[])
{
int ret = 0;
uid_t uid = getuid();
struct passwd *pw;
pw = getpwuid(uid);
if ( !pw ) {
fprintf(stderr, "Could not get a user name of active user\n");
exit(1);
}
char *user_name = NULL;
if ( !(user_name = strdup(pw->pw_name)) ) {
fprintf(stderr, "Could not process a user name of the active user: out of memory\n");
exit(1);
}
Display *display;
if ( !(display = XOpenDisplay(NULL)) ) {
fprintf(stderr, "Could not connect to DISPLAY\n");
exit(1);
}
char *defaultStrPAMService, *defaultStrColorIgnore, *defaultStrColorStore;
if ( (ret = load_resources( display, &defaultStrPAMService, &defaultStrColorIgnore, &defaultStrColorStore )) < 0) {
fprintf(stderr, "Failed to process Xresouces\n");
exit(1);
}
char *service_name = NULL;
char *ptr = NULL;
if ( !defaultStrPAMService) {
if ( (ptr = getenv("PAM_SERVICE")) ) {
service_name = strdup(ptr);
}
if ( !service_name ) {
fprintf(stderr, "Could not get lock service name for PAM\n");
exit(1);
}
} else {
service_name = defaultStrPAMService;
}
char *strColorLockedIgnore = return_dup_if_null_or_self( defaultStrColorIgnore, DEFAULT_LOCKED_IGNORE_STR );
if ( !strColorLockedIgnore) {
fprintf(stderr, "Failed to obtain locked ignore color: Out of memory\n");
exit(1);
}
char *strColorLockedStore = return_dup_if_null_or_self( defaultStrColorStore, DEFAULT_LOCKED_STORE_STR );
if ( !strColorLockedStore) {
fprintf(stderr, "Failed to obtain locked store color: Out of memory\n");
exit(1);
}
int screen = DefaultScreen( display );
Window root = DefaultRootWindow(display);
if ( root < 0 ) {
fprintf(stderr, "Failed to get a root window ID of a default screen\n");
exit(1);
}
Colormap cmap = DefaultColormap( display, screen );
Status retStatus;
XColor colorLockedIgnore;
if ( !XParseColor( display, cmap, strColorLockedIgnore, &colorLockedIgnore )) {
fprintf(stderr, "Failed to parse a locked ignore color %s\n", strColorLockedIgnore);
exit(1);
}
retStatus = XAllocColor( display, cmap, &colorLockedIgnore );
if (retStatus == BadColor) {
fprintf(stderr, "Failed to allocate a locked ignore color %s\n", strColorLockedIgnore);
exit(1);
}
unsigned long pixelColorLockedIgnore = colorLockedIgnore.pixel;
XColor colorLockedStore;
if ( !XParseColor( display, cmap, strColorLockedStore, &colorLockedStore )) {
fprintf(stderr, "Failed to parse a locked store color %s\n", strColorLockedStore);
exit(1);
}
retStatus = XAllocColor ( display, cmap, &colorLockedStore );
if (retStatus == BadColor) {
fprintf(stderr, "Failed to allocate a locked store color %s\n", strColorLockedStore);
exit(1);
}
unsigned long pixelColorLockedStore = colorLockedStore.pixel;
XWindowAttributes xwRootWinAttr;
XGetWindowAttributes( display, root, &xwRootWinAttr );
Window w = XCreateSimpleWindow( display, root, 0, 0, xwRootWinAttr.width, xwRootWinAttr.height, 0, 0, pixelColorLockedIgnore );
Window w_store = XCreateSimpleWindow( display, root, 0, 0, xwRootWinAttr.width, xwRootWinAttr.height, 0, 0, pixelColorLockedStore );
openlog("XL-more", LOG_PID, LOG_AUTHPRIV);
syslog(LOG_INFO, "User `%s` locked X screen", user_name);
static char noData[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Pixmap emptyBitMap_ignore = XCreateBitmapFromData( display, w, noData, 8, 8);
Pixmap emptyBitMap_store = XCreateBitmapFromData( display, w_store, noData, 8, 8);
Cursor invisibleCursor_ignore = XCreatePixmapCursor( display, emptyBitMap_ignore, emptyBitMap_ignore, &colorLockedIgnore, &colorLockedIgnore, 0, 0);
Cursor invisibleCursor_store = XCreatePixmapCursor( display, emptyBitMap_store, emptyBitMap_store, &colorLockedStore, &colorLockedStore, 0, 0);
XDefineCursor( display, w, invisibleCursor_ignore );
XDefineCursor( display, w_store, invisibleCursor_store );
XFreeCursor( display, invisibleCursor_ignore );
XFreeCursor( display, invisibleCursor_store );
XFreePixmap( display, emptyBitMap_ignore );
XFreePixmap( display, emptyBitMap_store );
/* Attributes for the locked window */
XSetWindowAttributes xwWindowSetAttr, xwWindowSetAttr_store;
xwWindowSetAttr.save_under = xwWindowSetAttr_store.save_under = True;
xwWindowSetAttr.override_redirect = xwWindowSetAttr_store.override_redirect = True;
XChangeWindowAttributes( display, w, CWOverrideRedirect | CWSaveUnder, &xwWindowSetAttr );
XChangeWindowAttributes( display, w_store, CWOverrideRedirect | CWSaveUnder, &xwWindowSetAttr_store );
XMapWindow( display, w);
XGrabPointer(display, root, 1, ButtonPress, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
XGrabKeyboard(display, root, 0, GrabModeAsync, GrabModeAsync, CurrentTime);
XSelectInput(display, root, KeyPressMask);
char input_buffer[MAX_PASSWORD_LEN];
char passwd_buffer[MAX_PASSWORD_LEN];
memset( passwd_buffer, 0x00, MAX_PASSWORD_LEN );
unsigned current_offset = 0;
unsigned remember_keys = 0;
XEvent ev;
while (XNextEvent(display, &ev), 1) {
if (ev.type == KeyPress) {
KeySym keysym;
XComposeStatus compose;
int i = XLookupString(&ev.xkey, input_buffer, 10, &keysym, &compose);
if ( keysym == XK_Return ) {
if ( !remember_keys ) {
// start recording key presses
XMapWindow( display, w_store );
memset( passwd_buffer, 0x00, MAX_PASSWORD_LEN );
remember_keys = 1;
current_offset = 0;
} else {
XUnmapWindow ( display, w_store );
XL_LOCK_SCREEN_COLOR( pixelColorLockedIgnore );
remember_keys = 0;
if ( current_offset && !authenticate_using_pam(service_name, user_name, passwd_buffer) ) {
// success - password correct
memset(passwd_buffer, 0x00, MAX_PASSWORD_LEN);
syslog(LOG_INFO, "User `%s` unlocked X screen", user_name);
XUngrabKeyboard(display, CurrentTime);
XUngrabPointer(display, CurrentTime);
exit(0);
}
current_offset = 0;
memset( passwd_buffer, 0x00, MAX_PASSWORD_LEN );
syslog( LOG_ERR, "User `%s` failed to unlocked X screen", user_name );
}
} else {
if ( current_offset + i > MAX_PASSWORD_LEN - 1 ) {
// This password is too long so pretend that it failed and reset
XUnmapWindow( display, w_store );
XL_LOCK_SCREEN_COLOR( pixelColorLockedIgnore );
remember_keys = 0;
current_offset = 0;
memset( passwd_buffer, 0x00, MAX_PASSWORD_LEN );
syslog( LOG_ERR, "User `%s` failed to unlocked X screen", user_name );
} else {
if ( remember_keys ) {
/* Not only key pressed, but we are in a record keypress to assemble password mode */
memcpy( passwd_buffer + current_offset, input_buffer, i );
current_offset += i;
}
memset( input_buffer, 0x00, i );
}
}
}
}
}