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