~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to source4/rpc_server/unixinfo/dcesrv_unixinfo.c

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
30
static NTSTATUS dcerpc_unixinfo_bind(struct dcesrv_call_state *dce_call,
 
31
                                     const struct dcesrv_interface *iface)
 
32
{
 
33
        struct wbc_context *wbc_ctx;
 
34
 
 
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);
 
38
 
 
39
        dce_call->context->private_data = wbc_ctx;
 
40
 
 
41
        return NT_STATUS_OK;
 
42
}
 
43
 
 
44
#define DCESRV_INTERFACE_UNIXINFO_BIND dcerpc_unixinfo_bind
 
45
 
 
46
static NTSTATUS dcesrv_unixinfo_SidToUid(struct dcesrv_call_state *dce_call,
 
47
                                  TALLOC_CTX *mem_ctx,
 
48
                                  struct unixinfo_SidToUid *r)
 
49
{
 
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;
 
56
 
 
57
        DEBUG(5, ("dcesrv_unixinfo_SidToUid called\n"));
 
58
 
 
59
        ids = talloc(mem_ctx, struct  id_mapping);
 
60
        NT_STATUS_HAVE_NO_MEMORY(ids);
 
61
 
 
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);
 
67
 
 
68
        status = wbc_sids_to_xids_recv(ctx, &ids);
 
69
        NT_STATUS_NOT_OK_RETURN(status);
 
70
 
 
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;
 
77
        }
 
78
}
 
79
 
 
80
static NTSTATUS dcesrv_unixinfo_UidToSid(struct dcesrv_call_state *dce_call,
 
81
                                  TALLOC_CTX *mem_ctx,
 
82
                                  struct unixinfo_UidToSid *r)
 
83
{
 
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;
 
91
 
 
92
        DEBUG(5, ("dcesrv_unixinfo_UidToSid called\n"));
 
93
 
 
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;
 
98
        }
 
99
 
 
100
        ids = talloc(mem_ctx, struct id_mapping);
 
101
        NT_STATUS_HAVE_NO_MEMORY(ids);
 
102
 
 
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);
 
107
 
 
108
        ids->unixid->id = uid;
 
109
        ids->unixid->type = ID_TYPE_UID;
 
110
 
 
111
        ctx = wbc_xids_to_sids_send(wbc_ctx, ids, 1, ids);
 
112
        NT_STATUS_HAVE_NO_MEMORY(ctx);
 
113
 
 
114
        status = wbc_xids_to_sids_recv(ctx, &ids);
 
115
        NT_STATUS_NOT_OK_RETURN(status);
 
116
 
 
117
        r->out.sid = ids->sid;
 
118
        return NT_STATUS_OK;
 
119
}
 
120
 
 
121
static NTSTATUS dcesrv_unixinfo_SidToGid(struct dcesrv_call_state *dce_call,
 
122
                                  TALLOC_CTX *mem_ctx,
 
123
                                  struct unixinfo_SidToGid *r)
 
124
{
 
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;
 
131
 
 
132
        DEBUG(5, ("dcesrv_unixinfo_SidToGid called\n"));
 
133
 
 
134
        ids = talloc(mem_ctx, struct  id_mapping);
 
135
        NT_STATUS_HAVE_NO_MEMORY(ids);
 
136
 
 
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);
 
142
 
 
143
        status = wbc_sids_to_xids_recv(ctx, &ids);
 
144
        NT_STATUS_NOT_OK_RETURN(status);
 
145
 
 
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;
 
152
        }
 
153
}
 
154
 
 
155
static NTSTATUS dcesrv_unixinfo_GidToSid(struct dcesrv_call_state *dce_call,
 
156
                                  TALLOC_CTX *mem_ctx,
 
157
                                  struct unixinfo_GidToSid *r)
 
158
{
 
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;
 
166
 
 
167
        DEBUG(5, ("dcesrv_unixinfo_GidToSid called\n"));
 
168
 
 
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;
 
173
        }
 
174
 
 
175
        ids = talloc(mem_ctx, struct id_mapping);
 
176
        NT_STATUS_HAVE_NO_MEMORY(ids);
 
177
 
 
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);
 
182
 
 
183
        ids->unixid->id = gid;
 
184
        ids->unixid->type = ID_TYPE_GID;
 
185
 
 
186
        ctx = wbc_xids_to_sids_send(wbc_ctx, ids, 1, ids);
 
187
        NT_STATUS_HAVE_NO_MEMORY(ctx);
 
188
 
 
189
        status = wbc_xids_to_sids_recv(ctx, &ids);
 
190
        NT_STATUS_NOT_OK_RETURN(status);
 
191
 
 
192
        r->out.sid = ids->sid;
 
193
        return NT_STATUS_OK;
 
194
}
 
195
 
 
196
static NTSTATUS dcesrv_unixinfo_GetPWUid(struct dcesrv_call_state *dce_call,
 
197
                                  TALLOC_CTX *mem_ctx,
 
198
                                  struct unixinfo_GetPWUid *r)
 
199
{
 
200
        int i;
 
201
 
 
202
        *r->out.count = 0;
 
203
 
 
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;
 
208
 
 
209
        for (i=0; i < *r->in.count; i++) {
 
210
                uid_t uid;
 
211
                struct passwd *pwd;
 
212
 
 
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;
 
221
                }
 
222
 
 
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);
 
225
 
 
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;
 
232
                }
 
233
 
 
234
                r->out.infos[i].status = NT_STATUS_OK;
 
235
        }
 
236
 
 
237
        return NT_STATUS_OK;
 
238
}
 
239
 
 
240
/* include the generated boilerplate */
 
241
#include "librpc/gen_ndr/ndr_unixinfo_s.c"