From nobody Tue Jan 10 07:52:06 2023 X-Original-To: dev-commits-src-branches@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 4Nrjh66ytvz2r1D6; Tue, 10 Jan 2023 07:52:06 +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 4Nrjh66XkHz4GT0; Tue, 10 Jan 2023 07:52:06 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673337126; 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=Pb2dIno/9OAIZmbrpp/xw+H1O88UtuYCq4pS8kViwkE=; b=V/uM41SIo/q2OvR71rXC4Lf2X16jz903OwUIBqsnogDAta7mVO54VtsZAVFjnH9Mn1blQU 6/M+Ohbuet4KNjOhGc2YeI/GPCzkQUkc6rBEgfuEmRGa5U8hd8a3SjJRHDGRsxBnWLGvw+ chFDgTxxrRDC+PSnQa10MvOfMNa6UTeUxQafN2UrehSbdlAoLB7xDTuFWWSPyg1ojssPac 9ih9nb/W35H6/T1IUBXSlaGXPKsFDwNrN1T7lcEOt7PSltcSvBiHRKJwKFeWSpyYSjNi0W NvQYVb7idDJOg5cbnLVFAt3P7Lkr9PUD4B9WJxAH/h2jHvF7JYa/OkMY4t6xsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673337126; 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=Pb2dIno/9OAIZmbrpp/xw+H1O88UtuYCq4pS8kViwkE=; b=ojR+qKOg/+UsfNswpJHPEDZgEDzOF4Qu+rBCHJ9I5Ng4bMGr7Q2pQ0QybVa08Xyfzs3DpW OQXKgDUT+lXr63Q7QX1BJJAol4Srl2OGBnIuZeIO17fooIgcM43fl43Fpf/DavKCrxd8Rk unlcyB92F4Fx+ZQJv3wnm2MJ18hQ+ppKEmNQ05fpmFtQodSyqRYh1v56KYYawCGXsxIr3K a8NvUyutJ3MnVVfINvxe/huvO4gN46g4GsEACmFXCxaxLDHZWKmGmNG9XWmQ9ZKnZLwQH5 +6C/NcvWm/Y4+UYa2/Kcm0YYWPHc3gudv41iUJyh5IlLKD4dT/7N5zJgIG8ekg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1673337126; a=rsa-sha256; cv=none; b=YPRQc6DfGzAhmXVkV5Y/TG5m+7L2zBx0+Ztylgx3xcohm0JAtGuxyZ39BhyZD3qHK4dqyK PMsrfdzKfxjbngXcwuRftsNCEbFfomMbauYvBE253yQxkQca0kVZPahRJJA8yo70B4jwPf cCHY2cnLK/lZT6GySeeHWiAl0GnECAIKbRVm61Tzmz+f7oLqWsnTh6KcRPc1qQvdQrVauM 87s7Qzp3h9P+0jvq8OK5mPp1aTIyKEEIuZdPvOXwh7uEnXWx5dkTRvgzJbWtSQYi26p6rR NSY0RBXVdr7MeHoxnOX1NTt6prJfH1kxg+rcRKhDqKH+PWOC65e7fIYLxRwDCw== 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 4Nrjh65c61z11dx; Tue, 10 Jan 2023 07:52:06 +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 30A7q6MN079915; Tue, 10 Jan 2023 07:52:06 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30A7q6qu079914; Tue, 10 Jan 2023 07:52:06 GMT (envelope-from git) Date: Tue, 10 Jan 2023 07:52:06 GMT Message-Id: <202301100752.30A7q6qu079914@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: "Tobias C. Berner" Subject: git: 8fd1953b7eb2 - stable/13 - bin/uuidgen: add support for v4 uuids List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@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/stable/13 X-Git-Reftype: branch X-Git-Commit: 8fd1953b7eb2416e29b6b784f03082725e96f685 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by tcberner (ports committer): URL: https://cgit.FreeBSD.org/src/commit/?id=8fd1953b7eb2416e29b6b784f03082725e96f685 commit 8fd1953b7eb2416e29b6b784f03082725e96f685 Author: Tobias C. Berner AuthorDate: 2022-12-14 05:59:36 +0000 Commit: Tobias C. Berner CommitDate: 2023-01-10 07:48:55 +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 (cherry picked from commit f176fe8e7f638e585afcd2f4dd52a522c4648f63) --- 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); }