s3: Make name_query return NTSTATUS
authorVolker Lendecke <vl@samba.org>
Tue, 28 Dec 2010 12:47:35 +0000 (13:47 +0100)
committerVolker Lendecke <vlendec@samba.org>
Tue, 28 Dec 2010 17:21:05 +0000 (18:21 +0100)
Also use talloc for the result

Autobuild-User: Volker Lendecke <vlendec@samba.org>
Autobuild-Date: Tue Dec 28 18:21:05 CET 2010 on sn-devel-104

nsswitch/wins.c
source3/include/proto.h
source3/libsmb/namequery.c
source3/utils/nmblookup.c
source3/web/diagnose.c
source3/winbindd/winbindd_wins.c

index d7beed5352688204a7f25768141c6d2256d6eead..ac062fe311977ad9c79a360c7306642d70cc53e1 100644 (file)
@@ -140,17 +140,20 @@ static struct in_addr *lookup_byname_backend(const char *name, int *count)
                const struct in_addr *bcast = iface_n_bcast_v4(j);
                struct sockaddr_storage ss;
                struct sockaddr_storage *pss;
+               NTSTATUS status;
+
                if (!bcast) {
                        continue;
                }
                in_addr_to_sockaddr_storage(&ss, *bcast);
-               pss = name_query(fd,name,0x00,True,True,&ss,count, &flags, NULL);
+               status = name_query(fd, name, 0x00, True, True, &ss,
+                                   NULL, &pss, count, &flags, NULL);
                if (pss) {
                        if ((ret = SMB_MALLOC_P(struct in_addr)) == NULL) {
                                return NULL;
                        }
                        *ret = ((struct sockaddr_in *)pss)->sin_addr;
-                       SAFE_FREE(pss);
+                       TALLOC_FREE(pss);
                        break;
                }
        }
index 8278ffbcd5f5560c56568575f33675f4f4daa27a..9b391ae7671216efe58a65c5abc5004b38a2b435 100644 (file)
@@ -2712,12 +2712,14 @@ bool name_status_find(const char *q_name,
                        const struct sockaddr_storage *to_ss,
                        fstring name);
 int ip_service_compare(struct ip_service *ss1, struct ip_service *ss2);
