| 1 | /*
|
|---|
| 2 | * Unix SMB/CIFS implementation.
|
|---|
| 3 | * UUID server routines
|
|---|
| 4 | * Copyright (C) Theodore Ts'o 1996, 1997,
|
|---|
| 5 | * Copyright (C) Jim McDonough <[email protected]> 2002, 2003
|
|---|
| 6 | *
|
|---|
| 7 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 8 | * it under the terms of the GNU General Public License as published by
|
|---|
| 9 | * the Free Software Foundation; either version 3 of the License, or
|
|---|
| 10 | * (at your option) any later version.
|
|---|
| 11 | *
|
|---|
| 12 | * This program is distributed in the hope that it will be useful,
|
|---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 15 | * GNU General Public License for more details.
|
|---|
| 16 | *
|
|---|
| 17 | * You should have received a copy of the GNU General Public License
|
|---|
| 18 | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #include "includes.h"
|
|---|
| 22 |
|
|---|
| 23 | /*
|
|---|
| 24 | * Offset between 15-Oct-1582 and 1-Jan-70
|
|---|
| 25 | */
|
|---|
| 26 | #define TIME_OFFSET_HIGH 0x01B21DD2
|
|---|
| 27 | #define TIME_OFFSET_LOW 0x13814000
|
|---|
| 28 |
|
|---|
| 29 | void smb_uuid_pack(const struct GUID uu, UUID_FLAT *ptr)
|
|---|
| 30 | {
|
|---|
| 31 | SIVAL(ptr->info, 0, uu.time_low);
|
|---|
| 32 | SSVAL(ptr->info, 4, uu.time_mid);
|
|---|
| 33 | SSVAL(ptr->info, 6, uu.time_hi_and_version);
|
|---|
| 34 | memcpy(ptr->info+8, uu.clock_seq, 2);
|
|---|
| 35 | memcpy(ptr->info+10, uu.node, 6);
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | void smb_uuid_unpack(const UUID_FLAT in, struct GUID *uu)
|
|---|
| 39 | {
|
|---|
| 40 | uu->time_low = IVAL(in.info, 0);
|
|---|
| 41 | uu->time_mid = SVAL(in.info, 4);
|
|---|
| 42 | uu->time_hi_and_version = SVAL(in.info, 6);
|
|---|
| 43 | memcpy(uu->clock_seq, in.info+8, 2);
|
|---|
| 44 | memcpy(uu->node, in.info+10, 6);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | void smb_uuid_generate_random(struct GUID *uu)
|
|---|
| 48 | {
|
|---|
| 49 | UUID_FLAT tmp;
|
|---|
| 50 |
|
|---|
| 51 | generate_random_buffer(tmp.info, sizeof(tmp.info));
|
|---|
| 52 | smb_uuid_unpack(tmp, uu);
|
|---|
| 53 |
|
|---|
| 54 | uu->clock_seq[0] = (uu->clock_seq[0] & 0x3F) | 0x80;
|
|---|
| 55 | uu->time_hi_and_version = (uu->time_hi_and_version & 0x0FFF) | 0x4000;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | const char *smb_uuid_string(TALLOC_CTX *mem_ctx, const struct GUID uu)
|
|---|
| 59 | {
|
|---|
| 60 | char *result;
|
|---|
| 61 |
|
|---|
| 62 | result = talloc_asprintf(
|
|---|
| 63 | mem_ctx,
|
|---|
| 64 | "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
|---|
| 65 | uu.time_low, uu.time_mid, uu.time_hi_and_version,
|
|---|
| 66 | uu.clock_seq[0], uu.clock_seq[1],
|
|---|
| 67 | uu.node[0], uu.node[1], uu.node[2],
|
|---|
| 68 | uu.node[3], uu.node[4], uu.node[5]);
|
|---|
| 69 |
|
|---|
| 70 | SMB_ASSERT(result != NULL);
|
|---|
| 71 | return result;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | bool smb_string_to_uuid(const char *in, struct GUID* uu)
|
|---|
| 75 | {
|
|---|
| 76 | bool ret = False;
|
|---|
| 77 | const char *ptr = in;
|
|---|
| 78 | char *end = (char *)in;
|
|---|
| 79 | int i;
|
|---|
| 80 | unsigned v1, v2;
|
|---|
| 81 |
|
|---|
| 82 | if (!in || !uu) goto out;
|
|---|
| 83 |
|
|---|
| 84 | uu->time_low = strtoul(ptr, &end, 16);
|
|---|
| 85 | if ((end - ptr) != 8 || *end != '-') goto out;
|
|---|
| 86 | ptr = (end + 1);
|
|---|
| 87 |
|
|---|
| 88 | uu->time_mid = strtoul(ptr, &end, 16);
|
|---|
| 89 | if ((end - ptr) != 4 || *end != '-') goto out;
|
|---|
| 90 | ptr = (end + 1);
|
|---|
| 91 |
|
|---|
| 92 | uu->time_hi_and_version = strtoul(ptr, &end, 16);
|
|---|
| 93 | if ((end - ptr) != 4 || *end != '-') goto out;
|
|---|
| 94 | ptr = (end + 1);
|
|---|
| 95 |
|
|---|
| 96 | if (sscanf(ptr, "%02x%02x", &v1, &v2) != 2) {
|
|---|
| 97 | goto out;
|
|---|
| 98 | }
|
|---|
| 99 | uu->clock_seq[0] = v1;
|
|---|
| 100 | uu->clock_seq[1] = v2;
|
|---|
| 101 | ptr += 4;
|
|---|
| 102 |
|
|---|
| 103 | if (*ptr != '-') goto out;
|
|---|
| 104 | ptr++;
|
|---|
| 105 |
|
|---|
| 106 | for (i = 0; i < 6; i++) {
|
|---|
| 107 | if (sscanf(ptr, "%02x", &v1) != 1) {
|
|---|
| 108 | goto out;
|
|---|
| 109 | }
|
|---|
| 110 | uu->node[i] = v1;
|
|---|
| 111 | ptr += 2;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | ret = True;
|
|---|
| 115 | out:
|
|---|
| 116 | return ret;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /*****************************************************************
|
|---|
| 120 | Return the binary string representation of a GUID.
|
|---|
| 121 | Caller must free.
|
|---|
| 122 | *****************************************************************/
|
|---|
| 123 |
|
|---|
| 124 | char *guid_binstring(const struct GUID *guid)
|
|---|
| 125 | {
|
|---|
| 126 | UUID_FLAT guid_flat;
|
|---|
| 127 |
|
|---|
| 128 | smb_uuid_pack(*guid, &guid_flat);
|
|---|
| 129 |
|
|---|
| 130 | return binary_string_rfc2254((char *)guid_flat.info, UUID_FLAT_SIZE);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|