source: roaraudio/libroar/authfile.c @ 4792:a9cb74d9acb5

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

added support to roard to load authfiles

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