summaryrefslogtreecommitdiff
path: root/libpurple/keyring.h
blob: 8346c1d8d2d242dcabc41dba24079370761f558a (plain)
1
2
3
4
5
6
7
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/**
 * @file keyring.h Keyring API
 * @ingroup core
 */

/* purple
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
 */

#ifndef _PURPLE_KEYRING_H_
#define _PURPLE_KEYRING_H_

#include "account.h"
#include "request.h"

#define PURPLE_TYPE_KEYRING  (purple_keyring_get_type())

/**
 * PURPLE_DEFAULT_KEYRING:
 *
 * Default keyring ID.
 */
#define PURPLE_DEFAULT_KEYRING "keyring-internal"

/**
 * PURPLE_KEYRING_ERROR:
 *
 * Keyring subsystem error domain.
 */
#define PURPLE_KEYRING_ERROR purple_keyring_error_domain()

/**************************************************************************/
/** @name Data structures and types                                       */
/**************************************************************************/
/*@{*/

typedef struct _PurpleKeyring PurpleKeyring;

/*@}*/

/**************************************************************************/
/** @name Callbacks for keyrings access functions                         */
/**************************************************************************/
/*@{*/

/**
 * PurpleKeyringReadCallback:
 * @account:  The account.
 * @password: The password.
 * @error:    Error that may have occurred.
 * @data:     Data passed to the callback.
 *
 * Callback for once a password is read.
 *
 * If there was a problem, the password will be NULL, and the error set.
 */
typedef void (*PurpleKeyringReadCallback)(PurpleAccount *account,
	const gchar *password, GError *error, gpointer data);

/**
 * PurpleKeyringSaveCallback:
 * @account: The account.
 * @error:   Error that may have occurred.
 * @data:    Data passed to the callback.
 *
 * Callback for once a password has been stored.
 *
 * If there was a problem, the error will be set.
 */
typedef void (*PurpleKeyringSaveCallback)(PurpleAccount *account, GError *error,
	gpointer data);

/**
 * PurpleKeyringChangeMasterCallback:
 * @error:  Error that has occurred.
 * @data:   Data passed to the callback.
 *
 * Callback for once the master password for a keyring has been changed.
 */
typedef void (*PurpleKeyringChangeMasterCallback)(GError *error, gpointer data);

/**
 * PurpleKeyringSetInUseCallback:
 * @error:   An error that might have occurred.
 * @data:    A pointer to user supplied data.
 *
 * Callback for when we change the keyring.
 */
typedef void (*PurpleKeyringSetInUseCallback)(GError *error, gpointer data);

/*@}*/

/**************************************************************************/
/** @name Keyrings access functions                                       */
/**************************************************************************/
/*@{*/

/**
 * PurpleKeyringRead:
 * @account: The account.
 * @cb:      A callback for once the password is found.
 * @data:    Data to be passed to the callback.
 *
 * Read the password for an account.
 */
typedef void (*PurpleKeyringRead)(PurpleAccount *account,
	PurpleKeyringReadCallback cb, gpointer data);

/**
 * PurpleKeyringSave:
 * @account:  The account.
 * @password: The password to be stored. If the password is NULL, this
 *                 means that the keyring should forget about that password.
 * @cb:       A callback for once the password is saved.
 * @data:     Data to be passed to the callback.
 *
 * Store a password in the keyring.
 */
typedef void (*PurpleKeyringSave)(PurpleAccount *account, const gchar *password,
	PurpleKeyringSaveCallback cb, gpointer data);

/**
 * PurpleKeyringCancelRequests:
 *
 * Cancel all running requests.
 *
 * After calling that, all queued requests should run their callbacks (most
 * probably, with failure result).
 */
typedef void (*PurpleKeyringCancelRequests)(void);

/**
 * PurpleKeyringClose:
 *
 * Close the keyring.
 *
 * This will be called so the keyring can do any cleanup it needs.
 */
typedef void (*PurpleKeyringClose)(void);

/**
 * PurpleKeyringImportPassword:
 * @account: The account.
 * @mode:    A keyring specific option that was stored. Can be NULL.
 * @data:    Data that was stored. Can be NULL.
 *
 * Import serialized (and maybe encrypted) password.
 *
 * This is not async because it is not meant to prompt for a master password and
 * decrypt passwords.
 *
 * Returns: TRUE on success, FALSE on failure.
 */
