iAdditional files for winbind merge.
[abartlet/samba.git/.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
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_copy(&tmp_sid, &sid);
47         sid_split_rid(&tmp_sid, &rid);
48
49         if (sid_equal(&tmp_sid, &global_sid_Builtin)) {
50                 return WINBINDD_ERROR;
51         }
52
53         /* Lookup the sid */
54
55         if (!winbindd_lookup_name_by_sid(&sid, name, &type)) {
56                 return WINBINDD_ERROR;
57         }
58
59         string_sub(name, "\\", lp_winbind_separator(), sizeof(fstring));
60         fstrcpy(state->response.data.name.name, name);
61         state->response.data.name.type = type;
62
63         return WINBINDD_OK;
64 }
65
66 /* Convert a sid to a string */
67
68 enum winbindd_result winbindd_lookupname(struct winbindd_cli_state *state)
69 {
70         enum SID_NAME_USE type;
71         fstring sid_str, name_domain, name_user, name;
72         DOM_SID sid;
73         
74         DEBUG(3, ("[%5d]: lookupname %s\n", state->pid,
75                   state->request.data.name));
76
77         parse_domain_user(state->request.data.name, name_domain, name_user);
78
79         snprintf(name, sizeof(name), "%s\\%s", name_domain, name_user);
80
81         /* Lookup name from PDC using lsa_lookup_names() */
82
83         if (!winbindd_lookup_sid_by_name(name, &sid, &type)) {
84                 return WINBINDD_ERROR;
85         }
86
87         sid_to_string(sid_str, &sid);
88         fstrcpy(state->response.data.sid.sid, sid_str);
89         state->response.data.sid.type = type;
90
91         return WINBINDD_OK;
92 }
93
94 /* Convert a sid to a uid.  We assume we only have one rid attached to the
95    sid. */
96
97 enum winbindd_result winbindd_sid_to_uid(struct winbindd_cli_state *state)
98 {
99         DOM_SID sid;
100         uint32 user_rid;
101         struct winbindd_domain *domain;
102
103         DEBUG(3, ("[%5d]: sid to uid %s\n", state->pid,
104                   state->request.data.sid));
105
106         /* Split sid into domain sid and user rid */
107
108         string_to_sid(&sid, state->request.data.sid);
109         sid_split_rid(&sid, &user_rid);
110
111         /* Find domain this sid belongs to */
112
113         if ((domain = find_domain_from_sid(&sid)) == NULL) {
114                 fstring sid_str;
115
116                 sid_to_string(sid_str, &sid);
117                 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
118                 return WINBINDD_ERROR;
119         }
120
121         /* Find uid for this sid and return it */
122
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;
128         }
129
130         return WINBINDD_OK;
131 }
132
133 /* Convert a sid to a gid.  We assume we only have one rid attached to the
134    sid.*/
135
136 enum winbindd_result winbindd_sid_to_gid(struct winbindd_cli_state *state)
137 {
138         DOM_SID sid;
139         uint32 group_rid;
140         struct winbindd_domain *domain;
141
142         DEBUG(3, ("[%5d]: sid to gid %s\n", state->pid, 
143                   state->request.data.sid));
144
145         /* Split sid into domain sid and user rid */
146
147         string_to_sid(&sid, state->request.data.sid);
148         sid_split_rid(&sid, &group_rid);
149
150         /* Find domain this sid belongs to */
151
152         if ((domain = find_domain_from_sid(&sid)) == NULL) {
153                 fstring sid_str;
154
155                 sid_to_string(sid_str, &sid);
156                 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
157                 return WINBINDD_ERROR;
158         }
159
160         /* Find uid for this sid and return it */
161
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;
167         }
168
169         return WINBINDD_OK;
170 }
171
172 /* Convert a uid to a sid */
173
174 enum winbindd_result winbindd_uid_to_sid(struct winbindd_cli_state *state)
175 {
176         struct winbindd_domain *domain;
177         uint32 user_rid;
178         DOM_SID sid;
179
180         /* Bug out if the uid isn't in the winbind range */
181
182         if ((state->request.data.uid < server_state.uid_low ) ||
183             (state->request.data.uid > server_state.uid_high)) {
184                 return WINBINDD_ERROR;
185         }
186
187         DEBUG(3, ("[%5d]: uid to sid %d\n", state->pid, 
188                   state->request.data.uid));
189
190         /* Lookup rid for this uid */
191
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;
197         }
198
199         /* Construct sid and return it */
200
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;
205
206         return WINBINDD_OK;
207 }
208
209 /* Convert a gid to a sid */
210
211 enum winbindd_result winbindd_gid_to_sid(struct winbindd_cli_state *state)
212 {
213         struct winbindd_domain *domain;
214         uint32 group_rid;
215         DOM_SID sid;
216
217         /* Bug out if the gid isn't in the winbind range */
218
219         if ((state->request.data.gid < server_state.gid_low) ||
220             (state->request.data.gid > server_state.gid_high)) {
221                 return WINBINDD_ERROR;
222         }
223
224         DEBUG(3, ("[%5d]: gid to sid %d\n", state->pid,
225                   state->request.data.gid));
226
227         /* Lookup rid for this uid */
228
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;
234         }
235
236         /* Construct sid and return it */
237
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;
242
243         return WINBINDD_OK;
244 }