summaryrefslogtreecommitdiffstats
path: root/source3/utils/net_rpc_sh_acct.c
blob: 5305f517489a75e5f7de9784ae56b1508ce3174e (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
   Samba Unix/Linux SMB client library
   Distributed SMB/CIFS Server Management Utility
   Copyright (C) 2006 Volker Lendecke (vl@samba.org)

   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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "popt_common.h"
#include "utils/net.h"
#include "../librpc/gen_ndr/ndr_samr_c.h"
#include "../libcli/security/security.h"

/*
 * Do something with the account policies. Read them all, run a function on
 * them and possibly write them back. "fn" has to return the container index
 * it has modified, it can return 0 for no change.
 */

static NTSTATUS rpc_sh_acct_do(struct net_context *c,
			       TALLOC_CTX *mem_ctx,
			       struct rpc_sh_ctx *ctx,
			       struct rpc_pipe_client *pipe_hnd,
			       int argc, const char **argv,
			       int (*fn)(struct net_context *c,
					  TALLOC_CTX *mem_ctx,
					  struct rpc_sh_ctx *ctx,
					  struct samr_DomInfo1 *i1,
					  struct samr_DomInfo3 *i3,
					  struct samr_DomInfo12 *i12,
					  int argc, const char **argv))
{
	struct policy_handle connect_pol, domain_pol;
	NTSTATUS status, result;
	union samr_DomainInfo *info1 = NULL;
	union samr_DomainInfo *info3 = NULL;
	union samr_DomainInfo *info12 = NULL;
	int store;
	struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;

	ZERO_STRUCT(connect_pol);
	ZERO_STRUCT(domain_pol);

	/* Get sam policy handle */

	status = dcerpc_samr_Connect2(b, mem_ctx,
				      pipe_hnd->desthost,
				      MAXIMUM_ALLOWED_ACCESS,
				      &connect_pol,
				      &result);
	if (!NT_STATUS_IS_OK(status)) {
		goto done;
	}
	if (!NT_STATUS_IS_OK(result)) {
		status = result;
		goto done;
	}

	/* Get domain policy handle */

	status = dcerpc_samr_OpenDomain(b, mem_ctx,
					&connect_pol,
					MAXIMUM_ALLOWED_ACCESS,
					ctx->domain_sid,
					&domain_pol,
					&result);
	if (!NT_STATUS_IS_OK(status)) {
		goto done;
	}
	if (!NT_STATUS_IS_OK(result)) {
		status = result;
		goto done;
	}

	status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
					     &domain_pol,
					     1,
					     &info1,
					     &result);
	if (!NT_STATUS_IS_OK(status)) {
		goto done;
	}
	if (!NT_STATUS_IS_OK(result)) {
		status = result;
		d_fprintf(stderr, _("query_domain_info level 1 failed: %s\n"),
			  nt_errstr(result));
		goto done;
	}

	status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
					     &domain_pol,
					     3,
					     &info3,
					     &result);
	if (!NT_STATUS_IS_OK(status)) {
		goto done;
	}
	if (!NT_STATUS_IS_OK(result)) {
		status = result;
		d_fprintf(stderr, _("query_domain_info level 3 failed: %s\n"),
			  nt_errstr(result));
		goto done;
	}

	status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
					     &domain_pol,
					     12,
					     &info12,
					     &result);
	if (!NT_STATUS_IS_OK(status)) {
		goto done;
	}
	if (!NT_STATUS_IS_OK(result)) {
		status = result;
		d_fprintf(stderr, _("query_domain_info level 12 failed: %s\n"),
			  nt_errstr(result));
		goto done;
	}

	store = fn(c, mem_ctx, ctx, &info1->info1, &info3->info3,
		   &info12->info12, argc, argv);

	if (store <= 0) {
		/* Don't save anything */
		goto done;
	}

	switch (store) {
	case 1:
		status = dcerpc_samr_SetDomainInfo(b, mem_ctx,
						   &domain_pol,
						   1,
						   info1,
						   &result);
		break;
	case 3:
		status = dcerpc_samr_SetDomainInfo(b, mem_ctx,
						   &domain_pol,
						   3,
						   info3,
						   &result);
		break;
	case 12:
		status = dcerpc_samr_SetDomainInfo(b, mem_ctx,
						   &domain_pol,
						   12,
						   info12,
						   &result);
		break;
	default:
		d_fprintf(stderr, _("Got unexpected info level %d\n"), store);
		status = NT_STATUS_INTERNAL_ERROR;
		goto done;
	}

	if (!NT_STATUS_IS_OK(status)) {
		goto done;
	}

	status = result;

 done:
	if (is_valid_policy_hnd(&domain_pol)) {
		dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
	}
	if (is_valid_policy_hnd(&connect_pol)) {
		dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
	}

	return status;
}

