[590] | 1 | /*
|
---|
| 2 | Unix SMB/CIFS implementation.
|
---|
| 3 |
|
---|
| 4 | UUID/GUID functions
|
---|
| 5 |
|
---|
| 6 | Copyright (C) Theodore Ts'o 1996, 1997,
|
---|
| 7 | Copyright (C) Jim McDonough 2002.
|
---|
| 8 | Copyright (C) Andrew Tridgell 2003.
|
---|
| 9 |
|
---|
| 10 | This program is free software; you can redistribute it and/or modify
|
---|
| 11 | it under the terms of the GNU General Public License as published by
|
---|
| 12 | the Free Software Foundation; either version 3 of the License, or
|
---|
| 13 | (at your option) any later version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful,
|
---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | GNU General Public License for more details.
|
---|
| 19 |
|
---|
| 20 | You should have received a copy of the GNU General Public License
|
---|
| 21 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | #include "includes.h"
|
---|
| 25 | #include "librpc/ndr/libndr.h"
|
---|
| 26 | #include "librpc/gen_ndr/ndr_misc.h"
|
---|
| 27 |
|
---|
[745] | 28 | /**
|
---|
| 29 | build a NDR blob from a GUID
|
---|
| 30 | */
|
---|
| 31 | _PUBLIC_ NTSTATUS GUID_to_ndr_blob(const struct GUID *guid, TALLOC_CTX *mem_ctx, DATA_BLOB *b)
|
---|
| 32 | {
|
---|
| 33 | enum ndr_err_code ndr_err;
|
---|
| 34 | ndr_err = ndr_push_struct_blob(b, mem_ctx, guid,
|
---|
| 35 | (ndr_push_flags_fn_t)ndr_push_GUID);
|
---|
| 36 | return ndr_map_error2ntstatus(ndr_err);
|
---|
| 37 | }
|
---|
[590] | 38 |
|
---|
[745] | 39 |
|
---|
[590] | 40 | /**
|
---|
| 41 | build a GUID from a NDR data blob
|
---|
| 42 | */
|
---|
| 43 | _PUBLIC_ NTSTATUS GUID_from_ndr_blob(const DATA_BLOB *b, struct GUID *guid)
|
---|
| 44 | {
|
---|
| 45 | enum ndr_err_code ndr_err;
|
---|
| 46 | TALLOC_CTX *mem_ctx;
|
---|
| 47 |
|
---|
| 48 | mem_ctx = talloc_new(NULL);
|
---|
| 49 | NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
|
---|
| 50 |
|
---|
[745] | 51 | ndr_err = ndr_pull_struct_blob_all(b, mem_ctx, guid,
|
---|
[590] | 52 | (ndr_pull_flags_fn_t)ndr_pull_GUID);
|
---|
| 53 | talloc_free(mem_ctx);
|
---|
| 54 | return ndr_map_error2ntstatus(ndr_err);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | build a GUID from a string
|
---|
| 60 | */
|
---|
| 61 | _PUBLIC_ NTSTATUS GUID_from_data_blob(const DATA_BLOB *s, struct GUID *guid)
|
---|
| 62 | {
|
---|
| 63 | NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
|
---|
| 64 | uint32_t time_low;
|
---|
| 65 | uint32_t time_mid, time_hi_and_version;
|
---|
| 66 | uint32_t clock_seq[2];
|
---|
| 67 | uint32_t node[6];
|
---|
| 68 | uint8_t buf16[16];
|
---|
| 69 |
|
---|
| 70 | DATA_BLOB blob16 = data_blob_const(buf16, sizeof(buf16));
|
---|
| 71 | int i;
|
---|
| 72 |
|
---|
| 73 | if (s->data == NULL) {
|
---|
| 74 | return NT_STATUS_INVALID_PARAMETER;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[745] | 77 | switch(s->length) {
|
---|
| 78 | case 36:
|
---|
| 79 | {
|
---|
[590] | 80 | TALLOC_CTX *mem_ctx;
|
---|
| 81 | const char *string;
|
---|
| 82 |
|
---|
| 83 | mem_ctx = talloc_new(NULL);
|
---|
| 84 | NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
|
---|
| 85 | string = talloc_strndup(mem_ctx, (const char *)s->data, s->length);
|
---|
| 86 | NT_STATUS_HAVE_NO_MEMORY(string);
|
---|
| 87 | if (11 == sscanf(string,
|
---|
| 88 | "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
---|
| 89 | &time_low, &time_mid, &time_hi_and_version,
|
---|
| 90 | &clock_seq[0], &clock_seq[1],
|
---|
| 91 | &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
|
---|
| 92 | status = NT_STATUS_OK;
|
---|
| 93 | }
|
---|
| 94 | talloc_free(mem_ctx);
|
---|
[745] | 95 | break;
|
---|
| 96 | }
|
---|
| 97 | case 38:
|
---|
| 98 | {
|
---|
[590] | 99 | TALLOC_CTX *mem_ctx;
|
---|
| 100 | const char *string;
|
---|
| 101 |
|
---|
| 102 | mem_ctx = talloc_new(NULL);
|
---|
| 103 | NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
|
---|
| 104 | string = talloc_strndup(mem_ctx, (const char *)s->data, s->length);
|
---|
| 105 | NT_STATUS_HAVE_NO_MEMORY(string);
|
---|
| 106 | if (11 == sscanf((const char *)s->data,
|
---|
| 107 | "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
|
---|
| 108 | &time_low, &time_mid, &time_hi_and_version,
|
---|
| 109 | &clock_seq[0], &clock_seq[1],
|
---|
| 110 | &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
|
---|
| 111 | status = NT_STATUS_OK;
|
---|
| 112 | }
|
---|
| 113 | talloc_free(mem_ctx);
|
---|
[745] | 114 | break;
|
---|
| 115 | }
|
---|
| 116 | case 32:
|
---|
| 117 | {
|
---|
[590] | 118 | size_t rlen = strhex_to_str((char *)blob16.data, blob16.length,
|
---|
| 119 | (const char *)s->data, s->length);
|
---|
| 120 | if (rlen != blob16.length) {
|
---|
| 121 | return NT_STATUS_INVALID_PARAMETER;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | s = &blob16;
|
---|
| 125 | return GUID_from_ndr_blob(s, guid);
|
---|
| 126 | }
|
---|
[745] | 127 | case 16:
|
---|
[590] | 128 | return GUID_from_ndr_blob(s, guid);
|
---|
[745] | 129 | default:
|
---|
| 130 | status = NT_STATUS_INVALID_PARAMETER;
|
---|
| 131 | break;
|
---|
[590] | 132 | }
|
---|
| 133 |
|
---|
| 134 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 135 | return status;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | guid->time_low = time_low;
|
---|
| 139 | guid->time_mid = time_mid;
|
---|
| 140 | guid->time_hi_and_version = time_hi_and_version;
|
---|
| 141 | guid->clock_seq[0] = clock_seq[0];
|
---|
| 142 | guid->clock_seq[1] = clock_seq[1];
|
---|
| 143 | for (i=0;i<6;i++) {
|
---|
| 144 | guid->node[i] = node[i];
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | return NT_STATUS_OK;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /**
|
---|
| 151 | build a GUID from a string
|
---|
| 152 | */
|
---|
| 153 | _PUBLIC_ NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
|
---|
| 154 | {
|
---|
| 155 | DATA_BLOB blob = data_blob_string_const(s);
|
---|
| 156 | return GUID_from_data_blob(&blob, guid);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | /**
|
---|
| 160 | build a GUID from a string
|
---|
| 161 | */
|
---|
| 162 | _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
|
---|
| 163 | {
|
---|
| 164 | NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
|
---|
| 165 | uint32_t time_low;
|
---|
| 166 | uint32_t time_mid, time_hi_and_version;
|
---|
| 167 | uint32_t clock_seq[2];
|
---|
| 168 | uint32_t node[6];
|
---|
| 169 | int i;
|
---|
| 170 |
|
---|
| 171 | if (s == NULL) {
|
---|
| 172 | return NT_STATUS_INVALID_PARAMETER;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | if (11 == sscanf(s, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
|
---|
| 176 | &time_low, &time_mid, &time_hi_and_version,
|
---|
| 177 | &clock_seq[0], &clock_seq[1],
|
---|
| 178 | &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
|
---|
| 179 | status = NT_STATUS_OK;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 183 | return status;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | guid->time_low = time_low;
|
---|
| 187 | guid->time_mid = time_mid;
|
---|
| 188 | guid->time_hi_and_version = time_hi_and_version;
|
---|
| 189 | guid->clock_seq[0] = clock_seq[0];
|
---|
| 190 | guid->clock_seq[1] = clock_seq[1];
|
---|
| 191 | for (i=0;i<6;i++) {
|
---|
| 192 | guid->node[i] = node[i];
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | return NT_STATUS_OK;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /**
|
---|
| 199 | * generate a random GUID
|
---|
| 200 | */
|
---|
| 201 | _PUBLIC_ struct GUID GUID_random(void)
|
---|
| 202 | {
|
---|
| 203 | struct GUID guid;
|
---|
| 204 |
|
---|
| 205 | generate_random_buffer((uint8_t *)&guid, sizeof(guid));
|
---|
| 206 | guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
|
---|
| 207 | guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
|
---|
| 208 |
|
---|
| 209 | return guid;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | /**
|
---|
| 213 | * generate an empty GUID
|
---|
| 214 | */
|
---|
| 215 | _PUBLIC_ struct GUID GUID_zero(void)
|
---|
| 216 | {
|
---|
| 217 | struct GUID guid;
|
---|
| 218 |
|
---|
| 219 | ZERO_STRUCT(guid);
|
---|
| 220 |
|
---|
| 221 | return guid;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | _PUBLIC_ bool GUID_all_zero(const struct GUID *u)
|
---|
| 225 | {
|
---|
| 226 | if (u->time_low != 0 ||
|
---|
| 227 | u->time_mid != 0 ||
|
---|
| 228 | u->time_hi_and_version != 0 ||
|
---|
| 229 | u->clock_seq[0] != 0 ||
|
---|
| 230 | u->clock_seq[1] != 0 ||
|
---|
| 231 | !all_zero(u->node, 6)) {
|
---|
| 232 | return false;
|
---|
| 233 | }
|
---|
| 234 | return true;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | _PUBLIC_ bool GUID_equal(const struct GUID *u1, const struct GUID *u2)
|
---|
| 238 | {
|
---|
| 239 | if (u1->time_low != u2->time_low ||
|
---|
| 240 | u1->time_mid != u2->time_mid ||
|
---|
| 241 | u1->time_hi_and_version != u2->time_hi_and_version ||
|
---|
| 242 | u1->clock_seq[0] != u2->clock_seq[0] ||
|
---|
| 243 | u1->clock_seq[1] != u2->clock_seq[1] ||
|
---|
| 244 | memcmp(u1->node, u2->node, 6) != 0) {
|
---|
| 245 | return false;
|
---|
| 246 | }
|
---|
| 247 | return true;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | _PUBLIC_ int GUID_compare(const struct GUID *u1, const struct GUID *u2)
|
---|
| 251 | {
|
---|
| 252 | if (u1->time_low != u2->time_low) {
|
---|
[745] | 253 | return u1->time_low > u2->time_low ? 1 : -1;
|
---|
[590] | 254 | }
|
---|
| 255 |
|
---|
| 256 | if (u1->time_mid != u2->time_mid) {
|
---|
[745] | 257 | return u1->time_mid > u2->time_mid ? 1 : -1;
|
---|
[590] | 258 | }
|
---|
| 259 |
|
---|
| 260 | if (u1->time_hi_and_version != u2->time_hi_and_version) {
|
---|
[745] | 261 | return u1->time_hi_and_version > u2->time_hi_and_version ? 1 : -1;
|
---|
[590] | 262 | }
|
---|
| 263 |
|
---|
| 264 | if (u1->clock_seq[0] != u2->clock_seq[0]) {
|
---|
[745] | 265 | return u1->clock_seq[0] > u2->clock_seq[0] ? 1 : -1;
|
---|
[590] | 266 | }
|
---|
| 267 |
|
---|
| 268 | if (u1->clock_seq[1] != u2->clock_seq[1]) {
|
---|
[745] | 269 | return u1->clock_seq[1] > u2->clock_seq[1] ? 1 : -1;
|
---|
[590] | 270 | }
|
---|
| 271 |
|
---|
| 272 | return memcmp(u1->node, u2->node, 6);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | /**
|
---|
| 276 | its useful to be able to display these in debugging messages
|
---|
| 277 | */
|
---|
| 278 | _PUBLIC_ char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
|
---|
| 279 | {
|
---|
| 280 | return talloc_asprintf(mem_ctx,
|
---|
| 281 | "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
---|
| 282 | guid->time_low, guid->time_mid,
|
---|
| 283 | guid->time_hi_and_version,
|
---|
| 284 | guid->clock_seq[0],
|
---|
| 285 | guid->clock_seq[1],
|
---|
| 286 | guid->node[0], guid->node[1],
|
---|
| 287 | guid->node[2], guid->node[3],
|
---|
| 288 | guid->node[4], guid->node[5]);
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | _PUBLIC_ char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
|
---|
| 292 | {
|
---|
| 293 | char *ret, *s = GUID_string(mem_ctx, guid);
|
---|
| 294 | ret = talloc_asprintf(mem_ctx, "{%s}", s);
|
---|
| 295 | talloc_free(s);
|
---|
| 296 | return ret;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | _PUBLIC_ char *GUID_hexstring(TALLOC_CTX *mem_ctx, const struct GUID *guid)
|
---|
| 300 | {
|
---|
| 301 | char *ret;
|
---|
| 302 | DATA_BLOB guid_blob;
|
---|
| 303 | TALLOC_CTX *tmp_mem;
|
---|
[745] | 304 | NTSTATUS status;
|
---|
[590] | 305 |
|
---|
| 306 | tmp_mem = talloc_new(mem_ctx);
|
---|
| 307 | if (!tmp_mem) {
|
---|
| 308 | return NULL;
|
---|
| 309 | }
|
---|
[745] | 310 | status = GUID_to_ndr_blob(guid, tmp_mem, &guid_blob);
|
---|
| 311 | if (!NT_STATUS_IS_OK(status)) {
|
---|
[590] | 312 | talloc_free(tmp_mem);
|
---|
| 313 | return NULL;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[745] | 316 | ret = data_blob_hex_string_upper(mem_ctx, &guid_blob);
|
---|
[590] | 317 | talloc_free(tmp_mem);
|
---|
| 318 | return ret;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
|
---|
| 322 | {
|
---|
| 323 | return talloc_asprintf(mem_ctx,
|
---|
| 324 | "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
|
---|
| 325 | guid->time_low, guid->time_mid,
|
---|
| 326 | guid->time_hi_and_version,
|
---|
| 327 | guid->clock_seq[0],
|
---|
| 328 | guid->clock_seq[1],
|
---|
| 329 | guid->node[0], guid->node[1],
|
---|
| 330 | guid->node[2], guid->node[3],
|
---|
| 331 | guid->node[4], guid->node[5]);
|
---|
| 332 | }
|
---|
| 333 |
|
---|
[745] | 334 | _PUBLIC_ bool policy_handle_empty(const struct policy_handle *h)
|
---|
[590] | 335 | {
|
---|
| 336 | return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
|
---|
| 337 | }
|
---|
[745] | 338 |
|
---|
| 339 | _PUBLIC_ bool is_valid_policy_hnd(const struct policy_handle *hnd)
|
---|
| 340 | {
|
---|
| 341 | return !policy_handle_empty(hnd);
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | _PUBLIC_ bool policy_handle_equal(const struct policy_handle *hnd1,
|
---|
| 345 | const struct policy_handle *hnd2)
|
---|
| 346 | {
|
---|
| 347 | if (!hnd1 || !hnd2) {
|
---|
| 348 | return false;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | return (memcmp(hnd1, hnd2, sizeof(*hnd1)) == 0);
|
---|
| 352 | }
|
---|