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 /* Convert a string */
28 enum winbindd_result winbindd_lookupsid(struct winbindd_cli_state *state)
30 extern DOM_SID global_sid_Builtin;
31 enum SID_NAME_USE type;
37 DEBUG(3, ("[%5d]: lookupsid %s\n", state->pid,
38 state->request.data.sid));
40 /* Lookup sid from PDC using lsa_lookup_sids() */
42 string_to_sid(&sid, state->request.data.sid);
44 /* Don't look up BUILTIN sids */
46 sid_copy(&tmp_sid, &sid);
47 sid_split_rid(&tmp_sid, &rid);
49 if (sid_equal(&tmp_sid, &global_sid_Builtin)) {
50 return WINBINDD_ERROR;
55 if (!winbindd_lookup_name_by_sid(&sid, dom_name, name, &type)) {
56 return WINBINDD_ERROR;
59 fstrcpy(state->response.data.name.dom_name, dom_name);
60 fstrcpy(state->response.data.name.name, name);
62 state->response.data.name.type = type;
67 /* Convert a sid to a string */
69 enum winbindd_result winbindd_lookupname(struct winbindd_cli_state *state)
71 enum SID_NAME_USE type;
73 char *name_domain, *name_user;
75 struct winbindd_domain *domain;
76 DEBUG(3, ("[%5d]: lookupname [%s]\\[%s]\n", state->pid,
77 state->request.data.name.dom_name, state->request.data.name.name));
79 name_domain = state->request.data.name.dom_name;
80 name_user = state->request.data.name.name;
82 if ((domain = find_domain_from_name(name_domain)) == NULL) {
83 DEBUG(0, ("could not find domain entry for domain %s\n",
85 return WINBINDD_ERROR;
88 /* Lookup name from PDC using lsa_lookup_names() */
89 if (!winbindd_lookup_sid_by_name(domain, name_user, &sid, &type)) {
90 return WINBINDD_ERROR;
93 sid_to_string(sid_str, &sid);
94 fstrcpy(state->response.data.sid.sid, sid_str);
95 state->response.data.sid.type = type;
100 /* Convert a sid to a uid. We assume we only have one rid attached to the
103 enum winbindd_result winbindd_sid_to_uid(struct winbindd_cli_state *state)
107 struct winbindd_domain *domain;
109 DEBUG(3, ("[%5d]: sid to uid %s\n", state->pid,
110 state->request.data.sid));
112 /* Split sid into domain sid and user rid */
114 string_to_sid(&sid, state->request.data.sid);
115 sid_split_rid(&sid, &user_rid);
117 /* Find domain this sid belongs to */
119 if ((domain = find_domain_from_sid(&sid)) == NULL) {
122 sid_to_string(sid_str, &sid);
123 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
124 return WINBINDD_ERROR;
127 /* Find uid for this sid and return it */
129 if (!winbindd_idmap_get_uid_from_rid(domain->name, user_rid,
130 &state->response.data.uid)) {
131 DEBUG(1, ("Could not get uid for sid %s\n",
132 state->request.data.sid));
133 return WINBINDD_ERROR;
139 /* Convert a sid to a gid. We assume we only have one rid attached to the
142 enum winbindd_result winbindd_sid_to_gid(struct winbindd_cli_state *state)
146 struct winbindd_domain *domain;
148 DEBUG(3, ("[%5d]: sid to gid %s\n", state->pid,
149 state->request.data.sid));
151 /* Split sid into domain sid and user rid */
153 string_to_sid(&sid, state->request.data.sid);
154 sid_split_rid(&sid, &group_rid);
156 /* Find domain this sid belongs to */
158 if ((domain = find_domain_from_sid(&sid)) == NULL) {
161 sid_to_string(sid_str, &sid);
162 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
163 return WINBINDD_ERROR;
166 /* Find uid for this sid and return it */
168 if (!winbindd_idmap_get_gid_from_rid(domain->name, group_rid,
169 &state->response.data.gid)) {
170 DEBUG(1, ("Could not get gid for sid %s\n",
171 state->request.data.sid));
172 return WINBINDD_ERROR;
178 /* Convert a uid to a sid */
180 enum winbindd_result winbindd_uid_to_sid(struct winbindd_cli_state *state)
182 struct winbindd_domain *domain;
186 /* Bug out if the uid isn't in the winbind range */
188 if ((state->request.data.uid < server_state.uid_low ) ||
189 (state->request.data.uid > server_state.uid_high)) {
190 return WINBINDD_ERROR;
193 DEBUG(3, ("[%5d]: uid to sid %d\n", state->pid,
194 state->request.data.uid));
196 /* Lookup rid for this uid */
198 if (!winbindd_idmap_get_rid_from_uid(state->request.data.uid,
199 &user_rid, &domain)) {
200 DEBUG(1, ("Could not convert uid %d to rid\n",
201 state->request.data.uid));
202 return WINBINDD_ERROR;
205 /* Construct sid and return it */
207 sid_copy(&sid, &domain->sid);
208 sid_append_rid(&sid, user_rid);
209 sid_to_string(state->response.data.sid.sid, &sid);
210 state->response.data.sid.type = SID_NAME_USER;
215 /* Convert a gid to a sid */
217 enum winbindd_result winbindd_gid_to_sid(struct winbindd_cli_state *state)
219 struct winbindd_domain *domain;
223 /* Bug out if the gid isn't in the winbind range */
225 if ((state->request.data.gid < server_state.gid_low) ||
226 (state->request.data.gid > server_state.gid_high)) {
227 return WINBINDD_ERROR;
230 DEBUG(3, ("[%5d]: gid to sid %d\n", state->pid,
231 state->request.data.gid));
233 /* Lookup rid for this uid */
235 if (!winbindd_idmap_get_rid_from_gid(state->request.data.gid,
236 &group_rid, &domain)) {
237 DEBUG(1, ("Could not convert gid %d to rid\n",
238 state->request.data.gid));
239 return WINBINDD_ERROR;
242 /* Construct sid and return it */
244 sid_copy(&sid, &domain->sid);
245 sid_append_rid(&sid, group_rid);
246 sid_to_string(state->response.data.sid.sid, &sid);
247 state->response.data.sid.type = SID_NAME_DOM_GRP;