r16945: Sync trunk -> 3.0 for 3.0.24 code. Still need
[samba.git] / source3 / 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 2 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, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 /* This is the interface to the rpcunixinfo pipe. */
22
23 #include "includes.h"
24 #include "nterr.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_RPC_SRV
28
29 /* Map a sid to a uid */
30
31 NTSTATUS _unixinfo_sid_to_uid(pipes_struct *p,
32                               UNIXINFO_Q_SID_TO_UID *q_u,
33                               UNIXINFO_R_SID_TO_UID *r_u)
34 {
35         uid_t uid;
36
37         r_u->uid.low = 0;
38         r_u->uid.high = 0;
39
40         r_u->status = sid_to_uid(&q_u->sid, &uid) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
41         if (NT_STATUS_IS_OK(r_u->status))
42                 r_u->uid.low = uid;
43
44         return r_u->status;
45 }
46
47 /* Map a uid to a sid */
48
49 NTSTATUS _unixinfo_uid_to_sid(pipes_struct *p,
50                               UNIXINFO_Q_UID_TO_SID *q_u,
51                               UNIXINFO_R_UID_TO_SID *r_u)
52 {
53         DOM_SID sid;
54
55         r_u->status = NT_STATUS_NO_SUCH_USER;
56
57         if (q_u->uid.high == 0) {
58                 uid_to_sid(&sid, q_u->uid.low);
59                 r_u->status = NT_STATUS_OK;
60         }
61
62         init_r_unixinfo_uid_to_sid(r_u,
63                                 NT_STATUS_IS_OK(r_u->status) ? &sid : NULL);
64
65         return r_u->status;
66 }
67
68 /* Map a sid to a gid */
69
70 NTSTATUS _unixinfo_sid_to_gid(pipes_struct *p,
71                               UNIXINFO_Q_SID_TO_GID *q_u,
72                               UNIXINFO_R_SID_TO_GID *r_u)
73 {
74         gid_t gid;
75
76         r_u->gid.low = 0;
77         r_u->gid.high = 0;
78
79         r_u->status = sid_to_gid(&q_u->sid, &gid) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
80         if (NT_STATUS_IS_OK(r_u->status))
81                 r_u->gid.low = gid;
82
83         return r_u->status;
84 }
85
86 /* Map a gid to a sid */
87
88 NTSTATUS _unixinfo_gid_to_sid(pipes_struct *p,
89                               UNIXINFO_Q_GID_TO_SID *q_u,
90                               UNIXINFO_R_GID_TO_SID *r_u)
91 {
92         DOM_SID sid;
93
94         r_u->status = NT_STATUS_NO_SUCH_USER;
95
96         if (q_u->gid.high == 0) {
97                 gid_to_sid(&sid, q_u->gid.low);
98                 r_u->status = NT_STATUS_OK;
99         }
100
101         init_r_unixinfo_gid_to_sid(r_u,
102                                 NT_STATUS_IS_OK(r_u->status) ? &sid : NULL);
103
104         return r_u->status;
105 }
106
107 /* Get unix struct passwd information */
108
109 NTSTATUS _unixinfo_getpwuid(pipes_struct *p,
110                             UNIXINFO_Q_GETPWUID *q_u,
111                             UNIXINFO_R_GETPWUID *r_u)
112 {
113         int i;
114
115         if (r_u->count > 1023) {
116                 return NT_STATUS_INVALID_PARAMETER;
117         }
118
119         r_u->info = TALLOC_ARRAY(p->mem_ctx, struct unixinfo_getpwuid,
120                                  q_u->count);
121
122         if ((r_u->count > 0) && (r_u->info == NULL)) {
123                 return NT_STATUS_NO_MEMORY;
124         }
125
126         r_u->status = NT_STATUS_OK;
127         r_u->count = q_u->count;
128
129         for (i=0; i<r_u->count; i++) {
130                 struct passwd *pw;
131                 char *homedir, *shell;
132                 ssize_t len1, len2;
133
134                 r_u->info[i].status = NT_STATUS_NO_SUCH_USER;
135                 r_u->info[i].homedir = "";
136                 r_u->info[i].shell = "";
137
138                 if (q_u->uid[i].high != 0) {
139                         DEBUG(10, ("64-bit uids not yet supported...\n"));
140                         continue;
141                 }
142
143                 pw = getpwuid(q_u->uid[i].low);
144
145                 if (pw == NULL) {
146                         DEBUG(10, ("Did not find uid %d\n", q_u->uid[i].low));
147                         continue;
148                 }
149
150                 len1 = push_utf8_talloc(p->mem_ctx, &homedir, pw->pw_dir);
151                 len2 = push_utf8_talloc(p->mem_ctx, &shell, pw->pw_shell);
152
153                 if ((len1 < 0) || (len2 < 0) || (homedir == NULL) ||
154                     (shell == NULL)) {
155                         DEBUG(3, ("push_utf8_talloc failed\n"));
156                         r_u->info[i].status = NT_STATUS_NO_MEMORY;
157                         continue;
158                 }
159
160                 r_u->info[i].status = NT_STATUS_OK;
161                 r_u->info[i].homedir = homedir;
162                 r_u->info[i].shell = shell;
163         }
164
165         return r_u->status;
166 }