]>
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 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
41 #define RANDOM_DEVICE "/dev/urandom"
43 static int load(const char *fn
, void *data
, size_t length
) {
44 int fd
= -1, ret
= -1;
47 assert(fn
&& data
&& length
);
49 if ((fd
= open(fn
, O_RDONLY
)) < 0)
52 if ((r
= pa_loop_read(fd
, data
, length
)) < 0 || (size_t) r
!= length
) {
66 static int generate(const char *fn
, void *data
, size_t length
) {
67 int fd
= -1, random_fd
= -1, ret
= -1;
69 assert(fn
&& data
&& length
);
71 if ((fd
= open(fn
, O_WRONLY
|O_EXCL
|O_CREAT
, S_IRUSR
| S_IWUSR
)) < 0)
74 if ((random_fd
= open(RANDOM_DEVICE
, O_RDONLY
)) >= 0) {
76 if ((r
= pa_loop_read(random_fd
, data
, length
)) < 0 || (size_t) r
!= length
) {
84 pa_log(__FILE__
": WARNING: Failed to open entropy device '"RANDOM_DEVICE
"': %s, falling back to unsecure pseudo RNG.\n", strerror(errno
));
88 for (p
= data
, l
= length
; l
> 0; p
++, l
--)
89 *p
= (uint8_t) random();
92 if ((r
= pa_loop_write(fd
, data
, length
)) < 0 || (size_t) r
!= length
) {
111 int pa_authkey_load(const char *path
, void *data
, size_t length
) {
114 assert(path
&& data
&& length
);
116 for (i
= 0; i
< 10; i
++) {
117 if ((ret
= load(path
, data
, length
)) < 0)
118 if (ret
== -1 && errno
== ENOENT
)
119 if ((ret
= generate(path
, data
, length
)) < 0)
120 if (ret
== -1 && errno
== EEXIST
)
126 pa_log(__FILE__
": Failed to load authorization key '%s': %s\n", path
, (ret
== -1) ? strerror(errno
) : "file corrupt");
131 int pa_authkey_load_from_home(const char *fn
, void *data
, size_t length
) {
135 assert(fn
&& data
&& length
);
137 if (!(home
= getenv("HOME")))
140 snprintf(path
, sizeof(path
), "%s/%s", home
, fn
);
142 return pa_authkey_load(path
, data
, length
);
145 int pa_authkey_load_auto(const char *fn
, void *data
, size_t length
) {
146 assert(fn
&& data
&& length
);
149 return pa_authkey_load(fn
, data
, length
);
151 return pa_authkey_load_from_home(fn
, data
, length
);