From nobody Sat Dec 17 09:08:43 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4NZ0Wc1zpRzZmmY; Sat, 17 Dec 2022 09:08:44 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4NZ0Wc1TtZz46dG; Sat, 17 Dec 2022 09:08:44 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1671268124; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rbK9frxEV7kfJjm9XSHEbXy+XQgInfSrJSQMi3CCGYI=; b=uMn41L2bzVKRJbNwqMEDbkJ6aoL6G9xMQVxHnvkdwGxWDZqpKZXaAywR9yZ2uW0MRPMi6U buOvY79wix4MLSaEbdf+wO7WCrQtv88XbB/rjjCtR8RAln9SacRRZxPNz2mV4sSAf9ewQ/ A+vs/7gYMuoE1pd7qlDd9kEQJ8sB8vCsCiluI7EYcEfEjcJWx5U99Q85fJthyuKt1GZBPQ LuVu72R2/hwAz5ViRzH8J9DHFjMSJrpre/Msl/ocsv0zuK2/YWiHwBLeiAB6X2OpHw0S8P dwCbFLu1azkjBAHYCk3ElbLgm1hakND3r55kCWfsH+Vp8m6BvW1SSDNowCcVUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1671268124; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rbK9frxEV7kfJjm9XSHEbXy+XQgInfSrJSQMi3CCGYI=; b=bVUZfTarnHH/Ac19GF4kxD4JxTtaQgOWdmEPFLo6gZ5K+r02PHOZRxmx2FPL1wsrmerXI9 3bB4jSoI8NpgQNNcUCny0CMX4WOrlEWPGDCGG/GGS/tuuc//0yYhJZNasbg7Mp7G5CoFat sabFnD2IVLz9suJXZaSS2ILQw07+tKn68+WKhmOTtpnUs2CeGe3GFJzEPbqg0ZRTMJ5SiS Od9iOCJdiHmNQPhyRznDyt645cUZ+8fESgiJBcyssF7yZsKgjDIqFenzsr8cgxfP2sqFdz V0AFd5m3C0XHPnObisHcGBb25YuGYyhNshcxphtL042M7wQpWC7RQUm9AxGAIg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1671268124; a=rsa-sha256; cv=none; b=DnqhvP2M9cCRqRPN2prROy1sDouBaQy4U625R1Xq6uxkr3X4TQMRo6dbvR2nflIHPr3H5Q RvOmba+cZMe0lhomjfKF9ZsQ6NIOUUesX8wyXPt5b30EixMT/P3ILWa5zR86+vTxgTrIeC cINx3z4M0B7o8cislSKH5OFKITzI+GMj3ZXCex0ZdsJGb0171XCUfnZbnQZtmzqG8Ty1oZ HcJYU6+pfwMXYN/pKHzbQbk2u6AjQibmzQY3iITOiMQfIRT9fiSB3Dl7OInAEJxlCf6vYy tWSm7aZXhVrcFf21H7HQmvPNgKwn4NsYwMlTuCCMD4uofkuyMuGUihH5OufIcw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4NZ0Wc0RBCzmS6; Sat, 17 Dec 2022 09:08:44 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2BH98h7r098819; Sat, 17 Dec 2022 09:08:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2BH98h3p098818; Sat, 17 Dec 2022 09:08:43 GMT (envelope-from git) Date: Sat, 17 Dec 2022 09:08:43 GMT Message-Id: <202212170908.2BH98h3p098818@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Tobias C. Berner" Subject: git: f176fe8e7f63 - main - bin/uuidgen: add support for v4 uuids List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: tcberner X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: f176fe8e7f638e585afcd2f4dd52a522c4648f63 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by tcberner (ports committer): URL: https://cgit.FreeBSD.org/src/commit/?id=f176fe8e7f638e585afcd2f4dd52a522c4648f63 commit f176fe8e7f638e585afcd2f4dd52a522c4648f63 Author: Tobias C. Berner AuthorDate: 2022-12-14 05:59:36 +0000 Commit: Tobias C. Berner CommitDate: 2022-12-17 09:03:38 +0000 bin/uuidgen: add support for v4 uuids The version 4 UUID is meant for generating UUIDs from truly-random or pseudo-random numbers. [1] bin/uuidgen gained the new flag '-r' to create version 4 UUID. [1] RFC 4122, https://www.rfc-editor.org/rfc/rfc4122#section-4.4 Reviewed by: pstef Approved by: bapt MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D37695 --- bin/uuidgen/uuidgen.1 | 3 ++ bin/uuidgen/uuidgen.c | 76 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 67 insertions(+), 12 deletions(-) diff --git a/bin/uuidgen/uuidgen.1 b/bin/uuidgen/uuidgen.1 index f793fb248cdf..83326e3a189a 100644 --- a/bin/uuidgen/uuidgen.1 +++ b/bin/uuidgen/uuidgen.1 @@ -33,6 +33,7 @@ .Sh SYNOPSIS .Nm .Op Fl 1 +.Op Fl r .Op Fl n Ar count .Op Fl o Ar filename .Sh DESCRIPTION @@ -50,6 +51,8 @@ This option only has effect if multiple identifiers are to be generated and instructs .Nm to not generate them in batch, but one at a time. +.It Fl r +This option controls creation of random UUID (version 4). .It Fl n This option controls the number of identifiers generated. By default, multiple identifiers are generated in batch. diff --git a/bin/uuidgen/uuidgen.c b/bin/uuidgen/uuidgen.c index e135dfc4b6e0..061907f57109 100644 --- a/bin/uuidgen/uuidgen.c +++ b/bin/uuidgen/uuidgen.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2002 Marcel Moolenaar + * Copyright (c) 2022 Tobias C. Berner * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -40,26 +41,63 @@ __FBSDID("$FreeBSD$"); static void usage(void) { - (void)fprintf(stderr, "usage: uuidgen [-1] [-n count] [-o filename]\n"); + (void)fprintf(stderr, + "usage: uuidgen [-1] [-r] [-n count] [-o filename]\n"); exit(1); } +static int +uuidgen_v4(struct uuid *store, int count) +{ + int size; + struct uuid *item; + + if (count < 1) { + errno = EINVAL; + return (-1); + } + size = sizeof(struct uuid) * count; + arc4random_buf(store, size); + item = store; + for (int i = 0; i < count; ++i) { + /* + * Set the two most significant bits (bits 6 and 7) of the + * clock_seq_hi_and_reserved to zero and one, respectively. + */ + item->clock_seq_hi_and_reserved &= ~(3 << 6); + item->clock_seq_hi_and_reserved |= (2 << 6); + /* + * Set the four most significant bits (bits 12 through 15) of + * the time_hi_and_version field to the 4-bit version number + * from Section 4.1.3. + */ + item->time_hi_and_version &= ~(15 << 12); + item->time_hi_and_version |= (4 << 12); + item++; + }; + return (0); +} + int main(int argc, char *argv[]) { FILE *fp; uuid_t *store, *uuid; char *p; - int ch, count, i, iterate, status; + int ch, count, i, iterate, status, version; - count = -1; /* no count yet */ - fp = stdout; /* default output file */ - iterate = 0; /* not one at a time */ - while ((ch = getopt(argc, argv, "1n:o:")) != -1) + count = -1; /* no count yet */ + fp = stdout; /* default output file */ + iterate = 0; /* not one at a time */ + version = 1; /* create uuid v1 by default */ + while ((ch = getopt(argc, argv, "1rn:o:")) != -1) switch (ch) { case '1': iterate = 1; break; + case 'r': + version = 4; + break; case 'n': if (count > 0) usage(); @@ -92,19 +130,33 @@ main(int argc, char *argv[]) if (count == -1) count = 1; - store = (uuid_t*)malloc(sizeof(uuid_t) * count); + store = (uuid_t *)malloc(sizeof(uuid_t) * count); if (store == NULL) err(1, "malloc()"); if (!iterate) { /* Get them all in a single batch */ - if (uuidgen(store, count) != 0) - err(1, "uuidgen()"); + if (version == 1) { + if (uuidgen(store, count) != 0) + err(1, "uuidgen()"); + } else if (version == 4) { + if (uuidgen_v4(store, count) != 0) + err(1, "uuidgen_v4()"); + } else { + err(1, "unsupported version"); + } } else { uuid = store; for (i = 0; i < count; i++) { - if (uuidgen(uuid++, 1) != 0) - err(1, "uuidgen()"); + if (version == 1) { + if (uuidgen(uuid++, 1) != 0) + err(1, "uuidgen()"); + } else if (version == 4) { + if (uuidgen_v4(uuid++, 1) != 0) + err(1, "uuidgen_v4()"); + } else { + err(1, "unsupported version"); + } } } @@ -112,7 +164,7 @@ main(int argc, char *argv[]) while (count--) { uuid_to_string(uuid++, &p, &status); if (status != uuid_s_ok) - err(1, "cannot stringify a UUID"); + err(1, "cannot stringify a UUID"); fprintf(fp, "%s\n", p); free(p); }