a5e7031a26f116817a6fe462205fba665f55ee73
[jra/samba/.git] / source4 / dsdb / samdb / ldb_modules / pdc_fsmo.c
1 /* 
2    Unix SMB/CIFS mplementation.
3
4    The module that handles the PDC FSMO Role Owner checkings
5    
6    Copyright (C) Stefan Metzmacher 2007
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/ldb/include/ldb.h"
25 #include "lib/ldb/include/ldb_errors.h"
26 #include "lib/ldb/include/ldb_private.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "librpc/gen_ndr/ndr_misc.h"
29 #include "librpc/gen_ndr/ndr_drsuapi.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "lib/util/dlinklist.h"
32
33 static int pdc_fsmo_init(struct ldb_module *module)
34 {
35         TALLOC_CTX *mem_ctx;
36         struct ldb_dn *pdc_dn;
37         struct dsdb_pdc_fsmo *pdc_fsmo;
38         struct ldb_result *pdc_res;
39         int ret;
40         static const char *pdc_attrs[] = {
41                 "fSMORoleOwner",
42                 NULL
43         };
44
45         mem_ctx = talloc_new(module);
46         if (!mem_ctx) {
47                 ldb_oom(module->ldb);
48                 return LDB_ERR_OPERATIONS_ERROR;
49         }
50
51         pdc_dn = samdb_base_dn(module->ldb);
52         if (!pdc_dn) {
53                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
54                           "pdc_fsmo_init: no domain dn present: (skip loading of domain details)\n");
55                 talloc_free(mem_ctx);
56                 return ldb_next_init(module);
57         }
58
59         pdc_fsmo = talloc_zero(mem_ctx, struct dsdb_pdc_fsmo);
60         if (!pdc_fsmo) {
61                 ldb_oom(module->ldb);
62                 return LDB_ERR_OPERATIONS_ERROR;
63         }
64         module->private_data = pdc_fsmo;
65
66         ret = ldb_search(module->ldb, mem_ctx, &pdc_res,
67                          pdc_dn, LDB_SCOPE_BASE,
68                          pdc_attrs, NULL);
69         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
70                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
71                           "pdc_fsmo_init: no domain object present: (skip loading of domain details)\n");
72                 talloc_free(mem_ctx);
73                 return ldb_next_init(module);
74         } else if (ret != LDB_SUCCESS) {
75                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
76                               "pdc_fsmo_init: failed to search the domain object: %d:%s",
77                               ret, ldb_strerror(ret));
78                 talloc_free(mem_ctx);
79                 return ret;
80         }
81         if (pdc_res->count == 0) {
82                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
83                           "pdc_fsmo_init: no domain object present: (skip loading of domain details)\n");
84                 talloc_free(mem_ctx);
85                 return ldb_next_init(module);
86         } else if (pdc_res->count > 1) {
87                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
88                               "pdc_fsmo_init: [%u] domain objects found on a base search",
89                               pdc_res->count);
90                 talloc_free(mem_ctx);
91                 return LDB_ERR_CONSTRAINT_VIOLATION;
92         }
93
94         pdc_fsmo->master_dn = ldb_msg_find_attr_as_dn(module->ldb, mem_ctx, pdc_res->msgs[0], "fSMORoleOwner");
95         if (ldb_dn_compare(samdb_ntds_settings_dn(module->ldb), pdc_fsmo->master_dn) == 0) {
96                 pdc_fsmo->we_are_master = true;
97         } else {
98                 pdc_fsmo->we_are_master = false;
99         }
100
101         if (ldb_set_opaque(module->ldb, "dsdb_pdc_fsmo", pdc_fsmo) != LDB_SUCCESS) {
102                 ldb_oom(module->ldb);
103                 return LDB_ERR_OPERATIONS_ERROR;
104         }
105
106         talloc_steal(module, pdc_fsmo);
107
108         ldb_debug(module->ldb, LDB_DEBUG_TRACE,
109                           "pdc_fsmo_init: we are master: %s\n",
110                           (pdc_fsmo->we_are_master?"yes":"no"));
111
112         talloc_free(mem_ctx);
113         return ldb_next_init(module);
114 }
115
116 _PUBLIC_ const struct ldb_module_ops ldb_pdc_fsmo_module_ops = {
117         .name           = "pdc_fsmo",
118         .init_context   = pdc_fsmo_init
119 };