@ -11,6 +11,7 @@
* which is what it's designed for.
* which is what it's designed for.
*/
*/
#include "cache.h"
#include "cache.h"
#include "strbuf.h"
static char bad_path[] = "/bad-path/";
static char bad_path[] = "/bad-path/";
@ -207,43 +208,44 @@ int validate_headref(const char *path)
return -1;
return -1;
}
}
static char *user_path(char *buf, char *path, int sz)
static struct passwd *getpw_str(const char *username, size_t len)
{
{
struct passwd *pw;
struct passwd *pw;
char *slash;
char *username_z = xmalloc(len + 1);
int len, baselen;
memcpy(username_z, username, len);
username_z[len] = '\0';
pw = getpwnam(username_z);
free(username_z);
return pw;
}
if (!path || path[0] != '~')
/*
return NULL;
* Return a string with ~ and ~user expanded via getpw*. If buf != NULL,
path++;
* then it is a newly allocated string. Returns NULL on getpw failure or
slash = strchr(path, '/');
* if path is NULL.
if (path[0] == '/' || !path[0]) {
*/
pw = getpwuid(getuid());
char *expand_user_path(const char *path)
}
{
else {
struct strbuf user_path = STRBUF_INIT;
if (slash) {
const char *first_slash = strchrnul(path, '/');
*slash = 0;
const char *to_copy = path;
pw = getpwnam(path);
*slash = '/';
if (path == NULL)
}
goto return_null;
else
if (path[0] == '~') {
pw = getpwnam(path);
const char *username = path + 1;
}
size_t username_len = first_slash - username;
if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir))
struct passwd *pw = getpw_str(username, username_len);
return NULL;
if (!pw)
baselen = strlen(pw->pw_dir);
goto return_null;
memcpy(buf, pw->pw_dir, baselen);
strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir));
while ((1 < baselen) && (buf[baselen-1] == '/')) {
to_copy = first_slash;
buf[baselen-1] = 0;
baselen--;
}
}
if (slash && slash[1]) {
strbuf_add(&user_path, to_copy, strlen(to_copy));
len = strlen(slash);
return strbuf_detach(&user_path, NULL);
if (sz <= baselen + len)
return_null:
strbuf_release(&user_path);
return NULL;
return NULL;
memcpy(buf + baselen, slash, len + 1);
}
return buf;
}
}
/*
/*
@ -291,8 +293,18 @@ char *enter_repo(char *path, int strict)
if (PATH_MAX <= len)
if (PATH_MAX <= len)
return NULL;
return NULL;
if (path[0] == '~') {
if (path[0] == '~') {
if (!user_path(used_path, path, PATH_MAX))
char *newpath = expand_user_path(path);
if (!newpath || (PATH_MAX - 10 < strlen(newpath))) {
free(newpath);
return NULL;
return NULL;
}
/*
* Copy back into the static buffer. A pity
* since newpath was not bounded, but other
* branches of the if are limited by PATH_MAX
* anyway.
*/
strcpy(used_path, newpath); free(newpath);
strcpy(validated_path, path);
strcpy(validated_path, path);
path = used_path;
path = used_path;
}
}