Merge pull request #482 from xry111/useradd_segfault_fix

fix segfault running useradd -D
This commit is contained in:
Serge Hallyn 2021-12-29 12:03:34 -06:00 committed by GitHub
commit 5575995541
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -41,6 +41,7 @@
#include <getopt.h> #include <getopt.h>
#include <grp.h> #include <grp.h>
#include <lastlog.h> #include <lastlog.h>
#include <libgen.h>
#include <pwd.h> #include <pwd.h>
#ifdef ACCT_TOOLS_SETUID #ifdef ACCT_TOOLS_SETUID
#ifdef USE_PAM #ifdef USE_PAM
@ -538,6 +539,7 @@ static int set_defaults (void)
FILE *ofp; FILE *ofp;
char buf[1024]; char buf[1024];
char *new_file = NULL; char *new_file = NULL;
char *new_file_dup = NULL;
char *default_file = USER_DEFAULTS_FILE; char *default_file = USER_DEFAULTS_FILE;
char *cp; char *cp;
int ofd; int ofd;
@ -578,14 +580,24 @@ static int set_defaults (void)
assert (wlen == (int) len -1); assert (wlen == (int) len -1);
} }
ret = mkdir(dirname(NEW_USER_FILE), 0755); new_file_dup = strdup(new_file);
if (-1 == ret && EEXIST != errno) { if (new_file_dup == NULL) {
fprintf (stderr, fprintf (stderr,
_("%s: cannot create directory for defaults file\n"), _("%s: cannot create directory for defaults file\n"),
Prog); Prog);
goto setdef_err; goto setdef_err;
} }
ret = mkdir(dirname(new_file_dup), 0755);
if (-1 == ret && EEXIST != errno) {
fprintf (stderr,
_("%s: cannot create directory for defaults file\n"),
Prog);
free(new_file_dup);
goto setdef_err;
}
free(new_file_dup);
/* /*
* Create a temporary file to copy the new output to. * Create a temporary file to copy the new output to.
*/ */