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 |
|
---|
27 | /**
|
---|
28 | build a GUID from a string
|
---|
29 | */
|
---|
30 | _PUBLIC_ NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
|
---|
31 | {
|
---|
32 | NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
|
---|
33 | uint32_t time_low;
|
---|
34 | uint32_t time_mid, time_hi_and_version;
|
---|
35 | uint32_t clock_seq[2];
|
---|
36 | uint32_t node[6];
|
---|
37 | int i;
|
---|
38 |
|
---|
39 | if (s == NULL) {
|
---|
40 | return NT_STATUS_INVALID_PARAMETER;
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (11 == sscanf(s, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
---|
44 | &time_low, &time_mid, &time_hi_and_version,
|
---|
45 | &clock_seq[0], &clock_seq[1],
|
---|
46 | &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
|
---|
47 | status = NT_STATUS_OK;
|
---|
48 | } else if (11 == sscanf(s, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
|
---|
49 | &time_low, &time_mid, &time_hi_and_version,
|
---|
50 | &clock_seq[0], &clock_seq[1],
|
---|
51 | &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
|
---|
52 | status = NT_STATUS_OK;
|
---|
53 | }
|
---|
54 |
|
---|
55 | if (!NT_STATUS_IS_OK(status)) {
|
---|
56 | return status;
|
---|
57 | }
|
---|
58 |
|
---|
59 | guid->time_low = time_low;
|
---|
60 | guid->time_mid = time_mid;
|
---|
61 | guid->time_hi_and_version = time_hi_and_version;
|
---|
62 | guid->clock_seq[0] = clock_seq[0];
|
---|
63 | guid->clock_seq[1] = clock_seq[1];
|
---|
64 | for (i=0;i<6;i++) {
|
---|
65 | guid->node[i] = node[i];
|
---|
66 | }
|
---|
67 |
|
---|
68 | return NT_STATUS_OK;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | build a GUID from a string
|
---|
73 | */
|
---|
74 | _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
|
---|
75 | {
|
---|
76 | NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
|
---|
77 | uint32_t time_low;
|
---|
78 | uint32_t time_mid, time_hi_and_version;
|
---|
79 | uint32_t clock_seq[2];
|
---|
80 | uint32_t node[6];
|
---|
81 | int i;
|
---|
82 |
|
---|
83 | if (s == NULL) {
|
---|
84 | return NT_STATUS_INVALID_PARAMETER;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (11 == sscanf(s, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
|
---|
88 | &time_low, &time_mid, &time_hi_and_version,
|
---|
89 | &clock_seq[0], &clock_seq[1],
|
---|
90 | &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
|
---|
91 | status = NT_STATUS_OK;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (!NT_STATUS_IS_OK(status)) {
|
---|
95 | return status;
|
---|
96 | }
|
---|
97 |
|
---|
98 | guid->time_low = time_low;
|
---|
99 | guid->time_mid = time_mid;
|
---|
100 | guid->time_hi_and_version = time_hi_and_version;
|
---|
101 | guid->clock_seq[0] = clock_seq[0];
|
---|
102 | guid->clock_seq[1] = clock_seq[1];
|
---|
103 | for (i=0;i<6;i++) {
|
---|
104 | guid->node[i] = node[i];
|
---|
105 | }
|
---|
106 |
|
---|
107 | return NT_STATUS_OK;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * generate a random GUID
|
---|
112 | */
|
---|
113 | struct GUID GUID_random(void)
|
---|
114 | {
|
---|
115 | struct GUID guid;
|
---|
116 |
|
---|
117 | generate_random_buffer((uint8_t *)&guid, sizeof(guid));
|
---|
118 | guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
|
---|
119 | guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
|
---|
120 |
|
---|
121 | return guid;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * generate an empty GUID
|
---|
126 | */
|
---|
127 | _PUBLIC_ struct GUID GUID_zero(void)
|
---|
128 | {
|
---|
129 | struct GUID guid;
|
---|
130 |
|
---|
131 | ZERO_STRUCT(guid);
|
---|
132 |
|
---|
133 | return guid;
|
---|
134 | }
|
---|
135 |
|
---|
136 | _PUBLIC_ bool GUID_all_zero(const struct GUID *u)
|
---|
137 | {
|
---|
138 | if (u->time_low != 0 ||
|
---|
139 | u->time_mid != 0 ||
|
---|
140 | u->time_hi_and_version != 0 ||
|
---|
141 | u->clock_seq[0] != 0 ||
|
---|
142 | u->clock_seq[1] != 0 ||
|
---|
143 | !all_zero(u->node, 6)) {
|
---|
144 | return false;
|
---|
145 | }
|
---|
146 | return true;
|
---|
147 | }
|
---|
148 |
|
---|
149 | _PUBLIC_ bool GUID_equal(const struct GUID *u1, const struct GUID *u2)
|
---|
150 | {
|
---|
151 | if (u1->time_low != u2->time_low ||
|
---|
152 | u1->time_mid != u2->time_mid ||
|
---|
153 | u1->time_hi_and_version != u2->time_hi_and_version ||
|
---|
154 | u1->clock_seq[0] != u2->clock_seq[0] ||
|
---|
155 | u1->clock_seq[1] != u2->clock_seq[1] ||
|
---|
156 | memcmp(u1->node, u2->node, 6) != 0) {
|
---|
157 | return false;
|
---|
158 | }
|
---|
159 | return true;
|
---|
160 | }
|
---|
161 |
|
---|
162 | _PUBLIC_ int GUID_compare(const struct GUID *u1, const struct GUID *u2)
|
---|
163 | {
|
---|
164 | if (u1->time_low != u2->time_low) {
|
---|
165 | return u1->time_low - u2->time_low;
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (u1->time_mid != u2->time_mid) {
|
---|
169 | return u1->time_mid - u2->time_mid;
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (u1->time_hi_and_version != u2->time_hi_and_version) {
|
---|
173 | return u1->time_hi_and_version - u2->time_hi_and_version;
|
---|
174 | }
|
---|
175 |
|
---|
176 | if (u1->clock_seq[0] != u2->clock_seq[0]) {
|
---|
177 | return u1->clock_seq[0] - u2->clock_seq[0];
|
---|
178 | }
|
---|
179 |
|
---|
180 | if (u1->clock_seq[1] != u2->clock_seq[1]) {
|
---|
181 | return u1->clock_seq[1] - u2->clock_seq[1];
|
---|
182 | }
|
---|
183 |
|
---|
184 | return memcmp(u1->node, u2->node, 6);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | its useful to be able to display these in debugging messages
|
---|
189 | */
|
---|
190 | _PUBLIC_ char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
|
---|
191 | {
|
---|
192 | return talloc_asprintf(mem_ctx,
|
---|
193 | "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
---|
194 | guid->time_low, guid->time_mid,
|
---|
195 | guid->time_hi_and_version,
|
---|
196 | guid->clock_seq[0],
|
---|
197 | guid->clock_seq[1],
|
---|
198 | guid->node[0], guid->node[1],
|
---|
199 | guid->node[2], guid->node[3],
|
---|
200 | guid->node[4], guid->node[5]);
|
---|
201 | }
|
---|
202 |
|
---|
203 | _PUBLIC_ char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
|
---|
204 | {
|
---|
205 | char *ret, *s = GUID_string(mem_ctx, guid);
|
---|
206 | ret = talloc_asprintf(mem_ctx, "{%s}", s);
|
---|
207 | talloc_free(s);
|
---|
208 | return ret;
|
---|
209 | }
|
---|
210 |
|
---|
211 | _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
|
---|
212 | {
|
---|
213 | return talloc_asprintf(mem_ctx,
|
---|
214 | "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
|
---|
215 | guid->time_low, guid->time_mid,
|
---|
216 | guid->time_hi_and_version,
|
---|
217 | guid->clock_seq[0],
|
---|
218 | guid->clock_seq[1],
|
---|
219 | guid->node[0], guid->node[1],
|
---|
220 | guid->node[2], guid->node[3],
|
---|
221 | guid->node[4], guid->node[5]);
|
---|
222 | }
|
---|
223 |
|
---|
224 | _PUBLIC_ bool policy_handle_empty(struct policy_handle *h)
|
---|
225 | {
|
---|
226 | return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
|
---|
227 | }
|
---|