Debian Codegraph @

cache/accessor/linux.go

raw
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3
4 //go:build linux
5 // +build linux
6
7 package accessor
8
9 /*
10 #cgo LDFLAGS: -ldl
11 #include <dlfcn.h>
12 #include <stdlib.h>
13
14 typedef struct
15 {
16 int domain;
17 int code;
18 char *message;
19 } gError;
20
21 typedef struct
22 {
23 const char *name;
24 int type;
25 } schemaAttribute;
26
27 typedef struct
28 {
29 char *name;
30 int flags;
31 schemaAttribute attributes[32];
32
33 // private fields
34 int r;
35 char *r2;
36 char *r3;
37 char *r4;
38 char *r5;
39 char *r6;
40 char *r7;
41 char *r8;
42 } schema;
43
44 schema *new_schema(char *name, const char *key1, const char *key2)
45 {
46 int i = 0;
47 schema *s;
48 s = malloc(sizeof(schema));
49 s->flags = 0; // SECRET_SCHEMA_NONE
50 s->name = name;
51 if (key1 != NULL)
52 {
53 s->attributes[i++] = (schemaAttribute){key1, 0}; // 0 == SECRET_SCHEMA_ATTRIBUTE_STRING
54 }
55 if (key2 != NULL)
56 {
57 s->attributes[i++] = (schemaAttribute){key2, 0};
58 }
59 s->attributes[i] = (schemaAttribute){NULL, 0};
60 return s;
61 }
62
63 // free a gError. f must be a pointer to g_error_free
64 void free_g_error(void *f, gError *err)
65 {
66 void (*fn)(gError *err);
67 fn = (void (*)(gError *err))f;
68 fn(err);
69 }
70
71 // clear (delete) a secret. f must be a pointer to secret_password_clear_sync
72 // https://gnome.pages.gitlab.gnome.org/libsecret/func.password_clear_sync.html
73 int clear(void *f, schema *sch, void* cancellable, gError **err, char *key1, char *value1, char *key2, char *value2)
74 {
75 int (*fn)(schema *sch, void* cancellable, gError **err, char *key1, char *value1, char *key2, char *value2, ...);
76 fn = (int (*)(schema *sch, void* cancellable, gError **err, char *key1, char *value1, char *key2, char *value2, ...))f;
77 int r = fn(sch, cancellable, err, key1, value1, key2, value2, NULL);
78 return r;
79 }
80
81 // lookup a password. f must be a pointer to secret_password_lookup_sync
82 // https://gnome.pages.gitlab.gnome.org/libsecret/func.password_lookup_sync.html
83 char *lookup(void *f, schema *sch, void* cancellable, gError **err, char *key1, char *value1, char *key2, char *value2)
84 {
85 char *(*fn)(schema *s, void *cancellable, gError **err, char *attrKey1, char *attrValue1, char *attrKey2, char* attrValue2, ...);
86 fn = (char *(*)(schema *s, void *cancellable, gError **err, char *attrKey1, char *attrValue1, char *attrKey2, char* attrValue2, ...))f;
87 char *r = fn(sch, cancellable, err, key1, value1, key2, value2, NULL);
88 return r;
89 }
90
91 // store a password. f must be a pointer to secret_password_store_sync
92 // https://gnome.pages.gitlab.gnome.org/libsecret/func.password_store_sync.html
93 int store(void *f, schema *sch, char* collection, char *label, char *password, void* cancellable, gError **err, char *key1, char *value1, char *key2, char *value2)
94 {
95 int (*fn)(schema *s, char *collection, char *label, char *data, void *cancellable, gError **err, ...);
96 fn = (int (*)(schema *s, char *collection, char *label, char *data, void *cancellable, gError **err, ...))f;
97 int r = fn(sch, collection, label, password, cancellable, err, key1, value1, key2, value2, NULL);
98 return r;
99 }
100 */
101 import "C"
102 import (
103 "context"
104 "encoding/base64"
105 "errors"
106 "fmt"
107 "runtime"
108 "unsafe"
109 )
110
111 const so = "libsecret-1.so"
112
113 type attribute struct {
114 name, value string
115 }
116
117 type option func(*Storage) error
118
119 // WithAttribute adds an attribute to the schema representing the cache.
120 // [Storage] supports up to 2 attributes.
121 func WithAttribute(name, value string) option {
122 return func(s *Storage) error {
123 if len(s.attributes) == 2 {
124 return errors.New("Storage supports up to 2 attributes")
125 }
126 s.attributes = append(s.attributes, attribute{name: name, value: value})
127 return nil
128 }
129 }
130
131 // WithLabel sets a label on the schema representing the cache. The default label is "MSALCache".
132 func WithLabel(label string) option {
133 return func(s *Storage) error {
134 s.label = label
135 return nil
136 }
137 }
138
139 // Storage uses libsecret to store data with a DBus Secret Service such as GNOME Keyring or KDE Wallet. The Service
140 // must be unlocked before Storage can access it. Unlocking typically requires user interaction, and some systems may
141 // be unable to unlock the Service in a headless environment such as an SSH session.
142 type Storage struct {
143 // attributes are key/value pairs on the secret schema
144 attributes []attribute
145 // handle is an opaque handle for libsecret returned by dlopen(). It should be
146 // released via dlclose() when no longer needed so the loader knows when it's
147 // safe to unload libsecret.
148 handle unsafe.Pointer
149 // label of the secret schema
150 label string
151 // clear, freeError, lookup and store are the addresses of libsecret functions
152 clear, freeError, lookup, store unsafe.Pointer
153 // schema identifies the cached data in the secret service
154 schema *C.schema
155 }
156
157 // New is the constructor for Storage. "name" is the name of the secret schema.
158 func New(name string, opts ...option) (*Storage, error) {
159 s := Storage{label: "MSALCache"}
160 for _, o := range opts {
161 if err := o(&s); err != nil {
162 return nil, err
163 }
164 }
165 n := C.CString(so)
166 defer C.free(unsafe.Pointer(n))
167
168 // set the handle and finalizer first so any handle will be
169 // released even when this constructor goes on to return an error
170 s.handle = C.dlopen(n, C.RTLD_LAZY)
171 if s.handle == nil {
172 msg := fmt.Sprintf("encrypted storage isn't possible because the dynamic linker couldn't open %s", so)
173 if e := C.dlerror(); e != nil {
174 msg += fmt.Sprintf(". The underlying error is %q", C.GoString(e))
175 }
176 return nil, errors.New(msg)
177 }
178 runtime.SetFinalizer(&s, func(s *Storage) {
179 if s.handle != nil {
180 C.dlclose(s.handle)
181 }
182 if s.schema != nil {
183 for _, attr := range s.schema.attributes {
184 if attr.name != nil {
185 C.free(unsafe.Pointer(attr.name))
186 }
187 }
188 C.free(unsafe.Pointer(s.schema.name))
189 C.free(unsafe.Pointer(s.schema))
190 }
191 })
192
193 clear, err := s.symbol("secret_password_clear_sync")
194 if err != nil {
195 return nil, err
196 }
197 freeError, err := s.symbol("g_error_free")
198 if err != nil {
199 return nil, err
200 }
201 lookup, err := s.symbol("secret_password_lookup_sync")
202 if err != nil {
203 return nil, err
204 }
205 store, err := s.symbol("secret_password_store_sync")
206 if err != nil {
207 return nil, err
208 }
209 s.clear = clear
210 s.freeError = freeError
211 s.lookup = lookup
212 s.store = store
213
214 // the first nil terminates the list and libsecret ignores any extras
215 attrs := []*C.char{nil, nil}
216 for i, attr := range s.attributes {
217 // libsecret hangs on to these pointers; the finalizer frees them
218 attrs[i] = C.CString(attr.name)
219 }
220 s.schema = C.new_schema(C.CString(name), attrs[0], attrs[1])
221 return &s, nil
222 }
223
224 // Delete deletes the stored data, if any exists.
225 func (s *Storage) Delete(context.Context) error {
226 // the first nil terminates the list and libsecret ignores any extras
227 attrs := []*C.char{nil, nil, nil, nil}
228 for i, attr := range s.attributes {
229 name := C.CString(attr.name)
230 defer C.free(unsafe.Pointer(name))
231 value := C.CString(attr.value)
232 defer C.free(unsafe.Pointer(value))
233 attrs[i*2] = name
234 attrs[(i*2)+1] = value
235 }
236 var e *C.gError
237 _ = C.clear(s.clear, s.schema, nil, &e, attrs[0], attrs[1], attrs[2], attrs[3])
238 if e != nil {
239 defer C.free_g_error(s.freeError, e)
240 return fmt.Errorf("couldn't delete cache data: %q", C.GoString(e.message))
241 }
242 return nil
243 }
244
245 // Read returns data stored according to the secret schema or, if no such data exists, a nil slice and nil error.
246 func (s *Storage) Read(context.Context) ([]byte, error) {
247 // the first nil terminates the list and libsecret ignores any extras
248 attrs := []*C.char{nil, nil, nil, nil}
249 for i, attr := range s.attributes {
250 name := C.CString(attr.name)
251 defer C.free(unsafe.Pointer(name))
252 value := C.CString(attr.value)
253 defer C.free(unsafe.Pointer(value))
254 attrs[i*2] = name
255 attrs[(i*2)+1] = value
256 }
257 var e *C.gError
258 data := C.lookup(s.lookup, s.schema, nil, &e, attrs[0], attrs[1], attrs[2], attrs[3])
259 if e != nil {
260 defer C.free_g_error(s.freeError, e)
261 return nil, fmt.Errorf("couldn't read data from secret service: %q", C.GoString(e.message))
262 }
263 if data == nil {
264 return nil, nil
265 }
266 defer C.free(unsafe.Pointer(data))
267 result, err := base64.StdEncoding.DecodeString(C.GoString(data))
268 return result, err
269 }
270
271 // Write stores cache data.
272 func (s *Storage) Write(_ context.Context, data []byte) error {
273 // the first nil terminates the list and libsecret ignores any extras
274 attrs := []*C.char{nil, nil, nil, nil}
275 for i, attr := range s.attributes {
276 name := C.CString(attr.name)
277 defer C.free(unsafe.Pointer(name))
278 value := C.CString(attr.value)
279 defer C.free(unsafe.Pointer(value))
280 attrs[i*2] = name
281 attrs[(i*2)+1] = value
282 }
283 pw := C.CString(base64.StdEncoding.EncodeToString(data))
284 defer C.free(unsafe.Pointer(pw))
285 var label *C.char
286 if s.label != "" {
287 label = C.CString(s.label)
288 defer C.free(unsafe.Pointer(label))
289 }
290 var e *C.gError
291 if r := C.store(s.store, s.schema, nil, label, pw, nil, &e, attrs[0], attrs[1], attrs[2], attrs[3]); r == 0 {
292 msg := "couldn't write data to secret service"
293 if e != nil {
294 defer C.free_g_error(s.freeError, e)
295 if e.message != nil {
296 msg += ": " + C.GoString(e.message)
297 }
298 }
299 return errors.New(msg)
300 }
301 return nil
302 }
303
304 func (s *Storage) symbol(name string) (unsafe.Pointer, error) {
305 n := C.CString(name)
306 defer C.free(unsafe.Pointer(n))
307 C.dlerror()
308 fp := C.dlsym(s.handle, n)
309 if e := C.dlerror(); e != nil {
310 return nil, fmt.Errorf("couldn't load %q: %s", name, C.GoString(e))
311 }
312 return fp, nil
313 }
314
315 var _ Accessor = (*Storage)(nil)