Merge Samba3 and Samba4 together
[samba.git] / source4 / rpc_server / unixinfo / dcesrv_unixinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    endpoint server for the unixinfo pipe
5
6    Copyright (C) Volker Lendecke 2005
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "rpc_server/dcerpc_server.h"
24 #include "rpc_server/common/common.h"
25 #include "librpc/gen_ndr/ndr_unixinfo.h"
26 #include "libcli/wbclient/wbclient.h"
27 #include "lib/events/events.h"
28 #include "system/passwd.h"
29 #include "param/param.h"
30
31 static NTSTATUS dcerpc_unixinfo_bind(struct dcesrv_call_state *dce_call,
32                                      const struct dcesrv_interface *iface)
33 {
34         struct wbc_context *wbc_ctx;
35
36         wbc_ctx = wbc_init(dce_call->context, dce_call->msg_ctx,
37                            dce_call->event_ctx);
38         NT_STATUS_HAVE_NO_MEMORY(wbc_ctx);
39
40         dce_call->context->private = wbc_ctx;
41
42         return NT_STATUS_OK;
43 }
44
45 #define DCESRV_INTERFACE_UNIXINFO_BIND dcerpc_unixinfo_bind
46
47 static NTSTATUS dcesrv_unixinfo_SidToUid(struct dcesrv_call_state *dce_call,
48                                   TALLOC_CTX *mem_ctx,
49                                   struct unixinfo_SidToUid *r)
50 {
51         NTSTATUS status;
52         struct wbc_context *wbc_ctx = talloc_get_type_abort(
53                                                 dce_call->context->private,
54                                                 struct wbc_context);
55         struct id_mapping *ids;
56         struct composite_context *ctx;
57
58         DEBUG(5, ("dcesrv_unixinfo_SidToUid called\n"));
59
60         ids = talloc(mem_ctx, struct  id_mapping);
61         NT_STATUS_HAVE_NO_MEMORY(ids);
62
63         ids->sid = &r->in.sid;
64         ids->status = NT_STATUS_NONE_MAPPED;
65         ids->unixid = NULL;
66         ctx = wbc_sids_to_xids_send(wbc_ctx, ids, 1, ids);
67         NT_STATUS_HAVE_NO_MEMORY(ctx);
68
69         status = wbc_sids_to_xids_recv(ctx, &ids);
70         NT_STATUS_NOT_OK_RETURN(status);
71
72         if (ids->unixid->type == ID_TYPE_BOTH ||
73             ids->unixid->type == ID_TYPE_UID) {
74                 *r->out.uid = ids->unixid->id;
75                 return NT_STATUS_OK;
76         } else {
77                 return NT_STATUS_INVALID_SID;
78         }
79 }
80
81 static NTSTATUS dcesrv_unixinfo_UidToSid(struct dcesrv_call_state *dce_call,
82                                   TALLOC_CTX *mem_ctx,
83                                   struct unixinfo_UidToSid *r)
84 {
85         struct wbc_context *wbc_ctx = talloc_get_type_abort(
86                                                 dce_call->context->private,
87                                                 struct wbc_context);
88         struct id_mapping *ids;
89         struct composite_context *ctx;
90         uint32_t uid;
91         NTSTATUS status;
92
93         DEBUG(5, ("dcesrv_unixinfo_UidToSid called\n"));
94
95         uid = r->in.uid;        /* This cuts uid to 32 bit */
96         if ((uint64_t)uid != r->in.uid) {
97                 DEBUG(10, ("uid out of range\n"));
98                 return NT_STATUS_INVALID_PARAMETER;
99         }
100
101         ids = talloc(mem_ctx, struct id_mapping);
102         NT_STATUS_HAVE_NO_MEMORY(ids);
103
104         ids->sid = NULL;
105         ids->status = NT_STATUS_NONE_MAPPED;
106         ids->unixid = talloc(ids, struct unixid);
107         NT_STATUS_HAVE_NO_MEMORY(ids->unixid);
108
109         ids->unixid->id = uid;
110         ids->unixid->type = ID_TYPE_UID;
111
112         ctx = wbc_xids_to_sids_send(wbc_ctx, ids, 1, ids);
113         NT_STATUS_HAVE_NO_MEMORY(ctx);
114
115         status = wbc_xids_to_sids_recv(ctx, &ids);
116         NT_STATUS_NOT_OK_RETURN(status);
117
118         r->out.sid = ids->sid;
119         return NT_STATUS_OK;
120 }
121
122 static NTSTATUS dcesrv_unixinfo_SidToGid(struct dcesrv_call_state *dce_call,
123                                   TALLOC_CTX *mem_ctx,
124                                   struct unixinfo_SidToGid *r)
125 {
126         NTSTATUS status;
127         struct wbc_context *wbc_ctx = talloc_get_type_abort(
128                                                 dce_call->context->private,
129                                                 struct wbc_context);
130         struct id_mapping *ids;
131         struct composite_context *ctx;
132
133         DEBUG(5, ("dcesrv_unixinfo_SidToGid called\n"));
134
135         ids = talloc(mem_ctx, struct  id_mapping);
136         NT_STATUS_HAVE_NO_MEMORY(ids);
137
138         ids->sid = &r->in.sid;
139         ids->status = NT_STATUS_NONE_MAPPED;
140         ids->unixid = NULL;
141         ctx = wbc_sids_to_xids_send(wbc_ctx, ids, 1, ids);
142         NT_STATUS_HAVE_NO_MEMORY(ctx);
143
144         status = wbc_sids_to_xids_recv(ctx, &ids);
145         NT_STATUS_NOT_OK_RETURN(status);
146
147         if (ids->unixid->type == ID_TYPE_BOTH ||
148             ids->unixid->type == ID_TYPE_GID) {
149                 *r->out.gid = ids->unixid->id;
150                 return NT_STATUS_OK;
151         } else {
152                 return NT_STATUS_INVALID_SID;
153         }
154 }
155
156 static NTSTATUS dcesrv_unixinfo_GidToSid(struct dcesrv_call_state *dce_call,
157                                   TALLOC_CTX *mem_ctx,
158                                   struct unixinfo_GidToSid *r)
159 {
160         struct wbc_context *wbc_ctx = talloc_get_type_abort(
161                                                 dce_call->context->private,
162                                                 struct wbc_context);
163         struct id_mapping *ids;
164         struct composite_context *ctx;
165         uint32_t gid;
166         NTSTATUS status;
167
168         DEBUG(5, ("dcesrv_unixinfo_GidToSid called\n"));
169
170         gid = r->in.gid;        /* This cuts gid to 32 bit */
171         if ((uint64_t)gid != r->in.gid) {
172                 DEBUG(10, ("gid out of range\n"));
173                 return NT_STATUS_INVALID_PARAMETER;
174         }
175
176         ids = talloc(mem_ctx, struct id_mapping);
177         NT_STATUS_HAVE_NO_MEMORY(ids);
178
179         ids->sid = NULL;
180         ids->status = NT_STATUS_NONE_MAPPED;
181         ids->unixid = talloc(ids, struct unixid);
182         NT_STATUS_HAVE_NO_MEMORY(ids->unixid);
183
184         ids->unixid->id = gid;
185         ids->unixid->type = ID_TYPE_GID;
186
187         ctx = wbc_xids_to_sids_send(wbc_ctx, ids, 1, ids);
188         NT_STATUS_HAVE_NO_MEMORY(ctx);
189
190         status = wbc_xids_to_sids_recv(ctx, &ids);
191         NT_STATUS_NOT_OK_RETURN(status);
192
193         r->out.sid = ids->sid;
194         return NT_STATUS_OK;
195 }
196
197 static NTSTATUS dcesrv_unixinfo_GetPWUid(struct dcesrv_call_state *dce_call,
198                                   TALLOC_CTX *mem_ctx,
199                                   struct unixinfo_GetPWUid *r)
200 {
201         int i;
202
203         *r->out.count = 0;
204
205         r->out.infos = talloc_zero_array(mem_ctx, struct unixinfo_GetPWUidInfo,
206                                          *r->in.count);
207         NT_STATUS_HAVE_NO_MEMORY(r->out.infos);
208         *r->out.count = *r->in.count;
209
210         for (i=0; i < *r->in.count; i++) {
211                 uid_t uid;
212                 struct passwd *pwd;
213
214                 uid = r->in.uids[i];
215                 pwd = getpwuid(uid);
216                 if (pwd == NULL) {
217                         DEBUG(10, ("uid %d not found\n", uid));
218                         r->out.infos[i].homedir = "";
219                         r->out.infos[i].shell = "";
220                         r->out.infos[i].status = NT_STATUS_NO_SUCH_USER;
221                         continue;
222                 }
223
224                 r->out.infos[i].homedir = talloc_strdup(mem_ctx, pwd->pw_dir);
225                 r->out.infos[i].shell = talloc_strdup(mem_ctx, pwd->pw_shell);
226
227                 if ((r->out.infos[i].homedir == NULL) ||
228                     (r->out.infos[i].shell == NULL)) {
229                         r->out.infos[i].homedir = "";
230                         r->out.infos[i].shell = "";
231                         r->out.infos[i].status = NT_STATUS_NO_MEMORY;
232                         continue;
233                 }
234
235                 r->out.infos[i].status = NT_STATUS_OK;
236         }
237
238         return NT_STATUS_OK;
239 }
240
241 /* include the generated boilerplate */
242 #include "librpc/gen_ndr/ndr_unixinfo_s.c"