r4627: - simplified the dcerpc auth code using a common function
[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 (s == NULL) {
41                 return NT_STATUS_INVALID_PARAMETER;
42         }
43
44         if (11 == sscanf(s, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
45                          &time_low, &time_mid, &time_hi_and_version, 
46                          &clock_seq[0], &clock_seq[1],
47                          &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
48                 status = NT_STATUS_OK;
49         } else if (11 == sscanf(s, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
50                                 &time_low, &time_mid, &time_hi_and_version, 
51                                 &clock_seq[0], &clock_seq[1],
52                                 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
53                 status = NT_STATUS_OK;
54         }
55
56         if (!NT_STATUS_IS_OK(status)) {
57                 return status;
58         }
59
60         guid->time_low = time_low;
61         guid->time_mid = time_mid;
62         guid->time_hi_and_version = time_hi_and_version;
63         guid->clock_seq[0] = clock_seq[0];
64         guid->clock_seq[1] = clock_seq[1];
65         for (i=0;i<6;i++) {
66                 guid->node[i] = node[i];
67         }
68
69         return NT_STATUS_OK;
70 }
71
72 /* generate a random GUID */
73 struct GUID GUID_random(void)
74 {
75         struct GUID guid;
76
77         generate_random_buffer((uint8_t *)&guid, sizeof(guid));
78         guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
79         guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
80
81         return guid;
82 }
83
84 BOOL GUID_all_zero(const struct GUID *u)
85 {
86         if (u->time_low != 0 ||
87             u->time_mid != 0 ||
88             u->time_hi_and_version != 0 ||
89             u->clock_seq[0] != 0 ||
90             u->clock_seq[1] != 0 ||
91             !all_zero(u->node, 6)) {
92                 return False;
93         }
94         return True;
95 }
96
97 BOOL GUID_equal(const struct GUID *u1, const struct GUID *u2)
98 {
99         if (u1->time_low != u2->time_low ||
100             u1->time_mid != u2->time_mid ||
101             u1->time_hi_and_version != u2->time_hi_and_version ||
102             u1->clock_seq[0] != u2->clock_seq[0] ||
103             u1->clock_seq[1] != u2->clock_seq[1] ||
104             memcmp(u1->node, u2->node, 6) != 0) {
105                 return False;
106         }
107         return True;
108 }
109
110 /*
111   its useful to be able to display these in debugging messages
112 */
113 char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
114 {
115         return talloc_asprintf(mem_ctx, 
116                                "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
117                                guid->time_low, guid->time_mid,
118                                guid->time_hi_and_version,
119                                guid->clock_seq[0],
120                                guid->clock_seq[1],
121                                guid->node[0], guid->node[1],
122                                guid->node[2], guid->node[3],
123                                guid->node[4], guid->node[5]);
124 }
125
126 char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
127 {
128         return talloc_asprintf(mem_ctx, 
129                                "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
130                                guid->time_low, guid->time_mid,
131                                guid->time_hi_and_version,
132                                guid->clock_seq[0],
133                                guid->clock_seq[1],
134                                guid->node[0], guid->node[1],
135                                guid->node[2], guid->node[3],
136                                guid->node[4], guid->node[5]);
137 }
138
139 void ndr_print_GUID(struct ndr_print *ndr, const char *name, const struct GUID *guid)
140 {
141         ndr->print(ndr, "%-25s: %s", name, GUID_string(ndr, guid));
142 }
143
144 BOOL policy_handle_empty(struct policy_handle *h) 
145 {
146         return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
147 }