Create path for mpw.d sites files if it doesn't exist yet.
This commit is contained in:
parent
a85efc5736
commit
724b357dd8
@ -600,7 +600,6 @@ static MPMarshalledUser *mpw_marshall_read_flat(
|
|||||||
if (siteLoginName && strlen( siteLoginName ))
|
if (siteLoginName && strlen( siteLoginName ))
|
||||||
site->loginContent = mpw_siteState( masterKey, site->name, MPCounterValueInitial,
|
site->loginContent = mpw_siteState( masterKey, site->name, MPCounterValueInitial,
|
||||||
MPKeyPurposeIdentification, NULL, site->loginType, siteLoginName, site->algorithm );
|
MPKeyPurposeIdentification, NULL, site->loginType, siteLoginName, site->algorithm );
|
||||||
dbg( "site->content: %p\n", (void *)site->content );
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Redacted
|
// Redacted
|
||||||
@ -608,7 +607,6 @@ static MPMarshalledUser *mpw_marshall_read_flat(
|
|||||||
site->content = strdup( siteContent );
|
site->content = strdup( siteContent );
|
||||||
if (siteLoginName && strlen( siteLoginName ))
|
if (siteLoginName && strlen( siteLoginName ))
|
||||||
site->loginContent = strdup( siteLoginName );
|
site->loginContent = strdup( siteLoginName );
|
||||||
dbg( "site->content: %p\n", (void *)site->content );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -785,7 +783,6 @@ static MPMarshalledUser *mpw_marshall_read_json(
|
|||||||
if (siteLoginName && strlen( siteLoginName ))
|
if (siteLoginName && strlen( siteLoginName ))
|
||||||
site->loginContent = mpw_siteState( masterKey, site->name, MPCounterValueInitial,
|
site->loginContent = mpw_siteState( masterKey, site->name, MPCounterValueInitial,
|
||||||
MPKeyPurposeIdentification, NULL, site->loginType, siteLoginName, site->algorithm );
|
MPKeyPurposeIdentification, NULL, site->loginType, siteLoginName, site->algorithm );
|
||||||
dbg( "site->content: %p\n", (void *)site->content );
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Redacted
|
// Redacted
|
||||||
@ -793,7 +790,6 @@ static MPMarshalledUser *mpw_marshall_read_json(
|
|||||||
site->content = strdup( siteContent );
|
site->content = strdup( siteContent );
|
||||||
if (siteLoginName && strlen( siteLoginName ))
|
if (siteLoginName && strlen( siteLoginName ))
|
||||||
site->loginContent = strdup( siteLoginName );
|
site->loginContent = strdup( siteLoginName );
|
||||||
dbg( "site->content: %p\n", (void *)site->content );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
json_object_iter json_site_question;
|
json_object_iter json_site_question;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -135,29 +136,74 @@ static const char *mpw_getpass(const char *prompt) {
|
|||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *mpw_path(const char *prefix, const char *extension) {
|
static const char *mpw_path(const char *prefix, const char *extension) {
|
||||||
|
|
||||||
char *homedir = NULL;
|
// Resolve user's home directory.
|
||||||
struct passwd *passwd = getpwuid( getuid() );
|
const char *homedir = NULL;
|
||||||
if (passwd)
|
|
||||||
homedir = passwd->pw_dir;
|
|
||||||
if (!homedir)
|
if (!homedir)
|
||||||
homedir = getenv( "HOME" );
|
homedir = getenv( "HOME" );
|
||||||
|
if (!homedir)
|
||||||
|
homedir = getenv( "USERPROFILE" );
|
||||||
|
if (!homedir) {
|
||||||
|
const char *homedrive = getenv( "HOMEDRIVE" ), *homepath = getenv( "HOMEPATH" );
|
||||||
|
if (homedrive && homepath)
|
||||||
|
homedir = mpw_str( "%s%s", homedrive, homepath );
|
||||||
|
}
|
||||||
|
if (!homedir) {
|
||||||
|
struct passwd *passwd = getpwuid( getuid() );
|
||||||
|
if (passwd)
|
||||||
|
homedir = passwd->pw_dir;
|
||||||
|
}
|
||||||
if (!homedir)
|
if (!homedir)
|
||||||
homedir = getcwd( NULL, 0 );
|
homedir = getcwd( NULL, 0 );
|
||||||
|
|
||||||
char *mpwPath = NULL;
|
// Compose filename.
|
||||||
asprintf( &mpwPath, "%s.%s", prefix, extension );
|
char *path = NULL;
|
||||||
|
asprintf( &path, "%s.%s", prefix, extension );
|
||||||
|
|
||||||
char *slash = strstr( mpwPath, "/" );
|
// This is a filename, remove all potential directory separators.
|
||||||
if (slash)
|
for (char *slash; (slash = strstr( path, "/" )); *slash = '_');
|
||||||
*slash = '\0';
|
|
||||||
|
|
||||||
asprintf( &mpwPath, "%s/.mpw.d/%s", homedir, mpwPath );
|
// Compose pathname.
|
||||||
return mpwPath;
|
if (homedir)
|
||||||
|
asprintf( &path, "%s/.mpw.d/%s", homedir, path );
|
||||||
|
|
||||||
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *const argv[]) {
|
static bool mpw_mkdirs(const char *filePath) {
|
||||||
|
|
||||||
|
if (!filePath)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
char *pathEnd = strrchr( filePath, '/' );
|
||||||
|
char *path = pathEnd? strndup( filePath, pathEnd - filePath ): strdup( filePath );
|
||||||
|
if (!path)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Save the cwd and for absolute paths, start at the root.
|
||||||
|
char *cwd = getcwd( NULL, 0 );
|
||||||
|
if (*filePath == '/')
|
||||||
|
chdir( "/" );
|
||||||
|
|
||||||
|
// Walk the path.
|
||||||
|
bool success = true;
|
||||||
|
for (char *dirName = strtok( path, "/" ); success && dirName; dirName = strtok( NULL, "/" )) {
|
||||||
|
if (!strlen( dirName ))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
success &= (mkdir( dirName, 0700 ) != ERR || errno == EEXIST) && chdir( dirName ) != ERR;
|
||||||
|
}
|
||||||
|
free( path );
|
||||||
|
|
||||||
|
if (chdir( cwd ) == ERR)
|
||||||
|
wrn( "Could not restore cwd:\n %s: %s\n", cwd, strerror( errno ) );
|
||||||
|
free( cwd );
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(const int argc, char *const argv[]) {
|
||||||
|
|
||||||
// CLI defaults.
|
// CLI defaults.
|
||||||
bool allowPasswordUpdate = false, sitesFormatFixed = false;
|
bool allowPasswordUpdate = false, sitesFormatFixed = false;
|
||||||
@ -285,7 +331,7 @@ int main(int argc, char *const argv[]) {
|
|||||||
|
|
||||||
// Find the user's sites file.
|
// Find the user's sites file.
|
||||||
FILE *sitesFile = NULL;
|
FILE *sitesFile = NULL;
|
||||||
char *sitesPath = mpw_path( fullName, mpw_marshall_format_extension( sitesFormat ) );
|
const char *sitesPath = mpw_path( fullName, mpw_marshall_format_extension( sitesFormat ) );
|
||||||
if (!sitesPath || !(sitesFile = fopen( sitesPath, "r" ))) {
|
if (!sitesPath || !(sitesFile = fopen( sitesPath, "r" ))) {
|
||||||
dbg( "Couldn't open configuration file:\n %s: %s\n", sitesPath, strerror( errno ) );
|
dbg( "Couldn't open configuration file:\n %s: %s\n", sitesPath, strerror( errno ) );
|
||||||
|
|
||||||
@ -586,7 +632,7 @@ int main(int argc, char *const argv[]) {
|
|||||||
sitesPath = mpw_path( user->fullName, mpw_marshall_format_extension( sitesFormat ) );
|
sitesPath = mpw_path( user->fullName, mpw_marshall_format_extension( sitesFormat ) );
|
||||||
|
|
||||||
dbg( "Updating: %s (%s)\n", sitesPath, mpw_nameForFormat( sitesFormat ) );
|
dbg( "Updating: %s (%s)\n", sitesPath, mpw_nameForFormat( sitesFormat ) );
|
||||||
if (!sitesPath || !(sitesFile = fopen( sitesPath, "w" )))
|
if (!sitesPath || !mpw_mkdirs( sitesPath ) || !(sitesFile = fopen( sitesPath, "w" )))
|
||||||
wrn( "Couldn't create updated configuration file:\n %s: %s\n", sitesPath, strerror( errno ) );
|
wrn( "Couldn't create updated configuration file:\n %s: %s\n", sitesPath, strerror( errno ) );
|
||||||
|
|
||||||
else {
|
else {
|
||||||
|
Loading…
Reference in New Issue
Block a user