RIP BOOL. Convert BOOL -> bool. I found a few interesting
[nivanova/samba-autobuild/.git] / source3 / nmbd / nmbd_winsserver.c
index 86f7fd58502852af3ca0c259aa1ec4918400045c..38962c2b3941f954208d9043ab328f06d670e7d5 100644 (file)
@@ -6,7 +6,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,
@@ -15,8 +15,7 @@
    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/>.
    
    Converted to store WINS data in a tdb. Dec 2005. JRA.
 */
 
 TDB_CONTEXT *wins_tdb;
 
+/****************************************************************************
+ Delete all the temporary name records on the in-memory linked list.
+*****************************************************************************/
+
+static void wins_delete_all_tmp_in_memory_records(void)
+{
+       struct name_record *nr = NULL;
+       struct name_record *nrnext = NULL;
+
+       /* Delete all temporary name records on the wins subnet linked list. */
+       for( nr = wins_server_subnet->namelist; nr; nr = nrnext) {
+               nrnext = nr->next;
+               DLIST_REMOVE(wins_server_subnet->namelist, nr);
+               SAFE_FREE(nr->data.ip);
+               SAFE_FREE(nr);
+       }
+}
+
 /****************************************************************************
  Convert a wins.tdb record to a struct name_record. Add in our global_scope().
 *****************************************************************************/
@@ -85,7 +102,7 @@ static struct name_record *wins_record_to_name_record(TDB_DATA key, TDB_DATA dat
        }
 
        namerec->subnet = wins_server_subnet;
-       push_ascii_nstring(namerec->name.name, key.dptr);
+       push_ascii_nstring(namerec->name.name, (const char *)key.dptr);
        namerec->name.name_type = key.dptr[sizeof(unstring)];
        /* Add the scope. */
        push_ascii(namerec->name.scope, global_scope(), 64, STR_TERMINATE);
@@ -141,7 +158,7 @@ static TDB_DATA name_record_to_wins_record(const struct name_record *namerec)
        len = (2 + 1 + (7*4)); /* "wbddddddd" */
        len += (namerec->data.num_ips * 4);
 
-       data.dptr = SMB_MALLOC(len);
+       data.dptr = (uint8 *)SMB_MALLOC(len);
        if (!data.dptr) {
                return data;
        }
@@ -179,7 +196,7 @@ static TDB_DATA name_to_key(const struct nmb_name *nmbname)
        pull_ascii_nstring(keydata, sizeof(unstring), nmbname->name);
        strupper_m(keydata);
        keydata[sizeof(unstring)] = nmbname->name_type;
-       key.dptr = keydata;
+       key.dptr = (uint8 *)keydata;
        key.dsize = sizeof(keydata);
 
        return key;
@@ -190,7 +207,7 @@ static TDB_DATA name_to_key(const struct nmb_name *nmbname)
  on the linked list. We will free this later in XXXX().
 *****************************************************************************/
 
