]> code.delx.au - pulseaudio/blob - src/polypcore/authkey.c
Reorganised the source tree. We now have src/ with a couple of subdirs:
[pulseaudio] / src / polypcore / 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 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 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 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 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 #include "random.h"
42
43 /* Generate a new authorization key, store it in file fd and return it in *data */
44 static int generate(int fd, void *ret_data, size_t length) {
45 ssize_t r;
46 assert(fd >= 0 && ret_data && length);
47
48 pa_random(ret_data, length);
49
50 lseek(fd, 0, SEEK_SET);
51 ftruncate(fd, 0);
52
53 if ((r = pa_loop_write(fd, ret_data, length)) < 0 || (size_t) r != length) {
54 pa_log(__FILE__": failed to write cookie file: %s\n", strerror(errno));
55 return -1;
56 }
57
58 return 0;
59 }
60
61 /* Load an euthorization cookie from file fn and store it in data. If
62 * the cookie file doesn't exist, create it */
63 static int load(const char *fn, void *data, size_t length) {
64 int fd = -1;
65 int writable = 1;
66 int unlock = 0, ret = -1;
67 ssize_t r;
68 assert(fn && data && length);
69
70 if ((fd = open(fn, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
71 if (errno != EACCES || (fd = open(fn, O_RDONLY)) < 0) {
72 pa_log(__FILE__": failed to open cookie file '%s': %s\n", fn, strerror(errno));
73 goto finish;
74 } else
75 writable = 0;
76 }
77
78 unlock = pa_lock_fd(fd, 1) >= 0;
79
80 if ((r = pa_loop_read(fd, data, length)) < 0) {
81 pa_log(__FILE__": failed to read cookie file '%s': %s\n", fn, strerror(errno));
82 goto finish;
83 }
84
85 if ((size_t) r != length) {
86
87 if (!writable) {
88 pa_log(__FILE__": unable to write cookie to read only file\n");
89 goto finish;
90 }
91
92 if (generate(fd, data, length) < 0)
93 goto finish;
94 }
95
96 ret = 0;
97
98 finish:
99
100 if (fd >= 0) {
101
102 if (unlock)
103 pa_lock_fd(fd, 0);
104
105 close(fd);
106 }
107
108 return ret;
109 }
110
111 /* Load a cookie from a cookie file. If the file doesn't exist, create it. */
112 int pa_authkey_load(const char *path, void *data, size_t length) {
113 int ret;
114
115 assert(path && data && length);
116
117 ret = load(path, data, length);
118
119 if (ret < 0)
120 pa_log(__FILE__": Failed to load authorization key '%s': %s\n", path,
121 (ret == -1) ? strerror(errno) : "file corrupt");
122
123 return ret;
124 }
125
126 /* If the specified file path starts with / return it, otherwise
127 * return path prepended with home directory */
128 static const char *normalize_path(const char *fn, char *s, size_t l) {
129 assert(fn && s && l > 0);
130
131 #ifndef OS_IS_WIN32
132 if (fn[0] != '/') {
133 #else
134 if (strlen(fn) < 3 || !isalpha(fn[0]) || fn[1] != ':' || fn[2] != '\\') {
135 #endif
136 char homedir[PATH_MAX];
137 if (!pa_get_home_dir(homedir, sizeof(homedir)))
138 return NULL;
139
140 #ifndef OS_IS_WIN32
141 snprintf(s, l, "%s/%s", homedir, fn);
142 #else
143 snprintf(s, l, "%s\\%s", homedir, fn);
144 #endif
145 return s;
146 }
147
148 return fn;
149 }
150
151 /* Load a cookie from a file in the home directory. If the specified
152 * path starts with /, use it as absolute path instead. */
153 int pa_authkey_load_auto(const char *fn, void *data, size_t length) {
154 char path[PATH_MAX];
155 const char *p;
156 assert(fn && data && length);
157
158 if (!(p = normalize_path(fn, path, sizeof(path))))
159 return -2;
160
161 return pa_authkey_load(p, data, length);
162 }
163
164 /* Store the specified cookie in the speicified cookie file */
165 int pa_authkey_save(const char *fn, const void *data, size_t length) {
166 int fd = -1;
167 int unlock = 0, ret = -1;
168 ssize_t r;
169 char path[PATH_MAX];
170 const char *p;
171 assert(fn && data && length);
172
173 if (!(p = normalize_path(fn, path, sizeof(path))))
174 return -2;
175
176 if ((fd = open(p, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
177 pa_log(__FILE__": failed to open cookie file '%s': %s\n", fn, strerror(errno));
178 goto finish;
179 }
180
181 unlock = pa_lock_fd(fd, 1) >= 0;
182
183 if ((r = pa_loop_write(fd, data, length)) < 0 || (size_t) r != length) {
184 pa_log(__FILE__": failed to read cookie file '%s': %s\n", fn, strerror(errno));
185 goto finish;
186 }
187
188 ret = 0;
189
190 finish:
191
192 if (fd >= 0) {
193
194 if (unlock)
195 pa_lock_fd(fd, 0);
196
197 close(fd);
198 }
199
200 return ret;
201 }