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