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