-struct sockaddr_storage *name_query(int fd,
+NTSTATUS name_query(int fd,
                        const char *name,
                        int name_type,
                        bool bcast,
                        bool recurse,
                        const struct sockaddr_storage *to_ss,
+                       TALLOC_CTX *mem_ctx,
+                       struct sockaddr_storage **addrs,
                        int *count,
                        int *flags,
                        bool *timed_out);
index a283038c32f32d06ce1a531b3a8c60146ddedc09..7016f6b091ad69cb73af0d3622add31be802f31f 100644 (file)
@@ -679,12 +679,14 @@ static bool prioritize_ipv4_list(struct ip_service *iplist, int count)
  *timed_out is set if we failed by timing out
 ****************************************************************************/
 
-struct sockaddr_storage *name_query(int fd,
+NTSTATUS name_query(int fd,
                        const char *name,
                        int name_type,
                        bool bcast,
                        bool recurse,
                        const struct sockaddr_storage *to_ss,
+                       TALLOC_CTX *mem_ctx,
+                       struct sockaddr_storage **addrs,
                        int *count,
                        int *flags,
                        bool *timed_out)
@@ -701,11 +703,11 @@ struct sockaddr_storage *name_query(int fd,
        if (lp_disable_netbios()) {
                DEBUG(5,("name_query(%s#%02x): netbios is disabled\n",
                                        name, name_type));
-               return NULL;
+               return NT_STATUS_NOT_FOUND;
        }
 
        if (to_ss->ss_family != AF_INET) {
-               return NULL;
+               return NT_STATUS_INVALID_ADDRESS;
        }
 
        if (timed_out) {
@@ -745,7 +747,7 @@ struct sockaddr_storage *name_query(int fd,
        clock_gettime_mono(&tp);
 
        if (!send_packet_request(&p))
-               return NULL;
+               return NT_STATUS_NOT_FOUND;
 
        retries--;
 
@@ -757,7 +759,7 @@ struct sockaddr_storage *name_query(int fd,
                        if (!retries)
                                break;
                        if (!found && !send_packet_request(&p))
-                               return NULL;
+                               return NT_STATUS_NOT_FOUND;
                        clock_gettime_mono(&tp);
                        retries--;
                }
@@ -806,7 +808,7 @@ struct sockaddr_storage *name_query(int fd,
                                        }
                                }
                                free_packet(p2);
-                               return( NULL );
+                               return NT_STATUS_NOT_FOUND;
                        }
 
                        if (nmb2->header.opcode != 0 ||
@@ -822,7 +824,7 @@ struct sockaddr_storage *name_query(int fd,
                                continue;
                        }
 
-                       ss_list = SMB_REALLOC_ARRAY(ss_list,
+                       ss_list = TALLOC_REALLOC_ARRAY(mem_ctx, ss_list,
                                                struct sockaddr_storage,
                                                (*count) +
                                                nmb2->answers->rdlength/6);
@@ -830,7 +832,7 @@ struct sockaddr_storage *name_query(int fd,
                        if (!ss_list) {
                                DEBUG(0,("name_query: Realloc failed.\n"));
                                free_packet(p2);
-                               return NULL;
+                               return NT_STATUS_NO_MEMORY;
                        }
 
                        DEBUG(2,("Got a positive name query response "
@@ -882,7 +884,8 @@ struct sockaddr_storage *name_query(int fd,
        /* sort the ip list so we choose close servers first if possible */
        sort_addr_list(ss_list, *count);
 
-       return ss_list;
+       *addrs = ss_list;
+       return NT_STATUS_OK;
 }
 
 /********************************************************
@@ -953,7 +956,7 @@ NTSTATUS name_resolve_bcast(const char *name,
 
        sock = open_socket_in( SOCK_DGRAM, 0, 3, &ss, true );
        if (sock == -1) {
-               return NT_STATUS_UNSUCCESSFUL;
+               return map_nt_error_from_unix(errno);
        }
 
        set_socket_options(sock,"SO_BROADCAST");
@@ -969,9 +972,10 @@ NTSTATUS name_resolve_bcast(const char *name,
                if (!pss) {
                        continue;
                }
-               ss_list = name_query(sock, name, name_type, true,
-                                   true, pss, return_count, &flags, NULL);
-               if (ss_list) {
+               status = name_query(sock, name, name_type, true, true, pss,
+                                   talloc_tos(), &ss_list, return_count,
+                                   &flags, NULL);
+               if (NT_STATUS_IS_OK(status)) {
                        goto success;
                }
        }
@@ -979,15 +983,14 @@ NTSTATUS name_resolve_bcast(const char *name,
        /* failed - no response */
 
        close(sock);
-       return NT_STATUS_UNSUCCESSFUL;
+       return status;
 
 success:
 
-       status = NT_STATUS_OK;
        if (!convert_ss2service(return_iplist, ss_list, *return_count) )
-               status = NT_STATUS_INVALID_PARAMETER;
+               status = NT_STATUS_NO_MEMORY;
 
-       SAFE_FREE(ss_list);
+       TALLOC_FREE(ss_list);
        close(sock);
        return status;
 }
@@ -1083,20 +1086,23 @@ NTSTATUS resolve_wins(const char *name,
                        }
 
                        in_addr_to_sockaddr_storage(&wins_ss, wins_ip);
-                       ss_list = name_query(sock,
+                       status = name_query(sock,
                                                name,
                                                name_type,
                                                false,
                                                true,
                                                &wins_ss,
+                                               talloc_tos(),
+                                               &ss_list,
                                                return_count,
                                                &flags,
                                                &timed_out);
 
                        /* exit loop if we got a list of addresses */
 
-                       if (ss_list)
+                       if (NT_STATUS_IS_OK(status)) {
                                goto success;
+                       }
 
                        close(sock);
 
@@ -1123,7 +1129,7 @@ success:
        if (!convert_ss2service(return_iplist, ss_list, *return_count))
                status = NT_STATUS_INVALID_PARAMETER;
 
-       SAFE_FREE(ss_list);
+       TALLOC_FREE(ss_list);
        wins_srv_tags_free(wins_tags);
        close(sock);
 
index 5bbd06c99408c65e87312f110c7b813b607f11e6..4a55647361492ad7cb4044a4a7b964e28a3d4148 100644 (file)
@@ -156,14 +156,16 @@ static bool query_one(const char *lookup, unsigned int lookup_type)
 {
        int j, count, flags = 0;
        struct sockaddr_storage *ip_list=NULL;
+       NTSTATUS status;
 
        if (got_bcast) {
                char addr[INET6_ADDRSTRLEN];
                print_sockaddr(addr, sizeof(addr), &bcast_addr);
                d_printf("querying %s on %s\n", lookup, addr);
-               ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
-                                    use_bcast?true:recursion_desired,
-                                    &bcast_addr, &count, &flags, NULL);
+               status = name_query(ServerFD,lookup,lookup_type,use_bcast,
+                                   use_bcast?true:recursion_desired,
+                                   &bcast_addr, talloc_tos(),
+                                   &ip_list, &count, &flags, NULL);
        } else {
                const struct in_addr *bcast;
                for (j=iface_count() - 1;
@@ -180,14 +182,15 @@ static bool query_one(const char *lookup, unsigned int lookup_type)
                        print_sockaddr(addr, sizeof(addr), &bcast_ss);
                        d_printf("querying %s on %s\n",
                               lookup, addr);
-                       ip_list = name_query(ServerFD,lookup,lookup_type,
-                                            use_bcast,
-                                            use_bcast?True:recursion_desired,
-                                            &bcast_ss,&count, &flags, NULL);
+                       status = name_query(ServerFD,lookup,lookup_type,
+                                           use_bcast,
+                                           use_bcast?True:recursion_desired,
+                                           &bcast_ss, talloc_tos(),
+                                           &ip_list, &count, &flags, NULL);
                }
        }
 
-       if (!ip_list) {
+       if (!NT_STATUS_IS_OK(status)) {
                return false;
        }
 
@@ -220,9 +223,9 @@ static bool query_one(const char *lookup, unsigned int lookup_type)
                }
        }
 
-       free(ip_list);
+       TALLOC_FREE(ip_list);
 
-       return (ip_list != NULL);
+       return NT_STATUS_IS_OK(status);
 }
 
 
index 221ce83f82780863c3248522d059d20ad241bea0..df61658f8d71537af178d5e026ac5682a054698c 100644 (file)
@@ -44,10 +44,12 @@ bool nmbd_running(void)
 
        if ((fd = open_socket_in(SOCK_DGRAM, 0, 3,
                                 &ss, True)) != -1) {
-               if ((ss_list = name_query(fd, "__SAMBA__", 0, 
-                                         True, True, &ss,
-                                         &count, &flags, NULL)) != NULL) {
-                       SAFE_FREE(ss_list);
+               NTSTATUS status = name_query(fd, "__SAMBA__", 0,
+                                            True, True, &ss,
+                                            talloc_tos(), &ss_list, &count,
+                                            &flags, NULL);
+               if (NT_STATUS_IS_OK(status)) {
+                       TALLOC_FREE(ss_list);
                        close(fd);
                        return True;
                }
index 484b39342a629c8faf1ebcc7fdd3a1f4457ca147..720feaf83a45032fd87acc0a9065dac59d2d267d 100644 (file)
@@ -96,13 +96,15 @@ static struct node_status *lookup_byaddr_backend(TALLOC_CTX *mem_ctx,
        return result;
 }
 
-static struct sockaddr_storage *lookup_byname_backend(const char *name,
-                                       int *count)
+static struct sockaddr_storage *lookup_byname_backend(TALLOC_CTX *mem_ctx,
+                                                     const char *name,
+                                                     int *count)
 {
        int fd;
        struct ip_service *ret = NULL;
        struct sockaddr_storage *return_ss = NULL;
        int j, i, flags = 0;
+       NTSTATUS status;
 
        *count = 0;
 
@@ -110,7 +112,9 @@ static struct sockaddr_storage *lookup_byname_backend(const char *name,
        if (NT_STATUS_IS_OK(resolve_wins(name,0x20,&ret,count))) {
                if ( *count == 0 )
                        return NULL;
-               if ( (return_ss = SMB_MALLOC_ARRAY(struct sockaddr_storage, *count)) == NULL ) {
+               return_ss = TALLOC_ARRAY(mem_ctx, struct sockaddr_storage,
+                                        *count);
+               if (return_ss == NULL ) {
                        free( ret );
                        return NULL;
                }
@@ -136,8 +140,9 @@ static struct sockaddr_storage *lookup_byname_backend(const char *name,
                if (!bcast_ss) {
                        continue;
                }
-               return_ss = name_query(fd,name,0x20,True,True,bcast_ss,count, &flags, NULL);
-               if (return_ss) {
+               status = name_query(fd, name, 0x20, True, True,bcast_ss,
+                                   mem_ctx, &return_ss, count, &flags, NULL);
+               if (NT_STATUS_IS_OK(status)) {
                        break;
                }
        }
@@ -213,12 +218,14 @@ void winbindd_wins_byname(struct winbindd_cli_state *state)
        *response = '\0';
        maxlen = sizeof(response) - 1;
 
-       if ((ip_list = lookup_byname_backend(state->request->data.winsreq,&count))){
+       ip_list = lookup_byname_backend(
+               state->mem_ctx, state->request->data.winsreq, &count);
+       if (ip_list != NULL){
                for (i = count; i ; i--) {
                        print_sockaddr(addr, sizeof(addr), &ip_list[i-1]);
                        size = strlen(addr);
                        if (size > maxlen) {
-                               SAFE_FREE(ip_list);
+                               TALLOC_FREE(ip_list);
                                request_error(state);
                                return;
                        }
@@ -235,13 +242,13 @@ void winbindd_wins_byname(struct winbindd_cli_state *state)
                }
                size = strlen(state->request->data.winsreq) + strlen(response);
                if (size > maxlen) {
-                   SAFE_FREE(ip_list);
+                   TALLOC_FREE(ip_list);
                    request_error(state);
                    return;
                }
                fstrcat(response,state->request->data.winsreq);
                fstrcat(response,"\n");
-               SAFE_FREE(ip_list);
+               TALLOC_FREE(ip_list);
        } else {
                request_error(state);
                return;