0b5954d7f98d6715eb52b54fe0871afeb96523e0
[samba.git] / source4 / librpc / ndr / ndr_misc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    UUID/GUID/policy_handle functions
5
6    Copyright (C) Theodore Ts'o               1996, 1997,
7    Copyright (C) Jim McDonough                     2002.
8    Copyright (C) Andrew Tridgell                   2003.
9    Copyright (C) Stefan (metze) Metzmacher         2004.
10    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include "includes.h"
27
28 /*
29   build a GUID from a string
30 */
31 NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
32 {
33         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
34         uint32_t time_low;
35         uint32_t time_mid, time_hi_and_version;
36         uint32_t clock_seq[2];
37         uint32_t node[6];
38         int i;
39
40         if (11 == sscanf(s, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
41                          &time_low, &time_mid, &time_hi_and_version, 
42                          &clock_seq[0], &clock_seq[1],
43                          &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
44                 status = NT_STATUS_OK;
45         } else if (11 == sscanf(s, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
46                                 &time_low, &time_mid, &time_hi_and_version, 
47                                 &clock_seq[0], &clock_seq[1],
48                                 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
49                 status = NT_STATUS_OK;
50         }
51
52         if (!NT_STATUS_IS_OK(status)) {
53                 return status;
54         }
55
56         guid->time_low = time_low;
57         guid->time_mid = time_mid;
58         guid->time_hi_and_version = time_hi_and_version;
59         guid->clock_seq[0] = clock_seq[0];
60         guid->clock_seq[1] = clock_seq[1];
61         for (i=0;i<6;i++) {
62                 guid->node[i] = node[i];
63         }
64
65         return NT_STATUS_OK;
66 }
67
68 /* generate a random GUID */
69 struct GUID GUID_random(void)
70 {
71         struct GUID guid;
72
73         generate_random_buffer((uint8_t *)&guid, sizeof(guid));
74         guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
75         guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
76
77         return guid;
78 }
79
80 BOOL GUID_all_zero(const struct GUID *u)
81 {
82         if (u->time_low != 0 ||
83             u->time_mid != 0 ||
84             u->time_hi_and_version != 0 ||
85             u->clock_seq[0] != 0 ||
86             u->clock_seq[1] != 0 ||
87             !all_zero(u->node, 6)) {
88                 return False;
89         }
90         return True;
91 }
92
93 BOOL GUID_equal(const struct GUID *u1, const struct GUID *u2)
94 {
95         if (u1->time_low != u2->time_low ||
96             u1->time_mid != u2->time_mid ||
97             u1->time_hi_and_version != u2->time_hi_and_version ||
98             u1->clock_seq[0] != u2->clock_seq[0] ||
99             u1->clock_seq[1] != u2->clock_seq[1] ||
100             memcmp(u1->node, u2->node, 6) != 0) {
101                 return False;
102         }
103         return True;
104 }
105
106 /*
107   its useful to be able to display these in debugging messages
108 */
109 char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
110 {
111         return talloc_asprintf(mem_ctx, 
112                                "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
113                                guid->time_low, guid->time_mid,
114                                guid->time_hi_and_version,
115                                guid->clock_seq[0],
116                                guid->clock_seq[1],
117                                guid->node[0], guid->node[1],
118                                guid->node[2], guid->node[3],
119                                guid->node[4], guid->node[5]);
120 }
121
122 char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
123 {
124         return talloc_asprintf(mem_ctx, 
125                                "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
126                                guid->time_low, guid->time_mid,
127                                guid->time_hi_and_version,
128                                guid->clock_seq[0],
129                                guid->clock_seq[1],
130                                guid->node[0], guid->node[1],
131                                guid->node[2], guid->node[3],
132                                guid->node[4], guid->node[5]);
133 }
134
135 void ndr_print_GUID(struct ndr_print *ndr, const char *name, const struct GUID *guid)
136 {
137         ndr->print(ndr, "%-25s: %s", name, GUID_string(ndr, guid));
138 }
139
140 BOOL policy_handle_empty(struct policy_handle *h) 
141 {
142         return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
143 }