Restore the '/' in the path before returning if we encounter an error.

This commit is contained in:
Todd C. Miller
2017-03-21 10:15:31 -06:00
parent 2dbd091443
commit cfb15106e3

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009-2016 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 2009-2017 Todd C. Miller <Todd.Miller@courtesan.com>
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@@ -35,6 +35,7 @@
/* /*
* Create any parent directories needed by path (but not path itself). * Create any parent directories needed by path (but not path itself).
* Note that path is modified but is restored before it returns.
*/ */
bool bool
sudo_mkdir_parents(char *path, uid_t uid, gid_t *gidp, mode_t mode, bool quiet) sudo_mkdir_parents(char *path, uid_t uid, gid_t *gidp, mode_t mode, bool quiet)
@@ -42,7 +43,6 @@ sudo_mkdir_parents(char *path, uid_t uid, gid_t *gidp, mode_t mode, bool quiet)
struct stat sb; struct stat sb;
gid_t parent_gid = 0; gid_t parent_gid = 0;
char *slash = path; char *slash = path;
bool ret = true;
debug_decl(sudo_mkdir_parents, SUDOERS_DEBUG_UTIL) debug_decl(sudo_mkdir_parents, SUDOERS_DEBUG_UTIL)
/* If no gid specified, inherit from parent dir. */ /* If no gid specified, inherit from parent dir. */
@@ -63,22 +63,19 @@ sudo_mkdir_parents(char *path, uid_t uid, gid_t *gidp, mode_t mode, bool quiet)
if (errno != EEXIST) { if (errno != EEXIST) {
if (!quiet) if (!quiet)
sudo_warn(U_("unable to mkdir %s"), path); sudo_warn(U_("unable to mkdir %s"), path);
ret = false; goto bad;
break;
} }
/* Already exists, make sure it is a directory. */ /* Already exists, make sure it is a directory. */
if (stat(path, &sb) != 0) { if (stat(path, &sb) != 0) {
if (!quiet) if (!quiet)
sudo_warn(U_("unable to stat %s"), path); sudo_warn(U_("unable to stat %s"), path);
ret = false; goto bad;
break;
} }
if (!S_ISDIR(sb.st_mode)) { if (!S_ISDIR(sb.st_mode)) {
if (!quiet) if (!quiet)
sudo_warnx(U_("%s exists but is not a directory (0%o)"), sudo_warnx(U_("%s exists but is not a directory (0%o)"),
path, (unsigned int) sb.st_mode); path, (unsigned int) sb.st_mode);
ret = false; goto bad;
break;
} }
/* Inherit gid of parent dir for ownership. */ /* Inherit gid of parent dir for ownership. */
if (*gidp == (gid_t)-1) if (*gidp == (gid_t)-1)
@@ -88,7 +85,11 @@ sudo_mkdir_parents(char *path, uid_t uid, gid_t *gidp, mode_t mode, bool quiet)
} }
/* Return parent gid if none was specified by caller. */ /* Return parent gid if none was specified by caller. */
if (ret && *gidp == (gid_t)-1) if (*gidp == (gid_t)-1)
*gidp = parent_gid; *gidp = parent_gid;
debug_return_bool(ret); debug_return_bool(true);
bad:
/* We must restore the path before we return. */
*slash = '/';
debug_return_bool(false);
} }