typedef gboolean (*PurpleKeyringImportPassword)(PurpleAccount *account,
	const gchar *mode, const gchar *data, GError **error);

/**
 * PurpleKeyringExportPassword:
 * @account: The account.
 * @mode:    An option field that can be used by the plugin. This is
 *                expected to be a static string.
 * @data:    The data to be stored in the XML node. This string will be
 *                freed using destroy() once not needed anymore.
 * @error:   Will be set if a problem occured.
 * @destroy: A function to be called, if non NULL, to free data.
 *
 * Export serialized (and maybe encrypted) password.
 *
 * Returns: TRUE on success, FALSE on failure.
 */
typedef gboolean (*PurpleKeyringExportPassword)(PurpleAccount *account,
	const gchar **mode, gchar **data, GError **error,
	GDestroyNotify *destroy);

/**
 * PurpleKeyringReadSettings:
 *
 * Read keyring settings.
 *
 * Returns: New copy of current settings (must be free'd with
 *         purple_request_fields_destroy).
 */
typedef PurpleRequestFields * (*PurpleKeyringReadSettings)(void);

/**
 * PurpleKeyringApplySettings:
 * @notify_handle: A handle that can be passed to purple_notify_message.
 * @fields:        Modified settings (originally taken from
 *                      PurpleKeyringReadSettings).
 *
 * Applies modified keyring settings.
 *
 * Returns: TRUE, if succeeded, FALSE otherwise.
 */
typedef gboolean (*PurpleKeyringApplySettings)(void *notify_handle,
	PurpleRequestFields *fields);

/*@}*/

G_BEGIN_DECLS

/**************************************************************************/
/** @name Setting used keyrings                                           */
/**************************************************************************/
/*@{*/

/**
 * purple_keyring_find_keyring_by_id:
 * @id: The id for the keyring.
 *
 * Find a keyring by an id.
 *
 * Returns: The keyring, or NULL if not found.
 */
PurpleKeyring *
purple_keyring_find_keyring_by_id(const gchar *id);

/**
 * purple_keyring_get_inuse:
 *
 * Get the keyring being used.
 */
PurpleKeyring *
purple_keyring_get_inuse(void);

/**
 * purple_keyring_set_inuse:
 * @newkeyring: The new keyring to use.
 * @force:      FALSE if the change can be cancelled. If this is TRUE and
 *                   an error occurs, data might be lost.
 * @cb:         A callback for once the change is complete.
 * @data:       Data to be passed to the callback.
 *
 * Set the keyring to use. This function will move all passwords from
 * the old keyring to the new one.
 *
 * If it fails, it will cancel all changes, close the new keyring, and notify
 * the callback. If it succeeds, it will remove all passwords from the old safe
 * and close that safe.
 */
void
purple_keyring_set_inuse(PurpleKeyring *newkeyring, gboolean force,
	PurpleKeyringSetInUseCallback cb, gpointer data);

/**
 * purple_keyring_register:
 * @keyring: The keyring to register.
 *
 * Register a keyring plugin.
 */
void
purple_keyring_register(PurpleKeyring *keyring);

/**
 * purple_keyring_unregister:
 * @keyring: The keyring to unregister.
 *
 * Unregister a keyring plugin.
 *
 * In case the keyring is in use, passwords will be moved to a fallback safe,
 * and the keyring to unregister will be properly closed.
 */
void
purple_keyring_unregister(PurpleKeyring *keyring);

/**
 * purple_keyring_get_options:
 *
 * Returns a GList containing the IDs and names of the registered
 * keyrings.
 *
 * Returns: The list of IDs and names.
 */
GList *
purple_keyring_get_options(void);

/*@}*/

/**************************************************************************/
/** @name Keyring plugin wrappers                                         */
/**************************************************************************/
/*@{*/

/**
 * purple_keyring_import_password:
 * @account:    The account.
 * @keyring_id: The plugin ID that was stored in the xml file. Can be NULL.
 * @mode:       A keyring specific option that was stored. Can be NULL.
 * @data:       Data that was stored, can be NULL.
 *
 * Import serialized (and maybe encrypted) password into current keyring.
 *
 * It's used by account.c while reading a password from xml.
 *
 * Returns: TRUE if the input was accepted, FALSE otherwise.
 */
gboolean
purple_keyring_import_password(PurpleAccount *account, const gchar *keyring_id,
	const gchar *mode, const gchar *data, GError **error);

