Skip to content

Commit 3dbe74c

Browse files
committed
[mod_webdav] webdav_uuid_v4() to supplant libuuid (#1056)
Implement simple UUID v4 generator to supplant libuuid dependency x-ref: "libuuid - non-portabile configure.in" https://redmine.lighttpd.net/issues/1056
1 parent 381ec0a commit 3dbe74c

File tree

1 file changed

+52
-9
lines changed

1 file changed

+52
-9
lines changed

src/mod_webdav.c

+52-9
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,13 @@ typedef off_t loff_t;
241241
#ifndef MOD_WEBDAV_BUILD_MINIMAL
242242
#if defined(HAVE_LIBXML_H) && defined(HAVE_SQLITE3_H)
243243

244+
#define USE_LOCKS
244245
#define USE_PROPPATCH
245246
/* minor: libxml2 includes stdlib.h in headers, too */
246247
#include <libxml/tree.h>
247248
#include <libxml/parser.h>
248249
#include <sqlite3.h>
249250

250-
#if defined(HAVE_LIBUUID) && defined(HAVE_UUID_UUID_H)
251-
#define USE_LOCKS
252-
#include <uuid/uuid.h>
253-
#endif
254-
255251
#endif /* defined(HAVE_LIBXML_H) && defined(HAVE_SQLITE3_H) */
256252
#endif /* MOD_WEBDAV_BUILD_MINIMAL */
257253

@@ -5607,6 +5603,54 @@ webdav_conflicting_lock_cb (void * const vdata,
56075603
}
56085604

56095605

5606+
#include "rand.h" /* li_rand_pseudo_bytes() */
5607+
5608+
static void
5609+
webdav_uuid_v4 (char * const s) /* s receives 36 chars of output */
5610+
{
5611+
/* generate UUID version 4: random number based */
5612+
char uuid[16];
5613+
/*(could have written into s+20 instead of uuid[] of stack
5614+
* except that would violate 'restrict' keyword on li_tohex_lc() args)*/
5615+
/*(li_rand_pseudo_bytes() aims to produce high-quality pseudorandom bytes)*/
5616+
/*(If using li_rand_bytes() instead, then check return value)*/
5617+
li_rand_pseudo_bytes((unsigned char *)uuid, sizeof(uuid));
5618+
5619+
/* set version 4 */
5620+
uuid[6] &= 0x0f;
5621+
uuid[6] |= 0x40;
5622+
5623+
/* set variant (always DCE 1.1 only) */
5624+
uuid[8] &= 0x3f;
5625+
uuid[8] |= 0x80;
5626+
5627+
/* stringify UUID */
5628+
#if 0 /* write string into hex[] and then memcpy() into s */
5629+
char hex[32];
5630+
li_tohex_lc(hex, sizeof(hex), uuid, sizeof(uuid));
5631+
memcpy(s, hex, 8);
5632+
s[8] = '-';
5633+
memcpy(s+9, hex, 4);
5634+
s[13] = '-';
5635+
memcpy(s+14, hex, 4);
5636+
s[18] = '-';
5637+
memcpy(s+19, hex, 4);
5638+
s[23] = '-';
5639+
memcpy(s+24, hex, 12);
5640+
#else /* write string into s and then memmove() in-place */
5641+
li_tohex_lc(s+4, 36-4, uuid, sizeof(uuid));
5642+
memmove(s, s+4, 8);
5643+
s[8] = '-';
5644+
memmove(s+9, s+4+8, 4);
5645+
s[13] = '-';
5646+
memmove(s+14, s+4+8+4, 4);
5647+
s[18] = '-';
5648+
memmove(s+19, s+4+8+4+4, 4);
5649+
s[23] = '-';
5650+
#endif
5651+
}
5652+
5653+
56105654
static handler_t
56115655
mod_webdav_lock (request_st * const r, const plugin_config * const pconf)
56125656
{
@@ -5861,13 +5905,12 @@ mod_webdav_lock (request_st * const r, const plugin_config * const pconf)
58615905
lockdata.depth = 0; /* force Depth: 0 on non-collections */
58625906

58635907
/* create locktoken
5864-
* (uuid_unparse() output is 36 chars + '\0') */
5865-
uuid_t id;
5908+
* (uuid v4 string is 36 chars) */
58665909
char lockstr[sizeof("<urn:uuid:>") + 36] = "<urn:uuid:";
58675910
lockdata.locktoken.ptr = lockstr+1; /*(without surrounding <>)*/
58685911
lockdata.locktoken.used = sizeof(lockstr)-2;/*(without surrounding <>)*/
5869-
uuid_generate(id);
5870-
uuid_unparse(id, lockstr+sizeof("<urn:uuid:")-1);
5912+
lockstr[sizeof(lockstr)-2] = '\0';
5913+
webdav_uuid_v4(lockstr+sizeof("<urn:uuid:")-1);
58715914

58725915
/* XXX: consider fix TOC-TOU race condition by starting transaction
58735916
* and re-running webdav_lock_activelocks() check before running

0 commit comments

Comments
 (0)