node_device_udev: add error reporting to udevProcessCCWGroup
[libvirt.git] / src / libvirt-secret.c
blob4d0c88745de9048c6f819896f96c253bda1f83d0
1 /*
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/>.
21 #include <config.h>
23 #include "datatypes.h"
24 #include "virlog.h"
26 VIR_LOG_INIT("libvirt.secret");
28 #define VIR_FROM_THIS VIR_FROM_SECRET
30 /**
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.
39 * Since: 0.7.1
41 virConnectPtr
42 virSecretGetConnect(virSecretPtr secret)
44 VIR_DEBUG("secret=%p", secret);
46 virResetLastError();
48 virCheckSecretReturn(secret, NULL);
50 return secret->conn;
54 /**
55 * virConnectNumOfSecrets:
56 * @conn: virConnect connection
58 * Fetch number of currently defined secrets.
60 * Returns the number currently defined secrets.
62 * Since: 0.7.1
64 int
65 virConnectNumOfSecrets(virConnectPtr conn)
67 VIR_DEBUG("conn=%p", conn);
69 virResetLastError();
71 virCheckConnectReturn(conn, -1);
73 if (conn->secretDriver != NULL &&
74 conn->secretDriver->connectNumOfSecrets != NULL) {
75 int ret;
77 ret = conn->secretDriver->connectNumOfSecrets(conn);
78 if (ret < 0)
79 goto error;
80 return ret;
83 virReportUnsupportedError();
85 error:
86 virDispatchError(conn);
87 return -1;
91 /**
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
96 * number of secrets).
97 * @flags: bitwise-OR of virConnectListAllSecretsFlags.
99 * Collect the list of secrets, and allocate an array to store those
100 * objects.
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.
124 * Since: 0.10.2
127 virConnectListAllSecrets(virConnectPtr conn,
128 virSecretPtr **secrets,
129 unsigned int flags)
131 VIR_DEBUG("conn=%p, secrets=%p, flags=0x%x", conn, secrets, flags);
133 virResetLastError();
135 if (secrets)
136 *secrets = NULL;
138 virCheckConnectReturn(conn, -1);
140 if (conn->secretDriver &&
141 conn->secretDriver->connectListAllSecrets) {
142 int ret;
143 ret = conn->secretDriver->connectListAllSecrets(conn, secrets, flags);
144 if (ret < 0)
145 goto error;
146 return ret;
149 virReportUnsupportedError();
151 error:
152 virDispatchError(conn);
153 return -1;
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.
170 * Since: 0.7.1
173 virConnectListSecrets(virConnectPtr conn, char **uuids, int maxuuids)
175 VIR_DEBUG("conn=%p, uuids=%p, maxuuids=%d", conn, uuids, maxuuids);
177 virResetLastError();
179 virCheckConnectReturn(conn, -1);
180 virCheckNonNullArrayArgGoto(uuids, maxuuids, error);
181 virCheckNonNegativeArgGoto(maxuuids, error);
183 if (conn->secretDriver != NULL && conn->secretDriver->connectListSecrets != NULL) {
184 int ret;
186 ret = conn->secretDriver->connectListSecrets(conn, uuids, maxuuids);
187 if (ret < 0)
188 goto error;
189 return ret;
192 virReportUnsupportedError();
194 error:
195 virDispatchError(conn);
196 return -1;
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.
214 * Since: 0.7.1
216 virSecretPtr
217 virSecretLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
219 VIR_UUID_DEBUG(conn, uuid);
221 virResetLastError();
223 virCheckConnectReturn(conn, NULL);
224 virCheckNonNullArgGoto(uuid, error);
226 if (conn->secretDriver &&
227 conn->secretDriver->secretLookupByUUID) {
228 virSecretPtr ret;
229 ret = conn->secretDriver->secretLookupByUUID(conn, uuid);
230 if (!ret)
231 goto error;
232 return ret;
235 virReportUnsupportedError();
237 error:
238 virDispatchError(conn);
239 return NULL;
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.
257 * Since: 0.7.1
259 virSecretPtr
260 virSecretLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
262 unsigned char uuid[VIR_UUID_BUFLEN];
263 VIR_DEBUG("conn=%p, uuidstr=%s", conn, NULLSTR(uuidstr));
265 virResetLastError();
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"),
273 __FUNCTION__);
274 goto error;
277 return virSecretLookupByUUID(conn, &uuid[0]);
279 error:
280 virDispatchError(conn);
281 return NULL;
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.
301 * Since: 0.7.1
303 virSecretPtr
304 virSecretLookupByUsage(virConnectPtr conn,
305 int usageType,
306 const char *usageID)
308 VIR_DEBUG("conn=%p, usageType=%d usageID=%s", conn, usageType, NULLSTR(usageID));
310 virResetLastError();
312 virCheckConnectReturn(conn, NULL);
313 virCheckNonNullArgGoto(usageID, error);
315 if (conn->secretDriver &&
316 conn->secretDriver->secretLookupByUsage) {
317 virSecretPtr ret;
318 ret = conn->secretDriver->secretLookupByUsage(conn, usageType, usageID);
319 if (!ret)
320 goto error;
321 return ret;
324 virReportUnsupportedError();
326 error:
327 virDispatchError(conn);
328 return NULL;
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.
350 * Since: 0.7.1
352 virSecretPtr
353 virSecretDefineXML(virConnectPtr conn, const char *xml, unsigned int flags)
355 VIR_DEBUG("conn=%p, xml=%s, flags=0x%x", conn, NULLSTR(xml), flags);
357 virResetLastError();
359 virCheckConnectReturn(conn, NULL);
360 virCheckReadOnlyGoto(conn->flags, error);
361 virCheckNonNullArgGoto(xml, error);
363 if (conn->secretDriver != NULL && conn->secretDriver->secretDefineXML != NULL) {
364 virSecretPtr ret;
366 ret = conn->secretDriver->secretDefineXML(conn, xml, flags);
367 if (ret == NULL)
368 goto error;
369 return ret;
372 virReportUnsupportedError();
374 error:
375 virDispatchError(conn);
376 return NULL;
381 * virSecretGetUUID:
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
388 * -1 upon failure.
390 * Since: 0.7.1
393 virSecretGetUUID(virSecretPtr secret, unsigned char *uuid)
395 VIR_DEBUG("secret=%p", secret);
397 virResetLastError();
399 virCheckSecretReturn(secret, -1);
400 virCheckNonNullArgGoto(uuid, error);
402 memcpy(uuid, &secret->uuid[0], VIR_UUID_BUFLEN);
404 return 0;
406 error:
407 virDispatchError(secret->conn);
408 return -1;
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
418 * UUID see RFC4122.
420 * Returns -1 in case of error, 0 in case of success
422 * Since: 0.7.1
425 virSecretGetUUIDString(virSecretPtr secret, char *buf)
427 VIR_DEBUG("secret=%p, buf=%p", secret, buf);
429 virResetLastError();
431 virCheckSecretReturn(secret, -1);
432 virCheckNonNullArgGoto(buf, error);
434 virUUIDFormat(secret->uuid, buf);
435 return 0;
437 error:
438 virDispatchError(secret->conn);
439 return -1;
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,
454 * or -1 upon error.
456 * Since: 0.7.1
459 virSecretGetUsageType(virSecretPtr secret)
461 VIR_DEBUG("secret=%p", secret);
463 virResetLastError();
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,
485 * or NULL upon error
487 * Since: 0.7.1
489 const char *
490 virSecretGetUsageID(virSecretPtr secret)
492 VIR_DEBUG("secret=%p", secret);
494 virResetLastError();
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
510 * free() the XML.
512 * Since: 0.7.1
514 char *
515 virSecretGetXMLDesc(virSecretPtr secret, unsigned int flags)
517 virConnectPtr conn;
519 VIR_DEBUG("secret=%p, flags=0x%x", secret, flags);
521 virResetLastError();
523 virCheckSecretReturn(secret, NULL);
524 conn = secret->conn;
526 if (conn->secretDriver != NULL && conn->secretDriver->secretGetXMLDesc != NULL) {
527 char *ret;
529 ret = conn->secretDriver->secretGetXMLDesc(secret, flags);
530 if (ret == NULL)
531 goto error;
532 return ret;
535 virReportUnsupportedError();
537 error:
538 virDispatchError(conn);
539 return NULL;
544 * virSecretSetValue:
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.
554 * Since: 0.7.1
557 virSecretSetValue(virSecretPtr secret, const unsigned char *value,
558 size_t value_size, unsigned int flags)
560 virConnectPtr conn;
562 VIR_DEBUG("secret=%p, value=%p, value_size=%zu, flags=0x%x", secret, value,
563 value_size, flags);
565 virResetLastError();
567 virCheckSecretReturn(secret, -1);
568 conn = secret->conn;
570 virCheckReadOnlyGoto(conn->flags, error);
571 virCheckNonNullArgGoto(value, error);
573 if (conn->secretDriver != NULL && conn->secretDriver->secretSetValue != NULL) {
574 int ret;
576 ret = conn->secretDriver->secretSetValue(secret, value, value_size, flags);
577 if (ret < 0)
578 goto error;
579 return ret;
582 virReportUnsupportedError();
584 error:
585 virDispatchError(conn);
586 return -1;
591 * virSecretGetValue:
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.
601 * Since: 0.7.1
603 unsigned char *
604 virSecretGetValue(virSecretPtr secret, size_t *value_size, unsigned int flags)
606 virConnectPtr conn;
608 VIR_DEBUG("secret=%p, value_size=%p, flags=0x%x", secret, value_size, flags);
610 virResetLastError();
612 virCheckSecretReturn(secret, NULL);
613 conn = secret->conn;
615 virCheckReadOnlyGoto(conn->flags, error);
616 virCheckNonNullArgGoto(value_size, error);
618 if (conn->secretDriver != NULL && conn->secretDriver->secretGetValue != NULL) {
619 unsigned char *ret;
621 ret = conn->secretDriver->secretGetValue(secret, value_size, flags);
622 if (ret == NULL)
623 goto error;
624 return ret;
627 virReportUnsupportedError();
629 error:
630 virDispatchError(conn);
631 return NULL;
636 * virSecretUndefine:
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.
644 * Since: 0.7.1
647 virSecretUndefine(virSecretPtr secret)
649 virConnectPtr conn;
651 VIR_DEBUG("secret=%p", secret);
653 virResetLastError();
655 virCheckSecretReturn(secret, -1);
656 conn = secret->conn;
658 virCheckReadOnlyGoto(conn->flags, error);
660 if (conn->secretDriver != NULL && conn->secretDriver->secretUndefine != NULL) {
661 int ret;
663 ret = conn->secretDriver->secretUndefine(secret);
664 if (ret < 0)
665 goto error;
666 return ret;
669 virReportUnsupportedError();
671 error:
672 virDispatchError(conn);
673 return -1;
678 * virSecretRef:
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
684 * object.
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.
693 * Since: 0.7.1
696 virSecretRef(virSecretPtr secret)
698 VIR_DEBUG("secret=%p", secret);
700 virResetLastError();
702 virCheckSecretReturn(secret, -1);
704 virObjectRef(secret);
705 return 0;
710 * virSecretFree:
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
717 * Since: 0.7.1
720 virSecretFree(virSecretPtr secret)
722 VIR_DEBUG("secret=%p", secret);
724 virResetLastError();
726 virCheckSecretReturn(secret, -1);
728 virObjectUnref(secret);
729 return 0;
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.
768 * Since: 3.0.0
771 virConnectSecretEventRegisterAny(virConnectPtr conn,
772 virSecretPtr secret,
773 int eventID,
774 virConnectSecretEventGenericCallback cb,
775 void *opaque,
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);
781 virResetLastError();
783 virCheckConnectReturn(conn, -1);
784 if (secret) {
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__);
792 goto error;
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);
802 goto error;
805 if (conn->secretDriver &&
806 conn->secretDriver->connectSecretEventRegisterAny) {
807 int ret;
808 ret = conn->secretDriver->connectSecretEventRegisterAny(conn,
809 secret,
810 eventID,
812 opaque,
813 freecb);
814 if (ret < 0)
815 goto error;
816 return ret;
819 virReportUnsupportedError();
820 error:
821 virDispatchError(conn);
822 return -1;
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.
836 * Since: 3.0.0
839 virConnectSecretEventDeregisterAny(virConnectPtr conn,
840 int callbackID)
842 VIR_DEBUG("conn=%p, callbackID=%d", conn, callbackID);
844 virResetLastError();
846 virCheckConnectReturn(conn, -1);
847 virCheckNonNegativeArgGoto(callbackID, error);
849 if (conn->secretDriver &&
850 conn->secretDriver->connectSecretEventDeregisterAny) {
851 int ret;
852 ret = conn->secretDriver->connectSecretEventDeregisterAny(conn,
853 callbackID);
854 if (ret < 0)
855 goto error;
856 return ret;
859 virReportUnsupportedError();
860 error:
861 virDispatchError(conn);
862 return -1;