r23792: convert Samba4 to GPLv3
[tprouty/samba.git] / source4 / nbt_server / wins / wins_ldb.c
1 /* 
2    ldb database module
3
4    Copyright (C) Stefan Metzmacher 2006
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21  *  Name: ldb
22  *
23  *  Component: ldb winsdb module
24  *
25  *  Description: verify winsdb records before they're written to disk
26  *
27  *  Author: Stefan Metzmacher
28  */
29
30 #include "includes.h"
31 #include "nbt_server/nbt_server.h"
32 #include "nbt_server/wins/winsdb.h"
33 #include "lib/ldb/include/ldb.h"
34 #include "lib/ldb/include/ldb_errors.h"
35 #include "lib/ldb/include/ldb_private.h"
36 #include "system/network.h"
37 #include "lib/socket/netif.h"
38
39 static int wins_ldb_verify(struct ldb_module *module, struct ldb_request *req)
40 {
41         struct winsdb_handle *h = talloc_get_type(ldb_get_opaque(module->ldb, "winsdb_handle"),
42                                                   struct winsdb_handle);
43         const struct ldb_message *msg;
44
45         switch (req->operation) {
46         case LDB_ADD:
47                 msg = req->op.add.message;
48                 break;
49                 
50         case LDB_MODIFY:
51                 msg = req->op.mod.message;
52                 break;
53
54         default:
55                 return ldb_next_request(module, req);
56         }
57
58         /* do not manipulate our control entries */
59         if (ldb_dn_is_special(msg->dn)) {
60                 return ldb_next_request(module, req);
61         }
62
63         if (!h) {
64                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL, "%s", "WINS_LDB: INTERNAL ERROR: no winsdb_handle present!");
65                 return LDB_ERR_OTHER;
66         }
67
68         switch (h->caller) {
69         case WINSDB_HANDLE_CALLER_NBTD:
70         case WINSDB_HANDLE_CALLER_WREPL:
71                 /* we trust our nbt and wrepl code ... */
72                 return ldb_next_request(module, req);
73
74         case WINSDB_HANDLE_CALLER_ADMIN:
75                 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "%s\n", "WINS_LDB: TODO verify add/modify for WINSDB_HANDLE_CALLER_ADMIN");
76                 return ldb_next_request(module, req);
77         }
78
79         return LDB_ERR_OTHER;
80 }
81
82 static int wins_ldb_init(struct ldb_module *ctx)
83 {
84         struct winsdb_handle *h;
85         const char *owner;
86
87         ctx->private_data = NULL;
88
89         owner = lp_parm_string(-1, "winsdb", "local_owner");
90         if (!owner) {
91                 owner = iface_n_ip(0);
92                 if (!owner) {
93                         owner = "0.0.0.0";
94                 }
95         }
96
97         h = talloc(ctx, struct winsdb_handle);
98         if (!h) goto failed;
99         h->ldb          = ctx->ldb;
100         h->caller       = WINSDB_HANDLE_CALLER_ADMIN;
101         h->local_owner  = talloc_strdup(h, owner);
102         if (!h->local_owner) goto failed;
103
104         return ldb_set_opaque(ctx->ldb, "winsdb_handle", h);
105
106 failed:
107         talloc_free(h);
108         return LDB_ERR_OTHER;
109 }
110
111 static const struct ldb_module_ops wins_ldb_ops = {
112         .name          = "wins_ldb",
113         .add           = wins_ldb_verify,
114         .modify        = wins_ldb_verify,
115         .init_context  = wins_ldb_init
116 };
117
118
119 /* the init function */
120 int wins_ldb_module_init(void)
121 {
122         return ldb_register_module(&wins_ldb_ops);
123 }