r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[samba.git] / source / rpc_server / srv_unixinfo_nt.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines for unixinfo-pipe
4  *  Copyright (C) Volker Lendecke 2005
5  *  
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /* This is the interface to the rpcunixinfo pipe. */
21
22 #include "includes.h"
23 #include "nterr.h"
24
25
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_RPC_SRV
29
30 /* Map a sid to a uid */
31
32 NTSTATUS _unixinfo_SidToUid(pipes_struct *p, struct unixinfo_SidToUid *r)
33 {
34         uid_t real_uid;
35         NTSTATUS status;
36         *r->out.uid = 0;
37
38         status = sid_to_uid(&r->in.sid, &real_uid) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
39         if (NT_STATUS_IS_OK(status))
40                 *r->out.uid = real_uid;
41
42         return status;
43 }
44
45 /* Map a uid to a sid */
46
47 NTSTATUS _unixinfo_UidToSid(pipes_struct *p, struct unixinfo_UidToSid *r)
48 {
49         NTSTATUS status = NT_STATUS_NO_SUCH_USER;
50
51         uid_to_sid(r->out.sid, (uid_t)r->in.uid);
52         status = NT_STATUS_OK;
53
54         return status;
55 }
56
57 /* Map a sid to a gid */
58
59 NTSTATUS _unixinfo_SidToGid(pipes_struct *p, struct unixinfo_SidToGid *r)
60 {
61         gid_t real_gid;
62         NTSTATUS status;
63
64         *r->out.gid = 0;
65
66         status = sid_to_gid(&r->in.sid, &real_gid) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
67         if (NT_STATUS_IS_OK(status))
68                 *r->out.gid = real_gid;
69
70         return status;
71 }
72
73 /* Map a gid to a sid */
74
75 NTSTATUS _unixinfo_GidToSid(pipes_struct *p, struct unixinfo_GidToSid *r)
76 {
77         NTSTATUS status = NT_STATUS_NO_SUCH_GROUP;
78
79         gid_to_sid(r->out.sid, (gid_t)r->in.gid);
80         status = NT_STATUS_OK;
81
82         return status;
83 }
84
85 /* Get unix struct passwd information */
86
87 NTSTATUS _unixinfo_GetPWUid(pipes_struct *p, struct unixinfo_GetPWUid *r)
88 {
89         int i;
90         NTSTATUS status;
91
92         if (*r->in.count > 1023)
93                 return NT_STATUS_INVALID_PARAMETER;
94
95         status = NT_STATUS_OK;
96
97         for (i=0; i<*r->in.count; i++) {
98                 struct passwd *pw;
99                 char *homedir, *shell;
100                 ssize_t len1, len2;
101
102                 r->out.infos[i].status = NT_STATUS_NO_SUCH_USER;
103                 r->out.infos[i].homedir = "";
104                 r->out.infos[i].shell = "";
105
106                 pw = getpwuid(r->in.uids[i]);
107
108                 if (pw == NULL) {
109                         DEBUG(10, ("Did not find uid %lld\n",
110                                    (long long int)r->in.uids[i]));
111                         continue;
112                 }
113
114                 len1 = push_utf8_talloc(p->mem_ctx, &homedir, pw->pw_dir);
115                 len2 = push_utf8_talloc(p->mem_ctx, &shell, pw->pw_shell);
116
117                 if ((len1 < 0) || (len2 < 0) || (homedir == NULL) ||
118                     (shell == NULL)) {
119                         DEBUG(3, ("push_utf8_talloc failed\n"));
120                         r->out.infos[i].status = NT_STATUS_NO_MEMORY;
121                         continue;
122                 }
123
124                 r->out.infos[i].status = NT_STATUS_OK;
125                 r->out.infos[i].homedir = homedir;
126                 r->out.infos[i].shell = shell;
127         }
128
129         return status;
130 }