fa61887bd5b4d320b57d9f230700fa8ac58ca8f6
[ira/wip.git] / source4 / dsdb / samdb / ldb_modules / update_keytab.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007
5
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 2 of the License, or
9    (at your option) any later version.
10    
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.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22  *  Name: ldb
23  *
24  *  Component: ldb update_keytabs module
25  *
26  *  Description: Update keytabs whenever their matching secret record changes
27  *
28  *  Author: Andrew Bartlett
29  */
30
31 #include "includes.h"
32 #include "ldb/include/includes.h"
33 #include "auth/credentials/credentials.h"
34 #include "auth/credentials/credentials_krb5.h"
35 #include "system/kerberos.h"
36
37 struct dn_list {
38         struct cli_credentials *creds;
39         struct dn_list *prev, *next;
40 };
41
42 struct update_kt_private {
43         struct dn_list *changed_dns;
44 };
45
46 static int add_modified(struct ldb_module *module, struct ldb_dn *dn, BOOL delete) {
47         struct update_kt_private *data = talloc_get_type(module->private_data, struct update_kt_private);
48         struct dn_list *item = talloc(data->changed_dns? (void *)data->changed_dns: (void *)data, struct dn_list);
49         char *filter;
50         NTSTATUS status;
51         if (!item) {
52                 ldb_oom(module->ldb);
53                 return LDB_ERR_OPERATIONS_ERROR;
54         }
55         
56         item->creds = cli_credentials_init(item);
57         if (!item->creds) {
58                 DEBUG(1, ("cli_credentials_init failed!"));
59                 ldb_oom(module->ldb);
60                 return LDB_ERR_OPERATIONS_ERROR;
61         }
62
63         cli_credentials_set_conf(item->creds);
64 /*      filter = talloc_asprintf(item, "(&(&(&(objectClass=kerberosSecret)(privateKeytab=*))(|(secret=*)(ntPwdHash=*)))(distinguishedName=%s))", */ 
65         filter = talloc_asprintf(item, "dn=%s",
66                                  ldb_dn_get_linearized(dn));
67         status = cli_credentials_set_secrets(item->creds, module->ldb, NULL, filter);
68         talloc_free(filter);
69         if (NT_STATUS_IS_OK(status)) {
70                 if (delete) {
71                         /* Ensure we don't helpfully keep an old keytab entry */
72                         cli_credentials_set_kvno(item->creds, cli_credentials_get_kvno(item->creds)+2); 
73                         /* Wipe passwords */
74                         cli_credentials_set_nt_hash(item->creds, NULL, 
75                                                     CRED_SPECIFIED);
76                 }
77                 DLIST_ADD_END(data->changed_dns, item, struct dn_list *);
78         }
79         return LDB_SUCCESS;
80 }
81
82 /* add */
83 static int update_kt_add(struct ldb_module *module, struct ldb_request *req)
84 {
85         int ret;
86         ret = ldb_next_request(module, req);
87         if (ret != LDB_SUCCESS) {
88                 return ret;
89         }
90         return add_modified(module, req->op.add.message->dn, False);
91 }
92
93 /* modify */
94 static int update_kt_modify(struct ldb_module *module, struct ldb_request *req)
95 {
96         int ret;
97         ret = ldb_next_request(module, req);
98         if (ret != LDB_SUCCESS) {
99                 return ret;
100         }
101         return add_modified(module, req->op.mod.message->dn, False);
102 }
103
104 /* delete */
105 static int update_kt_delete(struct ldb_module *module, struct ldb_request *req)
106 {
107         int ret;
108         /* Before we delete it, record the details */
109         ret = add_modified(module, req->op.del.dn, True);
110         if (ret != LDB_SUCCESS) {
111                 return ret;
112         }
113         return ldb_next_request(module, req);
114 }
115
116 /* rename */
117 static int update_kt_rename(struct ldb_module *module, struct ldb_request *req)
118 {
119         int ret;
120         ret = ldb_next_request(module, req);
121         if (ret != LDB_SUCCESS) {
122                 return ret;
123         }
124         return add_modified(module, req->op.rename.newdn, False);
125 }
126
127 /* end a transaction */
128 static int update_kt_end_trans(struct ldb_module *module)
129 {
130         struct update_kt_private *data = talloc_get_type(module->private_data, struct update_kt_private);
131         
132         struct dn_list *p;
133         for (p=data->changed_dns; p; p = p->next) {
134                 int kret;
135                 kret = cli_credentials_update_keytab(p->creds);
136                 if (kret != 0) {
137                         talloc_free(data->changed_dns);
138                         data->changed_dns = NULL;
139                         ldb_asprintf_errstring(module->ldb, "Failed to update keytab: %s", error_message(kret));
140                         return LDB_ERR_OPERATIONS_ERROR;
141                 }
142         }
143
144         talloc_free(data->changed_dns);
145         data->changed_dns = NULL;
146         return ldb_next_end_trans(module);
147 }
148
149 /* end a transaction */
150 static int update_kt_del_trans(struct ldb_module *module)
151 {
152         struct update_kt_private *data = talloc_get_type(module->private_data, struct update_kt_private);
153         
154         talloc_free(data->changed_dns);
155         data->changed_dns = NULL;
156
157         return ldb_next_end_trans(module);
158 }
159
160 static int update_kt_init(struct ldb_module *module)
161 {
162         struct update_kt_private *data;
163
164         data = talloc(module, struct update_kt_private);
165         if (data == NULL) {
166                 ldb_oom(module->ldb);
167                 return LDB_ERR_OPERATIONS_ERROR;
168         }
169
170         module->private_data = data;
171         data->changed_dns = NULL;
172
173         return ldb_next_init(module);
174 }
175
176 static const struct ldb_module_ops update_kt_ops = {
177         .name              = "update_keytab",
178         .init_context      = update_kt_init,
179         .add               = update_kt_add,
180         .modify            = update_kt_modify,
181         .rename            = update_kt_rename,
182         .del               = update_kt_delete,
183         .end_transaction   = update_kt_end_trans,
184         .del_transaction   = update_kt_del_trans,
185 };
186
187 int ldb_update_kt_init(void)
188 {
189         return ldb_register_module(&update_kt_ops);
190 }