-struct name_record *find_name_on_wins_subnet(const struct nmb_name *nmbname, BOOL self_only)
+struct name_record *find_name_on_wins_subnet(const struct nmb_name *nmbname, bool self_only)
 {
        TDB_DATA data, key;
        struct name_record *nr = NULL;
@@ -217,6 +234,14 @@ struct name_record *find_name_on_wins_subnet(const struct nmb_name *nmbname, BOO
                return NULL;
        }
 
+       /* Self names only - these include permanent names. */
+       if( self_only && (namerec->data.source != SELF_NAME) && (namerec->data.source != PERMANENT_NAME) ) {
+               DEBUG( 9, ( "find_name_on_wins_subnet: self name %s NOT FOUND\n", nmb_namestr(nmbname) ) );
+               SAFE_FREE(namerec->data.ip);
+               SAFE_FREE(namerec);
+               return NULL;
+       }
+
        /* Search for this name record on the list. Replace it if found. */
 
        for( nr = wins_server_subnet->namelist; nr; nr = nr->next) {
@@ -237,7 +262,7 @@ struct name_record *find_name_on_wins_subnet(const struct nmb_name *nmbname, BOO
  Overwrite or add a given name in the wins.tdb.
 *****************************************************************************/
 
-static BOOL store_or_replace_wins_namerec(const struct name_record *namerec, int tdb_flag)
+static bool store_or_replace_wins_namerec(const struct name_record *namerec, int tdb_flag)
 {
        TDB_DATA key, data;
        int ret;
@@ -263,7 +288,7 @@ static BOOL store_or_replace_wins_namerec(const struct name_record *namerec, int
  Overwrite a given name in the wins.tdb.
 *****************************************************************************/
 
-BOOL wins_store_changed_namerec(const struct name_record *namerec)
+bool wins_store_changed_namerec(const struct name_record *namerec)
 {
        return store_or_replace_wins_namerec(namerec, TDB_REPLACE);
 }
@@ -272,7 +297,7 @@ BOOL wins_store_changed_namerec(const struct name_record *namerec)
  Primary interface into creating and overwriting records in the wins.tdb.
 *****************************************************************************/
 
-BOOL add_name_to_wins_subnet(const struct name_record *namerec)
+bool add_name_to_wins_subnet(const struct name_record *namerec)
 {
        return store_or_replace_wins_namerec(namerec, TDB_INSERT);
 }
@@ -282,7 +307,7 @@ BOOL add_name_to_wins_subnet(const struct name_record *namerec)
  on the linked list.
 *****************************************************************************/
 
-BOOL remove_name_from_wins_namelist(struct name_record *namerec)
+bool remove_name_from_wins_namelist(struct name_record *namerec)
 {
        TDB_DATA key;
        int ret;
@@ -387,7 +412,7 @@ static void update_wins_flag(struct name_record *namerec, int flags)
  Return the general ID value and increase it if requested.
 *****************************************************************************/
 
-static void get_global_id_and_update(SMB_BIG_UINT *current_id, BOOL update)
+static void get_global_id_and_update(SMB_BIG_UINT *current_id, bool update)
 {
        /*
         * it's kept as a static here, to prevent people from messing
@@ -457,7 +482,7 @@ static void wins_hook(const char *operation, struct name_record *namerec, int tt
 Determine if this packet should be allocated to the WINS server.
 *****************************************************************************/
 
-BOOL packet_is_for_wins_server(struct packet_struct *packet)
+bool packet_is_for_wins_server(struct packet_struct *packet)
 {
        struct nmb_packet *nmb = &packet->packet.nmb;
 
@@ -537,7 +562,7 @@ static int get_ttl_from_packet(struct nmb_packet *nmb)
 Load or create the WINS database.
 *****************************************************************************/
 
-BOOL initialise_wins(void)
+bool initialise_wins(void)
 {
        time_t time_now = time(NULL);
        XFILE *fp;
@@ -575,8 +600,8 @@ BOOL initialise_wins(void)
                int ttl;
                const char *ptr;
                char *p;
-               BOOL got_token;
-               BOOL was_ip;
+               bool got_token;
+               bool was_ip;
                int i;
                unsigned int hash;
                int version;
@@ -779,9 +804,9 @@ void wins_process_name_refresh_request( struct subnet_record *subrec,
 {
        struct nmb_packet *nmb = &p->packet.nmb;
        struct nmb_name *question = &nmb->question.question_name;
-       BOOL bcast = nmb->header.nm_flags.bcast;
+       bool bcast = nmb->header.nm_flags.bcast;
        uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
-       BOOL group = (nb_flags & NB_GROUP) ? True : False;
+       bool group = (nb_flags & NB_GROUP) ? True : False;
        struct name_record *namerec = NULL;
        int ttl = get_ttl_from_packet(nmb);
        struct in_addr from_ip;
@@ -1085,12 +1110,12 @@ void wins_process_name_registration_request(struct subnet_record *subrec,
        unstring name;
        struct nmb_packet *nmb = &p->packet.nmb;
        struct nmb_name *question = &nmb->question.question_name;
-       BOOL bcast = nmb->header.nm_flags.bcast;
+       bool bcast = nmb->header.nm_flags.bcast;
        uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
        int ttl = get_ttl_from_packet(nmb);
        struct name_record *namerec = NULL;
        struct in_addr from_ip;
-       BOOL registering_group_name = (nb_flags & NB_GROUP) ? True : False;
+       bool registering_group_name = (nb_flags & NB_GROUP) ? True : False;
        struct in_addr our_fake_ip = *interpret_addr2("0.0.0.0");
 
        putip((char *)&from_ip,&nmb->additional->rdata[2]);
@@ -1248,7 +1273,7 @@ already exists in WINS as a GROUP name.\n", nmb_namestr(question) ));
        if ( namerec != NULL ) {
                pull_ascii_nstring(name, sizeof(name), namerec->name.name);
                if( is_myname(name) ) {
-                       if(!ismyip(from_ip)) {
+                       if(!ismyip_v4(from_ip)) {
                                DEBUG(3,("wins_process_name_registration_request: Attempt to register name %s. Name \
 is one of our (WINS server) names. Denying registration.\n", nmb_namestr(question) ));
                                send_wins_name_registration_response(RFS_ERR, 0, p);
@@ -1455,12 +1480,12 @@ void wins_process_multihomed_name_registration_request( struct subnet_record *su
 {
        struct nmb_packet *nmb = &p->packet.nmb;
        struct nmb_name *question = &nmb->question.question_name;
-       BOOL bcast = nmb->header.nm_flags.bcast;
+       bool bcast = nmb->header.nm_flags.bcast;
        uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
        int ttl = get_ttl_from_packet(nmb);
        struct name_record *namerec = NULL;
        struct in_addr from_ip;
-       BOOL group = (nb_flags & NB_GROUP) ? True : False;
+       bool group = (nb_flags & NB_GROUP) ? True : False;
        struct in_addr our_fake_ip = *interpret_addr2("0.0.0.0");
        unstring qname;
 
@@ -1570,7 +1595,7 @@ already exists in WINS as a GROUP name.\n", nmb_namestr(question) ));
         */
 
        if((namerec != NULL) && (is_myname(namerec->name.name)) ) {
-               if(!ismyip(from_ip)) {
+               if(!ismyip_v4(from_ip)) {
                        DEBUG(3,("wins_process_multihomed_name_registration_request: Attempt to register name %s. Name \
 is one of our (WINS server) names. Denying registration.\n", nmb_namestr(question) ));
                        send_wins_name_registration_response(RFS_ERR, 0, p);
@@ -1739,6 +1764,11 @@ static void process_wins_dmb_query_request(struct subnet_record *subrec,
 
        num_ips = 0;
 
+       /* First, clear the in memory list - we're going to re-populate
+          it with the tdb_traversal in fetch_all_active_wins_1b_names. */
+
+       wins_delete_all_tmp_in_memory_records();
+
        fetch_all_active_wins_1b_names();
 
        for( namerec = subrec->namelist; namerec; namerec = namerec->next ) {
@@ -1964,11 +1994,11 @@ void wins_process_name_release_request(struct subnet_record *subrec,
 {
        struct nmb_packet *nmb = &p->packet.nmb;
        struct nmb_name *question = &nmb->question.question_name;
-       BOOL bcast = nmb->header.nm_flags.bcast;
+       bool bcast = nmb->header.nm_flags.bcast;
        uint16 nb_flags = get_nb_flags(nmb->additional->rdata);
        struct name_record *namerec = NULL;
        struct in_addr from_ip;
-       BOOL releasing_group_name = (nb_flags & NB_GROUP) ? True : False;;
+       bool releasing_group_name = (nb_flags & NB_GROUP) ? True : False;;
 
        putip((char *)&from_ip,&nmb->additional->rdata[2]);
 
@@ -2080,7 +2110,7 @@ release name %s as this record is not active anymore.\n", nmb_namestr(question)
 static int wins_processing_traverse_fn(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
 {
        time_t t = *(time_t *)state;
-       BOOL store_record = False;
+       bool store_record = False;
        struct name_record *namerec = NULL;
        struct in_addr our_fake_ip = *interpret_addr2("0.0.0.0");
 
@@ -2109,7 +2139,7 @@ static int wins_processing_traverse_fn(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA
 
                /* handle records, samba is the wins owner */
                if (ip_equal(namerec->data.wins_ip, our_fake_ip)) {
-                       switch (namerec->data.wins_flags | WINS_STATE_MASK) {
+                       switch (namerec->data.wins_flags & WINS_STATE_MASK) {
                                case WINS_ACTIVE:
                                        namerec->data.wins_flags&=~WINS_STATE_MASK;
                                        namerec->data.wins_flags|=WINS_RELEASED;
@@ -2134,7 +2164,7 @@ static int wins_processing_traverse_fn(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA
                                        goto done;
                        }
                } else {
-                       switch (namerec->data.wins_flags | WINS_STATE_MASK) {
+                       switch (namerec->data.wins_flags & WINS_STATE_MASK) {
                                case WINS_ACTIVE:
                                        /* that's not as MS says it should be */
                                        namerec->data.wins_flags&=~WINS_STATE_MASK;
@@ -2176,8 +2206,6 @@ we are not the wins owner !\n", nmb_namestr(&namerec->name)));
 void initiate_wins_processing(time_t t)
 {
        static time_t lasttime = 0;
-       struct name_record *nr = NULL;
-       struct name_record *nrnext = NULL;
 
        if (!lasttime) {
                lasttime = t;
@@ -2193,14 +2221,7 @@ void initiate_wins_processing(time_t t)
 
        tdb_traverse(wins_tdb, wins_processing_traverse_fn, &t);
 
-       
-       /* Delete all temporary name records on the wins subnet linked list. */
-       for( nr = wins_server_subnet->namelist; nr; nr = nrnext) {
-               nrnext = nr->next;
-               DLIST_REMOVE(wins_server_subnet->namelist, nr);
-               SAFE_FREE(nr->data.ip);
-               SAFE_FREE(nr);
-       }
+       wins_delete_all_tmp_in_memory_records();
 
        wins_write_database(t, True);
 
@@ -2281,7 +2302,7 @@ static int wins_writedb_traverse_fn(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA db
 }
 
 
-void wins_write_database(time_t t, BOOL background)
+void wins_write_database(time_t t, bool background)
 {
        static time_t last_write_time = 0;
        pstring fname, fnamenew;
@@ -2311,6 +2332,7 @@ void wins_write_database(time_t t, BOOL background)
                if (tdb_reopen(wins_tdb)) {
                        DEBUG(0,("wins_write_database: tdb_reopen failed. Error was %s\n",
                                strerror(errno)));
+                       _exit(0);
                        return;
                }
        }
@@ -2348,14 +2370,17 @@ void wins_write_database(time_t t, BOOL background)
  Process a internal Samba message receiving a wins record.
 ***************************************************************************/
 
-void nmbd_wins_new_entry(int msg_type, struct process_id src,
-                        void *buf, size_t len)
+void nmbd_wins_new_entry(struct messaging_context *msg,
+                                       void *private_data,
+                                       uint32_t msg_type,
+                                       struct server_id server_id,
+                                       DATA_BLOB *data)
 {
        WINS_RECORD *record;
        struct name_record *namerec = NULL;
        struct name_record *new_namerec = NULL;
        struct nmb_name question;
-       BOOL overwrite=False;
+       bool overwrite=False;
        struct in_addr our_fake_ip = *interpret_addr2("0.0.0.0");
        int i;