Skip to content

Commit

Permalink
Maybe we can just use built in UUID functions for generating IDs?
Browse files Browse the repository at this point in the history
  • Loading branch information
jmuehlner committed Nov 15, 2023
1 parent 3a8a23e commit 9cc9c08
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 54 deletions.
83 changes: 43 additions & 40 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -94,49 +94,52 @@ AC_CHECK_LIB([dl], [dlopen],
AC_MSG_ERROR("libdl is required on systems which do not otherwise provide dlopen()"),
[#include <dlfcn.h>])])

#
# libuuid
#

have_libuuid=disabled
AC_ARG_WITH([libuuid],
[AS_HELP_STRING([--with-libuuid],
[use libuuid to generate unique identifiers @<:@default=check@:>@])],
[],
[with_libuuid=check])

if test "x$with_libuuid" != "xno"
if test "x$with_cygwin" = "xno"
then
have_libuuid=yes
AC_CHECK_LIB([uuid], [uuid_generate],
[UUID_LIBS=-luuid]
[AC_DEFINE([HAVE_LIBUUID],, [Whether libuuid is available])],
[have_libuuid=no])
fi
#
# libuuid
#

have_libuuid=disabled
AC_ARG_WITH([libuuid],
[AS_HELP_STRING([--with-libuuid],
[use libuuid to generate unique identifiers @<:@default=check@:>@])],
[],
[with_libuuid=check])

# OSSP UUID (if libuuid is unavilable)
if test "x${have_libuuid}" != "xyes"
then
if test "x$with_libuuid" != "xno"
then
have_libuuid=yes
AC_CHECK_LIB([uuid], [uuid_generate],
[UUID_LIBS=-luuid]
[AC_DEFINE([HAVE_LIBUUID],, [Whether libuuid is available])],
[have_libuuid=no])
fi

AC_CHECK_LIB([ossp-uuid], [uuid_make], [UUID_LIBS=-lossp-uuid],
AC_CHECK_LIB([uuid], [uuid_make], [UUID_LIBS=-luuid],
AC_MSG_ERROR([
--------------------------------------------
Unable to find libuuid or the OSSP UUID library.
Either libuuid (from util-linux) or the OSSP UUID library is required for
guacamole-server to be built.
--------------------------------------------])))

# Check for and validate OSSP uuid.h header
AC_CHECK_HEADERS([ossp/uuid.h])
AC_CHECK_DECL([uuid_make],,
AC_MSG_ERROR("No OSSP uuid.h found in include path"),
[#ifdef HAVE_OSSP_UUID_H
#include <ossp/uuid.h>
#else
#include <uuid.h>
#endif
])
# OSSP UUID (if libuuid is unavilable)
if test "x${have_libuuid}" != "xyes"
then

AC_CHECK_LIB([ossp-uuid], [uuid_make], [UUID_LIBS=-lossp-uuid],
AC_CHECK_LIB([uuid], [uuid_make], [UUID_LIBS=-luuid],
AC_MSG_ERROR([
--------------------------------------------
Unable to find libuuid or the OSSP UUID library.
Either libuuid (from util-linux) or the OSSP UUID library is required for
guacamole-server to be built.
--------------------------------------------])))

# Check for and validate OSSP uuid.h header
AC_CHECK_HEADERS([ossp/uuid.h])
AC_CHECK_DECL([uuid_make],,
AC_MSG_ERROR("No OSSP uuid.h found in include path"),
[#ifdef HAVE_OSSP_UUID_H
#include <ossp/uuid.h>
#else
#include <uuid.h>
#endif
])
fi
fi

# cunit
Expand Down
51 changes: 37 additions & 14 deletions src/libguac/id.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
#include "guacamole/error.h"
#include <guacamole/id.h>

#if defined(HAVE_LIBUUID)

#ifdef CYGWIN_BUILD
#include <rpcdce.h>
#elif defined(HAVE_LIBUUID)
#include <uuid/uuid.h>
#elif defined(HAVE_OSSP_UUID_H)
#include <ossp/uuid.h>
Expand All @@ -37,6 +40,38 @@ char* guac_generate_id(char prefix) {
char* buffer;
char* identifier;

/* Allocate buffer for future formatted ID */
buffer = malloc(GUAC_UUID_LEN + 1);
if (buffer == NULL) {
guac_error = GUAC_STATUS_NO_MEMORY;
guac_error_message = "Could not allocate memory for unique ID";
return NULL;
}

identifier = &(buffer[1]);

#ifdef CYGWIN_BUILD

/* Generate a UUID using a built in windows function */
UUID uuid;
UuidCreate(&uuid);

/* Convert the UUID to an all-caps, null-terminated tring */
RPC_CSTR uuid_string;
if (UuidToString(uuid, &uuid_string) == RPC_S_OUT_OF_MEMORY) {
guac_error = GUAC_STATUS_NO_MEMORY;
guac_error_message = "Could not allocate memory for unique ID";
return NULL;
}

/* Copy over lowercase letters to the final target string */
for (int i = 0; i < GUAC_UUID_LEN; i++)
identifier[i] = tolower(uuid_string[i]);

RpcStringFree(uuid_string);

#else

/* Prepare object to receive generated UUID */
#ifdef HAVE_LIBUUID
uuid_t uuid;
Expand All @@ -61,19 +96,6 @@ char* guac_generate_id(char prefix) {
}
#endif

/* Allocate buffer for future formatted ID */
buffer = malloc(GUAC_UUID_LEN + 1);
if (buffer == NULL) {
#ifndef HAVE_LIBUUID
uuid_destroy(uuid);
#endif
guac_error = GUAC_STATUS_NO_MEMORY;
guac_error_message = "Could not allocate memory for unique ID";
return NULL;
}

identifier = &(buffer[1]);

/* Convert UUID to string to produce unique identifier */
#ifdef HAVE_LIBUUID
uuid_unparse_lower(uuid, identifier);
Expand All @@ -89,6 +111,7 @@ char* guac_generate_id(char prefix) {

/* Clean up generated UUID */
uuid_destroy(uuid);
#endif
#endif

buffer[0] = prefix;
Expand Down

0 comments on commit 9cc9c08

Please sign in to comment.