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;
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_peek_rid(&sid, &rid);
48 if (sid_equal(&tmp_sid, &global_sid_Builtin)) {
49 return WINBINDD_ERROR;
54 if (!winbindd_lookup_name_by_sid(&sid, name, &type)) {
55 return WINBINDD_ERROR;
58 string_sub(name, "\\", lp_winbind_separator(), sizeof(fstring));
59 fstrcpy(state->response.data.name.name, name);
60 state->response.data.name.type = type;
65 /* Convert a sid to a string */
67 enum winbindd_result winbindd_lookupname(struct winbindd_cli_state *state)
69 enum SID_NAME_USE type;
70 fstring sid_str, name_domain, name_user, name;
73 DEBUG(3, ("[%5d]: lookupname %s\n", state->pid,
74 state->request.data.name));
76 parse_domain_user(state->request.data.name, name_domain, name_user);
78 snprintf(name, sizeof(name), "%s\\%s", name_domain, name_user);
80 /* Lookup name from PDC using lsa_lookup_names() */
82 if (!winbindd_lookup_sid_by_name(name, &sid, &type)) {
83 return WINBINDD_ERROR;
86 sid_to_string(sid_str, &sid);
87 fstrcpy(state->response.data.sid.sid, sid_str);
88 state->response.data.sid.type = type;
93 /* Convert a sid to a uid. We assume we only have one rid attached to the
96 enum winbindd_result winbindd_sid_to_uid(struct winbindd_cli_state *state)
100 struct winbindd_domain *domain;
102 DEBUG(3, ("[%5d]: sid to uid %s\n", state->pid,
103 state->request.data.sid));
105 /* Split sid into domain sid and user rid */
107 string_to_sid(&sid, state->request.data.sid);
108 sid_split_rid(&sid, &user_rid);
110 /* Find domain this sid belongs to */
112 if ((domain = find_domain_from_sid(&sid)) == NULL) {
115 sid_to_string(sid_str, &sid);
116 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
117 return WINBINDD_ERROR;
120 /* Find uid for this sid and return it */
122 if (!winbindd_idmap_get_uid_from_rid(domain->name, user_rid,
123 &state->response.data.uid)) {
124 DEBUG(1, ("Could not get uid for sid %s\n",
125 state->request.data.sid));
126 return WINBINDD_ERROR;
132 /* Convert a sid to a gid. We assume we only have one rid attached to the
135 enum winbindd_result winbindd_sid_to_gid(struct winbindd_cli_state *state)
139 struct winbindd_domain *domain;
141 DEBUG(3, ("[%5d]: sid to gid %s\n", state->pid,
142 state->request.data.sid));
144 /* Split sid into domain sid and user rid */
146 string_to_sid(&sid, state->request.data.sid);
147 sid_split_rid(&sid, &group_rid);
149 /* Find domain this sid belongs to */
151 if ((domain = find_domain_from_sid(&sid)) == NULL) {
154 sid_to_string(sid_str, &sid);
155 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
156 return WINBINDD_ERROR;
159 /* Find uid for this sid and return it */
161 if (!winbindd_idmap_get_gid_from_rid(domain->name, group_rid,
162 &state->response.data.gid)) {
163 DEBUG(1, ("Could not get gid for sid %s\n",
164 state->request.data.sid));
165 return WINBINDD_ERROR;
171 /* Convert a uid to a sid */
173 enum winbindd_result winbindd_uid_to_sid(struct winbindd_cli_state *state)
175 struct winbindd_domain *domain;
179 /* Bug out if the uid isn't in the winbind range */
181 if ((state->request.data.uid < server_state.uid_low ) ||
182 (state->request.data.uid > server_state.uid_high)) {
183 return WINBINDD_ERROR;
186 DEBUG(3, ("[%5d]: uid to sid %d\n", state->pid,
187 state->request.data.uid));
189 /* Lookup rid for this uid */
191 if (!winbindd_idmap_get_rid_from_uid(state->request.data.uid,
192 &user_rid, &domain)) {
193 DEBUG(1, ("Could not convert uid %d to rid\n",
194 state->request.data.uid));
195 return WINBINDD_ERROR;
198 /* Construct sid and return it */
200 sid_copy(&sid, &domain->sid);
201 sid_append_rid(&sid, user_rid);
202 sid_to_string(state->response.data.sid.sid, &sid);
203 state->response.data.sid.type = SID_NAME_USER;
208 /* Convert a gid to a sid */
210 enum winbindd_result winbindd_gid_to_sid(struct winbindd_cli_state *state)
212 struct winbindd_domain *domain;
216 /* Bug out if the gid isn't in the winbind range */
218 if ((state->request.data.gid < server_state.gid_low) ||
219 (state->request.data.gid > server_state.gid_high)) {
220 return WINBINDD_ERROR;
223 DEBUG(3, ("[%5d]: gid to sid %d\n", state->pid,
224 state->request.data.gid));
226 /* Lookup rid for this uid */
228 if (!winbindd_idmap_get_rid_from_gid(state->request.data.gid,
229 &group_rid, &domain)) {
230 DEBUG(1, ("Could not convert gid %d to rid\n",
231 state->request.data.gid));
232 return WINBINDD_ERROR;
235 /* Construct sid and return it */
237 sid_copy(&sid, &domain->sid);
238 sid_append_rid(&sid, group_rid);
239 sid_to_string(state->response.data.sid.sid, &sid);
240 state->response.data.sid.type = SID_NAME_DOM_GRP;