s3-talloc Change TALLOC_ARRAY() to talloc_array()
[samba.git] / source3 / winbindd / winbindd_wins.c
index ea385a640607733f10a8cf0e13a912f516b6ef75..7ed330f81bcc4e127a51858664be278f04b3152a 100644 (file)
 
 #include "includes.h"
 #include "winbindd.h"
+#include "libsmb/nmblib.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
 
-/* Use our own create socket code so we don't recurse.... */
-
-static int wins_lookup_open_socket_in(void)
-{
-       struct sockaddr_in sock;
-       int val=1;
-       int res;
-
-       memset((char *)&sock,'\0',sizeof(sock));
-
-#ifdef HAVE_SOCK_SIN_LEN
-       sock.sin_len = sizeof(sock);
-#endif
-       sock.sin_port = 0;
-       sock.sin_family = AF_INET;
-       sock.sin_addr.s_addr = interpret_addr("0.0.0.0");
-       res = socket(AF_INET, SOCK_DGRAM, 0);
-       if (res == -1)
-               return -1;
-
-       if (setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val))) {
-               close(res);
-               return -1;
-       }
-#ifdef SO_REUSEPORT
-       if (setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val))) {
-               close(res);
-               return -1;
-       }
-#endif /* SO_REUSEPORT */
-
-       /* now we've got a socket - we need to bind it */
-
-       if (bind(res, (struct sockaddr *)(void *)&sock, sizeof(sock)) < 0) {
-               close(res);
-               return(-1);
-       }
-
-       set_socket_options(res,"SO_BROADCAST");
-
-       return res;
-}
-
-
-static struct node_status *lookup_byaddr_backend(TALLOC_CTX *mem_ctx,
-                                                const char *addr, int *count)
-{
-       struct sockaddr_storage ss;
-       struct nmb_name nname;
-       struct node_status *result;
-       NTSTATUS status;
-
-       make_nmb_name(&nname, "*", 0);
-       if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
-               return NULL;
-       }
-       status = node_status_query(mem_ctx, &nname, &ss,
-                                  &result, count, NULL);
-       if (!NT_STATUS_IS_OK(status)) {
-               return NULL;
-       }
-       return result;
-}
-
 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;
+       int j, i;
        NTSTATUS status;
 
        *count = 0;
@@ -106,7 +42,7 @@ static struct sockaddr_storage *lookup_byname_backend(TALLOC_CTX *mem_ctx,
        if (NT_STATUS_IS_OK(resolve_wins(name,0x20,&ret,count))) {
                if ( *count == 0 )
                        return NULL;
-               return_ss = TALLOC_ARRAY(mem_ctx, struct sockaddr_storage,
+               return_ss = talloc_array(mem_ctx, struct sockaddr_storage,
                                         *count);
                if (return_ss == NULL ) {
                        free( ret );
@@ -121,11 +57,6 @@ static struct sockaddr_storage *lookup_byname_backend(TALLOC_CTX *mem_ctx,
                return return_ss;
        }
 
-       fd = wins_lookup_open_socket_in();
-       if (fd == -1) {
-               return NULL;
-       }
-
        /* uggh, we have to broadcast to each interface in turn */
        for (j=iface_count() - 1;
             j >= 0;
@@ -134,66 +65,16 @@ static struct sockaddr_storage *lookup_byname_backend(TALLOC_CTX *mem_ctx,
                if (!bcast_ss) {
                        continue;
                }
-               status = name_query(fd, name, 0x20, True, True,bcast_ss,
-                                   mem_ctx, &return_ss, count, &flags, NULL);
+               status = name_query(name, 0x20, True, True,bcast_ss,
+                                   mem_ctx, &return_ss, count, NULL);
                if (NT_STATUS_IS_OK(status)) {
                        break;
                }
        }
 
-       close(fd);
        return return_ss;
 }
 
-/* Get hostname from IP  */
-
-void winbindd_wins_byip(struct winbindd_cli_state *state)
-{
-       fstring response;
-       int i, count, maxlen, size;
-       struct node_status *status;
-
-       /* Ensure null termination */
-       state->request->data.winsreq[sizeof(state->request->data.winsreq)-1]='\0';
-
-       DEBUG(3, ("[%5lu]: wins_byip %s\n", (unsigned long)state->pid,
-               state->request->data.winsreq));
-
-       *response = '\0';
-       maxlen = sizeof(response) - 1;
-
-       if ((status = lookup_byaddr_backend(
-                    state->mem_ctx, state->request->data.winsreq, &count))) {
-           size = strlen(state->request->data.winsreq);
-           if (size > maxlen) {
-               TALLOC_FREE(status);
-               request_error(state);
-               return;
-           }
-           fstrcat(response,state->request->data.winsreq);
-           fstrcat(response,"\t");
-           for (i = 0; i < count; i++) {
-               /* ignore group names */
-               if (status[i].flags & 0x80) continue;
-               if (status[i].type == 0x20) {
-                       size = sizeof(status[i].name) + strlen(response);
-                       if (size > maxlen) {
-                           TALLOC_FREE(status);
-                           request_error(state);
-                           return;
-                       }
-                       fstrcat(response, status[i].name);
-                       fstrcat(response, " ");
-               }
-           }
-           /* make last character a newline */
-           response[strlen(response)-1] = '\n';
-           TALLOC_FREE(status);
-       }
-       fstrcpy(state->response->data.winsresp,response);
-       request_ok(state);
-}
-
 /* Get IP from hostname */
 
 void winbindd_wins_byname(struct winbindd_cli_state *state)
@@ -231,8 +112,8 @@ void winbindd_wins_byname(struct winbindd_cli_state *state)
                                        response[strlen(response)-1] = ' ';
                                }
                        }
-                       fstrcat(response,addr);
-                       fstrcat(response,"\t");
+                       strlcat(response,addr,sizeof(response));
+                       strlcat(response,"\t",sizeof(response));
                }
                size = strlen(state->request->data.winsreq) + strlen(response);
                if (size > maxlen) {
@@ -240,15 +121,17 @@ void winbindd_wins_byname(struct winbindd_cli_state *state)
                    request_error(state);
                    return;
                }
-               fstrcat(response,state->request->data.winsreq);
-               fstrcat(response,"\n");
+               strlcat(response,state->request->data.winsreq,sizeof(response));
+               strlcat(response,"\n",sizeof(response));
                TALLOC_FREE(ip_list);
        } else {
                request_error(state);
                return;
        }
 
-       fstrcpy(state->response->data.winsresp,response);
+       strlcpy(state->response->data.winsresp,
+               response,
+               sizeof(state->response->data.winsresp));
 
        request_ok(state);
 }