]>
code.delx.au - pulseaudio/blob - src/pulsecore/authkey.c
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
35 #include <pulse/util.h>
36 #include <pulse/xmalloc.h>
37 #include <pulsecore/core-error.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/random.h>
41 #include <pulsecore/macro.h>
45 /* Generate a new authorization key, store it in file fd and return it in *data */
46 static int generate(int fd
, void *ret_data
, size_t length
) {
51 pa_assert(length
> 0);
53 pa_random(ret_data
, length
);
55 lseek(fd
, (off_t
) 0, SEEK_SET
);
56 if (ftruncate(fd
, (off_t
) 0) < 0) {
57 pa_log("Failed to truncate cookie file: %s", pa_cstrerror(errno
));
61 if ((r
= pa_loop_write(fd
, ret_data
, length
, NULL
)) < 0 || (size_t) r
!= length
) {
62 pa_log("Failed to write cookie file: %s", pa_cstrerror(errno
));
73 /* Load an authorization cookie from file fn and store it in data. If
74 * the cookie file doesn't exist, create it */
75 static int load(const char *fn
, bool create
, void *data
, size_t length
) {
78 int unlock
= 0, ret
= -1;
83 pa_assert(length
> 0);
86 pa_make_secure_parent_dir(fn
, pa_in_system_mode() ? 0755U : 0700U, -1, -1, false);
88 if ((fd
= pa_open_cloexec(fn
, (create
? O_RDWR
|O_CREAT
: O_RDONLY
)|O_BINARY
, S_IRUSR
|S_IWUSR
)) < 0) {
90 if (!create
|| errno
!= EACCES
|| (fd
= open(fn
, O_RDONLY
|O_BINARY
)) < 0) {
91 pa_log_warn("Failed to open cookie file '%s': %s", fn
, pa_cstrerror(errno
));
97 unlock
= pa_lock_fd(fd
, 1) >= 0;
99 if ((r
= pa_loop_read(fd
, data
, length
, NULL
)) < 0) {
100 pa_log("Failed to read cookie file '%s': %s", fn
, pa_cstrerror(errno
));
104 if ((size_t) r
!= length
) {
105 pa_log_debug("Got %d bytes from cookie file '%s', expected %d", (int) r
, fn
, (int) length
);
108 pa_log_warn("Unable to write cookie to read-only file");
112 if (generate(fd
, data
, length
) < 0)
125 if (pa_close(fd
) < 0) {
126 pa_log_warn("Failed to close cookie file: %s", pa_cstrerror(errno
));
134 /* Load a cookie from a cookie file. If the file doesn't exist, create it. */
135 int pa_authkey_load(const char *path
, bool create
, void *data
, size_t length
) {
140 pa_assert(length
> 0);
142 if ((ret
= load(path
, create
, data
, length
)) < 0)
143 pa_log_warn("Failed to load authorization key '%s': %s", path
, (ret
< 0) ? pa_cstrerror(errno
) : "File corrupt");
148 /* If the specified file path starts with / return it, otherwise
149 * return path prepended with home directory */
150 static char *normalize_path(const char *fn
) {
157 if (strlen(fn
) < 3 || !IsCharAlpha(fn
[0]) || fn
[1] != ':' || fn
[2] != '\\') {
161 if (!(homedir
= pa_get_home_dir_malloc()))
164 s
= pa_sprintf_malloc("%s" PA_PATH_SEP
"%s", homedir
, fn
);
170 return pa_xstrdup(fn
);
173 /* Load a cookie from a file in the home directory. If the specified
174 * path starts with /, use it as absolute path instead. */
175 int pa_authkey_load_auto(const char *fn
, bool create
, void *data
, size_t length
) {
181 pa_assert(length
> 0);
183 if (!(p
= normalize_path(fn
)))
186 ret
= pa_authkey_load(p
, create
, data
, length
);
192 /* Store the specified cookie in the specified cookie file */
193 int pa_authkey_save(const char *fn
, const void *data
, size_t length
) {
195 int unlock
= 0, ret
= -1;
201 pa_assert(length
> 0);
203 if (!(p
= normalize_path(fn
)))
206 if ((fd
= pa_open_cloexec(p
, O_RDWR
|O_CREAT
, S_IRUSR
|S_IWUSR
)) < 0) {
207 pa_log_warn("Failed to open cookie file '%s': %s", fn
, pa_cstrerror(errno
));
211 unlock
= pa_lock_fd(fd
, 1) >= 0;
213 if ((r
= pa_loop_write(fd
, data
, length
, NULL
)) < 0 || (size_t) r
!= length
) {
214 pa_log("Failed to read cookie file '%s': %s", fn
, pa_cstrerror(errno
));
227 if (pa_close(fd
) < 0) {
228 pa_log_warn("Failed to close cookie file: %s", pa_cstrerror(errno
));