mainline merge: clean homedir pathname

This commit is contained in:
startx2017 2018-10-17 19:07:46 -04:00
parent d95bd0616e
commit df7591d850
4 changed files with 45 additions and 4 deletions

View file

@ -479,6 +479,7 @@ int is_link(const char *fname);
void trim_trailing_slash_or_dot(char *path);
char *line_remove_spaces(const char *buf);
char *split_comma(char *str);
char *clean_pathname(const char *path);
void check_unsigned(const char *str, const char *msg);
int find_child(pid_t parent, pid_t *child);
void check_private_dir(void);

View file

@ -212,9 +212,8 @@ static void init_cfg(int argc, char **argv) {
// build home directory name
cfg.homedir = NULL;
if (pw->pw_dir != NULL) {
cfg.homedir = strdup(pw->pw_dir);
if (!cfg.homedir)
errExit("strdup");
cfg.homedir = clean_pathname(pw->pw_dir);
assert(cfg.homedir);
}
else {
fprintf(stderr, "Error: user %s doesn't have a user directory assigned\n", cfg.username);

View file

@ -532,6 +532,39 @@ char *split_comma(char *str) {
return ptr;
}
// remove consecutive and trailing slashes
// and return allocated memory
// e.g. /home//user/ -> /home/user
char *clean_pathname(const char *path) {
assert(path);
size_t len = strlen(path);
char *rv = calloc(len + 1, 1);
if (!rv)
errExit("calloc");
if (len > 0) {
int i, j, cnt;
for (i = 0, j = 0, cnt = 0; i < len; i++) {
if (path[i] == '/')
cnt++;
else
cnt = 0;
if (cnt < 2) {
rv[j] = path[i];
j++;
}
}
// remove a trailing slash
if (j > 1 && rv[j - 1] == '/')
rv[j - 1] = '\0';
size_t new_len = strlen(rv);
if (new_len < len) {
rv = realloc(rv, new_len + 1);
if (!rv)
errExit("realloc");
}
}
return rv;
}
void check_unsigned(const char *str, const char *msg) {
EUID_ASSERT();

10
status
View file

@ -1,4 +1,12 @@
Oct 9, 0.9.56.1, mainline merge
Oct 17, partial mainline merge
done: clean homedir pathname
done: allow overriding of disable-mnt with noblacklist
Oct 9, 0.9.56.1, full mainline merge
done: cleanup
done: regression: fix whitelisting of symlinks to other home dirs, small improvements
done: tiny memleaks