2 Unix SMB/Netbios implementation.
7 Copyright (C) Tim Potter 2000
8 Copyright (C) Andrew Tridgell 2000
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Library General Public
21 License along with this library; if not, write to the
22 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.
28 /* Call winbindd to convert a name to a sid */
30 BOOL
winbind_lookup_name(char *name
, DOM_SID
*sid
, uint8
*name_type
)
32 struct winbindd_request request
;
33 struct winbindd_response response
;
34 enum nss_status result
;
36 if (!sid
|| !name_type
)
39 /* Send off request */
42 ZERO_STRUCT(response
);
44 fstrcpy(request
.data
.name
, name
);
45 if ((result
= winbindd_request(WINBINDD_LOOKUPNAME
, &request
,
46 &response
)) == NSS_STATUS_SUCCESS
) {
47 string_to_sid(sid
, response
.data
.sid
.sid
);
48 *name_type
= response
.data
.sid
.type
;
51 return result
== NSS_STATUS_SUCCESS
;
54 /* Call winbindd to convert sid to name */
56 BOOL
winbind_lookup_sid(DOM_SID
*sid
, fstring dom_name
, fstring name
,
59 struct winbindd_request request
;
60 struct winbindd_response response
;
61 enum nss_status result
;
69 /* Check if this is our own sid. This should perhaps be done by
70 winbind? For the moment handle it here. */
72 if (sid
->num_auths
== 5) {
73 sid_copy(&tmp_sid
, sid
);
74 sid_split_rid(&tmp_sid
, &rid
);
76 if (sid_equal(&global_sam_sid
, &tmp_sid
)) {
78 return map_domain_sid_to_name(&tmp_sid
, dom_name
) &&
79 local_lookup_rid(rid
, name
, name_type
);
83 /* Initialise request */
86 ZERO_STRUCT(response
);
88 sid_to_string(sid_str
, sid
);
89 fstrcpy(request
.data
.sid
, sid_str
);
93 result
= winbindd_request(WINBINDD_LOOKUPSID
, &request
, &response
);
97 if (result
== NSS_STATUS_SUCCESS
) {
98 parse_domain_user(response
.data
.name
.name
, dom_name
, name
);
99 *name_type
= response
.data
.name
.type
;
102 return (result
== NSS_STATUS_SUCCESS
);
105 /* Call winbindd to convert uid to sid */
107 BOOL
winbind_uid_to_sid(DOM_SID
*sid
, uid_t uid
)
109 struct winbindd_request request
;
110 struct winbindd_response response
;
116 /* Initialise request */
118 ZERO_STRUCT(request
);
119 ZERO_STRUCT(response
);
121 request
.data
.uid
= uid
;
125 result
= winbindd_request(WINBINDD_UID_TO_SID
, &request
, &response
);
127 /* Copy out result */
129 if (result
== NSS_STATUS_SUCCESS
) {
130 string_to_sid(sid
, response
.data
.sid
.sid
);
132 sid_copy(sid
, &global_sid_NULL
);
135 return (result
== NSS_STATUS_SUCCESS
);
138 /* Call winbindd to convert uid to sid */
140 BOOL
winbind_gid_to_sid(DOM_SID
*sid
, gid_t gid
)
142 struct winbindd_request request
;
143 struct winbindd_response response
;
149 /* Initialise request */
151 ZERO_STRUCT(request
);
152 ZERO_STRUCT(response
);
154 request
.data
.gid
= gid
;
158 result
= winbindd_request(WINBINDD_GID_TO_SID
, &request
, &response
);
160 /* Copy out result */
162 if (result
== NSS_STATUS_SUCCESS
) {
163 string_to_sid(sid
, response
.data
.sid
.sid
);
165 sid_copy(sid
, &global_sid_NULL
);
168 return (result
== NSS_STATUS_SUCCESS
);
173 /*****************************************************************
174 *THE CANNONICAL* convert name to SID function.
175 Tries winbind first - then uses local lookup.
176 *****************************************************************/
178 BOOL
lookup_name(char *name
, DOM_SID
*psid
, uint8
*name_type
)
180 extern pstring global_myname
;
182 if (!winbind_lookup_name(name
, psid
, name_type
)) {
184 DEBUG(10,("lookup_name: winbind lookup for %s failed - trying local\n", name
));
186 return local_lookup_name(global_myname
, name
, psid
, name_type
);
191 /*****************************************************************
192 *THE CANNONICAL* convert SID to name function.
193 Tries winbind first - then uses local lookup.
194 *****************************************************************/
196 BOOL
lookup_sid(DOM_SID
*sid
, fstring dom_name
, fstring name
, uint8
*name_type
)
198 if (!winbind_lookup_sid(sid
, dom_name
, name
, name_type
)) {
203 DEBUG(10,("lookup_sid: winbind lookup for SID %s failed - trying local.\n", sid_to_string(sid_str
, sid
) ));
205 sid_copy(&tmp_sid
, sid
);
206 sid_split_rid(&tmp_sid
, &rid
);
207 return map_domain_sid_to_name(&tmp_sid
, dom_name
) &&
208 lookup_known_rid(&tmp_sid
, rid
, name
, name_type
);
213 /*****************************************************************
214 *THE CANNONICAL* convert uid_t to SID function.
215 Tries winbind first - then uses local lookup.
217 *****************************************************************/
219 DOM_SID
*uid_to_sid(DOM_SID
*psid
, uid_t uid
)
221 if (!winbind_uid_to_sid(psid
, uid
)) {
222 DEBUG(10,("uid_to_sid: winbind lookup for uid %u failed - trying local.\n", (unsigned int)uid
));
224 return local_uid_to_sid(psid
, uid
);
230 /*****************************************************************
231 *THE CANNONICAL* convert gid_t to SID function.
232 Tries winbind first - then uses local lookup.
234 *****************************************************************/
236 DOM_SID
*gid_to_sid(DOM_SID
*psid
, gid_t gid
)
238 if (!winbind_gid_to_sid(psid
, gid
)) {
239 DEBUG(10,("gid_to_sid: winbind lookup for gid %u failed - trying local.\n", (unsigned int)gid
));
241 return local_gid_to_sid(psid
, gid
);