2 Unix SMB/Netbios implementation.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern int DEBUGLEVEL;
28 /*****************************************************************
29 Convert a SID to an ascii string.
30 *****************************************************************/
32 char *sid_to_string(pstring sidstr_out, const DOM_SID *sid)
36 /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
37 uint32 ia = (sid->id_auth[5]) +
38 (sid->id_auth[4] << 8 ) +
39 (sid->id_auth[3] << 16) +
40 (sid->id_auth[2] << 24);
42 slprintf(sidstr_out, sizeof(pstring) - 1, "S-%u-%lu", (unsigned int)sid->sid_rev_num, (unsigned long)ia);
44 for (i = 0; i < sid->num_auths; i++)
46 slprintf(subauth, sizeof(subauth)-1, "-%lu", (unsigned long)sid->sub_auths[i]);
47 pstrcat(sidstr_out, subauth);
50 DEBUG(7,("sid_to_string returning %s\n", sidstr_out));
54 /*****************************************************************
55 Convert a string to a SID. Returns True on success, False on fail.
56 *****************************************************************/
58 BOOL string_to_sid(DOM_SID *sidout, const char *sidstr)
60 const char *p = sidstr;
61 /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
64 memset((char *)sidout, '\0', sizeof(DOM_SID));
66 if (StrnCaseCmp( sidstr, "S-", 2))
68 DEBUG(0,("string_to_sid: Sid %s does not start with 'S-'.\n", sidstr));
72 if ((p = strchr(p, '-')) == NULL)
74 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
80 /* Get the revision number. */
81 sidout->sid_rev_num = (uint8)strtoul(p,NULL,10);
83 if ((p = strchr(p, '-')) == NULL)
85 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
91 /* identauth in decimal should be < 2^32 */
92 ia = (uint32)strtoul(p,NULL,10);
94 /* NOTE - the ia value is in big-endian format. */
95 sidout->id_auth[0] = 0;
96 sidout->id_auth[1] = 0;
97 sidout->id_auth[2] = (ia & 0xff000000) >> 24;
98 sidout->id_auth[3] = (ia & 0x00ff0000) >> 16;
99 sidout->id_auth[4] = (ia & 0x0000ff00) >> 8;
100 sidout->id_auth[5] = (ia & 0x000000ff);
102 sidout->num_auths = 0;
104 while (((p = strchr(p, '-')) != NULL) && sidout->num_auths < MAXSUBAUTHS)
108 * NOTE - the subauths are in native machine-endian format. They
109 * are converted to little-endian when linearized onto the wire.
111 sid_append_rid(sidout, (uint32)strtoul(p, NULL, 10));
117 /*****************************************************************
118 add a rid to the end of a sid
119 *****************************************************************/
120 BOOL sid_append_rid(DOM_SID *sid, uint32 rid)
122 if (sid->num_auths < MAXSUBAUTHS)
124 sid->sub_auths[sid->num_auths++] = rid;
130 /*****************************************************************
131 removes the last rid from the end of a sid
132 *****************************************************************/
133 BOOL sid_split_rid(DOM_SID *sid, uint32 *rid)
135 if (sid->num_auths > 0)
140 (*rid) = sid->sub_auths[sid->num_auths];
147 /*****************************************************************
149 *****************************************************************/
150 void sid_copy(DOM_SID *sid1, const DOM_SID *sid2)
154 for (i = 0; i < 6; i++)
156 sid1->id_auth[i] = sid2->id_auth[i];
159 for (i = 0; i < sid2->num_auths; i++)
161 sid1->sub_auths[i] = sid2->sub_auths[i];
164 sid1->num_auths = sid2->num_auths;
165 sid1->sid_rev_num = sid2->sid_rev_num;
168 /*****************************************************************
169 compare two sids up to the auths of the first sid
170 *****************************************************************/
171 BOOL sid_front_equal(const DOM_SID *sid1, const DOM_SID *sid2)
175 /* compare most likely different rids, first: i.e start at end */
176 for (i = sid1->num_auths-1; i >= 0; --i)
178 if (sid1->sub_auths[i] != sid2->sub_auths[i]) return False;
181 if (sid1->num_auths > sid2->num_auths ) return False;
182 if (sid1->sid_rev_num != sid2->sid_rev_num) return False;
184 for (i = 0; i < 6; i++)
186 if (sid1->id_auth[i] != sid2->id_auth[i]) return False;
192 /*****************************************************************
194 *****************************************************************/
195 BOOL sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
199 /* compare most likely different rids, first: i.e start at end */
200 for (i = sid1->num_auths-1; i >= 0; --i)
202 if (sid1->sub_auths[i] != sid2->sub_auths[i]) return False;
205 if (sid1->num_auths != sid2->num_auths ) return False;
206 if (sid1->sid_rev_num != sid2->sid_rev_num) return False;
208 for (i = 0; i < 6; i++)
210 if (sid1->id_auth[i] != sid2->id_auth[i]) return False;
217 /*****************************************************************
218 calculates size of a sid
219 *****************************************************************/
220 int sid_size(const DOM_SID *sid)
226 return sid->num_auths * sizeof(uint32) + 8;