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

View File

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