Use sid_peek_rid() instead of sid_split_rid().
[nivanova/samba-autobuild/.git] / source3 / nsswitch / winbindd_sid.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4
5    Winbind daemon - sid related functions
6
7    Copyright (C) Tim Potter 2000
8    
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.
13    
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.
18    
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.
22 */
23
24 #include "winbindd.h"
25 #include "sids.h"
26
27 /* Convert a string  */
28
29 enum winbindd_result winbindd_lookupsid(struct winbindd_cli_state *state)
30 {
31         extern DOM_SID global_sid_Builtin;
32         enum SID_NAME_USE type;
33         DOM_SID sid;
34         uint32 rid;
35         fstring name;
36
37         DEBUG(3, ("[%5d]: lookupsid %s\n", state->pid, 
38                   state->request.data.sid));
39
40         /* Lookup sid from PDC using lsa_lookup_sids() */
41
42         string_to_sid(&sid, state->request.data.sid);
43
44         /* Don't look up BUILTIN sids */
45
46         sid_peek_rid(&sid, &rid);
47
48         if (sid_equal(&tmp_sid, &global_sid_Builtin)) {
49                 return WINBINDD_ERROR;
50         }
51
52         /* Lookup the sid */
53
54         if (!winbindd_lookup_name_by_sid(&sid, name, &type)) {
55                 return WINBINDD_ERROR;
56         }
57
58         string_sub(name, "\\", lp_winbind_separator(), sizeof(fstring));
59         fstrcpy(state->response.data.name.name, name);
60         state->response.data.name.type = type;
61
62         return WINBINDD_OK;
63 }
64
65 /* Convert a sid to a string */
66
67 enum winbindd_result winbindd_lookupname(struct winbindd_cli_state *state)
68 {
69         enum SID_NAME_USE type;
70         fstring sid_str, name_domain, name_user, name;
71         DOM_SID sid;
72         
73         DEBUG(3, ("[%5d]: lookupname %s\n", state->pid,
74                   state->request.data.name));
75
76         parse_domain_user(state->request.data.name, name_domain, name_user);
77
78         snprintf(name, sizeof(name), "%s\\%s", name_domain, name_user);
79
80         /* Lookup name from PDC using lsa_lookup_names() */
81
82         if (!winbindd_lookup_sid_by_name(name, &sid, &type)) {
83                 return WINBINDD_ERROR;
84         }
85
86         sid_to_string(sid_str, &sid);
87         fstrcpy(state->response.data.sid.sid, sid_str);
88         state->response.data.sid.type = type;
89
90         return WINBINDD_OK;
91 }
92
93 /* Convert a sid to a uid.  We assume we only have one rid attached to the
94    sid. */
95
96 enum winbindd_result winbindd_sid_to_uid(struct winbindd_cli_state *state)
97 {
98         DOM_SID sid;
99         uint32 user_rid;
100         struct winbindd_domain *domain;
101
102         DEBUG(3, ("[%5d]: sid to uid %s\n", state->pid,
103                   state->request.data.sid));
104
105         /* Split sid into domain sid and user rid */
106
107         string_to_sid(&sid, state->request.data.sid);
108         sid_split_rid(&sid, &user_rid);
109
110         /* Find domain this sid belongs to */
111
112         if ((domain = find_domain_from_sid(&sid)) == NULL) {
113                 fstring sid_str;
114
115                 sid_to_string(sid_str, &sid);
116                 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
117                 return WINBINDD_ERROR;
118         }
119
120         /* Find uid for this sid and return it */
121
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;
127         }
128
129         return WINBINDD_OK;
130 }
131
132 /* Convert a sid to a gid.  We assume we only have one rid attached to the
133    sid.*/
134
135 enum winbindd_result winbindd_sid_to_gid(struct winbindd_cli_state *state)
136 {
137         DOM_SID sid;
138         uint32 group_rid;
139         struct winbindd_domain *domain;
140
141         DEBUG(3, ("[%5d]: sid to gid %s\n", state->pid, 
142                   state->request.data.sid));
143
144         /* Split sid into domain sid and user rid */
145
146         string_to_sid(&sid, state->request.data.sid);
147         sid_split_rid(&sid, &group_rid);
148
149         /* Find domain this sid belongs to */
150
151         if ((domain = find_domain_from_sid(&sid)) == NULL) {
152                 fstring sid_str;
153
154                 sid_to_string(sid_str, &sid);
155                 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
156                 return WINBINDD_ERROR;
157         }
158
159         /* Find uid for this sid and return it */
160
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;
166         }
167
168         return WINBINDD_OK;
169 }
170
171 /* Convert a uid to a sid */
172
173 enum winbindd_result winbindd_uid_to_sid(struct winbindd_cli_state *state)
174 {
175         struct winbindd_domain *domain;
176         uint32 user_rid;
177         DOM_SID sid;
178
179         /* Bug out if the uid isn't in the winbind range */
180
181         if ((state->request.data.uid < server_state.uid_low ) ||
182             (state->request.data.uid > server_state.uid_high)) {
183                 return WINBINDD_ERROR;
184         }
185
186         DEBUG(3, ("[%5d]: uid to sid %d\n", state->pid, 
187                   state->request.data.uid));
188
189         /* Lookup rid for this uid */
190
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;
196         }
197
198         /* Construct sid and return it */
199
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;
204
205         return WINBINDD_OK;
206 }
207
208 /* Convert a gid to a sid */
209
210 enum winbindd_result winbindd_gid_to_sid(struct winbindd_cli_state *state)
211 {
212         struct winbindd_domain *domain;
213         uint32 group_rid;
214         DOM_SID sid;
215
216         /* Bug out if the gid isn't in the winbind range */
217
218         if ((state->request.data.gid < server_state.gid_low) ||
219             (state->request.data.gid > server_state.gid_high)) {
220                 return WINBINDD_ERROR;
221         }
222
223         DEBUG(3, ("[%5d]: gid to sid %d\n", state->pid,
224                   state->request.data.gid));
225
226         /* Lookup rid for this uid */
227
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;
233         }
234
235         /* Construct sid and return it */
236
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;
241
242         return WINBINDD_OK;
243 }