/**
 * purple_keyring_export_password:
 * @account:    The account for which we want the info.
 * @keyring_id: The plugin id to be stored in the XML node. This will be
 *                   NULL or a string that can be considered static.
 * @mode:       An option field that can be used by the plugin. This will
 *                   be NULL or a string that can be considered static.
 * @data:       The data to be stored in the XML node. This string must be
 *                   freed using destroy() once not needed anymore if it is not
 *                   NULL.
 * @error:      Will be set if a problem occured.
 * @destroy:    A function to be called, if non NULL, to free data.
 *
 * Export serialized (and maybe encrypted) password out of current keyring.
 *
 * It's used by account.c while syncing accounts to xml.
 *
 * Returns: TRUE if the info was exported successfully, FALSE otherwise.
 */
gboolean
purple_keyring_export_password(PurpleAccount *account, const gchar **keyring_id,
	const gchar **mode, gchar **data, GError **error,
	GDestroyNotify *destroy);

/**
 * purple_keyring_get_password:
 * @account: The account.
 * @cb:      A callback for once the password is read.
 * @data:    Data passed to the callback.
 *
 * Read a password from the current keyring.
 */
void
purple_keyring_get_password(PurpleAccount *account,
	PurpleKeyringReadCallback cb, gpointer data);

/**
 * purple_keyring_set_password:
 * @account:  The account.
 * @password: The password to save.
 * @cb:       A callback for once the password is saved.
 * @data:     Data to be passed to the callback.
 *
 * Save a password to the current keyring.
 */
void
purple_keyring_set_password(PurpleAccount *account, const gchar *password,
	PurpleKeyringSaveCallback cb, gpointer data);

/**
 * purple_keyring_read_settings:
 *
 * Reads settings from current keyring.
 *
 * Returns: New copy of current settings (must be free'd with
 *         purple_request_fields_destroy).
 */
PurpleRequestFields *
purple_keyring_read_settings(void);

/**
 * purple_keyring_apply_settings:
 * @notify_handle: A handle that can be passed to purple_notify_message.
 * @fields:        Modified settings (originally taken from
 *                      PurpleKeyringReadSettings).
 *
 * Applies modified settings to current keyring.
 *
 * Returns: TRUE, if succeeded, FALSE otherwise.
 */
gboolean
purple_keyring_apply_settings(void *notify_handle, PurpleRequestFields *fields);

/*@}*/

/**************************************************************************/
/** @name PurpleKeyring accessors                                         */
/**************************************************************************/
/*@{*/

/**
 * purple_keyring_get_type:
 *
 * Returns the GType for the PurpleKeyring boxed structure.
 */
GType purple_keyring_get_type(void);

/**
 * purple_keyring_new:
 *
 * Creates a new keyring wrapper.
 */
PurpleKeyring *
purple_keyring_new(void);

/**
 * purple_keyring_free:
 * @keyring: Keyring wrapper struct.
 *
 * Frees all data allocated with purple_keyring_new.
 */
void
purple_keyring_free(PurpleKeyring *keyring);

/**
 * purple_keyring_get_name:
 * @keyring: The keyring.
 *
 * Gets friendly user name.
 *
 * Returns: Friendly user name.
 */
const gchar *
purple_keyring_get_name(const PurpleKeyring *keyring);

/**
 * purple_keyring_get_id:
 * @keyring: The keyring.
 *
 * Gets keyring ID.
 *
 * Returns: Keyring ID.
 */
const gchar *
purple_keyring_get_id(const PurpleKeyring *keyring);

PurpleKeyringRead
purple_keyring_get_read_password(const PurpleKeyring *keyring);

PurpleKeyringSave
purple_keyring_get_save_password(const PurpleKeyring *keyring);

PurpleKeyringCancelRequests
purple_keyring_get_cancel_requests(const PurpleKeyring *keyring);

PurpleKeyringClose
purple_keyring_get_close_keyring(const PurpleKeyring *keyring);

PurpleKeyringImportPassword
purple_keyring_get_import_password(const PurpleKeyring *keyring);

PurpleKeyringExportPassword
purple_keyring_get_export_password(const PurpleKeyring *keyring);

PurpleKeyringReadSettings
purple_keyring_get_read_settings(const PurpleKeyring *keyring);

PurpleKeyringApplySettings
purple_keyring_get_apply_settings(const PurpleKeyring *keyring);

