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