| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package accessor |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 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 |
|
| 120 |
|
| 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 |
|
| 132 |
func WithLabel(label string) option { |
| 133 |
return func(s *Storage) error { |
| 134 |
s.label = label |
| 135 |
return nil |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
type Storage struct { |
| 143 |
|
| 144 |
attributes []attribute |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
handle unsafe.Pointer |
| 149 |
|
| 150 |
label string |
| 151 |
|
| 152 |
clear, freeError, lookup, store unsafe.Pointer |
| 153 |
|
| 154 |
schema *C.schema |
| 155 |
} |
| 156 |
|
| 157 |
|
| 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 |
|
| 169 |
|
| 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 |
|
| 215 |
attrs := []*C.char{nil, nil} |
| 216 |
for i, attr := range s.attributes { |
| 217 |
|
| 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 |
|
| 225 |
func (s *Storage) Delete(context.Context) error { |
| 226 |
|
| 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 |
|
| 246 |
func (s *Storage) Read(context.Context) ([]byte, error) { |
| 247 |
|
| 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 |
|
| 272 |
func (s *Storage) Write(_ context.Context, data []byte) error { |
| 273 |
|
| 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) |