r13924: Split more prototypes out of include/proto.h + initial work on header
[metze/samba/wip.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 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 winsdb module
25  *
26  *  Description: verify winsdb records before they're written to disk
27  *
28  *  Author: Stefan Metzmacher
29  */
30
31 #include "includes.h"
32 #include "nbt_server/nbt_server.h"
33 #include "nbt_server/wins/winsdb.h"
34 #include "lib/ldb/include/ldb.h"
35 #include "lib/ldb/include/ldb_errors.h"
36 #include "lib/ldb/include/ldb_private.h"
37 #include "system/network.h"
38 #include "netif/netif.h"
39
40 /* add_record: do things with the sambaPassword attribute */
41 static int wins_ldb_verify(struct ldb_module *module, struct ldb_request *req, const struct ldb_message *msg)
42 {
43         struct winsdb_handle *h = talloc_get_type(ldb_get_opaque(module->ldb, "winsdb_handle"),
44                                                   struct winsdb_handle);
45         char *error = NULL;
46
47         if (!h) {
48                 error = talloc_strdup(module, "WINS_LDB: INTERNAL ERROR: no winsdb_handle present!");
49                 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "%s", error);
50                 ldb_set_errstring(module->ldb, error);
51                 return LDB_ERR_OTHER;
52         }
53
54         switch (h->caller) {
55         case WINSDB_HANDLE_CALLER_NBTD:
56         case WINSDB_HANDLE_CALLER_WREPL:
57                 /* we trust our nbt and wrepl code ... */
58                 return ldb_next_request(module, req);
59
60         case WINSDB_HANDLE_CALLER_ADMIN:
61                 error = talloc_strdup(module, "WINS_LDB: TODO verify add/modify for WINSDB_HANDLE_CALLER_ADMIN");
62                 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "%s\n", error);
63                 return ldb_next_request(module, req);
64         }
65
66         return LDB_ERR_OTHER;
67 }
68
69 static int wins_ldb_request(struct ldb_module *module, struct ldb_request *req)
70 {
71         const struct ldb_message *msg = req->op.mod.message;
72
73         switch (req->operation) {
74         case LDB_REQ_ADD:
75                 msg = req->op.add.message;
76                 break;
77
78         case LDB_REQ_MODIFY:
79                 msg = req->op.mod.message;
80                 break;
81
82         default:
83                 goto call_next;
84         }
85
86         if (ldb_dn_is_special(msg->dn)) goto call_next;
87
88         return wins_ldb_verify(module, req, msg);
89
90 call_next:
91         return ldb_next_request(module, req);   
92 }
93
94 static int wins_ldb_init(struct ldb_module *ctx)
95 {
96         struct winsdb_handle *h;
97         const char *owner;
98
99         ctx->private_data = NULL;
100
101         owner = lp_parm_string(-1, "winsdb", "local_owner");
102         if (!owner) {
103                 owner = iface_n_ip(0);
104                 if (!owner) {
105                         owner = "0.0.0.0";
106                 }
107         }
108
109         h = talloc(ctx, struct winsdb_handle);
110         if (!h) goto failed;
111         h->ldb          = ctx->ldb;
112         h->caller       = WINSDB_HANDLE_CALLER_ADMIN;
113         h->local_owner  = talloc_strdup(h, owner);
114         if (!h->local_owner) goto failed;
115
116         return ldb_set_opaque(ctx->ldb, "winsdb_handle", h);
117
118 failed:
119         talloc_free(h);
120         return LDB_ERR_OTHER;
121 }
122
123 static const struct ldb_module_ops wins_ldb_ops = {
124         .name          = "wins_ldb",
125         .request       = wins_ldb_request,
126         .init_context  = wins_ldb_init
127 };
128
129
130 /* the init function */
131 int wins_ldb_module_init(void)
132 {
133         return ldb_register_module(&wins_ldb_ops);
134 }