1/*
2 * Copyright (c) 2002 Marcel Moolenaar
3 * Copyright (c) 2022 Tobias C. Berner
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
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.
15 *
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.
26 *
27 */
28
29#include <sys/capsicum.h>
30
31#include <capsicum_helpers.h>
32#include <err.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <unistd.h>
36#include <uuid.h>
37
38static void
39usage(void)
40{
41	(void)fprintf(stderr,
42	    "usage: uuidgen [-1] [-r] [-n count] [-o filename]\n");
43	exit(1);
44}
45
46static void
47uuid_to_compact_string(const uuid_t *u, char **s, uint32_t *status)
48{
49	uuid_t nil;
50
51	if (status != NULL)
52		*status = uuid_s_ok;
53
54	if (s == NULL)
55		return;
56
57	if (u == NULL) {
58		u = &nil;
59		uuid_create_nil(&nil, NULL);
60	}
61
62	asprintf(s, "%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x",
63	    u->time_low, u->time_mid, u->time_hi_and_version,
64	    u->clock_seq_hi_and_reserved, u->clock_seq_low, u->node[0],
65	    u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]);
66
67	if (*s == NULL && status != NULL)
68		*status = uuid_s_no_memory;
69}
70
71static int
72uuidgen_v4(struct uuid *store, int count)
73{
74	int size;
75	struct uuid *item;
76
77	if (count < 1) {
78		errno = EINVAL;
79		return (-1);
80	}
81	size = sizeof(struct uuid) * count;
82	arc4random_buf(store, size);
83	item = store;
84	for (int i = 0; i < count; ++i) {
85		/*
86		 * Set the two most significant bits (bits 6 and 7) of the
87		 * clock_seq_hi_and_reserved to zero and one, respectively.
88		 */
89		item->clock_seq_hi_and_reserved &= ~(3 << 6);
90		item->clock_seq_hi_and_reserved |= (2 << 6);
91		/*
92		 * Set the four most significant bits (bits 12 through 15) of
93		 * the time_hi_and_version field to the 4-bit version number
94		 * from  Section 4.1.3.
95		 */
96		item->time_hi_and_version &= ~(15 << 12);
97		item->time_hi_and_version |= (4 << 12);
98		item++;
99	};
100	return (0);
101}
102
103int
104main(int argc, char *argv[])
105{
106	FILE *fp;
107	uuid_t *store, *uuid;
108	char *p;
109	int ch, count, i, iterate, status, version;
110	void (*tostring)(const uuid_t *, char **, uint32_t *) = uuid_to_string;
111
112	count = -1;  /* no count yet */
113	fp = stdout; /* default output file */
114	iterate = 0; /* not one at a time */
115	version = 1; /* create uuid v1 by default */
116	while ((ch = getopt(argc, argv, "1crn:o:")) != -1)
117		switch (ch) {
118		case '1':
119			iterate = 1;
120			break;
121		case 'c':
122			tostring = uuid_to_compact_string;
123			break;
124		case 'r':
125			version = 4;
126			break;
127		case 'n':
128			if (count > 0)
129				usage();
130			count = strtol(optarg, &p, 10);
131			if (*p != 0 || count < 1)
132				usage();
133			break;
134		case 'o':
135			if (fp != stdout)
136				errx(1, "multiple output files not allowed");
137			fp = fopen(optarg, "w");
138			if (fp == NULL)
139				err(1, "fopen");
140			break;
141		default:
142			usage();
143		}
144	argv += optind;
145	argc -= optind;
146
147	if (argc)
148		usage();
149
150	caph_cache_catpages();
151	if (caph_limit_stdio() < 0)
152		err(1, "Unable to limit stdio");
153	if (caph_enter() < 0)
154		err(1, "Unable to enter capability mode");
155
156	if (count == -1)
157		count = 1;
158
159	store = (uuid_t *)malloc(sizeof(uuid_t) * count);
160	if (store == NULL)
161		err(1, "malloc()");
162
163	if (!iterate) {
164		/* Get them all in a single batch */
165		if (version == 1) {
166			if (uuidgen(store, count) != 0)
167				err(1, "uuidgen()");
168		} else if (version == 4) {
169			if (uuidgen_v4(store, count) != 0)
170				err(1, "uuidgen_v4()");
171		} else {
172			err(1, "unsupported version");
173		}
174	} else {
175		uuid = store;
176		for (i = 0; i < count; i++) {
177			if (version == 1) {
178				if (uuidgen(uuid++, 1) != 0)
179					err(1, "uuidgen()");
180			} else if (version == 4) {
181				if (uuidgen_v4(uuid++, 1) != 0)
182					err(1, "uuidgen_v4()");
183			} else {
184				err(1, "unsupported version");
185			}
186		}
187	}
188
189	uuid = store;
190	while (count--) {
191		tostring(uuid++, &p, &status);
192		if (status != uuid_s_ok)
193			err(1, "cannot stringify a UUID");
194		fprintf(fp, "%s\n", p);
195		free(p);
196	}
197
198	free(store);
199	if (fp != stdout)
200		fclose(fp);
201	return (0);
202}
203