r23779: Change from v2 or later to v3 or later.
[sfrench/samba-autobuild/.git] / source / rpcclient / cmd_unixinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4
5    Copyright (C) Volker Lendecke 2005
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "rpcclient.h"
24
25 static NTSTATUS cmd_unixinfo_uid2sid(struct rpc_pipe_client *cli,
26                                      TALLOC_CTX *mem_ctx,
27                                      int argc, const char **argv)
28 {
29         uid_t uid;
30         DOM_SID sid;
31         NTSTATUS result;
32
33         if (argc != 2) {
34                 printf("Usage: %s uid\n", argv[0]);
35                 return NT_STATUS_INVALID_PARAMETER;
36         }
37
38         uid = atoi(argv[1]);
39         result = rpccli_unixinfo_UidToSid(cli, mem_ctx, uid, &sid);
40
41         if (!NT_STATUS_IS_OK(result))
42                 goto done;
43
44         printf("%s\n", sid_string_static(&sid));
45
46 done:
47         return result;
48 }
49
50 static NTSTATUS cmd_unixinfo_sid2uid(struct rpc_pipe_client *cli,
51                                      TALLOC_CTX *mem_ctx,
52                                      int argc, const char **argv)
53 {
54         uint64_t uid;
55         DOM_SID sid;
56         NTSTATUS result;
57
58         if (argc != 2) {
59                 printf("Usage: %s sid\n", argv[0]);
60                 return NT_STATUS_INVALID_PARAMETER;
61         }
62
63         if (!string_to_sid(&sid, argv[1])) {
64                 result = NT_STATUS_INVALID_SID;
65                 goto done;
66         }
67
68         result = rpccli_unixinfo_SidToUid(cli, mem_ctx, sid, &uid);
69
70         if (!NT_STATUS_IS_OK(result))
71                 goto done;
72
73         printf("%llu\n", (unsigned long long)uid);
74
75 done:
76         return result;
77 }
78
79 static NTSTATUS cmd_unixinfo_gid2sid(struct rpc_pipe_client *cli,
80                                      TALLOC_CTX *mem_ctx,
81                                      int argc, const char **argv)
82 {
83         gid_t gid;
84         DOM_SID sid;
85         NTSTATUS result;
86
87         if (argc != 2) {
88                 printf("Usage: %s gid\n", argv[0]);
89                 return NT_STATUS_INVALID_PARAMETER;
90         }
91
92         gid = atoi(argv[1]);
93
94         result = rpccli_unixinfo_GidToSid(cli, mem_ctx, gid, &sid);
95
96         if (!NT_STATUS_IS_OK(result))
97                 goto done;
98
99         printf("%s\n", sid_string_static(&sid));
100
101 done:
102         return result;
103 }
104
105 static NTSTATUS cmd_unixinfo_sid2gid(struct rpc_pipe_client *cli,
106                                      TALLOC_CTX *mem_ctx,
107                                      int argc, const char **argv)
108 {
109         uint64_t gid;
110         DOM_SID sid;
111         NTSTATUS result;
112
113         if (argc != 2) {
114                 printf("Usage: %s sid\n", argv[0]);
115                 return NT_STATUS_INVALID_PARAMETER;
116         }
117
118         if (!string_to_sid(&sid, argv[1])) {
119                 result = NT_STATUS_INVALID_SID;
120                 goto done;
121         }
122
123         result = rpccli_unixinfo_SidToGid(cli, mem_ctx, sid, &gid);
124
125         if (!NT_STATUS_IS_OK(result))
126                 goto done;
127
128         printf("%llu\n", (unsigned long long)gid);
129
130 done:
131         return result;
132 }
133
134 static NTSTATUS cmd_unixinfo_getpwuid(struct rpc_pipe_client *cli,
135                                       TALLOC_CTX *mem_ctx,
136                                       int argc, const char **argv)
137 {
138         uint64_t *uids;
139         unsigned int i, num_uids;
140         struct unixinfo_GetPWUidInfo *info;
141         NTSTATUS result;
142
143         if (argc < 2) {
144                 printf("Usage: %s uid [uid2 uid3 ...]\n", argv[0]);
145                 return NT_STATUS_INVALID_PARAMETER;
146         }
147
148         num_uids = argc-1;
149         uids = TALLOC_ARRAY(mem_ctx, uint64_t, num_uids);
150
151         if (uids == NULL) {
152                 return NT_STATUS_NO_MEMORY;
153         }
154
155         for (i=0; i<num_uids; i++) {
156                 uids[i] = atoi(argv[i+1]);
157         }
158
159         info = TALLOC_ARRAY(mem_ctx, struct unixinfo_GetPWUidInfo, num_uids);
160         if (info == NULL) {
161                 return NT_STATUS_NO_MEMORY;
162         }
163
164
165         result = rpccli_unixinfo_GetPWUid(cli, mem_ctx, &num_uids, uids,
166                                           info);
167
168         if (!NT_STATUS_IS_OK(result)) {
169                 return result;
170         }
171
172         for (i=0; i<num_uids; i++) {
173                 if (NT_STATUS_IS_OK(info[i].status)) {
174                         printf("%llu:%s:%s\n", (unsigned long long)uids[i],
175                                info[i].homedir, info[i].shell);
176                 } else {
177                         printf("%llu:%s\n", (unsigned long long)uids[i],
178                                nt_errstr(info[i].status));
179                 }
180         }
181
182         return NT_STATUS_OK;
183 }
184
185 /* List of commands exported by this module */
186
187 struct cmd_set unixinfo_commands[] = {
188
189         { "UNIXINFO" },
190
191         { "uid2sid", RPC_RTYPE_NTSTATUS, cmd_unixinfo_uid2sid, NULL,
192           PI_UNIXINFO, NULL, "Convert a uid to a sid", "" },
193         { "sid2uid", RPC_RTYPE_NTSTATUS, cmd_unixinfo_sid2uid, NULL,
194           PI_UNIXINFO, NULL, "Convert a sid to a uid", "" },
195         { "gid2sid", RPC_RTYPE_NTSTATUS, cmd_unixinfo_gid2sid, NULL,
196           PI_UNIXINFO, NULL, "Convert a gid to a sid", "" },
197         { "sid2gid", RPC_RTYPE_NTSTATUS, cmd_unixinfo_sid2gid, NULL,
198           PI_UNIXINFO, NULL, "Convert a sid to a gid", "" },
199         { "getpwuid", RPC_RTYPE_NTSTATUS, cmd_unixinfo_getpwuid, NULL,
200           PI_UNIXINFO, NULL, "Get passwd info", "" },
201         { NULL }
202 };