372898a08a4562fd7de3b6793c9a938c67e5106f
[samba.git] / source3 / 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
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_WINBIND
27
28 /* Convert a string  */
29
30 enum winbindd_result winbindd_lookupsid(struct winbindd_cli_state *state)
31 {
32         extern DOM_SID global_sid_Builtin;
33         enum SID_NAME_USE type;
34         DOM_SID sid, tmp_sid;
35         uint32 rid;
36         fstring name;
37         fstring dom_name;
38
39         DEBUG(3, ("[%5d]: lookupsid %s\n", state->pid, 
40                   state->request.data.sid));
41
42         /* Lookup sid from PDC using lsa_lookup_sids() */
43
44         if (!string_to_sid(&sid, state->request.data.sid)) {
45                 DEBUG(5, ("%s not a SID\n", state->request.data.sid));
46                 return WINBINDD_ERROR;
47         }
48
49         /* Don't look up BUILTIN sids */
50
51         sid_copy(&tmp_sid, &sid);
52         sid_split_rid(&tmp_sid, &rid);
53
54         if (sid_equal(&tmp_sid, &global_sid_Builtin)) {
55                 return WINBINDD_ERROR;
56         }
57
58         /* Lookup the sid */
59
60         if (!winbindd_lookup_name_by_sid(&sid, dom_name, name, &type)) {
61                 return WINBINDD_ERROR;
62         }
63
64         fstrcpy(state->response.data.name.dom_name, dom_name);
65         fstrcpy(state->response.data.name.name, name);
66
67         state->response.data.name.type = type;
68
69         return WINBINDD_OK;
70 }
71
72 /* Convert a sid to a string */
73
74 enum winbindd_result winbindd_lookupname(struct winbindd_cli_state *state)
75 {
76         enum SID_NAME_USE type;
77         fstring sid_str;
78         char *name_domain, *name_user;
79         DOM_SID sid;
80         struct winbindd_domain *domain;
81
82         DEBUG(3, ("[%5d]: lookupname %s%s%s\n", state->pid,
83                   state->request.data.name.dom_name, 
84                   lp_winbind_separator(),
85                   state->request.data.name.name));
86
87         name_domain = state->request.data.name.dom_name;
88         name_user = state->request.data.name.name;
89
90         if ((domain = find_domain_from_name(name_domain)) == NULL) {
91                 DEBUG(0, ("could not find domain entry for domain %s\n", 
92                           name_domain));
93                 return WINBINDD_ERROR;
94         }
95
96         /* Lookup name from PDC using lsa_lookup_names() */
97         if (!winbindd_lookup_sid_by_name(domain, name_user, &sid, &type)) {
98                 return WINBINDD_ERROR;
99         }
100
101         sid_to_string(sid_str, &sid);
102         fstrcpy(state->response.data.sid.sid, sid_str);
103         state->response.data.sid.type = type;
104
105         return WINBINDD_OK;
106 }
107
108 /* Convert a sid to a uid.  We assume we only have one rid attached to the
109    sid. */
110
111 enum winbindd_result winbindd_sid_to_uid(struct winbindd_cli_state *state)
112 {
113         DOM_SID sid;
114
115         DEBUG(3, ("[%5d]: sid to uid %s\n", state->pid,
116                   state->request.data.sid));
117
118         /* Split sid into domain sid and user rid */
119         if (!string_to_sid(&sid, state->request.data.sid)) {
120                 DEBUG(1, ("Could not get convert sid %s from string\n",
121                           state->request.data.sid));
122                 return WINBINDD_ERROR;
123         }
124
125         /* Find uid for this sid and return it */
126         if (!winbindd_idmap_get_uid_from_sid(&sid, &state->response.data.uid)) {
127                 DEBUG(1, ("Could not get uid for sid %s\n",
128                           state->request.data.sid));
129                 return WINBINDD_ERROR;
130         }
131
132         return WINBINDD_OK;
133 }
134
135 /* Convert a sid to a gid.  We assume we only have one rid attached to the
136    sid.*/
137
138 enum winbindd_result winbindd_sid_to_gid(struct winbindd_cli_state *state)
139 {
140         DOM_SID sid;
141
142         DEBUG(3, ("[%5d]: sid to gid %s\n", state->pid, 
143                   state->request.data.sid));
144
145         if (!string_to_sid(&sid, state->request.data.sid)) {
146                 DEBUG(1, ("Could not cvt string to sid %s\n",
147                           state->request.data.sid));
148                 return WINBINDD_ERROR;
149         }
150
151         /* Find gid for this sid and return it */
152         if (!winbindd_idmap_get_gid_from_sid(&sid, &state->response.data.gid)) {
153                 DEBUG(1, ("Could not get gid for sid %s\n",
154                           state->request.data.sid));
155                 return WINBINDD_ERROR;
156         }
157
158         return WINBINDD_OK;
159 }
160
161 /* Convert a uid to a sid */
162
163 enum winbindd_result winbindd_uid_to_sid(struct winbindd_cli_state *state)
164 {
165         DOM_SID sid;
166
167         /* Bug out if the uid isn't in the winbind range */
168
169         if ((state->request.data.uid < server_state.uid_low ) ||
170             (state->request.data.uid > server_state.uid_high)) {
171                 return WINBINDD_ERROR;
172         }
173
174         DEBUG(3, ("[%5d]: uid to sid %d\n", state->pid, 
175                   state->request.data.uid));
176
177         /* Lookup rid for this uid */
178         if (!winbindd_idmap_get_sid_from_uid(state->request.data.uid, &sid)) {
179                 DEBUG(1, ("Could not convert uid %d to rid\n",
180                           state->request.data.uid));
181                 return WINBINDD_ERROR;
182         }
183
184         sid_to_string(state->response.data.sid.sid, &sid);
185         state->response.data.sid.type = SID_NAME_USER;
186
187         return WINBINDD_OK;
188 }
189
190 /* Convert a gid to a sid */
191
192 enum winbindd_result winbindd_gid_to_sid(struct winbindd_cli_state *state)
193 {
194         DOM_SID sid;
195
196         /* Bug out if the gid isn't in the winbind range */
197
198         if ((state->request.data.gid < server_state.gid_low) ||
199             (state->request.data.gid > server_state.gid_high)) {
200                 return WINBINDD_ERROR;
201         }
202
203         DEBUG(3, ("[%5d]: gid to sid %d\n", state->pid,
204                   state->request.data.gid));
205
206         /* Lookup sid for this uid */
207         if (!winbindd_idmap_get_sid_from_gid(state->request.data.gid, &sid)) {
208                 DEBUG(1, ("Could not convert gid %d to sid\n",
209                           state->request.data.gid));
210                 return WINBINDD_ERROR;
211         }
212
213         /* Construct sid and return it */
214         sid_to_string(state->response.data.sid.sid, &sid);
215         state->response.data.sid.type = SID_NAME_DOM_GRP;
216
217         return WINBINDD_OK;
218 }