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