Change the winbind interface to use seperate 'domain' and 'username' feilds for
[samba.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, tmp_sid;
34         uint32 rid;
35         fstring name;
36         fstring dom_name;
37
38         DEBUG(3, ("[%5d]: lookupsid %s\n", state->pid, 
39                   state->request.data.sid));
40
41         /* Lookup sid from PDC using lsa_lookup_sids() */
42
43         string_to_sid(&sid, state->request.data.sid);
44
45         /* Don't look up BUILTIN sids */
46
47         sid_copy(&tmp_sid, &sid);
48         sid_split_rid(&tmp_sid, &rid);
49
50         if (sid_equal(&tmp_sid, &global_sid_Builtin)) {
51                 return WINBINDD_ERROR;
52         }
53
54         /* Lookup the sid */
55
56         if (!winbindd_lookup_name_by_sid(&sid, dom_name, name, &type)) {
57                 return WINBINDD_ERROR;
58         }
59
60         fstrcpy(state->response.data.name.dom_name, dom_name);
61         fstrcpy(state->response.data.name.name, name);
62
63         state->response.data.name.type = type;
64
65         return WINBINDD_OK;
66 }
67
68 /* Convert a sid to a string */
69
70 enum winbindd_result winbindd_lookupname(struct winbindd_cli_state *state)
71 {
72         enum SID_NAME_USE type;
73         fstring sid_str;
74         char *name_domain, *name_user;
75         DOM_SID sid;
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));
79
80         name_domain = state->request.data.name.dom_name;
81         name_user = state->request.data.name.name;
82
83         if ((domain = find_domain_from_name(name_domain)) == NULL) {
84                 DEBUG(0, ("could not find domain entry for domain %s\n", 
85                           name_domain));
86                 return WINBINDD_ERROR;
87         }
88
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;
92         }
93
94         sid_to_string(sid_str, &sid);
95         fstrcpy(state->response.data.sid.sid, sid_str);
96         state->response.data.sid.type = type;
97
98         return WINBINDD_OK;
99 }
100
101 /* Convert a sid to a uid.  We assume we only have one rid attached to the
102    sid. */
103
104 enum winbindd_result winbindd_sid_to_uid(struct winbindd_cli_state *state)
105 {
106         DOM_SID sid;
107         uint32 user_rid;
108         struct winbindd_domain *domain;
109
110         DEBUG(3, ("[%5d]: sid to uid %s\n", state->pid,
111                   state->request.data.sid));
112
113         /* Split sid into domain sid and user rid */
114
115         string_to_sid(&sid, state->request.data.sid);
116         sid_split_rid(&sid, &user_rid);
117
118         /* Find domain this sid belongs to */
119
120         if ((domain = find_domain_from_sid(&sid)) == NULL) {
121                 fstring sid_str;
122
123                 sid_to_string(sid_str, &sid);
124                 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
125                 return WINBINDD_ERROR;
126         }
127
128         /* Find uid for this sid and return it */
129
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;
135         }
136
137         return WINBINDD_OK;
138 }
139
140 /* Convert a sid to a gid.  We assume we only have one rid attached to the
141    sid.*/
142
143 enum winbindd_result winbindd_sid_to_gid(struct winbindd_cli_state *state)
144 {
145         DOM_SID sid;
146         uint32 group_rid;
147         struct winbindd_domain *domain;
148
149         DEBUG(3, ("[%5d]: sid to gid %s\n", state->pid, 
150                   state->request.data.sid));
151
152         /* Split sid into domain sid and user rid */
153
154         string_to_sid(&sid, state->request.data.sid);
155         sid_split_rid(&sid, &group_rid);
156
157         /* Find domain this sid belongs to */
158
159         if ((domain = find_domain_from_sid(&sid)) == NULL) {
160                 fstring sid_str;
161
162                 sid_to_string(sid_str, &sid);
163                 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
164                 return WINBINDD_ERROR;
165         }
166
167         /* Find uid for this sid and return it */
168
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;
174         }
175
176         return WINBINDD_OK;
177 }
178
179 /* Convert a uid to a sid */
180
181 enum winbindd_result winbindd_uid_to_sid(struct winbindd_cli_state *state)
182 {
183         struct winbindd_domain *domain;
184         uint32 user_rid;
185         DOM_SID sid;
186
187         /* Bug out if the uid isn't in the winbind range */
188
189         if ((state->request.data.uid < server_state.uid_low ) ||
190             (state->request.data.uid > server_state.uid_high)) {
191                 return WINBINDD_ERROR;
192         }
193
194         DEBUG(3, ("[%5d]: uid to sid %d\n", state->pid, 
195                   state->request.data.uid));
196
197         /* Lookup rid for this uid */
198
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;
204         }
205
206         /* Construct sid and return it */
207
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;
212
213         return WINBINDD_OK;
214 }
215
216 /* Convert a gid to a sid */
217
218 enum winbindd_result winbindd_gid_to_sid(struct winbindd_cli_state *state)
219 {
220         struct winbindd_domain *domain;
221         uint32 group_rid;
222         DOM_SID sid;
223
224         /* Bug out if the gid isn't in the winbind range */
225
226         if ((state->request.data.gid < server_state.gid_low) ||
227             (state->request.data.gid > server_state.gid_high)) {
228                 return WINBINDD_ERROR;
229         }
230
231         DEBUG(3, ("[%5d]: gid to sid %d\n", state->pid,
232                   state->request.data.gid));
233
234         /* Lookup rid for this uid */
235
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;
241         }
242
243         /* Construct sid and return it */
244
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;
249
250         return WINBINDD_OK;
251 }