Fixes #68 (pam_panic_pw doesn't create dir if it doesn't exist)

This commit is contained in:
Bandie 2018-10-03 20:28:15 +02:00
parent f3e04abfad
commit b393c8c80c
Signed by: Bandie
GPG Key ID: C1E133BC65A822DD
1 changed files with 16 additions and 1 deletions

View File

@ -12,6 +12,7 @@ LICENSE : GNU-GPLv3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
@ -20,9 +21,23 @@ LICENSE : GNU-GPLv3
#include "../../lib/gettext.h"
#define _(String) gettext(String)
#define FMODE 0644
#define DMODE 0755
int writePasswords(char pw[][256], char* pwfile){
// 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);
}
FILE *f = fopen(pwfile, "w");
if(f == NULL){
fprintf(stderr, _("ERROR opening file!\n"));
@ -33,7 +48,7 @@ int writePasswords(char pw[][256], char* pwfile){
fclose(f);
chmod(pwfile, 0644);
chmod(pwfile, FMODE);
return 0;
}