Avoid including libds/common/roles.h in public loadparm.h header.
[obnox/samba/samba-obnox.git] / source4 / nbt_server / register.c
index 958ab42a3dda4f9e48083749a424153ecb68c283..07f4e20e725fdba5c4642eabba9d624c61033e49 100644 (file)
@@ -7,7 +7,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 #include "lib/events/events.h"
-#include "dlinklist.h"
+#include "../lib/util/dlinklist.h"
 #include "nbt_server/nbt_server.h"
 #include "smbd/service_task.h"
 #include "libcli/composite/composite.h"
 #include "librpc/gen_ndr/ndr_samr.h"
 #include "nbt_server/wins/winsserver.h"
-
+#include "librpc/gen_ndr/ndr_nbt.h"
+#include "dsdb/samdb/samdb.h"
+#include "param/param.h"
+#include "libds/common/roles.h"
 
 static void nbtd_start_refresh_timer(struct nbtd_iface_name *iname);
 
@@ -37,7 +39,7 @@ static void nbtd_start_refresh_timer(struct nbtd_iface_name *iname);
 */
 static void refresh_completion_handler(struct nbt_name_request *req)
 {
-       struct nbtd_iface_name *iname = talloc_get_type(req->async.private
+       struct nbtd_iface_name *iname = talloc_get_type(req->async.private_data,
                                                        struct nbtd_iface_name);
        NTSTATUS status;
        struct nbt_name_refresh io;
@@ -76,7 +78,7 @@ static void refresh_completion_handler(struct nbt_name_request *req)
 /*
   handle name refresh timer events
 */
-static void name_refresh_handler(struct event_context *ev, struct timed_event *te, 
+static void name_refresh_handler(struct tevent_context *ev, struct tevent_timer *te, 
                                 struct timeval t, void *private_data)
 {
        struct nbtd_iface_name *iname = talloc_get_type(private_data, struct nbtd_iface_name);
@@ -92,12 +94,13 @@ static void name_refresh_handler(struct event_context *ev, struct timed_event *t
           registration packets */
        io.in.name            = iname->name;
        io.in.dest_addr       = iface->bcast_address;
+       io.in.dest_port       = lpcfg_nbt_port(iface->nbtsrv->task->lp_ctx);
        io.in.address         = iface->ip_address;
        io.in.nb_flags        = iname->nb_flags;
        io.in.ttl             = iname->ttl;
-       io.in.register_demand = False;
-       io.in.broadcast       = True;
-       io.in.multi_homed     = False;
+       io.in.register_demand = false;
+       io.in.broadcast       = true;
+       io.in.multi_homed     = false;
        io.in.timeout         = 3;
        io.in.retries         = 0;
 
@@ -106,7 +109,7 @@ static void name_refresh_handler(struct event_context *ev, struct timed_event *t
        if (req == NULL) return;
 
        req->async.fn = refresh_completion_handler;
-       req->async.private = iname;
+       req->async.private_data = iname;
 }
 
 
@@ -116,36 +119,42 @@ static void name_refresh_handler(struct event_context *ev, struct timed_event *t
 static void nbtd_start_refresh_timer(struct nbtd_iface_name *iname)
 {
        uint32_t refresh_time;
-       uint32_t max_refresh_time = lp_parm_int(-1, "nbtd", "max_refresh_time", 7200);
+       uint32_t max_refresh_time = lpcfg_parm_int(iname->iface->nbtsrv->task->lp_ctx, NULL, "nbtd", "max_refresh_time", 7200);
 
        refresh_time = MIN(max_refresh_time, iname->ttl/2);
        
-       event_add_timed(iname->iface->nbtsrv->task->event_ctx, 
+       tevent_add_timer(iname->iface->nbtsrv->task->event_ctx,
                        iname, 
                        timeval_add(&iname->registration_time, refresh_time, 0),
                        name_refresh_handler, iname);
 }
 
+struct nbtd_register_name_state {
+       struct nbtd_iface_name *iname;
+       struct nbt_name_register_bcast io;
+};
 
 /*
   a name registration has completed
 */
-static void nbtd_register_handler(struct composite_context *creq)
+static void nbtd_register_name_handler(struct tevent_req *subreq)
 {
-       struct nbtd_iface_name *iname = talloc_get_type(creq->async.private_data, 
-                                                       struct nbtd_iface_name);
+       struct nbtd_register_name_state *state =
+               tevent_req_callback_data(subreq,
+               struct nbtd_register_name_state);
+       struct nbtd_iface_name *iname = state->iname;
        NTSTATUS status;
-       TALLOC_CTX *tmp_ctx = talloc_new(iname);
 
-       status = nbt_name_register_bcast_recv(creq);
+       status = nbt_name_register_bcast_recv(subreq);
+       TALLOC_FREE(subreq);
        if (NT_STATUS_IS_OK(status)) {
                /* good - nobody complained about our registration */
                iname->nb_flags |= NBT_NM_ACTIVE;
                DEBUG(3,("Registered %s with %s on interface %s\n",
-                        nbt_name_string(tmp_ctx, &iname->name), 
+                        nbt_name_string(state, &iname->name),
                         iname->iface->ip_address, iname->iface->bcast_address));
                iname->registration_time = timeval_current();
-               talloc_free(tmp_ctx);
+               talloc_free(state);
                nbtd_start_refresh_timer(iname);
                return;
        }
@@ -154,10 +163,10 @@ static void nbtd_register_handler(struct composite_context *creq)
        iname->nb_flags |= NBT_NM_CONFLICT;
 
        DEBUG(1,("Error registering %s with %s on interface %s - %s\n",
-                nbt_name_string(tmp_ctx, &iname->name),
+                nbt_name_string(state, &iname->name),
                 iname->iface->ip_address, iname->iface->bcast_address,
                 nt_errstr(status)));
-       talloc_free(tmp_ctx);
+       talloc_free(state);
 }
 
 
@@ -169,9 +178,9 @@ static void nbtd_register_name_iface(struct nbtd_interface *iface,
                                     uint16_t nb_flags)
 {
        struct nbtd_iface_name *iname;
-       const char *scope = lp_netbios_scope();
-       struct nbt_name_register_bcast io;
-       struct composite_context *creq;
+       const char *scope = lpcfg_netbios_scope(iface->nbtsrv->task->lp_ctx);
+       struct nbtd_register_name_state *state;
+       struct tevent_req *subreq;
        struct nbtd_server *nbtsrv = iface->nbtsrv;
 
        iname = talloc(iface, struct nbtd_iface_name);
@@ -186,7 +195,7 @@ static void nbtd_register_name_iface(struct nbtd_interface *iface,
                iname->name.scope = NULL;
        }
        iname->nb_flags          = nb_flags;
-       iname->ttl               = lp_parm_int(-1, "nbtd", "bcast_ttl", 300000);
+       iname->ttl               = lpcfg_parm_int(iface->nbtsrv->task->lp_ctx, NULL, "nbtd", "bcast_ttl", 300000);
        iname->registration_time = timeval_zero();
        iname->wins_server       = NULL;
 
@@ -206,28 +215,39 @@ static void nbtd_register_name_iface(struct nbtd_interface *iface,
                return;
        }
 
+       state = talloc_zero(iname, struct nbtd_register_name_state);
+       if (state == NULL) {
+               return;
+       }
+
+       state->iname = iname;
+
        /* setup a broadcast name registration request */
-       io.in.name            = iname->name;
-       io.in.dest_addr       = iface->bcast_address;
-       io.in.address         = iface->ip_address;
-       io.in.nb_flags        = nb_flags;
-       io.in.ttl             = iname->ttl;
+       state->io.in.name      = iname->name;
+       state->io.in.dest_addr = iface->bcast_address;
+       state->io.in.dest_port = lpcfg_nbt_port(iface->nbtsrv->task->lp_ctx);
+       state->io.in.address   = iface->ip_address;
+       state->io.in.nb_flags  = nb_flags;
+       state->io.in.ttl       = iname->ttl;
 
        nbtsrv->stats.total_sent++;
-       creq = nbt_name_register_bcast_send(iface->nbtsock, &io);
-       if (creq == NULL) return;
 
-       creq->async.fn = nbtd_register_handler;
-       creq->async.private_data = iname;
+       subreq = nbt_name_register_bcast_send(state, nbtsrv->task->event_ctx,
+                                             iface->nbtsock, &state->io);
+       if (subreq == NULL) {
+               return;
+       }
+
+       tevent_req_set_callback(subreq, nbtd_register_name_handler, state);
 }
 
 
 /*
   register one name on all our interfaces
 */
-static void nbtd_register_name(struct nbtd_server *nbtsrv, 
-                              const char *name, enum nbt_name_type type,
-                              uint16_t nb_flags)
+void nbtd_register_name(struct nbtd_server *nbtsrv, 
+                       const char *name, enum nbt_name_type type,
+                       uint16_t nb_flags)
 {
        struct nbtd_interface *iface;
        
@@ -259,30 +279,29 @@ void nbtd_register_names(struct nbtd_server *nbtsrv)
 
        /* note that we don't initially mark the names "ACTIVE". They are 
           marked active once registration is successful */
-       nbtd_register_name(nbtsrv, lp_netbios_name(), NBT_NAME_CLIENT, nb_flags);
-       nbtd_register_name(nbtsrv, lp_netbios_name(), NBT_NAME_USER,   nb_flags);
-       nbtd_register_name(nbtsrv, lp_netbios_name(), NBT_NAME_SERVER, nb_flags);
+       nbtd_register_name(nbtsrv, lpcfg_netbios_name(nbtsrv->task->lp_ctx), NBT_NAME_CLIENT, nb_flags);
+       nbtd_register_name(nbtsrv, lpcfg_netbios_name(nbtsrv->task->lp_ctx), NBT_NAME_USER,   nb_flags);
+       nbtd_register_name(nbtsrv, lpcfg_netbios_name(nbtsrv->task->lp_ctx), NBT_NAME_SERVER, nb_flags);
 
-       aliases = lp_netbios_aliases();
+       aliases = lpcfg_netbios_aliases(nbtsrv->task->lp_ctx);
        while (aliases && aliases[0]) {
                nbtd_register_name(nbtsrv, aliases[0], NBT_NAME_CLIENT, nb_flags);
                nbtd_register_name(nbtsrv, aliases[0], NBT_NAME_SERVER, nb_flags);
                aliases++;
        }
 
-       switch (lp_server_role()) {
-       case ROLE_DOMAIN_PDC:
-               nbtd_register_name(nbtsrv, lp_workgroup(),    NBT_NAME_PDC, nb_flags);
-               nbtd_register_name(nbtsrv, lp_workgroup(),    NBT_NAME_LOGON, nb_flags | NBT_NM_GROUP);
-               break;
-       case ROLE_DOMAIN_BDC:
-               nbtd_register_name(nbtsrv, lp_workgroup(),    NBT_NAME_LOGON, nb_flags | NBT_NM_GROUP);
-       default:
-               break;
+       if (lpcfg_server_role(nbtsrv->task->lp_ctx) == ROLE_ACTIVE_DIRECTORY_DC)        {
+               bool is_pdc = samdb_is_pdc(nbtsrv->sam_ctx);
+               if (is_pdc) {
+                       nbtd_register_name(nbtsrv, lpcfg_workgroup(nbtsrv->task->lp_ctx),
+                                          NBT_NAME_PDC, nb_flags);
+               }
+               nbtd_register_name(nbtsrv, lpcfg_workgroup(nbtsrv->task->lp_ctx),
+                                  NBT_NAME_LOGON, nb_flags | NBT_NM_GROUP);
        }
 
        nb_flags |= NBT_NM_GROUP;
-       nbtd_register_name(nbtsrv, lp_workgroup(),    NBT_NAME_CLIENT, nb_flags);
+       nbtd_register_name(nbtsrv, lpcfg_workgroup(nbtsrv->task->lp_ctx), NBT_NAME_CLIENT, nb_flags);
 
        nb_flags |= NBT_NM_PERMANENT;
        nbtd_register_name(nbtsrv, "__SAMBA__",       NBT_NAME_CLIENT, nb_flags);