2 * libvirt-secret.c: entry points for virSecretPtr APIs
4 * Copyright (C) 2006-2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
23 #include "datatypes.h"
26 VIR_LOG_INIT("libvirt.secret");
28 #define VIR_FROM_THIS VIR_FROM_SECRET
31 * virSecretGetConnect:
32 * @secret: A virSecret secret
34 * Provides the connection pointer associated with a secret. The reference
35 * counter on the connection is not increased by this call.
37 * Returns the virConnectPtr or NULL in case of failure.
42 virSecretGetConnect(virSecretPtr secret
)
44 VIR_DEBUG("secret=%p", secret
);
48 virCheckSecretReturn(secret
, NULL
);
55 * virConnectNumOfSecrets:
56 * @conn: virConnect connection
58 * Fetch number of currently defined secrets.
60 * Returns the number currently defined secrets.
65 virConnectNumOfSecrets(virConnectPtr conn
)
67 VIR_DEBUG("conn=%p", conn
);
71 virCheckConnectReturn(conn
, -1);
73 if (conn
->secretDriver
!= NULL
&&
74 conn
->secretDriver
->connectNumOfSecrets
!= NULL
) {
77 ret
= conn
->secretDriver
->connectNumOfSecrets(conn
);
83 virReportUnsupportedError();
86 virDispatchError(conn
);
92 * virConnectListAllSecrets:
93 * @conn: Pointer to the hypervisor connection.
94 * @secrets: Pointer to a variable to store the array containing the secret
95 * objects or NULL if the list is not required (just returns the
97 * @flags: bitwise-OR of virConnectListAllSecretsFlags.
99 * Collect the list of secrets, and allocate an array to store those
102 * Normally, all secrets are returned; however, @flags can be used to
103 * filter the results for a smaller list of targeted secrets. The valid
104 * flags are divided into groups, where each group contains bits that
105 * describe mutually exclusive attributes of a secret, and where all bits
106 * within a group describe all possible secrets.
108 * The first group of @flags is used to filter secrets by its storage
109 * location. Flag VIR_CONNECT_LIST_SECRETS_EPHEMERAL selects secrets that
110 * are kept only in memory. Flag VIR_CONNECT_LIST_SECRETS_NO_EPHEMERAL
111 * selects secrets that are kept in persistent storage.
113 * The second group of @flags is used to filter secrets by privacy. Flag
114 * VIR_CONNECT_LIST_SECRETS_PRIVATE selects secrets that are never revealed
115 * to any caller of libvirt nor to any other node. Flag
116 * VIR_CONNECT_LIST_SECRETS_NO_PRIVATE selects non-private secrets.
118 * Returns the number of secrets found or -1 and sets @secrets to NULL in case
119 * of error. On success, the array stored into @secrets is guaranteed to
120 * have an extra allocated element set to NULL but not included in the return count,
121 * to make iteration easier. The caller is responsible for calling
122 * virSecretFree() on each array element, then calling free() on @secrets.
127 virConnectListAllSecrets(virConnectPtr conn
,
128 virSecretPtr
**secrets
,
131 VIR_DEBUG("conn=%p, secrets=%p, flags=0x%x", conn
, secrets
, flags
);
138 virCheckConnectReturn(conn
, -1);
140 if (conn
->secretDriver
&&
141 conn
->secretDriver
->connectListAllSecrets
) {
143 ret
= conn
->secretDriver
->connectListAllSecrets(conn
, secrets
, flags
);
149 virReportUnsupportedError();
152 virDispatchError(conn
);
158 * virConnectListSecrets:
159 * @conn: virConnect connection
160 * @uuids: Pointer to an array to store the UUIDs
161 * @maxuuids: size of the array.
163 * List UUIDs of defined secrets, store pointers to names in uuids.
165 * The use of this function is discouraged. Instead, use
166 * virConnectListAllSecrets().
168 * Returns the number of UUIDs provided in the array, or -1 on failure.
173 virConnectListSecrets(virConnectPtr conn
, char **uuids
, int maxuuids
)
175 VIR_DEBUG("conn=%p, uuids=%p, maxuuids=%d", conn
, uuids
, maxuuids
);
179 virCheckConnectReturn(conn
, -1);
180 virCheckNonNullArrayArgGoto(uuids
, maxuuids
, error
);
181 virCheckNonNegativeArgGoto(maxuuids
, error
);
183 if (conn
->secretDriver
!= NULL
&& conn
->secretDriver
->connectListSecrets
!= NULL
) {
186 ret
= conn
->secretDriver
->connectListSecrets(conn
, uuids
, maxuuids
);
192 virReportUnsupportedError();
195 virDispatchError(conn
);
201 * virSecretLookupByUUID:
202 * @conn: pointer to the hypervisor connection
203 * @uuid: the raw UUID for the secret
205 * Try to lookup a secret on the given hypervisor based on its UUID.
206 * Uses the 16 bytes of raw data to describe the UUID
208 * virSecretFree should be used to free the resources after the
209 * secret object is no longer needed.
211 * Returns a new secret object or NULL in case of failure. If the
212 * secret cannot be found, then VIR_ERR_NO_SECRET error is raised.
217 virSecretLookupByUUID(virConnectPtr conn
, const unsigned char *uuid
)
219 VIR_UUID_DEBUG(conn
, uuid
);
223 virCheckConnectReturn(conn
, NULL
);
224 virCheckNonNullArgGoto(uuid
, error
);
226 if (conn
->secretDriver
&&
227 conn
->secretDriver
->secretLookupByUUID
) {
229 ret
= conn
->secretDriver
->secretLookupByUUID(conn
, uuid
);
235 virReportUnsupportedError();
238 virDispatchError(conn
);
244 * virSecretLookupByUUIDString:
245 * @conn: pointer to the hypervisor connection
246 * @uuidstr: the string UUID for the secret
248 * Try to lookup a secret on the given hypervisor based on its UUID.
249 * Uses the printable string value to describe the UUID
251 * virSecretFree should be used to free the resources after the
252 * secret object is no longer needed.
254 * Returns a new secret object or NULL in case of failure. If the
255 * secret cannot be found, then VIR_ERR_NO_SECRET error is raised.
260 virSecretLookupByUUIDString(virConnectPtr conn
, const char *uuidstr
)
262 unsigned char uuid
[VIR_UUID_BUFLEN
];
263 VIR_DEBUG("conn=%p, uuidstr=%s", conn
, NULLSTR(uuidstr
));
267 virCheckConnectReturn(conn
, NULL
);
268 virCheckNonNullArgGoto(uuidstr
, error
);
270 if (virUUIDParse(uuidstr
, uuid
) < 0) {
271 virReportInvalidArg(uuidstr
,
272 _("uuidstr in %1$s must be a valid UUID"),
277 return virSecretLookupByUUID(conn
, &uuid
[0]);
280 virDispatchError(conn
);
286 * virSecretLookupByUsage:
287 * @conn: pointer to the hypervisor connection
288 * @usageType: the type of secret usage
289 * @usageID: identifier of the object using the secret
291 * Try to lookup a secret on the given hypervisor based on its usage
292 * The usageID is unique within the set of secrets sharing the
293 * same usageType value.
295 * virSecretFree should be used to free the resources after the
296 * secret object is no longer needed.
298 * Returns a new secret object or NULL in case of failure. If the
299 * secret cannot be found, then VIR_ERR_NO_SECRET error is raised.
304 virSecretLookupByUsage(virConnectPtr conn
,
308 VIR_DEBUG("conn=%p, usageType=%d usageID=%s", conn
, usageType
, NULLSTR(usageID
));
312 virCheckConnectReturn(conn
, NULL
);
313 virCheckNonNullArgGoto(usageID
, error
);
315 if (conn
->secretDriver
&&
316 conn
->secretDriver
->secretLookupByUsage
) {
318 ret
= conn
->secretDriver
->secretLookupByUsage(conn
, usageType
, usageID
);
324 virReportUnsupportedError();
327 virDispatchError(conn
);
333 * virSecretDefineXML:
334 * @conn: virConnect connection
335 * @xml: XML describing the secret.
336 * @flags: bitwise-OR of virSecretDefineFlags
338 * If XML specifies a UUID, locates the specified secret and replaces all
339 * attributes of the secret specified by UUID by attributes specified in xml
340 * (any attributes not specified in xml are discarded).
342 * Otherwise, creates a new secret with an automatically chosen UUID, and
343 * initializes its attributes from xml.
345 * virSecretFree should be used to free the resources after the
346 * secret object is no longer needed.
348 * Returns a secret on success, NULL on failure.
353 virSecretDefineXML(virConnectPtr conn
, const char *xml
, unsigned int flags
)
355 VIR_DEBUG("conn=%p, xml=%s, flags=0x%x", conn
, NULLSTR(xml
), flags
);
359 virCheckConnectReturn(conn
, NULL
);
360 virCheckReadOnlyGoto(conn
->flags
, error
);
361 virCheckNonNullArgGoto(xml
, error
);
363 if (conn
->secretDriver
!= NULL
&& conn
->secretDriver
->secretDefineXML
!= NULL
) {
366 ret
= conn
->secretDriver
->secretDefineXML(conn
, xml
, flags
);
372 virReportUnsupportedError();
375 virDispatchError(conn
);
382 * @secret: A virSecret secret
383 * @uuid: buffer of VIR_UUID_BUFLEN bytes in size
385 * Fetches the UUID of the secret.
387 * Returns 0 on success with the uuid buffer being filled, or
393 virSecretGetUUID(virSecretPtr secret
, unsigned char *uuid
)
395 VIR_DEBUG("secret=%p", secret
);
399 virCheckSecretReturn(secret
, -1);
400 virCheckNonNullArgGoto(uuid
, error
);
402 memcpy(uuid
, &secret
->uuid
[0], VIR_UUID_BUFLEN
);
407 virDispatchError(secret
->conn
);
413 * virSecretGetUUIDString:
414 * @secret: a secret object
415 * @buf: pointer to a VIR_UUID_STRING_BUFLEN bytes array
417 * Get the UUID for a secret as string. For more information about
420 * Returns -1 in case of error, 0 in case of success
425 virSecretGetUUIDString(virSecretPtr secret
, char *buf
)
427 VIR_DEBUG("secret=%p, buf=%p", secret
, buf
);
431 virCheckSecretReturn(secret
, -1);
432 virCheckNonNullArgGoto(buf
, error
);
434 virUUIDFormat(secret
->uuid
, buf
);
438 virDispatchError(secret
->conn
);
444 * virSecretGetUsageType:
445 * @secret: a secret object
447 * Get the type of object which uses this secret. The returned
448 * value is one of the constants defined in the virSecretUsageType
449 * enumeration. More values may be added to this enumeration in
450 * the future, so callers should expect to see usage types they
451 * do not explicitly know about.
453 * Returns a positive integer identifying the type of object,
459 virSecretGetUsageType(virSecretPtr secret
)
461 VIR_DEBUG("secret=%p", secret
);
465 virCheckSecretReturn(secret
, -1);
467 return secret
->usageType
;
472 * virSecretGetUsageID:
473 * @secret: a secret object
475 * Get the unique identifier of the object with which this
476 * secret is to be used. The format of the identifier is
477 * dependent on the usage type of the secret. For a secret
478 * with a usage type of VIR_SECRET_USAGE_TYPE_VOLUME the
479 * identifier will be a fully qualified path name. The
480 * identifiers are intended to be unique within the set of
481 * all secrets sharing the same usage type. ie, there shall
482 * only ever be one secret for each volume path.
484 * Returns a string identifying the object using the secret,
490 virSecretGetUsageID(virSecretPtr secret
)
492 VIR_DEBUG("secret=%p", secret
);
496 virCheckSecretReturn(secret
, NULL
);
498 return secret
->usageID
;
503 * virSecretGetXMLDesc:
504 * @secret: A virSecret secret
505 * @flags: extra flags; not used yet, so callers should always pass 0
507 * Fetches an XML document describing attributes of the secret.
509 * Returns the XML document on success, NULL on failure. The caller must
515 virSecretGetXMLDesc(virSecretPtr secret
, unsigned int flags
)
519 VIR_DEBUG("secret=%p, flags=0x%x", secret
, flags
);
523 virCheckSecretReturn(secret
, NULL
);
526 if (conn
->secretDriver
!= NULL
&& conn
->secretDriver
->secretGetXMLDesc
!= NULL
) {
529 ret
= conn
->secretDriver
->secretGetXMLDesc(secret
, flags
);
535 virReportUnsupportedError();
538 virDispatchError(conn
);
545 * @secret: A virSecret secret
546 * @value: Value of the secret
547 * @value_size: Size of the value
548 * @flags: extra flags; not used yet, so callers should always pass 0
550 * Sets the value of a secret.
552 * Returns 0 on success, -1 on failure.
557 virSecretSetValue(virSecretPtr secret
, const unsigned char *value
,
558 size_t value_size
, unsigned int flags
)
562 VIR_DEBUG("secret=%p, value=%p, value_size=%zu, flags=0x%x", secret
, value
,
567 virCheckSecretReturn(secret
, -1);
570 virCheckReadOnlyGoto(conn
->flags
, error
);
571 virCheckNonNullArgGoto(value
, error
);
573 if (conn
->secretDriver
!= NULL
&& conn
->secretDriver
->secretSetValue
!= NULL
) {
576 ret
= conn
->secretDriver
->secretSetValue(secret
, value
, value_size
, flags
);
582 virReportUnsupportedError();
585 virDispatchError(conn
);
592 * @secret: A virSecret connection
593 * @value_size: Place for storing size of the secret value
594 * @flags: extra flags; not used yet, so callers should always pass 0
596 * Fetches the value of a secret.
598 * Returns the secret value on success, NULL on failure. The caller must
599 * free() the secret value.
604 virSecretGetValue(virSecretPtr secret
, size_t *value_size
, unsigned int flags
)
608 VIR_DEBUG("secret=%p, value_size=%p, flags=0x%x", secret
, value_size
, flags
);
612 virCheckSecretReturn(secret
, NULL
);
615 virCheckReadOnlyGoto(conn
->flags
, error
);
616 virCheckNonNullArgGoto(value_size
, error
);
618 if (conn
->secretDriver
!= NULL
&& conn
->secretDriver
->secretGetValue
!= NULL
) {
621 ret
= conn
->secretDriver
->secretGetValue(secret
, value_size
, flags
);
627 virReportUnsupportedError();
630 virDispatchError(conn
);
637 * @secret: A virSecret secret
639 * Deletes the specified secret. This does not free the associated
640 * virSecretPtr object.
642 * Returns 0 on success, -1 on failure.
647 virSecretUndefine(virSecretPtr secret
)
651 VIR_DEBUG("secret=%p", secret
);
655 virCheckSecretReturn(secret
, -1);
658 virCheckReadOnlyGoto(conn
->flags
, error
);
660 if (conn
->secretDriver
!= NULL
&& conn
->secretDriver
->secretUndefine
!= NULL
) {
663 ret
= conn
->secretDriver
->secretUndefine(secret
);
669 virReportUnsupportedError();
672 virDispatchError(conn
);
679 * @secret: the secret to hold a reference on
681 * Increment the reference count on the secret. For each additional call to
682 * this method, there shall be a corresponding call to virSecretFree to release
683 * the reference count, once the caller no longer needs the reference to this
686 * This method is typically useful for applications where multiple threads are
687 * using a connection, and it is required that the connection remain open until
688 * all threads have finished using it. ie, each new thread using a secret would
689 * increment the reference count.
691 * Returns 0 in case of success, -1 in case of failure.
696 virSecretRef(virSecretPtr secret
)
698 VIR_DEBUG("secret=%p", secret
);
702 virCheckSecretReturn(secret
, -1);
704 virObjectRef(secret
);
711 * @secret: pointer to a secret
713 * Release the secret handle. The underlying secret continues to exist.
715 * Returns 0 on success, or -1 on error
720 virSecretFree(virSecretPtr secret
)
722 VIR_DEBUG("secret=%p", secret
);
726 virCheckSecretReturn(secret
, -1);
728 virObjectUnref(secret
);
734 * virConnectSecretEventRegisterAny:
735 * @conn: pointer to the connection
736 * @secret: pointer to the secret
737 * @eventID: the event type to receive
738 * @cb: callback to the function handling secret events
739 * @opaque: opaque data to pass on to the callback
740 * @freecb: optional function to deallocate opaque when not used anymore
742 * Adds a callback to receive notifications of arbitrary secret events
743 * occurring on a secret. This function requires that an event loop
744 * has been previously registered with virEventRegisterImpl() or
745 * virEventRegisterDefaultImpl().
747 * If @secret is NULL, then events will be monitored for any secret.
748 * If @secret is non-NULL, then only the specific secret will be monitored.
750 * Most types of events have a callback providing a custom set of parameters
751 * for the event. When registering an event, it is thus necessary to use
752 * the VIR_SECRET_EVENT_CALLBACK() macro to cast the
753 * supplied function pointer to match the signature of this method.
755 * The virSecretPtr object handle passed into the callback upon delivery
756 * of an event is only valid for the duration of execution of the callback.
757 * If the callback wishes to keep the secret object after the callback
758 * returns, it shall take a reference to it, by calling virSecretRef().
759 * The reference can be released once the object is no longer required
760 * by calling virSecretFree().
762 * The return value from this method is a positive integer identifier
763 * for the callback. To unregister a callback, this callback ID should
764 * be passed to the virConnectSecretEventDeregisterAny() method.
766 * Returns a callback identifier on success, -1 on failure.
771 virConnectSecretEventRegisterAny(virConnectPtr conn
,
774 virConnectSecretEventGenericCallback cb
,
776 virFreeCallback freecb
)
778 VIR_DEBUG("conn=%p, secret=%p, eventID=%d, cb=%p, opaque=%p, freecb=%p",
779 conn
, secret
, eventID
, cb
, opaque
, freecb
);
783 virCheckConnectReturn(conn
, -1);
785 virCheckSecretGoto(secret
, error
);
786 if (secret
->conn
!= conn
) {
787 char uuidstr
[VIR_UUID_STRING_BUFLEN
];
788 virUUIDFormat(secret
->uuid
, uuidstr
);
789 virReportInvalidArg(secret
,
790 _("secret '%1$s' in %2$s must match connection"),
791 uuidstr
, __FUNCTION__
);
795 virCheckNonNullArgGoto(cb
, error
);
796 virCheckNonNegativeArgGoto(eventID
, error
);
798 if (eventID
>= VIR_SECRET_EVENT_ID_LAST
) {
799 virReportInvalidArg(eventID
,
800 _("eventID in %1$s must be less than %2$d"),
801 __FUNCTION__
, VIR_SECRET_EVENT_ID_LAST
);
805 if (conn
->secretDriver
&&
806 conn
->secretDriver
->connectSecretEventRegisterAny
) {
808 ret
= conn
->secretDriver
->connectSecretEventRegisterAny(conn
,
819 virReportUnsupportedError();
821 virDispatchError(conn
);
827 * virConnectSecretEventDeregisterAny:
828 * @conn: pointer to the connection
829 * @callbackID: the callback identifier
831 * Removes an event callback. The callbackID parameter should be the
832 * value obtained from a previous virConnectSecretEventRegisterAny() method.
834 * Returns 0 on success, -1 on failure.
839 virConnectSecretEventDeregisterAny(virConnectPtr conn
,
842 VIR_DEBUG("conn=%p, callbackID=%d", conn
, callbackID
);
846 virCheckConnectReturn(conn
, -1);
847 virCheckNonNegativeArgGoto(callbackID
, error
);
849 if (conn
->secretDriver
&&
850 conn
->secretDriver
->connectSecretEventDeregisterAny
) {
852 ret
= conn
->secretDriver
->connectSecretEventDeregisterAny(conn
,
859 virReportUnsupportedError();
861 virDispatchError(conn
);