Move smb_create_user() in samsync
[Samba.git] / source4 / rpc_server / unixinfo / dcesrv_unixinfo.c
bloba1d76ef8115a2bbe47cb4dafef7344e22bfaf1f5
1 /*
2 Unix SMB/CIFS implementation.
4 endpoint server for the unixinfo pipe
6 Copyright (C) Volker Lendecke 2005
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.
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.
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/>.
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"
30 static NTSTATUS dcerpc_unixinfo_bind(struct dcesrv_call_state *dce_call,
31 const struct dcesrv_interface *iface)
33 struct wbc_context *wbc_ctx;
35 wbc_ctx = wbc_init(dce_call->context, dce_call->msg_ctx,
36 dce_call->event_ctx);
37 NT_STATUS_HAVE_NO_MEMORY(wbc_ctx);
39 dce_call->context->private_data = wbc_ctx;
41 return NT_STATUS_OK;
44 #define DCESRV_INTERFACE_UNIXINFO_BIND dcerpc_unixinfo_bind
46 static NTSTATUS dcesrv_unixinfo_SidToUid(struct dcesrv_call_state *dce_call,
47 TALLOC_CTX *mem_ctx,
48 struct unixinfo_SidToUid *r)
50 NTSTATUS status;
51 struct wbc_context *wbc_ctx = talloc_get_type_abort(
52 dce_call->context->private_data,
53 struct wbc_context);
54 struct id_mapping *ids;
55 struct composite_context *ctx;
57 DEBUG(5, ("dcesrv_unixinfo_SidToUid called\n"));
59 ids = talloc(mem_ctx, struct id_mapping);
60 NT_STATUS_HAVE_NO_MEMORY(ids);
62 ids->sid = &r->in.sid;
63 ids->status = NT_STATUS_NONE_MAPPED;
64 ids->unixid = NULL;
65 ctx = wbc_sids_to_xids_send(wbc_ctx, ids, 1, ids);
66 NT_STATUS_HAVE_NO_MEMORY(ctx);
68 status = wbc_sids_to_xids_recv(ctx, &ids);
69 NT_STATUS_NOT_OK_RETURN(status);
71 if (ids->unixid->type == ID_TYPE_BOTH ||
72 ids->unixid->type == ID_TYPE_UID) {
73 *r->out.uid = ids->unixid->id;
74 return NT_STATUS_OK;
75 } else {
76 return NT_STATUS_INVALID_SID;
80 static NTSTATUS dcesrv_unixinfo_UidToSid(struct dcesrv_call_state *dce_call,
81 TALLOC_CTX *mem_ctx,
82 struct unixinfo_UidToSid *r)
84 struct wbc_context *wbc_ctx = talloc_get_type_abort(
85 dce_call->context->private_data,
86 struct wbc_context);
87 struct id_mapping *ids;
88 struct composite_context *ctx;
89 uint32_t uid;
90 NTSTATUS status;
92 DEBUG(5, ("dcesrv_unixinfo_UidToSid called\n"));
94 uid = r->in.uid; /* This cuts uid to 32 bit */
95 if ((uint64_t)uid != r->in.uid) {
96 DEBUG(10, ("uid out of range\n"));
97 return NT_STATUS_INVALID_PARAMETER;
100 ids = talloc(mem_ctx, struct id_mapping);
101 NT_STATUS_HAVE_NO_MEMORY(ids);
103 ids->sid = NULL;
104 ids->status = NT_STATUS_NONE_MAPPED;
105 ids->unixid = talloc(ids, struct unixid);
106 NT_STATUS_HAVE_NO_MEMORY(ids->unixid);
108 ids->unixid->id = uid;
109 ids->unixid->type = ID_TYPE_UID;
111 ctx = wbc_xids_to_sids_send(wbc_ctx, ids, 1, ids);
112 NT_STATUS_HAVE_NO_MEMORY(ctx);
114 status = wbc_xids_to_sids_recv(ctx, &ids);
115 NT_STATUS_NOT_OK_RETURN(status);
117 r->out.sid = ids->sid;
118 return NT_STATUS_OK;
121 static NTSTATUS dcesrv_unixinfo_SidToGid(struct dcesrv_call_state *dce_call,
122 TALLOC_CTX *mem_ctx,
123 struct unixinfo_SidToGid *r)
125 NTSTATUS status;
126 struct wbc_context *wbc_ctx = talloc_get_type_abort(
127 dce_call->context->private_data,
128 struct wbc_context);
129 struct id_mapping *ids;
130 struct composite_context *ctx;
132 DEBUG(5, ("dcesrv_unixinfo_SidToGid called\n"));
134 ids = talloc(mem_ctx, struct id_mapping);
135 NT_STATUS_HAVE_NO_MEMORY(ids);
137 ids->sid = &r->in.sid;
138 ids->status = NT_STATUS_NONE_MAPPED;
139 ids->unixid = NULL;
140 ctx = wbc_sids_to_xids_send(wbc_ctx, ids, 1, ids);
141 NT_STATUS_HAVE_NO_MEMORY(ctx);
143 status = wbc_sids_to_xids_recv(ctx, &ids);
144 NT_STATUS_NOT_OK_RETURN(status);
146 if (ids->unixid->type == ID_TYPE_BOTH ||
147 ids->unixid->type == ID_TYPE_GID) {
148 *r->out.gid = ids->unixid->id;
149 return NT_STATUS_OK;
150 } else {
151 return NT_STATUS_INVALID_SID;
155 static NTSTATUS dcesrv_unixinfo_GidToSid(struct dcesrv_call_state *dce_call,
156 TALLOC_CTX *mem_ctx,
157 struct unixinfo_GidToSid *r)
159 struct wbc_context *wbc_ctx = talloc_get_type_abort(
160 dce_call->context->private_data,
161 struct wbc_context);
162 struct id_mapping *ids;
163 struct composite_context *ctx;
164 uint32_t gid;
165 NTSTATUS status;
167 DEBUG(5, ("dcesrv_unixinfo_GidToSid called\n"));
169 gid = r->in.gid; /* This cuts gid to 32 bit */
170 if ((uint64_t)gid != r->in.gid) {
171 DEBUG(10, ("gid out of range\n"));
172 return NT_STATUS_INVALID_PARAMETER;
175 ids = talloc(mem_ctx, struct id_mapping);
176 NT_STATUS_HAVE_NO_MEMORY(ids);
178 ids->sid = NULL;
179 ids->status = NT_STATUS_NONE_MAPPED;
180 ids->unixid = talloc(ids, struct unixid);
181 NT_STATUS_HAVE_NO_MEMORY(ids->unixid);
183 ids->unixid->id = gid;
184 ids->unixid->type = ID_TYPE_GID;
186 ctx = wbc_xids_to_sids_send(wbc_ctx, ids, 1, ids);
187 NT_STATUS_HAVE_NO_MEMORY(ctx);
189 status = wbc_xids_to_sids_recv(ctx, &ids);
190 NT_STATUS_NOT_OK_RETURN(status);
192 r->out.sid = ids->sid;
193 return NT_STATUS_OK;
196 static NTSTATUS dcesrv_unixinfo_GetPWUid(struct dcesrv_call_state *dce_call,
197 TALLOC_CTX *mem_ctx,
198 struct unixinfo_GetPWUid *r)
200 int i;
202 *r->out.count = 0;
204 r->out.infos = talloc_zero_array(mem_ctx, struct unixinfo_GetPWUidInfo,
205 *r->in.count);
206 NT_STATUS_HAVE_NO_MEMORY(r->out.infos);
207 *r->out.count = *r->in.count;
209 for (i=0; i < *r->in.count; i++) {
210 uid_t uid;
211 struct passwd *pwd;
213 uid = r->in.uids[i];
214 pwd = getpwuid(uid);
215 if (pwd == NULL) {
216 DEBUG(10, ("uid %d not found\n", uid));
217 r->out.infos[i].homedir = "";
218 r->out.infos[i].shell = "";
219 r->out.infos[i].status = NT_STATUS_NO_SUCH_USER;
220 continue;
223 r->out.infos[i].homedir = talloc_strdup(mem_ctx, pwd->pw_dir);
224 r->out.infos[i].shell = talloc_strdup(mem_ctx, pwd->pw_shell);
226 if ((r->out.infos[i].homedir == NULL) ||
227 (r->out.infos[i].shell == NULL)) {
228 r->out.infos[i].homedir = "";
229 r->out.infos[i].shell = "";
230 r->out.infos[i].status = NT_STATUS_NO_MEMORY;
231 continue;
234 r->out.infos[i].status = NT_STATUS_OK;
237 return NT_STATUS_OK;
240 /* include the generated boilerplate */
241 #include "librpc/gen_ndr/ndr_unixinfo_s.c"