]>
code.delx.au - pulseaudio/blob - src/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
40 #define RANDOM_DEVICE "/dev/urandom"
42 static int load(const char *fn
, void *data
, size_t length
) {
43 int fd
= -1, ret
= -1;
46 assert(fn
&& data
&& length
);
48 if ((fd
= open(fn
, O_RDONLY
)) < 0)
51 if ((r
= pa_loop_read(fd
, data
, length
)) < 0 || (size_t) r
!= length
) {
65 static int generate(const char *fn
, void *data
, size_t length
) {
66 int fd
= -1, random_fd
= -1, ret
= -1;
68 assert(fn
&& data
&& length
);
70 if ((fd
= open(fn
, O_WRONLY
|O_EXCL
|O_CREAT
, S_IRUSR
| S_IWUSR
)) < 0)
73 if ((random_fd
= open(RANDOM_DEVICE
, O_RDONLY
)) >= 0) {
75 if ((r
= pa_loop_read(random_fd
, data
, length
)) < 0 || (size_t) r
!= length
) {
83 fprintf(stderr
, "WARNING: Failed to open entropy device '"RANDOM_DEVICE
"': %s, falling back to unsecure pseudo RNG.\n", strerror(errno
));
87 for (p
= data
, l
= length
; l
> 0; p
++, l
--)
88 *p
= (uint8_t) random();
91 if ((r
= pa_loop_write(fd
, data
, length
)) < 0 || (size_t) r
!= length
) {
110 int pa_authkey_load(const char *path
, void *data
, size_t length
) {
113 assert(path
&& data
&& length
);
115 for (i
= 0; i
< 10; i
++) {
116 if ((ret
= load(path
, data
, length
)) < 0)
117 if (ret
== -1 && errno
== ENOENT
)
118 if ((ret
= generate(path
, data
, length
)) < 0)
119 if (ret
== -1 && errno
== EEXIST
)
125 fprintf(stderr
, "Failed to load authorization key '%s': %s\n", path
, (ret
== -1) ? strerror(errno
) : "file corrupt");
130 int pa_authkey_load_from_home(const char *fn
, void *data
, size_t length
) {
134 assert(fn
&& data
&& length
);
136 if (!(home
= getenv("HOME")))
139 snprintf(path
, sizeof(path
), "%s/%s", home
, fn
);
141 return pa_authkey_load(path
, data
, length
);
144 int pa_authkey_load_auto(const char *fn
, void *data
, size_t length
) {
145 assert(fn
&& data
&& length
);
148 return pa_authkey_load(fn
, data
, length
);
150 return pa_authkey_load_from_home(fn
, data
, length
);