2 Unix SMB/Netbios implementation.
6 Copyright (C) Tim Potter 2000
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "rpcclient.h"
26 static NTSTATUS
cmd_netlogon_logon_ctrl2(struct cli_state
*cli
,
27 TALLOC_CTX
*mem_ctx
, int argc
,
30 uint32 query_level
= 1;
31 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
34 fprintf(stderr
, "Usage: %s\n", argv
[0]);
38 result
= cli_netlogon_logon_ctrl2(cli
, mem_ctx
, query_level
);
40 if (!NT_STATUS_IS_OK(result
))
49 static NTSTATUS
cmd_netlogon_logon_ctrl(struct cli_state
*cli
,
50 TALLOC_CTX
*mem_ctx
, int argc
,
54 uint32 query_level
= 1;
56 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
59 fprintf(stderr
, "Usage: %s\n", argv
[0]);
64 result
= cli_netlogon_logon_ctrl(cli
, mem_ctx
, query_level
);
65 if (!NT_STATUS_IS_OK(result
)) {
75 /* Display sam synchronisation information */
77 static void display_sam_sync(uint32 num_deltas
, SAM_DELTA_HDR
*hdr_deltas
,
78 SAM_DELTA_CTR
*deltas
)
83 for (i
= 0; i
< num_deltas
; i
++) {
84 switch (hdr_deltas
[i
].type
) {
85 case SAM_DELTA_DOMAIN_INFO
:
86 unistr2_to_ascii(name
,
87 &deltas
[i
].domain_info
.uni_dom_name
,
89 printf("Domain: %s\n", name
);
91 case SAM_DELTA_GROUP_INFO
:
92 unistr2_to_ascii(name
,
93 &deltas
[i
].group_info
.uni_grp_name
,
95 printf("Group: %s\n", name
);
97 case SAM_DELTA_ACCOUNT_INFO
:
98 unistr2_to_ascii(name
,
99 &deltas
[i
].account_info
.uni_acct_name
,
101 printf("Account: %s\n", name
);
103 case SAM_DELTA_ALIAS_INFO
:
104 unistr2_to_ascii(name
,
105 &deltas
[i
].alias_info
.uni_als_name
,
107 printf("Alias: %s\n", name
);
109 case SAM_DELTA_ALIAS_MEM
: {
110 SAM_ALIAS_MEM_INFO
*alias
= &deltas
[i
].als_mem_info
;
112 for (j
= 0; j
< alias
->num_members
; j
++) {
115 sid_to_string(sid_str
, &alias
->sids
[j
].sid
);
117 printf("%s\n", sid_str
);
121 case SAM_DELTA_GROUP_MEM
: {
122 SAM_GROUP_MEM_INFO
*group
= &deltas
[i
].grp_mem_info
;
124 for (j
= 0; j
< group
->num_members
; j
++)
125 printf("rid 0x%x, attrib 0x%08x\n",
126 group
->rids
[j
], group
->attribs
[j
]);
129 case SAM_DELTA_SAM_STAMP
: {
130 SAM_DELTA_STAMP
*stamp
= &deltas
[i
].stamp
;
132 printf("sam sequence update: 0x%04x\n",
137 printf("unknown delta type 0x%02x\n",
144 /* Perform sam synchronisation */
146 static NTSTATUS
cmd_netlogon_sam_sync(struct cli_state
*cli
,
147 TALLOC_CTX
*mem_ctx
, int argc
,
150 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
151 unsigned char trust_passwd
[16];
152 uint32 database_id
= 0, num_deltas
;
153 SAM_DELTA_HDR
*hdr_deltas
;
154 SAM_DELTA_CTR
*deltas
;
157 fprintf(stderr
, "Usage: %s [database_id]\n", argv
[0]);
162 database_id
= atoi(argv
[1]);
164 if (!secrets_init()) {
165 fprintf(stderr
, "Unable to initialise secrets database\n");
169 /* Initialise session credentials */
171 if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd
,
173 fprintf(stderr
, "could not fetch trust account password\n");
177 result
= cli_nt_setup_creds(cli
, trust_passwd
);
179 if (!NT_STATUS_IS_OK(result
)) {
180 fprintf(stderr
, "Error initialising session creds\n");
184 /* Synchronise sam database */
186 result
= cli_netlogon_sam_sync(cli
, mem_ctx
, database_id
,
187 &num_deltas
, &hdr_deltas
, &deltas
);
189 if (!NT_STATUS_IS_OK(result
))
192 /* Display results */
194 display_sam_sync(num_deltas
, hdr_deltas
, deltas
);
200 /* Perform sam delta synchronisation */
202 static NTSTATUS
cmd_netlogon_sam_deltas(struct cli_state
*cli
,
203 TALLOC_CTX
*mem_ctx
, int argc
,
206 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
207 unsigned char trust_passwd
[16];
208 uint32 database_id
, num_deltas
, tmp
;
209 SAM_DELTA_HDR
*hdr_deltas
;
210 SAM_DELTA_CTR
*deltas
;
214 fprintf(stderr
, "Usage: %s database_id seqnum\n", argv
[0]);
218 database_id
= atoi(argv
[1]);
221 seqnum
.low
= tmp
& 0xffff;
224 if (!secrets_init()) {
225 fprintf(stderr
, "Unable to initialise secrets database\n");
229 /* Initialise session credentials */
231 if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd
,
233 fprintf(stderr
, "could not fetch trust account password\n");
237 result
= cli_nt_setup_creds(cli
, trust_passwd
);
239 if (!NT_STATUS_IS_OK(result
)) {
240 fprintf(stderr
, "Error initialising session creds\n");
244 /* Synchronise sam database */
246 result
= cli_netlogon_sam_deltas(cli
, mem_ctx
, database_id
,
248 &hdr_deltas
, &deltas
);
250 if (!NT_STATUS_IS_OK(result
))
253 /* Display results */
255 display_sam_sync(num_deltas
, hdr_deltas
, deltas
);
261 /* Log on a domain user */
263 static NTSTATUS
cmd_netlogon_sam_logon(struct cli_state
*cli
,
264 TALLOC_CTX
*mem_ctx
, int argc
,
267 unsigned char trust_passwd
[16];
268 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
269 int logon_type
= NET_LOGON_TYPE
;
270 char *username
, *password
;
272 /* Check arguments */
274 if (argc
< 3 || argc
> 4) {
275 fprintf(stderr
, "Usage: samlogon <username> <password> "
284 sscanf(argv
[3], "%i", &logon_type
);
286 /* Authenticate ourselves with the domain controller */
288 if (!secrets_init()) {
289 fprintf(stderr
, "Unable to initialise secrets database\n");
293 if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd
,
295 fprintf(stderr
, "could not fetch trust account password\n");
299 result
= cli_nt_setup_creds(cli
, trust_passwd
);
301 if (!NT_STATUS_IS_OK(result
)) {
302 fprintf(stderr
, "Error initialising session creds\n");
306 /* Perform the sam logon */
308 result
= cli_netlogon_sam_logon(cli
, mem_ctx
, username
, password
,
311 if (!NT_STATUS_IS_OK(result
))
318 /* List of commands exported by this module */
320 struct cmd_set netlogon_commands
[] = {
324 { "logonctrl2", cmd_netlogon_logon_ctrl2
, PIPE_NETLOGON
, "Logon Control 2", "" },
325 { "logonctrl", cmd_netlogon_logon_ctrl
, PIPE_NETLOGON
, "Logon Control", "" },
326 { "samsync", cmd_netlogon_sam_sync
, PIPE_NETLOGON
, "Sam Synchronisation", "" },
327 { "samdeltas", cmd_netlogon_sam_deltas
, PIPE_NETLOGON
, "Query Sam Deltas", "" },
328 { "samlogon", cmd_netlogon_sam_logon
, PIPE_NETLOGON
, "Sam Logon", "" },