~zulcss/samba/server-dailies-3.4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* 
   Unix SMB/CIFS implementation.

   endpoint server for the unixinfo pipe

   Copyright (C) Volker Lendecke 2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "rpc_server/dcerpc_server.h"
#include "rpc_server/common/common.h"
#include "librpc/gen_ndr/ndr_unixinfo.h"
#include "libcli/wbclient/wbclient.h"
#include "lib/events/events.h"
#include "system/passwd.h"

static NTSTATUS dcerpc_unixinfo_bind(struct dcesrv_call_state *dce_call,
				     const struct dcesrv_interface *iface)
{
	struct wbc_context *wbc_ctx;

	wbc_ctx = wbc_init(dce_call->context, dce_call->msg_ctx,
			   dce_call->event_ctx);
	NT_STATUS_HAVE_NO_MEMORY(wbc_ctx);

	dce_call->context->private_data = wbc_ctx;

	return NT_STATUS_OK;
}

#define DCESRV_INTERFACE_UNIXINFO_BIND dcerpc_unixinfo_bind

static NTSTATUS dcesrv_unixinfo_SidToUid(struct dcesrv_call_state *dce_call,
				  TALLOC_CTX *mem_ctx,
				  struct unixinfo_SidToUid *r)
{
	NTSTATUS status;
	struct wbc_context *wbc_ctx = talloc_get_type_abort(
						dce_call->context->private_data,
						struct wbc_context);
	struct id_mapping *ids;
	struct composite_context *ctx;

	DEBUG(5, ("dcesrv_unixinfo_SidToUid called\n"));

	ids = talloc(mem_ctx, struct  id_mapping);
	NT_STATUS_HAVE_NO_MEMORY(ids);

	ids->sid = &r->in.sid;
	ids->status = NT_STATUS_NONE_MAPPED;
	ids->unixid = NULL;
	ctx = wbc_sids_to_xids_send(wbc_ctx, ids, 1, ids);
	NT_STATUS_HAVE_NO_MEMORY(ctx);

	status = wbc_sids_to_xids_recv(ctx, &ids);
	NT_STATUS_NOT_OK_RETURN(status);

	if (ids->unixid->type == ID_TYPE_BOTH ||
	    ids->unixid->type == ID_TYPE_UID) {
		*r->out.uid = ids->unixid->id;
		return NT_STATUS_OK;
	} else {
		return NT_STATUS_INVALID_SID;
	}
}

static NTSTATUS dcesrv_unixinfo_UidToSid(struct dcesrv_call_state *dce_call,
				  TALLOC_CTX *mem_ctx,
				  struct unixinfo_UidToSid *r)
{
	struct wbc_context *wbc_ctx = talloc_get_type_abort(
						dce_call->context->private_data,
						struct wbc_context);
	struct id_mapping *ids;
	struct composite_context *ctx;
	uint32_t uid;
	NTSTATUS status;

	DEBUG(5, ("dcesrv_unixinfo_UidToSid called\n"));

	uid = r->in.uid; 	/* This cuts uid to 32 bit */
	if ((uint64_t)uid != r->in.uid) {
		DEBUG(10, ("uid out of range\n"));
		return NT_STATUS_INVALID_PARAMETER;
	}

	ids = talloc(mem_ctx, struct id_mapping);
	NT_STATUS_HAVE_NO_MEMORY(ids);

	ids->sid = NULL;
	ids->status = NT_STATUS_NONE_MAPPED;
	ids->unixid = talloc(ids, struct unixid);
	NT_STATUS_HAVE_NO_MEMORY(ids->unixid);

	ids->unixid->id = uid;
	ids->unixid->type = ID_TYPE_UID;

	ctx = wbc_xids_to_sids_send(wbc_ctx, ids, 1, ids);
	NT_STATUS_HAVE_NO_MEMORY(ctx);

	status = wbc_xids_to_sids_recv(ctx, &ids);
	NT_STATUS_NOT_OK_RETURN(status);

	r->out.sid = ids->sid;
	return NT_STATUS_OK;
}

static NTSTATUS dcesrv_unixinfo_SidToGid(struct dcesrv_call_state *dce_call,
				  TALLOC_CTX *mem_ctx,
				  struct unixinfo_SidToGid *r)
{
	NTSTATUS status;
	struct wbc_context *wbc_ctx = talloc_get_type_abort(
						dce_call->context->private_data,
						struct wbc_context);
	struct id_mapping *ids;
	struct composite_context *ctx;

