Last active
January 1, 2016 18:49
-
-
Save rjzak/8186295 to your computer and use it in GitHub Desktop.
A simple terminal locking application for Linux. Compile with: gcc -o tlock tlock.c -lpam -lpam_misc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <security/pam_appl.h> | |
#include <security/pam_misc.h> | |
#include <signal.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <time.h> | |
/** | |
* Code adapted from: | |
* PAM: http://www.makelinux.net/alp/084 | |
* Screen clearing: http://stackoverflow.com/a/263650/907773 | |
* Signal handling: http://www.yolinux.com/TUTORIALS/C++Signals.html | |
* Time display: http://www.cplusplus.com/reference/ctime/strftime/ | |
*/ | |
pam_handle_t *pamh; | |
struct pam_conv pamc; | |
char timeBuffer[80]; | |
char message[100]; | |
short useMessage = 0; | |
void lockTerm(); | |
void signalHandler(int sig); | |
int main(int argc, char* argv[]) { | |
time_t rawtime; | |
struct tm * timeinfo; | |
time (&rawtime); | |
timeinfo = localtime(&rawtime); | |
strftime(timeBuffer, 80, "%T on %a %F", timeinfo); | |
int i = 0; | |
for(i = 1; i < argc - 1; i++) { | |
if (strcmp(argv[i], "-m")==0) { | |
strcpy(message, argv[++i]); | |
useMessage = 1; | |
} | |
} | |
signal(SIGHUP, signalHandler); | |
signal(SIGINT, signalHandler); | |
signal(SIGQUIT, signalHandler); | |
signal(SIGTERM, signalHandler); | |
signal(SIGTSTP, signalHandler); | |
pamc.conv = &misc_conv; | |
pamc.appdata_ptr = NULL; | |
pam_start("common-auth", getenv("USER"), &pamc, &pamh); | |
lockTerm(); | |
pam_end(pamh, 0); | |
return 0; | |
} | |
void lockTerm() { | |
write(1,"\E[H\E[2J",7); | |
fprintf(stderr, "Terminal locked since %s by %s.\n", timeBuffer, getenv("USER")); | |
if (useMessage == 1) fprintf(stderr, "Message: %s\n", message); | |
while (pam_authenticate(pamh, 0) != PAM_SUCCESS) { | |
fprintf(stderr, "Authentication failed.\n"); | |
} | |
fprintf(stderr, "Terminal unlocked.\n"); | |
} | |
void signalHandler(int sig) { | |
write(1,"\E[H\E[2J",7); | |
fprintf(stderr, "Terminal locked since %s by %s.\n", timeBuffer, getenv("USER")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment