r23779: Change from v2 or later to v3 or later.
[amitay/samba.git] / source3 / nmbd / nmbd_nameregister.c
index 4ac5473d4ae69c958769297fb3e156698caf114f..fedf2ae9e4d4d9b41e75358fc561668e0c4d202d 100644 (file)
@@ -3,11 +3,11 @@
    NBT netbios routines and daemon - version 2
    Copyright (C) Andrew Tridgell 1994-1998
    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
-   Copyright (C) Jeremy Allison 1994-1998
+   Copyright (C) Jeremy Allison 1994-2003
    
    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,
@@ -23,8 +23,6 @@
 
 #include "includes.h"
 
-extern fstring global_myworkgroup;
-
 /* forward declarations */
 static void wins_next_registration(struct response_record *rrec);
 
@@ -87,7 +85,9 @@ static void register_name_response(struct subnet_record *subrec,
                 */
                
 #if 1 /* OLD_SAMBA_SERVER_HACK */
-               if((nmb->header.rcode == ACT_ERR) && strequal(global_myworkgroup, answer_name->name) &&
+               unstring ans_name;
+               pull_ascii_nstring(ans_name, sizeof(ans_name), answer_name->name);
+               if((nmb->header.rcode == ACT_ERR) && strequal(lp_workgroup(), ans_name) &&
                   (answer_name->name_type == 0x1b)) {
                        /* Pretend we did not get this. */
                        rrec->num_msgs--;
@@ -106,17 +106,7 @@ static void register_name_response(struct subnet_record *subrec,
                success = False;
        } else {
                /* Unicast - check to see if the response allows us to have the name. */
-               if(nmb->header.rcode != 0) {
-                       /* Error code - we didn't get the name. */
-                       success = False;
-
-                       DEBUG(0,("register_name_response: %sserver at IP %s rejected our name registration of %s IP %s with error code %d.\n", 
-                                subrec==unicast_subnet?"WINS ":"",
-                                inet_ntoa(p->ip), 
-                                nmb_namestr(answer_name), 
-                                reg_name,
-                                nmb->header.rcode));
-               } else if (nmb->header.opcode == NMB_WACK_OPCODE) {
+               if (nmb->header.opcode == NMB_WACK_OPCODE) {
                        /* WINS server is telling us to wait. Pretend we didn't get
                           the response but don't send out any more register requests. */
 
@@ -128,6 +118,16 @@ static void register_name_response(struct subnet_record *subrec,
                        rrec->repeat_time = p->timestamp + nmb->answers->ttl;
                        rrec->num_msgs--;
                        return;
+               } else if (nmb->header.rcode != 0) {
+                       /* Error code - we didn't get the name. */
+                       success = False;
+
+                       DEBUG(0,("register_name_response: %sserver at IP %s rejected our name registration of %s IP %s with error code %d.\n", 
+                                subrec==unicast_subnet?"WINS ":"",
+                                inet_ntoa(p->ip), 
+                                nmb_namestr(answer_name), 
+                                reg_name,
+                                nmb->header.rcode));
                } else {
                        success = True;
                        /* Get the data we need to pass to the success function. */
@@ -163,10 +163,10 @@ static void register_name_response(struct subnet_record *subrec,
        remove_response_record(subrec, rrec);
 }
 
-
 /****************************************************************************
  Deal with a timeout of a WINS registration request
 ****************************************************************************/
+
 static void wins_registration_timeout(struct subnet_record *subrec,
                                      struct response_record *rrec)
 {
@@ -235,7 +235,6 @@ static void wins_registration_timeout(struct subnet_record *subrec,
           us trying to register with each of our failover wins servers */
 }
 
-
 /****************************************************************************
  Deal with a timeout when registering one of our names.
 ****************************************************************************/
@@ -292,10 +291,10 @@ static void register_name_timeout_response(struct subnet_record *subrec,
        remove_response_record(subrec, rrec);
 }
 
-
 /****************************************************************************
-initiate one multi-homed name registration packet
+ Initiate one multi-homed name registration packet.
 ****************************************************************************/
+
 static void multihomed_register_one(struct nmb_name *nmbname,
                                    uint16 nb_flags,
                                    register_name_success_function success_fn,
@@ -307,7 +306,7 @@ static void multihomed_register_one(struct nmb_name *nmbname,
        struct in_addr wins_ip = wins_srv_ip_tag(tag, ip);
        fstring ip_str;
 
-       userdata = (struct userdata_struct *)malloc(sizeof(*userdata) + strlen(tag) + 1);
+       userdata = (struct userdata_struct *)SMB_MALLOC(sizeof(*userdata) + strlen(tag) + 1);
        if (!userdata) {
                DEBUG(0,("Failed to allocate userdata structure!\n"));
                return;
@@ -338,11 +337,11 @@ static void multihomed_register_one(struct nmb_name *nmbname,
        free(userdata);
 }
 
-
 /****************************************************************************
-we have finished the registration of one IP and need to see if we have
-any more IPs left to register with this group of wins server for this name
+ We have finished the registration of one IP and need to see if we have
+ any more IPs left to register with this group of wins server for this name.
 ****************************************************************************/
+
 static void wins_next_registration(struct response_record *rrec)
 {
        struct nmb_packet *sent_nmb = &rrec->packet->packet.nmb;
@@ -390,6 +389,7 @@ static void wins_next_registration(struct response_record *rrec)
 /****************************************************************************
  Try and register one of our names on the unicast subnet - multihomed.
 ****************************************************************************/
+
 static void multihomed_register_name(struct nmb_name *nmbname, uint16 nb_flags,
                                     register_name_success_function success_fn,
                                     register_name_fail_function fail_fn)
@@ -418,11 +418,12 @@ static void multihomed_register_name(struct nmb_name *nmbname, uint16 nb_flags,
        struct subnet_record *subrec;
        char **wins_tags;
        struct in_addr *ip_list;
+       unstring name;
 
        for(subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec) )
                num_ips++;
        
-       if((ip_list = (struct in_addr *)malloc(num_ips * sizeof(struct in_addr)))==NULL) {
+       if((ip_list = SMB_MALLOC_ARRAY(struct in_addr, num_ips))==NULL) {
                DEBUG(0,("multihomed_register_name: malloc fail !\n"));
                return;
        }
@@ -433,7 +434,8 @@ static void multihomed_register_name(struct nmb_name *nmbname, uint16 nb_flags,
                ip_list[i] = subrec->myip;
        }
 
-       add_name_to_subnet(unicast_subnet, nmbname->name, nmbname->name_type,
+       pull_ascii_nstring(name, sizeof(name), nmbname->name);
+       add_name_to_subnet(unicast_subnet, name, nmbname->name_type,
                           nb_flags, lp_max_ttl(), SELF_NAME,
                           num_ips, ip_list);
 
@@ -458,19 +460,30 @@ static void multihomed_register_name(struct nmb_name *nmbname, uint16 nb_flags,
        SAFE_FREE(ip_list);
 }
 
-
 /****************************************************************************
  Try and register one of our names.
 ****************************************************************************/
+
 void register_name(struct subnet_record *subrec,
-                   char *name, int type, uint16 nb_flags,
+                   const char *name, int type, uint16 nb_flags,
                    register_name_success_function success_fn,
                    register_name_fail_function fail_fn,
                    struct userdata_struct *userdata)
 {
        struct nmb_name nmbname;
-       
-       make_nmb_name(&nmbname, name, type);
+       nstring nname;
+
+       errno = 0;
+       push_ascii_nstring(nname, name);
+        if (errno == E2BIG) {
+               unstring tname;
+               pull_ascii_nstring(tname, sizeof(tname), nname);
+               DEBUG(0,("register_name: NetBIOS name %s is too long. Truncating to %s\n",
+                       name, tname));
+               make_nmb_name(&nmbname, tname, type);
+       } else {
+               make_nmb_name(&nmbname, name, type);
+       }
 
        /* Always set the NB_ACTIVE flag on the name we are
           registering. Doesn't make sense without it.
@@ -500,10 +513,10 @@ void register_name(struct subnet_record *subrec,
        }
 }
 
-
 /****************************************************************************
  Try and refresh one of our names. This is *only* called for WINS refresh
 ****************************************************************************/
+
 void wins_refresh_name(struct name_record *namerec)
 {
        int t;