]>
code.delx.au - pulseaudio/blob - polyp/authkey.c
4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
43 /* Generate a new authorization key, store it in file fd and return it in *data */
44 static int generate(int fd
, void *ret_data
, size_t length
) {
46 assert(fd
>= 0 && ret_data
&& length
);
48 pa_random(ret_data
, length
);
50 lseek(fd
, 0, SEEK_SET
);
53 if ((r
= pa_loop_write(fd
, ret_data
, length
)) < 0 || (size_t) r
!= length
) {
54 pa_log(__FILE__
": failed to write cookie file: %s\n", strerror(errno
));
61 /* Load an euthorization cookie from file fn and store it in data. If
62 * the cookie file doesn't exist, create it */
63 static int load(const char *fn
, void *data
, size_t length
) {
68 assert(fn
&& data
&& length
);
70 if ((fd
= open(fn
, O_RDWR
|O_CREAT
, S_IRUSR
|S_IWUSR
)) < 0) {
71 if (errno
!= EACCES
|| (fd
= open(fn
, O_RDONLY
)) < 0) {
72 pa_log(__FILE__
": failed to open cookie file '%s': %s\n", fn
, strerror(errno
));
78 unlock
= pa_lock_fd(fd
, 1) >= 0;
80 if ((r
= pa_loop_read(fd
, data
, length
)) < 0) {
81 pa_log(__FILE__
": failed to read cookie file '%s': %s\n", fn
, strerror(errno
));
85 if ((size_t) r
!= length
) {
88 pa_log(__FILE__
": unable to write cookie to read only file\n");
92 if (generate(fd
, data
, length
) < 0)
111 /* Load a cookie from a cookie file. If the file doesn't exist, create it. */
112 int pa_authkey_load(const char *path
, void *data
, size_t length
) {
115 assert(path
&& data
&& length
);
117 ret
= load(path
, data
, length
);
120 pa_log(__FILE__
": Failed to load authorization key '%s': %s\n", path
,
121 (ret
== -1) ? strerror(errno
) : "file corrupt");
126 /* If the specified file path starts with / return it, otherwise
127 * return path prepended with home directory */
128 static const char *normalize_path(const char *fn
, char *s
, size_t l
) {
129 assert(fn
&& s
&& l
> 0);
132 char homedir
[PATH_MAX
];
133 if (!pa_get_home_dir(homedir
, sizeof(homedir
)))
136 snprintf(s
, l
, "%s/%s", homedir
, fn
);
143 /* Load a cookie from a file in the home directory. If the specified
144 * path starts with /, use it as absolute path instead. */
145 int pa_authkey_load_auto(const char *fn
, void *data
, size_t length
) {
148 assert(fn
&& data
&& length
);
150 if (!(p
= normalize_path(fn
, path
, sizeof(path
))))
153 return pa_authkey_load(p
, data
, length
);
156 /* Store the specified cookie in the speicified cookie file */
157 int pa_authkey_save(const char *fn
, const void *data
, size_t length
) {
159 int unlock
= 0, ret
= -1;
163 assert(fn
&& data
&& length
);
165 if (!(p
= normalize_path(fn
, path
, sizeof(path
))))
168 if ((fd
= open(p
, O_RDWR
|O_CREAT
, S_IRUSR
|S_IWUSR
)) < 0) {
169 pa_log(__FILE__
": failed to open cookie file '%s': %s\n", fn
, strerror(errno
));
173 unlock
= pa_lock_fd(fd
, 1) >= 0;
175 if ((r
= pa_loop_write(fd
, data
, length
)) < 0 || (size_t) r
!= length
) {
176 pa_log(__FILE__
": failed to read cookie file '%s': %s\n", fn
, strerror(errno
));