2 Unix SMB/Netbios implementation.
5 Winbind daemon - sid related functions
7 Copyright (C) Tim Potter 2000
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 /* Convert a string */
29 enum winbindd_result winbindd_lookupsid(struct winbindd_cli_state *state)
31 extern DOM_SID global_sid_Builtin;
32 enum SID_NAME_USE type;
38 DEBUG(3, ("[%5d]: lookupsid %s\n", state->pid,
39 state->request.data.sid));
41 /* Lookup sid from PDC using lsa_lookup_sids() */
43 string_to_sid(&sid, state->request.data.sid);
45 /* Don't look up BUILTIN sids */
47 sid_copy(&tmp_sid, &sid);
48 sid_split_rid(&tmp_sid, &rid);
50 if (sid_equal(&tmp_sid, &global_sid_Builtin)) {
51 return WINBINDD_ERROR;
56 if (!winbindd_lookup_name_by_sid(&sid, dom_name, name, &type)) {
57 return WINBINDD_ERROR;
60 fstrcpy(state->response.data.name.dom_name, dom_name);
61 fstrcpy(state->response.data.name.name, name);
63 state->response.data.name.type = type;
68 /* Convert a sid to a string */
70 enum winbindd_result winbindd_lookupname(struct winbindd_cli_state *state)
72 enum SID_NAME_USE type;
74 char *name_domain, *name_user;
76 struct winbindd_domain *domain;
77 DEBUG(3, ("[%5d]: lookupname [%s]\\[%s]\n", state->pid,
78 state->request.data.name.dom_name, state->request.data.name.name));
80 name_domain = state->request.data.name.dom_name;
81 name_user = state->request.data.name.name;
83 if ((domain = find_domain_from_name(name_domain)) == NULL) {
84 DEBUG(0, ("could not find domain entry for domain %s\n",
86 return WINBINDD_ERROR;
89 /* Lookup name from PDC using lsa_lookup_names() */
90 if (!winbindd_lookup_sid_by_name(domain, name_user, &sid, &type)) {
91 return WINBINDD_ERROR;
94 sid_to_string(sid_str, &sid);
95 fstrcpy(state->response.data.sid.sid, sid_str);
96 state->response.data.sid.type = type;
101 /* Convert a sid to a uid. We assume we only have one rid attached to the
104 enum winbindd_result winbindd_sid_to_uid(struct winbindd_cli_state *state)
108 struct winbindd_domain *domain;
110 DEBUG(3, ("[%5d]: sid to uid %s\n", state->pid,
111 state->request.data.sid));
113 /* Split sid into domain sid and user rid */
115 string_to_sid(&sid, state->request.data.sid);
116 sid_split_rid(&sid, &user_rid);
118 /* Find domain this sid belongs to */
120 if ((domain = find_domain_from_sid(&sid)) == NULL) {
123 sid_to_string(sid_str, &sid);
124 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
125 return WINBINDD_ERROR;
128 /* Find uid for this sid and return it */
130 if (!winbindd_idmap_get_uid_from_rid(domain->name, user_rid,
131 &state->response.data.uid)) {
132 DEBUG(1, ("Could not get uid for sid %s\n",
133 state->request.data.sid));
134 return WINBINDD_ERROR;
140 /* Convert a sid to a gid. We assume we only have one rid attached to the
143 enum winbindd_result winbindd_sid_to_gid(struct winbindd_cli_state *state)
147 struct winbindd_domain *domain;
149 DEBUG(3, ("[%5d]: sid to gid %s\n", state->pid,
150 state->request.data.sid));
152 /* Split sid into domain sid and user rid */
154 string_to_sid(&sid, state->request.data.sid);
155 sid_split_rid(&sid, &group_rid);
157 /* Find domain this sid belongs to */
159 if ((domain = find_domain_from_sid(&sid)) == NULL) {
162 sid_to_string(sid_str, &sid);
163 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
164 return WINBINDD_ERROR;
167 /* Find uid for this sid and return it */
169 if (!winbindd_idmap_get_gid_from_rid(domain->name, group_rid,
170 &state->response.data.gid)) {
171 DEBUG(1, ("Could not get gid for sid %s\n",
172 state->request.data.sid));
173 return WINBINDD_ERROR;
179 /* Convert a uid to a sid */
181 enum winbindd_result winbindd_uid_to_sid(struct winbindd_cli_state *state)
183 struct winbindd_domain *domain;
187 /* Bug out if the uid isn't in the winbind range */
189 if ((state->request.data.uid < server_state.uid_low ) ||
190 (state->request.data.uid > server_state.uid_high)) {
191 return WINBINDD_ERROR;
194 DEBUG(3, ("[%5d]: uid to sid %d\n", state->pid,
195 state->request.data.uid));
197 /* Lookup rid for this uid */
199 if (!winbindd_idmap_get_rid_from_uid(state->request.data.uid,
200 &user_rid, &domain)) {
201 DEBUG(1, ("Could not convert uid %d to rid\n",
202 state->request.data.uid));
203 return WINBINDD_ERROR;
206 /* Construct sid and return it */
208 sid_copy(&sid, &domain->sid);
209 sid_append_rid(&sid, user_rid);
210 sid_to_string(state->response.data.sid.sid, &sid);
211 state->response.data.sid.type = SID_NAME_USER;
216 /* Convert a gid to a sid */
218 enum winbindd_result winbindd_gid_to_sid(struct winbindd_cli_state *state)
220 struct winbindd_domain *domain;
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 rid for this uid */
236 if (!winbindd_idmap_get_rid_from_gid(state->request.data.gid,
237 &group_rid, &domain)) {
238 DEBUG(1, ("Could not convert gid %d to rid\n",
239 state->request.data.gid));
240 return WINBINDD_ERROR;
243 /* Construct sid and return it */
245 sid_copy(&sid, &domain->sid);
246 sid_append_rid(&sid, group_rid);
247 sid_to_string(state->response.data.sid.sid, &sid);
248 state->response.data.sid.type = SID_NAME_DOM_GRP;