Skip to content

Instantly share code, notes, and snippets.

@framon
Created October 19, 2013 03:45
Show Gist options
  • Save framon/7051439 to your computer and use it in GitHub Desktop.
Save framon/7051439 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
void message(pid_t pid )
{
fprintf(stderr, "\nLock (pid=%d) -- terminal locker for Linux.\n", pid);
}
void usage()
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, "\tLock \n");
fprintf(stderr, "\tLock [password]\n");
fprintf(stderr, "\tLock -f[file with password]\n");
}
int main(int argc, char **argv)
{
pid_t pid;
char *fname = "lock.pas";//"~/backup/lock.passwd";
char *tty;
char key[256], senha[256], senha2[256];
int counterr=0;
FILE *file;
pid =getpid();
message(pid);
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
if (argc > 1)
{
if ( !strncmp(argv[1],"-",1) )
{
if ( !strncmp(argv[1],"-f",2) && strlen(argv[1]) > 2 )
{
argv[1] += 2;
if ( file=fopen(argv[1],"r") )
{
fscanf(file,"%s",&senha);
fclose(file);
}
else
{
fprintf(stderr, "ERROR read file %s\n",argv[1]);
}
}
else
{
usage();
exit(1);
}
}
else
{
strncpy(senha,argv[1],255);
}
}
if ( !strcmp(senha,"") )
{
do
{
fprintf(stderr, "\nYour password does not need to be your login password.");
strncpy(senha, getpass("\nEnter password: "), 255);
senha[255]='\0';
strncpy(senha2, getpass("ReEnter password: "), 255);
senha2[255]='\0';
if (strcmp(senha, senha2))
{
fprintf(stderr, "Sorry, password does not match!\n");
counterr++;
}
}while(strcmp(senha, senha2) && counterr<4);
if ( counterr==4 )
{
fprintf(stderr, "\nSorry, error to much!\n");
exit(1);
}
}
tty = ttyname(0);
counterr=0;
while(1)
{
fprintf(stderr, "\nThis terminal (%s) is locked by ", tty);
strncpy(key, getpass("\nEnter password to unlock: "), 255);
key[255]='\0';
if (!strcmp(senha, key))
{
fprintf(stderr,"Unlocked after %d errors\n\n",counterr);
exit(0);
}
else
{
if(file=fopen(fname,"a+"))
{
fprintf(file,"%s\n",key);
fclose(file);
}
fprintf(stderr, "\nInvalid Password\n");
counterr++;
if (counterr%10 == 0 )
{
fprintf(stderr, "Many errros, wait 15 secs\n");
message(pid);
sleep(10);
}
else
{
fprintf(stderr, "\n");
sleep(1);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment