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