summaryrefslogtreecommitdiff
path: root/source3/rpcclient/cmd_unixinfo.c
blob: 1725ce8bfe772f79dc773aa8757d7fdbc9922148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* 
   Unix SMB/CIFS implementation.
   RPC pipe client

   Copyright (C) Volker Lendecke 2005

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "rpcclient.h"

static NTSTATUS cmd_unixinfo_uid2sid(struct rpc_pipe_client *cli,
				     TALLOC_CTX *mem_ctx,
				     int argc, const char **argv)
{
	uid_t uid;
	DOM_SID sid;
	NTSTATUS result;

	if (argc != 2) {
		printf("Usage: %s uid\n", argv[0]);
		return NT_STATUS_INVALID_PARAMETER;
	}

	uid = atoi(argv[1]);
	result = rpccli_unixinfo_UidToSid(cli, mem_ctx, uid, &sid);

	if (!NT_STATUS_IS_OK(result))
		goto done;

	printf("%s\n", sid_string_static(&sid));

done:
	return result;
}

static NTSTATUS cmd_unixinfo_sid2uid(struct rpc_pipe_client *cli,
				     TALLOC_CTX *mem_ctx,
				     int argc, const char **argv)
{
	uint64_t uid;
	DOM_SID sid;
	NTSTATUS result;

	if (argc != 2) {
		printf("Usage: %s sid\n", argv[0]);
		return NT_STATUS_INVALID_PARAMETER;
	}

	if (!string_to_sid(&sid, argv[1])) {
		result = NT_STATUS_INVALID_SID;
		goto done;
	}

	result = rpccli_unixinfo_SidToUid(cli, mem_ctx, sid, &uid);

	if (!NT_STATUS_IS_OK(result))
		goto done;

	printf("%llu\n", (unsigned long long)uid);

done:
	return result;
}

static NTSTATUS cmd_unixinfo_gid2sid(struct rpc_pipe_client *cli,
				     TALLOC_CTX *mem_ctx,
				     int argc, const char **argv)
{
	gid_t gid;
	DOM_SID sid;
	NTSTATUS result;

	if (argc != 2) {
		printf("Usage: %s gid\n", argv[0]);
		return NT_STATUS_INVALID_PARAMETER;
	}

	gid = atoi(argv[1]);

	result = rpccli_unixinfo_GidToSid(cli, mem_ctx, gid, &sid);

	if (!NT_STATUS_IS_OK(result))
		goto done;

	printf("%s\n", sid_string_static(&sid));

done:
	return result;
}

static NTSTATUS cmd_unixinfo_sid2gid(struct rpc_pipe_client *cli,
				     TALLOC_CTX *mem_ctx,
				     int argc, const char **argv)
{
	uint64_t gid;
	DOM_SID sid;
	NTSTATUS result;

	if (argc != 2) {
		printf("Usage: %s sid\n", argv[0]);
		return NT_STATUS_INVALID_PARAMETER;
	}

	if (!string_to_sid(&sid, argv[1])) {
		result = NT_STATUS_INVALID_SID;
		goto done;
	}

	result = rpccli_unixinfo_SidToGid(cli, mem_ctx, sid, &gid);

	if (!NT_STATUS_IS_OK(result))
		goto done;

	printf("%llu\n", (unsigned long long)gid);

done:
	return result;
}

static NTSTATUS cmd_unixinfo_getpwuid(struct rpc_pipe_client *cli,
				      TALLOC_CTX *mem_ctx,
				      int argc, const char **argv)
{
	uint64_t *uids;
	unsigned int i, num_uids;
	struct unixinfo_GetPWUidInfo *info;
	NTSTATUS result;

	if (argc < 2) {
		printf("Usage: %s uid [uid2 uid3 ...]\n", argv[0]);
		return NT_STATUS_INVALID_PARAMETER;
	}

	num_uids = argc-1;
	uids = TALLOC_ARRAY(mem_ctx, uint64_t, num_uids);

	if (uids == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	for (i=0; i<num_uids; i++) {
		uids[i] = atoi(argv[i+1]);
	}

	info = TALLOC_ARRAY(mem_ctx, struct unixinfo_GetPWUidInfo, num_uids);
	if (info == NULL) {
		return NT_STATUS_NO_MEMORY;
	}


	result = rpccli_unixinfo_GetPWUid(cli, mem_ctx, &num_uids, uids,
					  info);

	if (!NT_STATUS_IS_OK(result)) {
		return result;
	}

	for (i=0; i<num_uids; i++) {
		if (NT_STATUS_IS_OK(info[i].status)) {
			printf("%llu:%s:%s\n", (unsigned long long)uids[i],
			       info[i].homedir, info[i].shell);
		} else {
			printf("%llu:%s\n", (unsigned long long)uids[i],
			       nt_errstr(info[i].status));
		}
	}

	return NT_STATUS_OK;
}

/* List of commands exported by this module */

struct cmd_set unixinfo_commands[] = {

	{ "UNIXINFO" },

	{ "uid2sid", RPC_RTYPE_NTSTATUS, cmd_unixinfo_uid2sid, NULL,
	  PI_UNIXINFO, NULL, "Convert a uid to a sid", "" },
	{ "sid2uid", RPC_RTYPE_NTSTATUS, cmd_unixinfo_sid2uid, NULL,
	  PI_UNIXINFO, NULL, "Convert a sid to a uid", "" },
	{ "gid2sid", RPC_RTYPE_NTSTATUS, cmd_unixinfo_gid2sid, NULL,
	  PI_UNIXINFO, NULL, "Convert a gid to a sid", "" },
	{ "sid2gid", RPC_RTYPE_NTSTATUS, cmd_unixinfo_sid2gid, NULL,
	  PI_UNIXINFO, NULL, "Convert a sid to a gid", "" },
	{ "getpwuid", RPC_RTYPE_NTSTATUS, cmd_unixinfo_getpwuid, NULL,
	  PI_UNIXINFO, NULL, "Get passwd info", "" },
	{ NULL }
};