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