	DEBUG(5, ("dcesrv_unixinfo_SidToGid called\n"));

	ids = talloc(mem_ctx, struct  id_mapping);
	NT_STATUS_HAVE_NO_MEMORY(ids);

	ids->sid = &r->in.sid;
	ids->status = NT_STATUS_NONE_MAPPED;
	ids->unixid = NULL;
	ctx = wbc_sids_to_xids_send(wbc_ctx, ids, 1, ids);
	NT_STATUS_HAVE_NO_MEMORY(ctx);

	status = wbc_sids_to_xids_recv(ctx, &ids);
	NT_STATUS_NOT_OK_RETURN(status);

	if (ids->unixid->type == ID_TYPE_BOTH ||
	    ids->unixid->type == ID_TYPE_GID) {
		*r->out.gid = ids->unixid->id;
		return NT_STATUS_OK;
	} else {
		return NT_STATUS_INVALID_SID;
	}
}

static NTSTATUS dcesrv_unixinfo_GidToSid(struct dcesrv_call_state *dce_call,
				  TALLOC_CTX *mem_ctx,
				  struct unixinfo_GidToSid *r)
{
	struct wbc_context *wbc_ctx = talloc_get_type_abort(
						dce_call->context->private_data,
						struct wbc_context);
	struct id_mapping *ids;
	struct composite_context *ctx;
	uint32_t gid;
	NTSTATUS status;

	DEBUG(5, ("dcesrv_unixinfo_GidToSid called\n"));

	gid = r->in.gid; 	/* This cuts gid to 32 bit */
	if ((uint64_t)gid != r->in.gid) {
		DEBUG(10, ("gid out of range\n"));
		return NT_STATUS_INVALID_PARAMETER;
	}

	ids = talloc(mem_ctx, struct id_mapping);
	NT_STATUS_HAVE_NO_MEMORY(ids);

	ids->sid = NULL;
	ids->status = NT_STATUS_NONE_MAPPED;
	ids->unixid = talloc(ids, struct unixid);
	NT_STATUS_HAVE_NO_MEMORY(ids->unixid);

	ids->unixid->id = gid;
	ids->unixid->type = ID_TYPE_GID;

	ctx = wbc_xids_to_sids_send(wbc_ctx, ids, 1, ids);
	NT_STATUS_HAVE_NO_MEMORY(ctx);

	status = wbc_xids_to_sids_recv(ctx, &ids);
	NT_STATUS_NOT_OK_RETURN(status);

	r->out.sid = ids->sid;
	return NT_STATUS_OK;
}

static NTSTATUS dcesrv_unixinfo_GetPWUid(struct dcesrv_call_state *dce_call,
				  TALLOC_CTX *mem_ctx,
				  struct unixinfo_GetPWUid *r)
{
	int i;

	*r->out.count = 0;

	r->out.infos = talloc_zero_array(mem_ctx, struct unixinfo_GetPWUidInfo,
					 *r->in.count);
	NT_STATUS_HAVE_NO_MEMORY(r->out.infos);
	*r->out.count = *r->in.count;

	for (i=0; i < *r->in.count; i++) {
		uid_t uid;
		struct passwd *pwd;

		uid = r->in.uids[i];
		pwd = getpwuid(uid);
		if (pwd == NULL) {
			DEBUG(10, ("uid %d not found\n", uid));
			r->out.infos[i].homedir = "";
			r->out.infos[i].shell = "";
			r->out.infos[i].status = NT_STATUS_NO_SUCH_USER;
			continue;
		}

		r->out.infos[i].homedir = talloc_strdup(mem_ctx, pwd->pw_dir);
		r->out.infos[i].shell = talloc_strdup(mem_ctx, pwd->pw_shell);

		if ((r->out.infos[i].homedir == NULL) ||
		    (r->out.infos[i].shell == NULL)) {
			r->out.infos[i].homedir = "";
			r->out.infos[i].shell = "";
			r->out.infos[i].status = NT_STATUS_NO_MEMORY;
			continue;
		}

		r->out.infos[i].status = NT_STATUS_OK;
	}

	return NT_STATUS_OK;
}

/* include the generated boilerplate */
#include "librpc/gen_ndr/ndr_unixinfo_s.c"