source: roaraudio/libroar/authfile.c @ 4488:13eb2a20987a

Last change on this file since 4488:13eb2a20987a was 4488:13eb2a20987a, checked in by phi, 13 years ago

support for writing ESD and PulseAudio? auth cookies

File size: 5.8 KB
Line 
1//authfile.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38struct roar_authfile {
39 size_t refc;
40 size_t lockc;
41 int type;
42 int rw;
43 int version;
44 size_t magiclen;
45 struct roar_vio_calls vio;
46};
47
48struct roar_authfile * roar_authfile_open(int type, const char * filename, int rw, int version) {
49 struct roar_vio_defaults def;
50 struct roar_authfile * ret;
51 size_t magiclen = 0;
52
53 switch (type) {
54  case ROAR_AUTHFILE_TYPE_ESD:
55  case ROAR_AUTHFILE_TYPE_PULSE:
56   break;
57  default:
58    return NULL;
59   break;
60 }
61
62 if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, rw ? O_RDWR|O_CREAT : O_RDONLY, 0600) == -1 )
63  return NULL;
64
65 if ( (ret = roar_mm_malloc(sizeof(struct roar_authfile))) == NULL )
66  return NULL;
67
68 memset(ret, 0, sizeof(struct roar_authfile));
69
70 ret->refc     = 1;
71 ret->lockc    = 0;
72 ret->type     = type;
73 ret->rw       = rw;
74 ret->version  = version;
75 ret->magiclen = magiclen;
76
77 filename = roar_mm_strdup(filename);
78
79 if ( roar_vio_open_dstr(&(ret->vio), (char*)filename, &def, 1) == -1 ) {
80  roar_mm_free((void*)filename);
81  roar_mm_free(ret);
82  return NULL;
83 }
84
85 roar_mm_free((void*)filename);
86
87 return ret;
88}
89
90int roar_authfile_close(struct roar_authfile * authfile) {
91 if ( authfile == NULL )
92  return -1;
93
94 roar_vio_close(&(authfile->vio));
95
96 roar_mm_free(authfile);
97
98 return 0;
99}
100
101int roar_authfile_lock(struct roar_authfile * authfile) {
102 if ( authfile == NULL )
103  return -1;
104
105 // TODO: implement real locking here.
106 return 0;
107}
108
109int roar_authfile_unlock(struct roar_authfile * authfile) {
110 if ( authfile == NULL )
111  return -1;
112
113 // TODO: implement real unlocking here.
114 return 0;
115}
116
117int roar_authfile_sync(struct roar_authfile * authfile) {
118 if ( authfile == NULL )
119  return -1;
120
121 return roar_vio_sync(&(authfile->vio));
122}
123
124
125struct roar_authfile_key * roar_authfile_key_new(int type, size_t len, const char * addr) {
126 struct roar_authfile_key * ret;
127 size_t addrlen;
128 size_t retlen;
129
130 if ( addr == NULL ) {
131  addrlen = 0;
132 } else {
133  addrlen = strlen(addr) + 1;
134 }
135
136 retlen = sizeof(struct roar_authfile_key) + len + addrlen;
137
138 if ( (ret = roar_mm_malloc(retlen)) == NULL )
139  return NULL;
140
141 memset(ret, 0, retlen);
142
143 ret->refc  = 1;
144 ret->type  = type;
145 ret->index = -1;
146
147 if ( addrlen == 0 ) {
148  ret->address = NULL;
149 } else {
150  ret->address = (const void *)ret+sizeof(struct roar_authfile_key);
151  memcpy((void*)ret->address, addr, addrlen);
152 }
153
154 ret->data = ret + sizeof(struct roar_authfile_key) + addrlen;
155
156 ret->len  = len;
157
158 return ret;
159}
160
161int roar_authfile_key_ref(struct roar_authfile_key * key) {
162 if ( key == NULL )
163  return -1;
164
165 key->refc++;
166
167 return 0;
168}
169
170int roar_authfile_key_unref(struct roar_authfile_key * key) {
171 if ( key == NULL )
172  return -1;
173
174 if ( key->refc == 0 ) {
175  ROAR_ERR("roar_authfile_key_unref(key=%p): Key has reference count of zero. This is bad. assuming refc=1", key);
176  key->refc = 1;
177 }
178
179 key->refc--;
180
181 if ( key->refc > 0 )
182  return 0;
183
184 roar_mm_free(key);
185
186 return 0;
187}
188
189int roar_authfile_add_key(struct roar_authfile * authfile, struct roar_authfile_key * key) {
190 if ( authfile == NULL || key == NULL )
191  return -1;
192
193 switch (authfile->type) {
194  case ROAR_AUTHFILE_TYPE_ESD:
195  case ROAR_AUTHFILE_TYPE_PULSE:
196    if ( key->type != ROAR_AUTH_T_COOKIE || (key->index != 0 && key->index != -1) )
197     return -1;
198    if ( roar_authfile_sync(authfile) == -1 )
199     return -1;
200    if ( roar_authfile_lock(authfile) == -1 )
201     return -1;
202    if ( roar_vio_lseek(&(authfile->vio), 0, SEEK_SET) != 0 )
203     return -1;
204    if ( roar_vio_write(&(authfile->vio), key->data, key->len) != key->len )
205     return -1;
206    if ( roar_authfile_unlock(authfile) == -1 )
207     return -1;
208    return 0;
209   break;
210  default:
211    return -1;
212   break;
213 }
214}
215
216struct roar_authfile_key * roar_authfile_lookup_key(struct roar_authfile * authfile,
217                                                    int type, int minindex, const char * address) {
218 struct roar_authfile_key * ret = NULL;
219 ssize_t len;
220
221 if ( authfile == NULL )
222  return NULL;
223
224 switch (authfile->type) {
225  case ROAR_AUTHFILE_TYPE_ESD:
226  case ROAR_AUTHFILE_TYPE_PULSE:
227    if ( type != ROAR_AUTH_T_COOKIE || minindex > 0 )
228     return NULL;
229
230    if ( (ret = roar_authfile_key_new(type, 256, NULL)) == NULL )
231     return NULL;
232
233    len = roar_vio_read(&(authfile->vio), ret->data, ret->len);
234
235    if ( len == -1 ) {
236     roar_authfile_key_unref(ret);
237     return NULL;
238    }
239
240    ret->len   = len;
241    ret->index = 0;
242   break;
243  default:
244    return NULL;
245   break;
246 }
247
248 return ret;
249}
250
251//ll
Note: See TracBrowser for help on using the repository browser.