summaryrefslogtreecommitdiff
path: root/source3/rpcclient/cmd_netlogon.c
blob: 1b493875585ab73598bf5647714bfb051af9acd4 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* 
   Unix SMB/Netbios implementation.
   Version 2.2
   RPC pipe client

   Copyright (C) Tim Potter 2000

   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"

extern int DEBUGLEVEL;

static uint32 cmd_netlogon_logon_ctrl2(struct cli_state *cli, int argc,
				       char **argv)
{
	uint32 query_level = 1;
	TALLOC_CTX *mem_ctx;
	uint32 result = NT_STATUS_UNSUCCESSFUL;

	if (argc > 1) {
		printf("Usage: %s\n", argv[0]);
		return 0;
	}

	if (!(mem_ctx = talloc_init())) {
		DEBUG(0,("cmd_srvsvc_srv_query_info: talloc_init failed\n"));
		goto done;
	}

	/* Initialise RPC connection */

	if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
		DEBUG(0, ("Could not initialize srvsvc pipe!\n"));
		goto done;
	}

	if ((result = cli_netlogon_logon_ctrl2(cli, mem_ctx, query_level))
	     != NT_STATUS_OK) {
		goto done;
	}

	/* Display results */

 done:
	return result;
}

static uint32 cmd_netlogon_logon_ctrl(struct cli_state *cli, int argc,
				      char **argv)
{
#if 0
	uint32 query_level = 1;
#endif
	TALLOC_CTX *mem_ctx;
	uint32 result = NT_STATUS_UNSUCCESSFUL;

	if (argc > 1) {
		printf("Usage: %s\n", argv[0]);
		return 0;
	}

	if (!(mem_ctx = talloc_init())) {
		DEBUG(0,("cmd_srvsvc_srv_query_info: talloc_init failed\n"));
		goto done;
	}

	/* Initialise RPC connection */

	if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
		DEBUG(0, ("Could not initialize netlogon pipe!\n"));
		goto done;
	}

#if 0
	if ((result = cli_netlogon_logon_ctrl(cli, mem_ctx, query_level))
	     != NT_STATUS_OK) {
		goto done;
	}
#endif

	/* Display results */

 done:
	return result;
}

/* Display sam synchronisation information */

static void display_sam_sync(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas,
                             SAM_DELTA_CTR *deltas)
{
        fstring name;
        uint32 i, j;

        for (i = 0; i < num_deltas; i++) {
                switch (hdr_deltas[i].type) {
                case SAM_DELTA_DOMAIN_INFO:
                        unistr2_to_ascii(name,
                                         &deltas[i].domain_info.uni_dom_name,
                                         sizeof(name) - 1);
                        DEBUG(0, ("Domain: %s\n", name));
                        break;
                case SAM_DELTA_GROUP_INFO:
                        unistr2_to_ascii(name,
                                         &deltas[i].group_info.uni_grp_name,
                                         sizeof(name) - 1);
                        DEBUG(0, ("Group: %s\n", name));
                        break;
                case SAM_DELTA_ACCOUNT_INFO:
                        unistr2_to_ascii(name, 
                                         &deltas[i].account_info.uni_acct_name,
                                         sizeof(name) - 1);
                        DEBUG(0, ("Account: %s\n", name));
                        break;
                case SAM_DELTA_ALIAS_INFO:
                        unistr2_to_ascii(name, 
                                         &deltas[i].alias_info.uni_als_name,
                                         sizeof(name) - 1);
                        DEBUG(0, ("Alias: %s\n", name));
                        break;
                case SAM_DELTA_ALIAS_MEM: {
                        SAM_ALIAS_MEM_INFO *alias = &deltas[i].als_mem_info;

                        for (j = 0; j < alias->num_members; j++) {
                                fstring sid_str;

                                sid_to_string(sid_str, &alias->sids[j].sid);

                                DEBUG(0, ("%s\n", sid_str));
                        }
                        break;
                }
                case SAM_DELTA_GROUP_MEM: {
                        SAM_GROUP_MEM_INFO *group = &deltas[i].grp_mem_info;

                        for (j = 0; j < group->num_members; j++)
                                DEBUG(0, ("rid 0x%x, attrib 0x%08x\n", 
                                          group->rids[j], group->attribs[j]));
                        break;
                }
                case SAM_DELTA_SAM_STAMP: {
                        SAM_DELTA_STAMP *stamp = &deltas[i].stamp;

                        DEBUG(0, ("sam sequence update: 0x%04x\n",
                                  stamp->seqnum));
                        break;
                }                                  
                default:
                        DEBUG(0, ("unknown delta type 0x%02x\n", 
                                  hdr_deltas[i].type));
                        break;
                }
        }
}

