r8839: adding patches from Marcin. Have to still work on
[Samba.git] / source / rpc_server / srv_unixinfo_nt.c
blob0640c5721c4aa58ad23bf8bf6be2d5b3091ff0a5
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.
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.
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.
21 /* This is the interface to the rpcunixinfo pipe. */
23 #include "includes.h"
24 #include "nterr.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_RPC_SRV
29 /* Map a sid to a uid */
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)
35 uid_t uid;
37 r_u->uid.low = 0;
38 r_u->uid.high = 0;
40 r_u->status = sid_to_uid(&q_u->sid, &uid);
41 if (NT_STATUS_IS_OK(r_u->status))
42 r_u->uid.low = uid;
44 return r_u->status;
47 /* Map a uid to a sid */
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)
53 DOM_SID sid;
55 r_u->status = NT_STATUS_NO_SUCH_USER;
57 if (q_u->uid.high == 0) {
58 r_u->status = uid_to_sid(&sid, q_u->uid.low);
61 init_r_unixinfo_uid_to_sid(r_u,
62 NT_STATUS_IS_OK(r_u->status) ? &sid : NULL);
64 return r_u->status;
67 /* Map a sid to a gid */
69 NTSTATUS _unixinfo_sid_to_gid(pipes_struct *p,
70 UNIXINFO_Q_SID_TO_GID *q_u,
71 UNIXINFO_R_SID_TO_GID *r_u)
73 gid_t gid;
75 r_u->gid.low = 0;
76 r_u->gid.high = 0;
78 r_u->status = sid_to_gid(&q_u->sid, &gid);
79 if (NT_STATUS_IS_OK(r_u->status))
80 r_u->gid.low = gid;
82 return r_u->status;
85 /* Map a gid to a sid */
87 NTSTATUS _unixinfo_gid_to_sid(pipes_struct *p,
88 UNIXINFO_Q_GID_TO_SID *q_u,
89 UNIXINFO_R_GID_TO_SID *r_u)
91 DOM_SID sid;
93 r_u->status = NT_STATUS_NO_SUCH_USER;
95 if (q_u->gid.high == 0) {
96 r_u->status = gid_to_sid(&sid, q_u->gid.low);
99 init_r_unixinfo_gid_to_sid(r_u,
100 NT_STATUS_IS_OK(r_u->status) ? &sid : NULL);
102 return r_u->status;
105 /* Get unix struct passwd information */
107 NTSTATUS _unixinfo_getpwuid(pipes_struct *p,
108 UNIXINFO_Q_GETPWUID *q_u,
109 UNIXINFO_R_GETPWUID *r_u)
111 int i;
113 if (r_u->count > 1023) {
114 return NT_STATUS_INVALID_PARAMETER;
117 r_u->info = TALLOC_ARRAY(p->mem_ctx, struct unixinfo_getpwuid,
118 q_u->count);
120 if ((r_u->count > 0) && (r_u->info == NULL)) {
121 return NT_STATUS_NO_MEMORY;
124 r_u->status = NT_STATUS_OK;
125 r_u->count = q_u->count;
127 for (i=0; i<r_u->count; i++) {
128 struct passwd *pw;
129 char *homedir, *shell;
130 ssize_t len1, len2;
132 r_u->info[i].status = NT_STATUS_NO_SUCH_USER;
133 r_u->info[i].homedir = "";
134 r_u->info[i].shell = "";
136 if (q_u->uid[i].high != 0) {
137 DEBUG(10, ("64-bit uids not yet supported...\n"));
138 continue;
141 pw = getpwuid(q_u->uid[i].low);
143 if (pw == NULL) {
144 DEBUG(10, ("Did not find uid %d\n", q_u->uid[i].low));
145 continue;
148 len1 = push_utf8_talloc(p->mem_ctx, &homedir, pw->pw_dir);
149 len2 = push_utf8_talloc(p->mem_ctx, &shell, pw->pw_shell);
151 if ((len1 < 0) || (len2 < 0) || (homedir == NULL) ||
152 (shell == NULL)) {
153 DEBUG(3, ("push_utf8_talloc failed\n"));
154 r_u->info[i].status = NT_STATUS_NO_MEMORY;
155 continue;
158 r_u->info[i].status = NT_STATUS_OK;
159 r_u->info[i].homedir = homedir;
160 r_u->info[i].shell = shell;
163 return r_u->status;