r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[samba.git] / source3 / 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "rpcclient.h"
23
24 static NTSTATUS cmd_unixinfo_uid2sid(struct rpc_pipe_client *cli,
25                                      TALLOC_CTX *mem_ctx,
26                                      int argc, const char **argv)
27 {
28         uid_t uid;
29         DOM_SID sid;
30         NTSTATUS result;
31
32         if (argc != 2) {
33                 printf("Usage: %s uid\n", argv[0]);
34                 return NT_STATUS_INVALID_PARAMETER;
35         }
36
37         uid = atoi(argv[1]);
38         result = rpccli_unixinfo_UidToSid(cli, mem_ctx, uid, &sid);
39
40         if (!NT_STATUS_IS_OK(result))
41                 goto done;
42
43         printf("%s\n", sid_string_static(&sid));
44
45 done:
46         return result;
47 }
48
49 static NTSTATUS cmd_unixinfo_sid2uid(struct rpc_pipe_client *cli,
50                                      TALLOC_CTX *mem_ctx,
51                                      int argc, const char **argv)
52 {
53         uint64_t uid;
54         DOM_SID sid;
55         NTSTATUS result;
56
57         if (argc != 2) {
58                 printf("Usage: %s sid\n", argv[0]);
59                 return NT_STATUS_INVALID_PARAMETER;
60         }
61
62         if (!string_to_sid(&sid, argv[1])) {
63                 result = NT_STATUS_INVALID_SID;
64                 goto done;
65         }
66
67         result = rpccli_unixinfo_SidToUid(cli, mem_ctx, sid, &uid);
68
69         if (!NT_STATUS_IS_OK(result))
70                 goto done;
71
72         printf("%llu\n", (unsigned long long)uid);
73
74 done:
75         return result;
76 }
77
78 static NTSTATUS cmd_unixinfo_gid2sid(struct rpc_pipe_client *cli,
79                                      TALLOC_CTX *mem_ctx,
80                                      int argc, const char **argv)
81 {
82         gid_t gid;
83         DOM_SID sid;
84         NTSTATUS result;
85
86         if (argc != 2) {
87                 printf("Usage: %s gid\n", argv[0]);
88                 return NT_STATUS_INVALID_PARAMETER;
89         }
90
91         gid = atoi(argv[1]);
92
93         result = rpccli_unixinfo_GidToSid(cli, mem_ctx, gid, &sid);
94
95         if (!NT_STATUS_IS_OK(result))
96                 goto done;
97
98         printf("%s\n", sid_string_static(&sid));
99
100 done:
101         return result;
102 }
103
104 static NTSTATUS cmd_unixinfo_sid2gid(struct rpc_pipe_client *cli,
105                                      TALLOC_CTX *mem_ctx,
106                                      int argc, const char **argv)
107 {
108         uint64_t gid;
109         DOM_SID sid;
110         NTSTATUS result;
111
112         if (argc != 2) {
113                 printf("Usage: %s sid\n", argv[0]);
114                 return NT_STATUS_INVALID_PARAMETER;
115         }
116
117         if (!string_to_sid(&sid, argv[1])) {
118                 result = NT_STATUS_INVALID_SID;
119                 goto done;
120         }
121
122         result = rpccli_unixinfo_SidToGid(cli, mem_ctx, sid, &gid);
123
124         if (!NT_STATUS_IS_OK(result))
125                 goto done;
126
127         printf("%llu\n", (unsigned long long)gid);
128
129 done:
130         return result;
131 }
132
133 static NTSTATUS cmd_unixinfo_getpwuid(struct rpc_pipe_client *cli,
134                                       TALLOC_CTX *mem_ctx,
135                                       int argc, const char **argv)
136 {
137         uint64_t *uids;
138         unsigned int i, num_uids;
139         struct unixinfo_GetPWUidInfo *info;
140         NTSTATUS result;
141
142         if (argc < 2) {
143                 printf("Usage: %s uid [uid2 uid3 ...]\n", argv[0]);
144                 return NT_STATUS_INVALID_PARAMETER;
145         }
146
147         num_uids = argc-1;
148         uids = TALLOC_ARRAY(mem_ctx, uint64_t, num_uids);
149
150         if (uids == NULL) {
151                 return NT_STATUS_NO_MEMORY;
152         }
153
154         for (i=0; i<num_uids; i++) {
155                 uids[i] = atoi(argv[i+1]);
156         }
157
158         info = TALLOC_ARRAY(mem_ctx, struct unixinfo_GetPWUidInfo, num_uids);
159         if (info == NULL) {
160                 return NT_STATUS_NO_MEMORY;
161         }
162
163
164         result = rpccli_unixinfo_GetPWUid(cli, mem_ctx, &num_uids, uids,
165                                           info);
166
167         if (!NT_STATUS_IS_OK(result)) {
168                 return result;
169         }
170
171         for (i=0; i<num_uids; i++) {
172                 if (NT_STATUS_IS_OK(info[i].status)) {
173                         printf("%llu:%s:%s\n", (unsigned long long)uids[i],
174                                info[i].homedir, info[i].shell);
175                 } else {
176                         printf("%llu:%s\n", (unsigned long long)uids[i],
177                                nt_errstr(info[i].status));
178                 }
179         }
180
181         return NT_STATUS_OK;
182 }
183
184 /* List of commands exported by this module */
185
186 struct cmd_set unixinfo_commands[] = {
187
188         { "UNIXINFO" },
189
190         { "uid2sid", RPC_RTYPE_NTSTATUS, cmd_unixinfo_uid2sid, NULL,
191           PI_UNIXINFO, NULL, "Convert a uid to a sid", "" },
192         { "sid2uid", RPC_RTYPE_NTSTATUS, cmd_unixinfo_sid2uid, NULL,
193           PI_UNIXINFO, NULL, "Convert a sid to a uid", "" },
194         { "gid2sid", RPC_RTYPE_NTSTATUS, cmd_unixinfo_gid2sid, NULL,
195           PI_UNIXINFO, NULL, "Convert a gid to a sid", "" },
196         { "sid2gid", RPC_RTYPE_NTSTATUS, cmd_unixinfo_sid2gid, NULL,
197           PI_UNIXINFO, NULL, "Convert a sid to a gid", "" },
198         { "getpwuid", RPC_RTYPE_NTSTATUS, cmd_unixinfo_getpwuid, NULL,
199           PI_UNIXINFO, NULL, "Get passwd info", "" },
200         { NULL }
201 };