2004-07-16 19:56:36 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2004-07-16 19:56:36 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
License, or (at your option) any later version.
|
2004-07-16 19:56:36 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2004-11-14 14:58:54 +00:00
|
|
|
Lesser General Public License for more details.
|
2004-07-16 19:56:36 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2006-06-19 21:53:48 +00:00
|
|
|
License along with PulseAudio; if not, write to the Free Software
|
2004-07-16 19:56:36 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-07-06 00:08:44 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <limits.h>
|
2004-09-23 15:57:15 +00:00
|
|
|
#include <sys/stat.h>
|
2004-07-06 00:08:44 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/util.h>
|
|
|
|
|
#include <pulsecore/core-error.h>
|
|
|
|
|
#include <pulsecore/core-util.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
|
|
|
|
#include <pulsecore/random.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-07-06 00:08:44 +00:00
|
|
|
#include "authkey.h"
|
|
|
|
|
|
2004-11-21 21:31:28 +00:00
|
|
|
/* Generate a new authorization key, store it in file fd and return it in *data */
|
2004-12-12 22:58:53 +00:00
|
|
|
static int generate(int fd, void *ret_data, size_t length) {
|
2004-07-06 00:08:44 +00:00
|
|
|
ssize_t r;
|
2004-12-12 22:58:53 +00:00
|
|
|
assert(fd >= 0 && ret_data && length);
|
2004-07-06 00:08:44 +00:00
|
|
|
|
2004-12-12 22:58:53 +00:00
|
|
|
pa_random(ret_data, length);
|
2004-07-06 00:08:44 +00:00
|
|
|
|
2004-09-27 15:40:18 +00:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
2004-11-21 21:31:28 +00:00
|
|
|
ftruncate(fd, 0);
|
2004-09-27 15:40:18 +00:00
|
|
|
|
2006-07-14 22:42:01 +00:00
|
|
|
if ((r = pa_loop_write(fd, ret_data, length, NULL)) < 0 || (size_t) r != length) {
|
2006-05-22 15:20:46 +00:00
|
|
|
pa_log(__FILE__": failed to write cookie file: %s", pa_cstrerror(errno));
|
2004-12-12 22:58:53 +00:00
|
|
|
return -1;
|
2004-07-06 00:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
2004-12-12 22:58:53 +00:00
|
|
|
return 0;
|
2004-09-27 15:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
2006-05-02 09:50:37 +00:00
|
|
|
#ifndef O_BINARY
|
|
|
|
|
#define O_BINARY 0
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-11-21 21:31:28 +00:00
|
|
|
/* Load an euthorization cookie from file fn and store it in data. If
|
|
|
|
|
* the cookie file doesn't exist, create it */
|
2004-09-27 15:40:18 +00:00
|
|
|
static int load(const char *fn, void *data, size_t length) {
|
|
|
|
|
int fd = -1;
|
|
|
|
|
int writable = 1;
|
2006-01-10 17:51:06 +00:00
|
|
|
int unlock = 0, ret = -1;
|
2004-09-27 15:40:18 +00:00
|
|
|
ssize_t r;
|
2004-10-12 21:52:50 +00:00
|
|
|
assert(fn && data && length);
|
2004-09-27 15:40:18 +00:00
|
|
|
|
2006-05-02 09:50:37 +00:00
|
|
|
if ((fd = open(fn, O_RDWR|O_CREAT|O_BINARY, S_IRUSR|S_IWUSR)) < 0) {
|
|
|
|
|
if (errno != EACCES || (fd = open(fn, O_RDONLY|O_BINARY)) < 0) {
|
2006-05-22 15:20:46 +00:00
|
|
|
pa_log(__FILE__": failed to open cookie file '%s': %s", fn, pa_cstrerror(errno));
|
2004-09-27 15:40:18 +00:00
|
|
|
goto finish;
|
|
|
|
|
} else
|
|
|
|
|
writable = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-28 22:47:48 +00:00
|
|
|
unlock = pa_lock_fd(fd, 1) >= 0;
|
2004-09-27 15:40:18 +00:00
|
|
|
|
2006-07-14 22:42:01 +00:00
|
|
|
if ((r = pa_loop_read(fd, data, length, NULL)) < 0) {
|
2006-05-22 15:20:46 +00:00
|
|
|
pa_log(__FILE__": failed to read cookie file '%s': %s", fn, pa_cstrerror(errno));
|
2004-09-27 15:40:18 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((size_t) r != length) {
|
2006-05-02 09:50:37 +00:00
|
|
|
pa_log_debug(__FILE__": got %d bytes from cookie file '%s', expected %d", (int)r, fn, (int)length);
|
2004-09-27 15:40:18 +00:00
|
|
|
|
|
|
|
|
if (!writable) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log(__FILE__": unable to write cookie to read only file");
|
2004-09-27 15:40:18 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (generate(fd, data, length) < 0)
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
|
|
finish:
|
|
|
|
|
|
2004-07-06 00:08:44 +00:00
|
|
|
if (fd >= 0) {
|
2004-09-27 15:40:18 +00:00
|
|
|
|
|
|
|
|
if (unlock)
|
2004-09-28 22:47:48 +00:00
|
|
|
pa_lock_fd(fd, 0);
|
2004-09-27 15:40:18 +00:00
|
|
|
|
2004-07-06 00:08:44 +00:00
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-21 21:31:28 +00:00
|
|
|
/* Load a cookie from a cookie file. If the file doesn't exist, create it. */
|
2004-07-06 00:08:44 +00:00
|
|
|
int pa_authkey_load(const char *path, void *data, size_t length) {
|
2004-09-27 15:40:18 +00:00
|
|
|
int ret;
|
2004-07-06 00:08:44 +00:00
|
|
|
|
|
|
|
|
assert(path && data && length);
|
2004-09-27 15:40:18 +00:00
|
|
|
|
|
|
|
|
ret = load(path, data, length);
|
2004-07-06 00:08:44 +00:00
|
|
|
|
|
|
|
|
if (ret < 0)
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log(__FILE__": Failed to load authorization key '%s': %s", path,
|
2006-05-22 15:20:46 +00:00
|
|
|
(ret == -1) ? pa_cstrerror(errno) : "file corrupt");
|
2004-07-06 00:08:44 +00:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-21 21:31:28 +00:00
|
|
|
/* If the specified file path starts with / return it, otherwise
|
|
|
|
|
* return path prepended with home directory */
|
2004-11-08 23:48:19 +00:00
|
|
|
static const char *normalize_path(const char *fn, char *s, size_t l) {
|
|
|
|
|
assert(fn && s && l > 0);
|
2004-07-06 00:08:44 +00:00
|
|
|
|
2006-01-10 17:51:06 +00:00
|
|
|
#ifndef OS_IS_WIN32
|
2004-09-28 22:47:48 +00:00
|
|
|
if (fn[0] != '/') {
|
2006-01-10 17:51:06 +00:00
|
|
|
#else
|
|
|
|
|
if (strlen(fn) < 3 || !isalpha(fn[0]) || fn[1] != ':' || fn[2] != '\\') {
|
|
|
|
|
#endif
|
2004-11-07 20:48:46 +00:00
|
|
|
char homedir[PATH_MAX];
|
|
|
|
|
if (!pa_get_home_dir(homedir, sizeof(homedir)))
|
2004-11-08 23:48:19 +00:00
|
|
|
return NULL;
|
2004-09-28 22:47:48 +00:00
|
|
|
|
2006-01-10 17:51:06 +00:00
|
|
|
#ifndef OS_IS_WIN32
|
2004-11-08 23:48:19 +00:00
|
|
|
snprintf(s, l, "%s/%s", homedir, fn);
|
2006-01-10 17:51:06 +00:00
|
|
|
#else
|
|
|
|
|
snprintf(s, l, "%s\\%s", homedir, fn);
|
|
|
|
|
#endif
|
2004-11-08 23:48:19 +00:00
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fn;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-21 21:31:28 +00:00
|
|
|
/* 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_auto(const char *fn, void *data, size_t length) {
|
2004-11-08 23:48:19 +00:00
|
|
|
char path[PATH_MAX];
|
|
|
|
|
const char *p;
|
|
|
|
|
assert(fn && data && length);
|
|
|
|
|
|
|
|
|
|
if (!(p = normalize_path(fn, path, sizeof(path))))
|
|
|
|
|
return -2;
|
2004-09-28 22:47:48 +00:00
|
|
|
|
|
|
|
|
return pa_authkey_load(p, data, length);
|
2004-07-06 00:08:44 +00:00
|
|
|
}
|
2004-07-11 22:20:08 +00:00
|
|
|
|
2004-11-21 21:31:28 +00:00
|
|
|
/* Store the specified cookie in the speicified cookie file */
|
2004-11-08 23:48:19 +00:00
|
|
|
int pa_authkey_save(const char *fn, const void *data, size_t length) {
|
|
|
|
|
int fd = -1;
|
|
|
|
|
int unlock = 0, ret = -1;
|
|
|
|
|
ssize_t r;
|
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
const char *p;
|
|
|
|
|
assert(fn && data && length);
|
|
|
|
|
|
|
|
|
|
if (!(p = normalize_path(fn, path, sizeof(path))))
|
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
|
|
if ((fd = open(p, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
|
2006-05-22 15:20:46 +00:00
|
|
|
pa_log(__FILE__": failed to open cookie file '%s': %s", fn, pa_cstrerror(errno));
|
2004-11-08 23:48:19 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unlock = pa_lock_fd(fd, 1) >= 0;
|
|
|
|
|
|
2006-07-14 22:42:01 +00:00
|
|
|
if ((r = pa_loop_write(fd, data, length, NULL)) < 0 || (size_t) r != length) {
|
2006-05-22 15:20:46 +00:00
|
|
|
pa_log(__FILE__": failed to read cookie file '%s': %s", fn, pa_cstrerror(errno));
|
2004-11-08 23:48:19 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
|
|
finish:
|
|
|
|
|
|
|
|
|
|
if (fd >= 0) {
|
|
|
|
|
|
|
|
|
|
if (unlock)
|
|
|
|
|
pa_lock_fd(fd, 0);
|
|
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|