xref: /freebsd/bin/uuidgen/uuidgen.c (revision f374ba41)
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/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/capsicum.h>
33 
34 #include <capsicum_helpers.h>
35 #include <err.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <uuid.h>
40 
41 static void
42 usage(void)
43 {
44 	(void)fprintf(stderr,
45 	    "usage: uuidgen [-1] [-r] [-n count] [-o filename]\n");
46 	exit(1);
47 }
48 
49 static int
50 uuidgen_v4(struct uuid *store, int count)
51 {
52 	int size;
53 	struct uuid *item;
54 
55 	if (count < 1) {
56 		errno = EINVAL;
57 		return (-1);
58 	}
59 	size = sizeof(struct uuid) * count;
60 	arc4random_buf(store, size);
61 	item = store;
62 	for (int i = 0; i < count; ++i) {
63 		/*
64 		 * Set the two most significant bits (bits 6 and 7) of the
65 		 * clock_seq_hi_and_reserved to zero and one, respectively.
66 		 */
67 		item->clock_seq_hi_and_reserved &= ~(3 << 6);
68 		item->clock_seq_hi_and_reserved |= (2 << 6);
69 		/*
70 		 * Set the four most significant bits (bits 12 through 15) of
71 		 * the time_hi_and_version field to the 4-bit version number
72 		 * from  Section 4.1.3.
73 		 */
74 		item->time_hi_and_version &= ~(15 << 12);
75 		item->time_hi_and_version |= (4 << 12);
76 		item++;
77 	};
78 	return (0);
79 }
80 
81 int
82 main(int argc, char *argv[])
83 {
84 	FILE *fp;
85 	uuid_t *store, *uuid;
86 	char *p;
87 	int ch, count, i, iterate, status, version;
88 
89 	count = -1;  /* no count yet */
90 	fp = stdout; /* default output file */
91 	iterate = 0; /* not one at a time */
92 	version = 1; /* create uuid v1 by default */
93 	while ((ch = getopt(argc, argv, "1rn:o:")) != -1)
94 		switch (ch) {
95 		case '1':
96 			iterate = 1;
97 			break;
98 		case 'r':
99 			version = 4;
100 			break;
101 		case 'n':
102 			if (count > 0)
103 				usage();
104 			count = strtol(optarg, &p, 10);
105 			if (*p != 0 || count < 1)
106 				usage();
107 			break;
108 		case 'o':
109 			if (fp != stdout)
110 				errx(1, "multiple output files not allowed");
111 			fp = fopen(optarg, "w");
112 			if (fp == NULL)
113 				err(1, "fopen");
114 			break;
115 		default:
116 			usage();
117 		}
118 	argv += optind;
119 	argc -= optind;
120 
121 	if (argc)
122 		usage();
123 
124 	caph_cache_catpages();
125 	if (caph_limit_stdio() < 0)
126 		err(1, "Unable to limit stdio");
127 	if (caph_enter() < 0)
128 		err(1, "Unable to enter capability mode");
129 
130 	if (count == -1)
131 		count = 1;
132 
133 	store = (uuid_t *)malloc(sizeof(uuid_t) * count);
134 	if (store == NULL)
135 		err(1, "malloc()");
136 
137 	if (!iterate) {
138 		/* Get them all in a single batch */
139 		if (version == 1) {
140 			if (uuidgen(store, count) != 0)
141 				err(1, "uuidgen()");
142 		} else if (version == 4) {
143 			if (uuidgen_v4(store, count) != 0)
144 				err(1, "uuidgen_v4()");
145 		} else {
146 			err(1, "unsupported version");
147 		}
148 	} else {
149 		uuid = store;
150 		for (i = 0; i < count; i++) {
151 			if (version == 1) {
152 				if (uuidgen(uuid++, 1) != 0)
153 					err(1, "uuidgen()");
154 			} else if (version == 4) {
155 				if (uuidgen_v4(uuid++, 1) != 0)
156 					err(1, "uuidgen_v4()");
157 			} else {
158 				err(1, "unsupported version");
159 			}
160 		}
161 	}
162 
163 	uuid = store;
164 	while (count--) {
165 		uuid_to_string(uuid++, &p, &status);
166 		if (status != uuid_s_ok)
167 			err(1, "cannot stringify a UUID");
168 		fprintf(fp, "%s\n", p);
169 		free(p);
170 	}
171 
172 	free(store);
173 	if (fp != stdout)
174 		fclose(fp);
175 	return (0);
176 }
177