If I have to do a 2.2.1a release, now I can.
[Samba.git] / source / nsswitch / winbindd_sid.c
blobbc014f26918d39a97694978c093cef51e32cf22d
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
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.
24 #include "winbindd.h"
25 #include "sids.h"
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;
33 DOM_SID sid, tmp_sid;
34 uint32 rid;
35 fstring name;
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;
53 /* Lookup the sid */
55 if (!winbindd_lookup_name_by_sid(&sid, name, &type)) {
56 return WINBINDD_ERROR;
59 string_sub(name, "\\", lp_winbind_separator(), sizeof(fstring));
60 fstrcpy(state->response.data.name.name, name);
61 state->response.data.name.type = type;
63 return WINBINDD_OK;
66 /* Convert a sid to a string */
68 enum winbindd_result winbindd_lookupname(struct winbindd_cli_state *state)
70 enum SID_NAME_USE type;
71 fstring sid_str, name_domain, name_user, name;
72 DOM_SID sid;
74 DEBUG(3, ("[%5d]: lookupname %s\n", state->pid,
75 state->request.data.name));
77 parse_domain_user(state->request.data.name, name_domain, name_user);
79 snprintf(name, sizeof(name), "%s\\%s", name_domain, name_user);
81 /* Lookup name from PDC using lsa_lookup_names() */
83 if (!winbindd_lookup_sid_by_name(name, &sid, &type)) {
84 return WINBINDD_ERROR;
87 sid_to_string(sid_str, &sid);
88 fstrcpy(state->response.data.sid.sid, sid_str);
89 state->response.data.sid.type = type;
91 return WINBINDD_OK;
94 /* Convert a sid to a uid. We assume we only have one rid attached to the
95 sid. */
97 enum winbindd_result winbindd_sid_to_uid(struct winbindd_cli_state *state)
99 DOM_SID sid;
100 uint32 user_rid;
101 struct winbindd_domain *domain;
103 DEBUG(3, ("[%5d]: sid to uid %s\n", state->pid,
104 state->request.data.sid));
106 /* Split sid into domain sid and user rid */
108 string_to_sid(&sid, state->request.data.sid);
109 sid_split_rid(&sid, &user_rid);
111 /* Find domain this sid belongs to */
113 if ((domain = find_domain_from_sid(&sid)) == NULL) {
114 fstring sid_str;
116 sid_to_string(sid_str, &sid);
117 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
118 return WINBINDD_ERROR;
121 /* Find uid for this sid and return it */
123 if (!winbindd_idmap_get_uid_from_rid(domain->name, user_rid,
124 &state->response.data.uid)) {
125 DEBUG(1, ("Could not get uid for sid %s\n",
126 state->request.data.sid));
127 return WINBINDD_ERROR;
130 return WINBINDD_OK;
133 /* Convert a sid to a gid. We assume we only have one rid attached to the
134 sid.*/
136 enum winbindd_result winbindd_sid_to_gid(struct winbindd_cli_state *state)
138 DOM_SID sid;
139 uint32 group_rid;
140 struct winbindd_domain *domain;
142 DEBUG(3, ("[%5d]: sid to gid %s\n", state->pid,
143 state->request.data.sid));
145 /* Split sid into domain sid and user rid */
147 string_to_sid(&sid, state->request.data.sid);
148 sid_split_rid(&sid, &group_rid);
150 /* Find domain this sid belongs to */
152 if ((domain = find_domain_from_sid(&sid)) == NULL) {
153 fstring sid_str;
155 sid_to_string(sid_str, &sid);
156 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
157 return WINBINDD_ERROR;
160 /* Find uid for this sid and return it */
162 if (!winbindd_idmap_get_gid_from_rid(domain->name, group_rid,
163 &state->response.data.gid)) {
164 DEBUG(1, ("Could not get gid for sid %s\n",
165 state->request.data.sid));
166 return WINBINDD_ERROR;
169 return WINBINDD_OK;
172 /* Convert a uid to a sid */
174 enum winbindd_result winbindd_uid_to_sid(struct winbindd_cli_state *state)
176 struct winbindd_domain *domain;
177 uint32 user_rid;
178 DOM_SID sid;
180 /* Bug out if the uid isn't in the winbind range */
182 if ((state->request.data.uid < server_state.uid_low ) ||
183 (state->request.data.uid > server_state.uid_high)) {
184 return WINBINDD_ERROR;
187 DEBUG(3, ("[%5d]: uid to sid %d\n", state->pid,
188 state->request.data.uid));
190 /* Lookup rid for this uid */
192 if (!winbindd_idmap_get_rid_from_uid(state->request.data.uid,
193 &user_rid, &domain)) {
194 DEBUG(1, ("Could not convert uid %d to rid\n",
195 state->request.data.uid));
196 return WINBINDD_ERROR;
199 /* Construct sid and return it */
201 sid_copy(&sid, &domain->sid);
202 sid_append_rid(&sid, user_rid);
203 sid_to_string(state->response.data.sid.sid, &sid);
204 state->response.data.sid.type = SID_NAME_USER;
206 return WINBINDD_OK;
209 /* Convert a gid to a sid */
211 enum winbindd_result winbindd_gid_to_sid(struct winbindd_cli_state *state)
213 struct winbindd_domain *domain;
214 uint32 group_rid;
215 DOM_SID sid;
217 /* Bug out if the gid isn't in the winbind range */
219 if ((state->request.data.gid < server_state.gid_low) ||
220 (state->request.data.gid > server_state.gid_high)) {
221 return WINBINDD_ERROR;
224 DEBUG(3, ("[%5d]: gid to sid %d\n", state->pid,
225 state->request.data.gid));
227 /* Lookup rid for this uid */
229 if (!winbindd_idmap_get_rid_from_gid(state->request.data.gid,
230 &group_rid, &domain)) {
231 DEBUG(1, ("Could not convert gid %d to rid\n",
232 state->request.data.gid));
233 return WINBINDD_ERROR;
236 /* Construct sid and return it */
238 sid_copy(&sid, &domain->sid);
239 sid_append_rid(&sid, group_rid);
240 sid_to_string(state->response.data.sid.sid, &sid);
241 state->response.data.sid.type = SID_NAME_DOM_GRP;
243 return WINBINDD_OK;