a483f85b845d9387e693f66f3b7538b5044f2413
[samba.git] / source3 / lib / util_sid.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Samba utility functions
5    Copyright (C) Andrew Tridgell 1992-1998
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24
25 extern int DEBUGLEVEL;
26
27
28 /*****************************************************************
29  Convert a SID to an ascii string.
30 *****************************************************************/
31
32 char *sid_to_string(pstring sidstr_out, DOM_SID *sid)
33 {
34   char subauth[16];
35   int i;
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);
41
42   slprintf(sidstr_out, sizeof(pstring) - 1, "S-%d-%d", sid->sid_rev_num, ia);
43
44   for (i = 0; i < sid->num_auths; i++)
45   {
46     slprintf(subauth, sizeof(subauth)-1, "-%u", sid->sub_auths[i]);
47     pstrcat(sidstr_out, subauth);
48   }
49
50   DEBUG(7,("sid_to_string returning %s\n", sidstr_out));
51   return sidstr_out;
52 }
53
54 /*****************************************************************
55  Convert a string to a SID. Returns True on success, False on fail.
56 *****************************************************************/  
57    
58 BOOL string_to_sid(DOM_SID *sidout, char *sidstr)
59 {
60   pstring tok;
61   char *p = sidstr;
62   /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
63   uint32 ia;
64
65   memset((char *)sidout, '\0', sizeof(DOM_SID));
66
67   if (StrnCaseCmp( sidstr, "S-", 2)) {
68     DEBUG(0,("string_to_sid: Sid %s does not start with 'S-'.\n", sidstr));
69     return False;
70   }
71
72   p += 2;
73   if (!next_token(&p, tok, "-", sizeof(tok))) {
74     DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
75     return False;
76   }
77
78   /* Get the revision number. */
79   sidout->sid_rev_num = atoi(tok);
80
81   if (!next_token(&p, tok, "-", sizeof(tok))) {
82     DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
83     return False;
84   }
85
86   /* identauth in decimal should be <  2^32 */
87   ia = atoi(tok);
88
89   /* NOTE - the ia value is in big-endian format. */
90   sidout->id_auth[0] = 0;
91   sidout->id_auth[1] = 0;
92   sidout->id_auth[2] = (ia & 0xff000000) >> 24;
93   sidout->id_auth[3] = (ia & 0x00ff0000) >> 16;
94   sidout->id_auth[4] = (ia & 0x0000ff00) >> 8;
95   sidout->id_auth[5] = (ia & 0x000000ff);
96
97   sidout->num_auths = 0;
98
99   while(next_token(&p, tok, "-", sizeof(tok)) && 
100         sidout->num_auths < MAXSUBAUTHS)
101   {
102     /* 
103      * NOTE - the subauths are in native machine-endian format. They
104      * are converted to little-endian when linearized onto the wire.
105      */
106         uint32 rid = (uint32)strtoul(tok, NULL, 10);
107         DEBUG(50,("string_to_sid: tok: %s rid 0x%x\n", tok, rid));
108         sid_append_rid(sidout, rid);
109   }
110
111   DEBUG(7,("string_to_sid: converted SID %s ok\n", sidstr));
112
113   return True;
114 }
115
116 /*****************************************************************
117  add a rid to the end of a sid
118 *****************************************************************/  
119 BOOL sid_append_rid(DOM_SID *sid, uint32 rid)
120 {
121         if (sid->num_auths < MAXSUBAUTHS)
122         {
123                 sid->sub_auths[sid->num_auths++] = rid;
124                 return True;
125         }
126         return False;
127 }
128
129 /*****************************************************************
130  removes the last rid from the end of a sid
131 *****************************************************************/  
132 BOOL sid_split_rid(DOM_SID *sid, uint32 *rid)
133 {
134         if (sid->num_auths > 0)
135         {
136                 sid->num_auths--;
137                 if (rid != NULL)
138                 {
139                         (*rid) = sid->sub_auths[sid->num_auths];
140                 }
141                 return True;
142         }
143         return False;
144 }
145
146 /*****************************************************************
147  copies a sid
148 *****************************************************************/  
149 void sid_copy(DOM_SID *sid1, DOM_SID *sid2)
150 {
151         int i;
152
153         for (i = 0; i < 6; i++)
154         {
155                 sid1->id_auth[i] = sid2->id_auth[i];
156         }
157
158         for (i = 0; i < sid2->num_auths; i++)
159         {
160                 sid1->sub_auths[i] = sid2->sub_auths[i];
161         }
162
163         sid1->num_auths   = sid2->num_auths;
164         sid1->sid_rev_num = sid2->sid_rev_num;
165 }
166 /*****************************************************************
167  compare two sids
168 *****************************************************************/  
169 BOOL sid_equal(DOM_SID *sid1, DOM_SID *sid2)
170 {
171         int i;
172
173         /* compare most likely different rids, first: i.e start at end */
174         for (i = sid1->num_auths-1; i >= 0; --i)
175         {
176                 if (sid1->sub_auths[i] != sid2->sub_auths[i]) return False;
177         }
178
179         if (sid1->num_auths   != sid2->num_auths  ) return False;
180         if (sid1->sid_rev_num != sid2->sid_rev_num) return False;
181
182         for (i = 0; i < 6; i++)
183         {
184                 if (sid1->id_auth[i] != sid2->id_auth[i]) return False;
185         }
186
187         return True;
188 }
189
190
191 /*****************************************************************
192  calculates size of a sid
193 *****************************************************************/  
194 int sid_size(DOM_SID *sid)
195 {
196         if (sid == NULL)
197         {
198                 return 0;
199         }
200         return sid->num_auths * sizeof(uint32) + 8;
201 }