pam_panic/src/pam_panic_pw/pam_panic_pw.c

131 lines
2.7 KiB
C
Raw Normal View History

2018-04-01 00:09:00 +00:00
/*
FILENAME : pam_panic_pw.c
DESCRIPTION : Generates and saves password for pam_panic
AUTHOR : Bandie, some Author of the glibc manpage
2018-04-01 00:09:00 +00:00
DATE : 2018-03-27T02:34:08+02:00
LICENSE : GNU-GPLv3
*/
2018-03-31 23:53:41 +00:00
#include "pam_panic_pw.h"
2018-03-31 23:53:41 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
2018-03-31 23:53:41 +00:00
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <crypt.h>
2018-10-01 18:51:38 +00:00
#include "../config.h"
2018-09-21 20:50:38 +00:00
#include "../../lib/gettext.h"
2018-03-31 23:53:41 +00:00
2018-09-21 20:50:38 +00:00
#define _(String) gettext(String)
#define FMODE 0644
#define DMODE 0755
#ifdef VERSION
2018-03-31 23:53:41 +00:00
2018-09-30 10:46:14 +00:00
int writePasswords(char pw[][256], char* pwfile){
2018-03-31 23:53:41 +00:00
// Check, if path of pwfile exist
// Get parent dir of pwfile
char* parentdir;
parentdir = strdup(pwfile);
dirname(parentdir);
// Get POSIX info about the parent dir and create dir, if it does not exist
struct stat pd;
if(stat(parentdir, &pd) != 0 && !(S_ISDIR(pd.st_mode))){
mkdir(parentdir, DMODE);
}
2018-09-29 23:37:34 +00:00
FILE *f = fopen(pwfile, "w");
2018-03-31 23:53:41 +00:00
if(f == NULL){
2018-09-21 20:50:38 +00:00
fprintf(stderr, _("ERROR opening file!\n"));
2018-03-31 23:53:41 +00:00
return 2;
}
fprintf(f, "%s\n%s\n", pw[0], pw[1]);
fclose(f);
chmod(pwfile, FMODE);
2018-03-31 23:53:41 +00:00
return 0;
}
2018-09-29 23:37:34 +00:00
#ifndef TEST
2018-03-31 23:53:41 +00:00
int main(void){
2018-09-21 20:50:38 +00:00
// gettext
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
// init
2018-03-31 23:53:41 +00:00
time_t t;
srand((unsigned) time(&t));
unsigned long seed[2];
char salt[] = "$6$........";
const char *const seedchars =
"./0123456789ABCDEFGHIJKLMNOPQRST"
"UVWXYZabcdefghijklmnopqrstuvwxyz";
char *password[2];
char *pwvrf[2];
2018-09-30 11:15:32 +00:00
char pw[2][256];
char pwv[2][256];
2018-03-31 23:53:41 +00:00
2018-09-21 20:50:38 +00:00
char *prompt[4] = {
_("Key password: "),
_("Panic password: "),
_("Confirm key password: "),
_("Confirm panic password: ")
2018-09-21 20:50:38 +00:00
};
2018-03-31 23:53:41 +00:00
int i;
if(getuid() != 0){
2018-09-21 20:50:38 +00:00
printf(_("Please run this program under root. Write access to %s is mandatory.\n"), PPASSFILE); return 1;
2018-03-31 23:53:41 +00:00
}
printf("pam_panic_pw %s\n", VERSION);
2018-03-31 23:53:41 +00:00
for(int j=0; j<2; j++){
seed[0] = time(&t);
seed[1] = rand() ^ (seed[0] >> 14 & 0x30000);
/* Turn it into printable characters from seedchars. */
for (i = 0; i < 8; i++)
salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];
/* Read in the users password and encrypt it. */
password[j] = crypt(getpass(prompt[j]), salt);
strcpy(pw[j], password[j]);
for(int k=0; k<3; k++){
pwvrf[j] = crypt(getpass(prompt[j+2]), pw[j]);
strcpy(pwv[j], pwvrf[j]);
2018-04-01 23:20:51 +00:00
int ok = strcmp(pw[j], pwv[j]) == 0;
2018-03-31 23:53:41 +00:00
if(!ok){
if(k==2){
2018-09-21 20:50:38 +00:00
printf(_("Didn't work. Bye.\n"));
2018-03-31 23:53:41 +00:00
return 1;
}else
2018-09-21 20:50:38 +00:00
printf(_("Password didn't match. Try again.\n"));
2018-03-31 23:53:41 +00:00
}else
break;
}
}
/* Save the results. */
2018-09-29 23:37:34 +00:00
return writePasswords(pw, PPASSFILE);
2018-03-31 23:53:41 +00:00
}
2018-09-29 23:37:34 +00:00
#endif
#endif