source: roaraudio/libroar/authfile.c @ 5375:2b4d1e027b2d

Last change on this file since 5375:2b4d1e027b2d was 5360:3dd9d1fd1b0e, checked in by phi, 12 years ago

added const keywords, removed an uneeded cast to non-const

File size: 6.6 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#if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT)
53 struct stat filestat;
54#endif
55
56 if ( type == ROAR_AUTHFILE_TYPE_AUTO ) {
57#if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_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), 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  roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
198 }
199
200 key->refc--;
201
202 if ( key->refc > 0 )
203  return 0;
204
205 roar_mm_free(key);
206
207 return 0;
208}
209
210int roar_authfile_add_key(struct roar_authfile * authfile, struct roar_authfile_key * key) {
211 if ( authfile == NULL || key == NULL )
212  return -1;
213
214 switch (authfile->type) {
215  case ROAR_AUTHFILE_TYPE_ESD:
216  case ROAR_AUTHFILE_TYPE_PULSE:
217    if ( key->type != ROAR_AUTH_T_COOKIE || (key->index != 0 && key->index != -1) )
218     return -1;
219    if ( roar_authfile_sync(authfile) == -1 )
220     return -1;
221    if ( roar_authfile_lock(authfile) == -1 )
222     return -1;
223    if ( roar_vio_lseek(&(authfile->vio), 0, SEEK_SET) != 0 )
224     return -1;
225    if ( roar_vio_write(&(authfile->vio), key->data, key->len) != (ssize_t)key->len )
226     return -1;
227    if ( roar_authfile_unlock(authfile) == -1 )
228     return -1;
229    return 0;
230   break;
231  default:
232    return -1;
233   break;
234 }
235}
236
237struct roar_authfile_key * roar_authfile_lookup_key(struct roar_authfile * authfile,
238                                                    int type, int minindex, const char * address) {
239 struct roar_authfile_key * ret = NULL;
240 ssize_t len;
241
242 if ( authfile == NULL )
243  return NULL;
244
245 switch (authfile->type) {
246  case ROAR_AUTHFILE_TYPE_ESD:
247  case ROAR_AUTHFILE_TYPE_PULSE:
248    if ( (type != ROAR_AUTH_T_COOKIE && type != ROAR_AUTH_T_AUTO) || minindex > 0 )
249     return NULL;
250
251    if ( (ret = roar_authfile_key_new(ROAR_AUTH_T_COOKIE, 256, NULL)) == NULL )
252     return NULL;
253
254    len = roar_vio_read(&(authfile->vio), ret->data, ret->len);
255
256    if ( len == -1 ) {
257     roar_authfile_key_unref(ret);
258     return NULL;
259    }
260
261    ret->len   = len;
262    ret->index = 0;
263   break;
264  default:
265    return NULL;
266   break;
267 }
268
269 return ret;
270}
271
272struct roar_authfile_key * roar_authfile_key_new_random(int type, size_t len, const char * addr) {
273 struct roar_authfile_key * ret = roar_authfile_key_new(type, len, addr);
274
275 if ( ret == NULL )
276  return NULL;
277
278 roar_random_gen_nonce(ret->data, ret->len);
279
280 return ret;
281}
282
283//ll
Note: See TracBrowser for help on using the repository browser.