s3: Slightly simplify is_stat_open
[Samba/gebeck_regimport.git] / source4 / dsdb / kcc / kcc_deleted.c
blob63bb97c08dcc4a5f20f8681a87a2bbc8d3bd4203
1 /*
2 Unix SMB/CIFS implementation.
4 handle removal of deleted objects
6 Copyright (C) 2009 Andrew Tridgell
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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "auth/auth.h"
27 #include "smbd/service.h"
28 #include "lib/messaging/irpc.h"
29 #include "dsdb/kcc/kcc_connection.h"
30 #include "dsdb/kcc/kcc_service.h"
31 #include <ldb_errors.h>
32 #include "../lib/util/dlinklist.h"
33 #include "librpc/gen_ndr/ndr_misc.h"
34 #include "librpc/gen_ndr/ndr_drsuapi.h"
35 #include "librpc/gen_ndr/ndr_drsblobs.h"
36 #include "param/param.h"
37 #include "dsdb/common/util.h"
40 check to see if any deleted objects need scavenging
42 NTSTATUS kccsrv_check_deleted(struct kccsrv_service *s, TALLOC_CTX *mem_ctx)
44 struct kccsrv_partition *part;
45 int ret;
46 uint32_t tombstoneLifetime;
47 bool do_fs = false;
49 time_t interval = lpcfg_parm_int(s->task->lp_ctx, NULL, "kccsrv",
50 "check_deleted_full_scan_interval", 86400);
51 time_t t = time(NULL);
53 if (t - s->last_deleted_check < lpcfg_parm_int(s->task->lp_ctx, NULL, "kccsrv",
54 "check_deleted_interval", 600)) {
55 return NT_STATUS_OK;
57 s->last_deleted_check = t;
59 ret = dsdb_tombstone_lifetime(s->samdb, &tombstoneLifetime);
60 if (ret != LDB_SUCCESS) {
61 DEBUG(1,(__location__ ": Failed to get tombstone lifetime\n"));
62 return NT_STATUS_INTERNAL_DB_CORRUPTION;
64 if (s->last_full_scan_deleted_check > 0 && ((t - s->last_full_scan_deleted_check) > interval )) {
65 do_fs = true;
66 s->last_full_scan_deleted_check = t;
69 if (s->last_full_scan_deleted_check == 0) {
71 * If we never made a full scan set the last full scan event to be in the past
72 * and that 9/10 of the full scan interval has already passed.
73 * This is done to avoid the full scan to fire just at the begining of samba
74 * or a couple of minutes after the start.
75 * With this "setup" and default values of interval, the full scan will fire
76 * 2.4 hours after the start of samba
78 s->last_full_scan_deleted_check = t - ((9 * interval) / 10);
81 for (part=s->partitions; part; part=part->next) {
82 struct ldb_dn *do_dn;
83 struct ldb_result *res;
84 const char *attrs[] = { "whenChanged", NULL };
85 unsigned int i;
86 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
87 if (!tmp_ctx) {
88 return NT_STATUS_NO_MEMORY;
91 ret = dsdb_get_deleted_objects_dn(s->samdb, tmp_ctx, part->dn, &do_dn);
92 if (ret != LDB_SUCCESS) {
93 TALLOC_FREE(tmp_ctx);
94 /* some partitions have no Deleted Objects
95 container */
96 continue;
99 if (!do_fs && ldb_dn_compare(ldb_get_config_basedn(s->samdb), part->dn)) {
100 ret = dsdb_search(s->samdb, tmp_ctx, &res, do_dn, LDB_SCOPE_ONELEVEL, attrs,
101 DSDB_SEARCH_SHOW_RECYCLED, NULL);
102 } else {
103 if (do_fs) {
104 DEBUG(1, ("Doing a full scan on %s and looking for deleted object\n",
105 ldb_dn_get_linearized(part->dn)));
107 ret = dsdb_search(s->samdb, tmp_ctx, &res, part->dn, LDB_SCOPE_SUBTREE, attrs,
108 DSDB_SEARCH_SHOW_RECYCLED, "(isDeleted=TRUE)");
111 if (ret != LDB_SUCCESS) {
112 DEBUG(1,(__location__ ": Failed to search for deleted objects in %s\n",
113 ldb_dn_get_linearized(do_dn)));
114 TALLOC_FREE(tmp_ctx);
115 continue;
118 for (i=0; i<res->count; i++) {
119 const char *tstring;
120 time_t whenChanged = 0;
122 if (ldb_dn_compare(do_dn, res->msgs[i]->dn) == 0) {
123 /* Skip the Deleted Object Container */
124 continue;
126 tstring = ldb_msg_find_attr_as_string(res->msgs[i], "whenChanged", NULL);
127 if (tstring) {
128 whenChanged = ldb_string_to_time(tstring);
130 if (t - whenChanged > tombstoneLifetime*60*60*24) {
131 ret = dsdb_delete(s->samdb, res->msgs[i]->dn, DSDB_SEARCH_SHOW_DELETED);
132 if (ret != LDB_SUCCESS) {
133 DEBUG(1,(__location__ ": Failed to remove deleted object %s\n",
134 ldb_dn_get_linearized(res->msgs[i]->dn)));
135 } else {
136 DEBUG(4,("Removed deleted object %s\n",
137 ldb_dn_get_linearized(res->msgs[i]->dn)));
142 TALLOC_FREE(tmp_ctx);
145 return NT_STATUS_OK;