From b393c8c80c28037208f3640ade06142c3efc4aa6 Mon Sep 17 00:00:00 2001 From: Bandie Date: Wed, 3 Oct 2018 20:28:15 +0200 Subject: [PATCH] Fixes #68 (pam_panic_pw doesn't create dir if it doesn't exist) --- src/pam_panic_pw/pam_panic_pw.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/pam_panic_pw/pam_panic_pw.c b/src/pam_panic_pw/pam_panic_pw.c index 992e569..6e12782 100644 --- a/src/pam_panic_pw/pam_panic_pw.c +++ b/src/pam_panic_pw/pam_panic_pw.c @@ -12,6 +12,7 @@ LICENSE : GNU-GPLv3 #include #include #include +#include #include #include #include @@ -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; }