static int account_show(struct net_context *c,
			TALLOC_CTX *mem_ctx, struct rpc_sh_ctx *ctx,
			struct samr_DomInfo1 *i1,
			struct samr_DomInfo3 *i3,
			struct samr_DomInfo12 *i12,
			int argc, const char **argv)
{
	if (argc != 0) {
		d_fprintf(stderr, "%s %s\n", _("Usage:"), ctx->whoami);
		return -1;
	}

	d_printf(_("Minimum password length: %d\n"), i1->min_password_length);
	d_printf(_("Password history length: %d\n"),
		 i1->password_history_length);

	d_printf(_("Minimum password age: "));
	if (!nt_time_is_zero((NTTIME *)&i1->min_password_age)) {
		time_t t = nt_time_to_unix_abs((NTTIME *)&i1->min_password_age);
		d_printf(_("%d seconds\n"), (int)t);
	} else {
		d_printf(_("not set\n"));
	}

	d_printf(_("Maximum password age: "));
	if (nt_time_is_set((NTTIME *)&i1->max_password_age)) {
		time_t t = nt_time_to_unix_abs((NTTIME *)&i1->max_password_age);
		d_printf(_("%d seconds\n"), (int)t);
	} else {
		d_printf(_("not set\n"));
	}

	d_printf(_("Bad logon attempts: %d\n"), i12->lockout_threshold);

	if (i12->lockout_threshold != 0) {

		d_printf(_("Account lockout duration: "));
		if (nt_time_is_set(&i12->lockout_duration)) {
			time_t t = nt_time_to_unix_abs(&i12->lockout_duration);
			d_printf(_("%d seconds\n"), (int)t);
		} else {
			d_printf(_("not set\n"));
		}

		d_printf(_("Bad password count reset after: "));
		if (nt_time_is_set(&i12->lockout_window)) {
			time_t t = nt_time_to_unix_abs(&i12->lockout_window);
			d_printf(_("%d seconds\n"), (int)t);
		} else {
			d_printf(_("not set\n"));
		}
	}

	d_printf(_("Disconnect users when logon hours expire: %s\n"),
		 nt_time_is_zero(&i3->force_logoff_time) ? _("yes") : _("no"));

	d_printf(_("User must logon to change password: %s\n"),
		 (i1->password_properties & 0x2) ? _("yes") : _("no"));

	return 0;		/* Don't save */
}

static NTSTATUS rpc_sh_acct_pol_show(struct net_context *c,
				     TALLOC_CTX *mem_ctx,
				     struct rpc_sh_ctx *ctx,
				     struct rpc_pipe_client *pipe_hnd,
				     int argc, const char **argv) {
	return rpc_sh_acct_do(c, mem_ctx, ctx, pipe_hnd, argc, argv,
			      account_show);
}

static int account_set_badpw(struct net_context *c,
			     TALLOC_CTX *mem_ctx, struct rpc_sh_ctx *ctx,
			     struct samr_DomInfo1 *i1,
			     struct samr_DomInfo3 *i3,
			     struct samr_DomInfo12 *i12,
			     int argc, const char **argv)
{
	if (argc != 1) {
		d_fprintf(stderr, "%s %s <count>\n", _("Usage:"), ctx->whoami);
		return -1;
	}

	i12->lockout_threshold = atoi(argv[0]);
	d_printf(_("Setting bad password count to %d\n"),
		 i12->lockout_threshold);

	return 12;
}

static NTSTATUS rpc_sh_acct_set_badpw(struct net_context *c,
				      TALLOC_CTX *mem_ctx,
				      struct rpc_sh_ctx *ctx,
				      struct rpc_pipe_client *pipe_hnd,
				      int argc, const char **argv)
{
	return rpc_sh_acct_do(c, mem_ctx, ctx, pipe_hnd, argc, argv,
			      account_set_badpw);
}

