]>
code.delx.au - pulseaudio/blob - polyp/authkey.c
ef395680b236ac2c1bf4cc566df0b88053038401
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 General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 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 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
42 #define RANDOM_DEVICE "/dev/urandom"
44 static int generate(int fd
, void *data
, size_t length
) {
45 int random_fd
, ret
= -1;
47 assert(fd
>= 0 && data
&& length
);
49 if ((random_fd
= open(RANDOM_DEVICE
, O_RDONLY
)) >= 0) {
51 if ((r
= pa_loop_read(random_fd
, data
, length
)) < 0 || (size_t) r
!= length
) {
52 pa_log(__FILE__
": failed to read entropy from '%s'\n", RANDOM_DEVICE
);
59 pa_log(__FILE__
": WARNING: Failed to open entropy device '"RANDOM_DEVICE
"': %s"
60 ", falling back to unsecure pseudo RNG.\n", strerror(errno
));
64 for (p
= data
, l
= length
; l
> 0; p
++, l
--)
65 *p
= (uint8_t) random();
68 lseek(fd
, 0, SEEK_SET
);
70 if ((r
= pa_loop_write(fd
, data
, length
)) < 0 || (size_t) r
!= length
) {
71 pa_log(__FILE__
": failed to write cookie file: %s\n", strerror(errno
));
85 static int load(const char *fn
, void *data
, size_t length
) {
90 assert(fn
&& data
&& length
);
92 if ((fd
= open(fn
, O_RDWR
|O_CREAT
, S_IRUSR
|S_IWUSR
)) < 0) {
93 if (errno
!= EACCES
|| (fd
= open(fn
, O_RDONLY
)) < 0) {
94 pa_log(__FILE__
": failed to open cookie file '%s': %s\n", fn
, strerror(errno
));
100 unlock
= pa_lock_fd(fd
, 1) >= 0;
102 if ((r
= pa_loop_read(fd
, data
, length
)) < 0) {
103 pa_log(__FILE__
": failed to read cookie file '%s': %s\n", fn
, strerror(errno
));
107 if ((size_t) r
!= length
) {
110 pa_log(__FILE__
": unable to write cookie to read only file\n");
114 if (generate(fd
, data
, length
) < 0)
133 int pa_authkey_load(const char *path
, void *data
, size_t length
) {
136 assert(path
&& data
&& length
);
138 ret
= load(path
, data
, length
);
141 pa_log(__FILE__
": Failed to load authorization key '%s': %s\n", path
,
142 (ret
== -1) ? strerror(errno
) : "file corrupt");
147 static const char *normalize_path(const char *fn
, char *s
, size_t l
) {
148 assert(fn
&& s
&& l
> 0);
151 char homedir
[PATH_MAX
];
152 if (!pa_get_home_dir(homedir
, sizeof(homedir
)))
155 snprintf(s
, l
, "%s/%s", homedir
, fn
);
162 int pa_authkey_load_from_home(const char *fn
, void *data
, size_t length
) {
165 assert(fn
&& data
&& length
);
167 if (!(p
= normalize_path(fn
, path
, sizeof(path
))))
170 return pa_authkey_load(p
, data
, length
);
173 int pa_authkey_load_auto(const char *fn
, void *data
, size_t length
) {
174 assert(fn
&& data
&& length
);
177 return pa_authkey_load(fn
, data
, length
);
179 return pa_authkey_load_from_home(fn
, data
, length
);
182 int pa_authkey_save(const char *fn
, const void *data
, size_t length
) {
184 int unlock
= 0, ret
= -1;
188 assert(fn
&& data
&& length
);
190 if (!(p
= normalize_path(fn
, path
, sizeof(path
))))
193 if ((fd
= open(p
, O_RDWR
|O_CREAT
, S_IRUSR
|S_IWUSR
)) < 0) {
194 pa_log(__FILE__
": failed to open cookie file '%s': %s\n", fn
, strerror(errno
));
198 unlock
= pa_lock_fd(fd
, 1) >= 0;
200 if ((r
= pa_loop_write(fd
, data
, length
)) < 0 || (size_t) r
!= length
) {
201 pa_log(__FILE__
": failed to read cookie file '%s': %s\n", fn
, strerror(errno
));