1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3 /* Copyright (C) 2002-2004 Novell, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU Lesser General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
29 #include <libxml/xmlmemory.h>
33 guint8 SubAuthorityCount;
35 guint8 IdentifierAuthority;
36 guint32 SubAuthority[1];
38 #define E2K_SID_SID_REVISION 1
40 struct _E2kSidPrivate {
42 E2kSid_SID *binary_sid;
47 #define PARENT_TYPE G_TYPE_OBJECT
48 static GObjectClass *parent_class = NULL;
50 static void dispose (GObject *object);
53 class_init (GObjectClass *object_class)
55 parent_class = g_type_class_ref (PARENT_TYPE);
57 object_class->dispose = dispose;
61 init (GObject *object)
63 E2kSid *sid = E2K_SID (object);
65 sid->priv = g_new0 (E2kSidPrivate, 1);
69 dispose (GObject *object)
71 E2kSid *sid = E2K_SID (object);
74 if (sid->priv->string_sid)
75 g_free (sid->priv->string_sid);
76 if (sid->priv->binary_sid)
77 g_free (sid->priv->binary_sid);
78 g_free (sid->priv->display_name);
84 G_OBJECT_CLASS (parent_class)->dispose (object);
87 E2K_MAKE_TYPE (e2k_sid, E2kSid, class_init, init, PARENT_TYPE)
90 sid_new_internal (E2kSidType type, const char *display_name,
91 const char *string_sid, const guint8 *binary_sid)
95 sid = g_object_new (E2K_TYPE_SID, NULL);
96 sid->priv->type = type;
99 sid->priv->binary_sid = g_memdup (binary_sid, E2K_SID_BINARY_SID_LEN (binary_sid));
101 sid->priv->string_sid = g_strdup (string_sid);
102 else if (!display_name)
103 e2k_sid_get_string_sid (sid);
106 if (type == E2K_SID_TYPE_WELL_KNOWN_GROUP) {
107 if (!strcmp (string_sid, E2K_SID_WKS_ANONYMOUS))
108 display_name = _(E2K_SID_WKS_ANONYMOUS_NAME);
109 else if (!strcmp (string_sid, E2K_SID_WKS_EVERYONE))
110 display_name = _(E2K_SID_WKS_EVERYONE_NAME);
113 display_name = string_sid;
115 sid->priv->display_name = g_strdup (display_name);
121 * e2k_sid_new_from_string_sid:
122 * @type: the type of SID that @string_sid is
123 * @string_sid: the string form of a Windows Security Identifier
124 * @display_name: UTF-8 display name of the user/group/etc identified
127 * Creates an %E2kSid from the given information
129 * Return value: the new SID
132 e2k_sid_new_from_string_sid (E2kSidType type, const char *string_sid,
133 const char *display_name)
135 g_return_val_if_fail (string_sid != NULL, NULL);
137 if (strlen (string_sid) < 6 || strncmp (string_sid, "S-1-", 4) != 0)
140 return sid_new_internal (type, display_name, string_sid, NULL);
144 * e2k_sid_new_from_binary_sid:
145 * @type: the type of SID that @binary_sid is
146 * @binary_sid: the binary form of a Windows Security Identifier
147 * @display_name: UTF-8 display name of the user/group/etc identified
150 * Creates an %E2kSid from the given information
152 * Return value: the new SID
155 e2k_sid_new_from_binary_sid (E2kSidType type,
156 const guint8 *binary_sid,
157 const char *display_name)
159 g_return_val_if_fail (binary_sid != NULL, NULL);
161 return sid_new_internal (type, display_name, NULL, binary_sid);
165 * e2k_sid_get_sid_type:
168 * Returns the type of @sid (user, group, etc)
170 * Return value: the %E2kSidType
173 e2k_sid_get_sid_type (E2kSid *sid)
175 g_return_val_if_fail (E2K_IS_SID (sid), E2K_SID_TYPE_USER);
177 return sid->priv->type;
181 * e2k_sid_get_string_sid:
184 * Returns the string form of @sid
186 * Return value: the string SID
189 e2k_sid_get_string_sid (E2kSid *sid)
191 g_return_val_if_fail (E2K_IS_SID (sid), NULL);
193 if (!sid->priv->string_sid) {
197 string = g_string_new (NULL);
199 /* Revision and IdentifierAuthority. */
200 g_string_append_printf (string, "S-%u-%u",
201 sid->priv->binary_sid->Revision,
202 sid->priv->binary_sid->IdentifierAuthority);
204 /* Subauthorities. */
205 for (sa = 0; sa < sid->priv->binary_sid->SubAuthorityCount; sa++) {
206 g_string_append_printf (string, "-%lu",
207 (unsigned long) GUINT32_FROM_LE (sid->priv->binary_sid->SubAuthority[sa]));
210 sid->priv->string_sid = string->str;
211 g_string_free (string, FALSE);
214 return sid->priv->string_sid;
218 * e2k_sid_get_binary_sid:
221 * Returns the binary form of @sid. Since the SID data is self-delimiting,
222 * no length value is needed. Use E2K_SID_BINARY_SID_LEN() if you need to
223 * know the size of the binary data.
225 * Return value: the binary SID
228 e2k_sid_get_binary_sid (E2kSid *sid)
230 g_return_val_if_fail (E2K_IS_SID (sid), NULL);
232 if (!sid->priv->binary_sid) {
233 int sa, subauth_count;
234 guint32 subauthority;
237 p = sid->priv->string_sid + 4;
239 while ((p = strchr (p, '-'))) {
244 sid->priv->binary_sid = g_malloc0 (sizeof (E2kSid_SID) + 4 * (subauth_count - 1));
245 sid->priv->binary_sid->Revision = E2K_SID_SID_REVISION;
246 sid->priv->binary_sid->IdentifierAuthority = strtoul (sid->priv->string_sid + 4, &p, 10);
247 sid->priv->binary_sid->SubAuthorityCount = subauth_count;
250 while (*p == '-' && sa < subauth_count) {
251 subauthority = strtoul (p + 1, &p, 10);
252 sid->priv->binary_sid->SubAuthority[sa++] =
253 GUINT32_TO_LE (subauthority);
257 return (guint8 *)sid->priv->binary_sid;
261 * e2k_sid_get_display_name:
264 * Returns the display name of the entity identified by @sid
266 * Return value: the UTF-8 display name
269 e2k_sid_get_display_name (E2kSid *sid)
271 g_return_val_if_fail (E2K_IS_SID (sid), NULL);
273 return sid->priv->display_name;
278 * e2k_sid_binary_sid_equal:
279 * @a: pointer to a binary SID
280 * @b: pointer to another binary SID
282 * Determines if @a and @b contain the same SID data. For use
285 * Return value: %TRUE or %FALSE
288 e2k_sid_binary_sid_equal (gconstpointer a, gconstpointer b)
290 const guint8 *bsida = (const guint8 *)a;
291 const guint8 *bsidb = (const guint8 *)b;
293 if (E2K_SID_BINARY_SID_LEN (bsida) !=
294 E2K_SID_BINARY_SID_LEN (bsidb))
296 return memcmp (bsida, bsidb, E2K_SID_BINARY_SID_LEN (bsida)) == 0;
300 * e2k_sid_binary_sid_hash:
301 * @key: pointer to a binary SID
303 * Hashes @key, a binary SID. For use with #GHashTable.
305 * Return value: the hash value
308 e2k_sid_binary_sid_hash (gconstpointer key)
310 const guint8 *bsid = (const guint8 *)key;
313 /* The majority of SIDs will differ only in the last
314 * subauthority value.
316 memcpy (&final_sa, bsid + E2K_SID_BINARY_SID_LEN (bsid) - 4, 4);