static int account_set_lockduration(struct net_context *c,
				    TALLOC_CTX *mem_ctx,
				    struct rpc_sh_ctx *ctx,
				    struct samr_DomInfo1 *i1,
				    struct samr_DomInfo3 *i3,
				    struct samr_DomInfo12 *i12,
				    int argc, const char **argv)
{
	if (argc != 1) {
		d_fprintf(stderr, _("Usage: %s <count>\n"), ctx->whoami);
		return -1;
	}

	unix_to_nt_time_abs(&i12->lockout_duration, atoi(argv[0]));
	d_printf(_("Setting lockout duration to %d seconds\n"),
		 (int)nt_time_to_unix_abs(&i12->lockout_duration));

	return 12;
}

static NTSTATUS rpc_sh_acct_set_lockduration(struct net_context *c,
					     TALLOC_CTX *mem_ctx,
					     struct rpc_sh_ctx *ctx,
					     struct rpc_pipe_client *pipe_hnd,
					     int argc, const char **argv)
{
	return rpc_sh_acct_do(c, mem_ctx, ctx, pipe_hnd, argc, argv,
			      account_set_lockduration);
}

static int account_set_resetduration(struct net_context *c,
				     TALLOC_CTX *mem_ctx,
				     struct rpc_sh_ctx *ctx,
				     struct samr_DomInfo1 *i1,
				     struct samr_DomInfo3 *i3,
				     struct samr_DomInfo12 *i12,
				     int argc, const char **argv)
{
	if (argc != 1) {
		d_fprintf(stderr, _("Usage: %s <count>\n"), ctx->whoami);
		return -1;
	}

	unix_to_nt_time_abs(&i12->lockout_window, atoi(argv[0]));
	d_printf(_("Setting bad password reset duration to %d seconds\n"),
		 (int)nt_time_to_unix_abs(&i12->lockout_window));

	return 12;
}

static NTSTATUS rpc_sh_acct_set_resetduration(struct net_context *c,
					      TALLOC_CTX *mem_ctx,
					      struct rpc_sh_ctx *ctx,
					      struct rpc_pipe_client *pipe_hnd,
					      int argc, const char **argv)
{
	return rpc_sh_acct_do(c, mem_ctx, ctx, pipe_hnd, argc, argv,
			      account_set_resetduration);
}

static int account_set_minpwage(struct net_context *c,
				TALLOC_CTX *mem_ctx,
				struct rpc_sh_ctx *ctx,
				struct samr_DomInfo1 *i1,
				struct samr_DomInfo3 *i3,
				struct samr_DomInfo12 *i12,
				int argc, const char **argv)
{
	if (argc != 1) {
		d_fprintf(stderr, _("Usage: %s <count>\n"), ctx->whoami);
		return -1;
	}

	unix_to_nt_time_abs((NTTIME *)&i1->min_password_age, atoi(argv[0]));
	d_printf(_("Setting minimum password age to %d seconds\n"),
		 (int)nt_time_to_unix_abs((NTTIME *)&i1->min_password_age));

	return 1;
}

static NTSTATUS rpc_sh_acct_set_minpwage(struct net_context *c,
					 TALLOC_CTX *mem_ctx,
					 struct rpc_sh_ctx *ctx,
					 struct rpc_pipe_client *pipe_hnd,
					 int argc, const char **argv)
{
	return rpc_sh_acct_do(c, mem_ctx, ctx, pipe_hnd, argc, argv,
			      account_set_minpwage);
}

static int account_set_maxpwage(struct net_context *c,
				TALLOC_CTX *mem_ctx,
				struct rpc_sh_ctx *ctx,
				struct samr_DomInfo1 *i1,
				struct samr_DomInfo3 *i3,
				struct samr_DomInfo12 *i12,
				int argc, const char **argv)
{
	if (argc != 1) {
		d_fprintf(stderr, _("Usage: %s <count>\n"), ctx->whoami);
		return -1;
	}

	unix_to_nt_time_abs((NTTIME *)&i1->max_password_age, atoi(argv[0]));
	d_printf(_("Setting maximum password age to %d seconds\n"),
		 (int)nt_time_to_unix_abs((NTTIME *)&i1->max_password_age));

	return 1;
}

