s4-kcc: Sets the isRecyled attributes on deleted entries when needed
[mat/samba.git] / source4 / dsdb / kcc / kcc_deleted.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    handle removal of deleted objects
5
6    Copyright (C) 2009 Andrew Tridgell
7
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.
12
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.
17
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/>.
20
21 */
22
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"
38
39 /*
40   check to see if any deleted objects need scavenging
41  */
42 NTSTATUS kccsrv_check_deleted(struct kccsrv_service *s, TALLOC_CTX *mem_ctx)
43 {
44         struct kccsrv_partition *part;
45         int ret;
46         uint32_t tombstoneLifetime;
47         const struct dsdb_schema *schema;
48         bool do_fs = false;
49         bool add_recycled = false;
50
51         time_t t = time(NULL);
52         if (t - s->last_deleted_check < lpcfg_parm_int(s->task->lp_ctx, NULL, "kccsrv",
53                                                     "check_deleted_interval", 600)) {
54                 return NT_STATUS_OK;
55         }
56         s->last_deleted_check = t;
57         DEBUG(11, ("Check deleted has kicked in !\n"));
58
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;
63         }
64
65         schema = dsdb_get_schema(s->samdb, mem_ctx);
66         if (!schema) {
67                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
68         }
69
70         if (dsdb_attribute_by_lDAPDisplayName(schema, "isRecycled") != NULL) {
71                 add_recycled = true;
72         }
73
74         for (part=s->partitions; part; part=part->next) {
75                 struct ldb_dn *do_dn;
76                 struct ldb_result *res;
77                 const char *attrs[] = { "whenChanged", "isRecycled", NULL };
78                 unsigned int i;
79
80                 ret = dsdb_get_deleted_objects_dn(s->samdb, mem_ctx, part->dn, &do_dn);
81                 if (ret != LDB_SUCCESS) {
82                         /* some partitions have no Deleted Objects
83                            container */
84                         continue;
85                 }
86                 ret = dsdb_search(s->samdb, do_dn, &res, do_dn, LDB_SCOPE_ONELEVEL, attrs,
87                                   DSDB_SEARCH_SHOW_RECYCLED, NULL);
88
89                 if (ret != LDB_SUCCESS) {
90                         DEBUG(1,(__location__ ": Failed to search for deleted objects in %s\n",
91                                  ldb_dn_get_linearized(do_dn)));
92                         talloc_free(do_dn);
93                         continue;
94                 }
95
96                 for (i=0; i<res->count; i++) {
97                         const char *tstring;
98                         time_t whenChanged = 0;
99
100                         if (ldb_dn_compare(do_dn, res->msgs[i]->dn) == 0) {
101                                 /* Skip the Deleted Object Container */
102                                 continue;
103                         }
104                         tstring = ldb_msg_find_attr_as_string(res->msgs[i], "whenChanged", NULL);
105                         if (tstring) {
106                                 whenChanged = ldb_string_to_time(tstring);
107                         }
108                         if (t - whenChanged > tombstoneLifetime*60*60*24) {
109                                 ret = dsdb_delete(s->samdb, res->msgs[i]->dn, DSDB_SEARCH_SHOW_DELETED);
110                                 if (ret != LDB_SUCCESS) {
111                                         DEBUG(1,(__location__ ": Failed to remove deleted object %s\n",
112                                                  ldb_dn_get_linearized(res->msgs[i]->dn)));
113                                 } else {
114                                         DEBUG(4,("Removed deleted object %s\n",
115                                                  ldb_dn_get_linearized(res->msgs[i]->dn)));
116                                 }
117                         } else if (add_recycled & !ldb_msg_find_ldb_val(res->msgs[i], "isRecycled")) {
118                                 /* There is no isRecycled attribute
119                                  * we used to not set this attribute before
120                                  * let's set it
121                                  */
122
123                                 DEBUG(1, ("Adding isRecycled\n"));
124                                 struct ldb_message *msg = ldb_msg_new(mem_ctx);
125                                 if (msg == NULL) {
126                                         talloc_free(do_dn);
127                                         return NT_STATUS_NO_MEMORY;
128                                 }
129
130                                 msg->dn = res->msgs[i]->dn;
131                                 ret = ldb_msg_add_string(msg, "isRecycled", "TRUE");
132                                 if (ret != LDB_SUCCESS) {
133                                         talloc_free(do_dn);
134                                         return NT_STATUS_NO_MEMORY;
135                                 }
136                                 msg->elements[msg->num_elements - 1].flags = LDB_FLAG_MOD_ADD;
137
138                                 ret = dsdb_modify(s->samdb, msg, DSDB_SEARCH_SHOW_DELETED);
139                                 if (ret != LDB_SUCCESS) {
140                                         talloc_free(do_dn);
141                                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
142                                 }
143                         }
144                 }
145
146                 talloc_free(do_dn);
147         }
148
149         return NT_STATUS_OK;
150 }