Removed version number from file header.
[tprouty/samba.git] / source / nsswitch / winbindd_sid.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon - sid related functions
5
6    Copyright (C) Tim Potter 2000
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "winbindd.h"
24 #include "sids.h"
25
26 /* Convert a string  */
27
28 enum winbindd_result winbindd_lookupsid(struct winbindd_cli_state *state)
29 {
30         extern DOM_SID global_sid_Builtin;
31         enum SID_NAME_USE type;
32         DOM_SID sid, tmp_sid;
33         uint32 rid;
34         fstring name;
35         fstring dom_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, dom_name, name, &type)) {
56                 return WINBINDD_ERROR;
57         }
58
59         fstrcpy(state->response.data.name.dom_name, dom_name);
60         fstrcpy(state->response.data.name.name, name);
61
62         state->response.data.name.type = type;
63
64         return WINBINDD_OK;
65 }
66
67 /* Convert a sid to a string */
68
69 enum winbindd_result winbindd_lookupname(struct winbindd_cli_state *state)
70 {
71         enum SID_NAME_USE type;
72         fstring sid_str;
73         char *name_domain, *name_user;
74         DOM_SID sid;
75         struct winbindd_domain *domain;
76         DEBUG(3, ("[%5d]: lookupname [%s]\\[%s]\n", state->pid,
77                   state->request.data.name.dom_name, state->request.data.name.name));
78
79         name_domain = state->request.data.name.dom_name;
80         name_user = state->request.data.name.name;
81
82         if ((domain = find_domain_from_name(name_domain)) == NULL) {
83                 DEBUG(0, ("could not find domain entry for domain %s\n", 
84                           name_domain));
85                 return WINBINDD_ERROR;
86         }
87
88         /* Lookup name from PDC using lsa_lookup_names() */
89         if (!winbindd_lookup_sid_by_name(domain, name_user, &sid, &type)) {
90                 return WINBINDD_ERROR;
91         }
92
93         sid_to_string(sid_str, &sid);
94         fstrcpy(state->response.data.sid.sid, sid_str);
95         state->response.data.sid.type = type;
96
97         return WINBINDD_OK;
98 }
99
100 /* Convert a sid to a uid.  We assume we only have one rid attached to the
101    sid. */
102
103 enum winbindd_result winbindd_sid_to_uid(struct winbindd_cli_state *state)
104 {
105         DOM_SID sid;
106         uint32 user_rid;
107         struct winbindd_domain *domain;
108
109         DEBUG(3, ("[%5d]: sid to uid %s\n", state->pid,
110                   state->request.data.sid));
111
112         /* Split sid into domain sid and user rid */
113
114         string_to_sid(&sid, state->request.data.sid);
115         sid_split_rid(&sid, &user_rid);
116
117         /* Find domain this sid belongs to */
118
119         if ((domain = find_domain_from_sid(&sid)) == NULL) {
120                 fstring sid_str;
121
122                 sid_to_string(sid_str, &sid);
123                 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
124                 return WINBINDD_ERROR;
125         }
126
127         /* Find uid for this sid and return it */
128
129         if (!winbindd_idmap_get_uid_from_rid(domain->name, user_rid,
130                                              &state->response.data.uid)) {
131                 DEBUG(1, ("Could not get uid for sid %s\n",
132                           state->request.data.sid));
133                 return WINBINDD_ERROR;
134         }
135
136         return WINBINDD_OK;
137 }
138
139 /* Convert a sid to a gid.  We assume we only have one rid attached to the
140    sid.*/
141
142 enum winbindd_result winbindd_sid_to_gid(struct winbindd_cli_state *state)
143 {
144         DOM_SID sid;
145         uint32 group_rid;
146         struct winbindd_domain *domain;
147
148         DEBUG(3, ("[%5d]: sid to gid %s\n", state->pid, 
149                   state->request.data.sid));
150
151         /* Split sid into domain sid and user rid */
152
153         string_to_sid(&sid, state->request.data.sid);
154         sid_split_rid(&sid, &group_rid);
155
156         /* Find domain this sid belongs to */
157
158         if ((domain = find_domain_from_sid(&sid)) == NULL) {
159                 fstring sid_str;
160
161                 sid_to_string(sid_str, &sid);
162                 DEBUG(1, ("Could not find domain for sid %s\n", sid_str));
163                 return WINBINDD_ERROR;
164         }
165
166         /* Find uid for this sid and return it */
167
168         if (!winbindd_idmap_get_gid_from_rid(domain->name, group_rid,
169                                              &state->response.data.gid)) {
170                 DEBUG(1, ("Could not get gid for sid %s\n",
171                           state->request.data.sid));
172                 return WINBINDD_ERROR;
173         }
174
175         return WINBINDD_OK;
176 }
177
178 /* Convert a uid to a sid */
179
180 enum winbindd_result winbindd_uid_to_sid(struct winbindd_cli_state *state)
181 {
182         struct winbindd_domain *domain;
183         uint32 user_rid;
184         DOM_SID sid;
185
186         /* Bug out if the uid isn't in the winbind range */
187
188         if ((state->request.data.uid < server_state.uid_low ) ||
189             (state->request.data.uid > server_state.uid_high)) {
190                 return WINBINDD_ERROR;
191         }
192
193         DEBUG(3, ("[%5d]: uid to sid %d\n", state->pid, 
194                   state->request.data.uid));
195
196         /* Lookup rid for this uid */
197
198         if (!winbindd_idmap_get_rid_from_uid(state->request.data.uid,
199                                              &user_rid, &domain)) {
200                 DEBUG(1, ("Could not convert uid %d to rid\n",
201                           state->request.data.uid));
202                 return WINBINDD_ERROR;
203         }
204
205         /* Construct sid and return it */
206
207         sid_copy(&sid, &domain->sid);
208         sid_append_rid(&sid, user_rid);
209         sid_to_string(state->response.data.sid.sid, &sid);
210         state->response.data.sid.type = SID_NAME_USER;
211
212         return WINBINDD_OK;
213 }
214
215 /* Convert a gid to a sid */
216
217 enum winbindd_result winbindd_gid_to_sid(struct winbindd_cli_state *state)
218 {
219         struct winbindd_domain *domain;
220         uint32 group_rid;
221         DOM_SID sid;
222
223         /* Bug out if the gid isn't in the winbind range */
224
225         if ((state->request.data.gid < server_state.gid_low) ||
226             (state->request.data.gid > server_state.gid_high)) {
227                 return WINBINDD_ERROR;
228         }
229
230         DEBUG(3, ("[%5d]: gid to sid %d\n", state->pid,
231                   state->request.data.gid));
232
233         /* Lookup rid for this uid */
234
235         if (!winbindd_idmap_get_rid_from_gid(state->request.data.gid,
236                                              &group_rid, &domain)) {
237                 DEBUG(1, ("Could not convert gid %d to rid\n",
238                           state->request.data.gid));
239                 return WINBINDD_ERROR;
240         }
241
242         /* Construct sid and return it */
243
244         sid_copy(&sid, &domain->sid);
245         sid_append_rid(&sid, group_rid);
246         sid_to_string(state->response.data.sid.sid, &sid);
247         state->response.data.sid.type = SID_NAME_DOM_GRP;
248
249         return WINBINDD_OK;
250 }