uuid.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012, Digium, Inc.
  5. *
  6. * Mark Michelson <mmmichelson@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Universally unique identifier support
  20. *
  21. * \extref Depends on libuuid, a component of the e2fsprogs package - http://e2fsprogs.sourceforge.net/
  22. */
  23. #include "asterisk.h"
  24. #include <uuid/uuid.h>
  25. #include <fcntl.h>
  26. #include "asterisk/uuid.h"
  27. #include "asterisk/utils.h"
  28. #include "asterisk/strings.h"
  29. #include "asterisk/logger.h"
  30. #include "asterisk/lock.h"
  31. AST_MUTEX_DEFINE_STATIC(uuid_lock);
  32. static int has_dev_urandom;
  33. struct ast_uuid {
  34. uuid_t uu;
  35. };
  36. /*!
  37. * \internal
  38. * \brief Generate a UUID.
  39. * \since 12.0.0
  40. *
  41. * \param uuid Fill this with a generated UUID.
  42. *
  43. * \return Nothing
  44. */
  45. static void generate_uuid(struct ast_uuid *uuid)
  46. {
  47. /* libuuid provides three methods of generating uuids,
  48. * uuid_generate(), uuid_generate_random(), and uuid_generate_time().
  49. *
  50. * uuid_generate_random() creates a UUID based on random numbers. The method
  51. * attempts to use either /dev/urandom or /dev/random to generate random values.
  52. * If these resources are unavailable, then random numbers will be generated
  53. * using C library calls to generate pseudorandom numbers.
  54. * This method of generating UUIDs corresponds to section 4.4 of RFC 4122.
  55. *
  56. * uuid_generate_time() creates a UUID based on the current time plus
  57. * a system identifier (MAC address of the ethernet interface). This
  58. * method of generating UUIDs corresponds to section 4.2 of RFC 4122.
  59. *
  60. * uuid_generate() will check if /dev/urandom or /dev/random is available to
  61. * use. If so, it will use uuid_generate_random(). Otherwise, it will use
  62. * uuid_generate_time(). The idea is that it avoids using pseudorandom
  63. * numbers if necessary.
  64. *
  65. * For our purposes, we do not use the time-based UUID at all. There are
  66. * several reasons for this:
  67. *
  68. * 1) The time-based algorithm makes use of a daemon process (uuidd) in order
  69. * to ensure that any concurrent requests for UUIDs result in unique results.
  70. * Use of this daemon is a bit dodgy for a few reasons
  71. *
  72. * a) libuuid assumes a hardcoded location for the .pid file of the daemon.
  73. * However, the daemon could already be running on the system in a different
  74. * location than expected. If this is the case, then attempting to connect
  75. * to the daemon will fail, and attempting to launch another instance in
  76. * the expected location will also fail.
  77. *
  78. * b) If the daemon is not running, then the first attempt to create a
  79. * time-based UUID will result in launching the daemon. Because of the hard-
  80. * coded locations that libuuid assumes for the daemon, Asterisk must be
  81. * run with permissions that will allow for the daemon to be launched in
  82. * the expected directories.
  83. *
  84. * c) Once the daemon is running, concurrent requests for UUIDs are thread-safe.
  85. * However, the actual launching of the daemon is not thread-safe since libuuid
  86. * uses no synchronization primitives to ensure that only one thread (or process)
  87. * launches the daemon.
  88. *
  89. * d) When libuuid launches the daemon, it sets an inactivity timer.
  90. * If no UUID generation requests are issued in that time period,
  91. * then the daemon will exit. If a new request should occur after the daemon
  92. * exits, then the daemon will be relaunched. Given point c), we cannot
  93. * necessarily guarantee the thread-safety of time-based UUID generation since
  94. * we cannot necessarily guarantee the daemon is running as we expect.
  95. * We could set up a watchdog thread to generate UUIDs at regular intervals to
  96. * prevent the daemon from exiting, but frankly, that sucks.
  97. *
  98. * 2) Since the MAC address of the Ethernet interface is part of the UUID when
  99. * using the time-based method, there is information leaked.
  100. *
  101. * Given these drawbacks, we stick to only using random UUIDs. The chance of /dev/random
  102. * or /dev/urandom not existing on systems in this age is next to none.
  103. */
  104. /* XXX Currently, we only protect this call if the user has no /dev/urandom on their system.
  105. * If it turns out that there are issues with UUID generation despite the presence of
  106. * /dev/urandom, then we may need to make the locking/unlocking unconditional.
  107. */
  108. if (!has_dev_urandom) {
  109. ast_mutex_lock(&uuid_lock);
  110. }
  111. uuid_generate_random(uuid->uu);
  112. if (!has_dev_urandom) {
  113. ast_mutex_unlock(&uuid_lock);
  114. }
  115. }
  116. struct ast_uuid *ast_uuid_generate(void)
  117. {
  118. struct ast_uuid *uuid = ast_malloc(sizeof(*uuid));
  119. if (!uuid) {
  120. return NULL;
  121. }
  122. generate_uuid(uuid);
  123. return uuid;
  124. }
  125. char *ast_uuid_to_str(struct ast_uuid *uuid, char *buf, size_t size)
  126. {
  127. ast_assert(size >= AST_UUID_STR_LEN);
  128. uuid_unparse(uuid->uu, buf);
  129. return ast_str_to_lower(buf);
  130. }
  131. char *ast_uuid_generate_str(char *buf, size_t size)
  132. {
  133. struct ast_uuid uuid;
  134. generate_uuid(&uuid);
  135. return ast_uuid_to_str(&uuid, buf, size);
  136. }
  137. struct ast_uuid *ast_str_to_uuid(char *str)
  138. {
  139. struct ast_uuid *uuid = ast_malloc(sizeof(*uuid));
  140. int res;
  141. if (!uuid) {
  142. return NULL;
  143. }
  144. res = uuid_parse(str, uuid->uu);
  145. if (res) {
  146. ast_log(LOG_WARNING, "Unable to convert string %s into a UUID\n", str);
  147. ast_free(uuid);
  148. return NULL;
  149. }
  150. return uuid;
  151. }
  152. struct ast_uuid *ast_uuid_copy(struct ast_uuid *src)
  153. {
  154. struct ast_uuid *dst = ast_malloc(sizeof(*dst));
  155. if (!dst) {
  156. return NULL;
  157. }
  158. uuid_copy(dst->uu, src->uu);
  159. return dst;
  160. }
  161. int ast_uuid_compare(struct ast_uuid *left, struct ast_uuid *right)
  162. {
  163. return uuid_compare(left->uu, right->uu);
  164. }
  165. void ast_uuid_clear(struct ast_uuid *uuid)
  166. {
  167. uuid_clear(uuid->uu);
  168. }
  169. int ast_uuid_is_nil(struct ast_uuid *uuid)
  170. {
  171. return uuid_is_null(uuid->uu);
  172. }
  173. void ast_uuid_init(void)
  174. {
  175. /* This requires some explanation.
  176. *
  177. * libuuid generates UUIDs based on random number generation. This involves
  178. * opening a handle to /dev/urandom or /dev/random in order to get random
  179. * data for the UUIDs.
  180. *
  181. * This is thread-safe, to a point. The problem is that the first attempt
  182. * to generate a UUID will result in opening the random number handle. Once
  183. * the handle is opened, all further generation is thread safe. This
  184. * first generation can be potentially risky if multiple threads attempt
  185. * to generate a UUID at the same time, though, since there is no thread
  186. * synchronization used within libuuid. To get around this potential
  187. * issue, we go ahead and generate a UUID up front so that the underlying
  188. * work is done before we start requesting UUIDs for real.
  189. *
  190. * Think of this along the same lines as initializing a singleton.
  191. */
  192. uuid_t uu;
  193. int dev_urandom_fd;
  194. dev_urandom_fd = open("/dev/urandom", O_RDONLY);
  195. if (dev_urandom_fd < 0) {
  196. ast_log(LOG_WARNING, "It appears your system does not have /dev/urandom on it. This\n"
  197. "means that UUID generation will use a pseudorandom number generator. Since\n"
  198. "the thread-safety of your system's random number generator cannot\n"
  199. "be guaranteed, we have to synchronize UUID generation. This may result\n"
  200. "in decreased performance. It is highly recommended that you set up your\n"
  201. "system to have /dev/urandom\n");
  202. } else {
  203. has_dev_urandom = 1;
  204. close(dev_urandom_fd);
  205. }
  206. uuid_generate_random(uu);
  207. ast_debug(1, "UUID system initiated\n");
  208. }