2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2006 Volker Lendecke (vl@samba.org)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "utils/net.h"
21 #include "../librpc/gen_ndr/ndr_samr_c.h"
22 #include "../libcli/security/security.h"
25 * Do something with the account policies. Read them all, run a function on
26 * them and possibly write them back. "fn" has to return the container index
27 * it has modified, it can return 0 for no change.
30 static NTSTATUS
rpc_sh_acct_do(struct net_context
*c
,
32 struct rpc_sh_ctx
*ctx
,
33 struct rpc_pipe_client
*pipe_hnd
,
34 int argc
, const char **argv
,
35 int (*fn
)(struct net_context
*c
,
37 struct rpc_sh_ctx
*ctx
,
38 struct samr_DomInfo1
*i1
,
39 struct samr_DomInfo3
*i3
,
40 struct samr_DomInfo12
*i12
,
41 int argc
, const char **argv
))
43 struct policy_handle connect_pol
, domain_pol
;
44 NTSTATUS status
, result
;
45 union samr_DomainInfo
*info1
= NULL
;
46 union samr_DomainInfo
*info3
= NULL
;
47 union samr_DomainInfo
*info12
= NULL
;
49 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
51 ZERO_STRUCT(connect_pol
);
52 ZERO_STRUCT(domain_pol
);
54 /* Get sam policy handle */
56 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
58 MAXIMUM_ALLOWED_ACCESS
,
61 if (!NT_STATUS_IS_OK(status
)) {
64 if (!NT_STATUS_IS_OK(result
)) {
69 /* Get domain policy handle */
71 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
73 MAXIMUM_ALLOWED_ACCESS
,
77 if (!NT_STATUS_IS_OK(status
)) {
80 if (!NT_STATUS_IS_OK(result
)) {
85 status
= dcerpc_samr_QueryDomainInfo(b
, mem_ctx
,
90 if (!NT_STATUS_IS_OK(status
)) {
93 if (!NT_STATUS_IS_OK(result
)) {
95 d_fprintf(stderr
, _("query_domain_info level 1 failed: %s\n"),
100 status
= dcerpc_samr_QueryDomainInfo(b
, mem_ctx
,
105 if (!NT_STATUS_IS_OK(status
)) {
108 if (!NT_STATUS_IS_OK(result
)) {
110 d_fprintf(stderr
, _("query_domain_info level 3 failed: %s\n"),
115 status
= dcerpc_samr_QueryDomainInfo(b
, mem_ctx
,
120 if (!NT_STATUS_IS_OK(status
)) {
123 if (!NT_STATUS_IS_OK(result
)) {
125 d_fprintf(stderr
, _("query_domain_info level 12 failed: %s\n"),
130 store
= fn(c
, mem_ctx
, ctx
, &info1
->info1
, &info3
->info3
,
131 &info12
->info12
, argc
, argv
);
134 /* Don't save anything */
140 status
= dcerpc_samr_SetDomainInfo(b
, mem_ctx
,
147 status
= dcerpc_samr_SetDomainInfo(b
, mem_ctx
,
154 status
= dcerpc_samr_SetDomainInfo(b
, mem_ctx
,
161 d_fprintf(stderr
, _("Got unexpected info level %d\n"), store
);
162 status
= NT_STATUS_INTERNAL_ERROR
;
166 if (!NT_STATUS_IS_OK(status
)) {
173 if (is_valid_policy_hnd(&domain_pol
)) {
174 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
176 if (is_valid_policy_hnd(&connect_pol
)) {
177 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
183 static int account_show(struct net_context
*c
,
184 TALLOC_CTX
*mem_ctx
, struct rpc_sh_ctx
*ctx
,
185 struct samr_DomInfo1
*i1
,
186 struct samr_DomInfo3
*i3
,
187 struct samr_DomInfo12
*i12
,
188 int argc
, const char **argv
)
191 d_fprintf(stderr
, "%s %s\n", _("Usage:"), ctx
->whoami
);
195 d_printf(_("Minimum password length: %d\n"), i1
->min_password_length
);
196 d_printf(_("Password history length: %d\n"),
197 i1
->password_history_length
);
199 d_printf(_("Minimum password age: "));
200 if (!nt_time_is_zero((NTTIME
*)&i1
->min_password_age
)) {
201 time_t t
= nt_time_to_unix_abs((NTTIME
*)&i1
->min_password_age
);
202 d_printf(_("%d seconds\n"), (int)t
);
204 d_printf(_("not set\n"));
207 d_printf(_("Maximum password age: "));
208 if (nt_time_is_set((NTTIME
*)&i1
->max_password_age
)) {
209 time_t t
= nt_time_to_unix_abs((NTTIME
*)&i1
->max_password_age
);
210 d_printf(_("%d seconds\n"), (int)t
);
212 d_printf(_("not set\n"));
215 d_printf(_("Bad logon attempts: %d\n"), i12
->lockout_threshold
);
217 if (i12
->lockout_threshold
!= 0) {
219 d_printf(_("Account lockout duration: "));
220 if (nt_time_is_set(&i12
->lockout_duration
)) {
221 time_t t
= nt_time_to_unix_abs(&i12
->lockout_duration
);
222 d_printf(_("%d seconds\n"), (int)t
);
224 d_printf(_("not set\n"));
227 d_printf(_("Bad password count reset after: "));
228 if (nt_time_is_set(&i12
->lockout_window
)) {
229 time_t t
= nt_time_to_unix_abs(&i12
->lockout_window
);
230 d_printf(_("%d seconds\n"), (int)t
);
232 d_printf(_("not set\n"));
236 d_printf(_("Disconnect users when logon hours expire: %s\n"),
237 nt_time_is_zero(&i3
->force_logoff_time
) ? _("yes") : _("no"));
239 d_printf(_("User must logon to change password: %s\n"),
240 (i1
->password_properties
& 0x2) ? _("yes") : _("no"));
242 return 0; /* Don't save */
245 static NTSTATUS
rpc_sh_acct_pol_show(struct net_context
*c
,
247 struct rpc_sh_ctx
*ctx
,
248 struct rpc_pipe_client
*pipe_hnd
,
249 int argc
, const char **argv
) {
250 return rpc_sh_acct_do(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
254 static int account_set_badpw(struct net_context
*c
,
255 TALLOC_CTX
*mem_ctx
, struct rpc_sh_ctx
*ctx
,
256 struct samr_DomInfo1
*i1
,
257 struct samr_DomInfo3
*i3
,
258 struct samr_DomInfo12
*i12
,
259 int argc
, const char **argv
)
262 d_fprintf(stderr
, "%s %s <count>\n", _("Usage:"), ctx
->whoami
);
266 i12
->lockout_threshold
= atoi(argv
[0]);
267 d_printf(_("Setting bad password count to %d\n"),
268 i12
->lockout_threshold
);
273 static NTSTATUS
rpc_sh_acct_set_badpw(struct net_context
*c
,
275 struct rpc_sh_ctx
*ctx
,
276 struct rpc_pipe_client
*pipe_hnd
,
277 int argc
, const char **argv
)
279 return rpc_sh_acct_do(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
283 static int account_set_lockduration(struct net_context
*c
,
285 struct rpc_sh_ctx
*ctx
,
286 struct samr_DomInfo1
*i1
,
287 struct samr_DomInfo3
*i3
,
288 struct samr_DomInfo12
*i12
,
289 int argc
, const char **argv
)
292 d_fprintf(stderr
, _("Usage: %s <count>\n"), ctx
->whoami
);
296 unix_to_nt_time_abs(&i12
->lockout_duration
, atoi(argv
[0]));
297 d_printf(_("Setting lockout duration to %d seconds\n"),
298 (int)nt_time_to_unix_abs(&i12
->lockout_duration
));
303 static NTSTATUS
rpc_sh_acct_set_lockduration(struct net_context
*c
,
305 struct rpc_sh_ctx
*ctx
,
306 struct rpc_pipe_client
*pipe_hnd
,
307 int argc
, const char **argv
)
309 return rpc_sh_acct_do(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
310 account_set_lockduration
);
313 static int account_set_resetduration(struct net_context
*c
,
315 struct rpc_sh_ctx
*ctx
,
316 struct samr_DomInfo1
*i1
,
317 struct samr_DomInfo3
*i3
,
318 struct samr_DomInfo12
*i12
,
319 int argc
, const char **argv
)
322 d_fprintf(stderr
, _("Usage: %s <count>\n"), ctx
->whoami
);
326 unix_to_nt_time_abs(&i12
->lockout_window
, atoi(argv
[0]));
327 d_printf(_("Setting bad password reset duration to %d seconds\n"),
328 (int)nt_time_to_unix_abs(&i12
->lockout_window
));
333 static NTSTATUS
rpc_sh_acct_set_resetduration(struct net_context
*c
,
335 struct rpc_sh_ctx
*ctx
,
336 struct rpc_pipe_client
*pipe_hnd
,
337 int argc
, const char **argv
)
339 return rpc_sh_acct_do(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
340 account_set_resetduration
);
343 static int account_set_minpwage(struct net_context
*c
,
345 struct rpc_sh_ctx
*ctx
,
346 struct samr_DomInfo1
*i1
,
347 struct samr_DomInfo3
*i3
,
348 struct samr_DomInfo12
*i12
,
349 int argc
, const char **argv
)
352 d_fprintf(stderr
, _("Usage: %s <count>\n"), ctx
->whoami
);
356 unix_to_nt_time_abs((NTTIME
*)&i1
->min_password_age
, atoi(argv
[0]));
357 d_printf(_("Setting minimum password age to %d seconds\n"),
358 (int)nt_time_to_unix_abs((NTTIME
*)&i1
->min_password_age
));
363 static NTSTATUS
rpc_sh_acct_set_minpwage(struct net_context
*c
,
365 struct rpc_sh_ctx
*ctx
,
366 struct rpc_pipe_client
*pipe_hnd
,
367 int argc
, const char **argv
)
369 return rpc_sh_acct_do(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
370 account_set_minpwage
);
373 static int account_set_maxpwage(struct net_context
*c
,
375 struct rpc_sh_ctx
*ctx
,
376 struct samr_DomInfo1
*i1
,
377 struct samr_DomInfo3
*i3
,
378 struct samr_DomInfo12
*i12
,
379 int argc
, const char **argv
)
382 d_fprintf(stderr
, _("Usage: %s <count>\n"), ctx
->whoami
);
386 unix_to_nt_time_abs((NTTIME
*)&i1
->max_password_age
, atoi(argv
[0]));
387 d_printf(_("Setting maximum password age to %d seconds\n"),
388 (int)nt_time_to_unix_abs((NTTIME
*)&i1
->max_password_age
));
393 static NTSTATUS
rpc_sh_acct_set_maxpwage(struct net_context
*c
,
395 struct rpc_sh_ctx
*ctx
,
396 struct rpc_pipe_client
*pipe_hnd
,
397 int argc
, const char **argv
)
399 return rpc_sh_acct_do(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
400 account_set_maxpwage
);
403 static int account_set_minpwlen(struct net_context
*c
,
405 struct rpc_sh_ctx
*ctx
,
406 struct samr_DomInfo1
*i1
,
407 struct samr_DomInfo3
*i3
,
408 struct samr_DomInfo12
*i12
,
409 int argc
, const char **argv
)
412 d_fprintf(stderr
, _("Usage: %s <count>\n"), ctx
->whoami
);
416 i1
->min_password_length
= atoi(argv
[0]);
417 d_printf(_("Setting minimum password length to %d\n"),
418 i1
->min_password_length
);
423 static NTSTATUS
rpc_sh_acct_set_minpwlen(struct net_context
*c
,
425 struct rpc_sh_ctx
*ctx
,
426 struct rpc_pipe_client
*pipe_hnd
,
427 int argc
, const char **argv
)
429 return rpc_sh_acct_do(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
430 account_set_minpwlen
);
433 static int account_set_pwhistlen(struct net_context
*c
,
435 struct rpc_sh_ctx
*ctx
,
436 struct samr_DomInfo1
*i1
,
437 struct samr_DomInfo3
*i3
,
438 struct samr_DomInfo12
*i12
,
439 int argc
, const char **argv
)
442 d_fprintf(stderr
, _("Usage: %s <count>\n"), ctx
->whoami
);
446 i1
->password_history_length
= atoi(argv
[0]);
447 d_printf(_("Setting password history length to %d\n"),
448 i1
->password_history_length
);
453 static NTSTATUS
rpc_sh_acct_set_pwhistlen(struct net_context
*c
,
455 struct rpc_sh_ctx
*ctx
,
456 struct rpc_pipe_client
*pipe_hnd
,
457 int argc
, const char **argv
)
459 return rpc_sh_acct_do(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
460 account_set_pwhistlen
);
463 struct rpc_sh_cmd
*net_rpc_acct_cmds(struct net_context
*c
, TALLOC_CTX
*mem_ctx
,
464 struct rpc_sh_ctx
*ctx
)
466 static struct rpc_sh_cmd cmds
[9] = {
467 { "show", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_acct_pol_show
,
468 N_("Show current account policy settings") },
469 { "badpw", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_acct_set_badpw
,
470 N_("Set bad password count before lockout") },
471 { "lockduration", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_acct_set_lockduration
,
472 N_("Set account lockout duration") },
473 { "resetduration", NULL
, &ndr_table_samr
.syntax_id
,
474 rpc_sh_acct_set_resetduration
,
475 N_("Set bad password count reset duration") },
476 { "minpwage", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_acct_set_minpwage
,
477 N_("Set minimum password age") },
478 { "maxpwage", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_acct_set_maxpwage
,
479 N_("Set maximum password age") },
480 { "minpwlen", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_acct_set_minpwlen
,
481 N_("Set minimum password length") },
482 { "pwhistlen", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_acct_set_pwhistlen
,
483 N_("Set the password history length") },
484 { NULL
, NULL
, 0, NULL
, NULL
}