/**
 * purple_keyring_set_name:
 * @keyring: The keyring.
 * @name:    Friendly user name.
 *
 * Sets friendly user name.
 *
 * This field is required.
 */
void
purple_keyring_set_name(PurpleKeyring *keyring, const gchar *name);

/**
 * purple_keyring_set_id:
 * @keyring: The keyring.
 * @name:    Keyring ID.
 *
 * Sets keyring ID.
 *
 * This field is required.
 */
void
purple_keyring_set_id(PurpleKeyring *keyring, const gchar *id);

/**
 * purple_keyring_set_read_password:
 * @keyring: The keyring.
 * @read_cb: Read password method.
 *
 * Sets read password method.
 *
 * This field is required.
 */
void
purple_keyring_set_read_password(PurpleKeyring *keyring,
	PurpleKeyringRead read_cb);

/**
 * purple_keyring_set_save_password:
 * @keyring: The keyring.
 * @save_cb: Save password method.
 *
 * Sets save password method.
 *
 * This field is required.
 */
void
purple_keyring_set_save_password(PurpleKeyring *keyring,
	PurpleKeyringSave save_cb);

void
purple_keyring_set_cancel_requests(PurpleKeyring *keyring,
	PurpleKeyringCancelRequests cancel_requests);

void
purple_keyring_set_close_keyring(PurpleKeyring *keyring,
	PurpleKeyringClose close_cb);

void
purple_keyring_set_import_password(PurpleKeyring *keyring,
	PurpleKeyringImportPassword import_password);

void
purple_keyring_set_export_password(PurpleKeyring *keyring,
	PurpleKeyringExportPassword export_password);

void
purple_keyring_set_read_settings(PurpleKeyring *keyring,
PurpleKeyringReadSettings read_settings);

void
purple_keyring_set_apply_settings(PurpleKeyring *keyring,
PurpleKeyringApplySettings apply_settings);

/*@}*/

/**************************************************************************/
/** @name Error Codes                                                     */
/**************************************************************************/
/*@{*/

/**
 * purple_keyring_error_domain:
 *
 * Gets keyring subsystem error domain.
 *
 * Returns: keyring subsystem error domain.
 */
GQuark
purple_keyring_error_domain(void);

/**
 * PurpleKeyringError:
 * @PURPLE_KEYRING_ERROR_UNKNOWN:      Unknown error.
 * @PURPLE_KEYRING_ERROR_NOKEYRING:    No keyring configured.
 * @PURPLE_KEYRING_ERROR_INTERNAL:     Internal keyring system error.
 * @PURPLE_KEYRING_ERROR_BACKENDFAIL:  Failed to communicate with the backend
 *                                     or internal backend error.
 * @PURPLE_KEYRING_ERROR_NOPASSWORD:   No password stored for the specified
 *                                     account.
 * @PURPLE_KEYRING_ERROR_ACCESSDENIED: Access denied for the specified keyring
 *                                     or entry.
 * @PURPLE_KEYRING_ERROR_CANCELLED:    Operation was cancelled.
 *
 * Error codes for keyring subsystem.
 */
enum PurpleKeyringError
{
	PURPLE_KEYRING_ERROR_UNKNOWN = 0,

	PURPLE_KEYRING_ERROR_NOKEYRING = 10,
	PURPLE_KEYRING_ERROR_INTERNAL,
	PURPLE_KEYRING_ERROR_BACKENDFAIL,

	PURPLE_KEYRING_ERROR_NOPASSWORD = 20,
	PURPLE_KEYRING_ERROR_ACCESSDENIED,
	PURPLE_KEYRING_ERROR_CANCELLED
};

/*}@*/

/**************************************************************************/
/** @name Keyring Subsystem                                               */
/**************************************************************************/
/*@{*/

/**
 * purple_keyring_init:
 *
 * Initializes the keyring subsystem.
 */
void
purple_keyring_init(void);

/**
 * purple_keyring_uninit:
 *
 * Uninitializes the keyring subsystem.
 */
void
purple_keyring_uninit(void);

/**
 * purple_keyring_get_handle:
 *
 * Returns the keyring subsystem handle.
 *
 * Returns: The keyring subsystem handle.
 */
void *
purple_keyring_get_handle(void);

/*}@*/

G_END_DECLS

#endif /* _PURPLE_KEYRING_H_ */