authkey: Use the config home dir for relative paths

Previously relative cookie paths were searched from the home
directory, now they are searched from the config home directory. This
fixes the problem that XDG_CONFIG_HOME didn't have effect on cookie
paths.

BugLink: https://bugs.freedesktop.org/show_bug.cgi?id=75006
This commit is contained in:
Tanu Kaskinen 2014-06-08 16:33:01 +03:00
parent 440f37af00
commit 59a8618dcd
2 changed files with 15 additions and 26 deletions

View file

@ -132,29 +132,18 @@ finish:
} }
/* If the specified file path starts with / return it, otherwise /* If the specified file path starts with / return it, otherwise
* return path prepended with home directory */ * return path prepended with the config home directory. */
static char *normalize_path(const char *fn) { static int normalize_path(const char *fn, char **_r) {
pa_assert(fn); pa_assert(fn);
pa_assert(_r);
#ifndef OS_IS_WIN32 if (!pa_is_path_absolute(fn))
if (fn[0] != '/') { return pa_append_to_config_home_dir(fn, _r);
#else
if (strlen(fn) < 3 || !IsCharAlpha(fn[0]) || fn[1] != ':' || fn[2] != '\\') {
#endif
char *s;
if (pa_append_to_home_dir(fn, &s) < 0) *_r = pa_xstrdup(fn);
return NULL; return 0;
return s;
}
return pa_xstrdup(fn);
} }
/* Load a cookie from a file in the home directory. If the specified
* path starts with /, use it as absolute path instead. */
int pa_authkey_load(const char *fn, bool create, void *data, size_t length) { int pa_authkey_load(const char *fn, bool create, void *data, size_t length) {
char *p; char *p;
int ret; int ret;
@ -163,8 +152,8 @@ int pa_authkey_load(const char *fn, bool create, void *data, size_t length) {
pa_assert(data); pa_assert(data);
pa_assert(length > 0); pa_assert(length > 0);
if (!(p = normalize_path(fn))) if ((ret = normalize_path(fn, &p)) < 0)
return -2; return ret;
if ((ret = load(p, create, data, length)) < 0) if ((ret = load(p, create, data, length)) < 0)
pa_log_warn("Failed to load authorization key '%s': %s", p, (ret < 0) ? pa_cstrerror(errno) : "File corrupt"); pa_log_warn("Failed to load authorization key '%s': %s", p, (ret < 0) ? pa_cstrerror(errno) : "File corrupt");
@ -177,7 +166,7 @@ int pa_authkey_load(const char *fn, bool create, void *data, size_t length) {
/* Store the specified cookie in the specified cookie file */ /* Store the specified cookie in the specified cookie file */
int pa_authkey_save(const char *fn, const void *data, size_t length) { int pa_authkey_save(const char *fn, const void *data, size_t length) {
int fd = -1; int fd = -1;
int unlock = 0, ret = -1; int unlock = 0, ret;
ssize_t r; ssize_t r;
char *p; char *p;
@ -185,11 +174,12 @@ int pa_authkey_save(const char *fn, const void *data, size_t length) {
pa_assert(data); pa_assert(data);
pa_assert(length > 0); pa_assert(length > 0);
if (!(p = normalize_path(fn))) if ((ret = normalize_path(fn, &p)) < 0)
return -2; return ret;
if ((fd = pa_open_cloexec(p, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { if ((fd = pa_open_cloexec(p, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
pa_log_warn("Failed to open cookie file '%s': %s", fn, pa_cstrerror(errno)); pa_log_warn("Failed to open cookie file '%s': %s", fn, pa_cstrerror(errno));
ret = -1;
goto finish; goto finish;
} }
@ -197,11 +187,10 @@ int pa_authkey_save(const char *fn, const void *data, size_t length) {
if ((r = pa_loop_write(fd, data, length, NULL)) < 0 || (size_t) r != length) { if ((r = pa_loop_write(fd, data, length, NULL)) < 0 || (size_t) r != length) {
pa_log("Failed to read cookie file '%s': %s", fn, pa_cstrerror(errno)); pa_log("Failed to read cookie file '%s': %s", fn, pa_cstrerror(errno));
ret = -1;
goto finish; goto finish;
} }
ret = 0;
finish: finish:
if (fd >= 0) { if (fd >= 0) {

View file

@ -180,7 +180,7 @@ enum {
}; };
#define PA_NATIVE_COOKIE_LENGTH 256 #define PA_NATIVE_COOKIE_LENGTH 256
#define PA_NATIVE_COOKIE_FILE ".config/pulse/cookie" #define PA_NATIVE_COOKIE_FILE "cookie"
#define PA_NATIVE_COOKIE_FILE_FALLBACK ".pulse-cookie" #define PA_NATIVE_COOKIE_FILE_FALLBACK ".pulse-cookie"
#define PA_NATIVE_DEFAULT_PORT 4713 #define PA_NATIVE_DEFAULT_PORT 4713