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 void 50 uuid_to_compact_string(const uuid_t *u, char **s, uint32_t *status) 51 { 52 uuid_t nil; 53 54 if (status != NULL) 55 *status = uuid_s_ok; 56 57 if (s == NULL) 58 return; 59 60 if (u == NULL) { 61 u = &nil; 62 uuid_create_nil(&nil, NULL); 63 } 64 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]); 69 70 if (*s == NULL && status != NULL) 71 *status = uuid_s_no_memory; 72 } 73 74 static int 75 uuidgen_v4(struct uuid *store, int count) 76 { 77 int size; 78 struct uuid *item; 79 80 if (count < 1) { 81 errno = EINVAL; 82 return (-1); 83 } 84 size = sizeof(struct uuid) * count; 85 arc4random_buf(store, size); 86 item = store; 87 for (int i = 0; i < count; ++i) { 88 /* 89 * Set the two most significant bits (bits 6 and 7) of the 90 * clock_seq_hi_and_reserved to zero and one, respectively. 91 */ 92 item->clock_seq_hi_and_reserved &= ~(3 << 6); 93 item->clock_seq_hi_and_reserved |= (2 << 6); 94 /* 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 97 * from Section 4.1.3. 98 */ 99 item->time_hi_and_version &= ~(15 << 12); 100 item->time_hi_and_version |= (4 << 12); 101 item++; 102 }; 103 return (0); 104 } 105 106 int 107 main(int argc, char *argv[]) 108 { 109 FILE *fp; 110 uuid_t *store, *uuid; 111 char *p; 112 int ch, count, i, iterate, status, version; 113 void (*tostring)(const uuid_t *, char **, uint32_t *) = uuid_to_string; 114 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) 120 switch (ch) { 121 case '1': 122 iterate = 1; 123 break; 124 case 'c': 125 tostring = uuid_to_compact_string; 126 break; 127 case 'r': 128 version = 4; 129 break; 130 case 'n': 131 if (count > 0) 132 usage(); 133 count = strtol(optarg, &p, 10); 134 if (*p != 0 || count < 1) 135 usage(); 136 break; 137 case 'o': 138 if (fp != stdout) 139 errx(1, "multiple output files not allowed"); 140 fp = fopen(optarg, "w"); 141 if (fp == NULL) 142 err(1, "fopen"); 143 break; 144 default: 145 usage(); 146 } 147 argv += optind; 148 argc -= optind; 149 150 if (argc) 151 usage(); 152 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"); 158 159 if (count == -1) 160 count = 1; 161 162 store = (uuid_t *)malloc(sizeof(uuid_t) * count); 163 if (store == NULL) 164 err(1, "malloc()"); 165 166 if (!iterate) { 167 /* Get them all in a single batch */ 168 if (version == 1) { 169 if (uuidgen(store, count) != 0) 170 err(1, "uuidgen()"); 171 } else if (version == 4) { 172 if (uuidgen_v4(store, count) != 0) 173 err(1, "uuidgen_v4()"); 174 } else { 175 err(1, "unsupported version"); 176 } 177 } else { 178 uuid = store; 179 for (i = 0; i < count; i++) { 180 if (version == 1) { 181 if (uuidgen(uuid++, 1) != 0) 182 err(1, "uuidgen()"); 183 } else if (version == 4) { 184 if (uuidgen_v4(uuid++, 1) != 0) 185 err(1, "uuidgen_v4()"); 186 } else { 187 err(1, "unsupported version"); 188 } 189 } 190 } 191 192 uuid = store; 193 while (count--) { 194 tostring(uuid++, &p, &status); 195 if (status != uuid_s_ok) 196 err(1, "cannot stringify a UUID"); 197 fprintf(fp, "%s\n", p); 198 free(p); 199 } 200 201 free(store); 202 if (fp != stdout) 203 fclose(fp); 204 return (0); 205 } 206