]> code.delx.au - pulseaudio/blob - polyp/authkey.c
ef395680b236ac2c1bf4cc566df0b88053038401
[pulseaudio] / polyp / 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 #include <sys/stat.h>
37
38 #include "authkey.h"
39 #include "util.h"
40 #include "log.h"
41
42 #define RANDOM_DEVICE "/dev/urandom"
43
44 static int generate(int fd, void *data, size_t length) {
45 int random_fd, ret = -1;
46 ssize_t r;
47 assert(fd >= 0 && data && length);
48
49 if ((random_fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
50
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);
53 goto finish;
54 }
55
56 } else {
57 uint8_t *p;
58 size_t l;
59 pa_log(__FILE__": WARNING: Failed to open entropy device '"RANDOM_DEVICE"': %s"
60 ", falling back to unsecure pseudo RNG.\n", strerror(errno));
61
62 srandom(time(NULL));
63
64 for (p = data, l = length; l > 0; p++, l--)
65 *p = (uint8_t) random();
66 }
67
68 lseek(fd, 0, SEEK_SET);
69
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));
72 goto finish;
73 }
74
75 ret = 0;
76
77 finish:
78
79 if (random_fd >= 0)
80 close(random_fd);
81
82 return ret;
83 }
84
85 static int load(const char *fn, void *data, size_t length) {
86 int fd = -1;
87 int writable = 1;
88 int unlock = 0, ret;
89 ssize_t r;
90 assert(fn && data && length);
91
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));
95 goto finish;
96 } else
97 writable = 0;
98 }
99
100 unlock = pa_lock_fd(fd, 1) >= 0;
101
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));
104 goto finish;
105 }
106
107 if ((size_t) r != length) {
108
109 if (!writable) {
110 pa_log(__FILE__": unable to write cookie to read only file\n");
111 goto finish;
112 }
113
114 if (generate(fd, data, length) < 0)
115 goto finish;
116 }
117
118 ret = 0;
119
120 finish:
121
122 if (fd >= 0) {
123
124 if (unlock)
125 pa_lock_fd(fd, 0);
126
127 close(fd);
128 }
129
130 return ret;
131 }
132
133 int pa_authkey_load(const char *path, void *data, size_t length) {
134 int ret;
135
136 assert(path && data && length);
137
138 ret = load(path, data, length);
139
140 if (ret < 0)
141 pa_log(__FILE__": Failed to load authorization key '%s': %s\n", path,
142 (ret == -1) ? strerror(errno) : "file corrupt");
143
144 return ret;
145 }
146
147 static const char *normalize_path(const char *fn, char *s, size_t l) {
148 assert(fn && s && l > 0);
149
150 if (fn[0] != '/') {
151 char homedir[PATH_MAX];
152 if (!pa_get_home_dir(homedir, sizeof(homedir)))
153 return NULL;
154
155 snprintf(s, l, "%s/%s", homedir, fn);
156 return s;
157 }
158
159 return fn;
160 }
161
162 int pa_authkey_load_from_home(const char *fn, void *data, size_t length) {
163 char path[PATH_MAX];
164 const char *p;
165 assert(fn && data && length);
166
167 if (!(p = normalize_path(fn, path, sizeof(path))))
168 return -2;
169
170 return pa_authkey_load(p, data, length);
171 }
172
173 int pa_authkey_load_auto(const char *fn, void *data, size_t length) {
174 assert(fn && data && length);
175
176 if (*fn == '/')
177 return pa_authkey_load(fn, data, length);
178 else
179 return pa_authkey_load_from_home(fn, data, length);
180 }
181
182 int pa_authkey_save(const char *fn, const void *data, size_t length) {
183 int fd = -1;
184 int unlock = 0, ret = -1;
185 ssize_t r;
186 char path[PATH_MAX];
187 const char *p;
188 assert(fn && data && length);
189
190 if (!(p = normalize_path(fn, path, sizeof(path))))
191 return -2;
192
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));
195 goto finish;
196 }
197
198 unlock = pa_lock_fd(fd, 1) >= 0;
199
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));
202 goto finish;
203 }
204
205 ret = 0;
206
207 finish:
208
209 if (fd >= 0) {
210
211 if (unlock)
212 pa_lock_fd(fd, 0);
213
214 close(fd);
215 }
216
217 return ret;
218 }