]> code.delx.au - pulseaudio/blob - src/pulsecore/authkey.c
big s/polyp/pulse/g
[pulseaudio] / src / pulsecore / authkey.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 PulseAudio 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 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; 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 <pulse/util.h>
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/random.h>
43
44 #include "authkey.h"
45
46 /* Generate a new authorization key, store it in file fd and return it in *data */
47 static int generate(int fd, void *ret_data, size_t length) {
48 ssize_t r;
49 assert(fd >= 0 && ret_data && length);
50
51 pa_random(ret_data, length);
52
53 lseek(fd, 0, SEEK_SET);
54 ftruncate(fd, 0);
55
56 if ((r = pa_loop_write(fd, ret_data, length)) < 0 || (size_t) r != length) {
57 pa_log(__FILE__": failed to write cookie file: %s", pa_cstrerror(errno));
58 return -1;
59 }
60
61 return 0;
62 }
63
64 #ifndef O_BINARY
65 #define O_BINARY 0
66 #endif
67
68 /* Load an euthorization cookie from file fn and store it in data. If
69 * the cookie file doesn't exist, create it */
70 static int load(const char *fn, void *data, size_t length) {
71 int fd = -1;
72 int writable = 1;
73 int unlock = 0, ret = -1;
74 ssize_t r;
75 assert(fn && data && length);
76
77 if ((fd = open(fn, O_RDWR|O_CREAT|O_BINARY, S_IRUSR|S_IWUSR)) < 0) {
78 if (errno != EACCES || (fd = open(fn, O_RDONLY|O_BINARY)) < 0) {
79 pa_log(__FILE__": failed to open cookie file '%s': %s", fn, pa_cstrerror(errno));
80 goto finish;
81 } else
82 writable = 0;
83 }
84
85 unlock = pa_lock_fd(fd, 1) >= 0;
86
87 if ((r = pa_loop_read(fd, data, length)) < 0) {
88 pa_log(__FILE__": failed to read cookie file '%s': %s", fn, pa_cstrerror(errno));
89 goto finish;
90 }
91
92 if ((size_t) r != length) {
93 pa_log_debug(__FILE__": got %d bytes from cookie file '%s', expected %d", (int)r, fn, (int)length);
94
95 if (!writable) {
96 pa_log(__FILE__": unable to write cookie to read only file");
97 goto finish;
98 }
99
100 if (generate(fd, data, length) < 0)
101 goto finish;
102 }
103
104 ret = 0;
105
106 finish:
107
108 if (fd >= 0) {
109
110 if (unlock)
111 pa_lock_fd(fd, 0);
112
113 close(fd);
114 }
115
116 return ret;
117 }
118
119 /* Load a cookie from a cookie file. If the file doesn't exist, create it. */
120 int pa_authkey_load(const char *path, void *data, size_t length) {
121 int ret;
122
123 assert(path && data && length);
124
125 ret = load(path, data, length);
126
127 if (ret < 0)
128 pa_log(__FILE__": Failed to load authorization key '%s': %s", path,
129 (ret == -1) ? pa_cstrerror(errno) : "file corrupt");
130
131 return ret;
132 }
133
134 /* If the specified file path starts with / return it, otherwise
135 * return path prepended with home directory */
136 static const char *normalize_path(const char *fn, char *s, size_t l) {
137 assert(fn && s && l > 0);
138
139 #ifndef OS_IS_WIN32
140 if (fn[0] != '/') {
141 #else
142 if (strlen(fn) < 3 || !isalpha(fn[0]) || fn[1] != ':' || fn[2] != '\\') {
143 #endif
144 char homedir[PATH_MAX];
145 if (!pa_get_home_dir(homedir, sizeof(homedir)))
146 return NULL;
147
148 #ifndef OS_IS_WIN32
149 snprintf(s, l, "%s/%s", homedir, fn);
150 #else
151 snprintf(s, l, "%s\\%s", homedir, fn);
152 #endif
153 return s;
154 }
155
156 return fn;
157 }
158
159 /* Load a cookie from a file in the home directory. If the specified
160 * path starts with /, use it as absolute path instead. */
161 int pa_authkey_load_auto(const char *fn, void *data, size_t length) {
162 char path[PATH_MAX];
163 const char *p;
164 assert(fn && data && length);
165
166 if (!(p = normalize_path(fn, path, sizeof(path))))
167 return -2;
168
169 return pa_authkey_load(p, data, length);
170 }
171
172 /* Store the specified cookie in the speicified cookie file */
173 int pa_authkey_save(const char *fn, const void *data, size_t length) {
174 int fd = -1;
175 int unlock = 0, ret = -1;
176 ssize_t r;
177 char path[PATH_MAX];
178 const char *p;
179 assert(fn && data && length);
180
181 if (!(p = normalize_path(fn, path, sizeof(path))))
182 return -2;
183
184 if ((fd = open(p, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
185 pa_log(__FILE__": failed to open cookie file '%s': %s", fn, pa_cstrerror(errno));
186 goto finish;
187 }
188
189 unlock = pa_lock_fd(fd, 1) >= 0;
190
191 if ((r = pa_loop_write(fd, data, length)) < 0 || (size_t) r != length) {
192 pa_log(__FILE__": failed to read cookie file '%s': %s", fn, pa_cstrerror(errno));
193 goto finish;
194 }
195
196 ret = 0;
197
198 finish:
199
200 if (fd >= 0) {
201
202 if (unlock)
203 pa_lock_fd(fd, 0);
204
205 close(fd);
206 }
207
208 return ret;
209 }