static NTSTATUS rpc_sh_acct_set_maxpwage(struct net_context *c,
					 TALLOC_CTX *mem_ctx,
					 struct rpc_sh_ctx *ctx,
					 struct rpc_pipe_client *pipe_hnd,
					 int argc, const char **argv)
{
	return rpc_sh_acct_do(c, mem_ctx, ctx, pipe_hnd, argc, argv,
			      account_set_maxpwage);
}

static int account_set_minpwlen(struct net_context *c,
				TALLOC_CTX *mem_ctx,
				struct rpc_sh_ctx *ctx,
				struct samr_DomInfo1 *i1,
				struct samr_DomInfo3 *i3,
				struct samr_DomInfo12 *i12,
				int argc, const char **argv)
{
	if (argc != 1) {
		d_fprintf(stderr, _("Usage: %s <count>\n"), ctx->whoami);
		return -1;
	}

	i1->min_password_length = atoi(argv[0]);
	d_printf(_("Setting minimum password length to %d\n"),
		 i1->min_password_length);

	return 1;
}

static NTSTATUS rpc_sh_acct_set_minpwlen(struct net_context *c,
					 TALLOC_CTX *mem_ctx,
					 struct rpc_sh_ctx *ctx,
					 struct rpc_pipe_client *pipe_hnd,
					 int argc, const char **argv)
{
	return rpc_sh_acct_do(c, mem_ctx, ctx, pipe_hnd, argc, argv,
			      account_set_minpwlen);
}

static int account_set_pwhistlen(struct net_context *c,
				 TALLOC_CTX *mem_ctx,
				 struct rpc_sh_ctx *ctx,
				 struct samr_DomInfo1 *i1,
				 struct samr_DomInfo3 *i3,
				 struct samr_DomInfo12 *i12,
				 int argc, const char **argv)
{
	if (argc != 1) {
		d_fprintf(stderr, _("Usage: %s <count>\n"), ctx->whoami);
		return -1;
	}

	i1->password_history_length = atoi(argv[0]);
	d_printf(_("Setting password history length to %d\n"),
		 i1->password_history_length);

	return 1;
}

static NTSTATUS rpc_sh_acct_set_pwhistlen(struct net_context *c,
					  TALLOC_CTX *mem_ctx,
					  struct rpc_sh_ctx *ctx,
					  struct rpc_pipe_client *pipe_hnd,
					  int argc, const char **argv)
{
	return rpc_sh_acct_do(c, mem_ctx, ctx, pipe_hnd, argc, argv,
			      account_set_pwhistlen);
}

struct rpc_sh_cmd *net_rpc_acct_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
				     struct rpc_sh_ctx *ctx)
{
	static struct rpc_sh_cmd cmds[9] = {
		{ "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_acct_pol_show,
		  N_("Show current account policy settings") },
		{ "badpw", NULL, &ndr_table_samr.syntax_id, rpc_sh_acct_set_badpw,
		  N_("Set bad password count before lockout") },
		{ "lockduration", NULL, &ndr_table_samr.syntax_id, rpc_sh_acct_set_lockduration,
		  N_("Set account lockout duration") },
		{ "resetduration", NULL, &ndr_table_samr.syntax_id,
		  rpc_sh_acct_set_resetduration,
		  N_("Set bad password count reset duration") },
		{ "minpwage", NULL, &ndr_table_samr.syntax_id, rpc_sh_acct_set_minpwage,
		  N_("Set minimum password age") },
		{ "maxpwage", NULL, &ndr_table_samr.syntax_id, rpc_sh_acct_set_maxpwage,
		  N_("Set maximum password age") },
		{ "minpwlen", NULL, &ndr_table_samr.syntax_id, rpc_sh_acct_set_minpwlen,
		  N_("Set minimum password length") },
		{ "pwhistlen", NULL, &ndr_table_samr.syntax_id, rpc_sh_acct_set_pwhistlen,
		  N_("Set the password history length") },
		{ NULL, NULL, 0, NULL, NULL }
	};

	return cmds;
}