/* Perform sam synchronisation */

static uint32 cmd_netlogon_sam_sync(struct cli_state *cli, int argc,
                                    char **argv)
{
	uint32 result = NT_STATUS_UNSUCCESSFUL;
        unsigned char trust_passwd[16];
        TALLOC_CTX *mem_ctx;
        uint32 database_id = 0, num_deltas;
        SAM_DELTA_HDR *hdr_deltas;
        SAM_DELTA_CTR *deltas;

        if (argc > 2) {
                printf("Usage: %s [database_id]\n", argv[0]);
                return 0;
        }

        if (argc == 2)
                database_id = atoi(argv[1]);

        if (!secrets_init()) {
                DEBUG(0, ("Unable to initialise secrets database\n"));
                return result;
        }

	if (!(mem_ctx = talloc_init())) {
		DEBUG(0,("talloc_init failed\n"));
		return result;
	}

	/* Initialise RPC connection */

	if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
		DEBUG(0, ("Could not initialize netlogon pipe!\n"));
		goto done;
	}

        /* Initialise session credentials */

	if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd,
                                                  NULL)) {
		DEBUG(0, ("could not fetch trust account password\n"));
		goto done;
	}        

        if (!cli_nt_setup_creds(cli, trust_passwd)) {
                DEBUG(0, ("Error initialising session creds\n"));
                goto done;
        }

        /* Synchronise sam database */

	if ((result = cli_netlogon_sam_sync(cli, mem_ctx, database_id,
                                            &num_deltas, &hdr_deltas, &deltas))
            != NT_STATUS_OK) {
		goto done;
	}

        /* Display results */

        display_sam_sync(num_deltas, hdr_deltas, deltas);

 done:
	cli_nt_session_close(cli);
	talloc_destroy(mem_ctx);
        
        return result;
}

/* Perform sam delta synchronisation */

static uint32 cmd_netlogon_sam_deltas(struct cli_state *cli, int argc,
                                      char **argv)
{
	uint32 result = NT_STATUS_UNSUCCESSFUL;
        unsigned char trust_passwd[16];
        TALLOC_CTX *mem_ctx = NULL;
        uint32 database_id, num_deltas, tmp;
        SAM_DELTA_HDR *hdr_deltas;
        SAM_DELTA_CTR *deltas;
        UINT64_S seqnum;

        if (argc != 3) {
                printf("Usage: %s database_id seqnum\n", argv[0]);
                return 0;
        }

        database_id = atoi(argv[1]);
        tmp = atoi(argv[2]);

        seqnum.low = tmp & 0xffff;
        seqnum.high = 0;

        if (!secrets_init()) {
                DEBUG(0, ("Unable to initialise secrets database\n"));
                goto done;
        }

	if (!(mem_ctx = talloc_init())) {
		DEBUG(0,("talloc_init failed\n"));
		goto done;
	}

	/* Initialise RPC connection */

	if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
		DEBUG(0, ("Could not initialize netlogon pipe!\n"));
		goto done;
	}

        /* Initialise session credentials */

	if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd,
                                                  NULL)) {
		DEBUG(0, ("could not fetch trust account password\n"));
		goto done;
	}        

        if (!cli_nt_setup_creds(cli, trust_passwd)) {
                DEBUG(0, ("Error initialising session creds\n"));
                goto done;
        }

        /* Synchronise sam database */

	if ((result = cli_netlogon_sam_deltas(cli, mem_ctx, database_id,
                                              seqnum, &num_deltas, 
                                              &hdr_deltas, &deltas))
            != NT_STATUS_OK) {
		goto done;
	}

        /* Display results */

        display_sam_sync(num_deltas, hdr_deltas, deltas);
        
 done:
	cli_nt_session_close(cli);
	talloc_destroy(mem_ctx);

        return result;
}

/* List of commands exported by this module */

struct cmd_set netlogon_commands[] = {

	{ "NETLOGON" },

	{ "logonctrl2", cmd_netlogon_logon_ctrl2, "Logon Control 2",     "" },
	{ "logonctrl",  cmd_netlogon_logon_ctrl,  "Logon Control",       "" },
	{ "samsync",    cmd_netlogon_sam_sync,    "Sam Synchronisation", "" },
	{ "samdeltas",  cmd_netlogon_sam_deltas,  "Query Sam Deltas", "" },

	{ NULL }
};