useradd: fix segfault trying to overwrite const data with mkstemp

Also fix memory leaks in error paths.
This commit is contained in:
Tomas Mraz 2018-10-12 10:14:02 +02:00
parent fb97da1ce1
commit 10e388efc2

View File

@ -353,6 +353,8 @@ static void get_defaults (void)
len = strlen(prefix) + strlen(USER_DEFAULTS_FILE) + 2; len = strlen(prefix) + strlen(USER_DEFAULTS_FILE) + 2;
default_file = malloc(len); default_file = malloc(len);
if (default_file == NULL)
return;
wlen = snprintf(default_file, len, "%s/%s", prefix, USER_DEFAULTS_FILE); wlen = snprintf(default_file, len, "%s/%s", prefix, USER_DEFAULTS_FILE);
assert (wlen == (int) len -1); assert (wlen == (int) len -1);
} }
@ -363,7 +365,7 @@ static void get_defaults (void)
fp = fopen (default_file, "r"); fp = fopen (default_file, "r");
if (NULL == fp) { if (NULL == fp) {
return; goto getdef_err;
} }
/* /*
@ -474,7 +476,7 @@ static void get_defaults (void)
} }
} }
(void) fclose (fp); (void) fclose (fp);
getdef_err:
if(prefix[0]) { if(prefix[0]) {
free(default_file); free(default_file);
} }
@ -509,7 +511,7 @@ static int set_defaults (void)
FILE *ifp; FILE *ifp;
FILE *ofp; FILE *ofp;
char buf[1024]; char buf[1024];
char* new_file = NEW_USER_FILE; char *new_file = NULL;
char *default_file = USER_DEFAULTS_FILE; char *default_file = USER_DEFAULTS_FILE;
char *cp; char *cp;
int ofd; int ofd;
@ -521,17 +523,30 @@ static int set_defaults (void)
bool out_shell = false; bool out_shell = false;
bool out_skel = false; bool out_skel = false;
bool out_create_mail_spool = false; bool out_create_mail_spool = false;
if(prefix[0]) {
size_t len; size_t len;
int ret = -1;
len = strlen(prefix) + strlen(NEW_USER_FILE) + 2; len = strlen(prefix) + strlen(NEW_USER_FILE) + 2;
new_file = malloc(len); new_file = malloc(len);
wlen = snprintf(new_file, len, "%s/%s", prefix, NEW_USER_FILE); if (new_file == NULL) {
assert (wlen == (int) len -1); fprintf (stderr,
_("%s: cannot create new defaults file: %s\n"),
Prog, strerror(errno));
return -1;
}
wlen = snprintf(new_file, len, "%s%s%s", prefix, prefix[0]?"/":"", NEW_USER_FILE);
assert (wlen <= (int) len -1);
if(prefix[0]) {
len = strlen(prefix) + strlen(USER_DEFAULTS_FILE) + 2; len = strlen(prefix) + strlen(USER_DEFAULTS_FILE) + 2;
default_file = malloc(len); default_file = malloc(len);
if (default_file == NULL) {
fprintf (stderr,
_("%s: cannot create new defaults file: %s\n"),
Prog, strerror(errno));
goto setdef_err;
}
wlen = snprintf(default_file, len, "%s/%s", prefix, USER_DEFAULTS_FILE); wlen = snprintf(default_file, len, "%s/%s", prefix, USER_DEFAULTS_FILE);
assert (wlen == (int) len -1); assert (wlen == (int) len -1);
} }
@ -544,7 +559,7 @@ static int set_defaults (void)
fprintf (stderr, fprintf (stderr,
_("%s: cannot create new defaults file\n"), _("%s: cannot create new defaults file\n"),
Prog); Prog);
return -1; goto setdef_err;
} }
ofp = fdopen (ofd, "w"); ofp = fdopen (ofd, "w");
@ -552,7 +567,7 @@ static int set_defaults (void)
fprintf (stderr, fprintf (stderr,
_("%s: cannot open new defaults file\n"), _("%s: cannot open new defaults file\n"),
Prog); Prog);
return -1; goto setdef_err;
} }
/* /*
@ -579,7 +594,7 @@ static int set_defaults (void)
_("%s: line too long in %s: %s..."), _("%s: line too long in %s: %s..."),
Prog, default_file, buf); Prog, default_file, buf);
(void) fclose (ifp); (void) fclose (ifp);
return -1; goto setdef_err;
} }
} }
@ -643,7 +658,7 @@ static int set_defaults (void)
|| (fsync (fileno (ofp)) != 0) || (fsync (fileno (ofp)) != 0)
|| (fclose (ofp) != 0)) { || (fclose (ofp) != 0)) {
unlink (new_file); unlink (new_file);
return -1; goto setdef_err;
} }
/* /*
@ -658,7 +673,7 @@ static int set_defaults (void)
_("%s: Cannot create backup file (%s): %s\n"), _("%s: Cannot create backup file (%s): %s\n"),
Prog, buf, strerror (err)); Prog, buf, strerror (err));
unlink (new_file); unlink (new_file);
return -1; goto setdef_err;
} }
/* /*
@ -669,7 +684,7 @@ static int set_defaults (void)
fprintf (stderr, fprintf (stderr,
_("%s: rename: %s: %s\n"), _("%s: rename: %s: %s\n"),
Prog, new_file, strerror (err)); Prog, new_file, strerror (err));
return -1; goto setdef_err;
} }
#ifdef WITH_AUDIT #ifdef WITH_AUDIT
audit_logger (AUDIT_USYS_CONFIG, Prog, audit_logger (AUDIT_USYS_CONFIG, Prog,
@ -683,13 +698,14 @@ static int set_defaults (void)
(unsigned int) def_group, def_home, def_shell, (unsigned int) def_group, def_home, def_shell,
def_inactive, def_expire, def_template, def_inactive, def_expire, def_template,
def_create_mail_spool)); def_create_mail_spool));
ret = 0;
if(prefix[0]) { setdef_err:
free(new_file); free(new_file);
if(prefix[0]) {
free(default_file); free(default_file);
} }
return 0; return ret;
} }
/* /*