4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * This is an extract from usr/src/common/smbsrv/smb_sid.c,
28 * with functions renamed as part of a tentative plan for convergence.
36 #include <sys/types.h>
37 #include <sys/sunddi.h>
45 * Returns the number of bytes required to hold the sid.
53 return (sizeof (sid_t
) - sizeof (uint32_t)
54 + (sid
->sid_subauthcnt
* sizeof (uint32_t)));
60 * Fill in the passed buffer with the string form of the given
64 sid_tostr(sid_t
*sid
, char *strsid
)
69 if (sid
== NULL
|| strsid
== NULL
)
72 (void) sprintf(p
, "S-%d-", sid
->sid_revision
);
76 for (i
= 0; i
< NT_SID_AUTH_MAX
; ++i
) {
77 if (sid
->sid_authority
[i
] != 0 || i
== NT_SID_AUTH_MAX
- 1) {
78 (void) sprintf(p
, "%d", sid
->sid_authority
[i
]);
84 for (i
= 0; i
< sid
->sid_subauthcnt
&& i
< NT_SID_SUBAUTH_MAX
; ++i
) {
85 (void) sprintf(p
, "-%u", sid
->sid_subauth
[i
]);
94 * Converts a SID in string form to a SID structure. There are lots of
95 * simplifying assumptions in here. The memory for the SID is allocated
96 * as if it was the largest possible SID; the caller is responsible for
97 * freeing the memory when it is no longer required. We assume that the
98 * string starts with "S-1-" and that the authority is held in the last
99 * byte, which should be okay for most situations. It also assumes the
100 * sub-authorities are in decimal format.
102 * On success, a pointer to a SID is returned. Otherwise a null pointer
106 sid_fromstr(char *sidstr
)
116 if (strncmp(sidstr
, "S-1-", 4) != 0)
119 size
= sizeof (sid_t
) + (NT_SID_SUBAUTH_MAX
* sizeof (uint32_t));
121 if ((sid
= malloc(size
)) == NULL
)
125 sid
->sid_revision
= NT_SID_REVISION
;
126 sid
->sid_authority
[5] = atoi(&sidstr
[4]);
128 for (i
= 0, p
= &sidstr
[5]; i
< NT_SID_SUBAUTH_MAX
&& *p
; ++i
) {
129 while (*p
&& *p
== '-')
132 if (*p
< '0' || *p
> '9') {
137 sid
->sid_subauth
[i
] = strtoul(p
, NULL
, 10);
139 while (*p
&& *p
!= '-')
143 sid
->sid_subauthcnt
= i
;
154 kmem_free(sid
, sid_len(sid
));
161 sid_to_le(sid_t
*sid
)
165 for (i
= 0; i
< sid
->sid_subauthcnt
&& i
< NT_SID_SUBAUTH_MAX
; ++i
) {
166 uint32_t v
= sid
->sid_subauth
[i
];
167 uint8_t *p
= (uint8_t *)&sid
->sid_subauth
[i
];
170 p
[1] = (v
>> 8) & 0xff;
171 p
[2] = (v
>> 16) & 0xff;
172 p
[3] = (v
>> 24) & 0xff;
177 sid_from_le(sid_t
*sid
)
181 for (i
= 0; i
< sid
->sid_subauthcnt
&& i
< NT_SID_SUBAUTH_MAX
; ++i
) {
183 uint8_t *p
= (uint8_t *)&sid
->sid_subauth
[i
];
185 v
= p
[0] | (p
[1] << 8) | (p
[2] << 16) | (p
[3] << 24);
187 sid
->sid_subauth
[i
] = v
;