source: roaraudio/libroar/authfile.c @ 4938:bb074f761e86

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

some minor fixes for win32

File size: 6.5 KB
Line 
1//authfile.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011
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#ifdef ROAR_HAVE_STAT
53 struct stat filestat;
54#endif
55
56 if ( type == ROAR_AUTHFILE_TYPE_AUTO ) {
57#ifdef ROAR_HAVE_STAT
58  if ( stat(filename, &filestat) != 0 )
59   return NULL;
60
61  if ( filestat.st_size == 16 ) {
62   type = ROAR_AUTHFILE_TYPE_ESD;
63  } else if ( filestat.st_size == 256 ) {
64   type = ROAR_AUTHFILE_TYPE_PULSE;
65  } else {
66   return NULL;
67  }
68#else
69  return NULL;
70#endif
71 }
72
73 switch (type) {
74  case ROAR_AUTHFILE_TYPE_ESD:
75  case ROAR_AUTHFILE_TYPE_PULSE:
76   break;
77  default:
78    return NULL;
79   break;
80 }
81
82 if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, rw ? O_RDWR|O_CREAT : O_RDONLY, 0600) == -1 )
83  return NULL;
84
85 if ( (ret = roar_mm_malloc(sizeof(struct roar_authfile))) == NULL )
86  return NULL;
87
88 memset(ret, 0, sizeof(struct roar_authfile));
89
90 ret->refc     = 1;
91 ret->lockc    = 0;
92 ret->type     = type;
93 ret->rw       = rw;
94 ret->version  = version;
95 ret->magiclen = magiclen;
96
97 filename = roar_mm_strdup(filename);
98
99 if ( roar_vio_open_dstr(&(ret->vio), (char*)filename, &def, 1) == -1 ) {
100  roar_mm_free((void*)filename);
101  roar_mm_free(ret);
102  return NULL;
103 }
104
105 roar_mm_free((void*)filename);
106
107 return ret;
108}
109
110int roar_authfile_close(struct roar_authfile * authfile) {
111 if ( authfile == NULL )
112  return -1;
113
114 roar_vio_close(&(authfile->vio));
115
116 roar_mm_free(authfile);
117
118 return 0;
119}
120
121int roar_authfile_lock(struct roar_authfile * authfile) {
122 if ( authfile == NULL )
123  return -1;
124
125 // TODO: implement real locking here.
126 return 0;
127}
128
129int roar_authfile_unlock(struct roar_authfile * authfile) {
130 if ( authfile == NULL )
131  return -1;
132
133 // TODO: implement real unlocking here.
134 return 0;
135}
136
137int roar_authfile_sync(struct roar_authfile * authfile) {
138 if ( authfile == NULL )
139  return -1;
140
141 return roar_vio_sync(&(authfile->vio));
142}
143
144
145struct roar_authfile_key * roar_authfile_key_new(int type, size_t len, const char * addr) {
146 struct roar_authfile_key * ret;
147 size_t addrlen;
148 size_t retlen;
149
150 if ( addr == NULL ) {
151  addrlen = 0;
152 } else {
153  addrlen = strlen(addr) + 1;
154 }
155
156 retlen = sizeof(struct roar_authfile_key) + len + addrlen;
157
158 if ( (ret = roar_mm_malloc(retlen)) == NULL )
159  return NULL;
160
161 memset(ret, 0, retlen);
162
163 ret->refc  = 1;
164 ret->type  = type;
165 ret->index = -1;
166
167 if ( addrlen == 0 ) {
168  ret->address = NULL;
169 } else {
170  ret->address = (const void *)ret+sizeof(struct roar_authfile_key);
171  memcpy((void*)ret->address, addr, addrlen);
172 }
173
174 ret->data = (void*)ret + sizeof(struct roar_authfile_key) + addrlen;
175
176 ret->len  = len;
177
178 return ret;
179}
180
181int roar_authfile_key_ref(struct roar_authfile_key * key) {
182 if ( key == NULL )
183  return -1;
184
185 key->refc++;
186
187 return 0;
188}
189
190int roar_authfile_key_unref(struct roar_authfile_key * key) {
191 if ( key == NULL )
192  return -1;
193
194 if ( key->refc == 0 ) {
195  ROAR_ERR("roar_authfile_key_unref(key=%p): Key has reference count of zero. This is bad. assuming refc=1", key);
196  key->refc = 1;
197 }
198
199 key->refc--;
200
201 if ( key->refc > 0 )
202  return 0;
203
204 roar_mm_free(key);
205
206 return 0;
207}
208
209int roar_authfile_add_key(struct roar_authfile * authfile, struct roar_authfile_key * key) {
210 if ( authfile == NULL || key == NULL )
211  return -1;
212
213 switch (authfile->type) {
214  case ROAR_AUTHFILE_TYPE_ESD:
215  case ROAR_AUTHFILE_TYPE_PULSE:
216    if ( key->type != ROAR_AUTH_T_COOKIE || (key->index != 0 && key->index != -1) )
217     return -1;
218    if ( roar_authfile_sync(authfile) == -1 )
219     return -1;
220    if ( roar_authfile_lock(authfile) == -1 )
221     return -1;
222    if ( roar_vio_lseek(&(authfile->vio), 0, SEEK_SET) != 0 )
223     return -1;
224    if ( roar_vio_write(&(authfile->vio), key->data, key->len) != key->len )
225     return -1;
226    if ( roar_authfile_unlock(authfile) == -1 )
227     return -1;
228    return 0;
229   break;
230  default:
231    return -1;
232   break;
233 }
234}
235
236struct roar_authfile_key * roar_authfile_lookup_key(struct roar_authfile * authfile,
237                                                    int type, int minindex, const char * address) {
238 struct roar_authfile_key * ret = NULL;
239 ssize_t len;
240
241 if ( authfile == NULL )
242  return NULL;
243
244 switch (authfile->type) {
245  case ROAR_AUTHFILE_TYPE_ESD:
246  case ROAR_AUTHFILE_TYPE_PULSE:
247    if ( (type != ROAR_AUTH_T_COOKIE && type != ROAR_AUTH_T_AUTO) || minindex > 0 )
248     return NULL;
249
250    if ( (ret = roar_authfile_key_new(ROAR_AUTH_T_COOKIE, 256, NULL)) == NULL )
251     return NULL;
252
253    len = roar_vio_read(&(authfile->vio), ret->data, ret->len);
254
255    if ( len == -1 ) {
256     roar_authfile_key_unref(ret);
257     return NULL;
258    }
259
260    ret->len   = len;
261    ret->index = 0;
262   break;
263  default:
264    return NULL;
265   break;
266 }
267
268 return ret;
269}
270
271struct roar_authfile_key * roar_authfile_key_new_random(int type, size_t len, const char * addr) {
272 struct roar_authfile_key * ret = roar_authfile_key_new(type, len, addr);
273
274 if ( ret == NULL )
275  return NULL;
276
277 roar_random_gen_nonce(ret->data, ret->len);
278
279 return ret;
280}
281
282//ll
Note: See TracBrowser for help on using the repository browser.