]> code.delx.au - pulseaudio/blob - src/authkey.c
rename configuration file
[pulseaudio] / src / authkey.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <inttypes.h>
33 #include <stdlib.h>
34 #include <time.h>
35 #include <limits.h>
36
37 #include "authkey.h"
38 #include "util.h"
39
40 #define RANDOM_DEVICE "/dev/urandom"
41
42 static int load(const char *fn, void *data, size_t length) {
43 int fd = -1, ret = -1;
44 ssize_t r;
45
46 assert(fn && data && length);
47
48 if ((fd = open(fn, O_RDONLY)) < 0)
49 goto finish;
50
51 if ((r = pa_loop_read(fd, data, length)) < 0 || (size_t) r != length) {
52 ret = -2;
53 goto finish;
54 }
55
56 ret = 0;
57
58 finish:
59 if (fd >= 0)
60 close(fd);
61
62 return ret;
63 }
64
65 static int generate(const char *fn, void *data, size_t length) {
66 int fd = -1, random_fd = -1, ret = -1;
67 ssize_t r;
68 assert(fn && data && length);
69
70 if ((fd = open(fn, O_WRONLY|O_EXCL|O_CREAT, S_IRUSR | S_IWUSR)) < 0)
71 goto finish;
72
73 if ((random_fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
74
75 if ((r = pa_loop_read(random_fd, data, length)) < 0 || (size_t) r != length) {
76 ret = -2;
77 goto finish;
78 }
79
80 } else {
81 uint8_t *p;
82 size_t l;
83 fprintf(stderr, "WARNING: Failed to open entropy device '"RANDOM_DEVICE"': %s, falling back to unsecure pseudo RNG.\n", strerror(errno));
84
85 srandom(time(NULL));
86
87 for (p = data, l = length; l > 0; p++, l--)
88 *p = (uint8_t) random();
89 }
90
91 if ((r = pa_loop_write(fd, data, length)) < 0 || (size_t) r != length) {
92 ret = -2;
93 goto finish;
94 }
95
96 ret = 0;
97
98 finish:
99 if (fd >= 0) {
100 if (ret != 0)
101 unlink(fn);
102 close(fd);
103 }
104 if (random_fd >= 0)
105 close(random_fd);
106
107 return ret;
108 }
109
110 int pa_authkey_load(const char *path, void *data, size_t length) {
111 int ret, i;
112
113 assert(path && data && length);
114
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)
120 continue;
121 break;
122 }
123
124 if (ret < 0)
125 fprintf(stderr, "Failed to load authorization key '%s': %s\n", path, (ret == -1) ? strerror(errno) : "file corrupt");
126
127 return ret;
128 }
129
130 int pa_authkey_load_from_home(const char *fn, void *data, size_t length) {
131 char *home;
132 char path[PATH_MAX];
133
134 assert(fn && data && length);
135
136 if (!(home = getenv("HOME")))
137 return -2;
138
139 snprintf(path, sizeof(path), "%s/%s", home, fn);
140
141 return pa_authkey_load(path, data, length);
142 }
143
144 int pa_authkey_load_auto(const char *fn, void *data, size_t length) {
145 assert(fn && data && length);
146
147 if (*fn == '/')
148 return pa_authkey_load(fn, data, length);
149 else
150 return pa_authkey_load_from_home(fn, data, length);
151 }