1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio 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 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <sys/types.h>
25
26 #include <pulse/xmalloc.h>
27
28 #include <pulsecore/refcnt.h>
29 #include <pulsecore/macro.h>
30 #include <pulsecore/core-util.h>
31 #include <pulsecore/shared.h>
32 #include <pulsecore/authkey.h>
33
34 #include "auth-cookie.h"
35
36 struct pa_auth_cookie {
37 PA_REFCNT_DECLARE;
38 pa_core *core;
39 char *name;
40 size_t size;
41 };
42
pa_auth_cookie_get(pa_core * core,const char * cn,bool create,size_t size)43 pa_auth_cookie* pa_auth_cookie_get(pa_core *core, const char *cn, bool create, size_t size) {
44 pa_auth_cookie *c;
45 char *t;
46
47 pa_assert(core);
48 pa_assert(size > 0);
49
50 t = pa_sprintf_malloc("auth-cookie%s%s", cn ? "@" : "", cn ? cn : "");
51
52 if ((c = pa_shared_get(core, t))) {
53
54 pa_xfree(t);
55
56 if (c->size != size)
57 return NULL;
58
59 return pa_auth_cookie_ref(c);
60 }
61
62 c = pa_xmalloc(PA_ALIGN(sizeof(pa_auth_cookie)) + size);
63 PA_REFCNT_INIT(c);
64 c->core = core;
65 c->name = t;
66 c->size = size;
67
68 pa_assert_se(pa_shared_set(core, t, c) >= 0);
69
70 if (pa_authkey_load(cn, create, (uint8_t*) c + PA_ALIGN(sizeof(pa_auth_cookie)), size) < 0) {
71 pa_auth_cookie_unref(c);
72 return NULL;
73 }
74
75 return c;
76 }
77
pa_auth_cookie_create(pa_core * core,const void * data,size_t size)78 pa_auth_cookie *pa_auth_cookie_create(pa_core *core, const void *data, size_t size) {
79 pa_auth_cookie *c;
80 char *t;
81
82 pa_assert(core);
83 pa_assert(data);
84 pa_assert(size > 0);
85
86 t = pa_xstrdup("auth-cookie");
87
88 if ((c = pa_shared_get(core, t))) {
89
90 pa_xfree(t);
91
92 if (c->size != size)
93 return NULL;
94
95 return pa_auth_cookie_ref(c);
96 }
97
98 c = pa_xmalloc(PA_ALIGN(sizeof(pa_auth_cookie)) + size);
99 PA_REFCNT_INIT(c);
100 c->core = core;
101 c->name = t;
102 c->size = size;
103
104 pa_assert_se(pa_shared_set(core, t, c) >= 0);
105
106 memcpy((uint8_t *) c + PA_ALIGN(sizeof(pa_auth_cookie)), data, size);
107
108 return c;
109 }
110
pa_auth_cookie_ref(pa_auth_cookie * c)111 pa_auth_cookie* pa_auth_cookie_ref(pa_auth_cookie *c) {
112 pa_assert(c);
113 pa_assert(PA_REFCNT_VALUE(c) >= 1);
114
115 PA_REFCNT_INC(c);
116
117 return c;
118 }
119
pa_auth_cookie_unref(pa_auth_cookie * c)120 void pa_auth_cookie_unref(pa_auth_cookie *c) {
121 pa_assert(c);
122 pa_assert(PA_REFCNT_VALUE(c) >= 1);
123
124 if (PA_REFCNT_DEC(c) > 0)
125 return;
126
127 pa_assert_se(pa_shared_remove(c->core, c->name) >= 0);
128
129 pa_xfree(c->name);
130 pa_xfree(c);
131 }
132
pa_auth_cookie_read(pa_auth_cookie * c,size_t size)133 const uint8_t* pa_auth_cookie_read(pa_auth_cookie *c, size_t size) {
134 pa_assert(c);
135 pa_assert(PA_REFCNT_VALUE(c) >= 1);
136 pa_assert(c->size == size);
137
138 return (const uint8_t*) c + PA_ALIGN(sizeof(pa_auth_cookie));
139 }
140