2 * Copyright (c) 2002 Marcel Moolenaar
3 * Copyright (c) 2022 Tobias C. Berner
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/capsicum.h>
34 #include <capsicum_helpers.h>
45 "usage: uuidgen [-1] [-r] [-n count] [-o filename]\n");
50 uuid_to_compact_string(const uuid_t *u, char **s, uint32_t *status)
62 uuid_create_nil(&nil, NULL);
65 asprintf(s, "%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x",
66 u->time_low, u->time_mid, u->time_hi_and_version,
67 u->clock_seq_hi_and_reserved, u->clock_seq_low, u->node[0],
68 u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]);
70 if (*s == NULL && status != NULL)
71 *status = uuid_s_no_memory;
75 uuidgen_v4(struct uuid *store, int count)
84 size = sizeof(struct uuid) * count;
85 arc4random_buf(store, size);
87 for (int i = 0; i < count; ++i) {
89 * Set the two most significant bits (bits 6 and 7) of the
90 * clock_seq_hi_and_reserved to zero and one, respectively.
92 item->clock_seq_hi_and_reserved &= ~(3 << 6);
93 item->clock_seq_hi_and_reserved |= (2 << 6);
95 * Set the four most significant bits (bits 12 through 15) of
96 * the time_hi_and_version field to the 4-bit version number
99 item->time_hi_and_version &= ~(15 << 12);
100 item->time_hi_and_version |= (4 << 12);
107 main(int argc, char *argv[])
110 uuid_t *store, *uuid;
112 int ch, count, i, iterate, status, version;
113 void (*tostring)(const uuid_t *, char **, uint32_t *) = uuid_to_string;
115 count = -1; /* no count yet */
116 fp = stdout; /* default output file */
117 iterate = 0; /* not one at a time */
118 version = 1; /* create uuid v1 by default */
119 while ((ch = getopt(argc, argv, "1crn:o:")) != -1)
125 tostring = uuid_to_compact_string;
133 count = strtol(optarg, &p, 10);
134 if (*p != 0 || count < 1)
139 errx(1, "multiple output files not allowed");
140 fp = fopen(optarg, "w");
153 caph_cache_catpages();
154 if (caph_limit_stdio() < 0)
155 err(1, "Unable to limit stdio");
156 if (caph_enter() < 0)
157 err(1, "Unable to enter capability mode");
162 store = (uuid_t *)malloc(sizeof(uuid_t) * count);
167 /* Get them all in a single batch */
169 if (uuidgen(store, count) != 0)
171 } else if (version == 4) {
172 if (uuidgen_v4(store, count) != 0)
173 err(1, "uuidgen_v4()");
175 err(1, "unsupported version");
179 for (i = 0; i < count; i++) {
181 if (uuidgen(uuid++, 1) != 0)
183 } else if (version == 4) {
184 if (uuidgen_v4(uuid++, 1) != 0)
185 err(1, "uuidgen_v4()");
187 err(1, "unsupported version");
194 tostring(uuid++, &p, &status);
195 if (status != uuid_s_ok)
196 err(1, "cannot stringify a UUID");
197 fprintf(fp, "%s\n", p);