r14736: - the ntvfs subsystem should not know about smb_server.h
[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 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 "includes.h"
24 #include "rpc_server/dcerpc_server.h"
25 #include "rpc_server/common/common.h"
26 #include "librpc/gen_ndr/ndr_unixinfo.h"
27 #include "lib/events/events.h"
28 #include "ntvfs/ntvfs.h"
29
30 #include <sys/types.h>
31 #include <pwd.h>
32
33 static NTSTATUS unixinfo_SidToUid(struct dcesrv_call_state *dce_call,
34                                   TALLOC_CTX *mem_ctx,
35                                   struct unixinfo_SidToUid *r)
36 {
37         struct sidmap_context *sidmap;
38         uid_t uid;
39
40         sidmap = sidmap_open(mem_ctx);
41         if (sidmap == NULL) {
42                 DEBUG(10, ("sidmap_open failed\n"));
43                 return NT_STATUS_NO_MEMORY;
44         }
45
46         r->out.result = sidmap_sid_to_unixuid(sidmap, &r->in.sid, &uid);
47
48         if (NT_STATUS_IS_OK(r->out.result)) {
49                 r->out.uid = uid;
50         }
51
52         return r->out.result;
53 }
54
55 static NTSTATUS unixinfo_UidToSid(struct dcesrv_call_state *dce_call,
56                                   TALLOC_CTX *mem_ctx,
57                                   struct unixinfo_UidToSid *r)
58 {
59         struct sidmap_context *sidmap;
60         uid_t uid;
61
62         sidmap = sidmap_open(mem_ctx);
63         if (sidmap == NULL) {
64                 DEBUG(10, ("sidmap_open failed\n"));
65                 return NT_STATUS_NO_MEMORY;
66         }
67
68         uid = r->in.uid;        /* This cuts uid to (probably) 32 bit */
69
70         if ((uint64_t)uid != r->in.uid) {
71                 DEBUG(10, ("uid out of range\n"));
72                 return NT_STATUS_INVALID_PARAMETER;
73         }
74
75         r->out.sid = NULL;
76         r->out.result = sidmap_uid_to_sid(sidmap, mem_ctx, uid, &r->out.sid);
77         return r->out.result;
78 }
79
80 static NTSTATUS unixinfo_SidToGid(struct dcesrv_call_state *dce_call,
81                                   TALLOC_CTX *mem_ctx,
82                                   struct unixinfo_SidToGid *r)
83 {
84         struct sidmap_context *sidmap;
85         gid_t gid;
86
87         sidmap = sidmap_open(mem_ctx);
88         if (sidmap == NULL) {
89                 DEBUG(10, ("sidmap_open failed\n"));
90                 return NT_STATUS_NO_MEMORY;
91         }
92
93         r->out.result = sidmap_sid_to_unixgid(sidmap, &r->in.sid, &gid);
94
95         if (NT_STATUS_IS_OK(r->out.result)) {
96                 r->out.gid = gid;
97         }
98
99         return r->out.result;
100 }
101
102 static NTSTATUS unixinfo_GidToSid(struct dcesrv_call_state *dce_call,
103                                   TALLOC_CTX *mem_ctx,
104                                   struct unixinfo_GidToSid *r)
105 {
106         struct sidmap_context *sidmap;
107         gid_t gid;
108
109         sidmap = sidmap_open(mem_ctx);
110         if (sidmap == NULL) {
111                 DEBUG(10, ("sidmap_open failed\n"));
112                 return NT_STATUS_NO_MEMORY;
113         }
114
115         gid = r->in.gid;        /* This cuts gid to (probably) 32 bit */
116
117         if ((uint64_t)gid != r->in.gid) {
118                 DEBUG(10, ("gid out of range\n"));
119                 return NT_STATUS_INVALID_PARAMETER;
120         }
121
122         r->out.sid = NULL;
123         r->out.result = sidmap_gid_to_sid(sidmap, mem_ctx, gid, &r->out.sid);
124         return r->out.result;
125 }
126
127 static NTSTATUS unixinfo_GetPWUid(struct dcesrv_call_state *dce_call,
128                                   TALLOC_CTX *mem_ctx,
129                                   struct unixinfo_GetPWUid *r)
130 {
131         int i;
132
133         r->out.infos = talloc_zero_array(mem_ctx, struct unixinfo_GetPWUidInfo,
134                                          r->in.count);
135         if (r->out.infos == NULL) {
136                 DEBUG(0, ("talloc failed\n"));
137                 return NT_STATUS_NO_MEMORY;
138         }
139
140         r->out.result = NT_STATUS_OK;
141         r->out.count = r->in.count;
142
143         for (i=0; i<r->in.count; i++) {
144                 uid_t uid;
145                 struct passwd *pwd;
146
147                 uid = r->in.uids[i];
148                 pwd = getpwuid(uid);
149                 if (pwd == NULL) {
150                         DEBUG(10, ("uid %d not found\n", uid));
151                         r->out.infos[i].homedir = "";
152                         r->out.infos[i].shell = "";
153                         r->out.infos[i].status = NT_STATUS_NO_SUCH_USER;
154                         continue;
155                 }
156
157                 r->out.infos[i].homedir = talloc_strdup(mem_ctx, pwd->pw_dir);
158                 r->out.infos[i].shell = talloc_strdup(mem_ctx, pwd->pw_shell);
159
160                 if ((r->out.infos[i].homedir == NULL) ||
161                     (r->out.infos[i].shell == NULL)) {
162                         r->out.infos[i].homedir = "";
163                         r->out.infos[i].shell = "";
164                         r->out.infos[i].status = NT_STATUS_NO_MEMORY;
165                         continue;
166                 }
167
168                 r->out.infos[i].status = NT_STATUS_OK;
169         }
170
171         return NT_STATUS_OK;
172 }
173
174 /* include the generated boilerplate */
175 #include "librpc/gen_ndr/ndr_unixinfo_s.c"