2 Unix SMB/CIFS implementation.
5 Copyright (C) Tim Potter 2000
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 2 of the License, or
10 (at your option) any later version.
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.
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.
23 #include "rpcclient.h"
25 static NTSTATUS cmd_netlogon_logon_ctrl2(struct rpc_pipe_client *cli,
26 TALLOC_CTX *mem_ctx, int argc,
29 uint32 query_level = 1;
30 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
33 fprintf(stderr, "Usage: %s\n", argv[0]);
37 result = rpccli_netlogon_logon_ctrl2(cli, mem_ctx, query_level);
39 if (!NT_STATUS_IS_OK(result))
48 static WERROR cmd_netlogon_getdcname(struct rpc_pipe_client *cli,
49 TALLOC_CTX *mem_ctx, int argc,
53 WERROR result = WERR_GENERAL_FAILURE;
57 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
61 /* Make sure to wait for our DC's reply */
62 old_timeout = cli_set_timeout(cli->cli, MAX(cli->cli->timeout,30000)); /* 30 seconds. */
64 result = rpccli_netlogon_getdcname(cli, mem_ctx, cli->cli->desthost, argv[1], dcname);
66 cli_set_timeout(cli->cli, old_timeout);
68 if (!W_ERROR_IS_OK(result))
73 printf("%s\n", dcname);
79 static WERROR cmd_netlogon_getanydcname(struct rpc_pipe_client *cli,
80 TALLOC_CTX *mem_ctx, int argc,
84 WERROR result = WERR_GENERAL_FAILURE;
88 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
92 /* Make sure to wait for our DC's reply */
93 old_timeout = cli_set_timeout(cli->cli, MAX(cli->cli->timeout,30000)); /* 30 seconds. */
95 result = rpccli_netlogon_getanydcname(cli, mem_ctx, cli->cli->desthost, argv[1], dcname);
97 cli_set_timeout(cli->cli, old_timeout);
99 if (!W_ERROR_IS_OK(result))
102 /* Display results */
104 printf("%s\n", dcname);
110 static WERROR cmd_netlogon_dsr_getdcname(struct rpc_pipe_client *cli,
111 TALLOC_CTX *mem_ctx, int argc,
115 char *dcname, *dcaddress;
118 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
122 result = rpccli_netlogon_dsr_getdcname(
123 cli, mem_ctx, cli->cli->desthost, argv[1], NULL, NULL,
124 0x40000000, &dcname, &dcaddress, NULL, NULL, NULL, NULL,
127 if (W_ERROR_IS_OK(result)) {
128 printf("Domain %s's DC is called %s at IP %s\n",
129 argv[1], dcname, dcaddress);
133 printf("rpccli_netlogon_dsr_getdcname returned %s\n",
134 nt_errstr(werror_to_ntstatus(result)));
139 static WERROR cmd_netlogon_dsr_getsitename(struct rpc_pipe_client *cli,
140 TALLOC_CTX *mem_ctx, int argc,
147 fprintf(stderr, "Usage: %s computername\n", argv[0]);
151 result = rpccli_netlogon_dsr_getsitename(cli, mem_ctx, argv[1], &sitename);
153 if (!W_ERROR_IS_OK(result)) {
154 printf("rpccli_netlogon_dsr_gesitename returned %s\n",
155 nt_errstr(werror_to_ntstatus(result)));
159 printf("Computer %s is on Site: %s\n", argv[1], sitename);
164 static NTSTATUS cmd_netlogon_logon_ctrl(struct rpc_pipe_client *cli,
165 TALLOC_CTX *mem_ctx, int argc,
169 uint32 query_level = 1;
171 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
174 fprintf(stderr, "Usage: %s\n", argv[0]);
179 result = cli_netlogon_logon_ctrl(cli, mem_ctx, query_level);
180 if (!NT_STATUS_IS_OK(result)) {
185 /* Display results */
190 /* Display sam synchronisation information */
192 static void display_sam_sync(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas,
193 SAM_DELTA_CTR *deltas)
198 for (i = 0; i < num_deltas; i++) {
199 switch (hdr_deltas[i].type) {
200 case SAM_DELTA_DOMAIN_INFO:
201 unistr2_to_ascii(name,
202 &deltas[i].domain_info.uni_dom_name,
204 printf("Domain: %s\n", name);
206 case SAM_DELTA_GROUP_INFO:
207 unistr2_to_ascii(name,
208 &deltas[i].group_info.uni_grp_name,
210 printf("Group: %s\n", name);
212 case SAM_DELTA_ACCOUNT_INFO:
213 unistr2_to_ascii(name,
214 &deltas[i].account_info.uni_acct_name,
216 printf("Account: %s\n", name);
218 case SAM_DELTA_ALIAS_INFO:
219 unistr2_to_ascii(name,
220 &deltas[i].alias_info.uni_als_name,
222 printf("Alias: %s\n", name);
224 case SAM_DELTA_ALIAS_MEM: {
225 SAM_ALIAS_MEM_INFO *alias = &deltas[i].als_mem_info;
227 for (j = 0; j < alias->num_members; j++) {
230 sid_to_string(sid_str, &alias->sids[j].sid);
232 printf("%s\n", sid_str);
236 case SAM_DELTA_GROUP_MEM: {
237 SAM_GROUP_MEM_INFO *group = &deltas[i].grp_mem_info;
239 for (j = 0; j < group->num_members; j++)
240 printf("rid 0x%x, attrib 0x%08x\n",
241 group->rids[j], group->attribs[j]);
244 case SAM_DELTA_MODIFIED_COUNT: {
245 SAM_DELTA_MOD_COUNT *mc = &deltas[i].mod_count;
247 printf("sam sequence update: 0x%04x\n", mc->seqnum);
251 printf("unknown delta type 0x%02x\n",
258 /* Perform sam synchronisation */
260 static NTSTATUS cmd_netlogon_sam_sync(struct rpc_pipe_client *cli,
261 TALLOC_CTX *mem_ctx, int argc,
264 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
265 uint32 database_id = 0, num_deltas;
266 SAM_DELTA_HDR *hdr_deltas;
267 SAM_DELTA_CTR *deltas;
270 fprintf(stderr, "Usage: %s [database_id]\n", argv[0]);
275 database_id = atoi(argv[1]);
277 /* Synchronise sam database */
279 result = rpccli_netlogon_sam_sync(cli, mem_ctx, database_id,
280 0, &num_deltas, &hdr_deltas, &deltas);
282 if (!NT_STATUS_IS_OK(result))
285 /* Display results */
287 display_sam_sync(num_deltas, hdr_deltas, deltas);
293 /* Perform sam delta synchronisation */
295 static NTSTATUS cmd_netlogon_sam_deltas(struct rpc_pipe_client *cli,
296 TALLOC_CTX *mem_ctx, int argc,
299 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
300 uint32 database_id, num_deltas, tmp;
301 SAM_DELTA_HDR *hdr_deltas;
302 SAM_DELTA_CTR *deltas;
306 fprintf(stderr, "Usage: %s database_id seqnum\n", argv[0]);
310 database_id = atoi(argv[1]);
313 seqnum = tmp & 0xffff;
315 result = rpccli_netlogon_sam_deltas(cli, mem_ctx, database_id,
317 &hdr_deltas, &deltas);
319 if (!NT_STATUS_IS_OK(result))
322 /* Display results */
324 display_sam_sync(num_deltas, hdr_deltas, deltas);
330 /* Log on a domain user */
332 static NTSTATUS cmd_netlogon_sam_logon(struct rpc_pipe_client *cli,
333 TALLOC_CTX *mem_ctx, int argc,
336 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
337 int logon_type = NET_LOGON_TYPE;
338 const char *username, *password;
340 uint32 logon_param = 0;
341 const char *workstation = NULL;
343 /* Check arguments */
345 if (argc < 3 || argc > 7) {
346 fprintf(stderr, "Usage: samlogon <username> <password> [workstation]"
347 "[logon_type (1 or 2)] [auth level (2 or 3)] [logon_parameter]\n");
355 workstation = argv[3];
358 sscanf(argv[4], "%i", &logon_type);
361 sscanf(argv[5], "%i", &auth_level);
364 sscanf(argv[6], "%x", &logon_param);
366 /* Perform the sam logon */
368 result = rpccli_netlogon_sam_logon(cli, mem_ctx, logon_param, lp_workgroup(), username, password, workstation, logon_type);
370 if (!NT_STATUS_IS_OK(result))
377 /* Change the trust account password */
379 static NTSTATUS cmd_netlogon_change_trust_pw(struct rpc_pipe_client *cli,
380 TALLOC_CTX *mem_ctx, int argc,
383 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
385 /* Check arguments */
388 fprintf(stderr, "Usage: change_trust_pw");
392 /* Perform the sam logon */
394 result = trust_pw_find_change_and_store_it(cli, mem_ctx,
397 if (!NT_STATUS_IS_OK(result))
405 /* List of commands exported by this module */
407 struct cmd_set netlogon_commands[] = {
411 { "logonctrl2", RPC_RTYPE_NTSTATUS, cmd_netlogon_logon_ctrl2, NULL, PI_NETLOGON, NULL, "Logon Control 2", "" },
412 { "getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcname, PI_NETLOGON, NULL, "Get trusted PDC name", "" },
413 { "getanydcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getanydcname, PI_NETLOGON, NULL, "Get trusted DC name", "" },
414 { "dsr_getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcname, PI_NETLOGON, NULL, "Get trusted DC name", "" },
415 { "dsr_getsitename", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getsitename, PI_NETLOGON, NULL, "Get sitename", "" },
416 { "logonctrl", RPC_RTYPE_NTSTATUS, cmd_netlogon_logon_ctrl, NULL, PI_NETLOGON, NULL, "Logon Control", "" },
417 { "samsync", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_sync, NULL, PI_NETLOGON, NULL, "Sam Synchronisation", "" },
418 { "samdeltas", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_deltas, NULL, PI_NETLOGON, NULL, "Query Sam Deltas", "" },
419 { "samlogon", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_logon, NULL, PI_NETLOGON, NULL, "Sam Logon", "" },
420 { "change_trust_pw", RPC_RTYPE_NTSTATUS, cmd_netlogon_change_trust_pw, NULL, PI_NETLOGON, NULL, "Change Trust Account Password", "" },