1 # domain management - domain tombstones
3 # Copyright Matthias Dieter Wallnoefer 2009
4 # Copyright Andrew Kroeger 2009
5 # Copyright Jelmer Vernooij 2007-2012
6 # Copyright Giampaolo Lauria 2011
7 # Copyright Matthieu Patou <mat@matws.net> 2011
8 # Copyright Andrew Bartlett 2008-2015
9 # Copyright Stefan Metzmacher 2012
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 import samba
.getopt
as options
29 from samba
.auth
import system_session
30 from samba
.netcmd
import Command
, CommandError
, Option
, SuperCommand
31 from samba
.samdb
import SamDB
34 class cmd_domain_tombstones_expunge(Command
):
35 """Expunge tombstones from the database.
37 This command expunges tombstones from the database."""
38 synopsis
= "%prog NC [NC [...]] [options]"
41 Option("-H", "--URL", help="LDB URL for database or target server", type=str,
42 metavar
="URL", dest
="H"),
43 Option("--current-time",
44 help="The current time to evaluate the tombstone lifetime from, expressed as YYYY-MM-DD",
46 Option("--tombstone-lifetime", help="Number of days a tombstone should be preserved for", type=int),
51 takes_optiongroups
= {
52 "sambaopts": options
.SambaOptions
,
53 "credopts": options
.CredentialsOptions
,
54 "versionopts": options
.VersionOptions
,
57 def run(self
, *ncs
, **kwargs
):
58 sambaopts
= kwargs
.get("sambaopts")
59 credopts
= kwargs
.get("credopts")
61 current_time_string
= kwargs
.get("current_time")
62 tombstone_lifetime
= kwargs
.get("tombstone_lifetime")
63 lp
= sambaopts
.get_loadparm()
64 creds
= credopts
.get_credentials(lp
)
65 samdb
= SamDB(url
=H
, session_info
=system_session(),
66 credentials
=creds
, lp
=lp
)
68 if current_time_string
is None and tombstone_lifetime
is None:
69 print("Note: without --current-time or --tombstone-lifetime "
70 "only tombstones already scheduled for deletion will "
71 "be deleted.", file=self
.outf
)
72 print("To remove all tombstones, use --tombstone-lifetime=0.",
75 if current_time_string
is not None:
76 current_time_obj
= time
.strptime(current_time_string
, "%Y-%m-%d")
77 current_time
= int(time
.mktime(current_time_obj
))
80 current_time
= int(time
.time())
83 res
= samdb
.search(expression
="", base
="", scope
=ldb
.SCOPE_BASE
,
84 attrs
=["namingContexts"])
87 for nc
in res
[0]["namingContexts"]:
92 started_transaction
= False
94 samdb
.transaction_start()
95 started_transaction
= True
97 removed_links
) = samdb
.garbage_collect_tombstones(ncs
,
98 current_time
=current_time
,
99 tombstone_lifetime
=tombstone_lifetime
)
101 except Exception as err
:
102 if started_transaction
:
103 samdb
.transaction_cancel()
104 raise CommandError("Failed to expunge / garbage collect tombstones", err
)
106 samdb
.transaction_commit()
108 self
.outf
.write("Removed %d objects and %d links successfully\n"
109 % (removed_objects
, removed_links
))
112 class cmd_domain_tombstones(SuperCommand
):
113 """Domain tombstone and recycled object management."""
116 subcommands
["expunge"] = cmd_domain_tombstones_expunge()