2 Unix SMB/CIFS implementation.
4 Winbind daemon - sid related functions
6 Copyright (C) Tim Potter 2000
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define DBGC_CLASS DBGC_WINBIND
28 /* Convert a string */
30 enum winbindd_result
winbindd_lookupsid(struct winbindd_cli_state
*state
)
32 extern DOM_SID global_sid_Builtin
;
33 enum SID_NAME_USE type
;
39 /* Ensure null termination */
40 state
->request
.data
.sid
[sizeof(state
->request
.data
.sid
)-1]='\0';
42 DEBUG(3, ("[%5d]: lookupsid %s\n", state
->pid
,
43 state
->request
.data
.sid
));
45 /* Lookup sid from PDC using lsa_lookup_sids() */
47 if (!string_to_sid(&sid
, state
->request
.data
.sid
)) {
48 DEBUG(5, ("%s not a SID\n", state
->request
.data
.sid
));
49 return WINBINDD_ERROR
;
52 /* Don't look up BUILTIN sids */
54 sid_copy(&tmp_sid
, &sid
);
55 sid_split_rid(&tmp_sid
, &rid
);
57 if (sid_equal(&tmp_sid
, &global_sid_Builtin
)) {
58 return WINBINDD_ERROR
;
63 if (!winbindd_lookup_name_by_sid(&sid
, dom_name
, name
, &type
)) {
64 return WINBINDD_ERROR
;
67 fstrcpy(state
->response
.data
.name
.dom_name
, dom_name
);
68 fstrcpy(state
->response
.data
.name
.name
, name
);
70 state
->response
.data
.name
.type
= type
;
77 * Look up the SID for a qualified name.
79 enum winbindd_result
winbindd_lookupname(struct winbindd_cli_state
*state
)
81 enum SID_NAME_USE type
;
83 char *name_domain
, *name_user
;
85 struct winbindd_domain
*domain
;
87 /* Ensure null termination */
88 state
->request
.data
.sid
[sizeof(state
->request
.data
.name
.dom_name
)-1]='\0';
90 /* Ensure null termination */
91 state
->request
.data
.sid
[sizeof(state
->request
.data
.name
.name
)-1]='\0';
93 DEBUG(3, ("[%5d]: lookupname %s%s%s\n", state
->pid
,
94 state
->request
.data
.name
.dom_name
,
95 lp_winbind_separator(),
96 state
->request
.data
.name
.name
));
98 name_domain
= state
->request
.data
.name
.dom_name
;
99 name_user
= state
->request
.data
.name
.name
;
101 if ((domain
= find_domain_from_name(name_domain
)) == NULL
) {
102 DEBUG(0, ("could not find domain entry for domain %s\n",
104 return WINBINDD_ERROR
;
107 /* Lookup name from PDC using lsa_lookup_names() */
108 if (!winbindd_lookup_sid_by_name(domain
, name_user
, &sid
, &type
)) {
109 return WINBINDD_ERROR
;
112 sid_to_string(sid_str
, &sid
);
113 fstrcpy(state
->response
.data
.sid
.sid
, sid_str
);
114 state
->response
.data
.sid
.type
= type
;
119 /* Convert a sid to a uid. We assume we only have one rid attached to the
122 enum winbindd_result
winbindd_sid_to_uid(struct winbindd_cli_state
*state
)
128 /* Ensure null termination */
129 state
->request
.data
.sid
[sizeof(state
->request
.data
.sid
)-1]='\0';
131 DEBUG(3, ("[%5d]: sid to uid %s\n", state
->pid
,
132 state
->request
.data
.sid
));
134 /* Split sid into domain sid and user rid */
135 if (!string_to_sid(&sid
, state
->request
.data
.sid
)) {
136 DEBUG(1, ("Could not get convert sid %s from string\n",
137 state
->request
.data
.sid
));
138 return WINBINDD_ERROR
;
141 /* Find uid for this sid and return it */
143 if (NT_STATUS_IS_ERR(idmap_get_id_from_sid(&id
, &id_type
, &sid
))) {
144 DEBUG(1, ("Could not get uid for sid %s\n",
145 state
->request
.data
.sid
));
146 return WINBINDD_ERROR
;
148 state
->response
.data
.uid
= id
.uid
;
153 /* Convert a sid to a gid. We assume we only have one rid attached to the
156 enum winbindd_result
winbindd_sid_to_gid(struct winbindd_cli_state
*state
)
162 /* Ensure null termination */
163 state
->request
.data
.sid
[sizeof(state
->request
.data
.sid
)-1]='\0';
165 DEBUG(3, ("[%5d]: sid to gid %s\n", state
->pid
,
166 state
->request
.data
.sid
));
168 if (!string_to_sid(&sid
, state
->request
.data
.sid
)) {
169 DEBUG(1, ("Could not cvt string to sid %s\n",
170 state
->request
.data
.sid
));
171 return WINBINDD_ERROR
;
174 /* Find gid for this sid and return it */
175 id_type
= ID_GROUPID
;
176 if (NT_STATUS_IS_ERR(idmap_get_id_from_sid(&id
, &id_type
, &sid
))) {
177 DEBUG(1, ("Could not get gid for sid %s\n",
178 state
->request
.data
.sid
));
179 return WINBINDD_ERROR
;
181 state
->response
.data
.gid
= id
.gid
;
186 /* Convert a uid to a sid */
188 enum winbindd_result
winbindd_uid_to_sid(struct winbindd_cli_state
*state
)
193 /* Bug out if the uid isn't in the winbind range */
195 if ((state
->request
.data
.uid
< server_state
.uid_low
) ||
196 (state
->request
.data
.uid
> server_state
.uid_high
)) {
197 return WINBINDD_ERROR
;
200 DEBUG(3, ("[%5d]: uid to sid %d\n", state
->pid
,
201 state
->request
.data
.uid
));
203 /* Lookup rid for this uid */
204 id
.uid
= state
->request
.data
.uid
;
205 if (NT_STATUS_IS_ERR(idmap_get_sid_from_id(&sid
, id
, ID_USERID
))) {
206 DEBUG(1, ("Could not convert uid %d to rid\n",
207 state
->request
.data
.uid
));
208 return WINBINDD_ERROR
;
211 sid_to_string(state
->response
.data
.sid
.sid
, &sid
);
212 state
->response
.data
.sid
.type
= SID_NAME_USER
;
217 /* Convert a gid to a sid */
219 enum winbindd_result
winbindd_gid_to_sid(struct winbindd_cli_state
*state
)
224 /* Bug out if the gid isn't in the winbind range */
226 if ((state
->request
.data
.gid
< server_state
.gid_low
) ||
227 (state
->request
.data
.gid
> server_state
.gid_high
)) {
228 return WINBINDD_ERROR
;
231 DEBUG(3, ("[%5d]: gid to sid %d\n", state
->pid
,
232 state
->request
.data
.gid
));
234 /* Lookup sid for this uid */
235 id
.gid
= state
->request
.data
.gid
;
236 if (NT_STATUS_IS_ERR(idmap_get_sid_from_id(&sid
, id
, ID_GROUPID
))) {
237 DEBUG(1, ("Could not convert gid %d to sid\n",
238 state
->request
.data
.gid
));
239 return WINBINDD_ERROR
;
242 /* Construct sid and return it */
243 sid_to_string(state
->response
.data
.sid
.sid
, &sid
);
244 state
->response
.data
.sid
.type
= SID_NAME_DOM_GRP
;