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